@scalar/client-side-rendering 0.2.6 → 0.3.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 +6 -0
- package/dist/html-rendering.d.ts +12 -0
- package/dist/html-rendering.d.ts.map +1 -1
- package/dist/html-rendering.js +25 -13
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/package.json +3 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
# @scalar/client-side-rendering
|
|
2
2
|
|
|
3
|
+
## 0.3.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- [#9588](https://github.com/scalar/scalar/pull/9588): Fix function-valued configuration options (like `onBeforeRequest` and the request hooks) being dropped in the Docusaurus integration. Docusaurus JSON-serializes route props, which silently strips functions, so the config is now serialized to JavaScript in the plugin and injected as a script. Exposes a reusable `serializeConfigToJs` helper from `@scalar/client-side-rendering`.
|
|
8
|
+
|
|
3
9
|
## 0.2.6
|
|
4
10
|
|
|
5
11
|
## 0.2.5
|
package/dist/html-rendering.d.ts
CHANGED
|
@@ -30,6 +30,18 @@ export declare function renderApiReference(options: {
|
|
|
30
30
|
*/
|
|
31
31
|
nonce?: string;
|
|
32
32
|
}, customTheme?: string): string;
|
|
33
|
+
/**
|
|
34
|
+
* Serialize a configuration object to a JavaScript object literal string.
|
|
35
|
+
*
|
|
36
|
+
* Unlike `JSON.stringify`, this preserves function-valued properties (for example `onBeforeRequest`
|
|
37
|
+
* or the request hooks) by emitting them as literal JavaScript source via `Function.prototype.toString()`.
|
|
38
|
+
* That is what lets callbacks survive being written into an inline `<script>` tag, or across any other
|
|
39
|
+
* boundary that would otherwise JSON-serialize the configuration and silently drop functions.
|
|
40
|
+
*
|
|
41
|
+
* Note: functions must be arrow functions or `function` expressions. Object method shorthand
|
|
42
|
+
* (`onBeforeRequest(request) {}`) does not serialize to a valid standalone expression.
|
|
43
|
+
*/
|
|
44
|
+
export declare function serializeConfigToJs(configuration: Record<string, unknown>): string;
|
|
33
45
|
/**
|
|
34
46
|
* The script tags to load the @scalar/api-reference package from the CDN.
|
|
35
47
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"html-rendering.d.ts","sourceRoot":"","sources":["../src/html-rendering.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,4BAA4B,EAAE,0BAA0B,EAAE,MAAM,6BAA6B,CAAA;AAE3G,YAAY,EAAE,4BAA4B,EAAE,0BAA0B,EAAE,CAAA;AAExE,uEAAuE;AACvE,eAAO,MAAM,WAAW,uDAAuD,CAAA;AAoE/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;IACZ;;;;;;;;;;OAUG;IACH,KAAK,CAAC,EAAE,MAAM,CAAA;CACf,EACD,WAAW,SAAK,GACf,MAAM,CA8BR;AASD;;;;;GAKG;AACH,wBAAgB,aAAa,CAAC,aAAa,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,GAAG,CAAC,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,MAAM,GAAG,MAAM,
|
|
1
|
+
{"version":3,"file":"html-rendering.d.ts","sourceRoot":"","sources":["../src/html-rendering.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,4BAA4B,EAAE,0BAA0B,EAAE,MAAM,6BAA6B,CAAA;AAE3G,YAAY,EAAE,4BAA4B,EAAE,0BAA0B,EAAE,CAAA;AAExE,uEAAuE;AACvE,eAAO,MAAM,WAAW,uDAAuD,CAAA;AAoE/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;IACZ;;;;;;;;;;OAUG;IACH,KAAK,CAAC,EAAE,MAAM,CAAA;CACf,EACD,WAAW,SAAK,GACf,MAAM,CA8BR;AASD;;;;;;;;;;GAUG;AACH,wBAAgB,mBAAmB,CAAC,aAAa,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,CA+BlF;AAED;;;;;GAKG;AACH,wBAAgB,aAAa,CAAC,aAAa,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,GAAG,CAAC,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,MAAM,GAAG,MAAM,CAa1G;AAED;;GAEG;AACH,eAAO,MAAM,gBAAgB,GAC3B,oBAAoB,OAAO,CAAC,0BAA0B,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAChF,MAAM,CAAC,MAAM,EAAE,OAAO,CAcxB,CAAA"}
|
package/dist/html-rendering.js
CHANGED
|
@@ -99,12 +99,17 @@ const serializeArrayWithFunctions = (arr) => {
|
|
|
99
99
|
return `[${arr.map((item) => (typeof item === 'function' ? item.toString() : JSON.stringify(item))).join(', ')}]`;
|
|
100
100
|
};
|
|
101
101
|
/**
|
|
102
|
-
*
|
|
102
|
+
* Serialize a configuration object to a JavaScript object literal string.
|
|
103
103
|
*
|
|
104
|
-
*
|
|
105
|
-
*
|
|
104
|
+
* Unlike `JSON.stringify`, this preserves function-valued properties (for example `onBeforeRequest`
|
|
105
|
+
* or the request hooks) by emitting them as literal JavaScript source via `Function.prototype.toString()`.
|
|
106
|
+
* That is what lets callbacks survive being written into an inline `<script>` tag, or across any other
|
|
107
|
+
* boundary that would otherwise JSON-serialize the configuration and silently drop functions.
|
|
108
|
+
*
|
|
109
|
+
* Note: functions must be arrow functions or `function` expressions. Object method shorthand
|
|
110
|
+
* (`onBeforeRequest(request) {}`) does not serialize to a valid standalone expression.
|
|
106
111
|
*/
|
|
107
|
-
export function
|
|
112
|
+
export function serializeConfigToJs(configuration) {
|
|
108
113
|
const restConfig = { ...configuration };
|
|
109
114
|
const functionProps = [];
|
|
110
115
|
for (const [key, value] of Object.entries(configuration)) {
|
|
@@ -122,16 +127,23 @@ export function getScriptTags(configuration, cdn, nonce) {
|
|
|
122
127
|
.split('\n')
|
|
123
128
|
.map((line, index) => (index === 0 ? line : ` ${line}`))
|
|
124
129
|
.join('\n');
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
}
|
|
130
|
-
else {
|
|
131
|
-
const jsonWithoutClosingBrace = indentedJsonString.split('\n').slice(0, -1).join('\n');
|
|
132
|
-
configString = `${jsonWithoutClosingBrace},\n ${functionProps.join(',\n ')}\n }`;
|
|
133
|
-
}
|
|
130
|
+
if (functionProps.length === 0) {
|
|
131
|
+
return indentedJsonString;
|
|
132
|
+
}
|
|
133
|
+
if (jsonString === '{}') {
|
|
134
|
+
return `{\n ${functionProps.join(',\n ')}\n }`;
|
|
134
135
|
}
|
|
136
|
+
const jsonWithoutClosingBrace = indentedJsonString.split('\n').slice(0, -1).join('\n');
|
|
137
|
+
return `${jsonWithoutClosingBrace},\n ${functionProps.join(',\n ')}\n }`;
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* The script tags to load the @scalar/api-reference package from the CDN.
|
|
141
|
+
*
|
|
142
|
+
* When a `nonce` is provided it is applied to both script tags so they are allowed under a strict
|
|
143
|
+
* `script-src` Content Security Policy.
|
|
144
|
+
*/
|
|
145
|
+
export function getScriptTags(configuration, cdn, nonce) {
|
|
146
|
+
const configString = serializeConfigToJs(configuration);
|
|
135
147
|
const nonceAttr = nonceAttribute(nonce);
|
|
136
148
|
return `
|
|
137
149
|
<!-- Load the Script -->
|
package/dist/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { type AnyApiReferenceConfiguration, DEFAULT_CDN, type HtmlRenderingConfiguration, getConfiguration, getScriptTags, renderApiReference, } from './html-rendering.js';
|
|
1
|
+
export { type AnyApiReferenceConfiguration, DEFAULT_CDN, type HtmlRenderingConfiguration, getConfiguration, getScriptTags, renderApiReference, serializeConfigToJs, } from './html-rendering.js';
|
|
2
2
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,4BAA4B,EACjC,WAAW,EACX,KAAK,0BAA0B,EAC/B,gBAAgB,EAChB,aAAa,EACb,kBAAkB,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,4BAA4B,EACjC,WAAW,EACX,KAAK,0BAA0B,EAC/B,gBAAgB,EAChB,aAAa,EACb,kBAAkB,EAClB,mBAAmB,GACpB,MAAM,kBAAkB,CAAA"}
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { DEFAULT_CDN, getConfiguration, getScriptTags, renderApiReference, } from './html-rendering.js';
|
|
1
|
+
export { DEFAULT_CDN, getConfiguration, getScriptTags, renderApiReference, serializeConfigToJs, } from './html-rendering.js';
|
package/package.json
CHANGED
|
@@ -11,11 +11,12 @@
|
|
|
11
11
|
"directory": "packages/client-side-rendering"
|
|
12
12
|
},
|
|
13
13
|
"keywords": [],
|
|
14
|
-
"version": "0.
|
|
14
|
+
"version": "0.3.0",
|
|
15
15
|
"engines": {
|
|
16
16
|
"node": ">=22"
|
|
17
17
|
},
|
|
18
18
|
"type": "module",
|
|
19
|
+
"types": "./dist/index.d.ts",
|
|
19
20
|
"exports": {
|
|
20
21
|
".": {
|
|
21
22
|
"import": "./dist/index.js",
|
|
@@ -28,8 +29,8 @@
|
|
|
28
29
|
"CHANGELOG.md"
|
|
29
30
|
],
|
|
30
31
|
"dependencies": {
|
|
31
|
-
"@scalar/schemas": "0.7.0",
|
|
32
32
|
"@scalar/validation": "0.6.0",
|
|
33
|
+
"@scalar/schemas": "0.7.0",
|
|
33
34
|
"@scalar/types": "0.16.0"
|
|
34
35
|
},
|
|
35
36
|
"devDependencies": {
|