aboutsummaryrefslogtreecommitdiff
path: root/packages/taler-util
diff options
context:
space:
mode:
authorFlorian Dold <florian@dold.me>2023-02-23 00:52:10 +0100
committerFlorian Dold <florian@dold.me>2023-02-23 00:52:17 +0100
commit7985b0a33ffc3e258da5d73f4056384c38e626fe (patch)
tree68908cb8ac2d49551f22bb4745bdf541156b8be5 /packages/taler-util
parent7879efcff70ea73935e139f4522aedadfe755c04 (diff)
downloadwallet-core-7985b0a33ffc3e258da5d73f4056384c38e626fe.tar.xz
taler-harness: deployment tooling for tipping
Diffstat (limited to 'packages/taler-util')
-rw-r--r--packages/taler-util/src/http-common.ts18
-rw-r--r--packages/taler-util/src/http-impl.node.ts7
-rw-r--r--packages/taler-util/src/http-impl.qtart.ts17
3 files changed, 26 insertions, 16 deletions
diff --git a/packages/taler-util/src/http-common.ts b/packages/taler-util/src/http-common.ts
index 54f26e615..e5dd92567 100644
--- a/packages/taler-util/src/http-common.ts
+++ b/packages/taler-util/src/http-common.ts
@@ -59,7 +59,7 @@ export interface HttpRequestOptions {
*/
cancellationToken?: CancellationToken;
- body?: string | ArrayBuffer | Record<string, unknown>;
+ body?: string | ArrayBuffer | object;
}
/**
@@ -344,9 +344,8 @@ export function getExpiry(
return t;
}
-
export interface HttpLibArgs {
- enableThrottling?: boolean,
+ enableThrottling?: boolean;
}
export function encodeBody(body: any): ArrayBuffer {
@@ -364,3 +363,16 @@ export function encodeBody(body: any): ArrayBuffer {
}
throw new TypeError("unsupported request body type");
}
+
+export function getDefaultHeaders(method: string): Record<string, string> {
+ const headers: Record<string, string> = {};
+
+ if (method === "POST" || method === "PUT" || method === "PATCH") {
+ // Default to JSON if we have a body
+ headers["Content-Type"] = "application/json";
+ }
+
+ headers["Accept"] = "application/json";
+
+ return headers;
+}
diff --git a/packages/taler-util/src/http-impl.node.ts b/packages/taler-util/src/http-impl.node.ts
index 798b81e2d..6dfce934f 100644
--- a/packages/taler-util/src/http-impl.node.ts
+++ b/packages/taler-util/src/http-impl.node.ts
@@ -23,7 +23,7 @@ import * as http from "node:http";
import * as https from "node:https";
import { RequestOptions } from "node:http";
import { TalerError } from "./errors.js";
-import { encodeBody, HttpLibArgs } from "./http-common.js";
+import { encodeBody, getDefaultHeaders, HttpLibArgs } from "./http-common.js";
import {
DEFAULT_REQUEST_TIMEOUT_MS,
Headers,
@@ -85,8 +85,7 @@ export class HttpLibImpl implements HttpRequestLibrary {
timeoutMs = DEFAULT_REQUEST_TIMEOUT_MS;
}
- const headers = { ...opt?.headers };
- headers["Content-Type"] = "application/json";
+ const requestHeadersMap = { ...getDefaultHeaders(method), ...opt?.headers };
let reqBody: ArrayBuffer | undefined;
@@ -114,7 +113,7 @@ export class HttpLibImpl implements HttpRequestLibrary {
host: parsedUrl.hostname,
method: method,
path,
- headers: opt?.headers,
+ headers: requestHeadersMap,
};
const chunks: Uint8Array[] = [];
diff --git a/packages/taler-util/src/http-impl.qtart.ts b/packages/taler-util/src/http-impl.qtart.ts
index 954b41802..ee3d1f725 100644
--- a/packages/taler-util/src/http-impl.qtart.ts
+++ b/packages/taler-util/src/http-impl.qtart.ts
@@ -21,7 +21,7 @@
*/
import { Logger } from "@gnu-taler/taler-util";
import { TalerError } from "./errors.js";
-import { encodeBody, HttpLibArgs } from "./http-common.js";
+import { encodeBody, getDefaultHeaders, HttpLibArgs } from "./http-common.js";
import {
Headers,
HttpRequestLibrary,
@@ -54,7 +54,7 @@ export class HttpLibImpl implements HttpRequestLibrary {
}
async fetch(url: string, opt?: HttpRequestOptions): Promise<HttpResponse> {
- const method = opt?.method ?? "GET";
+ const method = (opt?.method ?? "GET").toUpperCase();
logger.trace(`Requesting ${method} ${url}`);
@@ -72,19 +72,18 @@ export class HttpLibImpl implements HttpRequestLibrary {
}
let data: ArrayBuffer | undefined = undefined;
- let headers: string[] = [];
- if (opt?.headers) {
- for (let headerName of Object.keys(opt.headers)) {
- headers.push(`${headerName}: ${opt.headers[headerName]}`);
- }
+ const requestHeadersMap = { ...getDefaultHeaders(method), ...opt?.headers };
+ let headersList: string[] = [];
+ for (let headerName of Object.keys(requestHeadersMap)) {
+ headersList.push(`${headerName}: ${requestHeadersMap[headerName]}`);
}
- if (method.toUpperCase() === "POST") {
+ if (method === "POST") {
data = encodeBody(opt?.body);
}
const res = await qjsOs.fetchHttp(url, {
method,
data,
- headers,
+ headers: headersList,
});
return {
requestMethod: method,