@zimic/interceptor 0.16.0-canary.1 → 0.16.0-canary.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -5
- package/dist/{chunk-6TSSHQW5.mjs → chunk-NTRC2S4I.mjs} +253 -312
- package/dist/chunk-NTRC2S4I.mjs.map +1 -0
- package/dist/{chunk-R2ROSKU4.js → chunk-O6ZIPCUJ.js} +254 -313
- package/dist/chunk-O6ZIPCUJ.js.map +1 -0
- package/dist/cli.js +9 -9
- package/dist/cli.js.map +1 -1
- package/dist/cli.mjs +5 -5
- package/dist/cli.mjs.map +1 -1
- package/dist/http.d.ts +156 -176
- package/dist/http.js +265 -324
- package/dist/http.js.map +1 -1
- package/dist/http.mjs +263 -323
- package/dist/http.mjs.map +1 -1
- package/dist/server.d.ts +20 -46
- package/dist/server.js +7 -7
- package/dist/server.mjs +1 -1
- package/package.json +8 -8
- package/src/cli/browser/init.ts +2 -2
- package/src/cli/server/start.ts +2 -2
- package/src/http/index.ts +6 -13
- package/src/http/interceptor/HttpInterceptorClient.ts +30 -15
- package/src/http/interceptor/LocalHttpInterceptor.ts +8 -8
- package/src/http/interceptor/RemoteHttpInterceptor.ts +8 -8
- package/src/http/interceptor/errors/RequestSavingSafeLimitExceededError.ts +22 -0
- package/src/http/interceptor/factory.ts +1 -1
- package/src/http/interceptor/types/options.ts +4 -11
- package/src/http/interceptor/types/public.ts +44 -12
- package/src/http/interceptor/types/schema.ts +2 -2
- package/src/http/interceptorWorker/HttpInterceptorWorker.ts +6 -31
- package/src/http/requestHandler/HttpRequestHandlerClient.ts +15 -5
- package/src/http/requestHandler/errors/DisabledRequestSavingError.ts +2 -2
- package/src/http/requestHandler/errors/TimesCheckError.ts +15 -14
- package/src/http/requestHandler/types/public.ts +16 -8
- package/src/server/index.ts +1 -11
- package/src/utils/console.ts +2 -2
- package/dist/chunk-6TSSHQW5.mjs.map +0 -1
- package/dist/chunk-R2ROSKU4.js.map +0 -1
- package/src/http/interceptorWorker/HttpInterceptorWorkerStore.ts +0 -34
- package/src/http/namespace/HttpInterceptorNamespace.ts +0 -81
- package/src/server/namespace/InterceptorServerNamespace.ts +0 -21
package/dist/cli.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../package.json","../src/cli/browser/shared/constants.ts","../src/cli/browser/init.ts","../src/utils/processes.ts","../src/cli/server/start.ts","../src/cli/cli.ts","../src/cli/index.ts"],"names":["exitEvent","yargs"],"mappings":";;;;;;;;;;AAgBE,IAAW,OAAA,GAAA,iBAAA;;;AChBN,IAAM,wBAA2B,GAAA,sBAAA;;;ACQxC,IAAM,aAAA,GAAgB,KAAK,IAAK,CAAA,SAAA,CAAQ,QAAQ,KAAK,CAAA,EAAG,IAAM,EAAA,IAAA,EAAM,IAAI,CAAA;AACjE,IAAM,wBAA2B,GAAA,IAAA,CAAK,IAAK,CAAA,aAAA,EAAe,OAAO,wBAAwB,CAAA;AAMhG,eAAe,8BAAA,CAA+B,EAAE,eAAA,EAAoD,EAAA;AAClG,EAAM,MAAA,uBAAA,GAA0B,IAAK,CAAA,OAAA,CAAQ,eAAe,CAAA;AAC5D,EAAA,MAAM,WAAW,KAAM,CAAA,uBAAA,EAAyB,EAAE,SAAA,EAAW,MAAM,CAAA;AAEnE,EAAA,MAAM,eAAkB,GAAA,IAAA,CAAK,IAAK,CAAA,uBAAA,EAAyB,wBAAwB,CAAA;AACnF,EAAM,MAAA,UAAA,CAAW,QAAS,CAAA,wBAAA,EAA0B,eAAe,CAAA;AAEnE,EAAA,aAAA,CAAc,CAAkC,+BAAA,EAAA,KAAA,CAAM,KAAM,CAAA,eAAe,CAAC,CAAG,CAAA,CAAA,CAAA;AAC/E,EAAA,aAAA,CAAc,uCAAuC,CAAA;AACvD;AATe,MAAA,CAAA,8BAAA,EAAA,gCAAA,CAAA;AAWf,IAAO,YAAQ,GAAA,8BAAA;;;ACxBR,IAAM,mBAAA,GAAsB,OAA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U,CAAA;AAE7F,QAAA,MAAM,aAAuB,CAAA;AAAA,UAC3B,UAAU,YAAa,CAAA,QAAA;AAAA,UACvB,MAAM,YAAa,CAAA,IAAA;AAAA,UACnB,WAAW,YAAa,CAAA,SAAA;AAAA,UACxB,sBAAsB,YAAa,CAAA,oBAAA;AAAA,UACnC,SAAS,cACL,GAAA;AAAA,YACE,OAAA,EAAS,eAAe,QAAS,EAAA;AAAA,YACjC,SAAW,EAAA;AAAA,WAEb,GAAA;AAAA,SACL,CAAA;AAAA;AACH;AACF,IAGD,KAAM,EAAA;AACX;AApFe,MAAA,CAAA,MAAA,EAAA,QAAA,CAAA;AAsFf,IAAO,WAAQ,GAAA,MAAA;;;AC3Ff,KAAK,WAAO,EAAA","file":"cli.mjs","sourcesContent":["{\n \"name\": \"@zimic/interceptor\",\n \"description\": \"TypeScript-first HTTP intercepting and mocking\",\n \"keywords\": [\n \"zimic\",\n \"typescript\",\n \"types\",\n \"typegen\",\n \"validation\",\n \"inference\",\n \"http\",\n \"interceptor\",\n \"mock\",\n \"api\",\n \"static\"\n ],\n \"version\": \"0.16.0-canary.1\",\n \"repository\": {\n \"type\": \"git\",\n \"url\": \"https://github.com/zimicjs/zimic.git\",\n \"directory\": \"packages/zimic-interceptor\"\n },\n \"author\": {\n \"name\": \"Diego Aquino\",\n \"url\": \"https://github.com/diego-aquino\"\n },\n \"private\": false,\n \"publishConfig\": {\n \"access\": \"public\",\n \"provenance\": true\n },\n \"engines\": {\n \"node\": \">=18.13.0\"\n },\n \"license\": \"MIT\",\n \"files\": [\n \"package.json\",\n \"README.md\",\n \"LICENSE.md\",\n \"src\",\n \"!src/**/tests\",\n \"!src/**/__tests__\",\n \"!src/**/*.test.ts\",\n \"dist\",\n \"index.d.ts\",\n \"http.d.ts\",\n \"server.d.ts\"\n ],\n \"main\": \"./dist/index.js\",\n \"module\": \"./dist/index.mjs\",\n \"types\": \"index.d.ts\",\n \"bin\": {\n \"zimic-interceptor\": \"./dist/cli.js\"\n },\n \"exports\": {\n \".\": {\n \"types\": \"./index.d.ts\",\n \"import\": \"./dist/index.mjs\",\n \"require\": \"./dist/index.js\",\n \"default\": \"./dist/index.js\"\n },\n \"./http\": {\n \"types\": \"./http.d.ts\",\n \"import\": \"./dist/http.mjs\",\n \"require\": \"./dist/http.js\",\n \"default\": \"./dist/http.js\"\n },\n \"./server\": {\n \"types\": \"./server.d.ts\",\n \"import\": \"./dist/server.mjs\",\n \"require\": \"./dist/server.js\",\n \"default\": \"./dist/server.js\"\n },\n \"./package.json\": \"./package.json\"\n },\n \"scripts\": {\n \"dev\": \"tsup --watch\",\n \"cli\": \"node ./dist/cli.js\",\n \"build\": \"tsup\",\n \"lint\": \"eslint --cache --no-error-on-unmatched-pattern --no-warn-ignored --fix\",\n \"lint:turbo\": \"pnpm lint . --max-warnings 0\",\n \"style\": \"prettier --log-level warn --ignore-unknown --no-error-on-unmatched-pattern --cache\",\n \"style:check\": \"pnpm style --check\",\n \"style:format\": \"pnpm style --write\",\n \"test\": \"dotenv -v NODE_ENV=test -v FORCE_COLOR=1 -- vitest\",\n \"test:turbo\": \"dotenv -v CI=true -- pnpm run test run --coverage\",\n \"types:check\": \"tsc --noEmit\",\n \"deps:install-playwright\": \"playwright install chromium\",\n \"deps:init-msw\": \"msw init ./public --no-save\",\n \"deps:init\": \"pnpm deps:install-playwright && pnpm deps:init-msw\",\n \"postinstall\": \"node -e \\\"try{require('./dist/scripts/postinstall')}catch(error){console.error(error)}\\\"\"\n },\n \"dependencies\": {\n \"@whatwg-node/server\": \"0.10.1\",\n \"chalk\": \"4.1.2\",\n \"execa\": \"9.5.2\",\n \"isomorphic-ws\": \"5.0.0\",\n \"msw\": \"2.7.3\",\n \"ws\": \"8.18.1\",\n \"yargs\": \"17.7.2\"\n },\n \"optionalDependencies\": {\n \"bufferutil\": \"4.0.9\"\n },\n \"devDependencies\": {\n \"@types/node\": \"^22.13.10\",\n \"@types/ws\": \"^8.18.0\",\n \"@types/yargs\": \"^17.0.33\",\n \"@vitest/browser\": \"^3.0.8\",\n \"@vitest/coverage-istanbul\": \"^3.0.8\",\n \"@zimic/eslint-config-node\": \"workspace:*\",\n \"@zimic/lint-staged-config\": \"workspace:*\",\n \"@zimic/tsconfig\": \"workspace:*\",\n \"@zimic/utils\": \"workspace:*\",\n \"dotenv-cli\": \"^8.0.0\",\n \"eslint\": \"^9.22.0\",\n \"playwright\": \"^1.51.0\",\n \"tsup\": \"^8.4.0\",\n \"typescript\": \"^5.8.2\",\n \"vitest\": \"^3.0.8\"\n },\n \"peerDependencies\": {\n \"@zimic/http\": \"^0.1.0 || ^0.1.0-canary.0\",\n \"typescript\": \">=4.8.0\"\n },\n \"peerDependenciesMeta\": {\n \"typescript\": {\n \"optional\": true\n }\n }\n}\n","export const SERVICE_WORKER_FILE_NAME = 'mockServiceWorker.js';\n","import chalk from 'chalk';\nimport filesystem from 'fs/promises';\nimport path from 'path';\n\nimport { logWithPrefix } from '@/utils/console';\n\nimport { SERVICE_WORKER_FILE_NAME } from './shared/constants';\n\nconst MSW_ROOT_PATH = path.join(require.resolve('msw'), '..', '..', '..');\nexport const MOCK_SERVICE_WORKER_PATH = path.join(MSW_ROOT_PATH, 'lib', SERVICE_WORKER_FILE_NAME);\n\ninterface BrowserServiceWorkerInitOptions {\n publicDirectory: string;\n}\n\nasync function initializeBrowserServiceWorker({ publicDirectory }: BrowserServiceWorkerInitOptions) {\n const absolutePublicDirectory = path.resolve(publicDirectory);\n await filesystem.mkdir(absolutePublicDirectory, { recursive: true });\n\n const destinationPath = path.join(absolutePublicDirectory, SERVICE_WORKER_FILE_NAME);\n await filesystem.copyFile(MOCK_SERVICE_WORKER_PATH, destinationPath);\n\n logWithPrefix(`Service worker script saved to ${chalk.green(destinationPath)}!`);\n logWithPrefix('You can now use browser interceptors!');\n}\n\nexport default initializeBrowserServiceWorker;\n","import createCachedDynamicImport from '@zimic/utils/import/createCachedDynamicImport';\n\nexport const PROCESS_EXIT_EVENTS = Object.freeze([\n 'beforeExit',\n 'uncaughtExceptionMonitor',\n 'SIGINT',\n 'SIGTERM',\n 'SIGHUP',\n 'SIGBREAK',\n] as const);\n\nexport type ProcessExitEvent = (typeof PROCESS_EXIT_EVENTS)[number];\n\n// Having an undefined exit code means that the process will already exit with the default exit code.\nexport const PROCESS_EXIT_CODE_BY_EXIT_EVENT: Record<string, number | undefined> = {\n beforeExit: undefined,\n uncaughtExceptionMonitor: undefined,\n SIGINT: 130,\n SIGTERM: 143,\n SIGHUP: 129,\n SIGBREAK: 131,\n} satisfies Record<ProcessExitEvent, number | undefined>;\n\nexport const importExeca = createCachedDynamicImport(() => import('execa'));\n\ninterface CommandErrorOptions {\n command?: string[];\n exitCode?: number;\n signal?: NodeJS.Signals;\n originalMessage?: string;\n}\n\nexport class CommandError extends Error {\n static readonly DEFAULT_EXIT_CODE = 1;\n\n readonly command: string[];\n readonly exitCode: number;\n readonly signal?: NodeJS.Signals;\n\n constructor(executable: string, options: CommandErrorOptions) {\n const message = CommandError.createMessage(executable, options);\n super(message);\n\n this.name = 'CommandError';\n this.command = options.command ?? [executable];\n this.exitCode = this.getExitCode(options);\n this.signal = options.signal;\n }\n\n private getExitCode(options: CommandErrorOptions): number {\n const existingExitCode = options.exitCode;\n const exitCodeInferredFromSignal =\n options.signal === undefined ? undefined : PROCESS_EXIT_CODE_BY_EXIT_EVENT[options.signal];\n\n return existingExitCode ?? exitCodeInferredFromSignal ?? CommandError.DEFAULT_EXIT_CODE;\n }\n\n private static createMessage(command: string, options: CommandErrorOptions) {\n const suffix = options.originalMessage ? `: ${options.originalMessage}` : '';\n\n if (options.exitCode === undefined && options.signal === undefined) {\n return `Command '${command}' failed${suffix}`;\n }\n\n const prefix = `Command '${command}' exited `;\n const infix = options.exitCode === undefined ? `after signal ${options.signal}` : `with code ${options.exitCode}`;\n\n return `${prefix}${infix}${suffix}`;\n }\n}\n\nexport async function runCommand(command: string, commandArguments: string[]) {\n const { execa: $, ExecaError } = await importExeca();\n\n try {\n await $(command, commandArguments, { stdio: 'inherit' });\n } catch (error) {\n /* istanbul ignore if -- @preserve\n * This is a safeguard if the error is not an ExecaError. It is not expected to run. */\n if (!(error instanceof ExecaError)) {\n throw error;\n }\n\n const commandError = new CommandError(command, {\n command: [command, ...commandArguments],\n exitCode: error.exitCode,\n signal: error.signal,\n originalMessage: error.originalMessage,\n });\n\n throw commandError;\n }\n}\n","import { InterceptorServer, interceptorServer } from '@/server';\nimport { InterceptorServerOptions } from '@/server/types/options';\nimport { logWithPrefix } from '@/utils/console';\nimport {\n CommandError,\n PROCESS_EXIT_CODE_BY_EXIT_EVENT,\n PROCESS_EXIT_EVENTS,\n ProcessExitEvent,\n runCommand,\n} from '@/utils/processes';\n\ninterface InterceptorServerStartOptions extends InterceptorServerOptions {\n ephemeral: boolean;\n onReady?: {\n command: string;\n arguments: string[];\n };\n}\n\nexport let serverSingleton: InterceptorServer | undefined;\n\nasync function startInterceptorServer({\n hostname,\n port,\n ephemeral,\n logUnhandledRequests,\n onReady,\n}: InterceptorServerStartOptions) {\n const server = interceptorServer.create({\n hostname,\n port,\n logUnhandledRequests,\n });\n\n async function handleExitEvent(exitEvent: ProcessExitEvent | undefined) {\n await server.stop();\n\n // eslint-disable-next-line @typescript-eslint/no-use-before-define\n for (const { exitEvent, exitHandler } of exitHandlerGroups) {\n process.off(exitEvent, exitHandler);\n }\n\n const exitCode = exitEvent ? PROCESS_EXIT_CODE_BY_EXIT_EVENT[exitEvent] : undefined;\n if (exitCode !== undefined) {\n process.exit(exitCode);\n }\n }\n\n const exitHandlerGroups = PROCESS_EXIT_EVENTS.map((exitEvent) => ({\n exitEvent,\n exitHandler: handleExitEvent.bind(null, exitEvent),\n }));\n\n for (const { exitEvent, exitHandler } of exitHandlerGroups) {\n process.on(exitEvent, exitHandler);\n }\n\n serverSingleton = server;\n\n await server.start();\n\n logWithPrefix(`${ephemeral ? 'Ephemeral s' : 'S'}erver is running on ${server.hostname}:${server.port}`);\n\n if (onReady) {\n try {\n await runCommand(onReady.command, onReady.arguments);\n } catch (error) {\n console.error(error);\n\n /* istanbul ignore if -- @preserve\n * A CommandError is always expected here. */\n if (!(error instanceof CommandError)) {\n throw error;\n }\n\n process.exit(error.exitCode);\n }\n }\n\n if (ephemeral) {\n await handleExitEvent(undefined);\n process.exit(0);\n }\n}\n\nexport default startInterceptorServer;\n","import yargs from 'yargs';\nimport { hideBin } from 'yargs/helpers';\n\nimport { version } from '@@/package.json';\n\nimport initializeBrowserServiceWorker from './browser/init';\nimport startInterceptorServer from './server/start';\n\nasync function runCLI() {\n await yargs(hideBin(process.argv))\n .scriptName('zimic-interceptor')\n .version(version)\n .showHelpOnFail(false)\n .strict()\n\n .command('browser', 'Manage your browser mock configuration', (yargs) =>\n yargs.demandCommand().command(\n 'init <publicDirectory>',\n 'Initialize the browser service worker configuration.',\n (yargs) =>\n yargs.positional('publicDirectory', {\n type: 'string',\n description: 'The path to the public directory of your application.',\n demandOption: true,\n }),\n async (cliArguments) => {\n await initializeBrowserServiceWorker({\n publicDirectory: cliArguments.publicDirectory,\n });\n },\n ),\n )\n\n .command('server', 'Manage interceptor servers', (yargs) =>\n yargs.demandCommand().command(\n 'start [-- onReady]',\n 'Start an interceptor server.',\n (yargs) =>\n yargs\n .positional('onReady', {\n description: 'A command to run when the server is ready to accept connections.',\n type: 'string',\n })\n .option('hostname', {\n type: 'string',\n description: 'The hostname to start the server on.',\n alias: 'h',\n default: 'localhost',\n })\n .option('port', {\n type: 'number',\n description: 'The port to start the server on.',\n alias: 'p',\n })\n .option('ephemeral', {\n type: 'boolean',\n description:\n 'Whether the server should stop automatically after the on-ready command finishes. ' +\n 'If no on-ready command is provided and ephemeral is true, the server will stop immediately after ' +\n 'starting.',\n alias: 'e',\n default: false,\n })\n .option('log-unhandled-requests', {\n type: 'boolean',\n description:\n 'Whether to log a warning when no interceptors were found for the base URL of a request. ' +\n 'If an interceptor was matched, the logging behavior for that base URL is configured in the ' +\n 'interceptor itself.',\n alias: 'l',\n }),\n async (cliArguments) => {\n const onReadyCommand = cliArguments._.at(2)?.toString();\n const onReadyCommandArguments = cliArguments._.slice(3).map((argument) => argument.toString());\n\n await startInterceptorServer({\n hostname: cliArguments.hostname,\n port: cliArguments.port,\n ephemeral: cliArguments.ephemeral,\n logUnhandledRequests: cliArguments.logUnhandledRequests,\n onReady: onReadyCommand\n ? {\n command: onReadyCommand.toString(),\n arguments: onReadyCommandArguments,\n }\n : undefined,\n });\n },\n ),\n )\n\n .parse();\n}\n\nexport default runCLI;\n","#!/usr/bin/env node\nimport runCLI from './cli';\n\nvoid runCLI();\n"]}
|
|
1
|
+
{"version":3,"sources":["../package.json","../src/cli/browser/shared/constants.ts","../src/cli/browser/init.ts","../src/utils/processes.ts","../src/cli/server/start.ts","../src/cli/cli.ts","../src/cli/index.ts"],"names":["exitEvent","yargs"],"mappings":";;;;;;;;;;AAgBE,IAAW,OAAA,GAAA,kBAAA;;;AChBN,IAAM,wBAA2B,GAAA,sBAAA;;;ACQxC,IAAM,aAAA,GAAgB,KAAK,IAAK,CAAA,SAAA,CAAQ,QAAQ,KAAK,CAAA,EAAG,IAAM,EAAA,IAAA,EAAM,IAAI,CAAA;AACjE,IAAM,wBAA2B,GAAA,IAAA,CAAK,IAAK,CAAA,aAAA,EAAe,OAAO,wBAAwB,CAAA;AAMhG,eAAe,8BAAA,CAA+B,EAAE,eAAA,EAAoD,EAAA;AAClG,EAAM,MAAA,uBAAA,GAA0B,IAAK,CAAA,OAAA,CAAQ,eAAe,CAAA;AAC5D,EAAA,MAAM,WAAW,KAAM,CAAA,uBAAA,EAAyB,EAAE,SAAA,EAAW,MAAM,CAAA;AAEnE,EAAA,MAAM,eAAkB,GAAA,IAAA,CAAK,IAAK,CAAA,uBAAA,EAAyB,wBAAwB,CAAA;AACnF,EAAM,MAAA,UAAA,CAAW,QAAS,CAAA,wBAAA,EAA0B,eAAe,CAAA;AAEnE,EAAA,aAAA,CAAc,CAAkC,+BAAA,EAAA,KAAA,CAAM,KAAM,CAAA,eAAe,CAAC,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AAA,CAAC,GAAG,QAAS,EAAA;AACtD,QAAM,MAAA,uBAAA,GAA0B,YAAa,CAAA,CAAA,CAAE,KAAM,CAAA,CAAC,CAAE,CAAA,GAAA,CAAI,CAAC,QAAA,KAAa,QAAS,CAAA,QAAA,EAAU,CAAA;AAE7F,QAAA,MAAM,aAAuB,CAAA;AAAA,UAC3B,UAAU,YAAa,CAAA,QAAA;AAAA,UACvB,MAAM,YAAa,CAAA,IAAA;AAAA,UACnB,WAAW,YAAa,CAAA,SAAA;AAAA,UACxB,sBAAsB,YAAa,CAAA,oBAAA;AAAA,UACnC,SAAS,cACL,GAAA;AAAA,YACE,OAAA,EAAS,eAAe,QAAS,EAAA;AAAA,YACjC,SAAW,EAAA;AAAA,WAEb,GAAA;AAAA,SACL,CAAA;AAAA;AACH;AACF,IAGD,KAAM,EAAA;AACX;AApFe,MAAA,CAAA,MAAA,EAAA,QAAA,CAAA;AAsFf,IAAO,WAAQ,GAAA,MAAA;;;AC3Ff,KAAK,WAAO,EAAA","file":"cli.mjs","sourcesContent":["{\n \"name\": \"@zimic/interceptor\",\n \"description\": \"Next-gen, TypeScript-first HTTP intercepting and mocking\",\n \"keywords\": [\n \"zimic\",\n \"typescript\",\n \"types\",\n \"typegen\",\n \"validation\",\n \"inference\",\n \"http\",\n \"interceptor\",\n \"mock\",\n \"api\",\n \"static\"\n ],\n \"version\": \"0.16.0-canary.11\",\n \"repository\": {\n \"type\": \"git\",\n \"url\": \"https://github.com/zimicjs/zimic.git\",\n \"directory\": \"packages/zimic-interceptor\"\n },\n \"author\": {\n \"name\": \"Diego Aquino\",\n \"url\": \"https://github.com/diego-aquino\"\n },\n \"private\": false,\n \"publishConfig\": {\n \"access\": \"public\",\n \"provenance\": true\n },\n \"engines\": {\n \"node\": \">=18.13.0\"\n },\n \"license\": \"MIT\",\n \"files\": [\n \"package.json\",\n \"README.md\",\n \"LICENSE.md\",\n \"src\",\n \"!src/**/tests\",\n \"!src/**/__tests__\",\n \"!src/**/*.test.ts\",\n \"dist\",\n \"index.d.ts\",\n \"http.d.ts\",\n \"server.d.ts\"\n ],\n \"main\": \"./dist/index.js\",\n \"module\": \"./dist/index.mjs\",\n \"types\": \"index.d.ts\",\n \"bin\": {\n \"zimic-interceptor\": \"./dist/cli.js\"\n },\n \"exports\": {\n \".\": {\n \"types\": \"./index.d.ts\",\n \"import\": \"./dist/index.mjs\",\n \"require\": \"./dist/index.js\",\n \"default\": \"./dist/index.js\"\n },\n \"./http\": {\n \"types\": \"./http.d.ts\",\n \"import\": \"./dist/http.mjs\",\n \"require\": \"./dist/http.js\",\n \"default\": \"./dist/http.js\"\n },\n \"./server\": {\n \"types\": \"./server.d.ts\",\n \"import\": \"./dist/server.mjs\",\n \"require\": \"./dist/server.js\",\n \"default\": \"./dist/server.js\"\n },\n \"./package.json\": \"./package.json\"\n },\n \"scripts\": {\n \"dev\": \"tsup --watch\",\n \"cli\": \"node ./dist/cli.js\",\n \"build\": \"tsup\",\n \"lint\": \"eslint --cache --no-error-on-unmatched-pattern --no-warn-ignored --fix\",\n \"lint:turbo\": \"pnpm lint . --max-warnings 0\",\n \"style\": \"prettier --log-level warn --ignore-unknown --no-error-on-unmatched-pattern --cache\",\n \"style:check\": \"pnpm style --check\",\n \"style:format\": \"pnpm style --write\",\n \"test\": \"dotenv -v NODE_ENV=test -v FORCE_COLOR=1 -- vitest\",\n \"test:turbo\": \"dotenv -v CI=true -- pnpm run test run --coverage\",\n \"types:check\": \"tsc --noEmit\",\n \"deps:install-playwright\": \"playwright install chromium\",\n \"deps:init-msw\": \"msw init ./public --no-save\",\n \"deps:init\": \"pnpm deps:install-playwright && pnpm deps:init-msw\",\n \"postinstall\": \"node -e \\\"try{require('./dist/scripts/postinstall')}catch(error){console.error(error)}\\\"\"\n },\n \"dependencies\": {\n \"@whatwg-node/server\": \"0.10.1\",\n \"execa\": \"9.5.2\",\n \"isomorphic-ws\": \"5.0.0\",\n \"msw\": \"2.7.3\",\n \"picocolors\": \"^1.1.1\",\n \"ws\": \"8.18.1\",\n \"yargs\": \"17.7.2\"\n },\n \"optionalDependencies\": {\n \"bufferutil\": \"4.0.9\"\n },\n \"devDependencies\": {\n \"@types/node\": \"^22.13.10\",\n \"@types/ws\": \"^8.18.0\",\n \"@types/yargs\": \"^17.0.33\",\n \"@vitest/browser\": \"^3.0.9\",\n \"@vitest/coverage-istanbul\": \"^3.0.9\",\n \"@zimic/eslint-config-node\": \"workspace:*\",\n \"@zimic/lint-staged-config\": \"workspace:*\",\n \"@zimic/tsconfig\": \"workspace:*\",\n \"@zimic/utils\": \"workspace:*\",\n \"dotenv-cli\": \"^8.0.0\",\n \"eslint\": \"^9.22.0\",\n \"playwright\": \"^1.51.1\",\n \"tsup\": \"^8.4.0\",\n \"typescript\": \"^5.8.2\",\n \"vitest\": \"^3.0.9\"\n },\n \"peerDependencies\": {\n \"@zimic/http\": \"^0.2.0 || ^0.2.0-canary.0\",\n \"typescript\": \">=4.8.0\"\n },\n \"peerDependenciesMeta\": {\n \"typescript\": {\n \"optional\": true\n }\n }\n}\n","export const SERVICE_WORKER_FILE_NAME = 'mockServiceWorker.js';\n","import filesystem from 'fs/promises';\nimport path from 'path';\nimport color from 'picocolors';\n\nimport { logWithPrefix } from '@/utils/console';\n\nimport { SERVICE_WORKER_FILE_NAME } from './shared/constants';\n\nconst MSW_ROOT_PATH = path.join(require.resolve('msw'), '..', '..', '..');\nexport const MOCK_SERVICE_WORKER_PATH = path.join(MSW_ROOT_PATH, 'lib', SERVICE_WORKER_FILE_NAME);\n\ninterface BrowserServiceWorkerInitOptions {\n publicDirectory: string;\n}\n\nasync function initializeBrowserServiceWorker({ publicDirectory }: BrowserServiceWorkerInitOptions) {\n const absolutePublicDirectory = path.resolve(publicDirectory);\n await filesystem.mkdir(absolutePublicDirectory, { recursive: true });\n\n const destinationPath = path.join(absolutePublicDirectory, SERVICE_WORKER_FILE_NAME);\n await filesystem.copyFile(MOCK_SERVICE_WORKER_PATH, destinationPath);\n\n logWithPrefix(`Service worker script saved to ${color.green(destinationPath)}!`);\n logWithPrefix('You can now use browser interceptors!');\n}\n\nexport default initializeBrowserServiceWorker;\n","import createCachedDynamicImport from '@zimic/utils/import/createCachedDynamicImport';\n\nexport const PROCESS_EXIT_EVENTS = Object.freeze([\n 'beforeExit',\n 'uncaughtExceptionMonitor',\n 'SIGINT',\n 'SIGTERM',\n 'SIGHUP',\n 'SIGBREAK',\n] as const);\n\nexport type ProcessExitEvent = (typeof PROCESS_EXIT_EVENTS)[number];\n\n// Having an undefined exit code means that the process will already exit with the default exit code.\nexport const PROCESS_EXIT_CODE_BY_EXIT_EVENT: Record<string, number | undefined> = {\n beforeExit: undefined,\n uncaughtExceptionMonitor: undefined,\n SIGINT: 130,\n SIGTERM: 143,\n SIGHUP: 129,\n SIGBREAK: 131,\n} satisfies Record<ProcessExitEvent, number | undefined>;\n\nexport const importExeca = createCachedDynamicImport(() => import('execa'));\n\ninterface CommandErrorOptions {\n command?: string[];\n exitCode?: number;\n signal?: NodeJS.Signals;\n originalMessage?: string;\n}\n\nexport class CommandError extends Error {\n static readonly DEFAULT_EXIT_CODE = 1;\n\n readonly command: string[];\n readonly exitCode: number;\n readonly signal?: NodeJS.Signals;\n\n constructor(executable: string, options: CommandErrorOptions) {\n const message = CommandError.createMessage(executable, options);\n super(message);\n\n this.name = 'CommandError';\n this.command = options.command ?? [executable];\n this.exitCode = this.getExitCode(options);\n this.signal = options.signal;\n }\n\n private getExitCode(options: CommandErrorOptions): number {\n const existingExitCode = options.exitCode;\n const exitCodeInferredFromSignal =\n options.signal === undefined ? undefined : PROCESS_EXIT_CODE_BY_EXIT_EVENT[options.signal];\n\n return existingExitCode ?? exitCodeInferredFromSignal ?? CommandError.DEFAULT_EXIT_CODE;\n }\n\n private static createMessage(command: string, options: CommandErrorOptions) {\n const suffix = options.originalMessage ? `: ${options.originalMessage}` : '';\n\n if (options.exitCode === undefined && options.signal === undefined) {\n return `Command '${command}' failed${suffix}`;\n }\n\n const prefix = `Command '${command}' exited `;\n const infix = options.exitCode === undefined ? `after signal ${options.signal}` : `with code ${options.exitCode}`;\n\n return `${prefix}${infix}${suffix}`;\n }\n}\n\nexport async function runCommand(command: string, commandArguments: string[]) {\n const { execa: $, ExecaError } = await importExeca();\n\n try {\n await $(command, commandArguments, { stdio: 'inherit' });\n } catch (error) {\n /* istanbul ignore if -- @preserve\n * This is a safeguard if the error is not an ExecaError. It is not expected to run. */\n if (!(error instanceof ExecaError)) {\n throw error;\n }\n\n const commandError = new CommandError(command, {\n command: [command, ...commandArguments],\n exitCode: error.exitCode,\n signal: error.signal,\n originalMessage: error.originalMessage,\n });\n\n throw commandError;\n }\n}\n","import { InterceptorServer, createInterceptorServer } from '@/server';\nimport { InterceptorServerOptions } from '@/server/types/options';\nimport { logWithPrefix } from '@/utils/console';\nimport {\n CommandError,\n PROCESS_EXIT_CODE_BY_EXIT_EVENT,\n PROCESS_EXIT_EVENTS,\n ProcessExitEvent,\n runCommand,\n} from '@/utils/processes';\n\ninterface InterceptorServerStartOptions extends InterceptorServerOptions {\n ephemeral: boolean;\n onReady?: {\n command: string;\n arguments: string[];\n };\n}\n\nexport let serverSingleton: InterceptorServer | undefined;\n\nasync function startInterceptorServer({\n hostname,\n port,\n ephemeral,\n logUnhandledRequests,\n onReady,\n}: InterceptorServerStartOptions) {\n const server = createInterceptorServer({\n hostname,\n port,\n logUnhandledRequests,\n });\n\n async function handleExitEvent(exitEvent: ProcessExitEvent | undefined) {\n await server.stop();\n\n // eslint-disable-next-line @typescript-eslint/no-use-before-define\n for (const { exitEvent, exitHandler } of exitHandlerGroups) {\n process.off(exitEvent, exitHandler);\n }\n\n const exitCode = exitEvent ? PROCESS_EXIT_CODE_BY_EXIT_EVENT[exitEvent] : undefined;\n if (exitCode !== undefined) {\n process.exit(exitCode);\n }\n }\n\n const exitHandlerGroups = PROCESS_EXIT_EVENTS.map((exitEvent) => ({\n exitEvent,\n exitHandler: handleExitEvent.bind(null, exitEvent),\n }));\n\n for (const { exitEvent, exitHandler } of exitHandlerGroups) {\n process.on(exitEvent, exitHandler);\n }\n\n serverSingleton = server;\n\n await server.start();\n\n logWithPrefix(`${ephemeral ? 'Ephemeral s' : 'S'}erver is running on ${server.hostname}:${server.port}`);\n\n if (onReady) {\n try {\n await runCommand(onReady.command, onReady.arguments);\n } catch (error) {\n console.error(error);\n\n /* istanbul ignore if -- @preserve\n * A CommandError is always expected here. */\n if (!(error instanceof CommandError)) {\n throw error;\n }\n\n process.exit(error.exitCode);\n }\n }\n\n if (ephemeral) {\n await handleExitEvent(undefined);\n process.exit(0);\n }\n}\n\nexport default startInterceptorServer;\n","import yargs from 'yargs';\nimport { hideBin } from 'yargs/helpers';\n\nimport { version } from '@@/package.json';\n\nimport initializeBrowserServiceWorker from './browser/init';\nimport startInterceptorServer from './server/start';\n\nasync function runCLI() {\n await yargs(hideBin(process.argv))\n .scriptName('zimic-interceptor')\n .version(version)\n .showHelpOnFail(false)\n .strict()\n\n .command('browser', 'Manage your browser mock configuration', (yargs) =>\n yargs.demandCommand().command(\n 'init <publicDirectory>',\n 'Initialize the browser service worker configuration.',\n (yargs) =>\n yargs.positional('publicDirectory', {\n type: 'string',\n description: 'The path to the public directory of your application.',\n demandOption: true,\n }),\n async (cliArguments) => {\n await initializeBrowserServiceWorker({\n publicDirectory: cliArguments.publicDirectory,\n });\n },\n ),\n )\n\n .command('server', 'Manage interceptor servers', (yargs) =>\n yargs.demandCommand().command(\n 'start [-- onReady]',\n 'Start an interceptor server.',\n (yargs) =>\n yargs\n .positional('onReady', {\n description: 'A command to run when the server is ready to accept connections.',\n type: 'string',\n })\n .option('hostname', {\n type: 'string',\n description: 'The hostname to start the server on.',\n alias: 'h',\n default: 'localhost',\n })\n .option('port', {\n type: 'number',\n description: 'The port to start the server on.',\n alias: 'p',\n })\n .option('ephemeral', {\n type: 'boolean',\n description:\n 'Whether the server should stop automatically after the on-ready command finishes. ' +\n 'If no on-ready command is provided and ephemeral is true, the server will stop immediately after ' +\n 'starting.',\n alias: 'e',\n default: false,\n })\n .option('log-unhandled-requests', {\n type: 'boolean',\n description:\n 'Whether to log a warning when no interceptors were found for the base URL of a request. ' +\n 'If an interceptor was matched, the logging behavior for that base URL is configured in the ' +\n 'interceptor itself.',\n alias: 'l',\n }),\n async (cliArguments) => {\n const onReadyCommand = cliArguments._.at(2)?.toString();\n const onReadyCommandArguments = cliArguments._.slice(3).map((argument) => argument.toString());\n\n await startInterceptorServer({\n hostname: cliArguments.hostname,\n port: cliArguments.port,\n ephemeral: cliArguments.ephemeral,\n logUnhandledRequests: cliArguments.logUnhandledRequests,\n onReady: onReadyCommand\n ? {\n command: onReadyCommand.toString(),\n arguments: onReadyCommandArguments,\n }\n : undefined,\n });\n },\n ),\n )\n\n .parse();\n}\n\nexport default runCLI;\n","#!/usr/bin/env node\nimport runCLI from './cli';\n\nvoid runCLI();\n"]}
|
package/dist/http.d.ts
CHANGED
|
@@ -1,5 +1,87 @@
|
|
|
1
1
|
import { HttpMethodSchema, HttpRequest, HttpHeaders, HttpRequestHeadersSchema, InferPathParams, HttpSearchParams, HttpRequestSearchParamsSchema, HttpRequestBodySchema, HttpStatusCode, HttpResponseSchema, HttpHeadersInit, HttpResponse, HttpResponseHeadersSchema, HttpResponseBodySchema, HttpSchema, HttpBody, HttpSchemaMethod, HttpSchemaPath, HttpHeadersSchema, HttpSearchParamsSchema, HttpFormData, HttpResponseSchemaStatusCode, HttpMethod, LiteralHttpSchemaPathFromNonLiteral } from '@zimic/http';
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* An error thrown when the interceptor is running and some operation requires it to be stopped first.
|
|
5
|
+
*
|
|
6
|
+
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#http-interceptorstart `interceptor.start()` API reference}
|
|
7
|
+
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#http-interceptorstop `interceptor.stop()` API reference}
|
|
8
|
+
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#http-interceptorisrunning `interceptor.isRunning` API reference}
|
|
9
|
+
*/
|
|
10
|
+
declare class RunningHttpInterceptorError extends Error {
|
|
11
|
+
constructor(additionalMessage: string);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* An error thrown when the interceptor is not running and it's not possible to use the mocking utilities.
|
|
16
|
+
*
|
|
17
|
+
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#http-interceptorstart `interceptor.start()` API reference}
|
|
18
|
+
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#http-interceptorstop `interceptor.stop()` API reference}
|
|
19
|
+
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#http-interceptorisrunning `interceptor.isRunning` API reference}
|
|
20
|
+
*/
|
|
21
|
+
declare class NotRunningHttpInterceptorError extends Error {
|
|
22
|
+
constructor();
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* An error thrown when an unknown interceptor platform is detected. Currently, the platforms `node` and `browser` are
|
|
27
|
+
* supported.
|
|
28
|
+
*
|
|
29
|
+
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#http-interceptorplatform `interceptor.platform` API reference}
|
|
30
|
+
*/
|
|
31
|
+
declare class UnknownHttpInterceptorPlatformError extends Error {
|
|
32
|
+
constructor();
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
declare class UnknownHttpInterceptorTypeError extends TypeError {
|
|
36
|
+
constructor(unknownType: unknown);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Error thrown when the safe limit of saved intercepted requests is exceeded.
|
|
41
|
+
*
|
|
42
|
+
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#saving-requests Saving intercepted requests}
|
|
43
|
+
*/
|
|
44
|
+
declare class RequestSavingSafeLimitExceededError extends TypeError {
|
|
45
|
+
constructor(numberOfSavedRequests: number, safeLimit: number);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Error thrown when a value is not valid {@link https://developer.mozilla.org/docs/Web/API/FormData FormData}. HTTP
|
|
50
|
+
* interceptors might throw this error when trying to parse the body of a request or response with the header
|
|
51
|
+
* `'content-type': 'multipart/form-data'`, if the content cannot be parsed to form data.
|
|
52
|
+
*/
|
|
53
|
+
declare class InvalidFormDataError extends SyntaxError {
|
|
54
|
+
constructor(value: string);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Error thrown when a value is not valid JSON. HTTP interceptors might throw this error when trying to parse the body
|
|
59
|
+
* of a request or response with the header `'content-type': 'application/json'`, if the content cannot be parsed to
|
|
60
|
+
* JSON.
|
|
61
|
+
*/
|
|
62
|
+
declare class InvalidJSONError extends SyntaxError {
|
|
63
|
+
constructor(value: string);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* An error thrown when the browser mock service worker is not found.
|
|
68
|
+
*
|
|
69
|
+
* @see {@link https://github.com/zimicjs/zimic/wiki/cli‐zimic‐browser#zimic-browser-init `zimic-interceptor browser init` API reference}
|
|
70
|
+
*/
|
|
71
|
+
declare class UnregisteredBrowserServiceWorkerError extends Error {
|
|
72
|
+
constructor();
|
|
73
|
+
static matchesRawError(error: unknown): boolean;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Error thrown when trying to access requests when the interceptor is not configured to do so.
|
|
78
|
+
*
|
|
79
|
+
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#saving-requests Saving intercepted requests}
|
|
80
|
+
*/
|
|
81
|
+
declare class DisabledRequestSavingError extends TypeError {
|
|
82
|
+
constructor();
|
|
83
|
+
}
|
|
84
|
+
|
|
3
85
|
type Default<Type, IfEmpty = never> = [undefined | void] extends [Type] ? IfEmpty : Exclude<Type, undefined | void>;
|
|
4
86
|
type IfNever<Type, Yes, No = Type> = [Type] extends [never] ? Yes : No;
|
|
5
87
|
type PossiblePromise<Type> = Type | PromiseLike<Type>;
|
|
@@ -187,20 +269,12 @@ interface SharedHttpInterceptorOptions {
|
|
|
187
269
|
*/
|
|
188
270
|
baseURL: string;
|
|
189
271
|
/**
|
|
190
|
-
*
|
|
191
|
-
* should save their intercepted requests in memory and make them accessible through
|
|
192
|
-
* {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#http-handlerrequests `handler.requests`}.
|
|
272
|
+
* Configures if the intercepted requests are saved and how they are handled.
|
|
193
273
|
*
|
|
194
|
-
* **Important**: If `saveRequests` is true, make sure to regularly clear the interceptor to avoid that the requests
|
|
195
|
-
* accumulate in memory. A common practice is to call
|
|
196
|
-
* {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#http-interceptorclear `interceptor.clear()`}
|
|
197
|
-
* after each test.
|
|
198
|
-
*
|
|
199
|
-
* @default false
|
|
200
274
|
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#saving-requests Saving intercepted requests}
|
|
201
275
|
* @see {@link https://github.com/zimicjs/zimic/wiki/guides‐testing‐interceptor Testing}
|
|
202
276
|
*/
|
|
203
|
-
|
|
277
|
+
requestSaving?: Partial<HttpInterceptorRequestSaving>;
|
|
204
278
|
}
|
|
205
279
|
/**
|
|
206
280
|
* The options to create a
|
|
@@ -240,7 +314,7 @@ interface RemoteHttpInterceptorOptions extends SharedHttpInterceptorOptions {
|
|
|
240
314
|
* The options to create an
|
|
241
315
|
* {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#httpinterceptor HTTP interceptor}.
|
|
242
316
|
*
|
|
243
|
-
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#
|
|
317
|
+
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#createhttpinterceptoroptions `createHttpInterceptor(options)` API reference}
|
|
244
318
|
*/
|
|
245
319
|
type HttpInterceptorOptions = LocalHttpInterceptorOptions | RemoteHttpInterceptorOptions;
|
|
246
320
|
|
|
@@ -423,8 +497,9 @@ interface LocalHttpRequestHandler<Schema extends HttpSchema, Method extends Http
|
|
|
423
497
|
* that was not satisfied.
|
|
424
498
|
*
|
|
425
499
|
* When
|
|
426
|
-
* {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#
|
|
427
|
-
*
|
|
500
|
+
* {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#saving-requests
|
|
501
|
+
* `requestSaving.enabled`} is
|
|
502
|
+
* `true` in your interceptor, the `TimesCheckError` errors will also list each unmatched request with diff of the
|
|
428
503
|
* expected and received data. This is useful for debugging requests that did not match a handler with
|
|
429
504
|
* {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#http-handlerwithrestriction restrictions}.
|
|
430
505
|
*
|
|
@@ -452,11 +527,14 @@ interface LocalHttpRequestHandler<Schema extends HttpSchema, Method extends Http
|
|
|
452
527
|
* The intercepted requests that matched this handler, along with the responses returned to each of them. This is
|
|
453
528
|
* useful for testing that the correct requests were made by your application.
|
|
454
529
|
*
|
|
455
|
-
* **Important**: This method can only be used if
|
|
530
|
+
* **Important**: This method can only be used if
|
|
531
|
+
* {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#saving-requests
|
|
532
|
+
* `requestSaving.enabled`} is
|
|
533
|
+
* `true` in the interceptor. See
|
|
456
534
|
* {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#saving-requests Saving intercepted requests}
|
|
457
535
|
* for more information.
|
|
458
536
|
*
|
|
459
|
-
* @throws {DisabledRequestSavingError} If the interceptor
|
|
537
|
+
* @throws {DisabledRequestSavingError} If the interceptor has `requestSaving.enabled: false`.
|
|
460
538
|
* @readonly
|
|
461
539
|
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#http-handlerrequests `handler.requests` API reference}
|
|
462
540
|
*/
|
|
@@ -538,8 +616,9 @@ interface SyncedRemoteHttpRequestHandler<Schema extends HttpSchema, Method exten
|
|
|
538
616
|
* that was not satisfied.
|
|
539
617
|
*
|
|
540
618
|
* When
|
|
541
|
-
* {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#
|
|
542
|
-
*
|
|
619
|
+
* {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#saving-requests
|
|
620
|
+
* `requestSaving.enabled`} is
|
|
621
|
+
* true in your interceptor, the `TimesCheckError` errors will also list each unmatched request with diff of the
|
|
543
622
|
* expected and received data. This is useful for debugging requests that did not match a handler with
|
|
544
623
|
* {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#http-handlerwithrestriction restrictions}.
|
|
545
624
|
*
|
|
@@ -567,11 +646,14 @@ interface SyncedRemoteHttpRequestHandler<Schema extends HttpSchema, Method exten
|
|
|
567
646
|
* The intercepted requests that matched this handler, along with the responses returned to each of them. This is
|
|
568
647
|
* useful for testing that the correct requests were made by your application.
|
|
569
648
|
*
|
|
570
|
-
* **Important**: This method can only be used if
|
|
649
|
+
* **Important**: This method can only be used if
|
|
650
|
+
* {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#saving-requests
|
|
651
|
+
* `requestSaving.enabled`} is
|
|
652
|
+
* `true` in the interceptor. See
|
|
571
653
|
* {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#saving-requests Saving intercepted requests}
|
|
572
654
|
* for more information.
|
|
573
655
|
*
|
|
574
|
-
* @throws {DisabledRequestSavingError} If the interceptor
|
|
656
|
+
* @throws {DisabledRequestSavingError} If the interceptor has `requestSaving.enabled: false`.
|
|
575
657
|
* @readonly
|
|
576
658
|
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#http-handlerrequests `handler.requests` API reference}
|
|
577
659
|
*/
|
|
@@ -620,6 +702,43 @@ type RemoteHttpRequestHandler<Schema extends HttpSchema, Method extends HttpSche
|
|
|
620
702
|
type SyncHttpInterceptorMethodHandler<Schema extends HttpSchema, Method extends HttpMethod> = Method extends HttpSchemaMethod<Schema> ? <Path extends HttpSchemaPath.NonLiteral<Schema, Method>>(path: Path) => LocalHttpRequestHandler<Schema, Method, LiteralHttpSchemaPathFromNonLiteral<Schema, Method, Path>> : (path: never) => LocalHttpRequestHandler<any, any, never>;
|
|
621
703
|
type AsyncHttpInterceptorMethodHandler<Schema extends HttpSchema, Method extends HttpMethod> = Method extends HttpSchemaMethod<Schema> ? <Path extends HttpSchemaPath.NonLiteral<Schema, Method>>(path: Path) => RemoteHttpRequestHandler<Schema, Method, LiteralHttpSchemaPathFromNonLiteral<Schema, Method, Path>> : (path: never) => RemoteHttpRequestHandler<any, any, never>;
|
|
622
704
|
|
|
705
|
+
/**
|
|
706
|
+
* Configures if the intercepted requests are saved and how they are handled.
|
|
707
|
+
*
|
|
708
|
+
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#saving-requests Saving intercepted requests}
|
|
709
|
+
* @see {@link https://github.com/zimicjs/zimic/wiki/guides‐testing‐interceptor Testing}
|
|
710
|
+
*/
|
|
711
|
+
interface HttpInterceptorRequestSaving {
|
|
712
|
+
/**
|
|
713
|
+
* Whether {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#httprequesthandler request handlers}
|
|
714
|
+
* should save their intercepted requests in memory and make them accessible through
|
|
715
|
+
* {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#http-handlerrequests `handler.requests`}.
|
|
716
|
+
*
|
|
717
|
+
* If you are using
|
|
718
|
+
* {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#http-interceptorchecktimes `interceptor.checkTimes()`}
|
|
719
|
+
* or
|
|
720
|
+
* {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#http-handlerchecktimes `handler.checkTimes()`}
|
|
721
|
+
* during tests, consider enabling this option to get more detailed information in `TimesCheckError` errors.
|
|
722
|
+
*
|
|
723
|
+
* **Important**: If `requestSaving.enabled` is `true`, make sure to regularly clear the interceptor to avoid requests
|
|
724
|
+
* accumulating in memory. A common practice is to call
|
|
725
|
+
* {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#http-interceptorclear `interceptor.clear()`}
|
|
726
|
+
* after each test.
|
|
727
|
+
*
|
|
728
|
+
* @default process.env.NODE_ENV === 'test'
|
|
729
|
+
*/
|
|
730
|
+
enabled: boolean;
|
|
731
|
+
/**
|
|
732
|
+
* The safe number of requests to save in memory before logging warnings in the console. If `requestSaving.enabled` is
|
|
733
|
+
* `true` and the interceptor is not regularly cleared with
|
|
734
|
+
* {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#http-interceptorclear `interceptor.clear()`},
|
|
735
|
+
* the requests may accumulate in memory and cause performance issues. This option does not limit the number of
|
|
736
|
+
* requests saved in memory, only when to log warnings.
|
|
737
|
+
*
|
|
738
|
+
* @default 1000
|
|
739
|
+
*/
|
|
740
|
+
safeLimit: number;
|
|
741
|
+
}
|
|
623
742
|
/**
|
|
624
743
|
* An interceptor to handle HTTP requests and return mock responses. The methods, paths, status codes, parameters, and
|
|
625
744
|
* responses are statically-typed based on the provided service schema.
|
|
@@ -634,20 +753,12 @@ interface HttpInterceptor<_Schema extends HttpSchema> {
|
|
|
634
753
|
*/
|
|
635
754
|
baseURL: string;
|
|
636
755
|
/**
|
|
637
|
-
*
|
|
638
|
-
* should save their intercepted requests in memory and make them accessible through
|
|
639
|
-
* {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#http-handlerrequests `handler.requests`}.
|
|
756
|
+
* Configures if the intercepted requests are saved and how they are handled.
|
|
640
757
|
*
|
|
641
|
-
* **Important**: If `saveRequests` is true, make sure to regularly clear the interceptor to avoid that the requests
|
|
642
|
-
* accumulate in memory. A common practice is to call
|
|
643
|
-
* {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#http-interceptorclear `interceptor.clear()`}
|
|
644
|
-
* after each test.
|
|
645
|
-
*
|
|
646
|
-
* @default process.env.NODE_ENV === 'test'
|
|
647
758
|
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#saving-requests Saving intercepted requests}
|
|
648
759
|
* @see {@link https://github.com/zimicjs/zimic/wiki/guides‐testing‐interceptor Testing}
|
|
649
760
|
*/
|
|
650
|
-
|
|
761
|
+
requestSaving: HttpInterceptorRequestSaving;
|
|
651
762
|
/**
|
|
652
763
|
* The platform the interceptor is running on.
|
|
653
764
|
*
|
|
@@ -695,8 +806,9 @@ interface HttpInterceptor<_Schema extends HttpSchema> {
|
|
|
695
806
|
* of each test.
|
|
696
807
|
*
|
|
697
808
|
* When
|
|
698
|
-
* {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#
|
|
699
|
-
*
|
|
809
|
+
* {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#saving-requests
|
|
810
|
+
* `requestSaving.enabled`} is
|
|
811
|
+
* `true` in your interceptor, the `TimesCheckError` errors will also list each unmatched request with diff of the
|
|
700
812
|
* expected and received data. This is useful for debugging requests that did not match a handler with
|
|
701
813
|
* {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#http-handlerwithrestriction restrictions}.
|
|
702
814
|
*
|
|
@@ -1152,144 +1264,6 @@ interface RemoteHttpInterceptor<Schema extends HttpSchema> extends HttpIntercept
|
|
|
1152
1264
|
clear: () => Promise<void>;
|
|
1153
1265
|
}
|
|
1154
1266
|
|
|
1155
|
-
/**
|
|
1156
|
-
* Creates an HTTP interceptor.
|
|
1157
|
-
*
|
|
1158
|
-
* @param options The options for the interceptor.
|
|
1159
|
-
* @returns The created HTTP interceptor.
|
|
1160
|
-
* @throws {InvalidURLError} If the base URL is invalid.
|
|
1161
|
-
* @throws {UnsupportedURLProtocolError} If the base URL protocol is not either `http` or `https`.
|
|
1162
|
-
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#httpinterceptorcreateoptions `httpInterceptor.create(options)` API reference}
|
|
1163
|
-
*/
|
|
1164
|
-
declare function createHttpInterceptor<Schema extends HttpSchema>(options: LocalHttpInterceptorOptions): LocalHttpInterceptor<Schema>;
|
|
1165
|
-
declare function createHttpInterceptor<Schema extends HttpSchema>(options: RemoteHttpInterceptorOptions): RemoteHttpInterceptor<Schema>;
|
|
1166
|
-
declare function createHttpInterceptor<Schema extends HttpSchema>(options: HttpInterceptorOptions): LocalHttpInterceptor<Schema> | RemoteHttpInterceptor<Schema>;
|
|
1167
|
-
|
|
1168
|
-
/** Default HTTP interceptor settings. */
|
|
1169
|
-
declare class HttpInterceptorNamespaceDefault {
|
|
1170
|
-
local: {
|
|
1171
|
-
/**
|
|
1172
|
-
* Gets or sets the default strategy for local unhandled requests. If a request does not start with the base URL of
|
|
1173
|
-
* any local interceptors, this strategy will be used. If a function is provided, it will be called with the
|
|
1174
|
-
* unhandled request.
|
|
1175
|
-
*
|
|
1176
|
-
* You can override this default for specific interceptors by using `onUnhandledRequest` in
|
|
1177
|
-
* {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#httpinterceptorcreateoptions `httpInterceptor.create(options)`}.
|
|
1178
|
-
*
|
|
1179
|
-
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#unhandled-requests Unhandled requests}
|
|
1180
|
-
*/
|
|
1181
|
-
get onUnhandledRequest(): UnhandledRequestStrategy.Local;
|
|
1182
|
-
set onUnhandledRequest(strategy: UnhandledRequestStrategy.Local);
|
|
1183
|
-
};
|
|
1184
|
-
remote: {
|
|
1185
|
-
/**
|
|
1186
|
-
* Gets or sets the default strategy for remote unhandled requests. If a request does not start with the base URL of
|
|
1187
|
-
* any remote interceptors, this strategy will be used. If a function is provided, it will be called with the
|
|
1188
|
-
* unhandled request.
|
|
1189
|
-
*
|
|
1190
|
-
* You can override this default for specific interceptors by using `onUnhandledRequest` in
|
|
1191
|
-
* {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#httpinterceptorcreateoptions `httpInterceptor.create(options)`}.
|
|
1192
|
-
*
|
|
1193
|
-
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#unhandled-requests Unhandled requests}
|
|
1194
|
-
*/
|
|
1195
|
-
get onUnhandledRequest(): UnhandledRequestStrategy.Remote;
|
|
1196
|
-
set onUnhandledRequest(strategy: UnhandledRequestStrategy.Remote);
|
|
1197
|
-
};
|
|
1198
|
-
constructor();
|
|
1199
|
-
}
|
|
1200
|
-
/**
|
|
1201
|
-
* A namespace of interceptor resources for mocking HTTP requests.
|
|
1202
|
-
*
|
|
1203
|
-
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#httpinterceptor `HttpInterceptor` API reference}
|
|
1204
|
-
*/
|
|
1205
|
-
declare class HttpInterceptorNamespace {
|
|
1206
|
-
/**
|
|
1207
|
-
* Creates an HTTP interceptor.
|
|
1208
|
-
*
|
|
1209
|
-
* @param options The options for the interceptor.
|
|
1210
|
-
* @returns The created HTTP interceptor.
|
|
1211
|
-
* @throws {InvalidURLError} If the base URL is invalid.
|
|
1212
|
-
* @throws {UnsupportedURLProtocolError} If the base URL protocol is not either `http` or `https`.
|
|
1213
|
-
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#httpinterceptorcreateoptions `httpInterceptor.create(options)` API reference}
|
|
1214
|
-
*/
|
|
1215
|
-
create: typeof createHttpInterceptor;
|
|
1216
|
-
/** Default HTTP interceptor settings. */
|
|
1217
|
-
default: Readonly<HttpInterceptorNamespaceDefault>;
|
|
1218
|
-
}
|
|
1219
|
-
|
|
1220
|
-
/**
|
|
1221
|
-
* Error thrown when a value is not valid JSON. HTTP interceptors might throw this error when trying to parse the body
|
|
1222
|
-
* of a request or response with the header `'content-type': 'application/json'`, if the content cannot be parsed to
|
|
1223
|
-
* JSON.
|
|
1224
|
-
*/
|
|
1225
|
-
declare class InvalidJSONError extends SyntaxError {
|
|
1226
|
-
constructor(value: string);
|
|
1227
|
-
}
|
|
1228
|
-
|
|
1229
|
-
/**
|
|
1230
|
-
* Error thrown when a value is not valid {@link https://developer.mozilla.org/docs/Web/API/FormData FormData}. HTTP
|
|
1231
|
-
* interceptors might throw this error when trying to parse the body of a request or response with the header
|
|
1232
|
-
* `'content-type': 'multipart/form-data'`, if the content cannot be parsed to form data.
|
|
1233
|
-
*/
|
|
1234
|
-
declare class InvalidFormDataError extends SyntaxError {
|
|
1235
|
-
constructor(value: string);
|
|
1236
|
-
}
|
|
1237
|
-
|
|
1238
|
-
/**
|
|
1239
|
-
* An error thrown when the interceptor is running and some operation requires it to be stopped first.
|
|
1240
|
-
*
|
|
1241
|
-
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#http-interceptorstart `interceptor.start()` API reference}
|
|
1242
|
-
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#http-interceptorstop `interceptor.stop()` API reference}
|
|
1243
|
-
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#http-interceptorisrunning `interceptor.isRunning` API reference}
|
|
1244
|
-
*/
|
|
1245
|
-
declare class RunningHttpInterceptorError extends Error {
|
|
1246
|
-
constructor(additionalMessage: string);
|
|
1247
|
-
}
|
|
1248
|
-
|
|
1249
|
-
/**
|
|
1250
|
-
* An error thrown when the interceptor is not running and it's not possible to use the mocking utilities.
|
|
1251
|
-
*
|
|
1252
|
-
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#http-interceptorstart `interceptor.start()` API reference}
|
|
1253
|
-
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#http-interceptorstop `interceptor.stop()` API reference}
|
|
1254
|
-
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#http-interceptorisrunning `interceptor.isRunning` API reference}
|
|
1255
|
-
*/
|
|
1256
|
-
declare class NotRunningHttpInterceptorError extends Error {
|
|
1257
|
-
constructor();
|
|
1258
|
-
}
|
|
1259
|
-
|
|
1260
|
-
/**
|
|
1261
|
-
* An error thrown when an unknown interceptor platform is detected. Currently, the platforms `node` and `browser` are
|
|
1262
|
-
* supported.
|
|
1263
|
-
*
|
|
1264
|
-
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#http-interceptorplatform `interceptor.platform` API reference}
|
|
1265
|
-
*/
|
|
1266
|
-
declare class UnknownHttpInterceptorPlatformError extends Error {
|
|
1267
|
-
constructor();
|
|
1268
|
-
}
|
|
1269
|
-
|
|
1270
|
-
declare class UnknownHttpInterceptorTypeError extends TypeError {
|
|
1271
|
-
constructor(unknownType: unknown);
|
|
1272
|
-
}
|
|
1273
|
-
|
|
1274
|
-
/**
|
|
1275
|
-
* An error thrown when the browser mock service worker is not found.
|
|
1276
|
-
*
|
|
1277
|
-
* @see {@link https://github.com/zimicjs/zimic/wiki/cli‐zimic‐browser#zimic-browser-init `zimic-interceptor browser init` API reference}
|
|
1278
|
-
*/
|
|
1279
|
-
declare class UnregisteredBrowserServiceWorkerError extends Error {
|
|
1280
|
-
constructor();
|
|
1281
|
-
static matchesRawError(error: unknown): boolean;
|
|
1282
|
-
}
|
|
1283
|
-
|
|
1284
|
-
/**
|
|
1285
|
-
* Error thrown when trying to access requests when the interceptor is not configured to do so.
|
|
1286
|
-
*
|
|
1287
|
-
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#saving-requests Saving intercepted requests}
|
|
1288
|
-
*/
|
|
1289
|
-
declare class DisabledRequestSavingError extends TypeError {
|
|
1290
|
-
constructor();
|
|
1291
|
-
}
|
|
1292
|
-
|
|
1293
1267
|
declare class TimesDeclarationPointer extends Error {
|
|
1294
1268
|
constructor(minNumberOfRequests: number, maxNumberOfRequests?: number);
|
|
1295
1269
|
}
|
|
@@ -1300,7 +1274,7 @@ interface TimesCheckErrorOptions {
|
|
|
1300
1274
|
declarationPointer: TimesDeclarationPointer | undefined;
|
|
1301
1275
|
unmatchedRequestGroups: UnmatchedHttpInterceptorRequestGroup[];
|
|
1302
1276
|
hasRestrictions: boolean;
|
|
1303
|
-
|
|
1277
|
+
requestSaving: HttpInterceptorRequestSaving;
|
|
1304
1278
|
}
|
|
1305
1279
|
declare class TimesCheckError extends TypeError {
|
|
1306
1280
|
constructor(options: TimesCheckErrorOptions);
|
|
@@ -1311,9 +1285,9 @@ declare class TimesCheckError extends TypeError {
|
|
|
1311
1285
|
* {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#httpinterceptor `HttpInterceptor`}.
|
|
1312
1286
|
*
|
|
1313
1287
|
* @example
|
|
1314
|
-
* import {
|
|
1288
|
+
* import { type InferHttpInterceptorSchema } from '@zimic/http';
|
|
1315
1289
|
*
|
|
1316
|
-
* const interceptor =
|
|
1290
|
+
* const interceptor = createHttpInterceptor<{
|
|
1317
1291
|
* '/users': {
|
|
1318
1292
|
* GET: {
|
|
1319
1293
|
* response: { 200: { body: User[] } };
|
|
@@ -1338,10 +1312,16 @@ declare class TimesCheckError extends TypeError {
|
|
|
1338
1312
|
type InferHttpInterceptorSchema<Interceptor> = Interceptor extends LocalHttpInterceptor<infer Schema> ? Schema : Interceptor extends RemoteHttpInterceptor<infer Schema> ? Schema : never;
|
|
1339
1313
|
|
|
1340
1314
|
/**
|
|
1341
|
-
*
|
|
1315
|
+
* Creates an HTTP interceptor.
|
|
1342
1316
|
*
|
|
1343
|
-
* @
|
|
1317
|
+
* @param options The options for the interceptor.
|
|
1318
|
+
* @returns The created HTTP interceptor.
|
|
1319
|
+
* @throws {InvalidURLError} If the base URL is invalid.
|
|
1320
|
+
* @throws {UnsupportedURLProtocolError} If the base URL protocol is not either `http` or `https`.
|
|
1321
|
+
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#createhttpinterceptoroptions `createHttpInterceptor(options)` API reference}
|
|
1344
1322
|
*/
|
|
1345
|
-
declare
|
|
1323
|
+
declare function createHttpInterceptor<Schema extends HttpSchema>(options: LocalHttpInterceptorOptions): LocalHttpInterceptor<Schema>;
|
|
1324
|
+
declare function createHttpInterceptor<Schema extends HttpSchema>(options: RemoteHttpInterceptorOptions): RemoteHttpInterceptor<Schema>;
|
|
1325
|
+
declare function createHttpInterceptor<Schema extends HttpSchema>(options: HttpInterceptorOptions): LocalHttpInterceptor<Schema> | RemoteHttpInterceptor<Schema>;
|
|
1346
1326
|
|
|
1347
|
-
export { DisabledRequestSavingError, type HttpInterceptor,
|
|
1327
|
+
export { DisabledRequestSavingError, type HttpInterceptor, type HttpInterceptorOptions, type HttpInterceptorPlatform, type HttpInterceptorRequest, type HttpInterceptorResponse, type HttpInterceptorType, type HttpRequestHandler, type HttpRequestHandlerBodyStaticRestriction, type HttpRequestHandlerComputedRestriction, type HttpRequestHandlerHeadersStaticRestriction, type HttpRequestHandlerResponseDeclaration, type HttpRequestHandlerResponseDeclarationFactory, type HttpRequestHandlerRestriction, type HttpRequestHandlerSearchParamsStaticRestriction, type HttpRequestHandlerStaticRestriction, type InferHttpInterceptorSchema, type InterceptedHttpInterceptorRequest, InvalidFormDataError, InvalidJSONError, type LocalHttpInterceptor, type LocalHttpInterceptorOptions, type LocalHttpRequestHandler, NotRunningHttpInterceptorError, type PendingRemoteHttpRequestHandler, type RemoteHttpInterceptor, type RemoteHttpInterceptorOptions, type RemoteHttpRequestHandler, RequestSavingSafeLimitExceededError, RunningHttpInterceptorError, type SyncedRemoteHttpRequestHandler, TimesCheckError, type UnhandledHttpInterceptorRequest, UnhandledRequestStrategy, UnknownHttpInterceptorPlatformError, UnknownHttpInterceptorTypeError, UnregisteredBrowserServiceWorkerError, createHttpInterceptor };
|