基于 React + And Design 实现的仿微信公众号的后台模板
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
doad/packages/mock/src/utils.ts

28 lines
809 B

import { match, pathToRegexp } from 'path-to-regexp'
import {MockContext, MockMethod} from "./types";
interface MockItemNormalized<T = unknown> {
url: string
method: string
regex: RegExp
match: (path: string) => Record<string, unknown> | null
timeout?: number
statusCode?: number
response?: (ctx: MockContext) => T
}
const mockItems: MockItemNormalized[] = []
export function setMockData(data: MockMethod) {
mockItems.push({
...data,
method: ((data.method ?? 'get') as string).toUpperCase(),
regex: pathToRegexp(data.url),
match(path: string): Record<string, unknown> | null {
const urlMatch = match(data.url, { decode: decodeURIComponent })
const res = urlMatch(path)
if (!res) return null
return res.params as Record<string, unknown>
},
})
}