From 701497cac9edb90150e2fd5d7905d63f5d6f73ea Mon Sep 17 00:00:00 2001 From: Andrew Gerard Date: Tue, 26 Nov 2024 15:09:37 -0700 Subject: [PATCH] fix: fixes and tune-up for next15 async request support --- .../lib/layout/with-gasket-data.js | 4 ++-- .../test/layout/with-gasket-data.test.js | 2 +- packages/gasket-request/lib/request.js | 5 +++-- packages/gasket-request/test/request.test.js | 17 +++++++++++++++++ 4 files changed, 23 insertions(+), 5 deletions(-) diff --git a/packages/gasket-nextjs/lib/layout/with-gasket-data.js b/packages/gasket-nextjs/lib/layout/with-gasket-data.js index b690f95f6..046002572 100644 --- a/packages/gasket-nextjs/lib/layout/with-gasket-data.js +++ b/packages/gasket-nextjs/lib/layout/with-gasket-data.js @@ -1,4 +1,4 @@ -import { request } from '../server'; +import { request } from '../request'; import { injectGasketData } from '../inject-gasket-data.js' import NextScript from 'next/script'; @@ -29,7 +29,7 @@ function lookupIndex(bodyChildren, index = -1) { export function withGasketData(gasket, options = { index: -1 }) { const { index } = options; return layout => async props => { - const req = request(); + const req = await request(); const gasketData = req ? await gasket.actions.getPublicGasketData?.(req) ?? {} : {}; const html = await layout({ ...props }); return injectGasketData(html, gasketData, lookupIndex, index); diff --git a/packages/gasket-nextjs/test/layout/with-gasket-data.test.js b/packages/gasket-nextjs/test/layout/with-gasket-data.test.js index 086f49fb6..f92e1c91f 100644 --- a/packages/gasket-nextjs/test/layout/with-gasket-data.test.js +++ b/packages/gasket-nextjs/test/layout/with-gasket-data.test.js @@ -1,7 +1,7 @@ import { jest, expect } from '@jest/globals'; import { createElement, Children } from 'react'; -jest.unstable_mockModule('../../lib/server/request.js', () => ({ +jest.unstable_mockModule('../../lib/request/index.js', () => ({ request: jest.fn().mockReturnValue({}) })); diff --git a/packages/gasket-request/lib/request.js b/packages/gasket-request/lib/request.js index 051dab588..185a1a72c 100644 --- a/packages/gasket-request/lib/request.js +++ b/packages/gasket-request/lib/request.js @@ -5,6 +5,7 @@ import { WeakPromiseKeeper } from './keeper.js'; */ export class GasketRequest { constructor(normalizedRequest) { + console.log('normalizedRequest:', normalizedRequest); this.headers = normalizedRequest.headers; this.cookies = normalizedRequest.cookies; this.query = normalizedRequest.query; @@ -66,8 +67,8 @@ export async function makeGasketRequest(requestLike) { path ??= ''; return new GasketRequest(Object.seal({ - headers: headers.constructor.prototype.entries ? Object.fromEntries(headers.entries()) : headers, - cookies: cookies.constructor.prototype.getAll ? await objectFromCookieStore(cookies) : cookies, + headers: 'entries' in headers ? Object.fromEntries(headers.entries()) : headers, + cookies: 'getAll' in cookies ? await objectFromCookieStore(cookies) : cookies, query: query instanceof URLSearchParams ? Object.fromEntries(query) : query, path })); diff --git a/packages/gasket-request/test/request.test.js b/packages/gasket-request/test/request.test.js index 1fab3bfd8..5317db960 100644 --- a/packages/gasket-request/test/request.test.js +++ b/packages/gasket-request/test/request.test.js @@ -102,6 +102,23 @@ describe('makeGasketRequest', () => { expect(result.cookies).toEqual({ cookie1: 'value1', cookie2: 'value2' }); }); + it('handles cookie store shapes getAll', async () => { + const headers = new Map([['header1', 'value1'], ['header2', 'value2']]); + const cookieStore = { + getAll() { + return [ + { name: 'cookie1', value: 'value1' }, + { name: 'cookie2', value: 'value2' } + ]; + } + }; + const requestLike = { headers, cookies: cookieStore }; + + const result = await makeGasketRequest(requestLike); + + expect(result.cookies).toEqual({ cookie1: 'value1', cookie2: 'value2' }); + }); + it('handles no cookies', async () => { const headers = new Map([['header1', 'value1'], ['header2', 'value2']]); const requestLike = { headers };