diff options
Diffstat (limited to 'packages/taler-util')
-rw-r--r-- | packages/taler-util/src/http-common.ts | 29 |
1 files changed, 28 insertions, 1 deletions
diff --git a/packages/taler-util/src/http-common.ts b/packages/taler-util/src/http-common.ts index 4f6aaaf44..93cf9bba0 100644 --- a/packages/taler-util/src/http-common.ts +++ b/packages/taler-util/src/http-common.ts @@ -19,7 +19,12 @@ import { CancellationToken } from "./CancellationToken.js"; import { Codec } from "./codec.js"; import { j2s } from "./helpers.js"; -import { TalerError, makeErrorDetail } from "./index.js"; +import { + TalerError, + base64FromArrayBuffer, + makeErrorDetail, + stringToBytes, +} from "./index.js"; import { Logger } from "./logging.js"; import { TalerErrorCode } from "./taler-error-codes.js"; import { Duration, AbsoluteTime } from "./time.js"; @@ -306,6 +311,16 @@ export async function readSuccessResponseJsonOrThrow<T>( throwUnexpectedRequestError(httpResponse, r.talerErrorResponse); } +export async function expectSuccessResponseOrThrow<T>( + httpResponse: HttpResponse, +): Promise<void> { + if (httpResponse.status >= 200 && httpResponse.status <= 299) { + return; + } + const errResp = await readTalerErrorResponse(httpResponse); + throwUnexpectedRequestError(httpResponse, errResp); +} + export async function readSuccessResponseTextOrErrorCode<T>( httpResponse: HttpResponse, ): Promise<ResponseOrError<string>> { @@ -452,3 +467,15 @@ export function getDefaultHeaders(method: string): Record<string, string> { return headers; } + +/** + * Helper function to generate the "Authorization" HTTP header. + */ +export function makeBasicAuthHeader( + username: string, + password: string, +): string { + const auth = `${username}:${password}`; + const authEncoded: string = base64FromArrayBuffer(stringToBytes(auth)); + return `Basic ${authEncoded}`; +} |