Comparing version 2.2.0 to 3.0.0
import { AxiosInstance } from "axios"; | ||
import { ZodiosEndpointDescription, ZodiosRequestOptions, Body, Method, Paths, Response, ZodiosOptions } from "./zodios.types"; | ||
import { ReadonlyDeep } from "./utils.types"; | ||
import { ZodiosRequestOptions, Body, Method, Paths, Response, ZodiosOptions, ZodiosEnpointDescriptions } from "./zodios.types"; | ||
/** | ||
* zodios api client based on axios | ||
*/ | ||
export declare class Zodios<Api extends ReadonlyDeep<ZodiosEndpointDescription<any>[]>> { | ||
export declare class Zodios<Api extends ZodiosEnpointDescriptions> { | ||
private api; | ||
@@ -22,4 +21,7 @@ axiosInstance: AxiosInstance; | ||
get axios(): AxiosInstance; | ||
private createRequestInterceptor; | ||
private createResponseInterceptor; | ||
/** | ||
* use a plugin to cusomize the client | ||
* @param plugin - the plugin to use | ||
*/ | ||
use(plugin: (zodios: Zodios<Api>) => void): void; | ||
private findEndpoint; | ||
@@ -26,0 +28,0 @@ private validateResponse; |
@@ -8,2 +8,3 @@ "use strict"; | ||
const axios_1 = __importDefault(require("axios")); | ||
const api_1 = require("./plugins/api"); | ||
const utils_1 = require("./utils"); | ||
@@ -22,6 +23,6 @@ const paramsRegExp = /:([a-zA-Z_][a-zA-Z0-9_]*)/g; | ||
constructor(baseURL, api, options) { | ||
var _a; | ||
this.api = api; | ||
this.options = { | ||
validateResponse: true, | ||
usePluginApi: true, | ||
...options, | ||
@@ -38,7 +39,4 @@ }; | ||
} | ||
if (this.options.tokenProvider) { | ||
this.axiosInstance.interceptors.request.use(this.createRequestInterceptor()); | ||
if ((_a = this.options.tokenProvider) === null || _a === void 0 ? void 0 : _a.renewToken) { | ||
this.axiosInstance.interceptors.response.use(undefined, this.createResponseInterceptor()); | ||
} | ||
if (this.options.usePluginApi) { | ||
this.use((0, api_1.pluginApi)()); | ||
} | ||
@@ -52,43 +50,9 @@ } | ||
} | ||
createRequestInterceptor() { | ||
return async (config) => { | ||
var _a; | ||
config.withCredentials = true; | ||
// istanbul ignore next | ||
if (!config.headers) { | ||
config.headers = {}; | ||
} | ||
const token = await ((_a = this.options.tokenProvider) === null || _a === void 0 ? void 0 : _a.getToken()); | ||
if (token && config.method !== "get") { | ||
config.headers = { | ||
...config.headers, | ||
Authorization: `Bearer ${token}`, | ||
"Content-Type": "application/json", | ||
Accept: "application/json", | ||
}; | ||
} | ||
else if (token) { | ||
config.headers = { | ||
...config.headers, | ||
Authorization: `Bearer ${token}`, | ||
Accept: "application/json", | ||
}; | ||
} | ||
return config; | ||
}; | ||
/** | ||
* use a plugin to cusomize the client | ||
* @param plugin - the plugin to use | ||
*/ | ||
use(plugin) { | ||
plugin(this); | ||
} | ||
createResponseInterceptor() { | ||
return async (error) => { | ||
var _a, _b; | ||
if (axios_1.default.isAxiosError(error) && ((_a = this.options.tokenProvider) === null || _a === void 0 ? void 0 : _a.renewToken)) { | ||
const retryConfig = error.config; | ||
if (((_b = error.response) === null || _b === void 0 ? void 0 : _b.status) === 401 && !retryConfig.retried) { | ||
retryConfig.retried = true; | ||
this.options.tokenProvider.renewToken(); | ||
return this.axiosInstance.request(retryConfig); | ||
} | ||
} | ||
throw error; | ||
}; | ||
} | ||
findEndpoint(method, url) { | ||
@@ -95,0 +59,0 @@ return this.api.find((e) => e.method === method && e.path === url); |
import { AxiosInstance, AxiosRequestConfig } from "axios"; | ||
import type { FilterArray, MapSchemaParameters, MergeUnion, PickDefined, NeverIfEmpty, UndefinedToOptional, GetParamsKeys, ParamsToObject, SetPropsOptionalIfChildrenAreOptional } from "./utils.types"; | ||
import type { FilterArray, MapSchemaParameters, MergeUnion, PickDefined, NeverIfEmpty, UndefinedToOptional, GetParamsKeys, ParamsToObject, SetPropsOptionalIfChildrenAreOptional, ReadonlyDeep } from "./utils.types"; | ||
import { z } from "zod"; | ||
@@ -49,5 +49,5 @@ export declare type Method = "get" | "post" | "put" | "delete" | "patch" | "head" | "options"; | ||
/** | ||
* Token provider to allow zodios to inject a token into the request or renew it | ||
* use the header api interceptor? Default: true | ||
*/ | ||
tokenProvider?: TokenProvider; | ||
usePluginApi?: boolean; | ||
/** | ||
@@ -76,2 +76,3 @@ * Should zodios validate the response? Default: true | ||
}; | ||
export declare type ZodiosEnpointDescriptions = ReadonlyDeep<ZodiosEndpointDescription<any>[]>; | ||
export {}; |
{ | ||
"name": "zodios", | ||
"description": "Typescript API client with autocompletion and zod validations", | ||
"version": "2.2.0", | ||
"version": "3.0.0", | ||
"main": "lib/index.js", | ||
@@ -6,0 +6,0 @@ "typings": "lib/index.d.ts", |
@@ -67,8 +67,2 @@ <h1 align="center">Zodios</h1> | ||
] as const, | ||
// Optional Token provider | ||
{ | ||
tokenProvider: { | ||
getToken: () => Promise.resolve("token"), | ||
} | ||
} | ||
); | ||
@@ -81,2 +75,13 @@ // typed auto-complete url auto-complete params | ||
``` | ||
## Use token provider plugin | ||
Zodios comes with a plugin to inject and renew your tokens : | ||
```typescript | ||
import { pluginToken } from 'zodios/plugins/token'; | ||
apiClient.use(pluginToken({ | ||
getToken: async () => "token" | ||
})); | ||
``` | ||
## Get underlying axios instance | ||
@@ -110,3 +115,3 @@ | ||
[ ... ] as const, | ||
// Optional Axios instance | ||
// Disable validation | ||
{ | ||
@@ -113,0 +118,0 @@ validateResponse: false |
25797
17
534
119