@scalar/fastify-api-reference 1.32.0 → 1.32.4
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/CHANGELOG.md +22 -0
- package/README.md +1 -1
- package/dist/fastifyApiReference.js +9 -9
- package/dist/fastifyApiReference.js.map +2 -2
- package/dist/js/standalone.js +15732 -15435
- package/dist/types.d.ts +2 -2
- package/package.json +7 -7
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,27 @@
|
|
|
1
1
|
# @scalar/fastify-api-reference
|
|
2
2
|
|
|
3
|
+
## 1.32.4
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Updated dependencies [66347d4]
|
|
8
|
+
- @scalar/core@0.3.7
|
|
9
|
+
- @scalar/openapi-parser@0.18.1
|
|
10
|
+
|
|
11
|
+
## 1.32.3
|
|
12
|
+
|
|
13
|
+
## 1.32.2
|
|
14
|
+
|
|
15
|
+
### Patch Changes
|
|
16
|
+
|
|
17
|
+
- 2d7f995: refactor: use more common straight apostrophe ' instead of the real apostrophe ’
|
|
18
|
+
- Updated dependencies [2d7f995]
|
|
19
|
+
- @scalar/openapi-parser@0.18.1
|
|
20
|
+
- @scalar/openapi-types@0.3.5
|
|
21
|
+
- @scalar/core@0.3.6
|
|
22
|
+
|
|
23
|
+
## 1.32.1
|
|
24
|
+
|
|
3
25
|
## 1.32.0
|
|
4
26
|
|
|
5
27
|
### Patch Changes
|
package/README.md
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import fp from "fastify-plugin";
|
|
2
2
|
import { slug } from "github-slugger";
|
|
3
3
|
import { getJavaScriptFile } from "./utils/getJavaScriptFile.js";
|
|
4
|
-
import { normalize, toJson, toYaml } from "@scalar/openapi-parser";
|
|
5
4
|
import { getHtmlDocument } from "@scalar/core/libs/html-rendering";
|
|
5
|
+
import { normalize, toJson, toYaml } from "@scalar/openapi-parser";
|
|
6
6
|
const RELATIVE_JAVASCRIPT_PATH = "js/scalar.js";
|
|
7
7
|
const schemaToHideRoute = {
|
|
8
8
|
hide: true
|
|
@@ -129,7 +129,7 @@ const fastifyApiReference = fp(
|
|
|
129
129
|
})();
|
|
130
130
|
if (!specSource) {
|
|
131
131
|
fastify.log.warn(
|
|
132
|
-
"[@scalar/fastify-api-reference] You didn
|
|
132
|
+
"[@scalar/fastify-api-reference] You didn't provide a `content` or `url`, and @fastify/swagger could not be found. Please provide one of these options."
|
|
133
133
|
);
|
|
134
134
|
return;
|
|
135
135
|
}
|
|
@@ -150,7 +150,7 @@ const fastifyApiReference = fp(
|
|
|
150
150
|
fastify.route({
|
|
151
151
|
method: "GET",
|
|
152
152
|
url: openApiSpecUrlJson,
|
|
153
|
-
// @ts-ignore We don
|
|
153
|
+
// @ts-ignore We don't know whether @fastify/swagger is loaded.
|
|
154
154
|
schema: schemaToHideRoute,
|
|
155
155
|
...hooks,
|
|
156
156
|
...options.logLevel && { logLevel: options.logLevel },
|
|
@@ -165,7 +165,7 @@ const fastifyApiReference = fp(
|
|
|
165
165
|
fastify.route({
|
|
166
166
|
method: "GET",
|
|
167
167
|
url: openApiSpecUrlYaml,
|
|
168
|
-
// @ts-ignore We don
|
|
168
|
+
// @ts-ignore We don't know whether @fastify/swagger is loaded.
|
|
169
169
|
schema: schemaToHideRoute,
|
|
170
170
|
...hooks,
|
|
171
171
|
...options.logLevel && { logLevel: options.logLevel },
|
|
@@ -193,8 +193,8 @@ const fastifyApiReference = fp(
|
|
|
193
193
|
fastify.route({
|
|
194
194
|
method: "GET",
|
|
195
195
|
url: `${getRoutePrefix(options.routePrefix)}/`,
|
|
196
|
-
// We don
|
|
197
|
-
// @ts-ignore We don
|
|
196
|
+
// We don't know whether @fastify/swagger is registered, but it doesn't hurt to add a schema anyway.
|
|
197
|
+
// @ts-ignore We don't know whether @fastify/swagger is loaded.
|
|
198
198
|
schema: schemaToHideRoute,
|
|
199
199
|
...hooks,
|
|
200
200
|
...options.logLevel && { logLevel: options.logLevel },
|
|
@@ -213,7 +213,7 @@ const fastifyApiReference = fp(
|
|
|
213
213
|
return reply.header("Content-Type", "text/html; charset=utf-8").send(
|
|
214
214
|
getHtmlDocument(
|
|
215
215
|
{
|
|
216
|
-
// We
|
|
216
|
+
// We're using the bundled JS here by default, but the user can pass a CDN URL.
|
|
217
217
|
cdn: RELATIVE_JAVASCRIPT_PATH,
|
|
218
218
|
...configuration
|
|
219
219
|
},
|
|
@@ -225,8 +225,8 @@ const fastifyApiReference = fp(
|
|
|
225
225
|
fastify.route({
|
|
226
226
|
method: "GET",
|
|
227
227
|
url: getJavaScriptUrl(options.routePrefix),
|
|
228
|
-
// We don
|
|
229
|
-
// @ts-ignore We don
|
|
228
|
+
// We don't know whether @fastify/swagger is registered, but it doesn't hurt to add a schema anyway.
|
|
229
|
+
// @ts-ignore We don't know whether @fastify/swagger is loaded.
|
|
230
230
|
schema: schemaToHideRoute,
|
|
231
231
|
...hooks,
|
|
232
232
|
...options.logLevel && { logLevel: options.logLevel },
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/fastifyApiReference.ts"],
|
|
4
|
-
"sourcesContent": ["import type { OpenAPI } from '@scalar/openapi-types'\nimport type { FastifyBaseLogger, FastifyTypeProviderDefault, RawServerDefault } from 'fastify'\nimport fp from 'fastify-plugin'\nimport { slug } from 'github-slugger'\n\nimport type { FastifyApiReferenceHooksOptions, FastifyApiReferenceOptions } from './types'\nimport { getJavaScriptFile } from './utils/getJavaScriptFile'\n\nimport { normalize, toJson, toYaml } from '@scalar/openapi-parser'\nimport { getHtmlDocument } from '@scalar/core/libs/html-rendering'\nimport type { ApiReferenceConfiguration } from './types'\n\n/**\n * Path to the bundled Scalar JavaScript file\n */\nconst RELATIVE_JAVASCRIPT_PATH = 'js/scalar.js'\n\n// This Schema is used to hide the route from the documentation.\n// https://github.com/fastify/fastify-swagger#hide-a-route\nconst schemaToHideRoute = {\n hide: true,\n}\n\nconst getRoutePrefix = (routePrefix?: string) => {\n const prefix = routePrefix ?? '/reference'\n\n // Remove trailing slash if present\n return prefix.endsWith('/') ? prefix.slice(0, -1) : prefix\n}\n\n/**\n * Get the endpoints for the OpenAPI specification.\n */\nconst getOpenApiDocumentEndpoints = (\n openApiDocumentEndpoints: FastifyApiReferenceOptions['openApiDocumentEndpoints'],\n) => {\n const { json = '/openapi.json', yaml = '/openapi.yaml' } = openApiDocumentEndpoints ?? {}\n return { json, yaml }\n}\n\n/**\n * Get the URL for the Scalar JavaScript file.\n */\nconst getJavaScriptUrl = (routePrefix?: string) =>\n `${getRoutePrefix(routePrefix)}/${RELATIVE_JAVASCRIPT_PATH}`.replace(/\\/\\//g, '/')\n\n/**\n * The custom theme for Fastify\n */\nexport const customTheme = `\n.light-mode {\n color-scheme: light;\n --scalar-color-1: #1c1e21;\n --scalar-color-2: #757575;\n --scalar-color-3: #8e8e8e;\n --scalar-color-disabled: #b4b1b1;\n --scalar-color-ghost: #a7a7a7;\n --scalar-color-accent: #2f8555;\n --scalar-background-1: #fff;\n --scalar-background-2: #f5f5f5;\n --scalar-background-3: #ededed;\n --scalar-background-4: rgba(0, 0, 0, 0.06);\n --scalar-background-accent: #2f85551f;\n\n --scalar-border-color: rgba(0, 0, 0, 0.1);\n --scalar-scrollbar-color: rgba(0, 0, 0, 0.18);\n --scalar-scrollbar-color-active: rgba(0, 0, 0, 0.36);\n --scalar-lifted-brightness: 1;\n --scalar-backdrop-brightness: 1;\n\n --scalar-shadow-1: 0 1px 3px 0 rgba(0, 0, 0, 0.11);\n --scalar-shadow-2: rgba(0, 0, 0, 0.08) 0px 13px 20px 0px,\n rgba(0, 0, 0, 0.08) 0px 3px 8px 0px, #eeeeed 0px 0 0 1px;\n\n --scalar-button-1: rgb(49 53 56);\n --scalar-button-1-color: #fff;\n --scalar-button-1-hover: rgb(28 31 33);\n\n --scalar-color-green: #007300;\n --scalar-color-red: #af272b;\n --scalar-color-yellow: #b38200;\n --scalar-color-blue: #3b8ba5;\n --scalar-color-orange: #fb892c;\n --scalar-color-purple: #5203d1;\n}\n\n.dark-mode {\n color-scheme: dark;\n --scalar-color-1: rgba(255, 255, 255, 0.9);\n --scalar-color-2: rgba(255, 255, 255, 0.62);\n --scalar-color-3: rgba(255, 255, 255, 0.44);\n --scalar-color-disabled: rgba(255, 255, 255, 0.34);\n --scalar-color-ghost: rgba(255, 255, 255, 0.26);\n --scalar-color-accent: #27c2a0;\n --scalar-background-1: #1b1b1d;\n --scalar-background-2: #242526;\n --scalar-background-3: #3b3b3b;\n --scalar-background-4: rgba(255, 255, 255, 0.06);\n --scalar-background-accent: #27c2a01f;\n\n --scalar-border-color: rgba(255, 255, 255, 0.1);\n --scalar-scrollbar-color: rgba(255, 255, 255, 0.24);\n --scalar-scrollbar-color-active: rgba(255, 255, 255, 0.48);\n --scalar-lifted-brightness: 1.45;\n --scalar-backdrop-brightness: 0.5;\n\n --scalar-shadow-1: 0 1px 3px 0 rgb(0, 0, 0, 0.1);\n --scalar-shadow-2: rgba(15, 15, 15, 0.2) 0px 3px 6px,\n rgba(15, 15, 15, 0.4) 0px 9px 24px, 0 0 0 1px rgba(255, 255, 255, 0.1);\n\n --scalar-button-1: #f6f6f6;\n --scalar-button-1-color: #000;\n --scalar-button-1-hover: #e7e7e7;\n\n --scalar-color-green: #26b226;\n --scalar-color-red: #fb565b;\n --scalar-color-yellow: #ffc426;\n --scalar-color-blue: #6ecfef;\n --scalar-color-orange: #ff8d4d;\n --scalar-color-purple: #b191f9;\n}\n`\n\n/**\n * The default configuration for Fastify\n */\nconst DEFAULT_CONFIGURATION: Partial<ApiReferenceConfiguration> = {\n _integration: 'fastify',\n}\n\nconst fastifyApiReference = fp<\n FastifyApiReferenceOptions,\n RawServerDefault,\n FastifyTypeProviderDefault,\n FastifyBaseLogger\n>(\n async (fastify, options) => {\n const { configuration: givenConfiguration } = options\n\n // Merge the defaults\n let configuration = {\n ...DEFAULT_CONFIGURATION,\n ...givenConfiguration,\n }\n\n const specSource = (() => {\n const { content, url } = configuration ?? {}\n if (content) {\n return {\n type: 'content' as const,\n get: () => {\n if (typeof content === 'function') {\n return content()\n }\n return content\n },\n }\n }\n if (url) {\n return {\n type: 'url' as const,\n get: () => url,\n }\n }\n\n if (fastify.hasPlugin('@fastify/swagger')) {\n return {\n type: 'swagger' as const,\n // @ts-ignore We know that @fastify/swagger is loaded.\n get: () => fastify.swagger() as OpenAPI.Document,\n }\n }\n return void 0\n })()\n\n // If no OpenAPI specification is passed and @fastify/swagger isn\u2019t loaded, show a warning.\n if (!specSource) {\n fastify.log.warn(\n '[@scalar/fastify-api-reference] You didn\u2019t provide a spec.content or spec.url, and @fastify/swagger could not be found. Please provide one of these options.',\n )\n\n return\n }\n\n // Read the JavaScript file once.\n const fileContent = getJavaScriptFile()\n\n const hooks: FastifyApiReferenceHooksOptions = {}\n if (options.hooks) {\n const additionalHooks: (keyof FastifyApiReferenceHooksOptions)[] = ['onRequest', 'preHandler']\n\n for (const hook of additionalHooks) {\n if (options.hooks[hook]) {\n hooks[hook] = options.hooks[hook]\n }\n }\n }\n\n const getSpecFilenameSlug = async (spec: OpenAPI.Document) => {\n // Same GitHub Slugger and default file name as in `@scalar/api-reference`, when generating the download\n return slug(spec?.specification?.info?.title ?? 'spec')\n }\n\n const openApiSpecUrlJson = `${getRoutePrefix(options.routePrefix)}${getOpenApiDocumentEndpoints(options.openApiDocumentEndpoints).json}`\n fastify.route({\n method: 'GET',\n url: openApiSpecUrlJson,\n // @ts-ignore We don\u2019t know whether @fastify/swagger is loaded.\n schema: schemaToHideRoute,\n ...hooks,\n ...(options.logLevel && { logLevel: options.logLevel }),\n async handler(_, reply) {\n const spec = normalize(specSource.get())\n const filename: string = await getSpecFilenameSlug(spec)\n const json = JSON.parse(toJson(spec)) // parsing minifies the JSON\n\n return reply\n .header('Content-Type', 'application/json')\n .header('Content-Disposition', `filename=${filename}.json`)\n .header('Access-Control-Allow-Origin', '*')\n .header('Access-Control-Allow-Methods', '*')\n .send(json)\n },\n })\n\n const openApiSpecUrlYaml = `${getRoutePrefix(options.routePrefix)}${getOpenApiDocumentEndpoints(options.openApiDocumentEndpoints).yaml}`\n fastify.route({\n method: 'GET',\n url: openApiSpecUrlYaml,\n // @ts-ignore We don\u2019t know whether @fastify/swagger is loaded.\n schema: schemaToHideRoute,\n ...hooks,\n ...(options.logLevel && { logLevel: options.logLevel }),\n async handler(_, reply) {\n const spec = normalize(specSource.get())\n const filename: string = await getSpecFilenameSlug(spec)\n const yaml = toYaml(spec)\n return reply\n .header('Content-Type', 'application/yaml')\n .header('Content-Disposition', `filename=${filename}.yaml`)\n .header('Access-Control-Allow-Origin', '*')\n .header('Access-Control-Allow-Methods', '*')\n .send(yaml)\n },\n })\n\n // Redirect route without a trailing slash to force a trailing slash:\n // We need this so the request to the JS file is relative.\n\n // With ignoreTrailingSlash, fastify registeres both routes anyway.\n const doesNotIgnoreTrailingSlash = fastify.initialConfig.ignoreTrailingSlash !== true\n\n if (doesNotIgnoreTrailingSlash && getRoutePrefix(options.routePrefix)) {\n fastify.route({\n method: 'GET',\n url: getRoutePrefix(options.routePrefix),\n // @ts-ignore We don't know whether @fastify/swagger is loaded.\n schema: schemaToHideRoute,\n ...hooks,\n ...(options.logLevel && { logLevel: options.logLevel }),\n handler(_, reply) {\n return reply.redirect(getRoutePrefix(options.routePrefix) + '/', 302)\n },\n })\n }\n\n // If no theme is passed, use the default theme.\n fastify.route({\n method: 'GET',\n url: `${getRoutePrefix(options.routePrefix)}/`,\n // We don\u2019t know whether @fastify/swagger is registered, but it doesn\u2019t hurt to add a schema anyway.\n // @ts-ignore We don\u2019t know whether @fastify/swagger is loaded.\n schema: schemaToHideRoute,\n ...hooks,\n ...(options.logLevel && { logLevel: options.logLevel }),\n handler(_, reply) {\n // Redirect if it\u2019s the route without a slash\n const currentUrl = new URL(_.url, `${_.protocol}://${_.hostname}`)\n\n if (!currentUrl.pathname.endsWith('/')) {\n return reply.redirect(`${currentUrl.pathname}/`, 301)\n }\n\n /**\n * Regardless of where we source the spec from, provide it as a URL, to have the\n * download button point to the exposed endpoint.\n * If the URL is explicitly passed, defer to that URL instead.\n */\n if (specSource.type !== 'url') {\n configuration = {\n ...configuration,\n // Use a relative URL in case we're proxied\n url: `.${getOpenApiDocumentEndpoints(options.openApiDocumentEndpoints).json}`,\n }\n }\n\n // Respond with the HTML document\n return reply.header('Content-Type', 'text/html; charset=utf-8').send(\n getHtmlDocument(\n {\n // We\u2019re using the bundled JS here by default, but the user can pass a CDN URL.\n cdn: RELATIVE_JAVASCRIPT_PATH,\n ...configuration,\n },\n customTheme,\n ),\n )\n },\n })\n\n fastify.route({\n method: 'GET',\n url: getJavaScriptUrl(options.routePrefix),\n // We don\u2019t know whether @fastify/swagger is registered, but it doesn\u2019t hurt to add a schema anyway.\n // @ts-ignore We don\u2019t know whether @fastify/swagger is loaded.\n schema: schemaToHideRoute,\n ...hooks,\n ...(options.logLevel && { logLevel: options.logLevel }),\n handler(_, reply) {\n return reply.header('Content-Type', 'application/javascript; charset=utf-8').send(fileContent)\n },\n })\n },\n {\n name: '@scalar/fastify-api-reference',\n },\n)\n\nexport default fastifyApiReference\n"],
|
|
5
|
-
"mappings": "AAEA,OAAO,QAAQ;AACf,SAAS,YAAY;AAGrB,SAAS,yBAAyB;AAElC,SAAS,WAAW,QAAQ,cAAc;
|
|
4
|
+
"sourcesContent": ["import type { OpenAPI } from '@scalar/openapi-types'\nimport type { FastifyBaseLogger, FastifyTypeProviderDefault, RawServerDefault } from 'fastify'\nimport fp from 'fastify-plugin'\nimport { slug } from 'github-slugger'\n\nimport type { FastifyApiReferenceHooksOptions, FastifyApiReferenceOptions } from './types'\nimport { getJavaScriptFile } from './utils/getJavaScriptFile'\n\nimport { getHtmlDocument } from '@scalar/core/libs/html-rendering'\nimport { normalize, toJson, toYaml } from '@scalar/openapi-parser'\nimport type { ApiReferenceConfiguration } from './types'\n\n/**\n * Path to the bundled Scalar JavaScript file\n */\nconst RELATIVE_JAVASCRIPT_PATH = 'js/scalar.js'\n\n// This Schema is used to hide the route from the documentation.\n// https://github.com/fastify/fastify-swagger#hide-a-route\nconst schemaToHideRoute = {\n hide: true,\n}\n\nconst getRoutePrefix = (routePrefix?: string) => {\n const prefix = routePrefix ?? '/reference'\n\n // Remove trailing slash if present\n return prefix.endsWith('/') ? prefix.slice(0, -1) : prefix\n}\n\n/**\n * Get the endpoints for the OpenAPI specification.\n */\nconst getOpenApiDocumentEndpoints = (\n openApiDocumentEndpoints: FastifyApiReferenceOptions['openApiDocumentEndpoints'],\n) => {\n const { json = '/openapi.json', yaml = '/openapi.yaml' } = openApiDocumentEndpoints ?? {}\n return { json, yaml }\n}\n\n/**\n * Get the URL for the Scalar JavaScript file.\n */\nconst getJavaScriptUrl = (routePrefix?: string) =>\n `${getRoutePrefix(routePrefix)}/${RELATIVE_JAVASCRIPT_PATH}`.replace(/\\/\\//g, '/')\n\n/**\n * The custom theme for Fastify\n */\nexport const customTheme = `\n.light-mode {\n color-scheme: light;\n --scalar-color-1: #1c1e21;\n --scalar-color-2: #757575;\n --scalar-color-3: #8e8e8e;\n --scalar-color-disabled: #b4b1b1;\n --scalar-color-ghost: #a7a7a7;\n --scalar-color-accent: #2f8555;\n --scalar-background-1: #fff;\n --scalar-background-2: #f5f5f5;\n --scalar-background-3: #ededed;\n --scalar-background-4: rgba(0, 0, 0, 0.06);\n --scalar-background-accent: #2f85551f;\n\n --scalar-border-color: rgba(0, 0, 0, 0.1);\n --scalar-scrollbar-color: rgba(0, 0, 0, 0.18);\n --scalar-scrollbar-color-active: rgba(0, 0, 0, 0.36);\n --scalar-lifted-brightness: 1;\n --scalar-backdrop-brightness: 1;\n\n --scalar-shadow-1: 0 1px 3px 0 rgba(0, 0, 0, 0.11);\n --scalar-shadow-2: rgba(0, 0, 0, 0.08) 0px 13px 20px 0px,\n rgba(0, 0, 0, 0.08) 0px 3px 8px 0px, #eeeeed 0px 0 0 1px;\n\n --scalar-button-1: rgb(49 53 56);\n --scalar-button-1-color: #fff;\n --scalar-button-1-hover: rgb(28 31 33);\n\n --scalar-color-green: #007300;\n --scalar-color-red: #af272b;\n --scalar-color-yellow: #b38200;\n --scalar-color-blue: #3b8ba5;\n --scalar-color-orange: #fb892c;\n --scalar-color-purple: #5203d1;\n}\n\n.dark-mode {\n color-scheme: dark;\n --scalar-color-1: rgba(255, 255, 255, 0.9);\n --scalar-color-2: rgba(255, 255, 255, 0.62);\n --scalar-color-3: rgba(255, 255, 255, 0.44);\n --scalar-color-disabled: rgba(255, 255, 255, 0.34);\n --scalar-color-ghost: rgba(255, 255, 255, 0.26);\n --scalar-color-accent: #27c2a0;\n --scalar-background-1: #1b1b1d;\n --scalar-background-2: #242526;\n --scalar-background-3: #3b3b3b;\n --scalar-background-4: rgba(255, 255, 255, 0.06);\n --scalar-background-accent: #27c2a01f;\n\n --scalar-border-color: rgba(255, 255, 255, 0.1);\n --scalar-scrollbar-color: rgba(255, 255, 255, 0.24);\n --scalar-scrollbar-color-active: rgba(255, 255, 255, 0.48);\n --scalar-lifted-brightness: 1.45;\n --scalar-backdrop-brightness: 0.5;\n\n --scalar-shadow-1: 0 1px 3px 0 rgb(0, 0, 0, 0.1);\n --scalar-shadow-2: rgba(15, 15, 15, 0.2) 0px 3px 6px,\n rgba(15, 15, 15, 0.4) 0px 9px 24px, 0 0 0 1px rgba(255, 255, 255, 0.1);\n\n --scalar-button-1: #f6f6f6;\n --scalar-button-1-color: #000;\n --scalar-button-1-hover: #e7e7e7;\n\n --scalar-color-green: #26b226;\n --scalar-color-red: #fb565b;\n --scalar-color-yellow: #ffc426;\n --scalar-color-blue: #6ecfef;\n --scalar-color-orange: #ff8d4d;\n --scalar-color-purple: #b191f9;\n}\n`\n\n/**\n * The default configuration for Fastify\n */\nconst DEFAULT_CONFIGURATION: Partial<ApiReferenceConfiguration> = {\n _integration: 'fastify',\n}\n\nconst fastifyApiReference = fp<\n FastifyApiReferenceOptions,\n RawServerDefault,\n FastifyTypeProviderDefault,\n FastifyBaseLogger\n>(\n async (fastify, options) => {\n const { configuration: givenConfiguration } = options\n\n // Merge the defaults\n let configuration = {\n ...DEFAULT_CONFIGURATION,\n ...givenConfiguration,\n }\n\n const specSource = (() => {\n const { content, url } = configuration ?? {}\n if (content) {\n return {\n type: 'content' as const,\n get: () => {\n if (typeof content === 'function') {\n return content()\n }\n return content\n },\n }\n }\n if (url) {\n return {\n type: 'url' as const,\n get: () => url,\n }\n }\n\n if (fastify.hasPlugin('@fastify/swagger')) {\n return {\n type: 'swagger' as const,\n // @ts-ignore We know that @fastify/swagger is loaded.\n get: () => fastify.swagger() as OpenAPI.Document,\n }\n }\n return void 0\n })()\n\n // If no OpenAPI specification is passed and @fastify/swagger isn't loaded, show a warning.\n if (!specSource) {\n fastify.log.warn(\n \"[@scalar/fastify-api-reference] You didn't provide a `content` or `url`, and @fastify/swagger could not be found. Please provide one of these options.\",\n )\n\n return\n }\n\n // Read the JavaScript file once.\n const fileContent = getJavaScriptFile()\n\n const hooks: FastifyApiReferenceHooksOptions = {}\n if (options.hooks) {\n const additionalHooks: (keyof FastifyApiReferenceHooksOptions)[] = ['onRequest', 'preHandler']\n\n for (const hook of additionalHooks) {\n if (options.hooks[hook]) {\n hooks[hook] = options.hooks[hook]\n }\n }\n }\n\n const getSpecFilenameSlug = async (spec: OpenAPI.Document) => {\n // Same GitHub Slugger and default file name as in `@scalar/api-reference`, when generating the download\n return slug(spec?.specification?.info?.title ?? 'spec')\n }\n\n const openApiSpecUrlJson = `${getRoutePrefix(options.routePrefix)}${getOpenApiDocumentEndpoints(options.openApiDocumentEndpoints).json}`\n fastify.route({\n method: 'GET',\n url: openApiSpecUrlJson,\n // @ts-ignore We don't know whether @fastify/swagger is loaded.\n schema: schemaToHideRoute,\n ...hooks,\n ...(options.logLevel && { logLevel: options.logLevel }),\n async handler(_, reply) {\n const spec = normalize(specSource.get())\n const filename: string = await getSpecFilenameSlug(spec)\n const json = JSON.parse(toJson(spec)) // parsing minifies the JSON\n\n return reply\n .header('Content-Type', 'application/json')\n .header('Content-Disposition', `filename=${filename}.json`)\n .header('Access-Control-Allow-Origin', '*')\n .header('Access-Control-Allow-Methods', '*')\n .send(json)\n },\n })\n\n const openApiSpecUrlYaml = `${getRoutePrefix(options.routePrefix)}${getOpenApiDocumentEndpoints(options.openApiDocumentEndpoints).yaml}`\n fastify.route({\n method: 'GET',\n url: openApiSpecUrlYaml,\n // @ts-ignore We don't know whether @fastify/swagger is loaded.\n schema: schemaToHideRoute,\n ...hooks,\n ...(options.logLevel && { logLevel: options.logLevel }),\n async handler(_, reply) {\n const spec = normalize(specSource.get())\n const filename: string = await getSpecFilenameSlug(spec)\n const yaml = toYaml(spec)\n return reply\n .header('Content-Type', 'application/yaml')\n .header('Content-Disposition', `filename=${filename}.yaml`)\n .header('Access-Control-Allow-Origin', '*')\n .header('Access-Control-Allow-Methods', '*')\n .send(yaml)\n },\n })\n\n // Redirect route without a trailing slash to force a trailing slash:\n // We need this so the request to the JS file is relative.\n\n // With ignoreTrailingSlash, fastify registeres both routes anyway.\n const doesNotIgnoreTrailingSlash = fastify.initialConfig.ignoreTrailingSlash !== true\n\n if (doesNotIgnoreTrailingSlash && getRoutePrefix(options.routePrefix)) {\n fastify.route({\n method: 'GET',\n url: getRoutePrefix(options.routePrefix),\n // @ts-ignore We don't know whether @fastify/swagger is loaded.\n schema: schemaToHideRoute,\n ...hooks,\n ...(options.logLevel && { logLevel: options.logLevel }),\n handler(_, reply) {\n return reply.redirect(getRoutePrefix(options.routePrefix) + '/', 302)\n },\n })\n }\n\n // If no theme is passed, use the default theme.\n fastify.route({\n method: 'GET',\n url: `${getRoutePrefix(options.routePrefix)}/`,\n // We don't know whether @fastify/swagger is registered, but it doesn't hurt to add a schema anyway.\n // @ts-ignore We don't know whether @fastify/swagger is loaded.\n schema: schemaToHideRoute,\n ...hooks,\n ...(options.logLevel && { logLevel: options.logLevel }),\n handler(_, reply) {\n // Redirect if it's the route without a slash\n const currentUrl = new URL(_.url, `${_.protocol}://${_.hostname}`)\n\n if (!currentUrl.pathname.endsWith('/')) {\n return reply.redirect(`${currentUrl.pathname}/`, 301)\n }\n\n /**\n * Regardless of where we source the spec from, provide it as a URL, to have the\n * download button point to the exposed endpoint.\n * If the URL is explicitly passed, defer to that URL instead.\n */\n if (specSource.type !== 'url') {\n configuration = {\n ...configuration,\n // Use a relative URL in case we're proxied\n url: `.${getOpenApiDocumentEndpoints(options.openApiDocumentEndpoints).json}`,\n }\n }\n\n // Respond with the HTML document\n return reply.header('Content-Type', 'text/html; charset=utf-8').send(\n getHtmlDocument(\n {\n // We're using the bundled JS here by default, but the user can pass a CDN URL.\n cdn: RELATIVE_JAVASCRIPT_PATH,\n ...configuration,\n },\n customTheme,\n ),\n )\n },\n })\n\n fastify.route({\n method: 'GET',\n url: getJavaScriptUrl(options.routePrefix),\n // We don't know whether @fastify/swagger is registered, but it doesn't hurt to add a schema anyway.\n // @ts-ignore We don't know whether @fastify/swagger is loaded.\n schema: schemaToHideRoute,\n ...hooks,\n ...(options.logLevel && { logLevel: options.logLevel }),\n handler(_, reply) {\n return reply.header('Content-Type', 'application/javascript; charset=utf-8').send(fileContent)\n },\n })\n },\n {\n name: '@scalar/fastify-api-reference',\n },\n)\n\nexport default fastifyApiReference\n"],
|
|
5
|
+
"mappings": "AAEA,OAAO,QAAQ;AACf,SAAS,YAAY;AAGrB,SAAS,yBAAyB;AAElC,SAAS,uBAAuB;AAChC,SAAS,WAAW,QAAQ,cAAc;AAM1C,MAAM,2BAA2B;AAIjC,MAAM,oBAAoB;AAAA,EACxB,MAAM;AACR;AAEA,MAAM,iBAAiB,CAAC,gBAAyB;AAC/C,QAAM,SAAS,eAAe;AAG9B,SAAO,OAAO,SAAS,GAAG,IAAI,OAAO,MAAM,GAAG,EAAE,IAAI;AACtD;AAKA,MAAM,8BAA8B,CAClC,6BACG;AACH,QAAM,EAAE,OAAO,iBAAiB,OAAO,gBAAgB,IAAI,4BAA4B,CAAC;AACxF,SAAO,EAAE,MA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