dashboard/shell/plugins/internal-api/index.ts

38 lines
1.1 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { Store } from 'vuex';
interface PluginContext {
store: Store<any>;
[key: string]: any;
}
export default function(context: PluginContext, inject: (key: string, value: any) => void) {
const { store } = context;
// Load all API modules
const apiContext = (require as any).context(
'@shell/plugins/internal-api', // the base directory
true, // whether to search subdirectories
/\.api\.ts$/ // only .api.ts files
);
apiContext.keys().forEach((relativePath: string) => {
const mod = apiContext(relativePath);
const ApiClass = mod.default;
if (typeof ApiClass === 'function') {
// Check for a static `apiName` property, or fallback to filename
let apiName: string = ApiClass.apiName();
if (!apiName) {
// fallback to filename (strip leading ./ and extension)
apiName = `$${ relativePath.replace(/^\.\//, '').replace(/\.\w+$/, '') }`;
}
const instance = new ApiClass(store);
// The inject() method automatically adds the `$` prefix
inject(apiName, instance);
}
});
}