Make ygg.ctx a getter instead of a function; clone the response before modifying; version bump
diff --git a/demo/src/routes/goodbye.ts b/demo/src/routes/goodbye.ts
index 22ffba2..1faa1e9 100644
--- a/demo/src/routes/goodbye.ts
+++ b/demo/src/routes/goodbye.ts
@@ -4,6 +4,6 @@
import { DemoServer } from "../server";
export const goodbye = DemoServer.defineRoute("GET", "/goodbye", async (ygg) => {
- const goodbyeMessage = await ygg.ctx().conduit().goodbye();
+ const goodbyeMessage = await ygg.ctx.conduit().goodbye();
return new Response(goodbyeMessage, { status: 200 });
});
\ No newline at end of file
diff --git a/docs/docs/features/conduit.md b/docs/docs/features/conduit.md
index b8dc52c..c8b2e70 100644
--- a/docs/docs/features/conduit.md
+++ b/docs/docs/features/conduit.md
@@ -50,7 +50,7 @@
For example, TypeScript would stop you from calling `client.this.path.does.not.exist()`,
but this would work at runtime for any arbitrary path (this example makes a request to `/this/path/does/not/exist`).
- The Conduit server client must be called from a Yggdrasil context. This means that you must include it as
- a top-level function in your ctx, and call it via ygg.ctx().\<the name of your conduit client\>().
+ a top-level function in your ctx, and call it via ygg.ctx.\<the name of your conduit client\>().
Please refer to the "Context" documentation for more information.
## Implementation
@@ -153,7 +153,7 @@
AnotherDemoServer.handleRequest(request, async (ygg) => {
// Call the Conduit API
- const greeting = await ygg.ctx().conduit().hello("morning");
+ const greeting = await ygg.ctx.conduit().hello("morning");
// Do whatever you want based on the outcome
}, {
diff --git a/docs/docs/features/context.md b/docs/docs/features/context.md
index dded0e8..245695e 100644
--- a/docs/docs/features/context.md
+++ b/docs/docs/features/context.md
@@ -43,8 +43,8 @@
Any top-level member functions present in your app context will be automatically
bound to the appropriate context, so you can use `this` inside them to access
-the context without requiring a separate `ygg` parameter (this is why `ygg.ctx` is
-a function instead of an object). Please note that this strategy is intended only
+the context without requiring a separate `ygg` parameter.
+Please note that this strategy is intended only
for accessing whatever context is available. If you specifically need a Conduit
context, for example, it is recommended to use an explicit `ygg` parameter to
enforce that.
\ No newline at end of file
diff --git a/docs/docs/index.md b/docs/docs/index.md
index 0511c35..04308c1 100644
--- a/docs/docs/index.md
+++ b/docs/docs/index.md
@@ -51,7 +51,7 @@
status: 200
});
},
- // Your custom request context, available as ygg.ctx() in request handlers.
+ // Your custom request context, available as ygg.ctx in request handlers.
{
env, ctx
},
diff --git a/yggdrasil/package.json b/yggdrasil/package.json
index 5b2e22c..4e9e3b7 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.1",
+ "version": "0.0.1-alpha.2",
"license": "LGPL-2.1-only",
"author": "jottocraft <[email protected]>",
"main": "lib/index.js",
diff --git a/yggdrasil/src/YggdrasilServer.ts b/yggdrasil/src/YggdrasilServer.ts
index 8f4454c..04b8b1a 100644
--- a/yggdrasil/src/YggdrasilServer.ts
+++ b/yggdrasil/src/YggdrasilServer.ts
@@ -49,7 +49,7 @@
const baseURL = options.baseURL ? new URL(options.baseURL) : undefined;
const requestedPath = url.pathname;
-
+
let normalizedPath: string;
if (requestedPath === "/") {
normalizedPath = "/";
@@ -85,7 +85,7 @@
assert(condition, ...args) {
if (!condition) {
throw this.status(...args);
- }
+ }
},
server: this,
request,
@@ -99,7 +99,7 @@
relative: relativePath
},
baseURL,
- ctx() {
+ get ctx() {
const boundCtx: AppContext = {} as AppContext;
for (const key in ctx) {
@@ -141,9 +141,12 @@
} else {
throw e;
}
- }
+ }
}
+ // Make response mutable
+ response = new Response(response.body, response);
+
applyHeaders(response, ygg.additionalHeaders);
return response;
diff --git a/yggdrasil/src/context.ts b/yggdrasil/src/context.ts
index a22b239..c09d083 100644
--- a/yggdrasil/src/context.ts
+++ b/yggdrasil/src/context.ts
@@ -61,7 +61,7 @@
/** This server's base URL. */
baseURL?: URL;
/** Custom app context associated with this request. */
- ctx: () => AppContext;
+ ctx: AppContext;
/** Yggdrasil Server options. */
options: YggdrasilServerContextOptions<AppContext>;
/** Headers that will be set/appended to the final response. */