Skip to content

Commit

Permalink
Handful of fixes from canary tests (#983)
Browse files Browse the repository at this point in the history
* fix: metadata plugin export

* fix: server template

* fix: align packages

* fix: fixes and tune-up for next15 async request support

* fix: clarify tests

* fix: cleanup test console

* fix: correct req type order

* fix: align dep versions

* fix: handle URLSearchParams array values
  • Loading branch information
agerard-godaddy authored Dec 11, 2024
1 parent 77ece1e commit 9ee3782
Show file tree
Hide file tree
Showing 13 changed files with 78 additions and 24 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
1 change: 1 addition & 0 deletions packages/gasket-plugin-command/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "7.0.15",
"description": "Plugin to enable other plugins to inject new gasket commands",
"type": "module",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"files": [
"lib"
Expand Down
8 changes: 5 additions & 3 deletions packages/gasket-plugin-https-proxy/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
"name": "@gasket/plugin-https-proxy",
"version": "7.0.0-canary.0",
"description": "Adds support for running an https proxy",
"type": "module",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"files": [
"lib"
],
"type": "module",
"scripts": {
"lint": "eslint .",
"lint:fix": "npm run lint -- --fix",
Expand Down Expand Up @@ -55,10 +55,12 @@
"eslintConfig": {
"extends": [
"godaddy",
"plugin:jest/recommended"
"plugin:jest/recommended",
"plugin:jsdoc/recommended-typescript-flavor"
],
"plugins": [
"unicorn"
"unicorn",
"jsdoc"
],
"rules": {
"unicorn/filename-case": "error"
Expand Down
6 changes: 3 additions & 3 deletions packages/gasket-plugin-metadata/lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,9 @@ declare module '@gasket/core' {
}
}

const plugin: Plugin = {
export default {
name: '@gasket/plugin-metadata',
version: '',
description: '',
hooks: {}
};

export = plugin;
2 changes: 1 addition & 1 deletion packages/gasket-plugin-metadata/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
"name": "@gasket/plugin-metadata",
"version": "7.0.15",
"description": "Adds metadata to gasket lifecycles",
"type": "module",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"files": [
"lib"
],
"type": "module",
"scripts": {
"lint": "eslint .",
"lint:fix": "npm run lint -- --fix",
Expand Down
3 changes: 1 addition & 2 deletions packages/gasket-plugin-nextjs/generator/next/server.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import gasket from './gasket.js';
{{/if}}
{{#if nextDevProxy }}
{{#if nextDevProxy}}
gasket.actions.startProxyServer();
{{else}}
gasket.actions.startServer();
Expand Down
4 changes: 1 addition & 3 deletions packages/gasket-preset-nextjs/lib/preset-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ export default async function presetConfig(gasket, context) {

plugins.push(pluginHttps);
plugins.push(frameworkPlugin.default || frameworkPlugin);
}

if (context.nextDevProxy) {
} else if (context.nextDevProxy) {
plugins.push(pluginHttpsProxy);
}

Expand Down
11 changes: 9 additions & 2 deletions packages/gasket-request/lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ export type RequestLike = {
*/
async function objectFromCookieStore(cookieStore: CookieStore): Promise<Record<string, string>>;

/**
* Capture the search params as a key/value object
* Necessary to capture array values
* @see https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams
*/
async function objectFromSearchParams(searchParams: URLSearchParams): Record<string, string>;

/**
* Expected request shape for GasketActions
*/
Expand Down Expand Up @@ -44,8 +51,8 @@ export class WeakPromiseKeeper<Key extends WeakKey = WeakKey, Value = any> {
*/
export async function makeGasketRequest(req: RequestLike): Promise<GasketRequest>;

type RequestActionFn<Result, Args extends Array<unknown>> = (gasket: Gasket, req: RequestLike, ...args: Args) => Promise<Result>;
type RequestActionWrapperFn<Result, Args extends Array<unknown>> = (gasket: Gasket, req: GasketRequest, ...args: Args) => Promise<Result>;
type RequestActionFn<Result, Args extends Array<unknown>> = (gasket: Gasket, req: GasketRequest, ...args: Args) => Promise<Result>;
type RequestActionWrapperFn<Result, Args extends Array<unknown>> = (gasket: Gasket, req: RequestLike, ...args: Args) => Promise<Result>;

export function withGasketRequest<Result, Args extends Array<unknown>>(actionFn: RequestActionFn<Result, Args>): RequestActionWrapperFn<Result, Args>;
export function withGasketRequestCache<Result, Args extends Array<unknown>>(actionFn: RequestActionFn<Result, Args>): RequestActionWrapperFn<Result, Args>;
19 changes: 16 additions & 3 deletions packages/gasket-request/lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,19 @@ async function objectFromCookieStore(cookieStore) {
}, {});
}

/**
* @type {import('./index.js').objectFromSearchParams}
*/
function objectFromSearchParams(searchParams) {
const entries = Object.fromEntries(searchParams);
const keys = Object.keys(entries);
return keys.reduce((acc, key) => {
const value = searchParams.getAll(key);
acc[key] = value.length === 1 ? value[0] : value;
return acc;
}, {});
}

/**
* @type {import('./index.js').makeGasketRequest}
*/
Expand Down Expand Up @@ -66,9 +79,9 @@ 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,
query: query instanceof URLSearchParams ? Object.fromEntries(query) : query,
headers: 'entries' in headers ? Object.fromEntries(headers.entries()) : headers,
cookies: 'getAll' in cookies ? await objectFromCookieStore(cookies) : cookies,
query: query instanceof URLSearchParams ? objectFromSearchParams(query) : query,
path
}));
};
Expand Down
6 changes: 3 additions & 3 deletions packages/gasket-request/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@
"typecheck:watch": "tsc --watch",
"build": "swc lib -d cjs --delete-dir-on-start --strip-leading-paths --out-file-extension cjs",
"build:watch": "npm run build -- --watch",
"postbuild:replace": "replace '.js' '.cjs' cjs/*",
"postbuild:package": "node -e \"require('fs').writeFileSync('cjs/package.json', '{}')\"",
"postbuild": "npm run postbuild:package && npm run postbuild:replace",
"prepublishOnly": "npm run build"
"prepublishOnly": "npm run build",
"postbuild:package": "node -e \"require('fs').writeFileSync('cjs/package.json', '{}')\"",
"postbuild:replace": "replace '.js' '.cjs' cjs/*"
},
"repository": {
"type": "git",
Expand Down
35 changes: 34 additions & 1 deletion packages/gasket-request/test/request.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,22 @@ describe('makeGasketRequest', () => {
expect(result.query).toEqual({ query1: 'value1', query2: 'value2' });
});

it('handles URLSearchParams array values', async () => {
const headers = new Map([['header1', 'value1'], ['header2', 'value2']]);
const query = new URLSearchParams({ query1: 'value1', query2: 'value2' });
query.append('query3', 'value3');
query.append('query3', 'value4');
const requestLike = { headers, cookies: {}, query };

const result = await makeGasketRequest(requestLike);

expect(result.query).toEqual({
query1: 'value1',
query2: 'value2',
query3: ['value3', 'value4']
});
});

it('handles no query', async () => {
const headers = new Map([['header1', 'value1'], ['header2', 'value2']]);
const requestLike = { headers, cookies: {} };
Expand All @@ -89,7 +105,7 @@ describe('makeGasketRequest', () => {
expect(result.query).toEqual({});
});

it('handles CookieStore for cookies', async () => {
it('handles Next15 style CookieStore for cookies', async () => {
const headers = new Map([['header1', 'value1'], ['header2', 'value2']]);
const cookieStore = new MockCookieStore([
{ name: 'cookie1', value: 'value1' },
Expand All @@ -102,6 +118,23 @@ describe('makeGasketRequest', () => {
expect(result.cookies).toEqual({ cookie1: 'value1', cookie2: 'value2' });
});

it('handles Next14 style CookieStore for cookies', 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
1 change: 1 addition & 0 deletions scripts/align-packages.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ const scriptsOrder = [
'typecheck:watch',
'build',
'build:watch',
'postbuild',
'prepack',
'postpack',
'prepublish',
Expand Down

0 comments on commit 9ee3782

Please sign in to comment.