From 41798ebc695e04719e198a7867be6d80e053fd0e Mon Sep 17 00:00:00 2001 From: Hofer Ivan Date: Mon, 7 Nov 2022 13:06:00 +0100 Subject: [PATCH] feat: makes the library esm-compatible (#1399) * feat: better esm support fixes #1267 * refactor: add workaround for `useFetch` * refactor: move `statuses` to `devDependencies` * fix: remove unneeded type assertion again Co-authored-by: Artem Zakharchenko --- package.json | 1 + src/context/fetch.ts | 7 ++++++- src/node/createSetupServer.ts | 3 ++- src/node/setupServer.ts | 4 ++-- src/utils/matching/matchRequestUrl.ts | 2 +- 5 files changed, 12 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 338e7dc5e..4d9708436 100644 --- a/package.json +++ b/package.json @@ -141,6 +141,7 @@ "regenerator-runtime": "^0.13.9", "rimraf": "^3.0.2", "simple-git-hooks": "^2.8.0", + "statuses": "^2.0.0", "ts-jest": "26", "ts-loader": "^9.2.6", "ts-node": "^10.1.0", diff --git a/src/context/fetch.ts b/src/context/fetch.ts index a596d43d7..0121fdeb7 100644 --- a/src/context/fetch.ts +++ b/src/context/fetch.ts @@ -3,7 +3,12 @@ import { Headers } from 'headers-polyfill' import { MockedRequest } from '../utils/request/MockedRequest' const useFetch: (input: RequestInfo, init?: RequestInit) => Promise = - isNodeProcess() ? require('node-fetch') : window.fetch + isNodeProcess() + ? (input, init) => + import('node-fetch').then(({ default: nodeFetch }) => + (nodeFetch as unknown as typeof window.fetch)(input, init), + ) + : window.fetch export const augmentRequestInit = (requestInit: RequestInit): RequestInit => { const headers = new Headers(requestInit.headers) diff --git a/src/node/createSetupServer.ts b/src/node/createSetupServer.ts index 56b501ace..dc3ee55d7 100644 --- a/src/node/createSetupServer.ts +++ b/src/node/createSetupServer.ts @@ -1,4 +1,5 @@ -import { bold } from 'chalk' +import chalk from 'chalk' +const { bold } = chalk import { isNodeProcess } from 'is-node-process' import { StrictEventEmitter } from 'strict-event-emitter' import { diff --git a/src/node/setupServer.ts b/src/node/setupServer.ts index b8de95938..3b3426528 100644 --- a/src/node/setupServer.ts +++ b/src/node/setupServer.ts @@ -1,5 +1,5 @@ -import { ClientRequestInterceptor } from '@mswjs/interceptors/lib/interceptors/ClientRequest' -import { XMLHttpRequestInterceptor } from '@mswjs/interceptors/lib/interceptors/XMLHttpRequest' +import { ClientRequestInterceptor } from '@mswjs/interceptors/lib/interceptors/ClientRequest/index.js' +import { XMLHttpRequestInterceptor } from '@mswjs/interceptors/lib/interceptors/XMLHttpRequest/index.js' import { createSetupServer } from './createSetupServer' /** diff --git a/src/utils/matching/matchRequestUrl.ts b/src/utils/matching/matchRequestUrl.ts index c97466a44..8d5d25cfc 100644 --- a/src/utils/matching/matchRequestUrl.ts +++ b/src/utils/matching/matchRequestUrl.ts @@ -1,5 +1,5 @@ import { match } from 'path-to-regexp' -import { getCleanUrl } from '@mswjs/interceptors/lib/utils/getCleanUrl' +import { getCleanUrl } from '@mswjs/interceptors/lib/utils/getCleanUrl.js' import { normalizePath } from './normalizePath' export type Path = string | RegExp