@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.
Files changed (129) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/dist/entities/cookie/cookie.js +11 -15
  3. package/dist/entities/cookie/index.js +1 -5
  4. package/dist/entities/environment/environment.js +7 -11
  5. package/dist/entities/environment/index.js +1 -5
  6. package/dist/entities/hotkeys/hotkeys.js +116 -111
  7. package/dist/entities/hotkeys/index.js +1 -6
  8. package/dist/entities/shared/index.js +1 -7
  9. package/dist/entities/shared/utility.js +9 -9
  10. package/dist/entities/spec/collection.js +91 -89
  11. package/dist/entities/spec/index.js +10 -59
  12. package/dist/entities/spec/operation.js +6 -6
  13. package/dist/entities/spec/parameters.js +38 -38
  14. package/dist/entities/spec/request-examples.js +421 -331
  15. package/dist/entities/spec/requests.js +102 -84
  16. package/dist/entities/spec/server.js +61 -46
  17. package/dist/entities/spec/spec-objects.js +121 -76
  18. package/dist/entities/spec/x-scalar-environments.js +18 -20
  19. package/dist/entities/spec/x-scalar-secrets.js +6 -8
  20. package/dist/entities/workspace/index.js +1 -7
  21. package/dist/entities/workspace/workspace.js +47 -46
  22. package/dist/helpers/client-plugins.js +13 -13
  23. package/dist/helpers/fetch-document.js +30 -25
  24. package/dist/helpers/fetch-with-proxy-fallback.js +26 -21
  25. package/dist/helpers/index.d.ts +1 -2
  26. package/dist/helpers/index.d.ts.map +1 -1
  27. package/dist/helpers/index.js +80 -119
  28. package/dist/helpers/normalize-mime-type-object.js +19 -18
  29. package/dist/helpers/normalize-mime-type.js +11 -9
  30. package/dist/helpers/operation-stability.js +25 -20
  31. package/dist/helpers/parse.d.ts +0 -4
  32. package/dist/helpers/parse.d.ts.map +1 -1
  33. package/dist/helpers/parse.js +77 -77
  34. package/dist/helpers/schema-model.js +13 -16
  35. package/dist/helpers/security/get-schemes.js +7 -8
  36. package/dist/helpers/security/has-token.js +18 -19
  37. package/dist/helpers/security/index.js +2 -7
  38. package/dist/helpers/servers.js +128 -79
  39. package/dist/helpers/should-ignore-entity.js +4 -5
  40. package/dist/migrations/data-version.js +15 -7
  41. package/dist/migrations/generate-types.js +34 -37
  42. package/dist/migrations/index.js +4 -18
  43. package/dist/migrations/local-storage.js +31 -29
  44. package/dist/migrations/migrate-to-indexdb.js +706 -529
  45. package/dist/migrations/migrator.js +58 -54
  46. package/dist/migrations/semver.js +24 -24
  47. package/dist/migrations/v-0.0.0/types.generated.js +1 -1
  48. package/dist/migrations/v-2.1.0/migration.js +272 -258
  49. package/dist/migrations/v-2.1.0/types.generated.js +1 -1
  50. package/dist/migrations/v-2.2.0/migration.js +99 -96
  51. package/dist/migrations/v-2.2.0/types.generated.js +1 -1
  52. package/dist/migrations/v-2.3.0/migration.js +45 -48
  53. package/dist/migrations/v-2.3.0/types.generated.js +1 -1
  54. package/dist/migrations/v-2.4.0/migration.js +25 -25
  55. package/dist/migrations/v-2.4.0/types.generated.js +1 -1
  56. package/dist/migrations/v-2.5.0/migration.js +122 -139
  57. package/dist/migrations/v-2.5.0/types.generated.js +1 -1
  58. package/dist/spec-getters/get-example-from-schema.js +489 -385
  59. package/dist/spec-getters/get-parameters-from-operation.js +39 -23
  60. package/dist/spec-getters/get-request-body-from-operation.d.ts.map +1 -1
  61. package/dist/spec-getters/get-request-body-from-operation.js +168 -126
  62. package/dist/spec-getters/get-server-variable-examples.js +10 -13
  63. package/dist/spec-getters/index.js +4 -11
  64. package/dist/transforms/import-spec.js +381 -291
  65. package/dist/transforms/index.js +1 -11
  66. package/package.json +15 -19
  67. package/dist/entities/cookie/cookie.js.map +0 -7
  68. package/dist/entities/cookie/index.js.map +0 -7
  69. package/dist/entities/environment/environment.js.map +0 -7
  70. package/dist/entities/environment/index.js.map +0 -7
  71. package/dist/entities/hotkeys/hotkeys.js.map +0 -7
  72. package/dist/entities/hotkeys/index.js.map +0 -7
  73. package/dist/entities/shared/index.js.map +0 -7
  74. package/dist/entities/shared/utility.js.map +0 -7
  75. package/dist/entities/spec/collection.js.map +0 -7
  76. package/dist/entities/spec/index.js.map +0 -7
  77. package/dist/entities/spec/operation.js.map +0 -7
  78. package/dist/entities/spec/parameters.js.map +0 -7
  79. package/dist/entities/spec/request-examples.js.map +0 -7
  80. package/dist/entities/spec/requests.js.map +0 -7
  81. package/dist/entities/spec/server.js.map +0 -7
  82. package/dist/entities/spec/spec-objects.js.map +0 -7
  83. package/dist/entities/spec/x-scalar-environments.js.map +0 -7
  84. package/dist/entities/spec/x-scalar-secrets.js.map +0 -7
  85. package/dist/entities/workspace/index.js.map +0 -7
  86. package/dist/entities/workspace/workspace.js.map +0 -7
  87. package/dist/helpers/client-plugins.js.map +0 -7
  88. package/dist/helpers/fetch-document.js.map +0 -7
  89. package/dist/helpers/fetch-with-proxy-fallback.js.map +0 -7
  90. package/dist/helpers/index.js.map +0 -7
  91. package/dist/helpers/normalize-mime-type-object.js.map +0 -7
  92. package/dist/helpers/normalize-mime-type.js.map +0 -7
  93. package/dist/helpers/operation-stability.js.map +0 -7
  94. package/dist/helpers/parse.js.map +0 -7
  95. package/dist/helpers/pretty-print-json.d.ts +0 -9
  96. package/dist/helpers/pretty-print-json.d.ts.map +0 -1
  97. package/dist/helpers/pretty-print-json.js +0 -38
  98. package/dist/helpers/pretty-print-json.js.map +0 -7
  99. package/dist/helpers/schema-model.js.map +0 -7
  100. package/dist/helpers/security/get-schemes.js.map +0 -7
  101. package/dist/helpers/security/has-token.js.map +0 -7
  102. package/dist/helpers/security/index.js.map +0 -7
  103. package/dist/helpers/servers.js.map +0 -7
  104. package/dist/helpers/should-ignore-entity.js.map +0 -7
  105. package/dist/migrations/data-version.js.map +0 -7
  106. package/dist/migrations/generate-types.js.map +0 -7
  107. package/dist/migrations/index.js.map +0 -7
  108. package/dist/migrations/local-storage.js.map +0 -7
  109. package/dist/migrations/migrate-to-indexdb.js.map +0 -7
  110. package/dist/migrations/migrator.js.map +0 -7
  111. package/dist/migrations/semver.js.map +0 -7
  112. package/dist/migrations/v-0.0.0/types.generated.js.map +0 -7
  113. package/dist/migrations/v-2.1.0/migration.js.map +0 -7
  114. package/dist/migrations/v-2.1.0/types.generated.js.map +0 -7
  115. package/dist/migrations/v-2.2.0/migration.js.map +0 -7
  116. package/dist/migrations/v-2.2.0/types.generated.js.map +0 -7
  117. package/dist/migrations/v-2.3.0/migration.js.map +0 -7
  118. package/dist/migrations/v-2.3.0/types.generated.js.map +0 -7
  119. package/dist/migrations/v-2.4.0/migration.js.map +0 -7
  120. package/dist/migrations/v-2.4.0/types.generated.js.map +0 -7
  121. package/dist/migrations/v-2.5.0/migration.js.map +0 -7
  122. package/dist/migrations/v-2.5.0/types.generated.js.map +0 -7
  123. package/dist/spec-getters/get-example-from-schema.js.map +0 -7
  124. package/dist/spec-getters/get-parameters-from-operation.js.map +0 -7
  125. package/dist/spec-getters/get-request-body-from-operation.js.map +0 -7
  126. package/dist/spec-getters/get-server-variable-examples.js.map +0 -7
  127. package/dist/spec-getters/index.js.map +0 -7
  128. package/dist/transforms/import-spec.js.map +0 -7
  129. package/dist/transforms/index.js.map +0 -7
