diff options
author | Özgür Kesim <oec-taler@kesim.org> | 2023-10-17 12:04:44 +0200 |
---|---|---|
committer | Özgür Kesim <oec-taler@kesim.org> | 2023-10-17 12:04:44 +0200 |
commit | fba664f9a3c35dfeb5bc5ac28f0baea09ff9b8a0 (patch) | |
tree | 6d069afdecade1b13914f4d13020d0331c2f4036 /packages/taler-util/src/http-client/utils.ts | |
parent | def5ecda6fc4015417779af0a829d3f8aad4dd83 (diff) | |
parent | aca3bc9423f15354913d0114cafbd4bd1782d801 (diff) |
Merge branch 'master' into age-withdrawHEADage-withdraw
Diffstat (limited to 'packages/taler-util/src/http-client/utils.ts')
-rw-r--r-- | packages/taler-util/src/http-client/utils.ts | 68 |
1 files changed, 68 insertions, 0 deletions
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..4588f945c --- /dev/null +++ b/packages/taler-util/src/http-client/utils.ts @@ -0,0 +1,68 @@ +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) + } + const order = !pagination || pagination.order === "asc" ? 1 : -1 + const limit = !pagination || !pagination.limit || pagination.limit === 0 ? 5 : Math.abs(pagination.limit) + //always send delta + url.searchParams.set("delta", String(order * 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 + * always greater than 0 + */ + limit?: number, + /** + * milliseconds the server should wait for at least one result to be shown + */ + timoutMs?: number, + /** + * order + */ + order: "asc" | "dec" +} |