@scalar/oas-utils 0.10.8 → 0.10.9
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/entities/cookie/cookie.js +11 -15
- package/dist/entities/cookie/index.js +1 -5
- package/dist/entities/environment/environment.js +7 -11
- package/dist/entities/environment/index.js +1 -5
- package/dist/entities/hotkeys/hotkeys.js +116 -111
- package/dist/entities/hotkeys/index.js +1 -6
- package/dist/entities/shared/index.js +1 -7
- package/dist/entities/shared/utility.js +9 -9
- package/dist/entities/spec/collection.js +91 -89
- package/dist/entities/spec/index.js +10 -59
- package/dist/entities/spec/operation.js +6 -6
- package/dist/entities/spec/parameters.js +38 -38
- package/dist/entities/spec/request-examples.js +421 -331
- package/dist/entities/spec/requests.js +102 -84
- package/dist/entities/spec/server.js +61 -46
- package/dist/entities/spec/spec-objects.js +121 -76
- package/dist/entities/spec/x-scalar-environments.js +18 -20
- package/dist/entities/spec/x-scalar-secrets.js +6 -8
- package/dist/entities/workspace/index.js +1 -7
- package/dist/entities/workspace/workspace.js +47 -46
- package/dist/helpers/client-plugins.js +13 -13
- package/dist/helpers/fetch-document.js +30 -25
- package/dist/helpers/fetch-with-proxy-fallback.js +26 -21
- package/dist/helpers/index.d.ts +1 -2
- package/dist/helpers/index.d.ts.map +1 -1
- package/dist/helpers/index.js +80 -119
- package/dist/helpers/normalize-mime-type-object.js +19 -18
- package/dist/helpers/normalize-mime-type.js +11 -9
- package/dist/helpers/operation-stability.js +25 -20
- package/dist/helpers/parse.d.ts +0 -4
- package/dist/helpers/parse.d.ts.map +1 -1
- package/dist/helpers/parse.js +77 -77
- package/dist/helpers/schema-model.js +13 -16
- package/dist/helpers/security/get-schemes.js +7 -8
- package/dist/helpers/security/has-token.js +18 -19
- package/dist/helpers/security/index.js +2 -7
- package/dist/helpers/servers.js +128 -79
- package/dist/helpers/should-ignore-entity.js +4 -5
- package/dist/migrations/data-version.js +15 -7
- package/dist/migrations/generate-types.js +34 -37
- package/dist/migrations/index.js +4 -18
- package/dist/migrations/local-storage.js +31 -29
- package/dist/migrations/migrate-to-indexdb.js +706 -529
- package/dist/migrations/migrator.js +58 -54
- package/dist/migrations/semver.js +24 -24
- package/dist/migrations/v-0.0.0/types.generated.js +1 -1
- package/dist/migrations/v-2.1.0/migration.js +272 -258
- package/dist/migrations/v-2.1.0/types.generated.js +1 -1
- package/dist/migrations/v-2.2.0/migration.js +99 -96
- package/dist/migrations/v-2.2.0/types.generated.js +1 -1
- package/dist/migrations/v-2.3.0/migration.js +45 -48
- package/dist/migrations/v-2.3.0/types.generated.js +1 -1
- package/dist/migrations/v-2.4.0/migration.js +25 -25
- package/dist/migrations/v-2.4.0/types.generated.js +1 -1
- package/dist/migrations/v-2.5.0/migration.js +122 -139
- package/dist/migrations/v-2.5.0/types.generated.js +1 -1
- package/dist/spec-getters/get-example-from-schema.js +489 -385
- package/dist/spec-getters/get-parameters-from-operation.js +39 -23
- package/dist/spec-getters/get-request-body-from-operation.d.ts.map +1 -1
- package/dist/spec-getters/get-request-body-from-operation.js +168 -126
- package/dist/spec-getters/get-server-variable-examples.js +10 -13
- package/dist/spec-getters/index.js +4 -11
- package/dist/transforms/import-spec.js +381 -291
- package/dist/transforms/index.js +1 -11
- package/package.json +15 -19
- package/dist/entities/cookie/cookie.js.map +0 -7
- package/dist/entities/cookie/index.js.map +0 -7
- package/dist/entities/environment/environment.js.map +0 -7
- package/dist/entities/environment/index.js.map +0 -7
- package/dist/entities/hotkeys/hotkeys.js.map +0 -7
- package/dist/entities/hotkeys/index.js.map +0 -7
- package/dist/entities/shared/index.js.map +0 -7
- package/dist/entities/shared/utility.js.map +0 -7
- package/dist/entities/spec/collection.js.map +0 -7
- package/dist/entities/spec/index.js.map +0 -7
- package/dist/entities/spec/operation.js.map +0 -7
- package/dist/entities/spec/parameters.js.map +0 -7
- package/dist/entities/spec/request-examples.js.map +0 -7
- package/dist/entities/spec/requests.js.map +0 -7
- package/dist/entities/spec/server.js.map +0 -7
- package/dist/entities/spec/spec-objects.js.map +0 -7
- package/dist/entities/spec/x-scalar-environments.js.map +0 -7
- package/dist/entities/spec/x-scalar-secrets.js.map +0 -7
- package/dist/entities/workspace/index.js.map +0 -7
- package/dist/entities/workspace/workspace.js.map +0 -7
- package/dist/helpers/client-plugins.js.map +0 -7
- package/dist/helpers/fetch-document.js.map +0 -7
- package/dist/helpers/fetch-with-proxy-fallback.js.map +0 -7
- package/dist/helpers/index.js.map +0 -7
- package/dist/helpers/normalize-mime-type-object.js.map +0 -7
- package/dist/helpers/normalize-mime-type.js.map +0 -7
- package/dist/helpers/operation-stability.js.map +0 -7
- package/dist/helpers/parse.js.map +0 -7
- package/dist/helpers/pretty-print-json.d.ts +0 -9
- package/dist/helpers/pretty-print-json.d.ts.map +0 -1
- package/dist/helpers/pretty-print-json.js +0 -38
- package/dist/helpers/pretty-print-json.js.map +0 -7
- package/dist/helpers/schema-model.js.map +0 -7
- package/dist/helpers/security/get-schemes.js.map +0 -7
- package/dist/helpers/security/has-token.js.map +0 -7
- package/dist/helpers/security/index.js.map +0 -7
- package/dist/helpers/servers.js.map +0 -7
- package/dist/helpers/should-ignore-entity.js.map +0 -7
- package/dist/migrations/data-version.js.map +0 -7
- package/dist/migrations/generate-types.js.map +0 -7
- package/dist/migrations/index.js.map +0 -7
- package/dist/migrations/local-storage.js.map +0 -7
- package/dist/migrations/migrate-to-indexdb.js.map +0 -7
- package/dist/migrations/migrator.js.map +0 -7
- package/dist/migrations/semver.js.map +0 -7
- package/dist/migrations/v-0.0.0/types.generated.js.map +0 -7
- package/dist/migrations/v-2.1.0/migration.js.map +0 -7
- package/dist/migrations/v-2.1.0/types.generated.js.map +0 -7
- package/dist/migrations/v-2.2.0/migration.js.map +0 -7
- package/dist/migrations/v-2.2.0/types.generated.js.map +0 -7
- package/dist/migrations/v-2.3.0/migration.js.map +0 -7
- package/dist/migrations/v-2.3.0/types.generated.js.map +0 -7
- package/dist/migrations/v-2.4.0/migration.js.map +0 -7
- package/dist/migrations/v-2.4.0/types.generated.js.map +0 -7
- package/dist/migrations/v-2.5.0/migration.js.map +0 -7
- package/dist/migrations/v-2.5.0/types.generated.js.map +0 -7
- package/dist/spec-getters/get-example-from-schema.js.map +0 -7
- package/dist/spec-getters/get-parameters-from-operation.js.map +0 -7
- package/dist/spec-getters/get-request-body-from-operation.js.map +0 -7
- package/dist/spec-getters/get-server-variable-examples.js.map +0 -7
- package/dist/spec-getters/index.js.map +0 -7
- package/dist/transforms/import-spec.js.map +0 -7
- package/dist/transforms/index.js.map +0 -7
package/dist/helpers/parse.js
CHANGED
|
@@ -1,83 +1,83 @@
|
|
|
1
|
-
import { parse, stringify } from
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
1
|
+
import { parse, stringify } from 'yaml';
|
|
2
|
+
/** Yaml handling with optional safeparse */
|
|
3
|
+
export const yaml = {
|
|
4
|
+
/** Parse and throw if the return value is not an object */
|
|
5
|
+
parse: (val) => {
|
|
6
|
+
const yamlObject = parse(val, { merge: true, maxAliasCount: 10000 });
|
|
7
|
+
if (typeof yamlObject !== 'object') {
|
|
8
|
+
throw Error('Invalid YAML object');
|
|
9
|
+
}
|
|
10
|
+
return yamlObject;
|
|
11
|
+
},
|
|
12
|
+
/** Parse and return a fallback on failure */
|
|
13
|
+
parseSafe(val, fallback) {
|
|
14
|
+
try {
|
|
15
|
+
return yaml.parse(val);
|
|
16
|
+
}
|
|
17
|
+
catch (err) {
|
|
18
|
+
return typeof fallback === 'function' ? fallback(err) : fallback;
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
stringify,
|
|
22
|
+
};
|
|
23
|
+
/** JSON handling with optional safeparse */
|
|
24
|
+
export const json = {
|
|
25
|
+
/** Parse and throw if the return value is not an object */
|
|
26
|
+
parse: (val) => {
|
|
27
|
+
const jsonObject = JSON.parse(val);
|
|
28
|
+
if (typeof jsonObject !== 'object') {
|
|
29
|
+
throw Error('Invalid JSON object');
|
|
30
|
+
}
|
|
31
|
+
return jsonObject;
|
|
32
|
+
},
|
|
33
|
+
/** Parse and return a fallback on failure */
|
|
34
|
+
parseSafe(val, fallback) {
|
|
35
|
+
try {
|
|
36
|
+
return json.parse(val);
|
|
37
|
+
}
|
|
38
|
+
catch (err) {
|
|
39
|
+
return typeof fallback === 'function' ? fallback(err) : fallback;
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
stringify: (val) => JSON.stringify(val),
|
|
20
43
|
};
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
44
|
+
/**
|
|
45
|
+
* This helper is used to transform the content of the swagger file to JSON, even it was YAML.
|
|
46
|
+
*/
|
|
47
|
+
export const transformToJson = (value) => {
|
|
48
|
+
// Try json, then fallback to yaml, then fallback to string
|
|
49
|
+
return JSON.stringify(json.parseSafe(value, yaml.parseSafe(value, value)));
|
|
50
|
+
};
|
|
51
|
+
/** Validates a JSON string if provided. Otherwise returns the raw YAML */
|
|
52
|
+
export function formatJsonOrYamlString(value) {
|
|
53
|
+
// If we don't start with a bracket assume yaml
|
|
54
|
+
const trimmed = value.trim();
|
|
55
|
+
if (trimmed[0] !== '{' && trimmed[0] !== '[') {
|
|
56
|
+
return value;
|
|
27
57
|
}
|
|
28
|
-
return jsonObject;
|
|
29
|
-
},
|
|
30
|
-
/** Parse and return a fallback on failure */
|
|
31
|
-
parseSafe(val, fallback) {
|
|
32
58
|
try {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
59
|
+
// JSON
|
|
60
|
+
return JSON.stringify(JSON.parse(value), null, 2);
|
|
61
|
+
}
|
|
62
|
+
catch {
|
|
63
|
+
// YAML
|
|
64
|
+
return value;
|
|
36
65
|
}
|
|
37
|
-
},
|
|
38
|
-
stringify: (val) => JSON.stringify(val)
|
|
39
|
-
};
|
|
40
|
-
const isJsonString = (value) => {
|
|
41
|
-
if (typeof value !== "string") {
|
|
42
|
-
return false;
|
|
43
|
-
}
|
|
44
|
-
return !!json.parseSafe(value, false);
|
|
45
|
-
};
|
|
46
|
-
const transformToJson = (value) => {
|
|
47
|
-
return JSON.stringify(json.parseSafe(value, yaml.parseSafe(value, value)));
|
|
48
|
-
};
|
|
49
|
-
function formatJsonOrYamlString(value) {
|
|
50
|
-
const trimmed = value.trim();
|
|
51
|
-
if (trimmed[0] !== "{" && trimmed[0] !== "[") {
|
|
52
|
-
return value;
|
|
53
|
-
}
|
|
54
|
-
try {
|
|
55
|
-
return JSON.stringify(JSON.parse(value), null, 2);
|
|
56
|
-
} catch {
|
|
57
|
-
return value;
|
|
58
|
-
}
|
|
59
66
|
}
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
formatJsonOrYamlString,
|
|
77
|
-
isJsonString,
|
|
78
|
-
json,
|
|
79
|
-
parseJsonOrYaml,
|
|
80
|
-
transformToJson,
|
|
81
|
-
yaml
|
|
67
|
+
/** Parse JSON or YAML into an object */
|
|
68
|
+
export const parseJsonOrYaml = (value) => {
|
|
69
|
+
if (typeof value !== 'string') {
|
|
70
|
+
return value;
|
|
71
|
+
}
|
|
72
|
+
const jsonObject = json.parseSafe(value, null);
|
|
73
|
+
if (jsonObject) {
|
|
74
|
+
return jsonObject;
|
|
75
|
+
}
|
|
76
|
+
// Value is probably supposed to be JSON. Throw
|
|
77
|
+
if (value.length > 0 && ['{', '['].includes(value[0] ?? '')) {
|
|
78
|
+
throw Error('Invalid JSON or YAML');
|
|
79
|
+
}
|
|
80
|
+
return yaml.parseSafe(value, (err) => {
|
|
81
|
+
throw Error(err);
|
|
82
|
+
});
|
|
82
83
|
};
|
|
83
|
-
//# sourceMappingURL=parse.js.map
|
|
@@ -1,17 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
1
|
+
/** Parse a Zod */
|
|
2
|
+
export function schemaModel(data, schema, throwError = true, errorLocation) {
|
|
3
|
+
const result = schema.safeParse(data);
|
|
4
|
+
if (!result.success) {
|
|
5
|
+
console.group('Schema Error' + (errorLocation ? ` - ${errorLocation}` : ''));
|
|
6
|
+
console.warn(JSON.stringify(result.error.format(), null, 2));
|
|
7
|
+
console.log('Received: ', data);
|
|
8
|
+
console.groupEnd();
|
|
9
|
+
}
|
|
10
|
+
if (throwError && !result.success) {
|
|
11
|
+
throw new Error('Zod validation failure');
|
|
12
|
+
}
|
|
13
|
+
return result.data;
|
|
13
14
|
}
|
|
14
|
-
export {
|
|
15
|
-
schemaModel
|
|
16
|
-
};
|
|
17
|
-
//# sourceMappingURL=schema-model.js.map
|
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
import { isDefined } from
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import { isDefined } from '@scalar/helpers/array/is-defined';
|
|
2
|
+
/** Parses a list of selected security scheme uids which may be an array or single uid and returns a flattened array of security schemes */
|
|
3
|
+
export const getSchemes = (selectedSecuritySchemes, securitySchemes) => {
|
|
4
|
+
const uids = new Set(selectedSecuritySchemes.flat());
|
|
5
|
+
return Array.from(uids)
|
|
6
|
+
.map((uid) => securitySchemes[uid])
|
|
7
|
+
.filter(isDefined);
|
|
5
8
|
};
|
|
6
|
-
export {
|
|
7
|
-
getSchemes
|
|
8
|
-
};
|
|
9
|
-
//# sourceMappingURL=get-schemes.js.map
|
|
@@ -1,20 +1,19 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
)
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
1
|
+
/** Pass in a security scheme and it will return true if it has a token or value depending on the scheme type */
|
|
2
|
+
export const hasToken = (scheme) => {
|
|
3
|
+
// ApiKey
|
|
4
|
+
if (scheme.type === 'apiKey') {
|
|
5
|
+
return Boolean(scheme.value);
|
|
6
|
+
}
|
|
7
|
+
// Http
|
|
8
|
+
if (scheme.type === 'http') {
|
|
9
|
+
return Boolean((scheme.scheme === 'bearer' && scheme.token) || (scheme.scheme === 'basic' && scheme.username && scheme.password));
|
|
10
|
+
}
|
|
11
|
+
// OAuth2 just check for A token
|
|
12
|
+
if (scheme.type === 'oauth2') {
|
|
13
|
+
return Boolean(scheme.flows.authorizationCode?.token ||
|
|
14
|
+
scheme.flows.clientCredentials?.token ||
|
|
15
|
+
scheme.flows.password?.token ||
|
|
16
|
+
scheme.flows.implicit?.token);
|
|
17
|
+
}
|
|
18
|
+
return false;
|
|
16
19
|
};
|
|
17
|
-
export {
|
|
18
|
-
hasToken
|
|
19
|
-
};
|
|
20
|
-
//# sourceMappingURL=has-token.js.map
|
package/dist/helpers/servers.js
CHANGED
|
@@ -1,97 +1,146 @@
|
|
|
1
|
-
import { isDefined } from
|
|
2
|
-
import { combineUrlAndPath } from
|
|
3
|
-
import { serverSchema } from
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
1
|
+
import { isDefined } from '@scalar/helpers/array/is-defined';
|
|
2
|
+
import { combineUrlAndPath } from '@scalar/helpers/url/merge-urls';
|
|
3
|
+
import { serverSchema } from '../entities/spec/server.js';
|
|
4
|
+
/**
|
|
5
|
+
* Retrieves and processes servers from an OpenAPI document.
|
|
6
|
+
*
|
|
7
|
+
* This function handles several scenarios:
|
|
8
|
+
* 1. No servers provided - creates a default server from document URL or fallback
|
|
9
|
+
* 2. Invalid server configurations - filters them out with warnings
|
|
10
|
+
* 3. Relative URLs - resolves them to absolute URLs using available base URLs
|
|
11
|
+
*
|
|
12
|
+
* @param servers - Array of OpenAPI server objects from the document
|
|
13
|
+
* @param options - Configuration options for server processing
|
|
14
|
+
* @returns Array of validated Server entities
|
|
15
|
+
*/
|
|
16
|
+
export function getServersFromDocument(servers, options = {}) {
|
|
17
|
+
// Handle case where no servers are provided
|
|
18
|
+
if (!servers?.length) {
|
|
19
|
+
const fallbackServer = createFallbackServer(options);
|
|
20
|
+
return fallbackServer ? [fallbackServer] : [];
|
|
21
|
+
}
|
|
22
|
+
// Handle invalid server array
|
|
23
|
+
if (!Array.isArray(servers)) {
|
|
24
|
+
return [];
|
|
25
|
+
}
|
|
26
|
+
// Process each server and filter out invalid ones
|
|
27
|
+
const validServers = servers.map((server) => processServerObject(server, options)).filter(isDefined);
|
|
28
|
+
// If all servers were invalid, provide a fallback
|
|
29
|
+
if (validServers.length === 0) {
|
|
30
|
+
const fallbackServer = createFallbackServer(options);
|
|
31
|
+
return fallbackServer ? [fallbackServer] : [];
|
|
32
|
+
}
|
|
33
|
+
return validServers;
|
|
18
34
|
}
|
|
35
|
+
/**
|
|
36
|
+
* Extracts the base URL (protocol + hostname) from a document URL.
|
|
37
|
+
* Returns undefined if the URL is invalid.
|
|
38
|
+
*/
|
|
19
39
|
function extractBaseUrlFromDocumentUrl(documentUrl) {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
40
|
+
try {
|
|
41
|
+
const url = new URL(documentUrl);
|
|
42
|
+
const port = url.port ? `:${url.port}` : '';
|
|
43
|
+
return `${url.protocol}//${url.hostname}${port}`;
|
|
44
|
+
}
|
|
45
|
+
catch {
|
|
46
|
+
return undefined;
|
|
47
|
+
}
|
|
27
48
|
}
|
|
49
|
+
/**
|
|
50
|
+
* Gets the fallback URL from window.location.origin if available.
|
|
51
|
+
*/
|
|
28
52
|
function getFallbackUrl() {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
53
|
+
if (typeof window === 'undefined' || typeof window?.location?.origin !== 'string') {
|
|
54
|
+
return undefined;
|
|
55
|
+
}
|
|
56
|
+
return window.location.origin;
|
|
33
57
|
}
|
|
58
|
+
/**
|
|
59
|
+
* Creates a server object from a URL string, with error handling.
|
|
60
|
+
*/
|
|
34
61
|
function createServerFromUrl(url, context) {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
62
|
+
try {
|
|
63
|
+
return serverSchema.parse({ url });
|
|
64
|
+
}
|
|
65
|
+
catch {
|
|
66
|
+
console.warn(`Failed to create server from ${context}:`, url);
|
|
67
|
+
return undefined;
|
|
68
|
+
}
|
|
41
69
|
}
|
|
70
|
+
/**
|
|
71
|
+
* Creates a default server using the document URL as the base.
|
|
72
|
+
*/
|
|
42
73
|
function createDefaultServerFromDocumentUrl(documentUrl) {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
74
|
+
const baseUrl = extractBaseUrlFromDocumentUrl(documentUrl);
|
|
75
|
+
if (!baseUrl) {
|
|
76
|
+
return undefined;
|
|
77
|
+
}
|
|
78
|
+
return createServerFromUrl(baseUrl, 'document URL');
|
|
48
79
|
}
|
|
80
|
+
/**
|
|
81
|
+
* Creates a default server using the fallback URL (window.location.origin).
|
|
82
|
+
*/
|
|
49
83
|
function createDefaultServerFromFallback() {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
84
|
+
const fallbackUrl = getFallbackUrl();
|
|
85
|
+
if (!fallbackUrl) {
|
|
86
|
+
return undefined;
|
|
87
|
+
}
|
|
88
|
+
return createServerFromUrl(fallbackUrl, 'fallback URL');
|
|
55
89
|
}
|
|
90
|
+
/**
|
|
91
|
+
* Resolves a relative server URL to an absolute URL using available base URLs.
|
|
92
|
+
* Uses a priority system: baseServerURL > documentUrl > fallbackUrl.
|
|
93
|
+
*/
|
|
56
94
|
function resolveRelativeServerUrl(serverUrl, options) {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
if (
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
95
|
+
const { baseServerURL, documentUrl } = options;
|
|
96
|
+
// Priority 1: Use provided base server URL
|
|
97
|
+
if (baseServerURL) {
|
|
98
|
+
return combineUrlAndPath(baseServerURL, serverUrl);
|
|
99
|
+
}
|
|
100
|
+
// Priority 2: Extract base URL from document URL
|
|
101
|
+
if (documentUrl) {
|
|
102
|
+
const baseUrl = extractBaseUrlFromDocumentUrl(documentUrl);
|
|
103
|
+
if (baseUrl) {
|
|
104
|
+
return combineUrlAndPath(baseUrl, serverUrl);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
// Priority 3: Use fallback URL (window.location.origin)
|
|
108
|
+
const fallbackUrl = getFallbackUrl();
|
|
109
|
+
if (fallbackUrl) {
|
|
110
|
+
return combineUrlAndPath(fallbackUrl, serverUrl);
|
|
111
|
+
}
|
|
112
|
+
// If no base URL is available, return the original URL
|
|
113
|
+
return serverUrl;
|
|
72
114
|
}
|
|
115
|
+
/**
|
|
116
|
+
* Processes a single server object, handling validation and URL resolution.
|
|
117
|
+
*/
|
|
73
118
|
function processServerObject(server, options) {
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
119
|
+
try {
|
|
120
|
+
const parsedServer = serverSchema.parse(server);
|
|
121
|
+
// Resolve relative URLs to absolute URLs
|
|
122
|
+
if (parsedServer.url?.startsWith('/')) {
|
|
123
|
+
parsedServer.url = resolveRelativeServerUrl(parsedServer.url, options);
|
|
124
|
+
}
|
|
125
|
+
return parsedServer;
|
|
126
|
+
}
|
|
127
|
+
catch (error) {
|
|
128
|
+
console.warn('Invalid server configuration:', server, 'Error:', error);
|
|
129
|
+
return undefined;
|
|
130
|
+
}
|
|
84
131
|
}
|
|
132
|
+
/**
|
|
133
|
+
* Creates a fallback server when no valid servers are available.
|
|
134
|
+
* Uses document URL first, then fallback URL.
|
|
135
|
+
*/
|
|
85
136
|
function createFallbackServer(options) {
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
137
|
+
// Priority 1: Try to create default server from document URL
|
|
138
|
+
if (options.documentUrl) {
|
|
139
|
+
const defaultServer = createDefaultServerFromDocumentUrl(options.documentUrl);
|
|
140
|
+
if (defaultServer) {
|
|
141
|
+
return defaultServer;
|
|
142
|
+
}
|
|
90
143
|
}
|
|
91
|
-
|
|
92
|
-
|
|
144
|
+
// Priority 2: Try to create default server from fallback URL
|
|
145
|
+
return createDefaultServerFromFallback();
|
|
93
146
|
}
|
|
94
|
-
export {
|
|
95
|
-
getServersFromDocument
|
|
96
|
-
};
|
|
97
|
-
//# sourceMappingURL=servers.js.map
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
//# sourceMappingURL=should-ignore-entity.js.map
|
|
1
|
+
/**
|
|
2
|
+
* Check if an entity should be ignored
|
|
3
|
+
*/
|
|
4
|
+
export const shouldIgnoreEntity = (data) => data?.['x-internal'] === true || data?.['x-scalar-ignore'] === true;
|
|
@@ -1,7 +1,15 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Ensure we update this every migration
|
|
3
|
+
*
|
|
4
|
+
* previous data versions:
|
|
5
|
+
* - undefined
|
|
6
|
+
* - 0.0.0
|
|
7
|
+
* - 2.1.0 - refactor
|
|
8
|
+
* - 2.2.0 - auth compliancy
|
|
9
|
+
* - 2.3.0 - environments
|
|
10
|
+
* - 2.4.0 - draft collection servers
|
|
11
|
+
* - 2.5.0 - accept header
|
|
12
|
+
*/
|
|
13
|
+
export const DATA_VERSION = '2.5.0';
|
|
14
|
+
/** The localStorage key under which the data version is stored */
|
|
15
|
+
export const DATA_VERSION_LS_LEY = 'scalar_api_client_data_version';
|
|
@@ -1,38 +1,40 @@
|
|
|
1
|
-
import { writeFile } from
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
11
|
-
import {
|
|
12
|
-
import {
|
|
13
|
-
console.warn(
|
|
14
|
-
console.info(
|
|
1
|
+
import { writeFile } from 'node:fs';
|
|
2
|
+
import { securitySchemeSchema } from '@scalar/types/entities';
|
|
3
|
+
import { createTypeAlias, printNode, zodToTs } from 'zod-to-ts';
|
|
4
|
+
import { cookieSchema } from '../entities/cookie/cookie.js';
|
|
5
|
+
import { environmentSchema } from '../entities/environment/environment.js';
|
|
6
|
+
import { collectionSchema } from '../entities/spec/collection.js';
|
|
7
|
+
import { requestExampleSchema } from '../entities/spec/request-examples.js';
|
|
8
|
+
import { requestSchema } from '../entities/spec/requests.js';
|
|
9
|
+
import { serverSchema } from '../entities/spec/server.js';
|
|
10
|
+
import { tagSchema } from '../entities/spec/spec-objects.js';
|
|
11
|
+
import { workspaceSchema } from '../entities/workspace/workspace.js';
|
|
12
|
+
import { DATA_VERSION } from '../migrations/data-version.js';
|
|
13
|
+
console.warn('Make sure the generate types file is updated for the current version');
|
|
14
|
+
console.info('Generating...');
|
|
15
15
|
const entities = [
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
16
|
+
{ identifier: 'Collection', schema: collectionSchema },
|
|
17
|
+
{ identifier: 'Cookie', schema: cookieSchema },
|
|
18
|
+
{ identifier: 'Environment', schema: environmentSchema },
|
|
19
|
+
{ identifier: 'Tag', schema: tagSchema },
|
|
20
|
+
{ identifier: 'RequestExample', schema: requestExampleSchema },
|
|
21
|
+
{ identifier: 'Request', schema: requestSchema },
|
|
22
|
+
{ identifier: 'SecurityScheme', schema: securitySchemeSchema },
|
|
23
|
+
{ identifier: 'Server', schema: serverSchema },
|
|
24
|
+
{ identifier: 'Workspace', schema: workspaceSchema },
|
|
25
25
|
];
|
|
26
|
-
|
|
27
|
-
|
|
26
|
+
/**
|
|
27
|
+
* Export the types in a namespace
|
|
28
|
+
* TODO:
|
|
29
|
+
* - go back to typescript compiler api and print pretty types as these ones are optional
|
|
30
|
+
*/
|
|
31
|
+
let typeString = entities.reduce((prev, { identifier, schema }) => {
|
|
28
32
|
const { node } = zodToTs(schema, identifier);
|
|
29
33
|
const typeAlias = createTypeAlias(node, identifier);
|
|
30
|
-
const nodeString =
|
|
34
|
+
const nodeString = ' export ' + printNode(typeAlias) + '\n\n';
|
|
31
35
|
return prev + nodeString;
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
`
|
|
35
|
-
);
|
|
36
|
+
}, `export namespace v_${DATA_VERSION.replace(/\./g, '_')} {\n`);
|
|
37
|
+
// Add all types data object
|
|
36
38
|
typeString += `
|
|
37
39
|
export type DataRecord = {
|
|
38
40
|
collections: Record<string, Collection>
|
|
@@ -47,10 +49,5 @@ typeString += `
|
|
|
47
49
|
}
|
|
48
50
|
}
|
|
49
51
|
`;
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
typeString,
|
|
53
|
-
{ flag: "w" },
|
|
54
|
-
(err) => err ? console.error(err) : console.log("Generation complete!")
|
|
55
|
-
);
|
|
56
|
-
//# sourceMappingURL=generate-types.js.map
|
|
52
|
+
// Write to file
|
|
53
|
+
writeFile(import.meta.dirname + `/v-${DATA_VERSION}/types.generated.ts`, typeString, { flag: 'w' }, (err) => err ? console.error(err) : console.log('Generation complete!'));
|
package/dist/migrations/index.js
CHANGED
|
@@ -1,18 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
transformLegacyDataToWorkspace
|
|
6
|
-
} from "./migrate-to-indexdb.js";
|
|
7
|
-
import { migrator } from "./migrator.js";
|
|
8
|
-
import { semverLessThan } from "./semver.js";
|
|
9
|
-
export {
|
|
10
|
-
DATA_VERSION,
|
|
11
|
-
DATA_VERSION_LS_LEY,
|
|
12
|
-
migrateLocalStorageToIndexDb,
|
|
13
|
-
migrator,
|
|
14
|
-
semverLessThan,
|
|
15
|
-
shouldMigrateToIndexDb,
|
|
16
|
-
transformLegacyDataToWorkspace
|
|
17
|
-
};
|
|
18
|
-
//# sourceMappingURL=index.js.map
|
|
1
|
+
export { DATA_VERSION, DATA_VERSION_LS_LEY } from './data-version.js';
|
|
2
|
+
export { migrateLocalStorageToIndexDb, shouldMigrateToIndexDb, transformLegacyDataToWorkspace, } from './migrate-to-indexdb.js';
|
|
3
|
+
export { migrator } from './migrator.js';
|
|
4
|
+
export { semverLessThan } from './semver.js';
|