@scalar/oas-utils 0.2.102 → 0.2.104

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 (43) hide show
  1. package/CHANGELOG.md +26 -0
  2. package/dist/entities/spec/index.d.ts +1 -0
  3. package/dist/entities/spec/index.d.ts.map +1 -1
  4. package/dist/entities/spec/index.js +2 -1
  5. package/dist/entities/spec/operation.d.ts +243 -0
  6. package/dist/entities/spec/operation.d.ts.map +1 -0
  7. package/dist/entities/spec/operation.js +9 -0
  8. package/dist/entities/spec/request-examples.d.ts +267 -55
  9. package/dist/entities/spec/request-examples.d.ts.map +1 -1
  10. package/dist/entities/spec/request-examples.js +43 -12
  11. package/dist/entities/spec/requests.d.ts +0 -2
  12. package/dist/entities/spec/requests.d.ts.map +1 -1
  13. package/dist/entities/workspace/workspace.d.ts +19 -0
  14. package/dist/entities/workspace/workspace.d.ts.map +1 -1
  15. package/dist/entities/workspace/workspace.js +11 -0
  16. package/dist/helpers/ensure-protocol.d.ts +3 -0
  17. package/dist/helpers/ensure-protocol.d.ts.map +1 -0
  18. package/dist/helpers/ensure-protocol.js +11 -0
  19. package/dist/helpers/fetchWithProxyFallback.d.ts +1 -1
  20. package/dist/helpers/fetchWithProxyFallback.d.ts.map +1 -1
  21. package/dist/helpers/index.d.ts +2 -1
  22. package/dist/helpers/index.d.ts.map +1 -1
  23. package/dist/helpers/index.js +2 -1
  24. package/dist/helpers/merge-urls.d.ts +15 -0
  25. package/dist/helpers/merge-urls.d.ts.map +1 -0
  26. package/dist/helpers/merge-urls.js +79 -0
  27. package/dist/helpers/redirectToProxy.d.ts.map +1 -1
  28. package/dist/helpers/redirectToProxy.js +3 -2
  29. package/dist/helpers/regexHelpers.d.ts +4 -0
  30. package/dist/helpers/regexHelpers.d.ts.map +1 -1
  31. package/dist/helpers/regexHelpers.js +4 -0
  32. package/dist/migrations/v-2.5.0/migration.d.ts.map +1 -1
  33. package/dist/migrations/v-2.5.0/migration.js +11 -0
  34. package/dist/spec-getters/getRequestBodyFromOperation.d.ts +4 -10
  35. package/dist/spec-getters/getRequestBodyFromOperation.d.ts.map +1 -1
  36. package/dist/spec-getters/getRequestBodyFromOperation.js +29 -57
  37. package/dist/transforms/import-spec.d.ts +1 -3
  38. package/dist/transforms/import-spec.d.ts.map +1 -1
  39. package/dist/transforms/import-spec.js +49 -11
  40. package/package.json +5 -7
  41. package/dist/helpers/concatenateUrlAndPath.d.ts +0 -5
  42. package/dist/helpers/concatenateUrlAndPath.d.ts.map +0 -1
  43. package/dist/helpers/concatenateUrlAndPath.js +0 -17
@@ -16,9 +16,7 @@ export declare const parseSchema: (spec: string | UnknownObject, { shouldLoad }?
16
16
  errors: import("@scalar/openapi-parser").ErrorObject[];
17
17
  }>;
18
18
  /** Converts selected security requirements to uids */
