@scalar/oas-utils 0.2.48 → 0.2.50

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 CHANGED
@@ -1,5 +1,17 @@
1
1
  # @scalar/oas-utils
2
2
 
3
+ ## 0.2.50
4
+
5
+ ### Patch Changes
6
+
7
+ - 9057781: fix: add missing securityDefinitions check on import
8
+
9
+ ## 0.2.49
10
+
11
+ ### Patch Changes
12
+
13
+ - cd8ba1c: feat: added setting of initial security schemes in the client
14
+
3
15
  ## 0.2.48
4
16
 
5
17
  ### Patch Changes
@@ -1,5 +1,6 @@
1
1
  import { type Collection, type Request, type RequestExample, type Server, type Tag } from '../entities/spec/index.js';
2
2
  import { type SecurityScheme } from '../entities/spec/security.js';
3
+ import type { AuthenticationState } from '@scalar/types/legacy';
3
4
  import type { UnknownObject } from '@scalar/types/utils';
4
5
  /**
5
6
  * Import an OpenAPI spec file and convert it to workspace entities
@@ -9,7 +10,7 @@ import type { UnknownObject } from '@scalar/types/utils';
9
10
  * created and used at various levels we will index via the uids to create
10
11
  * the relationships
11
12
  */
12
- export declare function importSpecToWorkspace(spec: string | UnknownObject): Promise<{
13
+ export declare function importSpecToWorkspace(spec: string | UnknownObject, preferredSecurityScheme?: AuthenticationState['preferredSecurityScheme']): Promise<{
13
14
  error: false;
14
15
  collection: Collection;
15
16
  requests: Request[];
@@ -1 +1 @@
1
- {"version":3,"file":"import-spec.d.ts","sourceRoot":"","sources":["../../src/transforms/import-spec.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,UAAU,EACf,KAAK,OAAO,EACZ,KAAK,cAAc,EAGnB,KAAK,MAAM,EACX,KAAK,GAAG,EAOT,MAAM,iBAAiB,CAAA;AACxB,OAAO,EACL,KAAK,cAAc,EAGpB,MAAM,0BAA0B,CAAA;AAKjC,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAA;AAiCxD;;;;;;;GAOG;AACH,wB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AAc,EAAE,MAAM,EAAE,CAAA;CAAE,CAC5C,CAkQA"}
@@ -40,7 +40,7 @@ const convertOauth2Flows = (security, nameKey) => {
40
40
  * created and used at various levels we will index via the uids to create
41
41
  * the relationships
42
42
  */
43
- async function importSpecToWorkspace(spec) {
43
+ async function importSpecToWorkspace(spec, preferredSecurityScheme) {
44
44
  const { filesystem } = await load(spec);
45
45
  const { specification } = upgrade(filesystem);
46
46
  const { schema: _schema, errors = [] } = await dereference(specification);
@@ -66,7 +66,7 @@ async function importSpecToWorkspace(spec) {
66
66
  const tagNames = new Set();
67
67
  // ---------------------------------------------------------------------------
68
68
  // SECURITY HANDLING
69
- const security = schema.components?.securitySchemes ?? {};
69
+ const security = schema.components?.securitySchemes ?? schema?.securityDefinitions ?? {};
70
70
  const securitySchemes = Object.entries(security)
71
71
  .map?.(([nameKey, s]) => {
72
72
  const scheme = schemaModel(
@@ -107,12 +107,32 @@ async function importSpecToWorkspace(spec) {
107
107
  // TODO: make sure we add any loose requests with no tags to the collection children
108
108
  operation.tags?.forEach((t) => tagNames.add(t));
109
109
  // Remove security here and add it correctly below
110
- const { security: removed, ...operationWithoutSecurity } = operation;
110
+ const { security: operationSecurity, ...operationWithoutSecurity } = operation;
111
+ // Grab the security requirements for this operation
112
+ const securityRequirements = (operationSecurity ??
113
+ schema.security ??
114
+ []).flatMap((s) => {
115
+ const keys = Object.keys(s);
116
+ if (keys.length)
117
+ return keys[0];
118
+ else
119
+ return [];
120
+ });
121
+ let selectedSecuritySchemeUids = [];
122
+ // Set the initially selected security scheme
123
+ if (securityRequirements.length) {
124
+ const name = preferredSecurityScheme &&
125
+ securityRequirements.includes(preferredSecurityScheme ?? '')
126
+ ? preferredSecurityScheme
127
+ : securityRequirements[0];
128
+ const uid = securitySchemeMap[name];
129
+ selectedSecuritySchemeUids = [uid];
130
+ }
111
131
  const requestPayload = {
112
132
  ...operationWithoutSecurity,
113
133
  method,
114
134
  path: pathString,
115
- selectedSecuritySchemeUids: [],
135
+ selectedSecuritySchemeUids,
116
136
  // Merge path and operation level parameters
117
137
  parameters: [
118
138
  ...(path?.parameters ?? []),
@@ -122,8 +142,8 @@ async function importSpecToWorkspace(spec) {
122
142
  };
123
143
  // Add list of UIDs to associate security schemes
124
144
  // As per the spec if there is operation level security we ignore the top level requirements
125
- if (operation.security?.length)
126
- requestPayload.security = operation.security?.map((s) => {
145
+ if (operationSecurity?.length)
146
+ requestPayload.security = operationSecurity.map((s) => {
127
147
  const keys = Object.keys(s);
128
148
  // Handle the case of {} for optional
129
149
  if (keys.length) {
package/package.json CHANGED
@@ -16,7 +16,7 @@
16
16
  "specification",
17
17
  "yaml"
18
18
  ],
19
- "version": "0.2.48",
19
+ "version": "0.2.50",
20
20
  "engines": {
21
21
  "node": ">=18"
22
22
  },