Skip to content

Commit f716c9e

Browse files
[PFX-864] Express/Fastify/Middleware types (#1050)
* chore: update middlware types * chore: remove unused type * chore: adjust types * chore: fix types * chore: gen changeset
1 parent 0561cd5 commit f716c9e

File tree

19 files changed

+104
-129
lines changed

19 files changed

+104
-129
lines changed

.changeset/sharp-buses-travel.md

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
"@gasket/plugin-https-proxy": patch
3+
"@gasket/plugin-middleware": patch
4+
"@gasket/typescript-tests": patch
5+
"@gasket/plugin-manifest": patch
6+
"@gasket/plugin-express": patch
7+
"@gasket/plugin-fastify": patch
8+
"@gasket/plugin-morgan": patch
9+
"@gasket/plugin-redux": patch
10+
---
11+
12+
move middleware types to the middleware plugin

packages/gasket-plugin-express/README.md

-3
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ All the configurations for the plugin are added under `express` in the config:
3333

3434
- `compression`: true by default. Can be set to false if applying compression
3535
differently.
36-
- `excludedRoutesRegex`: (deprecated) renamed to more correct `middlewareInclusionRegex`.
37-
- `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.
3836
- 'trustProxy': Enable trust proxy option, [see Express documentation on Express behind proxies](https://expressjs.com/en/guide/behind-proxies.html)
3937

4038
#### Example configuration
@@ -46,7 +44,6 @@ export default makeGasket({
4644
],
4745
express: {
4846
compression: false,
49-
middlewareInclusionRegex: /^(?!\/_next\/)/,
5047
trustProxy: true
5148
}
5249
});

packages/gasket-plugin-express/lib/index.d.ts

+8-15
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,22 @@
11
import type { MaybeAsync, MaybeMultiple, Plugin } from '@gasket/core';
2-
import type { Application, ErrorRequestHandler, Handler } from 'express';
2+
import type { Application, ErrorRequestHandler } from 'express';
33

4+
5+
export interface ExpressConfig {
6+
/** Whether responses are compressed (true by default) */
7+
compression?: boolean;
8+
trustProxy?: boolean | string | number | Function;
9+
}
410
declare module '@gasket/core' {
511
export interface GasketActions {
612
/** @deprecated */
713
getExpressApp(): Application;
814
}
915
export interface GasketConfig {
10-
express?: {
11-
/** Whether responses are compressed (true by default) */
12-
compression?: boolean;
13-
/** Filter for which request URLs invoke Gasket middleware */
14-
middlewareInclusionRegex?: RegExp;
15-
/** @deprecated */
16-
excludedRoutesRegex?: RegExp;
17-
trustProxy?: boolean | string | number | Function;
18-
};
19-
middleware?: {
20-
plugin: string;
21-
paths?: (string | RegExp)[];
22-
}[];
16+
express?: ExpressConfig;
2317
}
2418

2519
export interface HookExecTypes {
26-
middleware(app: Application): MaybeAsync<MaybeMultiple<Handler> & { paths?: (string | RegExp)[] }>;
2720
express(app: Application): MaybeAsync<void>;
2821
errorMiddleware(): MaybeAsync<MaybeMultiple<ErrorRequestHandler>>;
2922
}

packages/gasket-plugin-express/lib/index.js

-18
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,6 @@ const plugin = {
5454
}
5555
],
5656
lifecycles: [
57-
{
58-
name: 'middleware',
59-
method: 'exec',
60-
description: 'Add Express style middleware',
61-
link: 'README.md#middleware',
62-
parent: 'createServers'
63-
},
6457
{
6558
name: 'express',
6659
method: 'exec',
@@ -89,17 +82,6 @@ const plugin = {
8982
description: 'Automatic compression',
9083
type: 'boolean',
9184
default: true
92-
}, {
93-
name: 'express.excludedRoutesRegex',
94-
link: 'README.md#configuration',
95-
description: 'Routes to be included for Gasket middleware, based on a regex',
96-
deprecated: true,
97-
type: 'RegExp'
98-
}, {
99-
name: 'express.middlewareInclusionRegex',
100-
link: 'README.md#configuration',
101-
description: 'Routes to be included for Gasket middleware, based on a regex',
102-
type: 'RegExp'
10385
}]
10486
};
10587
}

packages/gasket-plugin-fastify/lib/index.d.ts

+10-27
Original file line numberDiff line numberDiff line change
@@ -11,39 +11,25 @@ import type {
1111
} from 'fastify';
1212
import type { IncomingMessage, ServerResponse } from 'http';
1313

14+
export interface FastifyConfig {
15+
/** Enable compression */
16+
compression?: boolean;
17+
/** Trust proxy configuration */
18+
trustProxy?: FastifyServerOptions['trustProxy'];
19+
/** Fastify request logging per route */
20+
disableRequestLogging?: boolean;
21+
}
22+
1423
declare module '@gasket/core' {
1524
export interface GasketActions {
1625
/** @deprecated */
1726
getFastifyApp(): FastifyInstance<RawServerDefault, IncomingMessage, ServerResponse<IncomingMessage>, FastifyBaseLogger, FastifyTypeProviderDefault>;
1827
}
1928

2029
export interface GasketConfig {
21-
fastify?: {
22-
/** Enable compression */
23-
compression?: boolean;
24-
/** Filter for which request URLs invoke Gasket middleware */
25-
middlewareInclusionRegex?: RegExp;
26-
/** @deprecated */
27-
excludedRoutesRegex?: RegExp;
28-
/** Trust proxy configuration */
29-
trustProxy?: FastifyServerOptions['trustProxy'];
30-
/** Fastify request logging per route */
31-
disableRequestLogging?: boolean;
32-
};
33-
/** Middleware configuration */
34-
middleware?: {
35-
/** Plugin name */
36-
plugin: string;
37-
/** Paths for middleware */
38-
paths?: (string | RegExp)[];
39-
}[];
30+
fastify?: FastifyConfig
4031
}
4132

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

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

5642
export interface HookExecTypes {
57-
middleware(
58-
app: FastifyInstance<RawServerDefault, IncomingMessage, ServerResponse<IncomingMessage>, FastifyBaseLogger, FastifyTypeProviderDefault>
59-
): MaybeAsync<MaybeMultiple<Handler> & { paths?: (string | RegExp)[] }>;
6043
fastify(app: FastifyInstance<RawServerDefault, IncomingMessage, ServerResponse<IncomingMessage>, FastifyBaseLogger, FastifyTypeProviderDefault>): MaybeAsync<void>;
6144
errorMiddleware(): MaybeAsync<MaybeMultiple<ErrorHandler>>;
6245
}

packages/gasket-plugin-fastify/lib/index.js

-7
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,6 @@ const plugin = {
4646
}
4747
],
4848
lifecycles: [
49-
{
50-
name: 'middleware',
51-
method: 'exec',
52-
description: 'Add Express style middleware for Fastify',
53-
link: 'README.md#middleware',
54-
parent: 'createServers'
55-
},
5649
{
5750
name: 'fastify',
5851
method: 'exec',

packages/gasket-plugin-https-proxy/lib/index.d.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { Plugin, MaybeAsync } from '@gasket/core';
2-
import type { ServerOptions as ProxyServerOptions, Server as ProxyServer } from 'http-proxy';
2+
import type { ServerOptions as ProxyServerOptions } from 'http-proxy';
3+
import ProxyServer from 'http-proxy';
34
import type { RequireAtLeastOne } from '@gasket/plugin-https';
45

56
interface BaseHttpsProxyConfig extends ProxyServerOptions {

packages/gasket-plugin-manifest/lib/middleware.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/// <reference types="@gasket/plugin-service-worker" />
22
/// <reference types="@gasket/plugin-express" />
3+
/// <reference types="@gasket/plugin-middleware" />
34

45
const escapeRegex = require('escape-string-regexp');
56
const { gatherManifestData } = require('./utils');

packages/gasket-plugin-manifest/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
"@gasket/plugin-fastify": "^7.3.1",
4949
"@gasket/plugin-logger": "^7.3.0",
5050
"@gasket/plugin-metadata": "^7.3.0",
51+
"@gasket/plugin-middleware": "workspace:^",
5152
"@gasket/plugin-service-worker": "^7.3.0",
5253
"@types/express": "^4.17.21",
5354
"@types/jest": "^29.5.14",

packages/gasket-plugin-middleware/lib/index.d.ts

+36-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,31 @@
1-
import type { Plugin, MaybeAsync, MaybeMultiple, Handler } from '@gasket/core';
1+
import type { Plugin, MaybeAsync, MaybeMultiple, Gasket } from '@gasket/core';
2+
import type { FastifyInstance } from 'fastify'
3+
import type { Application as ExpressApplication } from 'express';
24

35
declare module 'fastify' {
46
interface FastifyReply {
57
locals: Record<string, any>;
68
}
79
}
810

11+
declare module '@gasket/plugin-fastify' {
12+
interface FastifyConfig {
13+
/** Filter for which request URLs invoke Gasket middleware */
14+
middlewareInclusionRegex?: RegExp;
15+
/** @deprecated */
16+
excludedRoutesRegex?: RegExp;
17+
}
18+
}
19+
20+
declare module '@gasket/plugin-express' {
21+
interface ExpressConfig {
22+
/** Filter for which request URLs invoke Gasket middleware */
23+
middlewareInclusionRegex?: RegExp;
24+
/** @deprecated */
25+
excludedRoutesRegex?: RegExp;
26+
}
27+
}
28+
929
declare module 'express-serve-static-core' {
1030
interface Response {
1131
/*
@@ -19,11 +39,23 @@ declare module 'express-serve-static-core' {
1939
}
2040
}
2141

42+
export type Handler = (req: any, res: any, next: (error?: Error) => void) => void;
43+
44+
type App = FastifyInstance | ExpressApplication;
45+
2246
declare module '@gasket/core' {
2347
export interface HookExecTypes {
24-
middleware(): MaybeAsync<MaybeMultiple<Handler> & {
25-
paths?: (string | RegExp)[]
26-
}>;
48+
middleware(gasket: Gasket, app: App): MaybeAsync<MaybeMultiple<Handler> >;
49+
}
50+
51+
export interface GasketConfig {
52+
/** Middleware configuration */
53+
middleware?: {
54+
/** Plugin name */
55+
plugin: string;
56+
/** Paths for middleware */
57+
paths?: (string | RegExp)[];
58+
}[];
2759
}
2860
}
2961

packages/gasket-plugin-middleware/lib/internal.d.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ import type {
1313
FastifyBaseLogger,
1414
RawServerDefault
1515
} from 'fastify';
16-
import type { Gasket, Plugin, MaybeMultiple, Handler } from '@gasket/core';
16+
import type { Gasket, Plugin, MaybeMultiple } from '@gasket/core';
17+
import type { Handler } from './index';
1718

1819
/** Type alias for Fastify application with HTTP/2 support */
1920
type FastifyApp<

packages/gasket-plugin-morgan/lib/middleware.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/// <reference types="@gasket/plugin-express" />
22
/// <reference types="@gasket/plugin-logger" />
3+
/// <reference types="@gasket/plugin-middleware" />
34

45
const morgan = require('morgan');
56
const split = require('split');

packages/gasket-plugin-morgan/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"@gasket/core": "^7.3.0",
4545
"@gasket/plugin-express": "^7.3.0",
4646
"@gasket/plugin-metadata": "^7.3.0",
47+
"@gasket/plugin-middleware": "^7.3.0",
4748
"@types/jest": "^29.5.14",
4849
"@types/morgan": "^1.9.9",
4950
"@types/node": "^20.17.19",

packages/gasket-plugin-redux/lib/middleware.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* eslint require-atomic-updates: warn */
22
/// <reference types="@gasket/plugin-express" />
33
/// <reference types="@gasket/plugin-logger" />
4+
/// <reference types="@gasket/plugin-middleware" />
45

56
/**
67
* Configure middleware

packages/gasket-plugin-redux/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
"@gasket/plugin-express": "^7.3.0",
4242
"@gasket/plugin-logger": "^7.3.0",
4343
"@gasket/plugin-metadata": "^7.3.0",
44-
"@gasket/plugin-middleware": "^7.3.0",
44+
"@gasket/plugin-middleware": "workspace:^",
4545
"@gasket/plugin-webpack": "^7.3.0",
4646
"@gasket/redux": "^7.3.0",
4747
"@types/jest": "^29.5.14",

packages/gasket-typescript-tests/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
"@gasket/plugin-logger": "^7.3.0",
6262
"@gasket/plugin-manifest": "^7.3.0",
6363
"@gasket/plugin-metadata": "^7.3.0",
64+
"@gasket/plugin-middleware": "workspace:^",
6465
"@gasket/plugin-mocha": "^7.3.0",
6566
"@gasket/plugin-morgan": "^7.3.0",
6667
"@gasket/plugin-nextjs": "^7.3.0",

packages/gasket-typescript-tests/test/plugin-express.spec.ts

-36
Original file line numberDiff line numberDiff line change
@@ -12,42 +12,6 @@ describe('@gasket/plugin-express', () => {
1212
};
1313
});
1414

15-
it('adds an excludedRoutesRegex config property', () => {
16-
const config: GasketConfigDefinition = {
17-
plugins: [{ name: 'example-plugin', version: '', description: '', hooks: {} }],
18-
express: {
19-
excludedRoutesRegex: /^(?!\/_next\/)/
20-
}
21-
};
22-
});
23-
24-
it('adds an middlewareInclusionRegex config property', () => {
25-
const badConfig: GasketConfigDefinition = {
26-
// @ts-expect-error
27-
middlewareInclusionRegex: '/api/*'
28-
};
29-
30-
const goodConfig: GasketConfigDefinition = {
31-
plugins: [{ name: 'example-plugin', version: '', description: '', hooks: {} }],
32-
express: {
33-
middlewareInclusionRegex: /^(?!\/_next\/)/
34-
}
35-
};
36-
});
37-
38-
it('declares the middleware lifecycle', () => {
39-
const hook: Hook<'middleware'> = (gasket: Gasket, app) => {
40-
return [];
41-
};
42-
});
43-
44-
it('validates the middleware return value', () => {
45-
// @ts-expect-error
46-
const hook: Hook<'middleware'> = (gasket: Gasket, app: Application) => {
47-
return 'huh?';
48-
};
49-
});
50-
5115
it('declares the express lifecycle', () => {
5216
const hook: Hook<'express'> = (gasket: Gasket, app: Application) => {
5317
app.use((req, res, next) => next());

packages/gasket-typescript-tests/test/preset-api.spec.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { Gasket, GasketConfigDefinition, Plugin } from '@gasket/core';
2+
import type { Handler } from '@gasket/plugin-middleware';
23

34
describe('@gasket/preset-api', () => {
45
const { log } = console;
@@ -32,12 +33,13 @@ describe('@gasket/preset-api', () => {
3233
servers(gasket, servers) {
3334
log(servers.http);
3435
},
35-
middleware(gasket) {
36+
middleware(gasket: Gasket) {
37+
const middleware: Handler = (req, res, next) => {
38+
res.statusCode = 404;
39+
res.send({ message: 'not found' });
40+
};
3641
return [
37-
(req, res, next) => {
38-
res.statusCode = 404;
39-
res.send({ message: 'not found' });
40-
}
42+
middleware
4143
];
4244
}
4345
}

0 commit comments

Comments
 (0)