UNPKG

@equinor/fusion-framework-vite-plugin-api-service

Version:

Vite plugin for proxying service discovery and mocking

87 lines (86 loc) 3.2 kB
import type { Plugin } from 'vite'; import { type ProcessRouteOptions } from './process-route.js'; import type { ApiProxyHandler, ApiRoute, PluginLogger } from './types.js'; /** * Arguments accepted by the {@link plugin} factory. * * At least one of `proxyHandler` or `routes` must be provided. * Supply `proxyHandler` to enable service discovery proxying, and/or * `routes` to register custom middleware or proxy route definitions. */ export type PluginArguments = { /** Proxy handler created via {@link createProxyHandler} for service discovery. */ proxyHandler?: ApiProxyHandler; /** Custom API route definitions (middleware or proxy). */ routes?: ApiRoute[]; } & ({ proxyHandler: ApiProxyHandler; routes?: ApiRoute[]; } | { routes: ApiRoute[]; proxyHandler?: ApiProxyHandler; }); /** * Optional configuration for the API service plugin. * * Controls the base middleware path, route processing behaviour, and logging. */ export type PluginOptions = { /** * Base router path where plugin middleware is mounted. * @default DEFAULT_VALUES.API_PATH ('/@fusion-api') */ route?: string; /** * Additional options forwarded to {@link processRoutes} (excluding `logger`, * which is set via the top-level `logger` option). */ process?: Omit<ProcessRouteOptions, 'logger'>; /** * Logger used for debug, info, warning, and error messages emitted by the * plugin. Pass `console` for quick debugging. */ logger?: PluginLogger; }; /** * Creates a Vite plugin that proxies API requests and serves custom routes * during Fusion Framework application development. * * Use this plugin to wire up service discovery proxying, define middleware * routes for mocked responses, or both. * * @param args - Plugin arguments specifying the routes and/or proxy handler. * @param args.routes - Custom {@link ApiRoute} definitions (middleware or proxy). * @param args.proxyHandler - A proxy handler created with {@link createProxyHandler} * for service discovery integration. * @param options - Optional plugin configuration. * @param options.route - Base middleware path. Defaults to {@link DEFAULT_VALUES.API_PATH}. * @param options.process - Route processing options forwarded to {@link processRoutes}. * @param options.logger - Logger for plugin diagnostics. * @returns A Vite `Plugin` that configures server proxy and middleware. * @throws {Error} When neither `routes` nor `proxyHandler` is provided. * * @example * ```ts * import apiPlugin, { createProxyHandler } from '@equinor/fusion-framework-vite-plugin-api-service'; * * export default defineConfig({ * plugins: [ * apiPlugin( * { * proxyHandler: createProxyHandler( * 'https://discovery.example.com/services', * (data, { route }) => ({ data, routes: [] }), * ), * routes: [ * { match: '/mock/health', middleware: (_req, res) => { res.end('ok'); } }, * ], * }, * { logger: console }, * ), * ], * }); * ``` */ export declare function plugin(args: PluginArguments, options?: PluginOptions): Plugin; export default plugin;