Skip to content

Commit

Permalink
feat(requestMockInterceptor): support handler overload
Browse files Browse the repository at this point in the history
  • Loading branch information
haoziqaq committed Dec 16, 2023
1 parent 5280b6a commit 9b2ccaa
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,25 @@ axle.useRequestInterceptor(
requestMockInterceptor({
// url and handler mapping
mapping: {
'/user/getUser': () => ({
data: {
code: 200,
message: 'success',
data: {
id: 1,
name: 'Mock Jack Ma',
},
}
}),

'/user/getUsers': {
// request delay time (ms)
delay: 1000,
method: 'get',
handler: () => ({
data: {
code: 200,
message: 'success',
data: [
{
id: 1,
Expand All @@ -25,7 +38,6 @@ axle.useRequestInterceptor(
name: 'Mock Tom',
},
],
message: 'success',
},
}),
},
Expand Down
21 changes: 17 additions & 4 deletions packages/axle/src/interceptors/requestMockInterceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { AxiosError } from 'axios'
import { minimatch } from 'minimatch'
import type { RequestInterceptor } from '../instance'
import type { AxiosInterceptorOptions, AxiosRequestConfig, AxiosResponse } from 'axios'
import { isFunction } from '@varlet/shared'

export type RequestMockInterceptorMappingValue = {
handler: (config: AxiosRequestConfig) => { data: any; status?: number; statusText?: string }
Expand All @@ -11,7 +12,7 @@ export type RequestMockInterceptorMappingValue = {
}

export interface RequestMockInterceptorOptions {
mapping?: Record<string, RequestMockInterceptorMappingValue>
mapping?: Record<string, RequestMockInterceptorMappingValue | RequestMockInterceptorMappingValue['handler']>
include?: string[]
exclude?: string[]
axiosInterceptorOptions?: AxiosInterceptorOptions
Expand All @@ -37,6 +38,14 @@ function settle(
}
}

function normalizeMapping(mapping: RequestMockInterceptorOptions['mapping']) {
return Object.entries(mapping ?? {}).reduce((normalizedMapping, [key, value]) => {
const normalizedValue: RequestMockInterceptorMappingValue = isFunction(value) ? { handler: value } : value
normalizedMapping[key] = normalizedValue
return normalizedMapping
}, {} as Record<string, RequestMockInterceptorMappingValue>)
}

export function requestMockInterceptor(options: RequestMockInterceptorOptions = {}): RequestInterceptor {
return {
onFulfilled(config) {
Expand All @@ -45,10 +54,14 @@ export function requestMockInterceptor(options: RequestMockInterceptorOptions =
return config
}

const mapping = normalizeMapping(options.mapping)

const findMappingRecord = () =>
Object.entries(options.mapping ?? {}).find(
([key, value]) => minimatch(config.url ?? '', key) && config.method === (value.method ?? 'get')
)
Object.entries(mapping).find(([key, value]) => {
const isMatchUrl = minimatch(config.url ?? '', key)
const isMatchMethod = value.method != null ? config.method === value.method : true
return isMatchUrl && isMatchMethod
})

const mappingRecord = findMappingRecord()
if (!mappingRecord) {
Expand Down
35 changes: 16 additions & 19 deletions packages/playground/src/request/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,22 @@ axle.useRequestInterceptor(

requestMockInterceptor({
mapping: {
'/mock/**': {
delay: 1000,
handler: () => ({
data: {
code: 200,
data: [
{
id: 1,
name: 'Mock Jack Ma',
},
{
id: 2,
name: 'Mock Tom',
},
],
message: 'success',
},
}),
},
'/mock/**': () => ({
data: {
code: 200,
data: [
{
id: 1,
name: 'Mock Jack Ma',
},
{
id: 2,
name: 'Mock Tom',
},
],
message: 'success',
},
}),
},
}),

Expand Down

0 comments on commit 9b2ccaa

Please sign in to comment.