From 48dba672a51731bc9b4d4ec1a4691bdc0481d121 Mon Sep 17 00:00:00 2001 From: fxy060608 Date: Tue, 6 Apr 2021 20:59:21 +0800 Subject: [PATCH] chore: types --- packages/uni-api/src/helpers/api/index.ts | 87 ++++++++++++----------- 1 file changed, 44 insertions(+), 43 deletions(-) diff --git a/packages/uni-api/src/helpers/api/index.ts b/packages/uni-api/src/helpers/api/index.ts index a859ab6e5..be472e010 100644 --- a/packages/uni-api/src/helpers/api/index.ts +++ b/packages/uni-api/src/helpers/api/index.ts @@ -25,6 +25,50 @@ type API_TYPES = | typeof API_TYPE_SYNC | typeof API_TYPE_ASYNC +interface AsyncMethodOptionLike { + success?: (...args: any[]) => void +} + +type PromisifySuccessResult = P extends { + success: any +} + ? void + : P extends { fail: any } + ? void + : P extends { complete: any } + ? void + : Promise + +type TaskApiLike = (args: any) => any + +type AsyncApiLike = (args: any) => Promise | void + +type AsyncApiOptions = Parameters[0] + +type AsyncApiRes = Parameters< + Exclude +>[0] + +type AsyncApiRequired =

( + args: P +) => PromisifySuccessResult> + +type AsyncApiOptional =

( + args?: P +) => PromisifySuccessResult> + +interface AsyncApiOptionalOptions { + success?: any + fail?: any + complete?: any +} + +type AsyncApi< + T extends AsyncMethodOptionLike +> = AsyncApiOptionalOptions extends T + ? AsyncApiOptional + : AsyncApiRequired + function formatApiArgs(args: any[], options?: ApiOptions) { const params = args[0] if ( @@ -145,8 +189,6 @@ export function defineOffApi( ) as unknown) as T } -type TaskApiLike = (args: any) => any - export function defineTaskApi>( name: string, fn: ( @@ -178,47 +220,6 @@ export function defineSyncApi( options ) as unknown) as T } -interface AsyncMethodOptionLike { - success?: (...args: any[]) => void -} - -type PromisifySuccessResult = P extends { - success: any -} - ? void - : P extends { fail: any } - ? void - : P extends { complete: any } - ? void - : Promise - -type AsyncApiLike = (args: any) => Promise | void - -type AsyncApiOptions = Parameters[0] - -type AsyncApiRes = Parameters< - Exclude ->[0] - -type AsyncApiRequired =

( - args: P -) => PromisifySuccessResult> - -type AsyncApiOptional =

( - args?: P -) => PromisifySuccessResult> - -interface AsyncApiOptionalOptions { - success?: any - fail?: any - complete?: any -} - -type AsyncApi< - T extends AsyncMethodOptionLike -> = AsyncApiOptionalOptions extends T - ? AsyncApiOptional - : AsyncApiRequired export function defineAsyncApi>( name: string, -- GitLab