diff options
author | Florian Dold <florian@dold.me> | 2024-01-30 15:05:01 +0100 |
---|---|---|
committer | Florian Dold <florian@dold.me> | 2024-01-30 15:05:01 +0100 |
commit | 0e79a79e6b9f159b3aebc39f2e278f062c4d4410 (patch) | |
tree | 30273b53255d22a567f1f6eec4f6c11cabbd72d7 /packages/taler-util/src/operation.ts | |
parent | 2d2c43a3015a52de9a4c08a329e51b3e713380dc (diff) | |
download | wallet-core-0e79a79e6b9f159b3aebc39f2e278f062c4d4410.tar.gz wallet-core-0e79a79e6b9f159b3aebc39f2e278f062c4d4410.tar.bz2 wallet-core-0e79a79e6b9f159b3aebc39f2e278f062c4d4410.zip |
formatting, comments, always return httpResp in OperationResult
Diffstat (limited to 'packages/taler-util/src/operation.ts')
-rw-r--r-- | packages/taler-util/src/operation.ts | 155 |
1 files changed, 117 insertions, 38 deletions
diff --git a/packages/taler-util/src/operation.ts b/packages/taler-util/src/operation.ts index 0501de726..213bfeecd 100644 --- a/packages/taler-util/src/operation.ts +++ b/packages/taler-util/src/operation.ts @@ -1,65 +1,137 @@ -import { HttpResponse, readSuccessResponseJsonOrThrow, readTalerErrorResponse } from "./http-common.js"; -import { Codec, HttpStatusCode, TalerError, TalerErrorCode, TalerErrorDetail } from "./index.js"; +/* + This file is part of GNU Taler + (C) 2023-2024 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * Imports. + */ +import { + HttpResponse, + readSuccessResponseJsonOrThrow, + readTalerErrorResponse, +} from "./http-common.js"; +import { + Codec, + HttpStatusCode, + TalerError, + TalerErrorCode, + TalerErrorDetail, +} from "./index.js"; export type OperationResult<Body, ErrorEnum> = | OperationOk<Body> | OperationAlternative<ErrorEnum, Body> | OperationFail<ErrorEnum>; -export function isOperationOk<T, E>(c: OperationResult<T, E>): c is OperationOk<T> { - return c.type === "ok" +export function isOperationOk<T, E>( + c: OperationResult<T, E>, +): c is OperationOk<T> { + return c.type === "ok"; } -export function isOperationFail<T, E>(c: OperationResult<T, E>): c is OperationFail<E> { - return c.type === "fail" + +export function isOperationFail<T, E>( + c: OperationResult<T, E>, +): c is OperationFail<E> { + return c.type === "fail"; } /** * successful operation */ -export interface OperationOk<T> { - type: "ok", - body: T; +export interface OperationOk<BodyT> { + type: "ok"; + + httpResp: HttpResponse; + + /** + * Parsed response body. + */ + body: BodyT; } /** * unsuccessful operation, see details */ export interface OperationFail<T> { - type: "fail", - case: T, - detail: TalerErrorDetail, + type: "fail"; + + httpResp: HttpResponse; + + /** + * Error case (either HTTP status code or TalerErrorCode) + */ + case: T; + + detail: TalerErrorDetail; } + /** * unsuccessful operation, see body */ export interface OperationAlternative<T, B> { - type: "fail", - case: T, - body: B, + type: "fail"; + + httpResp: HttpResponse; + + case: T; + body: B; } -export async function opSuccess<T>(resp: HttpResponse, codec: Codec<T>): Promise<OperationOk<T>> { - const body = await readSuccessResponseJsonOrThrow(resp, codec) - return { type: "ok" as const, body } +export async function opSuccess<T>( + resp: HttpResponse, + codec: Codec<T>, +): Promise<OperationOk<T>> { + const body = await readSuccessResponseJsonOrThrow(resp, codec); + return { type: "ok" as const, body, httpResp: resp }; } -export function opFixedSuccess<T>(body: T): OperationOk<T> { - return { type: "ok" as const, body } + +/** + * Success case, but instead of the body we're returning a fixed response + * to the client. + */ +export function opFixedSuccess<T>(resp: HttpResponse, body: T): OperationOk<T> { + return { type: "ok" as const, body, httpResp: resp }; } -export function opEmptySuccess(): OperationOk<void> { - return { type: "ok" as const, body: void 0 } + +export function opEmptySuccess(resp: HttpResponse): OperationOk<void> { + return { type: "ok" as const, body: void 0, httpResp: resp }; } -export async function opKnownAlternativeFailure<T extends HttpStatusCode, B>(resp: HttpResponse, s: T, codec: Codec<B>): Promise<OperationAlternative<T, B>> { - const body = await readSuccessResponseJsonOrThrow(resp, codec) - return { type: "fail", case: s, body } +export async function opKnownAlternativeFailure<T extends HttpStatusCode, B>( + resp: HttpResponse, + s: T, + codec: Codec<B>, +): Promise<OperationAlternative<T, B>> { + const body = await readSuccessResponseJsonOrThrow(resp, codec); + return { type: "fail", case: s, body, httpResp: resp }; } -export async function opKnownHttpFailure<T extends HttpStatusCode>(s: T, resp: HttpResponse): Promise<OperationFail<T>> { - const detail = await readTalerErrorResponse(resp) - return { type: "fail", case: s, detail } + +export async function opKnownHttpFailure<T extends HttpStatusCode>( + s: T, + resp: HttpResponse, +): Promise<OperationFail<T>> { + const detail = await readTalerErrorResponse(resp); + return { type: "fail", case: s, detail, httpResp: resp }; } -export async function opKnownTalerFailure<T extends TalerErrorCode>(s: T, resp: HttpResponse): Promise<OperationFail<T>> { - const detail = await readTalerErrorResponse(resp) - return { type: "fail", case: s, detail } + +export async function opKnownTalerFailure<T extends TalerErrorCode>( + s: T, + resp: HttpResponse, +): Promise<OperationFail<T>> { + const detail = await readTalerErrorResponse(resp); + return { type: "fail", case: s, detail, httpResp: resp }; } export function opUnknownFailure(resp: HttpResponse, text: string): never { @@ -75,11 +147,18 @@ export function opUnknownFailure(resp: HttpResponse, text: string): never { ); } -export type ResultByMethod<TT extends object, p extends keyof TT> = TT[p] extends (...args: any[]) => infer Ret ? - Ret extends Promise<infer Result> ? - Result extends OperationResult<any, any> ? Result : never : - never : //api always use Promises - never; //error cases just for functions - -export type FailCasesByMethod<TT extends object, p extends keyof TT> = Exclude<ResultByMethod<TT, p>, OperationOk<any>> +export type ResultByMethod< + TT extends object, + p extends keyof TT, +> = TT[p] extends (...args: any[]) => infer Ret + ? Ret extends Promise<infer Result> + ? Result extends OperationResult<any, any> + ? Result + : never + : never //api always use Promises + : never; //error cases just for functions +export type FailCasesByMethod<TT extends object, p extends keyof TT> = Exclude< + ResultByMethod<TT, p>, + OperationOk<any> +>; |