Skip to content

refactor: better types #1801

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

Merged
merged 2 commits into from
Mar 29, 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
28 changes: 14 additions & 14 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ const noop = () => {};
*/

/**
* @template {IncomingMessage} RequestInternal
* @template {ServerResponse} ResponseInternal
* @template {IncomingMessage} [RequestInternal=IncomingMessage]
* @template {ServerResponse} [ResponseInternal=ServerResponse]
* @callback ModifyResponseData
* @param {RequestInternal} req
* @param {ResponseInternal} res
Expand All @@ -75,8 +75,8 @@ const noop = () => {};
*/

/**
* @template {IncomingMessage} RequestInternal
* @template {ServerResponse} ResponseInternal
* @template {IncomingMessage} [RequestInternal=IncomingMessage]
* @template {ServerResponse} [ResponseInternal=ServerResponse]
* @typedef {Object} Context
* @property {boolean} state
* @property {Stats | MultiStats | undefined} stats
Expand All @@ -89,16 +89,16 @@ const noop = () => {};
*/

/**
* @template {IncomingMessage} RequestInternal
* @template {ServerResponse} ResponseInternal
* @template {IncomingMessage} [RequestInternal=IncomingMessage]
* @template {ServerResponse} [ResponseInternal=ServerResponse]
* @typedef {WithoutUndefined<Context<RequestInternal, ResponseInternal>, "watching">} FilledContext
*/

/** @typedef {Record<string, string | number> | Array<{ key: string, value: number | string }>} NormalizedHeaders */

/**
* @template {IncomingMessage} RequestInternal
* @template {ServerResponse} ResponseInternal
* @template {IncomingMessage} [RequestInternal=IncomingMessage]
* @template {ServerResponse} [ResponseInternal=ServerResponse]
* @typedef {NormalizedHeaders | ((req: RequestInternal, res: ResponseInternal, context: Context<RequestInternal, ResponseInternal>) => void | undefined | NormalizedHeaders) | undefined} Headers
*/

Expand All @@ -122,8 +122,8 @@ const noop = () => {};
*/

/**
* @template {IncomingMessage} RequestInternal
* @template {ServerResponse} ResponseInternal
* @template {IncomingMessage} [RequestInternal=IncomingMessage]
* @template {ServerResponse} [ResponseInternal=ServerResponse]
* @callback Middleware
* @param {RequestInternal} req
* @param {ResponseInternal} res
Expand Down Expand Up @@ -167,8 +167,8 @@ const noop = () => {};
*/

/**
* @template {IncomingMessage} RequestInternal
* @template {ServerResponse} ResponseInternal
* @template {IncomingMessage} [RequestInternal=IncomingMessage]
* @template {ServerResponse} [ResponseInternal=ServerResponse]
* @typedef {Middleware<RequestInternal, ResponseInternal> & AdditionalMethods<RequestInternal, ResponseInternal>} API
*/

