Skip to content

A platform for community discussion. Free, open, simple.

DEV: Improve types for `withPluginApi` and `apiInitializer` (#35338)

David Taylorac48afd84fd3
diff --git a/app/assets/javascripts/discourse/app/lib/api.js b/app/assets/javascripts/discourse/app/lib/api.js
index 51a933f67ad..33525a229d0 100644
--- a/app/assets/javascripts/discourse/app/lib/api.js
+++ b/app/assets/javascripts/discourse/app/lib/api.js
@@ -1,18 +1,24 @@
+// @ts-check
import { withPluginApi } from "discourse/lib/plugin-api";
+let _apiInitializerId = 0;
+
/**
- * apiInitializer(version, apiCodeCallback, opts)
+ * Define an initializer which will execute a callback with a PluginApi object.
*
- * An API to simplify the creation of initializers for plugins/themes by removing
- * some of the boilerplate.
+ * @param {(api: import("discourse/lib/plugin-api").PluginApi, opts: object) => any} apiCodeCallback - The callback function to execute
+ * @param {object} [opts] - Optional additional options to pass to the callback function.
*/
-let _apiInitializerId = 0;
-export function apiInitializer(version, cb, opts) {
+export function apiInitializer(apiCodeCallback, opts) {
+ if (typeof arguments[0] === "string") {
+ // Old path. First argument is the version string. Silently ignore.
+ [, apiCodeCallback, opts] = arguments;
+ }
return {
name: `api-initializer${_apiInitializerId++}`,
after: "inject-objects",
initialize() {
- return withPluginApi(version, cb, opts);
+ return withPluginApi(apiCodeCallback, opts);
},
};
}
diff --git a/app/assets/javascripts/discourse/app/lib/plugin-api.gjs b/app/assets/javascripts/discourse/app/lib/plugin-api.gjs
index 2c7d05ec513..83fef571680 100644
--- a/app/assets/javascripts/discourse/app/lib/plugin-api.gjs
+++ b/app/assets/javascripts/discourse/app/lib/plugin-api.gjs
@@ -239,6 +239,9 @@ function wrapWithErrorHandler(func, messageKey) {
};
}
+/**
+ * @typedef {PluginApi} PluginApi
+ */
class PluginApi {
constructor(container) {
this.container = container;
@@ -3465,18 +3468,14 @@ function getPluginApi() {
/**
* Executes the provided callback function with the `PluginApi` object.
*
- * @param {(api: PluginApi, opts: object) => void} apiCodeCallback - The callback function to execute
+ * @param {(api: PluginApi, opts: object) => any} apiCodeCallback - The callback function to execute
* @param {object} [opts] - Optional additional options to pass to the callback function.
- * @returns {*} The result of the `callback` function, if executed
+ * @returns {any} The result of the `callback` function, if executed
*/
-export function withPluginApi(...args) {
- let apiCodeCallback, opts;
- if (typeof args[0] === "function") {
- // Modern path. First argument is the callback
- [apiCodeCallback, opts] = args;
- } else {
+export function withPluginApi(apiCodeCallback, opts) {
+ if (typeof arguments[0] === "string") {
// Old path. First argument is the version string. Silently ignore.
- [, apiCodeCallback, opts] = args;
+ [, apiCodeCallback, opts] = arguments;
}
opts = opts || {};