UNPKG

@vercel/microfrontends

Version:

Defines configuration and utilities for microfrontends development

95 lines (92 loc) 4.24 kB
import { MiddlewareConfig, NextRequest, NextFetchEvent } from 'next/server.js'; import { MicrofrontendConfigIsomorphic } from '../config.js'; import '../types-dcd8b17a.js'; import '../types-b9ea41b2.js'; /** Replaces path wildcards (if they exist) with synthesized paths. */ declare function expandWildcards(path: string): string[]; declare function loadMicrofrontendConfigForEdge(path: string): MicrofrontendConfigIsomorphic; declare function getAllChildApplicationNames(mfConfig: MicrofrontendConfigIsomorphic): string[]; declare function getLaunchedPathsForApp(mfConfig: MicrofrontendConfigIsomorphic, appName: string): string[]; declare function getFlaggedPathsForApp(mfConfig: MicrofrontendConfigIsomorphic, appName: string): string[]; /** * Returns a list of examples for all paths in microfrontends.json. */ declare function getAllMicrofrontendPaths(mfConfig: MicrofrontendConfigIsomorphic): string[]; /** * A test to ensure that middleware is configured to work correctly with * microfrontends. Passing this test does NOT guarantee middleware is set up * correctly, but this should find many common problems. This should only be run * on the application marked as "default" in the microfrontend config. If a * configuration issue is found, this will throw an exception (this ensures it * works with any test framework). * * For example, if a microfrontend is configured to serve "/my/path" then the * default application should not contain any matcher that matches "/my/path". * * See [documentation](https://vercel.com/docs/microfrontends/troubleshooting#validatemiddlewareconfig) for more information. * * @example * ```ts * test('matches microfrontends paths', () => { * expect(() => * validateMiddlewareConfig(config, './microfrontends.json'), * ).not.toThrow(); * }); * ``` */ declare function validateMiddlewareConfig(middlewareConfig: MiddlewareConfig, microfrontendConfigOrPath: string | MicrofrontendConfigIsomorphic, extraProductionMatches?: string[]): void; /** * Ensures that middleware rewrites to the correct path for flagged paths. * IMPORTANT: you must enable the necessary flags before calling this function. * * See [documentation](https://vercel.com/docs/microfrontends/troubleshooting#validatemiddlewareonflaggedpaths) for more information. * * @example * ```ts * jest.mock('flags/next', () => ({ * flag: jest.fn().mockReturnValue(jest.fn().mockResolvedValue(true)), * })); * * test('matches microfrontends paths', () => { * await expect( * validateMiddlewareOnFlaggedPaths('./microfrontends.json', middleware), * ).resolves.not.toThrow(); * }); * ``` */ declare function validateMiddlewareOnFlaggedPaths(microfrontendConfigOrPath: string | MicrofrontendConfigIsomorphic, middleware: (request: NextRequest, event: NextFetchEvent) => Promise<Response | undefined>): Promise<void>; /** * Validates that the given paths route to the correct microfrontend. * The `routesToTest` parameter is a record mapping the application name * to a list a paths (with an optional flag) that should be routed to that * application. If an issue is found, this will throw an exception so that * it can be used with any test framework. * * See [documentation](https://vercel.com/docs/microfrontends/troubleshooting#validaterouting) for more information. * * For example: * * ```ts * { * 'microfrontend-a': ['/a/path', '/a/longer/path'], * 'microfrontend-b': ['/b/path'], * 'microfrontend-c': [ * '/c/path', * { * 'path': '/c/flagged', * 'flag': 'my-flag', * } * ], * } * ``` * * This ensures: * - `/a/path` and `/a/longer/path` get routed to `microfrontend-a` * - `/b/path` gets routed to `microfrontend-b` * - `/c/flagged` gets routed to `microfrontend-c` if `my-flag` is enabled. */ declare function validateRouting(microfrontendConfigOrPath: string | MicrofrontendConfigIsomorphic, routesToTest: Record<string, (string | { path: string; flag: string; })[]>): void; export { expandWildcards, getAllChildApplicationNames, getAllMicrofrontendPaths, getFlaggedPathsForApp, getLaunchedPathsForApp, loadMicrofrontendConfigForEdge, validateMiddlewareConfig, validateMiddlewareOnFlaggedPaths, validateRouting };