From cb90cb85b9e5fbf70e01ef85e4c990abaeaecf75 Mon Sep 17 00:00:00 2001 From: Andrew Gerard Date: Thu, 21 Nov 2024 16:48:32 -0700 Subject: [PATCH] fix: tighten types --- packages/gasket-nextjs/lib/request/index.js | 5 ++++ packages/gasket-request/lib/index.d.ts | 13 +++++++--- packages/gasket-request/lib/keeper.js | 2 +- packages/gasket-request/lib/request.js | 27 ++++++++++++--------- packages/gasket-request/lib/wrappers.js | 5 ++++ 5 files changed, 36 insertions(+), 16 deletions(-) diff --git a/packages/gasket-nextjs/lib/request/index.js b/packages/gasket-nextjs/lib/request/index.js index d12bf6963..a2b99416a 100644 --- a/packages/gasket-nextjs/lib/request/index.js +++ b/packages/gasket-nextjs/lib/request/index.js @@ -1,6 +1,11 @@ import { makeGasketRequest, WeakPromiseKeeper } from '@gasket/request'; import { cookies, headers } from 'next/headers'; +/** @typedef {import('@gasket/request').GasketRequest} GasketRequest */ + +/** + * @type {import('@gasket/request').WeakPromiseKeeper} + */ const keeper = new WeakPromiseKeeper(); /** @type {import('.').request} */ diff --git a/packages/gasket-request/lib/index.d.ts b/packages/gasket-request/lib/index.d.ts index c487567ef..b04d86f27 100644 --- a/packages/gasket-request/lib/index.d.ts +++ b/packages/gasket-request/lib/index.d.ts @@ -10,9 +10,8 @@ export type RequestLike = { } /** - * + * Capture the cookies as a key/value object * @see https://developer.mozilla.org/en-US/docs/Web/API/CookieStore/getAll - * @param cookieStore */ async function objectFromCookieStore(cookieStore: CookieStore): Promise>; @@ -33,7 +32,12 @@ export class GasketRequest { path: string } -export class WeakPromiseKeeper extends WeakMap {} +export class WeakPromiseKeeper { + set(key: Key, value: Promise): this + get(key: Key): Promise | Value + has(key: Key): boolean + delete(key: Key): boolean +} /** * Get a normalized request object for GasketActions @@ -45,3 +49,6 @@ type RequestActionWrapperFn> = (gasket: Gask export function withGasketRequest>(actionFn: RequestActionFn): RequestActionWrapperFn; export function withGasketRequestCache>(actionFn: RequestActionFn): RequestActionWrapperFn; + + +const map = new WeakPromiseKeeper(); diff --git a/packages/gasket-request/lib/keeper.js b/packages/gasket-request/lib/keeper.js index 8c82e5665..726ce7d8c 100644 --- a/packages/gasket-request/lib/keeper.js +++ b/packages/gasket-request/lib/keeper.js @@ -1,5 +1,5 @@ /** - * @type {import('./index.js').WeakPromiseKeeper} + * @type {typeof import('./index.js').WeakPromiseKeeper} */ // @ts-ignore - https://github.com/microsoft/TypeScript/issues/56664 export class WeakPromiseKeeper extends WeakMap { diff --git a/packages/gasket-request/lib/request.js b/packages/gasket-request/lib/request.js index 4ab959c65..51b46573b 100644 --- a/packages/gasket-request/lib/request.js +++ b/packages/gasket-request/lib/request.js @@ -1,5 +1,20 @@ import { WeakPromiseKeeper } from './keeper.js'; +/** + * @type {import('./index.js').GasketRequest} + */ +export class GasketRequest { + constructor(normalizedRequest) { + this.headers = normalizedRequest.headers; + this.cookies = normalizedRequest.cookies; + this.query = normalizedRequest.query; + this.path = normalizedRequest.path; + } +} + +/** + * @type {import('./index.js').WeakPromiseKeeper, GasketRequest>} + */ const keeper = new WeakPromiseKeeper(); /** @@ -13,18 +28,6 @@ async function objectFromCookieStore(cookieStore) { }, {}); } -/** - * @type {import('./index.js').GasketRequest} - */ -export class GasketRequest { - constructor(normalizedRequest) { - this.headers = normalizedRequest.headers; - this.cookies = normalizedRequest.cookies; - this.query = normalizedRequest.query; - this.path = normalizedRequest.path; - } -} - /** * @type {import('./index.js').makeGasketRequest} */ diff --git a/packages/gasket-request/lib/wrappers.js b/packages/gasket-request/lib/wrappers.js index b686bbf63..d1a5d3e4f 100644 --- a/packages/gasket-request/lib/wrappers.js +++ b/packages/gasket-request/lib/wrappers.js @@ -1,8 +1,13 @@ import { makeGasketRequest } from './request.js'; import { WeakPromiseKeeper } from './keeper.js'; +/** @typedef {import('./index.js').GasketRequest} GasketRequest */ + /** @type {typeof import('./index.d.ts').withGasketRequestCache} */ export function withGasketRequestCache(actionFn) { + /** + * @type {import('./index.js').WeakPromiseKeeper} + */ const keeper = new WeakPromiseKeeper(); return async (gasket, req, ...args) => {