diff options
author | Sebastian <sebasjm@gmail.com> | 2023-10-13 23:59:26 -0300 |
---|---|---|
committer | Sebastian <sebasjm@gmail.com> | 2023-10-13 23:59:46 -0300 |
commit | 36b7918a794c5ada65837ca2f617eaaa2f01460b (patch) | |
tree | 89c8f34e444b981089958773368ba7d4ff98ce09 /packages/taler-util/src/http-client/utils.ts | |
parent | 8ae4ad93426fa054272e22ad2ac8b0dd06710c41 (diff) |
wip: http-client
Diffstat (limited to 'packages/taler-util/src/http-client/utils.ts')
-rw-r--r-- | packages/taler-util/src/http-client/utils.ts | 62 |
1 files changed, 62 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..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, +} |