@scalar/fastify-api-reference 1.28.1 → 1.28.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.
@@ -0,0 +1,129 @@
1
+ /**
2
+ * Helper function to add consistent indentation to multiline strings
3
+ * @param str The string to indent
4
+ * @param spaces Number of spaces for each level
5
+ * @param initialIndent Whether to indent the first line
6
+ */
7
+ const addIndent = (str, spaces = 2, initialIndent = false) => {
8
+ const indent = ' '.repeat(spaces);
9
+ const lines = str.split('\n');
10
+ return lines
11
+ .map((line, index) => {
12
+ if (index === 0 && !initialIndent)
13
+ return line;
14
+ return `${indent}${line}`;
15
+ })
16
+ .join('\n');
17
+ };
18
+ /**
19
+ * Generate the style tag with custom theme if needed
20
+ */
21
+ const getStyles = (configuration, customTheme) => {
22
+ const styles = [];
23
+ if (configuration.customCss) {
24
+ styles.push('/* Custom CSS */');
25
+ styles.push(configuration.customCss);
26
+ }
27
+ if (!configuration.theme && customTheme) {
28
+ styles.push('/* Custom Theme */');
29
+ styles.push(customTheme);
30
+ }
31
+ if (styles.length === 0) {
32
+ return '';
33
+ }
34
+ return `
35
+ <style type="text/css">
36
+ ${addIndent(styles.join('\n\n'), 6)}
37
+ </style>`;
38
+ };
39
+ /**
40
+ * The HTML document to render the Scalar API reference.
41
+ *
42
+ * We must check the passed in configuration and not the configuration for the theme as the configuration will have it
43
+ * defaulted to 'default'
44
+ */
45
+ const getHtmlDocument = (givenConfiguration, customTheme = '') => {
46
+ const { cdn, pageTitle, customCss, theme, ...rest } = givenConfiguration;
47
+ const configuration = getConfiguration({
48
+ ...rest,
49
+ ...(theme ? { theme } : {}),
50
+ customCss,
51
+ });
52
+ const content = `<!doctype html>
53
+ <html>
54
+ <head>
55
+ <title>${pageTitle ?? 'Scalar API Reference'}</title>
56
+ <meta charset="utf-8" />
57
+ <meta
58
+ name="viewport"
59
+ content="width=device-width, initial-scale=1" />${getStyles(configuration, customTheme)}
60
+ </head>
61
+ <body>
62
+ <div id="app"></div>${getScriptTags(configuration, cdn)}
63
+ </body>
64
+ </html>`;
65
+ return content;
66
+ };
67
+ /**
68
+ * The script tags to load the @scalar/api-reference package from the CDN.
69
+ */
70
+ function getScriptTags(configuration, cdn) {
71
+ // Extract function properties before stringifying
72
+ const { tagsSorter, operationsSorter, generateHeadingSlug, generateModelSlug, generateTagSlug, generateOperationSlug, generateWebhookSlug, onLoaded, redirect, onSpecUpdate, onServerChange, ...restConfig } = configuration;
73
+ // Create the function strings if they exist
74
+ const functionProps = [];
75
+ const functionProperties = [
76
+ { name: 'tagsSorter', value: tagsSorter },
77
+ { name: 'operationsSorter', value: operationsSorter },
78
+ { name: 'generateHeadingSlug', value: generateHeadingSlug },
79
+ { name: 'generateModelSlug', value: generateModelSlug },
80
+ { name: 'generateTagSlug', value: generateTagSlug },
81
+ { name: 'generateOperationSlug', value: generateOperationSlug },
82
+ { name: 'generateWebhookSlug', value: generateWebhookSlug },
83
+ { name: 'onLoaded', value: onLoaded },
84
+ { name: 'redirect', value: redirect },
85
+ { name: 'onSpecUpdate', value: onSpecUpdate },
86
+ { name: 'onServerChange', value: onServerChange },
87
+ ];
88
+ functionProperties.forEach(({ name, value }) => {
89
+ if (value && typeof value === 'function') {
90
+ functionProps.push(`"${name}": ${value.toString()}`);
91
+ }
92
+ });
93
+ // Stringify the rest of the configuration
94
+ const configString = JSON.stringify(restConfig, null, 2)
95
+ .split('\n')
96
+ .map((line, index) => (index === 0 ? line : ' ' + line))
97
+ .join('\n')
98
+ .replace(/\s*}$/, ''); // Remove the closing brace and any whitespace before it
99
+ const functionPropsString = functionProps.length ? `,\n ${functionProps.join(',\n ')}\n }` : '}';
100
+ return `
101
+ <!-- Load the Script -->
102
+ <script src="${cdn ?? 'https://cdn.jsdelivr.net/npm/@scalar/api-reference'}"></script>
103
+
104
+ <!-- Initialize the Scalar API Reference -->
105
+ <script type="text/javascript">
106
+ Scalar.createApiReference('#app', ${configString}${functionPropsString})
107
+ </script>`;
108
+ }
109
+ /**
110
+ * The configuration to pass to the @scalar/api-reference package.
111
+ */
112
+ const getConfiguration = (givenConfiguration) => {
113
+ // Clone the given configuration
114
+ const configuration = {
115
+ ...givenConfiguration,
116
+ };
117
+ // Execute content if it's a function
118
+ if (typeof configuration.content === 'function') {
119
+ configuration.content = configuration.content();
120
+ }
121
+ // Only remove content if url is provided
122
+ if (configuration.content && configuration.url) {
123
+ delete configuration.content;
124
+ }
125
+ // Just return regular JSON string, no HTML escaping needed
126
+ return configuration;
127
+ };
128
+
129
+ export { getConfiguration, getHtmlDocument, getScriptTags };
package/package.json CHANGED
@@ -17,7 +17,7 @@
17
17
  "openapi",
