diff options
Diffstat (limited to 'packages/taler-util/src/operation.ts')
-rw-r--r-- | packages/taler-util/src/operation.ts | 26 |
1 files changed, 23 insertions, 3 deletions
diff --git a/packages/taler-util/src/operation.ts b/packages/taler-util/src/operation.ts index a554e1f31..02cf70196 100644 --- a/packages/taler-util/src/operation.ts +++ b/packages/taler-util/src/operation.ts @@ -30,10 +30,14 @@ import { TalerErrorDetail, } from "./index.js"; -export type OperationResult<Body, ErrorEnum> = +type OperationFailWithBodyOrNever<ErrorEnum, ErrorMap> = + ErrorEnum extends keyof ErrorMap ? OperationFailWithBody<ErrorMap> : never; + +export type OperationResult<Body, ErrorEnum, K = never> = | OperationOk<Body> - | OperationAlternative<ErrorEnum, Body> - | OperationFail<ErrorEnum>; + | OperationAlternative<ErrorEnum, any> + | OperationFail<ErrorEnum> + | OperationFailWithBodyOrNever<ErrorEnum, K>; export function isOperationOk<T, E>( c: OperationResult<T, E>, @@ -89,6 +93,15 @@ export interface OperationAlternative<T, B> { body: B; } +export interface OperationFailWithBody<B> { + type: "fail"; + + httpResp: HttpResponse; + + case: keyof B; + body: B[OperationFailWithBody<B>["case"]]; +} + export async function opSuccess<T>( resp: HttpResponse, codec: Codec<T>, @@ -109,6 +122,13 @@ export function opEmptySuccess(resp: HttpResponse): OperationOk<void> { return { type: "ok" as const, body: void 0, httpResp: resp }; } +export async function opKnownFailureWithBody<B>( + case_: keyof B, + body: B[typeof case_], +): Promise<OperationFailWithBody<B>> { + return { type: "fail", case: case_, body, httpResp: {} as any }; +} + export async function opKnownAlternativeFailure<T extends HttpStatusCode, B>( resp: HttpResponse, s: T, |