@uphold/fastify-openapi-router-plugin 1.0.0 → 1.0.1
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/package.json +1 -1
- package/src/index.test.js +54 -0
- package/src/parser/index.js +2 -2
- package/src/plugin.js +13 -7
package/package.json
CHANGED
package/src/index.test.js
CHANGED
|
@@ -164,4 +164,58 @@ describe('Fastify plugin', () => {
|
|
|
164
164
|
);
|
|
165
165
|
});
|
|
166
166
|
});
|
|
167
|
+
|
|
168
|
+
describe('encapsulation', () => {
|
|
169
|
+
it('should use the correct fastify instance when calling oas.route() from a child plugin', async () => {
|
|
170
|
+
const app = fastify({ logger: false });
|
|
171
|
+
|
|
172
|
+
await app.register(OpenAPIRouter, { spec });
|
|
173
|
+
|
|
174
|
+
await app.register(async childFastify => {
|
|
175
|
+
childFastify.decorateRequest('customProperty', null);
|
|
176
|
+
childFastify.addHook('onRequest', async request => {
|
|
177
|
+
request.customProperty = 'decorated-value';
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
childFastify.oas.route({
|
|
181
|
+
handler: async request => {
|
|
182
|
+
return { customProperty: request.customProperty };
|
|
183
|
+
},
|
|
184
|
+
operationId: 'getPets'
|
|
185
|
+
});
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
const result = await app.inject({ url: '/pets' });
|
|
189
|
+
|
|
190
|
+
expect(result.json()).toMatchObject({
|
|
191
|
+
customProperty: 'decorated-value'
|
|
192
|
+
});
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
it('should use the correct fastify instance when calling oas.installNotImplementedRoutes() from a child plugin', async () => {
|
|
196
|
+
const app = fastify({ logger: false });
|
|
197
|
+
|
|
198
|
+
await app.register(OpenAPIRouter, { spec });
|
|
199
|
+
|
|
200
|
+
await app.register(async childFastify => {
|
|
201
|
+
childFastify.decorateRequest('customProperty', null);
|
|
202
|
+
childFastify.addHook('onRequest', async request => {
|
|
203
|
+
request.customProperty = 'decorated-value';
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
childFastify.oas.route({
|
|
207
|
+
handler: async request => {
|
|
208
|
+
return { customProperty: request.customProperty };
|
|
209
|
+
},
|
|
210
|
+
operationId: 'getPets'
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
childFastify.oas.installNotImplementedRoutes();
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
const postPetsResult = await app.inject({ method: 'POST', url: '/pets' });
|
|
217
|
+
|
|
218
|
+
expect(postPetsResult.statusCode).toBe(501);
|
|
219
|
+
});
|
|
220
|
+
});
|
|
167
221
|
});
|
package/src/parser/index.js
CHANGED
|
@@ -22,7 +22,7 @@ export const parse = async options => {
|
|
|
22
22
|
const operation = methods[method];
|
|
23
23
|
|
|
24
24
|
const securityFn = applySecurity(operation, spec, options.securityHandlers, options.securityErrorMapper);
|
|
25
|
-
const
|
|
25
|
+
const paramsCoercingFn = applyParamsCoercing(operation);
|
|
26
26
|
|
|
27
27
|
// Build fastify route.
|
|
28
28
|
const route = {
|
|
@@ -32,7 +32,7 @@ export const parse = async options => {
|
|
|
32
32
|
request[DECORATOR_NAME].operation = operation;
|
|
33
33
|
|
|
34
34
|
await securityFn?.(request);
|
|
35
|
-
|
|
35
|
+
paramsCoercingFn?.(request);
|
|
36
36
|
}
|
|
37
37
|
],
|
|
38
38
|
schema: {
|
package/src/plugin.js
CHANGED
|
@@ -2,10 +2,10 @@ import { DECORATOR_NAME } from './utils/constants.js';
|
|
|
2
2
|
import { createNotImplementedError, errors } from './errors/index.js';
|
|
3
3
|
import { parse } from './parser/index.js';
|
|
4
4
|
|
|
5
|
-
const
|
|
5
|
+
const createRouting = (routes, notImplementedErrorMapper) => {
|
|
6
6
|
const missingRoutes = new Set(Object.values(routes));
|
|
7
7
|
|
|
8
|
-
const addMissingRoutes =
|
|
8
|
+
const addMissingRoutes = fastify => {
|
|
9
9
|
missingRoutes.forEach(route => {
|
|
10
10
|
fastify.route({
|
|
11
11
|
...route,
|
|
@@ -28,7 +28,7 @@ const createRoute = (fastify, routes, notImplementedErrorMapper) => {
|
|
|
28
28
|
missingRoutes.clear();
|
|
29
29
|
};
|
|
30
30
|
|
|
31
|
-
const addRoute = ({ method, onRequest, operationId, schema, url, ...routeOptions }) => {
|
|
31
|
+
const addRoute = (fastify, { method, onRequest, operationId, schema, url, ...routeOptions }) => {
|
|
32
32
|
const route = routes[operationId];
|
|
33
33
|
|
|
34
34
|
// Throw an error if the operation is unknown.
|
|
@@ -68,13 +68,19 @@ const plugin = async (fastify, options) => {
|
|
|
68
68
|
|
|
69
69
|
const routes = await parse(options);
|
|
70
70
|
|
|
71
|
-
const { addMissingRoutes, addRoute } =
|
|
71
|
+
const { addMissingRoutes, addRoute } = createRouting(routes, options.notImplementedErrorMapper);
|
|
72
72
|
|
|
73
73
|
// Decorate fastify object.
|
|
74
74
|
fastify.decorate(DECORATOR_NAME, {
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
75
|
+
getter: function () {
|
|
76
|
+
const fastify = this;
|
|
77
|
+
|
|
78
|
+
return {
|
|
79
|
+
errors,
|
|
80
|
+
installNotImplementedRoutes: () => addMissingRoutes(fastify),
|
|
81
|
+
route: route => addRoute(fastify, route)
|
|
82
|
+
};
|
|
83
|
+
}
|
|
78
84
|
});
|
|
79
85
|
|
|
80
86
|
// Avoid decorating the request with reference types.
|