Skip to content

Commit

Permalink
fix: returning a new Response should work
Browse files Browse the repository at this point in the history
  • Loading branch information
ematipico committed Apr 13, 2023
1 parent 76b5da0 commit fb79199
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 15 deletions.
20 changes: 15 additions & 5 deletions packages/astro/src/core/endpoint/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export function createAPIContext({
props: Record<string, any>;
adapterName?: string;
}): APIContext {
return {
const context = {
cookies: new AstroCookies(request),
request,
params,
Expand All @@ -62,7 +62,6 @@ export function createAPIContext({
});
},
url: new URL(request.url),
// @ts-expect-error
get clientAddress() {
if (!(clientAddressSymbol in request)) {
if (adapterName) {
Expand All @@ -77,8 +76,19 @@ export function createAPIContext({

return Reflect.get(request, clientAddressSymbol);
},
locals: Reflect.get(request, clientLocalsSymbol),
};
} as APIContext;

// We define a custom property, so we can correctly the value passed to locals
Object.defineProperty(context, 'locals', {
get() {
return Reflect.get(request, clientLocalsSymbol);
},
set(val) {
if (typeof val !== 'object') throw new AstroError(AstroErrorData.LocalsNotAnObject);
Reflect.set(request, clientLocalsSymbol, val);
},
});
return context;
}

export async function call(
Expand Down Expand Up @@ -121,7 +131,7 @@ export async function call(
const onRequest = middleware.onRequest as MiddlewareEndpointHandler;
response = await callMiddleware<Response | EndpointOutput>(onRequest, context, async () => {
const result = await renderEndpoint(mod, context, env.ssr);
if (!isValueSerializable(context.locals)) {
if (env.mode === 'development' && !isValueSerializable(context.locals)) {
throw new AstroError({
...AstroErrorData.LocalsNotSerializable,
message: AstroErrorData.LocalsNotSerializable.message(ctx.pathname),
Expand Down
7 changes: 3 additions & 4 deletions packages/astro/src/core/errors/errors-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -632,11 +632,10 @@ See https://docs.astro.build/en/guides/server-side-rendering/ for more informati
/**
* TODO [PLT-101] documentation
*/
LocalsNotAvailable: {
title: '`Astro.locals` is not available in current adapter.',
LocalsNotAnObject: {
title: 'Value assigned to `locals` is not accepted.',
code: 3030,
message: (adapterName: string) =>
`\`Astro.locals\` is not available in the "${adapterName}" adapter. File an issue with the adapter to add support.`,
message: `The \`locals\` can only be assigned to an object. Other values like numbers, strings, etc. are not accepted.`,
},

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/astro/src/core/middleware/callMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ export async function callMiddleware<R>(
await resolveResolve(responseResult);
return middlewarePromise;
} else {
// Middleware did not call resolve()
return await responseFunction();
// Middleware did not call resolve and returned a value
return value as R;
}
});
}
4 changes: 0 additions & 4 deletions packages/astro/test/fixtures/dev-middleware/src/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ const first = async (context, next) => {
if (context.request.url.endsWith('/lorem')) {
context.locals.name = 'ipsum';
} else if (context.request.url.endsWith('/rewrite')) {
const response = await next();
return new Response('<span>New content!!</span>', {
status: 200,
headers: response.headers,
});
} else {
context.locals.name = 'bar';
Expand All @@ -21,8 +19,6 @@ const second = async (context, next) => {
if (context.request.url.endsWith('/second')) {
context.locals.name = 'second';
} else if (context.request.url.endsWith('/redirect')) {
// before a redirect, we need to resolve the response
await next();
return context.redirect('/', 302);
}
return await next();
Expand Down

0 comments on commit fb79199

Please sign in to comment.