@scalar/fastify-api-reference 1.31.1 → 1.31.3
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 +10 -0
- package/dist/fastifyApiReference.d.ts +1 -1
- package/dist/fastifyApiReference.js +138 -168
- package/dist/fastifyApiReference.js.map +7 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +6 -1
- package/dist/index.js.map +7 -0
- package/dist/js/standalone.js +7532 -7508
- package/dist/types.js +1 -0
- package/dist/types.js.map +7 -0
- package/dist/utils/getJavaScriptFile.js +16 -21
- package/dist/utils/getJavaScriptFile.js.map +7 -0
- package/package.json +6 -6
- package/dist/packages/core/dist/libs/html-rendering/html-rendering.js +0 -130
package/CHANGELOG.md
CHANGED
|
@@ -1,37 +1,21 @@
|
|
|
1
|
-
import fp from
|
|
2
|
-
import { slug } from
|
|
3
|
-
import { getJavaScriptFile } from
|
|
4
|
-
import { normalize, toJson, toYaml } from
|
|
5
|
-
import { getHtmlDocument } from
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Path to the bundled Scalar JavaScript file
|
|
9
|
-
*/
|
|
10
|
-
const RELATIVE_JAVASCRIPT_PATH = 'js/scalar.js';
|
|
11
|
-
// This Schema is used to hide the route from the documentation.
|
|
12
|
-
// https://github.com/fastify/fastify-swagger#hide-a-route
|
|
1
|
+
import fp from "fastify-plugin";
|
|
2
|
+
import { slug } from "github-slugger";
|
|
3
|
+
import { getJavaScriptFile } from "./utils/getJavaScriptFile.js";
|
|
4
|
+
import { normalize, toJson, toYaml } from "@scalar/openapi-parser";
|
|
5
|
+
import { getHtmlDocument } from "@scalar/core/libs/html-rendering";
|
|
6
|
+
const RELATIVE_JAVASCRIPT_PATH = "js/scalar.js";
|
|
13
7
|
const schemaToHideRoute = {
|
|
14
|
-
|
|
8
|
+
hide: true
|
|
15
9
|
};
|
|
16
10
|
const getRoutePrefix = (routePrefix) => {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
return prefix.endsWith('/') ? prefix.slice(0, -1) : prefix;
|
|
11
|
+
const prefix = routePrefix ?? "/reference";
|
|
12
|
+
return prefix.endsWith("/") ? prefix.slice(0, -1) : prefix;
|
|
20
13
|
};
|
|
21
|
-
/**
|
|
22
|
-
* Get the endpoints for the OpenAPI specification.
|
|
23
|
-
*/
|
|
24
14
|
const getOpenApiDocumentEndpoints = (openApiDocumentEndpoints) => {
|
|
25
|
-
|
|
26
|
-
|
|
15
|
+
const { json = "/openapi.json", yaml = "/openapi.yaml" } = openApiDocumentEndpoints ?? {};
|
|
16
|
+
return { json, yaml };
|
|
27
17
|
};
|
|
28
|
-
|
|
29
|
-
* Get the URL for the Scalar JavaScript file.
|
|
30
|
-
*/
|
|
31
|
-
const getJavaScriptUrl = (routePrefix) => `${getRoutePrefix(routePrefix)}/${RELATIVE_JAVASCRIPT_PATH}`.replace(/\/\//g, '/');
|
|
32
|
-
/**
|
|
33
|
-
* The custom theme for Fastify
|
|
34
|
-
*/
|
|
18
|
+
const getJavaScriptUrl = (routePrefix) => `${getRoutePrefix(routePrefix)}/${RELATIVE_JAVASCRIPT_PATH}`.replace(/\/\//g, "/");
|
|
35
19
|
const customTheme = `
|
|
36
20
|
.light-mode {
|
|
37
21
|
color-scheme: light;
|
|
@@ -105,173 +89,159 @@ const customTheme = `
|
|
|
105
89
|
--scalar-color-purple: #b191f9;
|
|
106
90
|
}
|
|
107
91
|
`;
|
|
108
|
-
/**
|
|
109
|
-
* The default configuration for Fastify
|
|
110
|
-
*/
|
|
111
92
|
const DEFAULT_CONFIGURATION = {
|
|
112
|
-
|
|
93
|
+
_integration: "fastify"
|
|
113
94
|
};
|
|
114
|
-
const fastifyApiReference = fp(
|
|
95
|
+
const fastifyApiReference = fp(
|
|
96
|
+
async (fastify, options) => {
|
|
115
97
|
const { configuration: givenConfiguration } = options;
|
|
116
|
-
// Merge the defaults
|
|
117
98
|
let configuration = {
|
|
118
|
-
|
|
119
|
-
|
|
99
|
+
...DEFAULT_CONFIGURATION,
|
|
100
|
+
...givenConfiguration
|
|
120
101
|
};
|
|
121
102
|
const specSource = (() => {
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
103
|
+
const { content, url } = configuration ?? {};
|
|
104
|
+
if (content) {
|
|
105
|
+
return {
|
|
106
|
+
type: "content",
|
|
107
|
+
get: () => {
|
|
108
|
+
if (typeof content === "function") {
|
|
109
|
+
return content();
|
|
110
|
+
}
|
|
111
|
+
return content;
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
if (url) {
|
|
116
|
+
return {
|
|
117
|
+
type: "url",
|
|
118
|
+
get: () => url
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
if (fastify.hasPlugin("@fastify/swagger")) {
|
|
122
|
+
return {
|
|
123
|
+
type: "swagger",
|
|
124
|
+
// @ts-ignore We know that @fastify/swagger is loaded.
|
|
125
|
+
get: () => fastify.swagger()
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
return void 0;
|
|
148
129
|
})();
|
|
149
|
-
// If no OpenAPI specification is passed and @fastify/swagger isn’t loaded, show a warning.
|
|
150
130
|
if (!specSource) {
|
|
151
|
-
|
|
152
|
-
|
|
131
|
+
fastify.log.warn(
|
|
132
|
+
"[@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."
|
|
133
|
+
);
|
|
134
|
+
return;
|
|
153
135
|
}
|
|
154
|
-
// Read the JavaScript file once.
|
|
155
136
|
const fileContent = getJavaScriptFile();
|
|
156
137
|
const hooks = {};
|
|
157
138
|
if (options.hooks) {
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
}
|
|
139
|
+
const additionalHooks = ["onRequest", "preHandler"];
|
|
140
|
+
for (const hook of additionalHooks) {
|
|
141
|
+
if (options.hooks[hook]) {
|
|
142
|
+
hooks[hook] = options.hooks[hook];
|
|
163
143
|
}
|
|
144
|
+
}
|
|
164
145
|
}
|
|
165
146
|
const getSpecFilenameSlug = async (spec) => {
|
|
166
|
-
|
|
167
|
-
return slug(spec?.specification?.info?.title ?? 'spec');
|
|
147
|
+
return slug(spec?.specification?.info?.title ?? "spec");
|
|
168
148
|
};
|
|
169
149
|
const openApiSpecUrlJson = `${getRoutePrefix(options.routePrefix)}${getOpenApiDocumentEndpoints(options.openApiDocumentEndpoints).json}`;
|
|
170
150
|
fastify.route({
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
.header('Content-Disposition', `filename=${filename}.json`)
|
|
184
|
-
.header('Access-Control-Allow-Origin', '*')
|
|
185
|
-
.header('Access-Control-Allow-Methods', '*')
|
|
186
|
-
.send(json);
|
|
187
|
-
},
|
|
151
|
+
method: "GET",
|
|
152
|
+
url: openApiSpecUrlJson,
|
|
153
|
+
// @ts-ignore We don’t know whether @fastify/swagger is loaded.
|
|
154
|
+
schema: schemaToHideRoute,
|
|
155
|
+
...hooks,
|
|
156
|
+
...options.logLevel && { logLevel: options.logLevel },
|
|
157
|
+
async handler(_, reply) {
|
|
158
|
+
const spec = normalize(specSource.get());
|
|
159
|
+
const filename = await getSpecFilenameSlug(spec);
|
|
160
|
+
const json = JSON.parse(toJson(spec));
|
|
161
|
+
return reply.header("Content-Type", "application/json").header("Content-Disposition", `filename=${filename}.json`).header("Access-Control-Allow-Origin", "*").header("Access-Control-Allow-Methods", "*").send(json);
|
|
162
|
+
}
|
|
188
163
|
});
|
|
189
164
|
const openApiSpecUrlYaml = `${getRoutePrefix(options.routePrefix)}${getOpenApiDocumentEndpoints(options.openApiDocumentEndpoints).yaml}`;
|
|
190
165
|
fastify.route({
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
.header('Content-Disposition', `filename=${filename}.yaml`)
|
|
204
|
-
.header('Access-Control-Allow-Origin', '*')
|
|
205
|
-
.header('Access-Control-Allow-Methods', '*')
|
|
206
|
-
.send(yaml);
|
|
207
|
-
},
|
|
166
|
+
method: "GET",
|
|
167
|
+
url: openApiSpecUrlYaml,
|
|
168
|
+
// @ts-ignore We don’t know whether @fastify/swagger is loaded.
|
|
169
|
+
schema: schemaToHideRoute,
|
|
170
|
+
...hooks,
|
|
171
|
+
...options.logLevel && { logLevel: options.logLevel },
|
|
172
|
+
async handler(_, reply) {
|
|
173
|
+
const spec = normalize(specSource.get());
|
|
174
|
+
const filename = await getSpecFilenameSlug(spec);
|
|
175
|
+
const yaml = toYaml(spec);
|
|
176
|
+
return reply.header("Content-Type", "application/yaml").header("Content-Disposition", `filename=${filename}.yaml`).header("Access-Control-Allow-Origin", "*").header("Access-Control-Allow-Methods", "*").send(yaml);
|
|
177
|
+
}
|
|
208
178
|
});
|
|
209
|
-
// Redirect route without a trailing slash to force a trailing slash:
|
|
210
|
-
// We need this so the request to the JS file is relative.
|
|
211
|
-
// With ignoreTrailingSlash, fastify registeres both routes anyway.
|
|
212
179
|
const doesNotIgnoreTrailingSlash = fastify.initialConfig.ignoreTrailingSlash !== true;
|
|
213
180
|
if (doesNotIgnoreTrailingSlash && getRoutePrefix(options.routePrefix)) {
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
schema: schemaToHideRoute,
|
|
219
|
-
...hooks,
|
|
220
|
-
...(options.logLevel && { logLevel: options.logLevel }),
|
|
221
|
-
handler(_, reply) {
|
|
222
|
-
return reply.redirect(getRoutePrefix(options.routePrefix) + '/', 302);
|
|
223
|
-
},
|
|
224
|
-
});
|
|
225
|
-
}
|
|
226
|
-
// If no theme is passed, use the default theme.
|
|
227
|
-
fastify.route({
|
|
228
|
-
method: 'GET',
|
|
229
|
-
url: `${getRoutePrefix(options.routePrefix)}/`,
|
|
230
|
-
// We don’t know whether @fastify/swagger is registered, but it doesn’t hurt to add a schema anyway.
|
|
231
|
-
// @ts-ignore We don’t know whether @fastify/swagger is loaded.
|
|
181
|
+
fastify.route({
|
|
182
|
+
method: "GET",
|
|
183
|
+
url: getRoutePrefix(options.routePrefix),
|
|
184
|
+
// @ts-ignore We don't know whether @fastify/swagger is loaded.
|
|
232
185
|
schema: schemaToHideRoute,
|
|
233
186
|
...hooks,
|
|
234
|
-
...
|
|
187
|
+
...options.logLevel && { logLevel: options.logLevel },
|
|
235
188
|
handler(_, reply) {
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
}
|
|
189
|
+
return reply.redirect(getRoutePrefix(options.routePrefix) + "/", 302);
|
|
190
|
+
}
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
fastify.route({
|
|
194
|
+
method: "GET",
|
|
195
|
+
url: `${getRoutePrefix(options.routePrefix)}/`,
|
|
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
|
+
schema: schemaToHideRoute,
|
|
199
|
+
...hooks,
|
|
200
|
+
...options.logLevel && { logLevel: options.logLevel },
|
|
201
|
+
handler(_, reply) {
|
|
202
|
+
const currentUrl = new URL(_.url, `${_.protocol}://${_.hostname}`);
|
|
203
|
+
if (!currentUrl.pathname.endsWith("/")) {
|
|
204
|
+
return reply.redirect(`${currentUrl.pathname}/`, 301);
|
|
205
|
+
}
|
|
206
|
+
if (specSource.type !== "url") {
|
|
207
|
+
configuration = {
|
|
208
|
+
...configuration,
|
|
209
|
+
// Use a relative URL in case we're proxied
|
|
210
|
+
url: `.${getOpenApiDocumentEndpoints(options.openApiDocumentEndpoints).json}`
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
return reply.header("Content-Type", "text/html; charset=utf-8").send(
|
|
214
|
+
getHtmlDocument(
|
|
215
|
+
{
|
|
216
|
+
// We’re using the bundled JS here by default, but the user can pass a CDN URL.
|
|
217
|
+
cdn: RELATIVE_JAVASCRIPT_PATH,
|
|
218
|
+
...configuration
|
|
219
|
+
},
|
|
220
|
+
customTheme
|
|
221
|
+
)
|
|
222
|
+
);
|
|
223
|
+
}
|
|
260
224
|
});
|
|
261
225
|
fastify.route({
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
226
|
+
method: "GET",
|
|
227
|
+
url: getJavaScriptUrl(options.routePrefix),
|
|
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
|
+
schema: schemaToHideRoute,
|
|
231
|
+
...hooks,
|
|
232
|
+
...options.logLevel && { logLevel: options.logLevel },
|
|
233
|
+
handler(_, reply) {
|
|
234
|
+
return reply.header("Content-Type", "application/javascript; charset=utf-8").send(fileContent);
|
|
235
|
+
}
|
|
272
236
|
});
|
|
273
|
-
},
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
237
|
+
},
|
|
238
|
+
{
|
|
239
|
+
name: "@scalar/fastify-api-reference"
|
|
240
|
+
}
|
|
241
|
+
);
|
|
242
|
+
var fastifyApiReference_default = fastifyApiReference;
|
|
243
|
+
export {
|
|
244
|
+
customTheme,
|
|
245
|
+
fastifyApiReference_default as default
|
|
246
|
+
};
|
|
247
|
+
//# sourceMappingURL=fastifyApiReference.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 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;AAC1C,SAAS,uBAAuB;AAMhC,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,MAAM,KAAK;AACtB;AAKA,MAAM,mBAAmB,CAAC,gBACxB,GAAG,eAAe,WAAW,CAAC,IAAI,wBAAwB,GAAG,QAAQ,SAAS,GAAG;AAK5E,MAAM,cAAc;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA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