Skip to content

Commit

Permalink
fix: fixes and tune-up for next15 async request support
Browse files Browse the repository at this point in the history
  • Loading branch information
agerard-godaddy committed Nov 26, 2024
1 parent 611a641 commit 701497c
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
4 changes: 2 additions & 2 deletions packages/gasket-nextjs/lib/layout/with-gasket-data.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { request } from '../server';
import { request } from '../request';
import { injectGasketData } from '../inject-gasket-data.js'
import NextScript from 'next/script';

Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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({})
}));

Expand Down
5 changes: 3 additions & 2 deletions packages/gasket-request/lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
}));
Expand Down
17 changes: 17 additions & 0 deletions packages/gasket-request/test/request.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down

0 comments on commit 701497c

Please sign in to comment.