@scalar/client-side-rendering 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ # @scalar/client-side-rendering
2
+
3
+ ## 0.1.0
4
+
5
+ ### Minor Changes
6
+
7
+ - [#8431](https://github.com/scalar/scalar/pull/8431): feat: init :)
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023-present Scalar
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,35 @@
1
+ # Scalar Client-Side Rendering
2
+
3
+ [![Version](https://img.shields.io/npm/v/%40scalar/client-side-rendering)](https://www.npmjs.com/package/@scalar/client-side-rendering)
4
+ [![Downloads](https://img.shields.io/npm/dm/%40scalar/client-side-rendering)](https://www.npmjs.com/package/@scalar/client-side-rendering)
5
+ [![License](https://img.shields.io/npm/l/%40scalar%2Fclient-side-rendering)](https://www.npmjs.com/package/@scalar/client-side-rendering)
6
+ [![Discord](https://img.shields.io/discord/1135330207960678410?style=flat&color=5865F2)](https://discord.gg/scalar)
7
+
8
+ Render the Scalar API Reference as static HTML using the CDN. No server-side dependencies required.
9
+
10
+ ## Installation
11
+
12
+ ```bash
13
+ npm install @scalar/client-side-rendering
14
+ ```
15
+
16
+ ## Usage
17
+
18
+ ```ts
19
+ import { renderApiReference } from '@scalar/client-side-rendering'
20
+
21
+ const html = renderApiReference({
22
+ pageTitle: 'My API Reference',
23
+ config: {
24
+ url: 'https://registry.scalar.com/@scalar/apis/galaxy?format=json',
25
+ },
26
+ })
27
+ ```
28
+
29
+ ## Community
30
+
31
+ We are API nerds. You too? Let's chat on Discord: <https://discord.gg/scalar>
32
+
33
+ ## License
34
+
35
+ The source code in this repository is licensed under [MIT](https://github.com/scalar/scalar/blob/main/LICENSE).
@@ -0,0 +1,26 @@
1
+ import type { AnyApiReferenceConfiguration } from '@scalar/types/api-reference';
2
+ /**
3
+ * Render the Scalar API Reference as a complete HTML document using the CDN.
4
+ *
5
+ * Generates static HTML that loads the @scalar/api-reference standalone bundle
6
+ * from a CDN and renders client-side. No server-side dependencies required.
7
+ *
8
+ * For server-side rendering with hydration, use the server module instead.
9
+ */
10
+ export declare function renderApiReference(options: {
11
+ /** The API reference configuration. */
12
+ config: AnyApiReferenceConfiguration;
13
+ /** Page title. Defaults to "Scalar API Reference". */
14
+ pageTitle?: string;
15
+ /** CDN URL for the standalone bundle. Defaults to jsDelivr. */
16
+ cdn?: string;
17
+ }, customTheme?: string): string;
18
+ /**
19
+ * The script tags to load the @scalar/api-reference package from the CDN.
20
+ */
21
+ export declare function getScriptTags(configuration: Record<string, unknown>, cdn?: string): string;
22
+ /**
23
+ * The configuration to pass to the @scalar/api-reference package.
24
+ */
25
+ export declare const getConfiguration: (givenConfiguration: Record<string, unknown>) => Record<string, unknown>;
26
+ //# sourceMappingURL=html-rendering.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"html-rendering.d.ts","sourceRoot":"","sources":["../src/html-rendering.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,4BAA4B,EAAE,MAAM,6BAA6B,CAAA;AAwD/E;;;;;;;GAOG;AACH,wBAAgB,kBAAkB,CAChC,OAAO,EAAE;IACP,uCAAuC;IACvC,MAAM,EAAE,4BAA4B,CAAA;IACpC,sDAAsD;IACtD,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,+DAA+D;IAC/D,GAAG,CAAC,EAAE,MAAM,CAAA;CACb,EACD,WAAW,SAAK,GACf,MAAM,CA0BR;AASD;;GAEG;AACH,wBAAgB,aAAa,CAAC,aAAa,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,GAAG,CAAC,EAAE,MAAM,GAAG,MAAM,CAwC1F;AAED;;GAEG;AACH,eAAO,MAAM,gBAAgB,GAAI,oBAAoB,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAcpG,CAAA"}
@@ -0,0 +1,139 @@
1
+ const DEFAULT_CDN = 'https://cdn.jsdelivr.net/npm/@scalar/api-reference';
2
+ /**
3
+ * Escape HTML special characters in user-provided strings.
4
+ */
5
+ const escapeHtml = (str) => {
6
+ return str.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
7
+ };
8
+ /**
9
+ * Helper function to add consistent indentation to multiline strings
10
+ * @param str The string to indent
11
+ * @param spaces Number of spaces for each level
12
+ * @param initialIndent Whether to indent the first line
13
+ */
14
+ const addIndent = (str, spaces = 2, initialIndent = false) => {
15
+ const indent = ' '.repeat(spaces);
16
+ const lines = str.split('\n');
17
+ return lines
18
+ .map((line, index) => {
19
+ if (index === 0 && !initialIndent) {
20
+ return line;
21
+ }
22
+ return `${indent}${line}`;
23
+ })
24
+ .join('\n');
25
+ };
26
+ /**
27
+ * Generate the style tag with custom theme if needed
28
+ */
29
+ const getStyles = (configuration, customTheme) => {
30
+ const styles = [];
31
+ if (configuration.customCss) {
32
+ styles.push('/* Custom CSS */');
33
+ styles.push(configuration.customCss);
34
+ }
35
+ if (!configuration.theme && customTheme) {
36
+ styles.push('/* Custom Theme */');
37
+ styles.push(customTheme);
38
+ }
39
+ if (styles.length === 0) {
40
+ return '';
41
+ }
42
+ return `
43
+ <style type="text/css">
44
+ ${addIndent(styles.join('\n\n'), 6)}
45
+ </style>`;
46
+ };
47
+ /**
48
+ * Render the Scalar API Reference as a complete HTML document using the CDN.
49
+ *
50
+ * Generates static HTML that loads the @scalar/api-reference standalone bundle
51
+ * from a CDN and renders client-side. No server-side dependencies required.
52
+ *
53
+ * For server-side rendering with hydration, use the server module instead.
54
+ */
55
+ export function renderApiReference(options, customTheme = '') {
56
+ const { config: givenConfig, pageTitle, cdn } = options;
57
+ const title = escapeHtml(pageTitle ?? 'Scalar API Reference');
58
+ const unwrapped = Array.isArray(givenConfig) ? givenConfig[0] : givenConfig;
59
+ const { customCss, theme, ...rest } = (unwrapped ?? {});
60
+ const configuration = getConfiguration({
61
+ ...rest,
62
+ ...(theme ? { theme } : {}),
63
+ ...(customCss !== undefined ? { customCss } : {}),
64
+ });
65
+ return `<!doctype html>
66
+ <html>
67
+ <head>
68
+ <title>${title}</title>
69
+ <meta charset="utf-8" />
70
+ <meta
71
+ name="viewport"
72
+ content="width=device-width, initial-scale=1" />${getStyles(configuration, customTheme)}
73
+ </head>
74
+ <body>
75
+ <div id="app"></div>${getScriptTags(configuration, cdn)}
76
+ </body>
77
+ </html>`;
78
+ }
79
+ /**
80
+ * Helper function to serialize arrays that may contain functions
81
+ */
82
+ const serializeArrayWithFunctions = (arr) => {
83
+ return `[${arr.map((item) => (typeof item === 'function' ? item.toString() : JSON.stringify(item))).join(', ')}]`;
84
+ };
85
+ /**
86
+ * The script tags to load the @scalar/api-reference package from the CDN.
87
+ */
88
+ export function getScriptTags(configuration, cdn) {
89
+ const restConfig = { ...configuration };
90
+ const functionProps = [];
91
+ for (const [key, value] of Object.entries(configuration)) {
92
+ if (typeof value === 'function') {
93
+ functionProps.push(`"${key}": ${value.toString()}`);
94
+ delete restConfig[key];
95
+ }
96
+ else if (Array.isArray(value) && value.some((item) => typeof item === 'function')) {
97
+ functionProps.push(`"${key}": ${serializeArrayWithFunctions(value)}`);
98
+ delete restConfig[key];
99
+ }
100
+ }
101
+ const jsonString = JSON.stringify(restConfig, null, 2);
102
+ const indentedJsonString = jsonString
103
+ .split('\n')
104
+ .map((line, index) => (index === 0 ? line : ` ${line}`))
105
+ .join('\n');
106
+ let configString = indentedJsonString;
107
+ if (functionProps.length > 0) {
108
+ if (jsonString === '{}') {
109
+ configString = `{\n ${functionProps.join(',\n ')}\n }`;
110
+ }
111
+ else {
112
+ const jsonWithoutClosingBrace = indentedJsonString.split('\n').slice(0, -1).join('\n');
113
+ configString = `${jsonWithoutClosingBrace},\n ${functionProps.join(',\n ')}\n }`;
114
+ }
115
+ }
116
+ return `
117
+ <!-- Load the Script -->
118
+ <script src="${cdn ?? DEFAULT_CDN}"></script>
119
+
120
+ <!-- Initialize the Scalar API Reference -->
121
+ <script type="text/javascript">
122
+ Scalar.createApiReference('#app', ${configString})
123
+ </script>`;
124
+ }
125
+ /**
126
+ * The configuration to pass to the @scalar/api-reference package.
127
+ */
128
+ export const getConfiguration = (givenConfiguration) => {
129
+ const configuration = { ...givenConfiguration };
130
+ // Execute content if it's a function
131
+ if (typeof configuration.content === 'function') {
132
+ configuration.content = configuration.content();
133
+ }
134
+ // Only remove content if url is provided
135
+ if (configuration.content && configuration.url) {
136
+ delete configuration.content;
137
+ }
138
+ return configuration;
139
+ };
@@ -0,0 +1,2 @@
1
+ export { getConfiguration, getScriptTags, renderApiReference } from './html-rendering.js';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,aAAa,EAAE,kBAAkB,EAAE,MAAM,kBAAkB,CAAA"}
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ export { getConfiguration, getScriptTags, renderApiReference } from './html-rendering.js';
package/package.json ADDED
@@ -0,0 +1,41 @@
1
+ {
2
+ "name": "@scalar/client-side-rendering",
3
+ "description": "Client-side rendering for the Scalar API Reference using the CDN",
4
+ "license": "MIT",
5
+ "author": "Scalar (https://github.com/scalar)",
6
+ "homepage": "https://github.com/scalar/scalar",
7
+ "bugs": "https://github.com/scalar/scalar/issues/new/choose",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "git+https://github.com/scalar/scalar.git",
11
+ "directory": "packages/client-side-rendering"
12
+ },
13
+ "keywords": [],
14
+ "version": "0.1.0",
15
+ "engines": {
16
+ "node": ">=22"
17
+ },
18
+ "type": "module",
19
+ "exports": {
20
+ ".": {
21
+ "import": "./dist/index.js",
22
+ "types": "./dist/index.d.ts",
23
+ "default": "./dist/index.js"
24
+ }
25
+ },
26
+ "files": [
27
+ "dist",
28
+ "CHANGELOG.md"
29
+ ],
30
+ "dependencies": {
31
+ "@scalar/types": "0.8.0"
32
+ },
33
+ "devDependencies": {
34
+ "vite": "8.0.0"
35
+ },
36
+ "scripts": {
37
+ "build": "tsc -p tsconfig.build.json && tsc-alias -p tsconfig.build.json",
38
+ "test": "vitest",
39
+ "types:check": "tsc --noEmit"
40
+ }
41
+ }