|
1 | 1 | // DO NOT EDIT: generated by fsdgenjs |
2 | 2 | /* eslint-disable */ |
3 | 3 |
|
4 | | -import { FastifyPluginAsync, RegisterOptions } from 'fastify'; |
5 | | -import { IServiceResult, IServiceError } from 'facility-core'; |
| 4 | +import type * as fastifyTypes from 'fastify'; |
| 5 | +import type { IServiceResult, IServiceError } from 'facility-core'; |
6 | 6 |
|
7 | | -export type ConformanceApiPluginOptions = RegisterOptions & { |
8 | | - api: IConformanceApi; |
| 7 | +export type ConformanceApiPluginOptions = fastifyTypes.RegisterOptions & { |
| 8 | + /** The `IConformanceApi` service implementation. Can be a service instance or a factory function which is called on each request. */ |
| 9 | + serviceOrFactory: IConformanceApi | ((req: fastifyTypes.FastifyRequest) => IConformanceApi); |
| 10 | + |
| 11 | + /** Whether to make query string keys case insensitive. Defalts to false. */ |
9 | 12 | caseInsenstiveQueryStringKeys?: boolean; |
| 13 | + |
| 14 | + /** Whether to include error details in the response. Defaults to false. */ |
10 | 15 | includeErrorDetails?: boolean; |
11 | 16 | } |
12 | 17 |
|
13 | | -export const conformanceApiPlugin: FastifyPluginAsync<ConformanceApiPluginOptions> = async (fastify, opts) => { |
14 | | - const { api, caseInsenstiveQueryStringKeys, includeErrorDetails } = opts; |
| 18 | +export const conformanceApiPlugin: fastifyTypes.FastifyPluginAsync<ConformanceApiPluginOptions> = async (fastify, opts) => { |
| 19 | + const { serviceOrFactory, caseInsenstiveQueryStringKeys, includeErrorDetails } = opts; |
| 20 | + |
| 21 | + const getService = typeof serviceOrFactory === 'function' ? serviceOrFactory : () => serviceOrFactory; |
15 | 22 |
|
16 | 23 | for (const jsonSchema of jsonSchemas) { |
17 | 24 | fastify.addSchema(jsonSchema); |
@@ -66,7 +73,7 @@ export const conformanceApiPlugin: FastifyPluginAsync<ConformanceApiPluginOption |
66 | 73 | handler: async function (req, res) { |
67 | 74 | const request: Partial<IGetApiInfoRequest> = {}; |
68 | 75 |
|
69 | | - const result = await api.getApiInfo(request as IGetApiInfoRequest); |
| 76 | + const result = await getService(req).getApiInfo(request as IGetApiInfoRequest); |
70 | 77 |
|
71 | 78 | if (result.error) { |
72 | 79 | const status = result.error.code && standardErrorCodes[result.error.code]; |
@@ -102,7 +109,7 @@ export const conformanceApiPlugin: FastifyPluginAsync<ConformanceApiPluginOption |
102 | 109 | const query = req.query as Record<string, string>; |
103 | 110 | if (typeof query['q'] === 'string') request.query = query['q']; |
104 | 111 |
|
105 | | - const result = await api.getWidgets(request as IGetWidgetsRequest); |
| 112 | + const result = await getService(req).getWidgets(request as IGetWidgetsRequest); |
106 | 113 |
|
107 | 114 | if (result.error) { |
108 | 115 | const status = result.error.code && standardErrorCodes[result.error.code]; |
@@ -132,7 +139,7 @@ export const conformanceApiPlugin: FastifyPluginAsync<ConformanceApiPluginOption |
132 | 139 |
|
133 | 140 | request.widget = req.body as never; |
134 | 141 |
|
135 | | - const result = await api.createWidget(request as ICreateWidgetRequest); |
| 142 | + const result = await getService(req).createWidget(request as ICreateWidgetRequest); |
136 | 143 |
|
137 | 144 | if (result.error) { |
138 | 145 | const status = result.error.code && standardErrorCodes[result.error.code]; |
@@ -172,7 +179,7 @@ export const conformanceApiPlugin: FastifyPluginAsync<ConformanceApiPluginOption |
172 | 179 | const headers = req.headers as Record<string, string>; |
173 | 180 | if (typeof headers['if-none-match'] === 'string') request.ifNotETag = headers['if-none-match']; |
174 | 181 |
|
175 | | - const result = await api.getWidget(request as IGetWidgetRequest); |
| 182 | + const result = await getService(req).getWidget(request as IGetWidgetRequest); |
176 | 183 |
|
177 | 184 | if (result.error) { |
178 | 185 | const status = result.error.code && standardErrorCodes[result.error.code]; |
@@ -217,7 +224,7 @@ export const conformanceApiPlugin: FastifyPluginAsync<ConformanceApiPluginOption |
217 | 224 | const headers = req.headers as Record<string, string>; |
218 | 225 | if (typeof headers['if-match'] === 'string') request.ifETag = headers['if-match']; |
219 | 226 |
|
220 | | - const result = await api.deleteWidget(request as IDeleteWidgetRequest); |
| 227 | + const result = await getService(req).deleteWidget(request as IDeleteWidgetRequest); |
221 | 228 |
|
222 | 229 | if (result.error) { |
223 | 230 | const status = result.error.code && standardErrorCodes[result.error.code]; |
@@ -257,7 +264,7 @@ export const conformanceApiPlugin: FastifyPluginAsync<ConformanceApiPluginOption |
257 | 264 |
|
258 | 265 | request.ids = req.body as never; |
259 | 266 |
|
260 | | - const result = await api.getWidgetBatch(request as IGetWidgetBatchRequest); |
| 267 | + const result = await getService(req).getWidgetBatch(request as IGetWidgetBatchRequest); |
261 | 268 |
|
262 | 269 | if (result.error) { |
263 | 270 | const status = result.error.code && standardErrorCodes[result.error.code]; |
@@ -297,7 +304,7 @@ export const conformanceApiPlugin: FastifyPluginAsync<ConformanceApiPluginOption |
297 | 304 | request.field = body.field; |
298 | 305 | request.matrix = body.matrix; |
299 | 306 |
|
300 | | - const result = await api.mirrorFields(request as IMirrorFieldsRequest); |
| 307 | + const result = await getService(req).mirrorFields(request as IMirrorFieldsRequest); |
301 | 308 |
|
302 | 309 | if (result.error) { |
303 | 310 | const status = result.error.code && standardErrorCodes[result.error.code]; |
@@ -335,7 +342,7 @@ export const conformanceApiPlugin: FastifyPluginAsync<ConformanceApiPluginOption |
335 | 342 | if (typeof query['enum'] === 'string') request.enum = query['enum'] as Answer; |
336 | 343 | if (typeof query['datetime'] === 'string') request.datetime = query['datetime']; |
337 | 344 |
|
338 | | - const result = await api.checkQuery(request as ICheckQueryRequest); |
| 345 | + const result = await getService(req).checkQuery(request as ICheckQueryRequest); |
339 | 346 |
|
340 | 347 | if (result.error) { |
341 | 348 | const status = result.error.code && standardErrorCodes[result.error.code]; |
@@ -373,7 +380,7 @@ export const conformanceApiPlugin: FastifyPluginAsync<ConformanceApiPluginOption |
373 | 380 | if (typeof params['enum'] === 'string') request.enum = params['enum'] as Answer; |
374 | 381 | if (typeof params['datetime'] === 'string') request.datetime = params['datetime']; |
375 | 382 |
|
376 | | - const result = await api.checkPath(request as ICheckPathRequest); |
| 383 | + const result = await getService(req).checkPath(request as ICheckPathRequest); |
377 | 384 |
|
378 | 385 | if (result.error) { |
379 | 386 | const status = result.error.code && standardErrorCodes[result.error.code]; |
@@ -411,7 +418,7 @@ export const conformanceApiPlugin: FastifyPluginAsync<ConformanceApiPluginOption |
411 | 418 | if (typeof headers['enum'] === 'string') request.enum = headers['enum'] as Answer; |
412 | 419 | if (typeof headers['datetime'] === 'string') request.datetime = headers['datetime']; |
413 | 420 |
|
414 | | - const result = await api.mirrorHeaders(request as IMirrorHeadersRequest); |
| 421 | + const result = await getService(req).mirrorHeaders(request as IMirrorHeadersRequest); |
415 | 422 |
|
416 | 423 | if (result.error) { |
417 | 424 | const status = result.error.code && standardErrorCodes[result.error.code]; |
@@ -467,7 +474,7 @@ export const conformanceApiPlugin: FastifyPluginAsync<ConformanceApiPluginOption |
467 | 474 | const body = req.body as Record<string, never>; |
468 | 475 | request.normal = body.normal; |
469 | 476 |
|
470 | | - const result = await api.mixed(request as IMixedRequest); |
| 477 | + const result = await getService(req).mixed(request as IMixedRequest); |
471 | 478 |
|
472 | 479 | if (result.error) { |
473 | 480 | const status = result.error.code && standardErrorCodes[result.error.code]; |
@@ -526,7 +533,7 @@ export const conformanceApiPlugin: FastifyPluginAsync<ConformanceApiPluginOption |
526 | 533 | request.hasWidget = body.hasWidget; |
527 | 534 | request.point = body.point; |
528 | 535 |
|
529 | | - const result = await api.required(request as IRequiredRequest); |
| 536 | + const result = await getService(req).required(request as IRequiredRequest); |
530 | 537 |
|
531 | 538 | if (result.error) { |
532 | 539 | const status = result.error.code && standardErrorCodes[result.error.code]; |
@@ -559,7 +566,7 @@ export const conformanceApiPlugin: FastifyPluginAsync<ConformanceApiPluginOption |
559 | 566 |
|
560 | 567 | request.content = req.body as never; |
561 | 568 |
|
562 | | - const result = await api.mirrorBytes(request as IMirrorBytesRequest); |
| 569 | + const result = await getService(req).mirrorBytes(request as IMirrorBytesRequest); |
563 | 570 |
|
564 | 571 | if (result.error) { |
565 | 572 | const status = result.error.code && standardErrorCodes[result.error.code]; |
@@ -596,7 +603,7 @@ export const conformanceApiPlugin: FastifyPluginAsync<ConformanceApiPluginOption |
596 | 603 |
|
597 | 604 | request.content = req.body as never; |
598 | 605 |
|
599 | | - const result = await api.mirrorText(request as IMirrorTextRequest); |
| 606 | + const result = await getService(req).mirrorText(request as IMirrorTextRequest); |
600 | 607 |
|
601 | 608 | if (result.error) { |
602 | 609 | const status = result.error.code && standardErrorCodes[result.error.code]; |
@@ -630,7 +637,7 @@ export const conformanceApiPlugin: FastifyPluginAsync<ConformanceApiPluginOption |
630 | 637 |
|
631 | 638 | request.content = req.body as never; |
632 | 639 |
|
633 | | - const result = await api.bodyTypes(request as IBodyTypesRequest); |
| 640 | + const result = await getService(req).bodyTypes(request as IBodyTypesRequest); |
634 | 641 |
|
635 | 642 | if (result.error) { |
636 | 643 | const status = result.error.code && standardErrorCodes[result.error.code]; |
|
0 commit comments