Skip to content

Commit

Permalink
feat: support status (#11)
Browse files Browse the repository at this point in the history
* feat: adapt to request

* refactor: optimize code

* docs: update readme
  • Loading branch information
chouchouji authored Dec 17, 2023
1 parent 0419835 commit 0ec529b
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 16 deletions.
6 changes: 3 additions & 3 deletions packages/axle/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -250,14 +250,14 @@ Each built-in interceptor supports `include` `exclude` `axiosInterceptorOptions

#### include & exclude

It is used to request filtering to determine what request should apply the interceptor and support specifying the `method` or `glob` syntax. The usage method is as follows.
It is used to request filtering to determine what request should apply the interceptor and support specifying the `method``glob` syntax or `status` code. The usage method is as follows.

```ts
axle.useResponseInterceptor(
responseRetryInterceptor({
count: 3,
include: ['method:put', 'method:post'],
exclude: ['/system/**', '/user/addUser']
include: ['method:put', 'method:post', 'status:500'],
exclude: ['/system/**', '/user/addUser', 'status:400']
}),
)
```
Expand Down
6 changes: 3 additions & 3 deletions packages/axle/README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -250,14 +250,14 @@ axle.useResponseInterceptor(responseRetryInterceptor({ count: 3 }))

#### include & exclude

用于请求过滤,以确定什么请求应该应用该拦截器,支持指定 method 或是 glob 语法,使用方式如下。
用于请求过滤,以确定什么请求应该应用该拦截器,支持指定 `method``glob` 或者 `status`,使用方式如下。

```ts
axle.useResponseInterceptor(
responseRetryInterceptor({
count: 3,
include: ['method:put', 'method:post'],
exclude: ['/system/**', '/user/addUser']
include: ['method:put', 'method:post', 'status:500'],
exclude: ['/system/**', '/user/addUser', 'status:400']
}),
)
```
Expand Down
2 changes: 1 addition & 1 deletion packages/axle/src/interceptors/requestMd5Interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { minimatch } from 'minimatch'
import { set, get } from 'lodash-es'
import { MD5 } from 'crypto-js'
import { formDataToObject, isFormData, objectToFormData } from '../utils'
import { isArray, isString } from '@varlet/shared'
import { isString } from '@varlet/shared'

export type RequestMd5InterceptorMapping = {
url: string
Expand Down
2 changes: 1 addition & 1 deletion packages/axle/src/interceptors/responseBlobInterceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export function responseBlobInterceptor(options: ResponseBlobInterceptorOptions
return {
onFulfilled(response) {
const matcher = createMatcher(options.include, options.exclude)
if (!matcher(response.config.method ?? '', response.config.url ?? '')) {
if (!matcher(response.config.method ?? '', response.config.url ?? '', response.status)) {
return response
}

Expand Down
2 changes: 1 addition & 1 deletion packages/axle/src/interceptors/responseRetryInterceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export function responseRetryInterceptor(options: ResponseRetryInterceptorOption
onFulfilled: (response) => response,
async onRejected(error) {
const matcher = createMatcher(options.include, options.exclude)
if (!matcher(error.config.method ?? '', error.config.url ?? '') || isCancel(error)) {
if (!matcher(error.config.method ?? '', error.config.url ?? '', error?.response?.status) || isCancel(error)) {
return Promise.reject(error)
}

Expand Down
4 changes: 2 additions & 2 deletions packages/axle/src/interceptors/responseStatusInterceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export function responseStatusInterceptor(options: ResponseStatusInterceptorOpti
return {
onFulfilled: (response) => {
const matcher = createMatcher(options.include, options.exclude)
if (!matcher(response.config.method ?? '', response.config.url ?? '')) {
if (!matcher(response.config.method ?? '', response.config.url ?? '', response.status)) {
return response
}

Expand All @@ -30,7 +30,7 @@ export function responseStatusInterceptor(options: ResponseStatusInterceptorOpti

onRejected: (error) => {
const matcher = createMatcher(options.include, options.exclude)
if (!matcher(error.config.method ?? '', error.config.url ?? '')) {
if (!matcher(error.config.method ?? '', error.config.url ?? '', error?.response?.status)) {
return Promise.reject(error)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export function responseTimeoutInterceptor(options: ResponseTimeoutInterceptorOp
onFulfilled: (response) => response,
onRejected(error) {
const matcher = createMatcher(options.include, options.exclude)
if (!matcher(error.config.method ?? '', error.config.url ?? '')) {
if (!matcher(error.config.method ?? '', error.config.url ?? '', error?.response?.status)) {
return Promise.reject(error)
}

Expand Down
12 changes: 8 additions & 4 deletions packages/axle/src/matcher.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
import { minimatch } from 'minimatch'

export function matchPattern(pattern: string, method: string, url: string) {
export function matchPattern(pattern: string, method: string, url: string, status?: number | null | undefined) {
if (pattern.startsWith('status:')) {
return pattern.replace('status:', '').trim() === String(status)
}

return pattern.startsWith('method:')
? pattern.replace('method:', '').trim() === method
: minimatch(url ?? '', pattern)
}

export function createMatcher(include?: string[], exclude?: string[]) {
function matcher(method: string, url: string) {
function matcher(method: string, url: string, status?: number | null | undefined) {
if (!include && !exclude) {
return true
}

const isExclude = (exclude ?? []).some((pattern) => matchPattern(pattern, method, url))
const isExclude = (exclude ?? []).some((pattern) => matchPattern(pattern, method, url, status))

if (isExclude) {
return false
Expand All @@ -22,7 +26,7 @@ export function createMatcher(include?: string[], exclude?: string[]) {
return true
}

const isInclude = (include ?? []).some((pattern) => matchPattern(pattern, method, url))
const isInclude = (include ?? []).some((pattern) => matchPattern(pattern, method, url, status))

return isInclude
}
Expand Down

0 comments on commit 0ec529b

Please sign in to comment.