Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handful of fixes from canary tests #983

Merged
merged 9 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -10,7 +10,7 @@

/**
* Dependency name and expected version range
* @type {Object.<string,string>}

Check warning on line 13 in scripts/align-packages.js

View workflow job for this annotation

GitHub Actions / ci (20.x)

Use object shorthand or index signatures instead of `Object`, e.g., `{[key: string]: string}`

Check warning on line 13 in scripts/align-packages.js

View workflow job for this annotation

GitHub Actions / ci (22.x)

Use object shorthand or index signatures instead of `Object`, e.g., `{[key: string]: string}`
*/
const depVersions = {
'@babel/cli': '^7.23.4',
Expand Down Expand Up @@ -59,7 +59,7 @@

/**
* Peer dependency name and expected version range
* @type {Object.<string,string>}

Check warning on line 62 in scripts/align-packages.js

View workflow job for this annotation

GitHub Actions / ci (20.x)

Use object shorthand or index signatures instead of `Object`, e.g., `{[key: string]: string}`

Check warning on line 62 in scripts/align-packages.js

View workflow job for this annotation

GitHub Actions / ci (22.x)

Use object shorthand or index signatures instead of `Object`, e.g., `{[key: string]: string}`
*/
const peerDepVersions = {
'next': '^14',
Expand Down Expand Up @@ -127,6 +127,7 @@
'typecheck:watch',
'build',
'build:watch',
'postbuild',
'prepack',
'postpack',
'prepublish',
Expand Down Expand Up @@ -274,7 +275,7 @@

/**
* Check if typecheck scripts are present and adds them if not
* @param pkgJson

Check warning on line 278 in scripts/align-packages.js

View workflow job for this annotation

GitHub Actions / ci (20.x)

Missing JSDoc @param "pkgJson" description

Check warning on line 278 in scripts/align-packages.js

View workflow job for this annotation

GitHub Actions / ci (20.x)

Missing JSDoc @param "pkgJson" type

Check warning on line 278 in scripts/align-packages.js

View workflow job for this annotation

GitHub Actions / ci (22.x)

Missing JSDoc @param "pkgJson" description

Check warning on line 278 in scripts/align-packages.js

View workflow job for this annotation

GitHub Actions / ci (22.x)

Missing JSDoc @param "pkgJson" type
*/
function checkTypecheckScripts(pkgJson) {
const { scripts } = pkgJson;
Expand All @@ -287,7 +288,7 @@

/**
* Check if eslintConfig is present and adds jsdoc recommended typescript flavor
* @param pkgJson

Check warning on line 291 in scripts/align-packages.js

View workflow job for this annotation

GitHub Actions / ci (20.x)

Missing JSDoc @param "pkgJson" description

Check warning on line 291 in scripts/align-packages.js

View workflow job for this annotation

GitHub Actions / ci (20.x)

Missing JSDoc @param "pkgJson" type

Check warning on line 291 in scripts/align-packages.js

View workflow job for this annotation

GitHub Actions / ci (22.x)

Missing JSDoc @param "pkgJson" description

Check warning on line 291 in scripts/align-packages.js

View workflow job for this annotation

GitHub Actions / ci (22.x)

Missing JSDoc @param "pkgJson" type
*/
function checkEslintConfig(pkgJson) {
const { eslintConfig } = pkgJson;
Expand All @@ -307,7 +308,7 @@

/**
* Check if typescript is in devDependencies and adds it if not
* @param pkgJson

Check warning on line 311 in scripts/align-packages.js

View workflow job for this annotation

GitHub Actions / ci (20.x)

Missing JSDoc @param "pkgJson" description

Check warning on line 311 in scripts/align-packages.js

View workflow job for this annotation

GitHub Actions / ci (20.x)

Missing JSDoc @param "pkgJson" type

Check warning on line 311 in scripts/align-packages.js

View workflow job for this annotation

GitHub Actions / ci (22.x)

Missing JSDoc @param "pkgJson" description

Check warning on line 311 in scripts/align-packages.js

View workflow job for this annotation

GitHub Actions / ci (22.x)

Missing JSDoc @param "pkgJson" type
*/
function checkDevDeps(pkgJson) {
const { devDependencies } = pkgJson;
Expand All @@ -318,7 +319,7 @@

/**
* Setup typescript scripts and dependencies
* @param pkgJson

Check warning on line 322 in scripts/align-packages.js

View workflow job for this annotation

GitHub Actions / ci (20.x)

Missing JSDoc @param "pkgJson" description

Check warning on line 322 in scripts/align-packages.js

View workflow job for this annotation

GitHub Actions / ci (20.x)

Missing JSDoc @param "pkgJson" type

Check warning on line 322 in scripts/align-packages.js

View workflow job for this annotation

GitHub Actions / ci (22.x)

Missing JSDoc @param "pkgJson" description

Check warning on line 322 in scripts/align-packages.js

View workflow job for this annotation

GitHub Actions / ci (22.x)

Missing JSDoc @param "pkgJson" type
*/
function setupTypes(pkgJson) {
const { name } = pkgJson;
Expand Down
Loading