Skip to content

Commit

Permalink
feat: makes the library esm-compatible (#1399)
Browse files Browse the repository at this point in the history
* 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 <kettanaito@gmail.com>
  • Loading branch information
ivanhofer and kettanaito committed Nov 7, 2022
1 parent 409512b commit 41798eb
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 5 deletions.
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -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",
Expand Down
7 changes: 6 additions & 1 deletion src/context/fetch.ts
Expand Up @@ -3,7 +3,12 @@ import { Headers } from 'headers-polyfill'
import { MockedRequest } from '../utils/request/MockedRequest'

const useFetch: (input: RequestInfo, init?: RequestInit) => Promise<Response> =
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)
Expand Down
3 changes: 2 additions & 1 deletion 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 {
Expand Down
4 changes: 2 additions & 2 deletions 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'

/**
Expand Down
2 changes: 1 addition & 1 deletion 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
Expand Down

0 comments on commit 41798eb

Please sign in to comment.