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

[PFX-864] Express/Fastify/Middleware types #1050

Merged
merged 8 commits into from
Mar 11, 2025
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
12 changes: 12 additions & 0 deletions .changeset/sharp-buses-travel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
"@gasket/plugin-https-proxy": patch
"@gasket/plugin-middleware": patch
"@gasket/typescript-tests": patch
"@gasket/plugin-manifest": patch
"@gasket/plugin-express": patch
"@gasket/plugin-fastify": patch
"@gasket/plugin-morgan": patch
"@gasket/plugin-redux": patch
---

move middleware types to the middleware plugin
3 changes: 0 additions & 3 deletions packages/gasket-plugin-express/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ All the configurations for the plugin are added under `express` in the config:

- `compression`: true by default. Can be set to false if applying compression
differently.
- `excludedRoutesRegex`: (deprecated) renamed to more correct `middlewareInclusionRegex`.
- `middlewareInclusionRegex`: RegExp filter to apply toward request URLs to determine when Gasket middleware will run. You can use negative lookahead patterns to exclude routes like static resource paths.
- 'trustProxy': Enable trust proxy option, [see Express documentation on Express behind proxies](https://expressjs.com/en/guide/behind-proxies.html)

#### Example configuration
Expand All @@ -46,7 +44,6 @@ export default makeGasket({
],
express: {
compression: false,
middlewareInclusionRegex: /^(?!\/_next\/)/,
trustProxy: true
}
});
Expand Down
23 changes: 8 additions & 15 deletions packages/gasket-plugin-express/lib/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,22 @@
import type { MaybeAsync, MaybeMultiple, Plugin } from '@gasket/core';
import type { Application, ErrorRequestHandler, Handler } from 'express';
import type { Application, ErrorRequestHandler } from 'express';


export interface ExpressConfig {
/** Whether responses are compressed (true by default) */
compression?: boolean;
trustProxy?: boolean | string | number | Function;
}
declare module '@gasket/core' {
export interface GasketActions {
/** @deprecated */
getExpressApp(): Application;
}
export interface GasketConfig {
express?: {
/** Whether responses are compressed (true by default) */
compression?: boolean;
/** Filter for which request URLs invoke Gasket middleware */
middlewareInclusionRegex?: RegExp;
/** @deprecated */
excludedRoutesRegex?: RegExp;
trustProxy?: boolean | string | number | Function;
};
middleware?: {
plugin: string;
paths?: (string | RegExp)[];
}[];
express?: ExpressConfig;
}

export interface HookExecTypes {
middleware(app: Application): MaybeAsync<MaybeMultiple<Handler> & { paths?: (string | RegExp)[] }>;
express(app: Application): MaybeAsync<void>;
errorMiddleware(): MaybeAsync<MaybeMultiple<ErrorRequestHandler>>;
}
Expand Down
18 changes: 0 additions & 18 deletions packages/gasket-plugin-express/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,6 @@ const plugin = {
}
],
lifecycles: [
{
name: 'middleware',
method: 'exec',
description: 'Add Express style middleware',
link: 'README.md#middleware',
parent: 'createServers'
},
{
name: 'express',
method: 'exec',
Expand Down Expand Up @@ -89,17 +82,6 @@ const plugin = {
description: 'Automatic compression',
type: 'boolean',
default: true
}, {
name: 'express.excludedRoutesRegex',
link: 'README.md#configuration',
description: 'Routes to be included for Gasket middleware, based on a regex',
deprecated: true,
type: 'RegExp'
}, {
name: 'express.middlewareInclusionRegex',
link: 'README.md#configuration',
description: 'Routes to be included for Gasket middleware, based on a regex',
type: 'RegExp'
}]
};
}
Expand Down
37 changes: 10 additions & 27 deletions packages/gasket-plugin-fastify/lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,39 +11,25 @@ import type {
} from 'fastify';
import type { IncomingMessage, ServerResponse } from 'http';

export interface FastifyConfig {
/** Enable compression */
compression?: boolean;
/** Trust proxy configuration */
trustProxy?: FastifyServerOptions['trustProxy'];
/** Fastify request logging per route */
disableRequestLogging?: boolean;
}

