@gasket/plugin-fastify 7.0.0-next.63 → 7.0.0-next.65
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/create-servers.js +1 -27
- package/lib/create.js +40 -4
- package/lib/index.d.ts +13 -12
- package/lib/index.js +16 -0
- package/lib/utils.js +1 -0
- package/package.json +6 -3
package/lib/create-servers.js
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
/// <reference types="@gasket/plugin-https" />
|
|
2
2
|
/// <reference types="@gasket/plugin-logger" />
|
|
3
3
|
|
|
4
|
-
const { alignLogger } = require('./utils');
|
|
5
|
-
|
|
6
4
|
/**
|
|
7
5
|
* Create the Fastify instance and setup the lifecycle hooks.
|
|
8
6
|
* Fastify is compatible with express middleware out of the box, so we can
|
|
@@ -11,34 +9,10 @@ const { alignLogger } = require('./utils');
|
|
|
11
9
|
*/
|
|
12
10
|
// eslint-disable-next-line max-statements
|
|
13
11
|
module.exports = async function createServers(gasket, serverOpts) {
|
|
14
|
-
const
|
|
15
|
-
|
|
16
|
-
const { config } = gasket;
|
|
17
|
-
const {
|
|
18
|
-
fastify: {
|
|
19
|
-
routes,
|
|
20
|
-
trustProxy = false
|
|
21
|
-
} = {},
|
|
22
|
-
http2
|
|
23
|
-
} = config;
|
|
24
|
-
|
|
25
|
-
const fastifyLogger = alignLogger(gasket.logger);
|
|
26
|
-
|
|
27
|
-
// @ts-ignore
|
|
28
|
-
const app = fastify({ logger: fastifyLogger, trustProxy, http2 });
|
|
29
|
-
|
|
12
|
+
const app = gasket.actions.getFastifyApp();
|
|
30
13
|
// allow consuming apps to directly append options to their server
|
|
31
14
|
await gasket.exec('fastify', app);
|
|
32
15
|
|
|
33
|
-
if (routes) {
|
|
34
|
-
for (const route of routes) {
|
|
35
|
-
if (typeof route !== 'function') {
|
|
36
|
-
throw new Error('Route must be a function');
|
|
37
|
-
}
|
|
38
|
-
await route(app);
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
16
|
const postRenderingStacks = (await gasket.exec('errorMiddleware')).filter(Boolean);
|
|
43
17
|
// @ts-ignore
|
|
44
18
|
postRenderingStacks.forEach((stack) => app.use(stack));
|
package/lib/create.js
CHANGED
|
@@ -2,22 +2,58 @@
|
|
|
2
2
|
|
|
3
3
|
const { name, version, devDependencies } = require('../package.json');
|
|
4
4
|
|
|
5
|
+
/**
|
|
6
|
+
* createTestFiles
|
|
7
|
+
* @property {Files} files - The Gasket Files API.
|
|
8
|
+
* @property {generatorDir} - The directory of the generator.
|
|
9
|
+
* @property {testPlugins} - Array of selected test plugins
|
|
10
|
+
* @property {globIgnore} - Extension pattern to ignore
|
|
11
|
+
*/
|
|
12
|
+
function createTestFiles({ files, generatorDir, testPlugins, globIgnore }) {
|
|
13
|
+
if (!testPlugins || testPlugins.length === 0) return;
|
|
14
|
+
const unit = ['jest', 'mocha'];
|
|
15
|
+
const integration = ['cypress'];
|
|
16
|
+
const frameworks = [...unit, ...integration];
|
|
17
|
+
const frameworksRegex = new RegExp(frameworks.join('|'));
|
|
18
|
+
|
|
19
|
+
testPlugins.forEach((testPlugin) => {
|
|
20
|
+
const match = frameworksRegex.exec(testPlugin);
|
|
21
|
+
if (match) {
|
|
22
|
+
const matchedFramework = match[0];
|
|
23
|
+
if (unit.includes(matchedFramework)) {
|
|
24
|
+
files.add(`${generatorDir}/${matchedFramework}/*/${globIgnore}`, `${generatorDir}/${matchedFramework}/**/${globIgnore}`);
|
|
25
|
+
} else {
|
|
26
|
+
files.add(`${generatorDir}/${matchedFramework}/*`, `${generatorDir}/${matchedFramework}/**/*`);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
|
|
5
32
|
/**
|
|
6
33
|
* Add files & extend package.json for new apps.
|
|
7
34
|
* @type {import('@gasket/core').HookHandler<'create'>}
|
|
8
35
|
*/
|
|
9
36
|
module.exports = async function create(gasket, context) {
|
|
37
|
+
const {
|
|
38
|
+
files,
|
|
39
|
+
typescript,
|
|
40
|
+
testPlugins,
|
|
41
|
+
pkg,
|
|
42
|
+
gasketConfig
|
|
43
|
+
} = context;
|
|
10
44
|
const generatorDir = `${__dirname}/../generator`;
|
|
11
45
|
|
|
12
|
-
|
|
46
|
+
gasketConfig.addPlugin('pluginFastify', name);
|
|
13
47
|
|
|
14
|
-
|
|
48
|
+
pkg.add('dependencies', {
|
|
15
49
|
[name]: `^${version}`,
|
|
16
50
|
fastify: devDependencies.fastify
|
|
17
51
|
});
|
|
18
52
|
|
|
19
53
|
if ('apiApp' in context && context.apiApp) {
|
|
20
|
-
const
|
|
21
|
-
|
|
54
|
+
const globIgnore = typescript ? '!(*.js)' : '!(*.ts)';
|
|
55
|
+
files.add(`${generatorDir}/app/**/${globIgnore}`);
|
|
56
|
+
|
|
57
|
+
createTestFiles({ files, generatorDir, testPlugins, globIgnore });
|
|
22
58
|
}
|
|
23
59
|
};
|
package/lib/index.d.ts
CHANGED
|
@@ -4,13 +4,21 @@ import type {
|
|
|
4
4
|
FastifyInstance,
|
|
5
5
|
FastifyRequest,
|
|
6
6
|
FastifyReply,
|
|
7
|
-
FastifyServerOptions
|
|
7
|
+
FastifyServerOptions,
|
|
8
|
+
FastifyBaseLogger,
|
|
9
|
+
FastifyTypeProviderDefault,
|
|
10
|
+
RawServerDefault
|
|
8
11
|
} from 'fastify';
|
|
9
|
-
import { Http2SecureServer, Http2ServerRequest, Http2ServerResponse } from 'http2'
|
|
12
|
+
import { Http2SecureServer, Http2ServerRequest, Http2ServerResponse } from 'http2';
|
|
13
|
+
import { IncomingMessage, ServerResponse } from 'http';
|
|
10
14
|
|
|
11
15
|
export type AppRoutes = Array<MaybeAsync<(app: FastifyInstance) => void>>;
|
|
12
16
|
|
|
13
17
|
declare module '@gasket/core' {
|
|
18
|
+
export interface GasketActions {
|
|
19
|
+
getFastifyApp(): FastifyInstance<RawServerDefault, IncomingMessage, ServerResponse<IncomingMessage>, FastifyBaseLogger, FastifyTypeProviderDefault>;
|
|
20
|
+
}
|
|
21
|
+
|
|
14
22
|
export interface GasketConfig {
|
|
15
23
|
fastify?: {
|
|
16
24
|
/** Enable compression */
|
|
@@ -21,8 +29,6 @@ declare module '@gasket/core' {
|
|
|
21
29
|
excludedRoutesRegex?: RegExp;
|
|
22
30
|
/** Trust proxy configuration */
|
|
23
31
|
trustProxy?: FastifyServerOptions['trustProxy'];
|
|
24
|
-
/** Glob pattern for source files setting up fastify routes */
|
|
25
|
-
routes?: Array<MaybeAsync<(app: FastifyInstance<Http2SecureServer, Http2ServerRequest, Http2ServerResponse>) => void>>;
|
|
26
32
|
};
|
|
27
33
|
/** Middleware configuration */
|
|
28
34
|
middleware?: {
|
|
@@ -49,21 +55,16 @@ declare module '@gasket/core' {
|
|
|
49
55
|
|
|
50
56
|
export interface HookExecTypes {
|
|
51
57
|
middleware(
|
|
52
|
-
app: FastifyInstance<
|
|
58
|
+
app: FastifyInstance<RawServerDefault, IncomingMessage, ServerResponse<IncomingMessage>, FastifyBaseLogger, FastifyTypeProviderDefault>
|
|
53
59
|
): MaybeAsync<MaybeMultiple<Handler> & { paths?: (string | RegExp)[] }>;
|
|
54
|
-
fastify(app: FastifyInstance<
|
|
60
|
+
fastify(app: FastifyInstance<RawServerDefault, IncomingMessage, ServerResponse<IncomingMessage>, FastifyBaseLogger, FastifyTypeProviderDefault>): MaybeAsync<void>;
|
|
55
61
|
errorMiddleware(): MaybeAsync<MaybeMultiple<ErrorHandler>>;
|
|
56
62
|
}
|
|
57
63
|
}
|
|
58
64
|
|
|
59
|
-
type FastifyLogger = Logger & {
|
|
60
|
-
trace?: () => MaybeAsync<any>,
|
|
61
|
-
fatal?: () => MaybeAsync<any>
|
|
62
|
-
}
|
|
63
|
-
|
|
64
65
|
export function alignLogger(
|
|
65
66
|
logger: Logger
|
|
66
|
-
):
|
|
67
|
+
): FastifyBaseLogger
|
|
67
68
|
|
|
68
69
|
declare module 'create-gasket-app' {
|
|
69
70
|
export interface CreateContext {
|
package/lib/index.js
CHANGED
|
@@ -8,12 +8,28 @@ const {
|
|
|
8
8
|
} = require('../package.json');
|
|
9
9
|
const create = require('./create');
|
|
10
10
|
const createServers = require('./create-servers');
|
|
11
|
+
const fastify = require('fastify');
|
|
12
|
+
const { alignLogger } = require('./utils');
|
|
13
|
+
|
|
14
|
+
let app;
|
|
11
15
|
|
|
12
16
|
/** @type {import('@gasket/core').Plugin} */
|
|
13
17
|
const plugin = {
|
|
14
18
|
name,
|
|
15
19
|
version,
|
|
16
20
|
description,
|
|
21
|
+
actions: {
|
|
22
|
+
getFastifyApp(gasket) {
|
|
23
|
+
const { fastify: fastifyConfig = {}, http2, https } = gasket.config;
|
|
24
|
+
const { trustProxy = false } = fastifyConfig;
|
|
25
|
+
const fastifyLogger = alignLogger(gasket.logger);
|
|
26
|
+
|
|
27
|
+
// @ts-ignore
|
|
28
|
+
app ??= fastify({ logger: fastifyLogger, trustProxy, https, http2 });
|
|
29
|
+
|
|
30
|
+
return app;
|
|
31
|
+
}
|
|
32
|
+
},
|
|
17
33
|
hooks: {
|
|
18
34
|
create,
|
|
19
35
|
createServers,
|
package/lib/utils.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gasket/plugin-fastify",
|
|
3
|
-
"version": "7.0.0-next.
|
|
3
|
+
"version": "7.0.0-next.65",
|
|
4
4
|
"description": "Adds fastify support to your application",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"types": "lib/index.d.ts",
|
|
@@ -39,7 +39,7 @@
|
|
|
39
39
|
},
|
|
40
40
|
"homepage": "https://github.com/godaddy/gasket/tree/main/packages/gasket-plugin-fastify",
|
|
41
41
|
"devDependencies": {
|
|
42
|
-
"@gasket/core": "7.0.0-next.
|
|
42
|
+
"@gasket/core": "7.0.0-next.65",
|
|
43
43
|
"cross-env": "^7.0.3",
|
|
44
44
|
"eslint": "^8.56.0",
|
|
45
45
|
"eslint-config-godaddy": "^7.1.1",
|
|
@@ -71,5 +71,8 @@
|
|
|
71
71
|
"eslintIgnore": [
|
|
72
72
|
"generator"
|
|
73
73
|
],
|
|
74
|
-
"
|
|
74
|
+
"jest": {
|
|
75
|
+
"rootDir": "test"
|
|
76
|
+
},
|
|
77
|
+
"gitHead": "93fb05fe933682704607eed6a2dfc870cf35c2d1"
|
|
75
78
|
}
|