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-850] - Fix server middleware stack discrepancies #1011

Merged
merged 4 commits into from
Jan 21, 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
3 changes: 3 additions & 0 deletions packages/gasket-plugin-express/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# `@gasket/plugin-express`

- Deprecate and remove the use of the `getExpressApp` action ([#1011])

### 7.1.3

- Adjust cookie parser invocation to earlier in the lifecycle chain ([#1009])
Expand Down Expand Up @@ -151,3 +153,4 @@
[#969]: https://github.com/godaddy/gasket/pull/969
[#1001]: https://github.com/godaddy/gasket/pull/1001
[#1009]: https://github.com/godaddy/gasket/pull/1009
[#1011]: https://github.com/godaddy/gasket/pull/1011
21 changes: 13 additions & 8 deletions packages/gasket-plugin-express/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,24 @@ export default makeGasket({
}
});
```
### Route Definition

## Actions

### getExpressApp

Get the Express app instance.
Routes can be defined in a in-app plugin in the `plugins` directory. The plugin will hook the `express` lifecycle to add the routes to the express app.

```js
const app = actions.gasket.getExpressApp();
// plugins/routes-plugin.js
export default {
name: 'routes-plugin',
hooks: {
express: async function (gasket, app) {
app.get('/hello', (req, res) => {
res.send('Hello World!');
});
}
}
};
```

Each Gasket creates a single shared Express instance, ensuring consistent access to the same app instance wherever it's needed.

## Lifecycles

### express
Expand Down
11 changes: 11 additions & 0 deletions packages/gasket-plugin-express/generator/app/plugins/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Local Plugins

### Routes plugin

A local plugin is used to define Express routes. The plugin hooks the `express` lifecycle and mutates the Express app.

The following routes are available:

```javascript
GET /default
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
export const defaultHandler = async (req, res) => {
res.status(200).json({
message: 'Welcome to your default route...'
});
};

export default {
name: 'routes-plugin',
hooks: {
express(gasket, app) {
{{#if useSwagger }}
/**
* @swagger
*
* /default:
* get:
* summary: "Get default route"
* produces:
* - "application/json"
* responses:
* "200":
* description: "Returns welcome message."
* content:
* application/json
*/
{{/if }}
app.get('/default', defaultHandler);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
export const defaultHandler = async (req, res) => {
res.status(200).json({
message: 'Welcome to your default route...'
});
};

export default {
name: 'routes-plugin',
hooks: {
express(gasket, app) {
{{#if useSwagger }}
/**
* @swagger
*
* /default:
* get:
* summary: "Get default route"
* produces:
* - "application/json"
* responses:
* "200":
* description: "Returns welcome message."
* content:
* application/json
*/
{{/if }}
app.get('/default', defaultHandler);
}
}
}
27 changes: 0 additions & 27 deletions packages/gasket-plugin-express/generator/app/routes/index.js

This file was deleted.

28 changes: 0 additions & 28 deletions packages/gasket-plugin-express/generator/app/routes/index.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defaultHandler } from '../routes';
import { defaultHandler } from '../plugins/routes-plugin.js';
import { jest, expect, beforeEach } from '@jest/globals';

describe('Routes', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { jest, describe, beforeEach, it, expect } from '@jest/globals';
import { defaultHandler } from '../routes';
import { defaultHandler } from '../plugins/routes-plugin.js';

describe('Routes', () => {
let mockRequest, mockResponse;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect } from 'chai';
import sinon from 'sinon';
import { defaultHandler } from '../routes/index.js';
import { defaultHandler } from '../plugins/routes-plugin.js';

describe('Routes', () => {
let mockRequest, mockResponse, jsonSpy;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect } from 'chai';
import sinon from 'sinon';
import { defaultHandler } from '../routes/index.js';
import { defaultHandler } from '../plugins/routes-plugin.js';

describe('Routes', () => {
let mockRequest, mockResponse, jsonSpy;
Expand Down
6 changes: 5 additions & 1 deletion packages/gasket-plugin-express/lib/create-servers.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
* @type {import('@gasket/core').HookHandler<'createServers'>}
*/
module.exports = async function createServers(gasket, serverOpts) {
const app = gasket.actions.getExpressApp();
const express = require('express');
const { http2 } = gasket.config;
const app = http2 ? require('http2-express')(express) : express();

app.use(require('cookie-parser')());

await gasket.exec('express', app);

Expand Down
1 change: 1 addition & 0 deletions packages/gasket-plugin-express/lib/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ module.exports = async function create(gasket, context) {
if (apiApp && addApiRoutes) {
const globIgnore = typescript ? '!(*.js)' : '!(*.ts)';
files.add(`${generatorDir}/app/**/${globIgnore}`);
gasketConfig.addPlugin('pluginRoutes', './plugins/routes-plugin.js');

createTestFiles({ files, generatorDir, testPlugins, globIgnore });
}
Expand Down
1 change: 1 addition & 0 deletions packages/gasket-plugin-express/lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { Application, ErrorRequestHandler, Handler } from 'express';

declare module '@gasket/core' {
export interface GasketActions {
/** @deprecated */
getExpressApp(): Application;
}
export interface GasketConfig {
Expand Down
4 changes: 3 additions & 1 deletion packages/gasket-plugin-express/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const plugin = {
version,
description,
actions: {
/** @deprecated */
getExpressApp(gasket) {
const express = require('express');
const { http2 } = gasket.config;
Expand Down Expand Up @@ -41,7 +42,8 @@ const plugin = {
{
name: 'getExpressApp',
description: 'Get the Express app instance',
link: 'README.md#getExpressApp'
link: 'README.md#getExpressApp',
deprecated: true
}
],
guides: [
Expand Down
9 changes: 7 additions & 2 deletions packages/gasket-plugin-express/test/plugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,19 @@ describe('createServers', () => {
expect(result).toEqual({ handler: app });
});

it('returns the handler as http2 bridge app', async function () {
it('attaches cookie parser middleware', async function () {
await plugin.hooks.createServers(gasket, {});
expect(app.use).toHaveBeenCalledWith(cookieParserMiddleware);
});

it('action: returns the handler as http2 bridge app(deprecated)', async function () {
gasket.actions.getExpressApp.mockReturnValueOnce(bridgedApp);
gasket.config.http2 = 8080;
const result = await plugin.hooks.createServers(gasket);
expect(result).toEqual({ handler: bridgedApp });
});

it('attaches cookie parser middleware', async function () {
it('action: attaches cookie parser middleware(deprecated)', async function () {
plugin.actions.getExpressApp(gasket);
expect(app.use).toHaveBeenCalledWith(cookieParserMiddleware);
});
Expand Down
3 changes: 3 additions & 0 deletions packages/gasket-plugin-fastify/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# `@gasket/plugin-fastify`

- Deprecate and remove the use of the `getFastifyApp` action ([#1011])

### 7.1.0

- Aligned version releases across all packages
Expand Down Expand Up @@ -88,3 +90,4 @@ Enable middleware support ([#172])
[#940]: https://github.com/godaddy/gasket/pull/940
[#969]: https://github.com/godaddy/gasket/pull/969
[#972]: https://github.com/godaddy/gasket/pull/972
[#1011]: https://github.com/godaddy/gasket/pull/1011
20 changes: 13 additions & 7 deletions packages/gasket-plugin-fastify/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,24 @@ export default makeGasket({
});
```

## Actions
### Route Definition

### getFastifyApp

Get the Fastify app instance.
Routes can be defined in a in-app plugin in the `plugins` directory. The plugin will hook the `fastify` lifecycle to add the routes to the fastify app.

```js
const app = actions.gasket.getFastifyApp();
// plugins/routes-plugin.js
export default {
name: 'routes-plugin',
hooks: {
fastify: async function (gasket, app) {
app.get('/hello', (req, res) => {
res.send('Hello World!');
});
}
}
};
```

Each Gasket creates a single shared Fastify instance, ensuring consistent access to the same app instance wherever it's needed.

## Lifecycles

### fastify
Expand Down
11 changes: 11 additions & 0 deletions packages/gasket-plugin-fastify/generator/app/plugins/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Local Plugins

### Routes plugin

A local plugin is used to define Fastify routes. The plugin hooks the `fastify` lifecycle and mutates the Fastify app.

The following routes are available:

```javascript
GET /default
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
export const defaultHandler = async (req, res) => {
if (res.statusCode === 200) {
res.send({ message: 'Welcome to your default route...' });
}
};

export default {
name: 'routes-plugin',
hooks: {
fastify(gasket, app) {
{{#if useSwagger }}
/**
* @swagger
*
* /default:
* get:
* summary: "Get default route"
* produces:
* - "application/json"
* responses:
* "200":
* description: "Returns welcome message."
* content:
* application/json
*/
{{/if }}
app.get('/default', defaultHandler);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
export const defaultHandler = async (req, res) => {
if (res.statusCode === 200) {
res.send({ message: 'Welcome to your default route...' });
}
};
export default {
name: 'routes-plugin',
hooks: {
fastify(gasket, app) {
{{#if useSwagger }}
/**
* @swagger
*
* /default:
* get:
* summary: "Get default route"
* produces:
* - "application/json"
* responses:
* "200":
* description: "Returns welcome message."
* content:
* application/json
*/
{{/if }}
app.get('/default', defaultHandler);
}
}
}
Loading
Loading