19
- export declare const getSelectedSecuritySchemeUids: (securityRequirements: SelectedSecuritySchemeUids, authentication: {
20
- preferredSecurityScheme?: string | null;
21
- } | undefined, securitySchemeMap: Record<string, string>) => SelectedSecuritySchemeUids;
19
+ export declare const getSelectedSecuritySchemeUids: (securityRequirements: SelectedSecuritySchemeUids, authentication: ReferenceConfiguration["authentication"] | undefined, securitySchemeMap: Record<string, string>) => SelectedSecuritySchemeUids;
22
20
  export type ImportSpecToWorkspaceArgs = Pick<CollectionPayload, 'documentUrl' | 'watchMode'> & Pick<ReferenceConfiguration, 'authentication' | 'baseServerURL' | 'servers'> & {
23
21
  /** Sets the preferred security scheme on the collection instead of the requests */
24
22
  setCollectionSecurity?: boolean;
@@ -1 +1 @@
1
- {"version":3,"file":"import-spec.d.ts","sourceRoot":"","sources":["../../src/transforms/import-spec.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,0BAA0B,EAAE,MAAM,2BAA2B,CAAA;AAC3E,OAAO,EACL,KAAK,UAAU,EACf,KAAK,iBAAiB,EACtB,KAAK,OAAO,EACZ,KAAK,cAAc,EAGnB,KAAK,MAAM,EACX,KAAK,GAAG,EAMT,MA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BAAyB,GAAG,IAAI,CAC1C,iBAAiB,EACjB,aAAa,GAAG,WAAW,CAC5B,GACC,IAAI,CACF,sBAAsB,EACtB,gBAAgB,GAAG,eAAe,GAAG,SAAS,CAC/C,GAAG;IACF,mFAAmF;IACnF,qBAAqB,CAAC,EAAE,OAAO,CAAA;IAC/B,yCAAyC;IACzC,UAAU,CAAC,EAAE,OAAO,CAAA;CACrB,CAAA;AAEH;;;;;;;;;;;;GAYG;AACH,wBAAsB,qBAAqB,CACzC,IAAI,EAAE,MAAM,GAAG,aAAa,EAC5B,EACE,cAAc,EACd,aAAa,EACb,WAAW,EACX,OAAO,EAAE,iBAAiB,EAC1B,qBAA6B,EAC7B,UAAU,EACV,SAAiB,GAClB,GAAE,yBAA8B,GAChC,OAAO,CACN;IACE,KAAK,EAAE,KAAK,CAAA;IACZ,UAAU,EAAE,UAAU,CAAA;IACtB,QAAQ,EAAE,OAAO,EAAE,CAAA;IACnB,MAAM,EAAE,WAAW,CAAC,QAAQ,CAAA;IAC5B,QAAQ,EAAE,cAAc,EAAE,CAAA;IAC1B,OAAO,EAAE,MAAM,EAAE,CAAA;IACjB,IAAI,EAAE,GAAG,EAAE,CAAA;IACX,eAAe,EAAE,cAAc,EAAE,CAAA;CAClC,GACD;IAAE,KAAK,EAAE,IAAI,CAAC;IAAC,cAAc,EAAE,MAAM,EAAE,CAAA;CAAE,CAC5C,CA6UA;AAED;;GAEG;AACH,wBAAgB,6BAA6B,CAC3C,OAAO,EAAE,WAAW,CAAC,YAAY,EAAE,GAAG,SAAS,EAC/C,EAAE,aAAa,EAAE,GAAE,IAAI,CAAC,sBAAsB,EAAE,eAAe,CAAM,GACpE,MAAM,EAAE,CA8CV"}
@@ -9,7 +9,7 @@ import { requestSchema } from '../entities/spec/requests.js';
9
9
  import { tagSchema } from '../entities/spec/spec-objects.js';
10
10
  import { createExampleFromRequest } from '../entities/spec/request-examples.js';
11
11
  import { collectionSchema } from '../entities/spec/collection.js';
12
- import { concatenateUrlAndPath } from '../helpers/concatenateUrlAndPath.js';
12
+ import { combineUrlAndPath } from '../helpers/merge-urls.js';
13
13
 
14
14
  /** Takes a string or object and parses it into an openapi spec compliant schema */
15
15
  const parseSchema = async (spec, { shouldLoad = true } = {}) => {
@@ -52,14 +52,31 @@ const parseSchema = async (spec, { shouldLoad = true } = {}) => {
52
52
  };
53
53
  /** Converts selected security requirements to uids */
54
54
  const getSelectedSecuritySchemeUids = (securityRequirements, authentication, securitySchemeMap) => {
55
- const name = authentication?.preferredSecurityScheme &&
56
- securityRequirements.includes(authentication.preferredSecurityScheme)
57
- ? authentication.preferredSecurityScheme
58
- : securityRequirements[0];
59
- const uids = Array.isArray(name)
55
+ // Filter the preferred security schemes to only include the ones that are in the security requirements
56
+ const preferredSecurityNames = [
57
+ authentication?.preferredSecurityScheme ?? [],
58
+ ]
59
+ .flat()
60
+ .filter((name) => {
61
+ // Match up complex security requirements, array to array
62
+ if (Array.isArray(name)) {
63
+ // We match every element in the array
64
+ return securityRequirements.some((r) => Array.isArray(r) &&
65
+ r.length === name.length &&
66
+ r.every((v, i) => v === name[i]));
67
+ }
68
+ else
69
+ return securityRequirements.includes(name);
70
+ });
71
+ // Set the first security requirement if no preferred security schemes are set
72
+ if (!preferredSecurityNames.length && securityRequirements[0]) {
73
+ preferredSecurityNames.push(securityRequirements[0]);
74
+ }
75
+ // Map names to uids
76
+ const uids = preferredSecurityNames.map((name) => Array.isArray(name)
60
77
  ? name.map((k) => securitySchemeMap[k])
61
- : securitySchemeMap[name];
62
- return [uids];
78
+ : securitySchemeMap[name]);
79
+ return uids;
63
80
  };
64
81
  /**
65
82
  * Imports an OpenAPI document and converts it to workspace entities (Collection, Request, Server, etc.)
@@ -87,6 +104,13 @@ async function importSpecToWorkspace(spec, { authentication, baseServerURL, docu
87
104
  const servers = getServersFromOpenApiDocument(configuredServers || schema.servers, {
88
105
  baseServerURL,
89
106
  });
107
+ // Fallback to the current window.location.origin if no servers are provided
108
+ if (!servers.length) {
109
+ const fallbackUrl = getFallbackUrl();
110
+ if (fallbackUrl) {
111
+ servers.push(serverSchema.parse({ url: fallbackUrl }));
112
+ }
113
+ }
90
114
  /**
91
115
  * List of all tag strings. For non compliant specs we may need to
92
116
  * add top level tag objects for missing tag objects
@@ -196,6 +220,7 @@ async function importSpecToWorkspace(spec, { authentication, baseServerURL, docu
196
220
  ...operationWithoutSecurity,
197
221
  method,
198
222
  path: pathString,
223
+ security: operationSecurity,
199
224
  selectedSecuritySchemeUids,
200
225
  // Merge path and operation level parameters
201
226
  parameters: [
@@ -338,12 +363,13 @@ function getServersFromOpenApiDocument(servers, { baseServerURL } = {}) {
338
363
  if (parsedSchema?.url?.startsWith('/')) {
339
364
  // Use the base server URL (if provided)
340
365
  if (baseServerURL) {
341
- parsedSchema.url = concatenateUrlAndPath(baseServerURL?.replace(/\/$/, ''), parsedSchema.url.replace(/^\//, ''));
366
+ parsedSchema.url = combineUrlAndPath(baseServerURL, parsedSchema.url);
342
367
  return parsedSchema;
343
368
  }
344
369
  // Fallback to the current window origin
345
- if (typeof window?.location?.origin === 'string') {
346
- parsedSchema.url = concatenateUrlAndPath(window.location.origin, parsedSchema.url.replace(/^\//, ''));
370
+ const fallbackUrl = getFallbackUrl();
371
+ if (fallbackUrl) {
372
+ parsedSchema.url = combineUrlAndPath(fallbackUrl, parsedSchema.url.replace(/^\//, ''));
347
373
  return parsedSchema;
348
374
  }
349
375
  }
@@ -360,5 +386,17 @@ function getServersFromOpenApiDocument(servers, { baseServerURL } = {}) {
360
386
  })
361
387
  .filter(isDefined);
362
388
  }
389
+ /**
390
+ * Fallback to the current window.location.origin, if available
391
+ */
392
+ function getFallbackUrl() {
393
+ if (typeof window === 'undefined') {
394
+ return undefined;
395
+ }
396
+ if (typeof window?.location?.origin !== 'string') {
397
+ return undefined;
398
+ }
399
+ return window.location.origin;
400
+ }
363
401
 
364
402
  export { getSelectedSecuritySchemeUids, getServersFromOpenApiDocument, importSpecToWorkspace, parseSchema };
package/package.json CHANGED
@@ -16,7 +16,7 @@
16
16
  "specification",
17
17
  "yaml"
18
18
  ],
19
- "version": "0.2.102",
19
+ "version": "0.2.104",
20
20
  "engines": {
21
21
  "node": ">=18"
22
22
  },
@@ -127,9 +127,9 @@
127
127
  "yaml": "^2.4.5",
128
128
  "zod": "^3.23.8",
129
129
  "@scalar/object-utils": "1.1.12",
130
- "@scalar/themes": "0.9.64",
131
130
  "@scalar/openapi-types": "0.1.7",
132
- "@scalar/types": "0.0.30"
131
+ "@scalar/themes": "0.9.66",
132
+ "@scalar/types": "0.0.32"
133
133
  },
134
134
  "devDependencies": {
135
135
  "type-fest": "^4.20.0",
@@ -137,15 +137,13 @@
137
137
  "vitest": "^1.6.0",
138
138
  "zod-to-ts": "github:amritk/zod-to-ts#build",
139
139
  "@scalar/build-tooling": "0.1.12",
140
- "@scalar/openapi-parser": "0.10.4",
141
- "@scalar/openapi-types": "0.1.7"
140
+ "@scalar/openapi-types": "0.1.7",
141
+ "@scalar/openapi-parser": "0.10.6"
142
142
  },
143
143
  "scripts": {
144
144
  "build": "scalar-build-rollup",
145
- "dev": "vite",
146
145
  "lint:check": "eslint .",
147
146
  "lint:fix": "eslint . --fix",
148
- "preview": "vite preview",
149
147
  "test": "vitest",
150
148
  "test:unit": "vitest .",
151
149
  "typegen:migration": "vite-node ./src/migrations/generate-types.ts",
@@ -1,5 +0,0 @@
1
- /**
2
- * Make sure the URL and path are concatenated correctly.
3
- */
4
- export declare const concatenateUrlAndPath: (url: string, path?: string) => string;
5
- //# sourceMappingURL=concatenateUrlAndPath.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"concatenateUrlAndPath.d.ts","sourceRoot":"","sources":["../../src/helpers/concatenateUrlAndPath.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,eAAO,MAAM,qBAAqB,QAAS,MAAM,SAAS,MAAM,WAc/D,CAAA"}
@@ -1,17 +0,0 @@
1
- /**
2
- * Make sure the URL and path are concatenated correctly.
3
- */
4
- const concatenateUrlAndPath = (url, path) => {
5
- if (typeof path !== 'string' || !path.length) {
6
- return url;
7
- }
8
- const trimmedUrl = url.trim();
9
- const trimmedPath = path.trim();
10
- const urlWithSlash = trimmedUrl.endsWith('/') ? trimmedUrl : `${trimmedUrl}/`;
11
- const pathWithoutSlash = trimmedPath.startsWith('/')
12
- ? trimmedPath.slice(1)
13
- : trimmedPath;
14
- return [urlWithSlash, pathWithoutSlash].join('');
15
- };
16
-
17
- export { concatenateUrlAndPath };