From 36b7918a794c5ada65837ca2f617eaaa2f01460b Mon Sep 17 00:00:00 2001 From: Sebastian Date: Fri, 13 Oct 2023 23:59:26 -0300 Subject: wip: http-client --- packages/taler-util/src/http-client/utils.ts | 62 ++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 packages/taler-util/src/http-client/utils.ts (limited to 'packages/taler-util/src/http-client/utils.ts') diff --git a/packages/taler-util/src/http-client/utils.ts b/packages/taler-util/src/http-client/utils.ts new file mode 100644 index 000000000..ecb4d14c4 --- /dev/null +++ b/packages/taler-util/src/http-client/utils.ts @@ -0,0 +1,62 @@ +import { base64FromArrayBuffer } from "../base64.js"; +import { stringToBytes } from "../taler-crypto.js"; +import { AccessToken, TalerAuthentication } from "./types.js"; + +/** + * 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}`; +} + +/** + * rfc8959 + * @param token + * @returns + */ +export function makeBearerTokenAuthHeader(token: AccessToken): string { + return `Bearer secret-token:${token}`; +} + +/** + * https://bugs.gnunet.org/view.php?id=7949 + */ +export function addPaginationParams(url: URL, pagination?: PaginationParams) { + if (!pagination) return; + if (pagination.timoutMs) { + url.searchParams.set("long_poll_ms", String(pagination.timoutMs)) + } + if (pagination.offset) { + url.searchParams.set("start", pagination.offset) + } + if (pagination.limit) { + url.searchParams.set("delta", String(pagination.limit)) + } +} + +export type UserAndPassword = { + username: string, + password: string, +} + +export type UserAndToken = { + username: string, + token: AccessToken, +} + +export type PaginationParams = { + /** + * row identifier as the starting point of the query + */ + offset?: string, + /** + * max number of element in the result response + */ + limit?: number, + /** + * milliseconds the server should wait for at least one result to be shown + */ + timoutMs?: number, +} -- cgit v1.2.3