|
1 | 1 | // DO NOT EDIT: generated by fsdgenjs
|
2 | 2 | /* eslint-disable */
|
3 | 3 |
|
4 |
| -import { FastifyPluginAsync, RegisterOptions } from 'fastify'; |
| 4 | +import type * as fastifyTypes from 'fastify'; |
5 | 5 | import { IServiceResult, IServiceError } from 'facility-core';
|
6 | 6 |
|
7 | 7 | const standardErrorCodes: { [code: string]: number } = {
|
@@ -32,14 +32,15 @@ function parseBoolean(value: string | undefined) {
|
32 | 32 | return undefined;
|
33 | 33 | }
|
34 | 34 |
|
35 |
| -export type ConformanceApiPluginOptions = RegisterOptions & { |
36 |
| - api: IConformanceApi; |
| 35 | +export type ConformanceApiPluginOptions = fastifyTypes.RegisterOptions & { |
| 36 | + serviceOrFactory: IConformanceApi | ((req: fastifyTypes.FastifyRequest) => IConformanceApi); |
37 | 37 | caseInsenstiveQueryStringKeys?: boolean;
|
38 | 38 | includeErrorDetails?: boolean;
|
39 | 39 | }
|
40 | 40 |
|
41 |
| -export const conformanceApiPlugin: FastifyPluginAsync<ConformanceApiPluginOptions> = async (fastify, opts) => { |
42 |
| - const { api, caseInsenstiveQueryStringKeys, includeErrorDetails } = opts; |
| 41 | +export const conformanceApiPlugin: fastifyTypes.FastifyPluginAsync<ConformanceApiPluginOptions> = async (fastify, opts) => { |
| 42 | + const { serviceOrFactory, caseInsenstiveQueryStringKeys, includeErrorDetails } = opts; |
| 43 | + const getService = typeof serviceOrFactory === 'function' ? serviceOrFactory : () => serviceOrFactory; |
43 | 44 |
|
44 | 45 | for (const jsonSchema of jsonSchemas) {
|
45 | 46 | fastify.addSchema(jsonSchema);
|
@@ -94,7 +95,8 @@ export const conformanceApiPlugin: FastifyPluginAsync<ConformanceApiPluginOption
|
94 | 95 | handler: async function (req, res) {
|
95 | 96 | const request: IGetApiInfoRequest = {};
|
96 | 97 |
|
97 |
| - const result = await api.getApiInfo(request); |
| 98 | + const service = getService(req); |
| 99 | + const result = await service.getApiInfo(request); |
98 | 100 |
|
99 | 101 | if (result.error) {
|
100 | 102 | const status = result.error.code && standardErrorCodes[result.error.code];
|
@@ -130,7 +132,8 @@ export const conformanceApiPlugin: FastifyPluginAsync<ConformanceApiPluginOption
|
130 | 132 | const query = req.query as Record<string, string>;
|
131 | 133 | if (typeof query['q'] === 'string') request.query = query['q'];
|
132 | 134 |
|
133 |
| - const result = await api.getWidgets(request); |
| 135 | + const service = getService(req); |
| 136 | + const result = await service.getWidgets(request); |
134 | 137 |
|
135 | 138 | if (result.error) {
|
136 | 139 | const status = result.error.code && standardErrorCodes[result.error.code];
|
@@ -160,7 +163,8 @@ export const conformanceApiPlugin: FastifyPluginAsync<ConformanceApiPluginOption
|
160 | 163 |
|
161 | 164 | request.widget = req.body as never;
|
162 | 165 |
|
163 |
| - const result = await api.createWidget(request); |
| 166 | + const service = getService(req); |
| 167 | + const result = await service.createWidget(request); |
164 | 168 |
|
165 | 169 | if (result.error) {
|
166 | 170 | const status = result.error.code && standardErrorCodes[result.error.code];
|
@@ -200,7 +204,8 @@ export const conformanceApiPlugin: FastifyPluginAsync<ConformanceApiPluginOption
|
200 | 204 | const headers = req.headers as Record<string, string>;
|
201 | 205 | if (typeof headers['if-none-match'] === 'string') request.ifNotETag = headers['if-none-match'];
|
202 | 206 |
|
203 |
| - const result = await api.getWidget(request); |
| 207 | + const service = getService(req); |
| 208 | + const result = await service.getWidget(request); |
204 | 209 |
|
205 | 210 | if (result.error) {
|
206 | 211 | const status = result.error.code && standardErrorCodes[result.error.code];
|
@@ -245,7 +250,8 @@ export const conformanceApiPlugin: FastifyPluginAsync<ConformanceApiPluginOption
|
245 | 250 | const headers = req.headers as Record<string, string>;
|
246 | 251 | if (typeof headers['if-match'] === 'string') request.ifETag = headers['if-match'];
|
247 | 252 |
|
248 |
| - const result = await api.deleteWidget(request); |
| 253 | + const service = getService(req); |
| 254 | + const result = await service.deleteWidget(request); |
249 | 255 |
|
250 | 256 | if (result.error) {
|
251 | 257 | const status = result.error.code && standardErrorCodes[result.error.code];
|
@@ -285,7 +291,8 @@ export const conformanceApiPlugin: FastifyPluginAsync<ConformanceApiPluginOption
|
285 | 291 |
|
286 | 292 | request.ids = req.body as never;
|
287 | 293 |
|
288 |
| - const result = await api.getWidgetBatch(request); |
| 294 | + const service = getService(req); |
| 295 | + const result = await service.getWidgetBatch(request); |
289 | 296 |
|
290 | 297 | if (result.error) {
|
291 | 298 | const status = result.error.code && standardErrorCodes[result.error.code];
|
@@ -325,7 +332,8 @@ export const conformanceApiPlugin: FastifyPluginAsync<ConformanceApiPluginOption
|
325 | 332 | request.field = body.field;
|
326 | 333 | request.matrix = body.matrix;
|
327 | 334 |
|
328 |
| - const result = await api.mirrorFields(request); |
| 335 | + const service = getService(req); |
| 336 | + const result = await service.mirrorFields(request); |
329 | 337 |
|
330 | 338 | if (result.error) {
|
331 | 339 | const status = result.error.code && standardErrorCodes[result.error.code];
|
@@ -363,7 +371,8 @@ export const conformanceApiPlugin: FastifyPluginAsync<ConformanceApiPluginOption
|
363 | 371 | if (typeof query['enum'] === 'string') request.enum = query['enum'] as Answer;
|
364 | 372 | if (typeof query['datetime'] === 'string') request.datetime = query['datetime'];
|
365 | 373 |
|
366 |
| - const result = await api.checkQuery(request); |
| 374 | + const service = getService(req); |
| 375 | + const result = await service.checkQuery(request); |
367 | 376 |
|
368 | 377 | if (result.error) {
|
369 | 378 | const status = result.error.code && standardErrorCodes[result.error.code];
|
@@ -401,7 +410,8 @@ export const conformanceApiPlugin: FastifyPluginAsync<ConformanceApiPluginOption
|
401 | 410 | if (typeof params['enum'] === 'string') request.enum = params['enum'] as Answer;
|
402 | 411 | if (typeof params['datetime'] === 'string') request.datetime = params['datetime'];
|
403 | 412 |
|
404 |
| - const result = await api.checkPath(request); |
| 413 | + const service = getService(req); |
| 414 | + const result = await service.checkPath(request); |
405 | 415 |
|
406 | 416 | if (result.error) {
|
407 | 417 | const status = result.error.code && standardErrorCodes[result.error.code];
|
@@ -439,7 +449,8 @@ export const conformanceApiPlugin: FastifyPluginAsync<ConformanceApiPluginOption
|
439 | 449 | if (typeof headers['enum'] === 'string') request.enum = headers['enum'] as Answer;
|
440 | 450 | if (typeof headers['datetime'] === 'string') request.datetime = headers['datetime'];
|
441 | 451 |
|
442 |
| - const result = await api.mirrorHeaders(request); |
| 452 | + const service = getService(req); |
| 453 | + const result = await service.mirrorHeaders(request); |
443 | 454 |
|
444 | 455 | if (result.error) {
|
445 | 456 | const status = result.error.code && standardErrorCodes[result.error.code];
|
@@ -495,7 +506,8 @@ export const conformanceApiPlugin: FastifyPluginAsync<ConformanceApiPluginOption
|
495 | 506 | const body = req.body as Record<string, never>;
|
496 | 507 | request.normal = body.normal;
|
497 | 508 |
|
498 |
| - const result = await api.mixed(request); |
| 509 | + const service = getService(req); |
| 510 | + const result = await service.mixed(request); |
499 | 511 |
|
500 | 512 | if (result.error) {
|
501 | 513 | const status = result.error.code && standardErrorCodes[result.error.code];
|
@@ -554,7 +566,8 @@ export const conformanceApiPlugin: FastifyPluginAsync<ConformanceApiPluginOption
|
554 | 566 | request.hasWidget = body.hasWidget;
|
555 | 567 | request.point = body.point;
|
556 | 568 |
|
557 |
| - const result = await api.required(request); |
| 569 | + const service = getService(req); |
| 570 | + const result = await service.required(request); |
558 | 571 |
|
559 | 572 | if (result.error) {
|
560 | 573 | const status = result.error.code && standardErrorCodes[result.error.code];
|
@@ -587,7 +600,8 @@ export const conformanceApiPlugin: FastifyPluginAsync<ConformanceApiPluginOption
|
587 | 600 |
|
588 | 601 | request.content = req.body as never;
|
589 | 602 |
|
590 |
| - const result = await api.mirrorBytes(request); |
| 603 | + const service = getService(req); |
| 604 | + const result = await service.mirrorBytes(request); |
591 | 605 |
|
592 | 606 | if (result.error) {
|
593 | 607 | const status = result.error.code && standardErrorCodes[result.error.code];
|
@@ -624,7 +638,8 @@ export const conformanceApiPlugin: FastifyPluginAsync<ConformanceApiPluginOption
|
624 | 638 |
|
625 | 639 | request.content = req.body as never;
|
626 | 640 |
|
627 |
| - const result = await api.mirrorText(request); |
| 641 | + const service = getService(req); |
| 642 | + const result = await service.mirrorText(request); |
628 | 643 |
|
629 | 644 | if (result.error) {
|
630 | 645 | const status = result.error.code && standardErrorCodes[result.error.code];
|
@@ -658,7 +673,8 @@ export const conformanceApiPlugin: FastifyPluginAsync<ConformanceApiPluginOption
|
658 | 673 |
|
659 | 674 | request.content = req.body as never;
|
660 | 675 |
|
661 |
| - const result = await api.bodyTypes(request); |
| 676 | + const service = getService(req); |
| 677 | + const result = await service.bodyTypes(request); |
662 | 678 |
|
663 | 679 | if (result.error) {
|
664 | 680 | const status = result.error.code && standardErrorCodes[result.error.code];
|
|
0 commit comments