Skip to content

Commit 4c70da7

Browse files
authoredMay 13, 2024··
fix: add back generics to httpincoming (#237)
Disappeared in the JSDoc rewrite
1 parent 707b397 commit 4c70da7

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed
 

‎lib/http-incoming.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,20 @@ const urlFromRequest = (request) => {
1010
};
1111

1212
/**
13+
* @template {Record<string, unknown>} [T=Record<string, unknown>]
1314
* @typedef {object} PodiumHttpIncoming
1415
* @property {string} name
1516
* @property {object} context
1617
* @property {object} view
1718
* @property {URL} url
1819
* @property {Array<import('./asset-css.js').CssAsset | string>} css
1920
* @property {Array<import('./asset-js.js').JavaScriptAsset | string>} js
20-
* @property {object} [params]
21+
* @property {T} [params]
2122
* @property {boolean} [development]
2223
* @property {boolean} [proxy]
2324
*/
2425

26+
/** @template {Record<string, unknown>} [T=Record<string, unknown>] */
2527
export default class HttpIncoming {
2628
#development;
2729
#response;
@@ -48,15 +50,17 @@ export default class HttpIncoming {
4850
#js;
4951

5052
/**
53+
* @constructor
5154
* @param {object} [request={}] The incoming HTTP request
5255
* @param {object} [response={}] The HTTP response
53-
* @param {object} [params={}] Parameters such as locale. Typically res.locals.
56+
* @param {T} [params={}] Parameters such as locale. Typically res.locals.
5457
*
5558
* @example
5659
* ```js
5760
* const incoming = new HttpIncoming(req, res, res.locals);
5861
* ```
5962
*/
63+
// @ts-expect-error Not happy about the generics, but this is safe
6064
constructor(request = {}, response = {}, params = {}) {
6165
this.#development = false;
6266
this.#response = response;
@@ -143,6 +147,9 @@ export default class HttpIncoming {
143147
throw new Error('Cannot set read-only property.');
144148
}
145149

150+
/**
151+
* @returns {T}
152+
*/
146153
get params() {
147154
return this.#params;
148155
}
@@ -201,7 +208,7 @@ export default class HttpIncoming {
201208
}
202209

203210
/**
204-
* @returns {PodiumHttpIncoming}
211+
* @returns {PodiumHttpIncoming<T>}
205212
*/
206213
toJSON() {
207214
return {

‎tests/http-incoming.test.js

+15
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ tap.test('PodiumHttpIncoming.params - set value', (t) => {
125125
const incoming = new HttpIncoming(ADVANCED_REQ, SIMPLE_RES);
126126
t.throws(
127127
() => {
128+
// @ts-ignore Testing bad input
128129
incoming.params = 'foo';
129130
},
130131
/Cannot set read-only property./,
@@ -311,3 +312,17 @@ tap.test(
311312
t.end();
312313
},
313314
);
315+
316+
tap.test('generic typing works as expected', (t) => {
317+
// really only here for tsc
318+
319+
/**
320+
* @template {{ [key: string]: unknown }} T
321+
* @param {( incoming: HttpIncoming<T>, fragment: string, ...args: unknown[]) => string} fn
322+
* @returns {void}
323+
*/
324+
// eslint-disable-next-line no-unused-vars
325+
function view(fn) {}
326+
t.ok(view);
327+
t.end();
328+
});

0 commit comments

Comments
 (0)
Please sign in to comment.