From fd982cd0fc3a517d82c529fd8a1f6257a75c79d3 Mon Sep 17 00:00:00 2001 From: Remco Haszing Date: Mon, 13 Feb 2023 11:33:54 +0100 Subject: [PATCH] Fix type exports MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- types/index.d.ts | 26 ++++++++++++++------------ types/test.ts | 4 ++-- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/types/index.d.ts b/types/index.d.ts index 181dddf..0d6cd39 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -10,16 +10,18 @@ type ResponseSpecFunc = ( 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 { @@ -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); @@ -76,4 +78,4 @@ declare class MockAdapter { onUnlink: RequestMatcherFunc; } -export default MockAdapter; +export = MockAdapter; diff --git a/types/test.ts b/types/test.ts index fa90104..430919f 100644 --- a/types/test.ts +++ b/types/test.ts @@ -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); @@ -149,6 +149,6 @@ namespace SupportsChaining { } namespace ExportsRequestHandlerInterface { - const handler: RequestHandler = mock.onAny(); + const handler: MockAdapter.RequestHandler = mock.onAny(); handler.reply(200); }