Skip to content

Commit

Permalink
Fix type exports
Browse files Browse the repository at this point in the history
According to the old type definitions, this was correct usage:

```js
// CJS
const MockAdapter = require('axios-mock-adapter');

const mock = new MockAdapter.default(/* … */)
```

```js
// ESM
const MockAdapter = require('axios-mock-adapter');

const mock = new MockAdapter.default(/* … */)
```

With the updated type definitions, this is:

```js
// CJS
const MockAdapter = require('axios-mock-adapter');

const mock = new MockAdapter(/* … */)
```

```js
// ESM
const MockAdapter = require('axios-mock-adapter');

const mock = new MockAdapter(/* … */)
```

This has always been an issue, but it has become more apparent with the
`"module": "node16"` option introduced in TypeScript 4.7.
  • Loading branch information
remcohaszing authored and marcbachmann committed Mar 26, 2023
1 parent 66e99a8 commit fd982cd
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
26 changes: 14 additions & 12 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,18 @@ type ResponseSpecFunc = <T = any>(
headers?: any
) => MockAdapter;

export interface RequestHandler {
reply: ResponseSpecFunc;
replyOnce: ResponseSpecFunc;
passThrough(): MockAdapter;
abortRequest(): MockAdapter;
abortRequestOnce(): MockAdapter;
networkError(): MockAdapter;
networkErrorOnce(): MockAdapter;
timeout(): MockAdapter;
timeoutOnce(): MockAdapter;
declare namespace MockAdapter {
export interface RequestHandler {
reply: ResponseSpecFunc;
replyOnce: ResponseSpecFunc;
passThrough(): MockAdapter;
abortRequest(): MockAdapter;
abortRequestOnce(): MockAdapter;
networkError(): MockAdapter;
networkErrorOnce(): MockAdapter;
timeout(): MockAdapter;
timeoutOnce(): MockAdapter;
}
}

interface MockAdapterOptions {
Expand Down Expand Up @@ -50,7 +52,7 @@ type RequestMatcherFunc = (
matcher?: string | RegExp,
body?: string | AsymmetricRequestDataMatcher,
headers?: AsymmetricHeadersMatcher
) => RequestHandler;
) => AxiosAdapter.RequestHandler;

declare class MockAdapter {
constructor(axiosInstance: AxiosInstance, options?: MockAdapterOptions);
Expand All @@ -76,4 +78,4 @@ declare class MockAdapter {
onUnlink: RequestMatcherFunc;
}

export default MockAdapter;
export = MockAdapter;
4 changes: 2 additions & 2 deletions types/test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import axios from 'axios';
import MockAdapter, { RequestHandler } from 'axios-mock-adapter';
import MockAdapter = require('axios-mock-adapter');

const instance = axios.create();
const mock = new MockAdapter(instance);
Expand Down Expand Up @@ -149,6 +149,6 @@ namespace SupportsChaining {
}

namespace ExportsRequestHandlerInterface {
const handler: RequestHandler = mock.onAny();
const handler: MockAdapter.RequestHandler = mock.onAny();
handler.reply(200);
}

0 comments on commit fd982cd

Please # to comment.