Expand Down Expand Up @@ -366,7 +366,7 @@ function koaWrapper(compiler, options) {
const devMiddleware = wdm(compiler, options);

/**
* @param {any} ctx
* @param {{ req: RequestInternal, res: ResponseInternal & import("./utils/compatibleAPI").ExpectedResponse, status: number, body: Buffer | import("fs").ReadStream | { message: string }, state: Object }} ctx
* @param {Function} next
* @returns {Promise<void>}
*/
Expand All @@ -392,7 +392,7 @@ function koaWrapper(compiler, options) {
resolve();
};
/**
* @param {string | Buffer} content content
* @param {Buffer} content content
*/
res.send = (content) => {
// eslint-disable-next-line no-param-reassign
Expand Down
1 change: 1 addition & 0 deletions src/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,7 @@ function wrapper(context) {
/** @type {import("fs").Stats | Buffer | ReadStream | undefined} */
let value;

// TODO cache etag generation?
if (context.options.etag === "weak") {
value = /** @type {import("fs").Stats} */ (extra.stats);
} else {
Expand Down
1 change: 1 addition & 0 deletions src/utils/getFilenameFromUrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ function decode(input) {
}

// TODO refactor me in the next major release, this function should return `{ filename, stats, error }`
// TODO fix redirect logic when `/` at the end, like https://github.com/pillarjs/send/blob/master/index.js#L586
/**
* @template {IncomingMessage} Request
* @template {ServerResponse} Response
Expand Down
54 changes: 30 additions & 24 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ export = wdm;
* @property {number} byteLength
*/
/**
* @template {IncomingMessage} RequestInternal
* @template {ServerResponse} ResponseInternal
* @template {IncomingMessage} [RequestInternal=IncomingMessage]
* @template {ServerResponse} [ResponseInternal=ServerResponse]
* @callback ModifyResponseData
* @param {RequestInternal} req
* @param {ResponseInternal} res
Expand All @@ -52,8 +52,8 @@ export = wdm;
* @return {ResponseData}
*/
/**
* @template {IncomingMessage} RequestInternal
* @template {ServerResponse} ResponseInternal
* @template {IncomingMessage} [RequestInternal=IncomingMessage]
* @template {ServerResponse} [ResponseInternal=ServerResponse]
* @typedef {Object} Context
* @property {boolean} state
* @property {Stats | MultiStats | undefined} stats
Expand All @@ -65,14 +65,14 @@ export = wdm;
* @property {OutputFileSystem} outputFileSystem
*/
/**
* @template {IncomingMessage} RequestInternal
* @template {ServerResponse} ResponseInternal
* @template {IncomingMessage} [RequestInternal=IncomingMessage]
* @template {ServerResponse} [ResponseInternal=ServerResponse]
* @typedef {WithoutUndefined<Context<RequestInternal, ResponseInternal>, "watching">} FilledContext
*/
/** @typedef {Record<string, string | number> | Array<{ key: string, value: number | string }>} NormalizedHeaders */
/**
* @template {IncomingMessage} RequestInternal
* @template {ServerResponse} ResponseInternal
* @template {IncomingMessage} [RequestInternal=IncomingMessage]
* @template {ServerResponse} [ResponseInternal=ServerResponse]
* @typedef {NormalizedHeaders | ((req: RequestInternal, res: ResponseInternal, context: Context<RequestInternal, ResponseInternal>) => void | undefined | NormalizedHeaders) | undefined} Headers
*/
/**
Expand All @@ -94,8 +94,8 @@ export = wdm;
* @property {boolean} [lastModified]
*/
/**
* @template {IncomingMessage} RequestInternal
* @template {ServerResponse} ResponseInternal
* @template {IncomingMessage} [RequestInternal=IncomingMessage]
* @template {ServerResponse} [ResponseInternal=ServerResponse]
* @callback Middleware
* @param {RequestInternal} req
* @param {ResponseInternal} res
Expand Down Expand Up @@ -132,8 +132,8 @@ export = wdm;
* @property {Context<RequestInternal, ResponseInternal>} context
*/
/**
* @template {IncomingMessage} RequestInternal
* @template {ServerResponse} ResponseInternal
* @template {IncomingMessage} [RequestInternal=IncomingMessage]
* @template {ServerResponse} [ResponseInternal=ServerResponse]
* @typedef {Middleware<RequestInternal, ResponseInternal> & AdditionalMethods<RequestInternal, ResponseInternal>} API
*/
/**
Expand Down Expand Up @@ -207,8 +207,9 @@ declare namespace wdm {
type Compiler = import("webpack").Compiler;
type MultiCompiler = import("webpack").MultiCompiler;
type API<
RequestInternal extends import("http").IncomingMessage,
ResponseInternal extends ServerResponse,
RequestInternal extends
import("http").IncomingMessage = import("http").IncomingMessage,
ResponseInternal extends ServerResponse = ServerResponse,
> = Middleware<RequestInternal, ResponseInternal> &
AdditionalMethods<RequestInternal, ResponseInternal>;
/**
Expand Down Expand Up @@ -288,17 +289,19 @@ type ResponseData = {
byteLength: number;
};
type ModifyResponseData<
RequestInternal extends import("http").IncomingMessage,
ResponseInternal extends ServerResponse,
RequestInternal extends
import("http").IncomingMessage = import("http").IncomingMessage,
ResponseInternal extends ServerResponse = ServerResponse,
> = (
req: RequestInternal,
res: ResponseInternal,
data: Buffer | ReadStream,
byteLength: number,
) => ResponseData;
type Context<
RequestInternal extends import("http").IncomingMessage,
ResponseInternal extends ServerResponse,
RequestInternal extends
import("http").IncomingMessage = import("http").IncomingMessage,
ResponseInternal extends ServerResponse = ServerResponse,
> = {
state: boolean;
stats: Stats | MultiStats | undefined;
Expand All @@ -310,8 +313,9 @@ type Context<
outputFileSystem: OutputFileSystem;
};
type FilledContext<
RequestInternal extends import("http").IncomingMessage,
ResponseInternal extends ServerResponse,
RequestInternal extends
import("http").IncomingMessage = import("http").IncomingMessage,
ResponseInternal extends ServerResponse = ServerResponse,
> = WithoutUndefined<Context<RequestInternal, ResponseInternal>, "watching">;
type NormalizedHeaders =
| Record<string, string | number>
Expand All @@ -320,8 +324,9 @@ type NormalizedHeaders =
value: number | string;
}>;
type Headers<
RequestInternal extends import("http").IncomingMessage,
ResponseInternal extends ServerResponse,
RequestInternal extends
import("http").IncomingMessage = import("http").IncomingMessage,
ResponseInternal extends ServerResponse = ServerResponse,
> =
| NormalizedHeaders
| ((
Expand Down Expand Up @@ -356,8 +361,9 @@ type Options<
lastModified?: boolean | undefined;
};
type Middleware<
RequestInternal extends import("http").IncomingMessage,
ResponseInternal extends ServerResponse,
RequestInternal extends
import("http").IncomingMessage = import("http").IncomingMessage,
ResponseInternal extends ServerResponse = ServerResponse,
> = (
req: RequestInternal,
res: ResponseInternal,
Expand Down