From 391729686bcc8404a7dd48c5987ee380daf3200f Mon Sep 17 00:00:00 2001 From: Arsh <69170106+lilnasy@users.noreply.github.com> Date: Thu, 12 Oct 2023 14:36:34 +0000 Subject: [PATCH] fix(middleware): instantiate locals if the adapter does not (#8800) --- .changeset/large-colts-jump.md | 5 +++ packages/astro/src/core/endpoint/index.ts | 33 +++++++++++-------- .../test/fixtures/locals/src/middleware.ts | 6 ++++ .../test/fixtures/locals/src/pages/api.js | 4 +-- ...{foo.astro => from-astro-middleware.astro} | 0 .../src/pages/from-node-middleware.astro | 4 +++ .../integrations/node/test/locals.test.js | 33 +++++++++++++++++-- 7 files changed, 67 insertions(+), 18 deletions(-) create mode 100644 .changeset/large-colts-jump.md create mode 100644 packages/integrations/node/test/fixtures/locals/src/middleware.ts rename packages/integrations/node/test/fixtures/locals/src/pages/{foo.astro => from-astro-middleware.astro} (100%) create mode 100644 packages/integrations/node/test/fixtures/locals/src/pages/from-node-middleware.astro diff --git a/.changeset/large-colts-jump.md b/.changeset/large-colts-jump.md new file mode 100644 index 0000000000..faa265319c --- /dev/null +++ b/.changeset/large-colts-jump.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fixed an issue where attempting to assign a variable onto locals threw an error. diff --git a/packages/astro/src/core/endpoint/index.ts b/packages/astro/src/core/endpoint/index.ts index b62ba8bedf..7e6128c373 100644 --- a/packages/astro/src/core/endpoint/index.ts +++ b/packages/astro/src/core/endpoint/index.ts @@ -57,7 +57,9 @@ export function createAPIContext({ ResponseWithEncoding, url: new URL(request.url), get clientAddress() { - if (!(clientAddressSymbol in request)) { + if (clientAddressSymbol in request) { + return Reflect.get(request, clientAddressSymbol) as string; + } if (adapterName) { throw new AstroError({ ...AstroErrorData.ClientAddressNotAvailable, @@ -66,26 +68,31 @@ export function createAPIContext({ } else { throw new AstroError(AstroErrorData.StaticClientAddressNotAvailable); } + }, + get locals() { + let locals = Reflect.get(request, clientLocalsSymbol) + + if (locals === undefined) { + locals = {} + Reflect.set(request, clientLocalsSymbol, locals) } - - return Reflect.get(request, clientAddressSymbol); + + if (typeof locals !== 'object') { + throw new AstroError(AstroErrorData.LocalsNotAnObject); + } + + return locals; }, - } as APIContext; - - // We define a custom property, so we can check the value passed to locals - Object.defineProperty(context, 'locals', { - enumerable: true, - get() { - return Reflect.get(request, clientLocalsSymbol); - }, - set(val) { + // We define a custom property, so we can check the value passed to locals + set locals(val) { if (typeof val !== 'object') { throw new AstroError(AstroErrorData.LocalsNotAnObject); } else { Reflect.set(request, clientLocalsSymbol, val); } }, - }); + } satisfies APIContext; + return context; } diff --git a/packages/integrations/node/test/fixtures/locals/src/middleware.ts b/packages/integrations/node/test/fixtures/locals/src/middleware.ts new file mode 100644 index 0000000000..e349ca41d8 --- /dev/null +++ b/packages/integrations/node/test/fixtures/locals/src/middleware.ts @@ -0,0 +1,6 @@ +import { defineMiddleware } from 'astro:middleware'; + +export const onRequest = defineMiddleware(({ url, locals }, next) => { + if (url.pathname === "/from-astro-middleware") locals.foo = "baz"; + return next(); +}) diff --git a/packages/integrations/node/test/fixtures/locals/src/pages/api.js b/packages/integrations/node/test/fixtures/locals/src/pages/api.js index 8b209c5826..3c279e37bd 100644 --- a/packages/integrations/node/test/fixtures/locals/src/pages/api.js +++ b/packages/integrations/node/test/fixtures/locals/src/pages/api.js @@ -1,6 +1,6 @@ -export async function post({ locals }) { - let out = { ...locals }; +export async function POST({ locals }) { + const out = { ...locals }; return new Response(JSON.stringify(out), { headers: { diff --git a/packages/integrations/node/test/fixtures/locals/src/pages/foo.astro b/packages/integrations/node/test/fixtures/locals/src/pages/from-astro-middleware.astro similarity index 100% rename from packages/integrations/node/test/fixtures/locals/src/pages/foo.astro rename to packages/integrations/node/test/fixtures/locals/src/pages/from-astro-middleware.astro diff --git a/packages/integrations/node/test/fixtures/locals/src/pages/from-node-middleware.astro b/packages/integrations/node/test/fixtures/locals/src/pages/from-node-middleware.astro new file mode 100644 index 0000000000..224a875ecc --- /dev/null +++ b/packages/integrations/node/test/fixtures/locals/src/pages/from-node-middleware.astro @@ -0,0 +1,4 @@ +--- +const { foo } = Astro.locals; +--- +