axios-interface
Advanced tools
Comparing version 1.4.5 to 1.4.6
@@ -22,3 +22,3 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
const enhance = request => request; | ||
const getUser = createInterface('GET', '/users', v => v, { enhance }); | ||
const getUser = createInterface('GET', '/users', { enhance }); | ||
expect.assertions(1); | ||
@@ -25,0 +25,0 @@ const response = yield getUser(); |
@@ -1,6 +0,6 @@ | ||
import { Enhance, Options, Method, UrlTemplate } from './types'; | ||
import { Options, Method, UrlTemplate } from './types'; | ||
declare const createFactory: (defaultOptions?: Options) => { | ||
request: (method: Method, url: string, params?: any, options?: Options) => Promise<any>; | ||
createInterface: <TParams = void, T = unknown>(method: Method, urlTemplate: UrlTemplate, optionsOrEnhance?: Options | Enhance, exOptions?: Options) => (params: TParams, requestOptions?: Options | undefined) => Promise<T>; | ||
createInterface: <TParams = void, T = unknown>(method: Method, urlTemplate: UrlTemplate, optionsOrEnhance?: Options, exOptions?: never) => (params: TParams, requestOptions?: Options | undefined) => Promise<T>; | ||
}; | ||
export default createFactory; |
@@ -17,2 +17,3 @@ /** | ||
if (typeof optionsOrEnhance === 'function') { | ||
console.warn('use function as options is deprecated, use {enhance: func} instead'); | ||
options = exOptions; | ||
@@ -19,0 +20,0 @@ options.enhance = optionsOrEnhance; |
@@ -55,3 +55,3 @@ "use strict"; | ||
enhance = function (request) { return request; }; | ||
getUser = createInterface('GET', '/users', function (v) { return v; }, { enhance: enhance }); | ||
getUser = createInterface('GET', '/users', { enhance: enhance }); | ||
expect.assertions(1); | ||
@@ -58,0 +58,0 @@ return [4 /*yield*/, getUser()]; |
@@ -1,6 +0,6 @@ | ||
import { Enhance, Options, Method, UrlTemplate } from './types'; | ||
import { Options, Method, UrlTemplate } from './types'; | ||
declare const createFactory: (defaultOptions?: Options) => { | ||
request: (method: Method, url: string, params?: any, options?: Options) => Promise<any>; | ||
createInterface: <TParams = void, T = unknown>(method: Method, urlTemplate: UrlTemplate, optionsOrEnhance?: Options | Enhance, exOptions?: Options) => (params: TParams, requestOptions?: Options | undefined) => Promise<T>; | ||
createInterface: <TParams = void, T = unknown>(method: Method, urlTemplate: UrlTemplate, optionsOrEnhance?: Options, exOptions?: never) => (params: TParams, requestOptions?: Options | undefined) => Promise<T>; | ||
}; | ||
export default createFactory; |
@@ -30,2 +30,3 @@ "use strict"; | ||
if (typeof optionsOrEnhance === 'function') { | ||
console.warn('use function as options is deprecated, use {enhance: func} instead'); | ||
options = exOptions; | ||
@@ -32,0 +33,0 @@ options.enhance = optionsOrEnhance; |
{ | ||
"name": "axios-interface", | ||
"version": "1.4.5", | ||
"version": "1.4.6", | ||
"description": "create interface from axios", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -20,3 +20,3 @@ # axios-interface | ||
```javascript | ||
```typescript | ||
import {createFactory} from 'axios-interface'; | ||
@@ -28,8 +28,15 @@ | ||
// 接口定义 | ||
const getUsers = createInterface('GET', '/rest/companies/{companyId}/users', options); | ||
// 或者 | ||
const getUsers = createInterface('GET', '/rest/companies/{companyId}/users', enhance, options); | ||
interface Params { | ||
companyId: string; | ||
keyword?: string; | ||
} | ||
interface User { | ||
name: string; | ||
} | ||
const getUsers = createInterface<Params, User[]>('GET', '/rest/companies/{companyId}/users', options); | ||
// 接口调用 | ||
const result = await getUsers(params); | ||
const result = await getUsers({companyId: '1', keyword: 'jack'}); // GET /rest/companies/1/users?keyword=jack | ||
``` | ||
@@ -44,3 +51,2 @@ | ||
onReject?: OnReject; | ||
enhance?: Enhance; | ||
interpolate?: RegExp; // 默认为 /{(\w+)}/g | ||
@@ -50,15 +56,10 @@ encodePathVariable?: boolean; // 是否转译 path 上的变量。如把 a/b 转译为 a%2fb。默认为 false | ||
transformDeleteParamsIntoBody?: boolean; // 改变 DELETE 是,对参数的处理方式,默认 DELETE 是不传 body 的,有需要时开启 | ||
// eslint-disable-next-line @typescript-eslint/member-ordering | ||
// 一些 axios 的配置项,常用的如 headers | ||
[whatever: string]: any; | ||
} | ||
// 其中 | ||
// 其中,以下类型都可以通过 import {OnPending} from 'axios-interface' 导入 | ||
type OnPending = <TParams>(params: TParams, options: Options) => Options | Promise<Options>; | ||
type OnResolve = <TParams>(response: AxiosResponse, params: TParams, options: Options) => any; | ||
type OnReject = <TParams>(response: AxiosError, params: TParams, options: Options) => any; | ||
type Enhance = <TRequest extends (params: any, options?: Options) => Promise<any>>( | ||
request: TRequest, | ||
options: Options, | ||
) => TRequest; | ||
``` | ||
@@ -75,13 +76,9 @@ | ||
### onPending, onResolve, onReject, enhance 的生命周期说明 | ||
### onPending, onResolve, onReject 的生命周期说明 | ||
1. 没有 enhance 的状态下 request 的整个流程 | ||
函数 call => onPending => transformRequest(axios) => 浏览器 => transformResponse(axios) => onResolve | onReject => 函数返回 | ||
函数 call => onPending => transformRequest(axios) => 浏览器 => transformResponse(axios) => onResolve | onReject => 函数返回 | ||
2. enhance() 是这个流程的外部包装 | ||
### whatever_you_want_to_custom 的使用说明 | ||
你可以在任何时候,在 options 里注入你想要的数据,在 `onPending, onResolve, onReject, enhance` 四个钩子,都会把 options 重新给你,此时,你可以根据 options 处理多种情况 | ||
你可以在任何时候,在 options 里注入你想要的数据,在 `onPending, onResolve, onReject` 三个钩子,都会把 options 重新给你,此时,你可以根据 options 处理多种情况 | ||
@@ -96,3 +93,3 @@ ### 参数声明时机 | ||
- enhance, encodePathVariable, enableUrlTemplateHeaders | ||
- encodePathVariable, enableUrlTemplateHeaders | ||
@@ -99,0 +96,0 @@ - 以下参数在 createFactory、 createInterface 和 request 时声明有效 |
2267
102274
99