axios-mock-plugin
Advanced tools
Comparing version 0.2.4 to 0.2.5
@@ -14,3 +14,3 @@ import { AxiosResponse } from 'axios'; | ||
addEndpoints(endpoints: EndpointsMap): void; | ||
addEndpoint(endpoint: string, handler: MockEndpoint): void; | ||
addEndpoint<R = any, P = any, Q = any, B = any>(endpoint: string, handler: MockEndpoint<R, P, Q, B>): void; | ||
removeEndpoint(endpoint: string): void; | ||
@@ -20,5 +20,5 @@ listEndpoints(): string[]; | ||
updateDefaultOptions(options: Partial<MockOptions>): void; | ||
addRequestHook(hook: (request: MockRequest) => void | Promise<void>): void; | ||
addResponseHook(hook: (response: AxiosResponse) => void | Promise<void>): void; | ||
handleRequest(axiosConfig: AxiosRequestConfigWithMock): Promise<AxiosResponse>; | ||
addRequestHook<P = any, Q = any, B = any>(hook: (request: MockRequest<P, Q, B>) => void | Promise<void>): void; | ||
addResponseHook<R = any>(hook: (response: AxiosResponse<R>) => void | Promise<void>): void; | ||
handleRequest<R = any>(axiosConfig: AxiosRequestConfigWithMock): Promise<AxiosResponse<R>>; | ||
} |
@@ -1,2 +0,2 @@ | ||
export { AxiosRequestConfigWithMock, AxiosMockerConfig, MockOptions, MockRequest, MockEndpoint, EndpointsMap, RequestHook, ResponseHook } from './types'; | ||
export { AxiosRequestConfigWithMock, AxiosMockerConfig, MockOptions, InternalMockOptions, MockRequest, MockEndpoint, EndpointsMap, RequestHook, ResponseHook } from './types'; | ||
export { AxiosMocker } from './axios-mocker'; | ||
@@ -3,0 +3,0 @@ export { attachMockInterceptor } from './attach-mock-interceptor'; |
import { AxiosResponse, InternalAxiosRequestConfig } from 'axios'; | ||
export interface AxiosRequestConfigWithMock extends InternalAxiosRequestConfig<any> { | ||
export interface AxiosRequestConfigWithMock<D = any> extends InternalAxiosRequestConfig<D> { | ||
mock?: boolean | Partial<MockOptions>; | ||
@@ -13,3 +13,3 @@ } | ||
message?: string; | ||
details?: unknown[]; | ||
details?: any; | ||
}; | ||
@@ -27,3 +27,3 @@ getDelay?: (endpoint: string, axiosConfig: AxiosRequestConfigWithMock) => number; | ||
message?: string; | ||
details?: unknown[]; | ||
details?: any; | ||
}; | ||
@@ -33,6 +33,6 @@ getDelay?: (endpoint: string, axiosConfig: AxiosRequestConfigWithMock) => number; | ||
} | ||
export interface MockRequest<Params = Record<string, unknown>, Query = Record<string, unknown>, Body = unknown> { | ||
params: Params; | ||
query: Query; | ||
body: Body; | ||
export interface MockRequest<P = any, Q = any, B = any> { | ||
params: P; | ||
query: Q; | ||
body: B; | ||
} | ||
@@ -43,3 +43,3 @@ export interface AxiosMockerConfig { | ||
} | ||
export type MockEndpoint<Params = Record<string, unknown>, Query = Record<string, unknown>, Body = unknown> = (request: MockRequest<Params, Query, Body>, axiosConfig: AxiosRequestConfigWithMock) => unknown | Promise<unknown>; | ||
export type MockEndpoint<R = any, P = any, Q = any, B = any> = (request: MockRequest<P, Q, B>, axiosConfig: AxiosRequestConfigWithMock) => R | Promise<R>; | ||
export type EndpointsMap = Map<string, MockEndpoint> | { | ||
@@ -46,0 +46,0 @@ [key: string]: MockEndpoint; |
{ | ||
"name": "axios-mock-plugin", | ||
"type": "module", | ||
"version": "0.2.4", | ||
"version": "0.2.5", | ||
"description": "Promise based mock adapter for axios.", | ||
@@ -6,0 +6,0 @@ "keywords": [ |
@@ -378,3 +378,3 @@ # Axios Mock Plugin | ||
```typescript | ||
addEndpoint(endpoint: string, handler: MockEndpoint): void | ||
addEndpoint<R = any, P = any, Q = any, B = any>(endpoint: string, handler: MockEndpoint<R, P, Q, B>): void | ||
``` | ||
@@ -384,5 +384,10 @@ | ||
- `endpoints`: *(string)*: The endpoint pattern (e.g., `"GET /users/:id"`). | ||
- `handler`: *(MockEndpoint)*: The function that handles the mock request. | ||
- `handler`: *(MockEndpoint<R, P, Q, B>)*: The function that handles the mock request. The generic type parameters represent: | ||
- **R**: The return type of the endpoint handler. | ||
- **P**: The type of URL parameters. | ||
- **Q**: The type of query parameters. | ||
- **B**: The type of the request body. | ||
#### removeEndpoint | ||
@@ -437,7 +442,11 @@ Removes a previously registered endpoint. | ||
```typescript | ||
addRequestHook(hook: (request: MockRequest) => void | Promise<void>): void | ||
addRequestHook<P = any, Q = any, B = any>(hook: (request: MockRequest<P, Q, B>) => void | Promise<void>): void | ||
``` | ||
- Parameters: | ||
- `hook`: *(function)*: A function that receives the `MockRequest` object and optionally returns a promise. | ||
- `hook`: *(function)*: A function that takes a MockRequest<P, Q, B>, where: | ||
- **P**: The type of URL parameters. | ||
- **Q**: The type of query parameters. | ||
- **B**: The type of the request body. | ||
<br>The function can return either `void` or a `Promise<void>`. | ||
@@ -448,7 +457,9 @@ #### addResponseHook | ||
```typescript | ||
addResponseHook(hook: (response: AxiosResponse) => void | Promise<void>): void | ||
addResponseHook<R = any>(hook: (response: AxiosResponse<R>) => void | Promise<void>): void | ||
``` | ||
- Parameters: | ||
- `hook`: *(function)*: A function that receives the Axios response object and optionally returns a promise. | ||
- `hook`: *(function)*: A function that takes an `AxiosResponse<R>`. | ||
- **R**: The type of the response data. | ||
<br>The function can return either `void` or a `Promise<void>`. | ||
@@ -460,3 +471,3 @@ #### handleRequest | ||
```typescript | ||
handleRequest(axiosConfig: AxiosRequestConfigWithMock): Promise<AxiosResponse> | ||
handleRequest<R = any>(axiosConfig: AxiosRequestConfigWithMock): Promise<AxiosResponse<R>> | ||
``` | ||
@@ -467,3 +478,4 @@ | ||
- Returns: | ||
- `Promise<AxiosResponse>`: A promise that resolves with a mocked Axios response. | ||
- `Promise<AxiosResponse<R>>`: A promise that resolves with a mocked Axios response. | ||
- **R** represents the type of the response data, allowing you to specify the expected structure of the response. | ||
@@ -514,10 +526,10 @@ | ||
```typescript | ||
interface MockRequest< | ||
Params = Record<string, unknown>, | ||
Query = Record<string, unknown>, | ||
Body = unknown | ||
export interface MockRequest< | ||
P = any, | ||
Q = any, | ||
B = any | ||
> { | ||
params: Params | ||
query: Query | ||
body: Body | ||
params: P; | ||
query: Q; | ||
body: B; | ||
} | ||
@@ -530,10 +542,11 @@ ``` | ||
```typescript | ||
type MockEndpoint< | ||
Params = Record<string, unknown>, | ||
Query = Record<string, unknown>, | ||
Body = unknown | ||
export type MockEndpoint< | ||
R = any, | ||
P = any, | ||
Q = any, | ||
B = any | ||
> = ( | ||
request: MockRequest<Params, Query, Body>, | ||
request: MockRequest<P, Q, B>, | ||
axiosConfig: AxiosRequestConfigWithMock | ||
) => unknown | Promise<unknown> | ||
) => R | Promise<R> | ||
``` | ||
@@ -540,0 +553,0 @@ |
68742
600