Skip to content

Commit

Permalink
fix: tighten types
Browse files Browse the repository at this point in the history
  • Loading branch information
agerard-godaddy committed Nov 21, 2024
1 parent 7f8e0f2 commit cb90cb8
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 16 deletions.
5 changes: 5 additions & 0 deletions packages/gasket-nextjs/lib/request/index.js
Original file line number Diff line number Diff line change
@@ -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<Headers, GasketRequest>}
*/
const keeper = new WeakPromiseKeeper();

/** @type {import('.').request} */
Expand Down
13 changes: 10 additions & 3 deletions packages/gasket-request/lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Record<string, string>>;

Expand All @@ -33,7 +32,12 @@ export class GasketRequest {
path: string
}

export class WeakPromiseKeeper extends WeakMap {}
export class WeakPromiseKeeper<Key extends WeakKey = WeakKey, Value = any> {
set(key: Key, value: Promise<Value>): this
get(key: Key): Promise<Value> | Value
has(key: Key): boolean
delete(key: Key): boolean
}

/**
* Get a normalized request object for GasketActions
Expand All @@ -45,3 +49,6 @@ type RequestActionWrapperFn<Result, Args extends Array<unknown>> = (gasket: Gask

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>;


const map = new WeakPromiseKeeper<GasketRequest, any>();
2 changes: 1 addition & 1 deletion packages/gasket-request/lib/keeper.js
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
27 changes: 15 additions & 12 deletions packages/gasket-request/lib/request.js
Original file line number Diff line number Diff line change
@@ -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<Headers|Record<string,string>, GasketRequest>}
*/
const keeper = new WeakPromiseKeeper();

/**
Expand All @@ -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}
*/
Expand Down
5 changes: 5 additions & 0 deletions packages/gasket-request/lib/wrappers.js
Original file line number Diff line number Diff line change
@@ -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<GasketRequest>}
*/
const keeper = new WeakPromiseKeeper();

return async (gasket, req, ...args) => {
Expand Down

0 comments on commit cb90cb8

Please sign in to comment.