Skip to content

Commit

Permalink
feat: add des to api func
Browse files Browse the repository at this point in the history
  • Loading branch information
wangxdmm committed Mar 7, 2024
1 parent ded682a commit 6c50f6e
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 6 deletions.
4 changes: 3 additions & 1 deletion .env-cmdrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
"build": {
"DEV": false,
"PRE": false,
"PKGS": [],
"PKGS": [
"-w"
],
"DEBUG": false
},
"release": {
Expand Down
3 changes: 2 additions & 1 deletion packages/easy-axios/__tests__/restful.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ describe('resutful', async () => {

expectTypeOf(api.get).toBeFunction()
expectTypeOf(api.get).parameter(0).toMatchTypeOf<{ name: string }>()

expect(api.get.des).toBe('get::/->get')

const serveResponse = {
success: true,
data: {
Expand Down
5 changes: 4 additions & 1 deletion packages/easy-axios/restful.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ export class Restful<T> extends SetupAxios<T> {
const defMes = this.parseDef(def)
if (defMes && defMes.id) {
const { method, id, url: urlDef, meta } = defMes
result[id] = (...args) => {
const callFn = (...args) => {
let config: Config
let userInputData
if (meta?.noArgs) {
Expand Down Expand Up @@ -206,6 +206,9 @@ export class Restful<T> extends SetupAxios<T> {

return this.genHandleFunc(() => this.instance(config))
}

callFn.des = def
result[id] = callFn
}
})
return result as ExtractAPI<T>
Expand Down
11 changes: 8 additions & 3 deletions packages/easy-axios/share.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type {
AxiosResponse,
Method,
} from 'axios'
import type { Equal } from '@monan/types'
import type { AnyFn, Equal } from '@monan/types'
import { isObject } from '@monan/shared'
import type { SetupAxios } from './setupAxios'

Expand Down Expand Up @@ -310,6 +310,11 @@ export type DefineRequestFuncParams<Data> = Data extends [infer Params, infer D]
? [config?: Config]
: [data: Data, config?: Config]

export type DescribeApi<T extends AnyFn> = {
des: string
(...args: Parameters<T>): ReturnType<T>
}

// export type ComputedResponse<T, Response> = Response extends
export type ExtractAPI<T, R extends object = object> = T extends [
infer F,
Expand All @@ -332,9 +337,9 @@ export type ExtractAPI<T, R extends object = object> = T extends [
Rest,
{
[k in Id | keyof R]: k extends Id
? <const T extends DefineRequestFuncParams<DataOrDefinition>>(
? DescribeApi<<const T extends DefineRequestFuncParams<DataOrDefinition>>(
...p: T
) => DefineResponseResult<Response>
) => DefineResponseResult<Response>>
: k extends keyof R
? R[k]
: never
Expand Down

0 comments on commit 6c50f6e

Please sign in to comment.