@scalar/import 0.3.22 → 0.3.24
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 +17 -0
- package/dist/get-configuration-attribute.test.js +33 -0
- package/dist/get-configuration-attribute.test.js.map +7 -0
- package/dist/get-content-of-script-tag.test.js +33 -0
- package/dist/get-content-of-script-tag.test.js.map +7 -0
- package/dist/index.js +5 -1
- package/dist/index.js.map +7 -0
- package/dist/resolve.js +208 -290
- package/dist/resolve.js.map +7 -0
- package/dist/resolve.test.js +1050 -0
- package/dist/resolve.test.js.map +7 -0
- package/package.json +5 -7
package/dist/resolve.js
CHANGED
|
@@ -1,316 +1,234 @@
|
|
|
1
|
-
import { makeUrlAbsolute } from
|
|
2
|
-
import { parse } from
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Find an OpenAPI document URL in the HTML of @scalar/api-reference and other places.
|
|
6
|
-
* This is useful to open the OpenAPI document from basically any source.
|
|
7
|
-
*/
|
|
1
|
+
import { makeUrlAbsolute } from "@scalar/oas-utils/helpers";
|
|
2
|
+
import { parse } from "yaml";
|
|
8
3
|
async function resolve(value, options) {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
4
|
+
if (value?.startsWith("http://") || value?.startsWith("https://")) {
|
|
5
|
+
const githubRawUrl = transformGitHubUrl(value);
|
|
6
|
+
if (githubRawUrl) {
|
|
7
|
+
return githubRawUrl;
|
|
8
|
+
}
|
|
9
|
+
if (value?.toLowerCase().endsWith(".json")) {
|
|
10
|
+
return value;
|
|
11
|
+
}
|
|
12
|
+
if (value?.toLowerCase().endsWith(".yaml") || value?.toLowerCase().endsWith(".yml")) {
|
|
13
|
+
return value;
|
|
14
|
+
}
|
|
15
|
+
const sandboxUrl = value.match(/https:\/\/sandbox\.scalar\.com\/(p|e)\/([a-zA-Z0-9]+)/);
|
|
16
|
+
if (sandboxUrl?.[2]) {
|
|
17
|
+
return `https://sandbox.scalar.com/files/${sandboxUrl[2]}/openapi.yaml`;
|
|
18
|
+
}
|
|
19
|
+
try {
|
|
20
|
+
let result = await (options?.fetch ? options.fetch(value) : fetch(value, {
|
|
21
|
+
cache: "no-cache"
|
|
22
|
+
}));
|
|
23
|
+
if (!result.ok && options?.fetch) {
|
|
24
|
+
result = await fetch(value, {
|
|
25
|
+
cache: "no-cache"
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
if (result.ok) {
|
|
29
|
+
const content = await result.text();
|
|
30
|
+
const forwardedHost = result.headers.get("X-Forwarded-Host");
|
|
31
|
+
try {
|
|
32
|
+
const jsonContent = JSON.parse(content);
|
|
33
|
+
if (jsonContent.openapi || jsonContent.swagger) {
|
|
18
34
|
return value;
|
|
35
|
+
}
|
|
36
|
+
} catch {
|
|
37
|
+
try {
|
|
38
|
+
const yamlContent = parse(content);
|
|
39
|
+
if (yamlContent?.openapi || yamlContent?.swagger) {
|
|
40
|
+
return value;
|
|
41
|
+
}
|
|
42
|
+
} catch {
|
|
43
|
+
}
|
|
19
44
|
}
|
|
20
|
-
|
|
21
|
-
if (
|
|
22
|
-
|
|
45
|
+
const urlOrPathOrDocument = parseHtml(content);
|
|
46
|
+
if (typeof urlOrPathOrDocument === "string") {
|
|
47
|
+
try {
|
|
48
|
+
return JSON.parse(urlOrPathOrDocument);
|
|
49
|
+
} catch {
|
|
50
|
+
try {
|
|
51
|
+
const yaml = parse(urlOrPathOrDocument);
|
|
52
|
+
if (typeof yaml === "object") {
|
|
53
|
+
return yaml;
|
|
54
|
+
}
|
|
55
|
+
} catch {
|
|
56
|
+
}
|
|
57
|
+
}
|
|
23
58
|
}
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
if (sandboxUrl?.[2]) {
|
|
27
|
-
return `https://sandbox.scalar.com/files/${sandboxUrl[2]}/openapi.yaml`;
|
|
59
|
+
if (typeof urlOrPathOrDocument === "object") {
|
|
60
|
+
return urlOrPathOrDocument;
|
|
28
61
|
}
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
let result = await (options?.fetch
|
|
32
|
-
? options.fetch(value)
|
|
33
|
-
: fetch(value, {
|
|
34
|
-
cache: 'no-cache',
|
|
35
|
-
}));
|
|
36
|
-
// If the custom fetch failed, try again with regular fetch
|
|
37
|
-
if (!result.ok && options?.fetch) {
|
|
38
|
-
result = await fetch(value, {
|
|
39
|
-
cache: 'no-cache',
|
|
40
|
-
});
|
|
41
|
-
}
|
|
42
|
-
if (result.ok) {
|
|
43
|
-
const content = await result.text();
|
|
44
|
-
const forwardedHost = result.headers.get('X-Forwarded-Host');
|
|
45
|
-
// Check if content is directly JSON/YAML
|
|
46
|
-
try {
|
|
47
|
-
// Try parsing as JSON
|
|
48
|
-
const jsonContent = JSON.parse(content);
|
|
49
|
-
if (jsonContent.openapi || jsonContent.swagger) {
|
|
50
|
-
return value;
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
catch {
|
|
54
|
-
// Try parsing as YAML
|
|
55
|
-
try {
|
|
56
|
-
const yamlContent = parse(content);
|
|
57
|
-
if (yamlContent?.openapi || yamlContent?.swagger) {
|
|
58
|
-
return value;
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
catch {
|
|
62
|
-
// Not YAML
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
const urlOrPathOrDocument = parseHtml(content);
|
|
66
|
-
// Document (string)
|
|
67
|
-
if (typeof urlOrPathOrDocument === 'string') {
|
|
68
|
-
try {
|
|
69
|
-
// JSON?
|
|
70
|
-
return JSON.parse(urlOrPathOrDocument);
|
|
71
|
-
}
|
|
72
|
-
catch {
|
|
73
|
-
// No JSON
|
|
74
|
-
try {
|
|
75
|
-
// YAML?
|
|
76
|
-
const yaml = parse(urlOrPathOrDocument);
|
|
77
|
-
if (typeof yaml === 'object') {
|
|
78
|
-
return yaml;
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
catch {
|
|
82
|
-
// Not YAML
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
// Document (object)
|
|
87
|
-
if (typeof urlOrPathOrDocument === 'object') {
|
|
88
|
-
return urlOrPathOrDocument;
|
|
89
|
-
}
|
|
90
|
-
// Relative or absolute URL
|
|
91
|
-
if (urlOrPathOrDocument) {
|
|
92
|
-
return makeUrlAbsolute(urlOrPathOrDocument, forwardedHost || value);
|
|
93
|
-
}
|
|
94
|
-
// Check for configuration attribute URL
|
|
95
|
-
const configUrl = getConfigurationAttributeUrl(content);
|
|
96
|
-
if (configUrl) {
|
|
97
|
-
return makeUrlAbsolute(configUrl, forwardedHost || value);
|
|
98
|
-
}
|
|
99
|
-
// Check for embedded OpenAPI document
|
|
100
|
-
const embeddedSpec = parseEmbeddedOpenApi(content);
|
|
101
|
-
if (embeddedSpec) {
|
|
102
|
-
return embeddedSpec;
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
else {
|
|
106
|
-
console.warn(`[@scalar/import] Failed to fetch ${value}`);
|
|
107
|
-
}
|
|
62
|
+
if (urlOrPathOrDocument) {
|
|
63
|
+
return makeUrlAbsolute(urlOrPathOrDocument, forwardedHost || value);
|
|
108
64
|
}
|
|
109
|
-
|
|
110
|
-
|
|
65
|
+
const configUrl = getConfigurationAttributeUrl(content);
|
|
66
|
+
if (configUrl) {
|
|
67
|
+
return makeUrlAbsolute(configUrl, forwardedHost || value);
|
|
111
68
|
}
|
|
112
|
-
|
|
113
|
-
|
|
69
|
+
const embeddedSpec = parseEmbeddedOpenApi(content);
|
|
70
|
+
if (embeddedSpec) {
|
|
71
|
+
return embeddedSpec;
|
|
72
|
+
}
|
|
73
|
+
} else {
|
|
74
|
+
console.warn(`[@scalar/import] Failed to fetch ${value}`);
|
|
75
|
+
}
|
|
76
|
+
} catch (error) {
|
|
77
|
+
console.error(`[@scalar/import] Failed to fetch ${value}`, error);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return void 0;
|
|
114
81
|
}
|
|
115
|
-
/**
|
|
116
|
-
* Go through the HTML and try to find the OpenAPI document URL
|
|
117
|
-
*/
|
|
118
82
|
function parseHtml(html) {
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
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
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
return scriptConfigMatch?.[1];
|
|
160
|
-
}
|
|
161
|
-
// Check for OpenAPI URLs in the HTML
|
|
162
|
-
const linkMatch = html.match(/<a[^>]*href=["']([^"']+\.(?:yaml|yml|json))["'][^>]*>/i);
|
|
163
|
-
if (linkMatch?.[1]) {
|
|
164
|
-
return linkMatch[1];
|
|
165
|
-
}
|
|
166
|
-
// Check for URLs in escaped JS objects
|
|
167
|
-
const escapedJsonMatch = html.match(/\\"spec\\":\{.*?\\"url\\":\\"([^"\\]+)\\"/);
|
|
168
|
-
if (escapedJsonMatch?.[1]) {
|
|
169
|
-
return escapedJsonMatch[1];
|
|
170
|
-
}
|
|
171
|
-
return undefined;
|
|
83
|
+
if (!html?.includes("<")) {
|
|
84
|
+
return void 0;
|
|
85
|
+
}
|
|
86
|
+
const dataUrlMatch = html.match(/id=["']api-reference["'][\s\S]*?data-url=["']([^"']+)["']/)?.slice(1).find(Boolean);
|
|
87
|
+
if (dataUrlMatch) {
|
|
88
|
+
return dataUrlMatch;
|
|
89
|
+
}
|
|
90
|
+
const specUrlMatch = html.match(/spec-url=["']([^"']+)["']/);
|
|
91
|
+
if (specUrlMatch?.[1]) {
|
|
92
|
+
return specUrlMatch[1];
|
|
93
|
+
}
|
|
94
|
+
const redocInit = html.match(/Redoc.init\(["']([^"']+)["']/);
|
|
95
|
+
if (redocInit?.[1]) {
|
|
96
|
+
return redocInit[1];
|
|
97
|
+
}
|
|
98
|
+
const doubleEncodedConfigurationUrl = html.match(/&quot;url&quot;:&quot;([^;]+)&quot;/);
|
|
99
|
+
if (doubleEncodedConfigurationUrl?.[1]) {
|
|
100
|
+
return doubleEncodedConfigurationUrl[1];
|
|
101
|
+
}
|
|
102
|
+
const encodedConfigurationUrl = html.match(/"url":"([^;]+)"/);
|
|
103
|
+
if (encodedConfigurationUrl?.[1]) {
|
|
104
|
+
return encodedConfigurationUrl[1];
|
|
105
|
+
}
|
|
106
|
+
const scriptContent = parseScriptContent(html);
|
|
107
|
+
if (scriptContent) {
|
|
108
|
+
return scriptContent;
|
|
109
|
+
}
|
|
110
|
+
const scriptConfigMatch = html.match(/url:\s*["']([^"']+)["']/i);
|
|
111
|
+
if (scriptConfigMatch?.[1]) {
|
|
112
|
+
return scriptConfigMatch?.[1];
|
|
113
|
+
}
|
|
114
|
+
const linkMatch = html.match(/<a[^>]*href=["']([^"']+\.(?:yaml|yml|json))["'][^>]*>/i);
|
|
115
|
+
if (linkMatch?.[1]) {
|
|
116
|
+
return linkMatch[1];
|
|
117
|
+
}
|
|
118
|
+
const escapedJsonMatch = html.match(/\\"spec\\":\{.*?\\"url\\":\\"([^"\\]+)\\"/);
|
|
119
|
+
if (escapedJsonMatch?.[1]) {
|
|
120
|
+
return escapedJsonMatch[1];
|
|
121
|
+
}
|
|
122
|
+
return void 0;
|
|
172
123
|
}
|
|
173
|
-
/**
|
|
174
|
-
* Get the content between the script tags
|
|
175
|
-
*
|
|
176
|
-
* @example <script id="api-reference">console.log("Hello, world!");</script>
|
|
177
|
-
*/
|
|
178
124
|
function getContentOfScriptTag(html) {
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
125
|
+
const patterns = [
|
|
126
|
+
// Double quotes
|
|
127
|
+
/<script[^>]*id="api-reference[\s\S]*?">([\s\S]*?)<\/script>/i,
|
|
128
|
+
// Single quotes
|
|
129
|
+
/<script[^>]*id='api-reference[\s\S]*?'>([\s\S]*?)<\/script>/i
|
|
130
|
+
];
|
|
131
|
+
for (const pattern of patterns) {
|
|
132
|
+
const match = html.match(pattern);
|
|
133
|
+
if (match?.[1]) {
|
|
134
|
+
return match[1].trim();
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
return void 0;
|
|
192
138
|
}
|
|
193
|
-
/**
|
|
194
|
-
* Parse OpenAPI document directly from script tag content
|
|
195
|
-
*/
|
|
196
139
|
function parseScriptContent(html) {
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
return JSON.parse(sanitizedContent);
|
|
209
|
-
}
|
|
210
|
-
catch {
|
|
211
|
-
// YAML
|
|
212
|
-
return parse(content);
|
|
213
|
-
}
|
|
214
|
-
}
|
|
140
|
+
const content = getContentOfScriptTag(html);
|
|
141
|
+
try {
|
|
142
|
+
if (content) {
|
|
143
|
+
try {
|
|
144
|
+
return JSON.parse(content);
|
|
145
|
+
} catch {
|
|
146
|
+
try {
|
|
147
|
+
const sanitizedContent = content.replace(/\\s/g, "\\\\s");
|
|
148
|
+
return JSON.parse(sanitizedContent);
|
|
149
|
+
} catch {
|
|
150
|
+
return parse(content);
|
|
215
151
|
}
|
|
152
|
+
}
|
|
216
153
|
}
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
154
|
+
} catch (error) {
|
|
155
|
+
console.error("[@scalar/import] Failed to parse script content:", error);
|
|
156
|
+
}
|
|
157
|
+
return void 0;
|
|
221
158
|
}
|
|
222
|
-
/**
|
|
223
|
-
* Get the configuration attribute from the script tag
|
|
224
|
-
*
|
|
225
|
-
* @example <script id="api-reference" data-configuration="{"spec":{"content":"foo"}}"></script>
|
|
226
|
-
*/
|
|
227
159
|
function getConfigurationAttribute(html) {
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
160
|
+
const patterns = [
|
|
161
|
+
// Double quotes
|
|
162
|
+
/<script[^>]*id="api-reference"[^>]*data-configuration=["]([^"]+)["][^>]*>(.*?)<\/script>/s,
|
|
163
|
+
// Single quotes
|
|
164
|
+
/<script[^>]*id='api-reference'[^>]*data-configuration=[']([^']+)['][^>]*>(.*?)<\/script>/s,
|
|
165
|
+
// Mix quote single first
|
|
166
|
+
/<script[^>]*id='api-reference'[^>]*data-configuration=["]([^"]+)["][^>]*>(.*?)<\/script>/s,
|
|
167
|
+
// Mix quote double first
|
|
168
|
+
/<script[^>]*id="api-reference"[^>]*data-configuration=[']([^']+)['][^>]*>(.*?)<\/script>/s
|
|
169
|
+
];
|
|
170
|
+
for (const pattern of patterns) {
|
|
171
|
+
const match = html.match(pattern);
|
|
172
|
+
if (match?.[1]) {
|
|
173
|
+
return match[1];
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
return void 0;
|
|
245
177
|
}
|
|
246
|
-
/** Grab the URL from the configuration data attribute */
|
|
247
178
|
const getConfigurationAttributeUrl = (html) => {
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
179
|
+
const configString = getConfigurationAttribute(html);
|
|
180
|
+
if (!configString) {
|
|
181
|
+
return void 0;
|
|
182
|
+
}
|
|
183
|
+
const config = JSON.parse(decodeHtmlEntities(configString));
|
|
184
|
+
return config.url || config.spec?.url;
|
|
254
185
|
};
|
|
255
|
-
/**
|
|
256
|
-
* Parse embedded OpenAPI document from HTML
|
|
257
|
-
*/
|
|
258
186
|
function parseEmbeddedOpenApi(html) {
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
}
|
|
277
|
-
catch (error) {
|
|
278
|
-
console.error('[@scalar/import] Failed to parse embedded OpenAPI document:', error);
|
|
279
|
-
}
|
|
280
|
-
return undefined;
|
|
187
|
+
const configString = getConfigurationAttribute(html);
|
|
188
|
+
if (!configString) {
|
|
189
|
+
return void 0;
|
|
190
|
+
}
|
|
191
|
+
try {
|
|
192
|
+
const config = JSON.parse(decodeHtmlEntities(configString));
|
|
193
|
+
const content = config.content || config.spec?.content;
|
|
194
|
+
if (content) {
|
|
195
|
+
if (typeof content === "string") {
|
|
196
|
+
return parse(content);
|
|
197
|
+
}
|
|
198
|
+
return content;
|
|
199
|
+
}
|
|
200
|
+
} catch (error) {
|
|
201
|
+
console.error("[@scalar/import] Failed to parse embedded OpenAPI document:", error);
|
|
202
|
+
}
|
|
203
|
+
return void 0;
|
|
281
204
|
}
|
|
282
|
-
/**
|
|
283
|
-
* Decode HTML entities in a string
|
|
284
|
-
*/
|
|
285
205
|
function decodeHtmlEntities(text) {
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
// console.log('ok', updatedText)
|
|
299
|
-
return decodeHtmlEntities(updatedText);
|
|
300
|
-
}
|
|
301
|
-
return updatedText;
|
|
206
|
+
const entities = {
|
|
207
|
+
"&": "&",
|
|
208
|
+
"<": "<",
|
|
209
|
+
">": ">",
|
|
210
|
+
""": '"',
|
|
211
|
+
"'": "'"
|
|
212
|
+
};
|
|
213
|
+
const updatedText = text.replace(new RegExp(Object.keys(entities).join("|"), "g"), (match) => entities[match]).replace(/\n/g, "\\n").trim();
|
|
214
|
+
if (updatedText.startsWith("{"")) {
|
|
215
|
+
return decodeHtmlEntities(updatedText);
|
|
216
|
+
}
|
|
217
|
+
return updatedText;
|
|
302
218
|
}
|
|
303
|
-
/**
|
|
304
|
-
* Transform GitHub URLs to raw file URLs, preserving the branch information
|
|
305
|
-
*/
|
|
306
219
|
function transformGitHubUrl(url) {
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
220
|
+
const githubRegex = /^https:\/\/github\.com\/([^/]+)\/([^/]+)\/blob\/([^/]+)\/(.+)$/;
|
|
221
|
+
const match = url.match(githubRegex);
|
|
222
|
+
if (match) {
|
|
223
|
+
const [, owner, repo, branch, path] = match;
|
|
224
|
+
return `https://raw.githubusercontent.com/${owner}/${repo}/refs/heads/${branch}/${path}`;
|
|
225
|
+
}
|
|
226
|
+
return void 0;
|
|
314
227
|
}
|
|
315
|
-
|
|
316
|
-
|
|
228
|
+
export {
|
|
229
|
+
getConfigurationAttribute,
|
|
230
|
+
getConfigurationAttributeUrl,
|
|
231
|
+
getContentOfScriptTag,
|
|
232
|
+
resolve
|
|
233
|
+
};
|
|
234
|
+
//# sourceMappingURL=resolve.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/resolve.ts"],
|
|
4
|
+
"sourcesContent": ["import { makeUrlAbsolute } from '@scalar/oas-utils/helpers'\nimport { parse } from 'yaml'\n\n/**\n * Find an OpenAPI document URL in the HTML of @scalar/api-reference and other places.\n * This is useful to open the OpenAPI document from basically any source.\n */\nexport async function resolve(\n value?: string | null,\n options?: {\n /**\n * Fetch function to use instead of the global fetch. Use this to intercept requests.\n */\n fetch?: (url: string) => Promise<Response>\n },\n): Promise<string | Record<string, any> | undefined> {\n // URLs\n if (value?.startsWith('http://') || value?.startsWith('https://')) {\n // Transform GitHub URLs to raw file URLs\n const githubRawUrl = transformGitHubUrl(value)\n\n if (githubRawUrl) {\n return githubRawUrl\n }\n\n // https://*.json\n if (value?.toLowerCase().endsWith('.json')) {\n return value\n }\n\n // https://*.yaml\n if (value?.toLowerCase().endsWith('.yaml') || value?.toLowerCase().endsWith('.yml')) {\n return value\n }\n\n // https://sandbox.scalar.com\n const sandboxUrl = value.match(/https:\\/\\/sandbox\\.scalar\\.com\\/(p|e)\\/([a-zA-Z0-9]+)/)\n\n if (sandboxUrl?.[2]) {\n return `https://sandbox.scalar.com/files/${sandboxUrl[2]}/openapi.yaml`\n }\n\n // Fetch URL\n try {\n let result = await (options?.fetch\n ? options.fetch(value)\n : fetch(value, {\n cache: 'no-cache',\n }))\n\n // If the custom fetch failed, try again with regular fetch\n if (!result.ok && options?.fetch) {\n result = await fetch(value, {\n cache: 'no-cache',\n })\n }\n\n if (result.ok) {\n const content = await result.text()\n const forwardedHost = result.headers.get('X-Forwarded-Host')\n\n // Check if content is directly JSON/YAML\n try {\n // Try parsing as JSON\n const jsonContent = JSON.parse(content)\n if (jsonContent.openapi || jsonContent.swagger) {\n return value\n }\n } catch {\n // Try parsing as YAML\n try {\n const yamlContent = parse(content)\n if (yamlContent?.openapi || yamlContent?.swagger) {\n return value\n }\n } catch {\n // Not YAML\n }\n }\n\n const urlOrPathOrDocument = parseHtml(content)\n\n // Document (string)\n if (typeof urlOrPathOrDocument === 'string') {\n try {\n // JSON?\n return JSON.parse(urlOrPathOrDocument)\n } catch {\n // No JSON\n try {\n // YAML?\n const yaml = parse(urlOrPathOrDocument)\n\n if (typeof yaml === 'object') {\n return yaml\n }\n } catch {\n // Not YAML\n }\n }\n }\n\n // Document (object)\n if (typeof urlOrPathOrDocument === 'object') {\n return urlOrPathOrDocument\n }\n\n // Relative or absolute URL\n if (urlOrPathOrDocument) {\n return makeUrlAbsolute(urlOrPathOrDocument, forwardedHost || value)\n }\n\n // Check for configuration attribute URL\n const configUrl = getConfigurationAttributeUrl(content)\n if (configUrl) {\n return makeUrlAbsolute(configUrl, forwardedHost || value)\n }\n\n // Check for embedded OpenAPI document\n const embeddedSpec = parseEmbeddedOpenApi(content)\n if (embeddedSpec) {\n return embeddedSpec\n }\n } else {\n console.warn(`[@scalar/import] Failed to fetch ${value}`)\n }\n } catch (error) {\n console.error(`[@scalar/import] Failed to fetch ${value}`, error)\n }\n }\n\n return undefined\n}\n\n/**\n * Go through the HTML and try to find the OpenAPI document URL\n */\nfunction parseHtml(html?: string) {\n // Check whether it could be HTML\n if (!html?.includes('<')) {\n return undefined\n }\n\n // id=\"api-reference\" data-url=\"*\"\n const dataUrlMatch = html\n .match(/id=[\"']api-reference[\"'][\\s\\S]*?data-url=[\"']([^\"']+)[\"']/)\n ?.slice(1)\n .find(Boolean)\n\n if (dataUrlMatch) {\n return dataUrlMatch\n }\n\n // spec-url=\"*\"\n const specUrlMatch = html.match(/spec-url=[\"']([^\"']+)[\"']/)\n if (specUrlMatch?.[1]) {\n return specUrlMatch[1]\n }\n\n // Redoc.init('*')\n const redocInit = html.match(/Redoc.init\\([\"']([^\"']+)[\"']/)\n\n if (redocInit?.[1]) {\n return redocInit[1]\n }\n\n // &quot;url&quot;:&quot;MY_CUSTOM_URL&quot;\n const doubleEncodedConfigurationUrl = html.match(/&quot;url&quot;:&quot;([^;]+)&quot;/)\n\n if (doubleEncodedConfigurationUrl?.[1]) {\n return doubleEncodedConfigurationUrl[1]\n }\n\n // &quot;url&quot;:&quot;MY_CUSTOM_URL&quot;\n const encodedConfigurationUrl = html.match(/"url":"([^;]+)"/)\n if (encodedConfigurationUrl?.[1]) {\n return encodedConfigurationUrl[1]\n }\n\n // Try to find embedded OpenAPI document in script tag first\n const scriptContent = parseScriptContent(html)\n if (scriptContent) {\n return scriptContent\n }\n\n // Check for configuration in script tag\n const scriptConfigMatch = html.match(/url:\\s*[\"']([^\"']+)[\"']/i)\n\n if (scriptConfigMatch?.[1]) {\n return scriptConfigMatch?.[1]\n }\n\n // Check for OpenAPI URLs in the HTML\n const linkMatch = html.match(/<a[^>]*href=[\"']([^\"']+\\.(?:yaml|yml|json))[\"'][^>]*>/i)\n\n if (linkMatch?.[1]) {\n return linkMatch[1]\n }\n\n // Check for URLs in escaped JS objects\n const escapedJsonMatch = html.match(/\\\\\"spec\\\\\":\\{.*?\\\\\"url\\\\\":\\\\\"([^\"\\\\]+)\\\\\"/)\n\n if (escapedJsonMatch?.[1]) {\n return escapedJsonMatch[1]\n }\n\n return undefined\n}\n\n/**\n * Get the content between the script tags\n *\n * @example <script id=\"api-reference\">console.log(\"Hello, world!\");</script>\n */\nexport function getContentOfScriptTag(html: string): string | undefined {\n const patterns = [\n // Double quotes\n /<script[^>]*id=\"api-reference[\\s\\S]*?\">([\\s\\S]*?)<\\/script>/i,\n // Single quotes\n /<script[^>]*id='api-reference[\\s\\S]*?'>([\\s\\S]*?)<\\/script>/i,\n ]\n\n for (const pattern of patterns) {\n const match = html.match(pattern)\n\n if (match?.[1]) {\n return match[1].trim()\n }\n }\n\n return undefined\n}\n\n/**\n * Parse OpenAPI document directly from script tag content\n */\nfunction parseScriptContent(html: string): Record<string, any> | undefined {\n const content = getContentOfScriptTag(html)\n\n try {\n if (content) {\n try {\n // JSON\n return JSON.parse(content)\n } catch {\n try {\n // JSON with escaped whitespace\n const sanitizedContent = content.replace(/\\\\s/g, '\\\\\\\\s')\n\n return JSON.parse(sanitizedContent)\n } catch {\n // YAML\n return parse(content)\n }\n }\n }\n } catch (error) {\n console.error('[@scalar/import] Failed to parse script content:', error)\n }\n\n return undefined\n}\n\n/**\n * Get the configuration attribute from the script tag\n *\n * @example <script id=\"api-reference\" data-configuration=\"{"spec":{"content":"foo"}}\"></script>\n */\nexport function getConfigurationAttribute(html: string): string | undefined {\n const patterns = [\n // Double quotes\n /<script[^>]*id=\"api-reference\"[^>]*data-configuration=[\"]([^\"]+)[\"][^>]*>(.*?)<\\/script>/s,\n // Single quotes\n /<script[^>]*id='api-reference'[^>]*data-configuration=[']([^']+)['][^>]*>(.*?)<\\/script>/s,\n // Mix quote single first\n /<script[^>]*id='api-reference'[^>]*data-configuration=[\"]([^\"]+)[\"][^>]*>(.*?)<\\/script>/s,\n // Mix quote double first\n /<script[^>]*id=\"api-reference\"[^>]*data-configuration=[']([^']+)['][^>]*>(.*?)<\\/script>/s,\n ]\n\n for (const pattern of patterns) {\n const match = html.match(pattern)\n\n if (match?.[1]) {\n return match[1]\n }\n }\n\n return undefined\n}\n\n/** Grab the URL from the configuration data attribute */\nexport const getConfigurationAttributeUrl = (html: string): string | undefined => {\n const configString = getConfigurationAttribute(html)\n if (!configString) {\n return undefined\n }\n\n const config = JSON.parse(decodeHtmlEntities(configString))\n return config.url || config.spec?.url\n}\n\n/**\n * Parse embedded OpenAPI document from HTML\n */\nfunction parseEmbeddedOpenApi(html: string): object | undefined {\n const configString = getConfigurationAttribute(html)\n if (!configString) {\n return undefined\n }\n\n try {\n const config = JSON.parse(decodeHtmlEntities(configString))\n\n // Handle the old spec format while we transition to the new one\n const content = config.content || config.spec?.content\n\n // Handle both direct JSON content and YAML content\n if (content) {\n // If content is a string, assume it's YAML\n if (typeof content === 'string') {\n return parse(content)\n }\n // If content is an object, return it directly\n return content\n }\n } catch (error) {\n console.error('[@scalar/import] Failed to parse embedded OpenAPI document:', error)\n }\n\n return undefined\n}\n\n/**\n * Decode HTML entities in a string\n */\nfunction decodeHtmlEntities(text: string): string {\n const entities = {\n '&': '&',\n '<': '<',\n '>': '>',\n '"': '\"',\n ''': \"'\",\n } as const\n\n const updatedText = text\n .replace(new RegExp(Object.keys(entities).join('|'), 'g'), (match) => entities[match as keyof typeof entities])\n .replace(/\\n/g, '\\\\n')\n .trim()\n\n if (updatedText.startsWith('{"')) {\n // console.log('ok', updatedText)\n return decodeHtmlEntities(updatedText)\n }\n\n return updatedText\n}\n\n/**\n * Transform GitHub URLs to raw file URLs, preserving the branch information\n */\nfunction transformGitHubUrl(url: string): string | undefined {\n const githubRegex = /^https:\\/\\/github\\.com\\/([^/]+)\\/([^/]+)\\/blob\\/([^/]+)\\/(.+)$/\n const match = url.match(githubRegex)\n\n if (match) {\n const [, owner, repo, branch, path] = match\n return `https://raw.githubusercontent.com/${owner}/${repo}/refs/heads/${branch}/${path}`\n }\n\n return undefined\n}\n"],
|
|
5
|
+
"mappings": "AAAA,SAAS,uBAAuB;AAChC,SAAS,aAAa;AAMtB,eAAsB,QACpB,OACA,SAMmD;AAEnD,MAAI,OAAO,WAAW,SAAS,KAAK,OAAO,WAAW,UAAU,GAAG;AAEjE,UAAM,eAAe,mBAAmB,KAAK;AAE7C,QAAI,cAAc;AAChB,aAAO;AAAA,IACT;AAGA,QAAI,OAAO,YAAY,EAAE,SAAS,OAAO,GAAG;AAC1C,aAAO;AAAA,IACT;AAGA,QAAI,OAAO,YAAY,EAAE,SAAS,OAAO,KAAK,OAAO,YAAY,EAAE,SAAS,MAAM,GAAG;AACnF,aAAO;AAAA,IACT;AAGA,UAAM,aAAa,MAAM,MAAM,uDAAuD;AAEtF,QAAI,aAAa,CAAC,GAAG;AACnB,aAAO,oCAAoC,WAAW,CAAC,CAAC;AAAA,IAC1D;AAGA,QAAI;AACF,UAAI,SAAS,OAAO,SAAS,QACzB,QAAQ,MAAM,KAAK,IACnB,MAAM,OAAO;AAAA,QACX,OAAO;AAAA,MACT,CAAC;AAGL,UAAI,CAAC,OAAO,MAAM,SAAS,OAAO;AAChC,iBAAS,MAAM,MAAM,OAAO;AAAA,UAC1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