UNPKG

@equinor/fusion-framework-cli

Version:

--- title: Fusion Framework CLI ---

89 lines 3.62 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; /** * Preserce token for executing proxy assets * * @remarks * This assumes the client will execute a api call using bearer token before * acquiring a asset. By default the Framework will execute a rest call to load * application manufest for resolving build assets to import * * @remarks * This is a quick and dirty method to authorize requests without bearer token * like browser `import`. * The correct way would be to have a auth controller within the dev-server, * but since the token is only exposed to the plugin and the cli is a tool for * local development, this should be sufficient. */ let __HELP_API_TOKEN__ = ''; /** * The `helpProxyPlugin` function creates a Vite plugin that configures a proxy * for assets API requests to the Fusion Help Service. * * @param {HelpProxyPluginOptions} options - The options for configuring the help proxy plugin. * * @returns {Plugin} - The configured Vite plugin. * * @example * ```typescript * const plugin = helpProxyPlugin({ * proxy: { * path: '/help-proxy', * target: 'https://help.ci.api.fusion-dev.net', * onProxyReq: (proxyReq, req, res) => { * proxyReq.on('response', (res) => { console.log(res.statusCode) }); * }, * }, * }); * * // Fetch a resource through the proxy * fetch('/help-proxy/assets/resources/images/image.jpg'); * ``` */ export const helpProxyPlugin = (options) => { const { proxy: { onProxyReq = () => void 0, path: proxyPath, target }, } = options; return { name: 'fusion:help-proxy', apply: 'serve', config(config) { var _a, _b; var _c; (_a = config.server) !== null && _a !== void 0 ? _a : (config.server = {}); (_b = (_c = config.server).proxy) !== null && _b !== void 0 ? _b : (_c.proxy = {}); config.server.proxy[proxyPath] = { target, changeOrigin: true, secure: false, rewrite: (path) => path.replace(proxyPath, ''), configure: (proxy) => { proxy.on('proxyReq', (proxyReq) => { if (__HELP_API_TOKEN__) { // apply token to proxy request proxyReq.setHeader('authorization', __HELP_API_TOKEN__); } }); proxy.on('proxyReq', onProxyReq); }, }; }, configureServer(server) { server.middlewares.use(proxyPath, (req, res, next) => __awaiter(this, void 0, void 0, function* () { if (req.headers.authorization) { __HELP_API_TOKEN__ = req.headers.authorization || ''; res.end(); return; } next(); })); }, }; }; export default helpProxyPlugin; //# sourceMappingURL=help-proxy-plugin.js.map