18
18
  "swagger"
19
19
  ],
20
- "version": "1.28.1",
20
+ "version": "1.28.3",
21
21
  "engines": {
22
22
  "node": ">=18"
23
23
  },
@@ -39,10 +39,10 @@
39
39
  "dependencies": {
40
40
  "fastify-plugin": "^4.5.1",
41
41
  "github-slugger": "^2.0.0",
42
- "@scalar/types": "0.1.0",
43
- "@scalar/openapi-types": "0.1.9",
44
- "@scalar/core": "0.1.3",
45
- "@scalar/openapi-parser": "0.10.10"
42
+ "@scalar/core": "0.2.1",
43
+ "@scalar/types": "0.1.1",
44
+ "@scalar/openapi-parser": "0.10.10",
45
+ "@scalar/openapi-types": "0.1.9"
46
46
  },
47
47
  "devDependencies": {
48
48
  "@fastify/basic-auth": "^5.1.1",
@@ -56,8 +56,8 @@
56
56
  "vite-plugin-static-copy": "^1.0.2",
57
57
  "vitest": "^1.6.0",
58
58
  "yaml": "^2.4.5",
59
- "@scalar/build-tooling": "0.1.17",
60
- "@scalar/api-reference": "1.28.1"
59
+ "@scalar/api-reference": "1.28.3",
60
+ "@scalar/build-tooling": "0.1.17"
61
61
  },
62
62
  "scripts": {
63
63
  "build": "scalar-build-rollup && pnpm copy:standalone",
@@ -1,66 +0,0 @@
1
- import { htmlRenderingConfigurationSchema, apiReferenceConfigurationSchema } from '@scalar/types/api-reference';
2
-
3
- /**
4
- * The HTML document to render the Scalar API reference.
5
- *
6
- * We must check the passed in configuration and not the parsedConfig for the theme as the parsedConfig will have it
7
- * defaulted to 'default'
8
- */
9
- const getHtmlDocument = (configuration, customTheme = '') => {
10
- const { cdn, pageTitle, ...rest } = configuration;
11
- const parsedHtmlOptions = htmlRenderingConfigurationSchema.parse({ cdn, pageTitle, customTheme });
12
- const parsedConfig = apiReferenceConfigurationSchema.parse(rest);
13
- return `
14
- <!DOCTYPE html>
15
- <html>
16
- <head>
17
- <title>${parsedHtmlOptions.pageTitle}</title>
18
- <meta charset="utf-8" />
19
- <meta
20
- name="viewport"
21
- content="width=device-width, initial-scale=1" />
22
- <style>
23
- ${configuration.theme ? '' : customTheme}
24
- </style>
25
- </head>
26
- <body>
27
- ${getScriptTags(parsedConfig, parsedHtmlOptions.cdn)}
28
- </body>
29
- </html>
30
- `;
31
- };
32
- /**
33
- * The script tags to load the @scalar/api-reference package from the CDN.
34
- */
35
- function getScriptTags(configuration, cdn) {
36
- return `
37
- <script
38
- id="api-reference"
39
- type="application/json"
40
- data-configuration="${getConfiguration(configuration)}">${getScriptTagContent(configuration)}</script>
41
- <script src="${cdn}"></script>
42
- `;
43
- }
44
- /**
45
- * The configuration to pass to the @scalar/api-reference package.
46
- */
47
- const getConfiguration = (givenConfiguration) => {
48
- // Clone before mutating
49
- const configuration = {
50
- ...givenConfiguration,
51
- };
52
- if (configuration.content) {
53
- delete configuration.content;
54
- }
55
- return JSON.stringify(configuration).split('"').join('&quot;');
56
- };
57
- /**
58
- * The content to pass to the @scalar/api-reference package as the <script> tag content.
59
- */
60
- const getScriptTagContent = (configuration) => configuration.content
61
- ? typeof configuration.content === 'function'
62
- ? JSON.stringify(configuration.content())
63
- : JSON.stringify(configuration.content)
64
- : '';
65
-
66
- export { getConfiguration, getHtmlDocument, getScriptTagContent, getScriptTags };