declare module '@gasket/core' {
export interface GasketActions {
/** @deprecated */
getFastifyApp(): FastifyInstance<RawServerDefault, IncomingMessage, ServerResponse<IncomingMessage>, FastifyBaseLogger, FastifyTypeProviderDefault>;
}

export interface GasketConfig {
fastify?: {
/** Enable compression */
compression?: boolean;
/** Filter for which request URLs invoke Gasket middleware */
middlewareInclusionRegex?: RegExp;
/** @deprecated */
excludedRoutesRegex?: RegExp;
/** Trust proxy configuration */
trustProxy?: FastifyServerOptions['trustProxy'];
/** Fastify request logging per route */
disableRequestLogging?: boolean;
};
/** Middleware configuration */
middleware?: {
/** Plugin name */
plugin: string;
/** Paths for middleware */
paths?: (string | RegExp)[];
}[];
fastify?: FastifyConfig
}

/**
* Handler function type - The middie middleware is added for express
* compatibility
*/
type Handler = (req: any, res: any, next: (error?: Error) => void) => void;

/** Error handler function type */
type ErrorHandler = (
Expand All @@ -54,9 +40,6 @@ declare module '@gasket/core' {
) => void;

export interface HookExecTypes {
middleware(
app: FastifyInstance<RawServerDefault, IncomingMessage, ServerResponse<IncomingMessage>, FastifyBaseLogger, FastifyTypeProviderDefault>
): MaybeAsync<MaybeMultiple<Handler> & { paths?: (string | RegExp)[] }>;
fastify(app: FastifyInstance<RawServerDefault, IncomingMessage, ServerResponse<IncomingMessage>, FastifyBaseLogger, FastifyTypeProviderDefault>): MaybeAsync<void>;
errorMiddleware(): MaybeAsync<MaybeMultiple<ErrorHandler>>;
}
Expand Down
7 changes: 0 additions & 7 deletions packages/gasket-plugin-fastify/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,6 @@ const plugin = {
}
],
lifecycles: [
{
name: 'middleware',
method: 'exec',
description: 'Add Express style middleware for Fastify',
link: 'README.md#middleware',
parent: 'createServers'
},
{
name: 'fastify',
method: 'exec',
Expand Down
3 changes: 2 additions & 1 deletion packages/gasket-plugin-https-proxy/lib/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Plugin, MaybeAsync } from '@gasket/core';
import type { ServerOptions as ProxyServerOptions, Server as ProxyServer } from 'http-proxy';
import type { ServerOptions as ProxyServerOptions } from 'http-proxy';
import ProxyServer from 'http-proxy';
import type { RequireAtLeastOne } from '@gasket/plugin-https';

interface BaseHttpsProxyConfig extends ProxyServerOptions {
Expand Down
1 change: 1 addition & 0 deletions packages/gasket-plugin-manifest/lib/middleware.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/// <reference types="@gasket/plugin-service-worker" />
/// <reference types="@gasket/plugin-express" />
/// <reference types="@gasket/plugin-middleware" />

const escapeRegex = require('escape-string-regexp');
const { gatherManifestData } = require('./utils');
Expand Down
1 change: 1 addition & 0 deletions packages/gasket-plugin-manifest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"@gasket/plugin-fastify": "^7.3.1",
"@gasket/plugin-logger": "^7.3.0",
"@gasket/plugin-metadata": "^7.3.0",
"@gasket/plugin-middleware": "workspace:^",
"@gasket/plugin-service-worker": "^7.3.0",
"@types/express": "^4.17.21",
"@types/jest": "^29.5.14",
Expand Down
40 changes: 36 additions & 4 deletions packages/gasket-plugin-middleware/lib/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,31 @@
import type { Plugin, MaybeAsync, MaybeMultiple, Handler } from '@gasket/core';
import type { Plugin, MaybeAsync, MaybeMultiple, Gasket } from '@gasket/core';
import type { FastifyInstance } from 'fastify'
import type { Application as ExpressApplication } from 'express';

declare module 'fastify' {
interface FastifyReply {
locals: Record<string, any>;
}
}

declare module '@gasket/plugin-fastify' {
interface FastifyConfig {
/** Filter for which request URLs invoke Gasket middleware */
middlewareInclusionRegex?: RegExp;
/** @deprecated */
excludedRoutesRegex?: RegExp;
}
}

declare module '@gasket/plugin-express' {
interface ExpressConfig {
/** Filter for which request URLs invoke Gasket middleware */
middlewareInclusionRegex?: RegExp;
/** @deprecated */
excludedRoutesRegex?: RegExp;
}
}

declare module 'express-serve-static-core' {
interface Response {
/*
Expand All @@ -19,11 +39,23 @@ declare module 'express-serve-static-core' {
}
}

export type Handler = (req: any, res: any, next: (error?: Error) => void) => void;

type App = FastifyInstance | ExpressApplication;

declare module '@gasket/core' {
export interface HookExecTypes {
middleware(): MaybeAsync<MaybeMultiple<Handler> & {
paths?: (string | RegExp)[]
}>;
middleware(gasket: Gasket, app: App): MaybeAsync<MaybeMultiple<Handler> >;
}

export interface GasketConfig {
/** Middleware configuration */
middleware?: {
/** Plugin name */
plugin: string;
/** Paths for middleware */
paths?: (string | RegExp)[];
}[];
}
}

Expand Down
3 changes: 2 additions & 1 deletion packages/gasket-plugin-middleware/lib/internal.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import type {
FastifyBaseLogger,
RawServerDefault
} from 'fastify';
import type { Gasket, Plugin, MaybeMultiple, Handler } from '@gasket/core';
import type { Gasket, Plugin, MaybeMultiple } from '@gasket/core';
import type { Handler } from './index';

/** Type alias for Fastify application with HTTP/2 support */
type FastifyApp<
Expand Down
1 change: 1 addition & 0 deletions packages/gasket-plugin-morgan/lib/middleware.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/// <reference types="@gasket/plugin-express" />
/// <reference types="@gasket/plugin-logger" />
/// <reference types="@gasket/plugin-middleware" />

const morgan = require('morgan');
const split = require('split');
Expand Down
1 change: 1 addition & 0 deletions packages/gasket-plugin-morgan/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"@gasket/core": "^7.3.0",
"@gasket/plugin-express": "^7.3.0",
"@gasket/plugin-metadata": "^7.3.0",
"@gasket/plugin-middleware": "^7.3.0",
"@types/jest": "^29.5.14",
"@types/morgan": "^1.9.9",
"@types/node": "^20.17.19",
Expand Down
1 change: 1 addition & 0 deletions packages/gasket-plugin-redux/lib/middleware.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint require-atomic-updates: warn */
/// <reference types="@gasket/plugin-express" />
/// <reference types="@gasket/plugin-logger" />
/// <reference types="@gasket/plugin-middleware" />

/**
* Configure middleware
Expand Down
2 changes: 1 addition & 1 deletion packages/gasket-plugin-redux/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"@gasket/plugin-express": "^7.3.0",
"@gasket/plugin-logger": "^7.3.0",
"@gasket/plugin-metadata": "^7.3.0",
"@gasket/plugin-middleware": "^7.3.0",
"@gasket/plugin-middleware": "workspace:^",
"@gasket/plugin-webpack": "^7.3.0",
"@gasket/redux": "^7.3.0",
"@types/jest": "^29.5.14",
Expand Down
1 change: 1 addition & 0 deletions packages/gasket-typescript-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"@gasket/plugin-logger": "^7.3.0",
"@gasket/plugin-manifest": "^7.3.0",
"@gasket/plugin-metadata": "^7.3.0",
"@gasket/plugin-middleware": "workspace:^",
"@gasket/plugin-mocha": "^7.3.0",
"@gasket/plugin-morgan": "^7.3.0",
"@gasket/plugin-nextjs": "^7.3.0",
Expand Down
36 changes: 0 additions & 36 deletions packages/gasket-typescript-tests/test/plugin-express.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,42 +12,6 @@ describe('@gasket/plugin-express', () => {
};
});

it('adds an excludedRoutesRegex config property', () => {
const config: GasketConfigDefinition = {
plugins: [{ name: 'example-plugin', version: '', description: '', hooks: {} }],
express: {
excludedRoutesRegex: /^(?!\/_next\/)/
}
};
});

it('adds an middlewareInclusionRegex config property', () => {
const badConfig: GasketConfigDefinition = {
// @ts-expect-error
middlewareInclusionRegex: '/api/*'
};

const goodConfig: GasketConfigDefinition = {
plugins: [{ name: 'example-plugin', version: '', description: '', hooks: {} }],
express: {
middlewareInclusionRegex: /^(?!\/_next\/)/
}
};
});

it('declares the middleware lifecycle', () => {
const hook: Hook<'middleware'> = (gasket: Gasket, app) => {
return [];
};
});

it('validates the middleware return value', () => {
// @ts-expect-error
const hook: Hook<'middleware'> = (gasket: Gasket, app: Application) => {
return 'huh?';
};
});

it('declares the express lifecycle', () => {
const hook: Hook<'express'> = (gasket: Gasket, app: Application) => {
app.use((req, res, next) => next());
Expand Down
12 changes: 7 additions & 5 deletions packages/gasket-typescript-tests/test/preset-api.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Gasket, GasketConfigDefinition, Plugin } from '@gasket/core';
import type { Handler } from '@gasket/plugin-middleware';

describe('@gasket/preset-api', () => {
const { log } = console;
Expand Down Expand Up @@ -32,12 +33,13 @@ describe('@gasket/preset-api', () => {
servers(gasket, servers) {
log(servers.http);
},
middleware(gasket) {
middleware(gasket: Gasket) {
const middleware: Handler = (req, res, next) => {
res.statusCode = 404;
res.send({ message: 'not found' });
};
return [
(req, res, next) => {
res.statusCode = 404;
res.send({ message: 'not found' });
}
middleware
];
}
}
Expand Down
Loading
Loading