Fix missing Accept header in conduit client, pass statusCtx through conduit
diff --git a/yggdrasil/package.json b/yggdrasil/package.json
index 2d0e701..f91df5d 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.13",
+ "version": "0.0.1-alpha.14",
"license": "LGPL-2.1-only",
"author": "jottocraft <[email protected]>",
"main": "lib/index.js",
diff --git a/yggdrasil/src/YggdrasilConduitServer.ts b/yggdrasil/src/YggdrasilConduitServer.ts
index e5f189a..8c98e3d 100644
--- a/yggdrasil/src/YggdrasilConduitServer.ts
+++ b/yggdrasil/src/YggdrasilConduitServer.ts
@@ -136,7 +136,7 @@
const data = await requestedApi(yggConduitCtx, ...request.parameters);
- const result: YggdrasilConduitResponse<unknown> = {
+ const result: YggdrasilConduitResponse<AppTypes["statusCtx"], unknown> = {
success: true,
data,
headers: yggConduitCtx.upstreamHeaders
diff --git a/yggdrasil/src/conduit.ts b/yggdrasil/src/conduit.ts
index 5b56774..1b69596 100644
--- a/yggdrasil/src/conduit.ts
+++ b/yggdrasil/src/conduit.ts
@@ -14,7 +14,7 @@
context: ApiCtx;
};
-export type YggdrasilConduitResponse<T> = YggdrasilConduitResponseSuccess<T> | YggdrasilConduitResponseError;
+export type YggdrasilConduitResponse<StatusCtx, T> = YggdrasilConduitResponseSuccess<T> | YggdrasilConduitResponseError<StatusCtx>;
export type YggdrasilConduitResponseCommon = {
headers: YggdrasilAdditionalHeaders;
@@ -25,10 +25,11 @@
data: T;
};
-export type YggdrasilConduitResponseError = YggdrasilConduitResponseCommon & {
+export type YggdrasilConduitResponseError<StatusCtx> = YggdrasilConduitResponseCommon & {
success: false;
error: string;
message?: string;
+ ctx?: StatusCtx;
debug?: {
stack?: string;
ygg?: Pick<YggdrasilContext<YggdrasilAppTypes>, "url" | "pathname" | "baseURL" | "additionalHeaders">;
diff --git a/yggdrasil/src/conduitClient.ts b/yggdrasil/src/conduitClient.ts
index d841a66..77448b8 100644
--- a/yggdrasil/src/conduitClient.ts
+++ b/yggdrasil/src/conduitClient.ts
@@ -28,7 +28,7 @@
async function doConduitRequest<ApiSpec extends YggdrasilConduitApi>(path: string[], args: unknown[], apiCtx: ApiSpec["apiCtx"], options: YggdrasilConduitClientOptions): Promise<{
response: Response;
- outcome: YggdrasilConduitResponse<unknown>;
+ outcome: YggdrasilConduitResponse<unknown, unknown>;
}> {
const fetcher = options.fetcher ?? fetch;
@@ -43,12 +43,13 @@
const response = await fetcher(callURL.href, {
method: "POST",
headers: {
+ "Accept": "application/json+jottocraft-ygg-res",
"Content-Type": "application/json+jottocraft-ygg-req"
},
body: JSON.stringify(request)
});
- let outcome: YggdrasilConduitResponse<unknown>;
+ let outcome: YggdrasilConduitResponse<unknown, unknown>;
if (response.headers.get("Content-Type") !== "application/json+jottocraft-ygg-res") {
outcome = { success: false, error: "YGG_CONDUIT_BAD_RESPONSE", message: "An unexpected response was received from the backend server.", headers: { set: {}, append: [] } };
} else {
@@ -83,7 +84,7 @@
if (outcome.success) {
return outcome.data;
} else {
- throw this.status(response.status, outcome.error, outcome.message);
+ throw this.status(response.status, outcome.error, { statusMessage: outcome.message, ctx: outcome.ctx });
}
}, {
get(target, name: string) {
@@ -109,7 +110,7 @@
if (outcome.success) {
return outcome.data;
} else {
- throw new YggdrasilClientStatus(response.status, outcome.error, outcome.message);
+ throw new YggdrasilClientStatus(response.status, outcome.error, { statusMessage: outcome.message, ctx: outcome.ctx });
}
}, {
get(target, name: string) {
diff --git a/yggdrasil/src/util/generateStatusPage.ts b/yggdrasil/src/util/generateStatusPage.ts
index 4c016b0..1260a84 100644
--- a/yggdrasil/src/util/generateStatusPage.ts
+++ b/yggdrasil/src/util/generateStatusPage.ts
@@ -26,10 +26,11 @@
* Formats an uncaught YggdrasilStatus exception to be passed to a Conduit client.
*/
async function generateConduitErrorPage<AppTypes extends YggdrasilAppTypes>(e: YggdrasilStatus<AppTypes>): Promise<Response> {
- const result: YggdrasilConduitResponseError = {
+ const result: YggdrasilConduitResponseError<AppTypes["statusCtx"]> = {
success: false,
error: e.statusCode,
message: e.statusMessage,
+ ctx: e.statusCtx,
headers: "upstreamHeaders" in e.ygg ? e.ygg.upstreamHeaders : { set: {}, append: [] }
};