@@ -1,83 +1,83 @@
1
- import { parse, stringify } from "yaml";
2
- const yaml = {
3
- /** Parse and throw if the return value is not an object */
4
- parse: (val) => {
5
- const yamlObject = parse(val, { merge: true, maxAliasCount: 1e4 });
6
- if (typeof yamlObject !== "object") {
7
- throw Error("Invalid YAML object");
8
- }
9
- return yamlObject;
10
- },
11
- /** Parse and return a fallback on failure */
12
- parseSafe(val, fallback) {
13
- try {
14
- return yaml.parse(val);
15
- } catch (err) {
16
- return typeof fallback === "function" ? fallback(err) : fallback;
17
- }
18
- },
19
- stringify
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
- const json = {
22
- /** Parse and throw if the return value is not an object */
23
- parse: (val) => {
24
- const jsonObject = JSON.parse(val);
25
- if (typeof jsonObject !== "object") {
26
- throw Error("Invalid JSON object");
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
- return json.parse(val);
34
- } catch (err) {
35
- return typeof fallback === "function" ? fallback(err) : fallback;
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
- const parseJsonOrYaml = (value) => {
61
- if (typeof value !== "string") {
62
- return value;
63
- }
64
- const jsonObject = json.parseSafe(value, null);
65
- if (jsonObject) {
66
- return jsonObject;
67
- }
68
- if (value.length > 0 && ["{", "["].includes(value[0] ?? "")) {
69
- throw Error("Invalid JSON or YAML");
70
- }
71
- return yaml.parseSafe(value, (err) => {
72
- throw Error(err);
73
- });
74
- };
75
- export {
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
- function schemaModel(data, schema, throwError = true, errorLocation) {
2
- const result = schema.safeParse(data);
3
- if (!result.success) {
4
- console.group("Schema Error" + (errorLocation ? ` - ${errorLocation}` : ""));
5
- console.warn(JSON.stringify(result.error.format(), null, 2));
6
- console.log("Received: ", data);
7
- console.groupEnd();
8
- }
9
- if (throwError && !result.success) {
10
- throw new Error("Zod validation failure");
11
- }
12
- return result.data;
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 "@scalar/helpers/array/is-defined";
2
- const getSchemes = (selectedSecuritySchemes, securitySchemes) => {
3
- const uids = new Set(selectedSecuritySchemes.flat());
4
- return Array.from(uids).map((uid) => securitySchemes[uid]).filter(isDefined);
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
- const hasToken = (scheme) => {
2
- if (scheme.type === "apiKey") {
3
- return Boolean(scheme.value);
4
- }
5
- if (scheme.type === "http") {
6
- return Boolean(
7
- scheme.scheme === "bearer" && scheme.token || scheme.scheme === "basic" && scheme.username && scheme.password
8
- );
9
- }
10
- if (scheme.type === "oauth2") {
11
- return Boolean(
12
- scheme.flows.authorizationCode?.token || scheme.flows.clientCredentials?.token || scheme.flows.password?.token || scheme.flows.implicit?.token
13
- );
14
- }
15
- return false;
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
@@ -1,7 +1,2 @@
1
- import { getSchemes } from "./get-schemes.js";
2
- import { hasToken } from "./has-token.js";
3
- export {
4
- getSchemes,
5
- hasToken
6
- };
7
- //# sourceMappingURL=index.js.map
1
+ export { getSchemes } from './get-schemes.js';
2
+ export { hasToken } from './has-token.js';
@@ -1,97 +1,146 @@
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
- function getServersFromDocument(servers, options = {}) {
5
- if (!servers?.length) {
6
- const fallbackServer = createFallbackServer(options);
7
- return fallbackServer ? [fallbackServer] : [];
8
- }
9
- if (!Array.isArray(servers)) {
10
- return [];
11
- }
12
- const validServers = servers.map((server) => processServerObject(server, options)).filter(isDefined);
13
- if (validServers.length === 0) {
14
- const fallbackServer = createFallbackServer(options);
15
- return fallbackServer ? [fallbackServer] : [];
16
- }
17
- return validServers;
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
- try {
21
- const url = new URL(documentUrl);
22
- const port = url.port ? `:${url.port}` : "";
23
- return `${url.protocol}//${url.hostname}${port}`;
24
- } catch {
25
- return void 0;
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
- if (typeof window === "undefined" || typeof window?.location?.origin !== "string") {
30
- return void 0;
31
- }
32
- return window.location.origin;
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
- try {
36
- return serverSchema.parse({ url });
37
- } catch {
38
- console.warn(`Failed to create server from ${context}:`, url);
39
- return void 0;
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
- const baseUrl = extractBaseUrlFromDocumentUrl(documentUrl);
44
- if (!baseUrl) {
45
- return void 0;
46
- }
47
- return createServerFromUrl(baseUrl, "document URL");
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
- const fallbackUrl = getFallbackUrl();
51
- if (!fallbackUrl) {
52
- return void 0;
53
- }
54
- return createServerFromUrl(fallbackUrl, "fallback URL");
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
- const { baseServerURL, documentUrl } = options;
58
- if (baseServerURL) {
59
- return combineUrlAndPath(baseServerURL, serverUrl);
60
- }
61
- if (documentUrl) {
62
- const baseUrl = extractBaseUrlFromDocumentUrl(documentUrl);
63
- if (baseUrl) {
64
- return combineUrlAndPath(baseUrl, serverUrl);
65
- }
66
- }
67
- const fallbackUrl = getFallbackUrl();
68
- if (fallbackUrl) {
69
- return combineUrlAndPath(fallbackUrl, serverUrl);
70
- }
71
- return serverUrl;
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
- try {
75
- const parsedServer = serverSchema.parse(server);
76
- if (parsedServer.url?.startsWith("/")) {
77
- parsedServer.url = resolveRelativeServerUrl(parsedServer.url, options);
78
- }
79
- return parsedServer;
80
- } catch (error) {
81
- console.warn("Invalid server configuration:", server, "Error:", error);
82
- return void 0;
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
- if (options.documentUrl) {
87
- const defaultServer = createDefaultServerFromDocumentUrl(options.documentUrl);
88
- if (defaultServer) {
89
- return defaultServer;
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
- return createDefaultServerFromFallback();
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
- const shouldIgnoreEntity = (data) => data?.["x-internal"] === true || data?.["x-scalar-ignore"] === true;
2
- export {
3
- shouldIgnoreEntity
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
- const DATA_VERSION = "2.5.0";
2
- const DATA_VERSION_LS_LEY = "scalar_api_client_data_version";
3
- export {
4
- DATA_VERSION,
5
- DATA_VERSION_LS_LEY
6
- };
7
- //# sourceMappingURL=data-version.js.map
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 "node:fs";
2
- import { cookieSchema } from "../entities/cookie/cookie.js";
3
- import { environmentSchema } from "../entities/environment/environment.js";
4
- import { collectionSchema } from "../entities/spec/collection.js";
5
- import { requestExampleSchema } from "../entities/spec/request-examples.js";
6
- import { requestSchema } from "../entities/spec/requests.js";
7
- import { serverSchema } from "../entities/spec/server.js";
8
- import { tagSchema } from "../entities/spec/spec-objects.js";
9
- import { workspaceSchema } from "../entities/workspace/workspace.js";
10
- import { DATA_VERSION } from "../migrations/data-version.js";
11
- import { securitySchemeSchema } from "@scalar/types/entities";
12
- import { createTypeAlias, printNode, zodToTs } from "zod-to-ts";
13
- console.warn("Make sure the generate types file is updated for the current version");
14
- console.info("Generating...");
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
- { 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 }
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
- let typeString = entities.reduce(
27
- (prev, { identifier, schema }) => {
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 = " export " + printNode(typeAlias) + "\n\n";
34
+ const nodeString = ' export ' + printNode(typeAlias) + '\n\n';
31
35
  return prev + nodeString;
32
- },
33
- `export namespace v_${DATA_VERSION.replace(/\./g, "_")} {
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
- writeFile(
51
- __dirname + `/v-${DATA_VERSION}/types.generated.ts`,
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!'));
@@ -1,18 +1,4 @@
1
- import { DATA_VERSION, DATA_VERSION_LS_LEY } from "./data-version.js";
2
- import {
3
- migrateLocalStorageToIndexDb,
4
- shouldMigrateToIndexDb,
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';