Require opt-in for conduit header manipulation
diff --git a/demo/src/index.ts b/demo/src/index.ts
index 03ad502..0d3935b 100644
--- a/demo/src/index.ts
+++ b/demo/src/index.ts
@@ -33,7 +33,7 @@
ctx: {
env,
ctx,
- conduit: createConduitServerSideClient<DemoAPI>({ name: "jottocraft" }, { baseURL: "https://yggdrasil.ns.jottocraft.com/demo/conduit/", fetcher: (input: RequestInfo | URL, init?: RequestInit) => this.fetch(new Request(input, init), env, ctx) })
+ conduit: createConduitServerSideClient<DemoAPI>({ name: "jottocraft" }, { baseURL: "https://yggdrasil.ns.jottocraft.com/demo/conduit/", fetcher: (input: RequestInfo | URL, init?: RequestInit) => this.fetch(new Request(input, init), env, ctx), allowHeaderManipulation: true })
}
}, {
debug: true,
diff --git a/yggdrasil/package.json b/yggdrasil/package.json
index 95b164e..8a5b00c 100644
--- a/yggdrasil/package.json
+++ b/yggdrasil/package.json
@@ -1,7 +1,7 @@
{
"name": "@jottocraft/yggdrasil",
"description": "A simple and flexible TypeScript library for streamlining web request handling.",
- "version": "0.0.1-alpha.9",
+ "version": "0.0.1-alpha.10",
"license": "LGPL-2.1-only",
"author": "jottocraft <[email protected]>",
"main": "lib/index.js",
diff --git a/yggdrasil/src/conduitClient.ts b/yggdrasil/src/conduitClient.ts
index a1d4dbe..0fba96b 100644
--- a/yggdrasil/src/conduitClient.ts
+++ b/yggdrasil/src/conduitClient.ts
@@ -59,6 +59,13 @@
baseURL: string;
};
+export type YggdrasilConduitServerSideClientOptions = YggdrasilConduitClientOptions & {
+ /**
+ * Set to true to allow the connected Conduit server to manipulate headers on the highest-level response.
+ */
+ allowHeaderManipulation?: boolean;
+};
+
async function doConduitRequest<ApiSpec extends YggdrasilConduitAPI>(path: string[], args: unknown[], apiCtx: ApiSpec["context"], options: YggdrasilConduitClientOptions): Promise<{
response: Response;
outcome: YggdrasilConduitResponse<unknown>;
@@ -94,21 +101,23 @@
/**
* Creates a client for calling Yggdrasil Server Conduit-powered APIs from another Yggdrasil Server-powered web or API server.
*/
-export function createConduitServerSideClient<ApiSpec extends YggdrasilConduitAPI>(apiCtx: ApiSpec["context"], options: YggdrasilConduitClientOptions): YggdrasilConduitAPIClient<ApiSpec> {
+export function createConduitServerSideClient<ApiSpec extends YggdrasilConduitAPI>(apiCtx: ApiSpec["context"], options: YggdrasilConduitServerSideClientOptions): YggdrasilConduitAPIClient<ApiSpec> {
return function conduitServerSideClient(this: YggdrasilContext<YggdrasilAppTypes>): ApiSpec["client"] {
const createAppFn = (path: string[]) => {
return new Proxy(async (...args: unknown[]) => {
const { response, outcome } = await doConduitRequest(path, args, apiCtx, options);
- const passHeadersTo: YggdrasilAdditionalHeaders = "upstreamHeaders" in this ? this.upstreamHeaders : this.additionalHeaders;
+ if (options.allowHeaderManipulation) {
+ const passHeadersTo: YggdrasilAdditionalHeaders = "upstreamHeaders" in this ? this.upstreamHeaders : this.additionalHeaders;
- for (const name in outcome.headers.set) {
- if (passHeadersTo.set[name]) continue;
- passHeadersTo.set[name] = outcome.headers.set[name];
- }
+ for (const name in outcome.headers.set) {
+ if (passHeadersTo.set[name]) continue;
+ passHeadersTo.set[name] = outcome.headers.set[name];
+ }
- for (const [name, value] of outcome.headers.append) {
- passHeadersTo.append.push([name, value]);
+ for (const [name, value] of outcome.headers.append) {
+ passHeadersTo.append.push([name, value]);
+ }
}
if (outcome.success) {