UNPKG

3.66 kBJavaScriptView Raw
1"use strict";
2var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3 function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4 return new (P || (P = Promise))(function (resolve, reject) {
5 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7 function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8 step((generator = generator.apply(thisArg, _arguments || [])).next());
9 });
10};
11Object.defineProperty(exports, "__esModule", { value: true });
12exports.serveDocumentsFastify = exports.processSwaggerOptions = void 0;
13function processSwaggerOptions(options = {}) {
14 const unifiedOptions = options;
15 const fastifyOptions = options;
16 const customOptions = {
17 useGlobalPrefix: unifiedOptions.useGlobalPrefix,
18 explorer: unifiedOptions.explorer,
19 customCss: unifiedOptions.customCss,
20 customCssUrl: unifiedOptions.customCssUrl,
21 customJs: unifiedOptions.customJs,
22 customfavIcon: unifiedOptions.customfavIcon,
23 swaggerUrl: unifiedOptions.swaggerUrl,
24 customSiteTitle: unifiedOptions.customSiteTitle,
25 validatorUrl: unifiedOptions.validatorUrl,
26 url: unifiedOptions.url,
27 urls: unifiedOptions.urls,
28 initOAuth: unifiedOptions.initOAuth,
29 swaggerOptions: unifiedOptions.swaggerOptions || fastifyOptions.uiConfig
30 };
31 const extra = {
32 uiHooks: fastifyOptions.uiHooks
33 };
34 return { customOptions, extra };
35}
36exports.processSwaggerOptions = processSwaggerOptions;
37function serveDocumentsFastify(finalPath, httpAdapter, swaggerInitJS, yamlDocument, jsonDocument, html, fastifyExtras) {
38 const httpServer = httpAdapter;
39 const hasParserGetterDefined = Object.getPrototypeOf(httpServer).hasOwnProperty('isParserRegistered');
40 if (hasParserGetterDefined && !httpServer.isParserRegistered) {
41 httpServer.registerParserMiddleware();
42 }
43 httpServer.register((fastifyApp) => __awaiter(this, void 0, void 0, function* () {
44 const hooks = Object.create(null);
45 if (fastifyExtras.uiHooks) {
46 const additionalHooks = ['onRequest', 'preHandler'];
47 for (const hook of additionalHooks) {
48 hooks[hook] = fastifyExtras.uiHooks[hook];
49 }
50 }
51 fastifyApp.route(Object.assign(Object.assign({ url: finalPath, method: 'GET', schema: { hide: true } }, hooks), { handler: (req, reply) => {
52 reply.type('text/html');
53 reply.send(html);
54 } }));
55 fastifyApp.route(Object.assign(Object.assign({ url: `${finalPath}/swagger-ui-init.js`, method: 'GET', schema: { hide: true } }, hooks), { handler: (req, reply) => {
56 reply.type('application/javascript');
57 reply.send(swaggerInitJS);
58 } }));
59 fastifyApp.route(Object.assign(Object.assign({ url: `${finalPath}-json`, method: 'GET', schema: { hide: true } }, hooks), { handler: (req, reply) => {
60 reply.type('text/json');
61 reply.send(jsonDocument);
62 } }));
63 fastifyApp.route(Object.assign(Object.assign({ url: `${finalPath}-yaml`, method: 'GET', schema: { hide: true } }, hooks), { handler: (req, reply) => {
64 reply.type('text/yaml');
65 reply.send(yamlDocument);
66 } }));
67 }));
68}
69exports.serveDocumentsFastify = serveDocumentsFastify;