From 71d57c2955861e86f8026068fb7cd4fe39b195f6 Mon Sep 17 00:00:00 2001 From: Odin Thomas Rochmann Date: Wed, 13 Nov 2024 11:06:37 +0100 Subject: [PATCH] feat: add configurable paths for app proxy plugin (#2567) --- .changeset/tidy-buttons-clean.md | 7 +++ packages/cli/src/bin/create-dev-serve.ts | 1 + .../lib/plugins/app-proxy/app-proxy-plugin.ts | 47 +++++++++++++++---- 3 files changed, 47 insertions(+), 8 deletions(-) create mode 100644 .changeset/tidy-buttons-clean.md diff --git a/.changeset/tidy-buttons-clean.md b/.changeset/tidy-buttons-clean.md new file mode 100644 index 0000000000..0e4694618e --- /dev/null +++ b/.changeset/tidy-buttons-clean.md @@ -0,0 +1,7 @@ +--- +'@equinor/fusion-framework-cli': minor +--- + +Added functionality for allow providing paths for `config`, `manifest` and bundle in `app-proxy-plugin`. This is useful for plugins that need to load resources from a specific path. + +This was required since the AppClient was changed in [#2520](https://github.com/equinor/fusion-framework/pull/2520) which broke the ability to load resources from the plugin. diff --git a/packages/cli/src/bin/create-dev-serve.ts b/packages/cli/src/bin/create-dev-serve.ts index 122643f8cd..3a3e4db6dc 100644 --- a/packages/cli/src/bin/create-dev-serve.ts +++ b/packages/cli/src/bin/create-dev-serve.ts @@ -111,6 +111,7 @@ export const createDevServer = async (options: { version: String(pkg.packageJson.version), generateConfig, generateManifest, + manifestPath: `persons/me/apps/${appKey}`, }, }), // Restart the server when config changes or the dev portal source is updated diff --git a/packages/cli/src/lib/plugins/app-proxy/app-proxy-plugin.ts b/packages/cli/src/lib/plugins/app-proxy/app-proxy-plugin.ts index 611c3ba0a7..2e5c275585 100644 --- a/packages/cli/src/lib/plugins/app-proxy/app-proxy-plugin.ts +++ b/packages/cli/src/lib/plugins/app-proxy/app-proxy-plugin.ts @@ -5,6 +5,7 @@ import { AppManifest } from '@equinor/fusion-framework-app'; import { ClientRequest, IncomingMessage, ServerResponse } from 'node:http'; import { ApiAppConfig } from '../../../schemas.js'; +import { join } from 'node:path'; /** * Preserve token for executing proxy assets @@ -47,8 +48,30 @@ export type AppProxyPluginOptions = { version: string; /** callback function for generating configuration for the application */ generateConfig: () => Promise; + + /** + * string path to the app config + * @example `persons/me/apps/${app.key}/builds/${app.version}/config` + * @default `apps/${app.key}/builds/${app.version}/config` + */ + configPath?: string; + /** callback function for generating manifest for the application */ generateManifest: () => Promise; + + /** + * string path to the app manifest + * @example `persons/me/apps/${app.key}` + * @default `apps/${app.key}` + */ + manifestPath?: string; + + /** + * string path to the app bundle + * @default `bundles/apps/${app.key}/${app.version}` + * @example `bundles/apps/${app.key}/${app.version}` + */ + bundlePath?: string; }; }; @@ -128,22 +151,30 @@ export const appProxyPlugin = (options: AppProxyPluginOptions): Plugin => { if (!app) return; // serve app config if request matches the current app and version - const configPath = `${proxyPath}/apps/${app.key}/builds/${app.version}/config`; + const configPath = join( + proxyPath, + app.configPath ?? `apps/${app.key}/builds/${app.version}/config`, + ); server.middlewares.use(configPath, async (_req, res) => { res.setHeader('content-type', 'application/json'); res.end(JSON.stringify(await app.generateConfig())); }); // serve app manifest if request matches the current app - // todo this should have version - const manifestPath = `${proxyPath}/apps/${app.key}`; - server.middlewares.use(manifestPath, async (_req, res) => { - res.setHeader('content-type', 'application/json'); - res.end(JSON.stringify(await app.generateManifest())); - }); + const manifestPath = join(proxyPath, app.manifestPath ?? `apps/${app.key}`); + server.middlewares.use( + [proxyPath, manifestPath, app.key].join('/'), + async (_req, res) => { + res.setHeader('content-type', 'application/json'); + res.end(JSON.stringify(await app.generateManifest())); + }, + ); // serve local bundles if request matches the current app and version - const bundlePath = `${proxyPath}/bundles/apps/${app.key}/${app.version}`; + const bundlePath = join( + proxyPath, + app.bundlePath ?? `bundles/apps/${app.key}/${app.version}`, + ); server.middlewares.use(async (req, _res, next) => { if (req.url?.match(bundlePath)) { // remove proxy path from url