You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
1 lines
5.8 KiB
1 lines
5.8 KiB
{"version":3,"file":"index.js","sourceRoot":"","sources":["../lib/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,2BAAoD;AACpD,+BAA4B;AAG5B,uDAAyC;AAEzC,mCAAsC;AACtC,6CAAoD;AAyCpD,MAAM,cAAc,GAAwB;IAC1C,KAAK,EAAE,YAAY;IACnB,YAAY,EAAE,KAAK;IACnB,cAAc,EAAE;QACd,MAAM,EAAE,aAAa;QACrB,GAAG,EAAE,6CAA6C;QAClD,MAAM,EAAE,kBAAkB;KAC3B;IACD,WAAW,EAAE,OAAO;IACpB,UAAU,EAAE,YAAY;IACxB,cAAc,EAAE,EAAE;IAClB,aAAa,EAAE,mDAAmD;IAClE,UAAU,EAAE,KAAK;IACjB,UAAU,EAAE,KAAK;IACjB,OAAO,EAAE,cAAc;IACvB,SAAS,EAAE,EAAE;CACd,CAAC;AAEF,SAAgB,UAAU,CACxB,SAAuC,EAAE;IAEzC,IAAI,MAAM,CAAC,cAAc,KAAK,SAAS,EAAE;QACvC,MAAM,GAAG,GAAG,IAAA,kBAAa,EAAC,EAAE,GAAG,EAAE,SAAS,EAAE,CAAC,CAAC;QAC9C,IAAI,GAAG,KAAK,SAAS,EAAE;YACrB,MAAM,IAAI,KAAK,CAAC,mBAAmB,CAAC,CAAC;SACtC;QAED,cAAc,CAAC,cAAc;YAC3B,GAAG,CAAC,WAAW,CAAC,eAAgB,CAAC,iBAAiB,CAAC,CAAC;KACvD;IAED,cAAc;IACd,MAAM,UAAU,GAAG,MAAM,CAAC,OAAO,CAAC;IAClC,MAAM,WAAW,GAAG,IAAA,WAAI,EAAC,SAAS,EAAE,cAAc,CAAC,OAAO,CAAC,CAAC;IAE5D,wBAAwB;IACxB,MAAM,OAAO,GAAwB,IAAA,qBAAY,EAAC,MAAM,EAAE,cAAc,CAAC,CAAC;IAE1E,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C;YAC1B,OAAO,IAAI,CAAC;SACb;QAED,IAAI,UAAU,KAAK,SAAS,IAAI,GAAG,CAAC,IAAI,KAAK,cAAc,CAAC,OAAO,EAAE;YACnE,GAAG,CAAC,IAAI,GAAG,WAAW,CAAC;YACvB,GAAG,CAAC,IAAI,GAAG,IAAA,qBAAgB,EAAC,WAAW,CAAC,CAAC;YACzC,OAAO,IAAI,CAAC;SACb;QAED,OAAO,IAAI,EAAE,CAAC;IAChB,CAAC,CAAC;AACJ,CAAC;AAzDD,gCAyDC","sourcesContent":["import { createReadStream, readFileSync } from 'fs';\nimport { join } from 'path';\n\nimport type { HelperDelegate, HelperOptions } from 'handlebars';\nimport * as Handlebars from 'handlebars';\nimport type { Context, Middleware, Next } from 'koa';\nimport { defaultsDeep } from 'lodash';\nimport { sync as readPkgUpSync } from 'read-pkg-up';\n\nexport interface SwaggerOptions {\n [key: string]:\n | string\n | boolean\n | string[]\n | Record<string, unknown>\n | Array<Record<string, unknown>>\n | null\n | undefined;\n dom_id?: string;\n url?: string;\n urls?: Array<{\n url: string;\n name: string;\n }>;\n supportedSubmitMethods?: string[];\n docExpansion?: string;\n jsonEditor?: boolean;\n defaultModelRendering?: string;\n showRequestHeaders?: boolean;\n layout?: string;\n spec?: Record<string, unknown>;\n validatorUrl?: string | null;\n}\n\nexport interface KoaSwaggerUiOptions {\n title: string;\n oauthOptions: boolean | any;\n swaggerOptions: SwaggerOptions;\n swaggerVersion: string;\n swaggerCdnUrl?: string;\n routePrefix: string | false;\n specPrefix: string;\n exposeSpec: boolean;\n hideTopbar: boolean;\n favicon: string;\n customCSS: string;\n}\n\nconst defaultOptions: KoaSwaggerUiOptions = {\n title: 'Swagger UI',\n oauthOptions: false,\n swaggerOptions: {\n dom_id: '#swagger-ui',\n url: 'https://petstore.swagger.io/v2/swagger.json',\n layout: 'StandaloneLayout',\n },\n routePrefix: '/docs',\n specPrefix: '/docs/spec',\n swaggerVersion: '',\n swaggerCdnUrl: 'https://cdnjs.cloudflare.com/ajax/libs/swagger-ui',\n exposeSpec: false,\n hideTopbar: false,\n favicon: '/favicon.png',\n customCSS: '',\n};\n\nexport function koaSwagger(\n config: Partial<KoaSwaggerUiOptions> = {},\n): Middleware {\n if (config.swaggerVersion === undefined) {\n const pkg = readPkgUpSync({ cwd: __dirname });\n if (pkg === undefined) {\n throw new Error('Package not found');\n }\n\n defaultOptions.swaggerVersion =\n pkg.packageJson.devDependencies!['swagger-ui-dist'];\n }\n\n // Setup icons\n const extFavicon = config.favicon;\n const faviconPath = join(__dirname, defaultOptions.favicon);\n\n // Setup default options\n const options: KoaSwaggerUiOptions = defaultsDeep(config, defaultOptions);\n\n const specPrefixRegex = new RegExp(`${options.specPrefix}[/]*$`, 'i');\n // eslint-disable-next-line @typescript-eslint/restrict-template-expressions\n const routePrefixRegex = new RegExp(`${options.routePrefix}[/]*$`, 'i');\n\n Handlebars.registerHelper('json', (context) => JSON.stringify(context));\n Handlebars.registerHelper('strfnc', (fnc: HelperDelegate) => fnc);\n Handlebars.registerHelper(\n 'isset',\n function (this: any, conditional: any, opt: HelperOptions) {\n return conditional ? opt.fn(this) : opt.inverse(this);\n },\n );\n const index = Handlebars.compile(\n readFileSync(join(__dirname, './index.hbs'), 'utf-8'),\n );\n\n // eslint-disable-next-line func-names\n return function koaSwaggerUi(ctx: Context, next: Next) {\n if (options.exposeSpec && specPrefixRegex.test(ctx.path)) {\n ctx.body = options.swaggerOptions.spec;\n return true;\n }\n\n if (options.routePrefix === false || routePrefixRegex.test(ctx.path)) {\n ctx.type = 'text/html';\n ctx.body = index(options);\n return true;\n }\n\n if (extFavicon === undefined && ctx.path === defaultOptions.favicon) {\n ctx.type = 'image/png';\n ctx.body = createReadStream(faviconPath);\n return true;\n }\n\n return next();\n };\n}\n"]}
|