@scalar/oas-utils 0.2.77 → 0.2.78
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 +11 -0
- package/dist/entities/spec/collection.d.ts +0 -278
- package/dist/entities/spec/collection.d.ts.map +1 -1
- package/dist/entities/spec/collection.js +1 -8
- package/dist/entities/spec/index.js +1 -1
- package/dist/entities/spec/requests.d.ts +2 -2
- package/dist/entities/spec/security.d.ts +1123 -783
- package/dist/entities/spec/security.d.ts.map +1 -1
- package/dist/entities/spec/security.js +73 -133
- package/dist/entities/workspace/workspace.d.ts +3 -7
- package/dist/entities/workspace/workspace.d.ts.map +1 -1
- package/dist/entities/workspace/workspace.js +0 -2
- package/dist/migrations/data-version.d.ts +3 -2
- package/dist/migrations/data-version.d.ts.map +1 -1
- package/dist/migrations/data-version.js +3 -2
- package/dist/migrations/local-storage.d.ts.map +1 -1
- package/dist/migrations/local-storage.js +3 -5
- package/dist/migrations/migrator.d.ts +2 -2
- package/dist/migrations/migrator.d.ts.map +1 -1
- package/dist/migrations/migrator.js +16 -13
- package/dist/migrations/v-0.0.0/types.generated.d.ts +139 -90
- package/dist/migrations/v-0.0.0/types.generated.d.ts.map +1 -1
- package/dist/migrations/v-2.1.0/migration.d.ts +11 -340
- package/dist/migrations/v-2.1.0/migration.d.ts.map +1 -1
- package/dist/migrations/v-2.1.0/migration.js +67 -46
- package/dist/migrations/v-2.1.0/types.generated.d.ts +353 -28
- package/dist/migrations/v-2.1.0/types.generated.d.ts.map +1 -1
- package/dist/migrations/v-2.2.0/index.d.ts +3 -0
- package/dist/migrations/v-2.2.0/index.d.ts.map +1 -0
- package/dist/migrations/v-2.2.0/index.js +1 -0
- package/dist/migrations/v-2.2.0/migration.d.ts +5 -0
- package/dist/migrations/v-2.2.0/migration.d.ts.map +1 -0
- package/dist/migrations/v-2.2.0/migration.js +110 -0
- package/dist/migrations/v-2.2.0/types.generated.d.ts +41 -0
- package/dist/migrations/v-2.2.0/types.generated.d.ts.map +1 -0
- package/dist/transforms/import-spec.d.ts +1 -3
- package/dist/transforms/import-spec.d.ts.map +1 -1
- package/dist/transforms/import-spec.js +54 -96
- package/dist/transforms/index.js +1 -1
- package/package.json +12 -7
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { securitySchemeSchema
|
|
1
|
+
import { securitySchemeSchema } from '../entities/spec/security.js';
|
|
2
2
|
import { schemaModel } from '../helpers/schema-model.js';
|
|
3
3
|
import { keysOf } from '@scalar/object-utils/arrays';
|
|
4
4
|
import { load, upgrade, dereference } from '@scalar/openapi-parser';
|
|
@@ -8,82 +8,6 @@ import { tagSchema } from '../entities/spec/spec-objects.js';
|
|
|
8
8
|
import { createExampleFromRequest } from '../entities/spec/request-examples.js';
|
|
9
9
|
import { collectionSchema } from '../entities/spec/collection.js';
|
|
10
10
|
|
|
11
|
-
/**
|
|
12
|
-
* We need to convert from openapi spec flows to our singular flow object here
|
|
13
|
-
* If we ever go spec compliant (flows), we will no longer need this conversion
|
|
14
|
-
*/
|
|
15
|
-
const convertOauth2Flows = (security, nameKey, auth) => {
|
|
16
|
-
if (security.type === 'oauth2') {
|
|
17
|
-
const entries = Object.entries(security.flows ?? {});
|
|
18
|
-
if (entries.length) {
|
|
19
|
-
const [[type, flow]] = entries;
|
|
20
|
-
const payload = {
|
|
21
|
-
...security,
|
|
22
|
-
nameKey,
|
|
23
|
-
flow: {
|
|
24
|
-
...flow,
|
|
25
|
-
scopes:
|
|
26
|
-
// Ensure we convert array scope to an object
|
|
27
|
-
Array.isArray(flow.scopes) && typeof flow.scopes[0] === 'string'
|
|
28
|
-
? flow.scopes.reduce((prev, s) => ({ ...prev, [s]: '' }), {})
|
|
29
|
-
: flow.scopes,
|
|
30
|
-
type,
|
|
31
|
-
},
|
|
32
|
-
};
|
|
33
|
-
if (auth?.oAuth2 && payload.flow) {
|
|
34
|
-
// Set client id
|
|
35
|
-
if (auth.oAuth2.clientId)
|
|
36
|
-
payload['x-scalar-client-id'] = auth.oAuth2.clientId;
|
|
37
|
-
// Set selected scopes
|
|
38
|
-
if (auth.oAuth2.scopes)
|
|
39
|
-
payload.flow.selectedScopes = auth.oAuth2.scopes;
|
|
40
|
-
}
|
|
41
|
-
// Handle x-defaultClientId
|
|
42
|
-
if ('x-defaultClientId' in flow &&
|
|
43
|
-
typeof flow['x-defaultClientId'] === 'string')
|
|
44
|
-
payload['x-scalar-client-id'] = flow['x-defaultClientId'];
|
|
45
|
-
return payload;
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
return {
|
|
49
|
-
...security,
|
|
50
|
-
nameKey,
|
|
51
|
-
};
|
|
52
|
-
};
|
|
53
|
-
/** Pre-fill baseValues if we have authentication config */
|
|
54
|
-
const getBaseAuthValues = (scheme, auth) => {
|
|
55
|
-
if (!auth)
|
|
56
|
-
return {};
|
|
57
|
-
// ApiKey
|
|
58
|
-
if (scheme.type === 'apiKey')
|
|
59
|
-
return { value: auth.apiKey?.token ?? '' };
|
|
60
|
-
// HTTP
|
|
61
|
-
else if (scheme.type === 'http') {
|
|
62
|
-
if (scheme.scheme === 'basic')
|
|
63
|
-
return {
|
|
64
|
-
username: auth.http?.basic?.username ?? '',
|
|
65
|
-
password: auth.http?.basic?.password ?? '',
|
|
66
|
-
};
|
|
67
|
-
else if (scheme.scheme === 'bearer')
|
|
68
|
-
return { token: auth.http?.bearer?.token ?? '' };
|
|
69
|
-
}
|
|
70
|
-
// oauth2 implicit only for now, when we support multi flow can expand this
|
|
71
|
-
else if (scheme.type === 'oauth2') {
|
|
72
|
-
if (scheme.flow?.type === 'implicit')
|
|
73
|
-
return {
|
|
74
|
-
type: 'oauth-implicit',
|
|
75
|
-
token: auth.oAuth2?.accessToken ?? '',
|
|
76
|
-
};
|
|
77
|
-
else if (scheme.flow?.type === 'password')
|
|
78
|
-
return {
|
|
79
|
-
type: 'oauth-password',
|
|
80
|
-
token: auth.oAuth2?.accessToken ?? '',
|
|
81
|
-
username: auth.oAuth2?.username ?? '',
|
|
82
|
-
password: auth.oAuth2?.password ?? '',
|
|
83
|
-
};
|
|
84
|
-
}
|
|
85
|
-
return {};
|
|
86
|
-
};
|
|
87
11
|
/** Takes a string or object and parses it into an openapi spec compliant schema */
|
|
88
12
|
const parseSchema = async (spec) => {
|
|
89
13
|
// TODO: Plugins for URLs and files with the proxy is missing here.
|
|
@@ -149,15 +73,58 @@ async function importSpecToWorkspace(spec, { authentication, baseServerURL, docu
|
|
|
149
73
|
// SECURITY HANDLING
|
|
150
74
|
const security = schema.components?.securitySchemes ?? schema?.securityDefinitions ?? {};
|
|
151
75
|
const securitySchemes = Object.entries(security)
|
|
152
|
-
.map?.(([nameKey,
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
76
|
+
.map?.(([nameKey, _scheme]) => {
|
|
77
|
+
// Apply any transforms we need before parsing
|
|
78
|
+
const payload = {
|
|
79
|
+
..._scheme,
|
|
80
|
+
nameKey,
|
|
81
|
+
};
|
|
82
|
+
// For oauth2 we need to add the type to the flows + prefill from authentication
|
|
83
|
+
if (payload.type === 'oauth2' && payload.flows) {
|
|
84
|
+
const flowKeys = Object.keys(payload.flows);
|
|
85
|
+
flowKeys.forEach((key) => {
|
|
86
|
+
if (!payload.flows?.[key])
|
|
87
|
+
return;
|
|
88
|
+
const flow = payload.flows[key];
|
|
89
|
+
// Set the type
|
|
90
|
+
flow.type = key;
|
|
91
|
+
// Prefill values from authorization config
|
|
92
|
+
if (authentication?.oAuth2) {
|
|
93
|
+
if (authentication.oAuth2.accessToken)
|
|
94
|
+
flow.token = authentication.oAuth2.accessToken;
|
|
95
|
+
if (flow.type === 'password') {
|
|
96
|
+
flow.username = authentication.oAuth2.username;
|
|
97
|
+
flow.password = authentication.oAuth2.password;
|
|
98
|
+
}
|
|
99
|
+
if (authentication.oAuth2.scopes) {
|
|
100
|
+
flow.selectedScopes = authentication.oAuth2.scopes;
|
|
101
|
+
flow['x-scalar-client-id'] = authentication.oAuth2.clientId;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
// Convert scopes to an object
|
|
105
|
+
if (Array.isArray(flow.scopes))
|
|
106
|
+
flow.scopes = flow.scopes.reduce((prev, s) => ({ ...prev, [s]: '' }), {});
|
|
107
|
+
// Handle x-defaultClientId
|
|
108
|
+
if (flow['x-defaultClientId'])
|
|
109
|
+
flow['x-scalar-client-id'] = flow['x-defaultClientId'];
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
// Otherwise we just prefill
|
|
113
|
+
else if (authentication) {
|
|
114
|
+
// ApiKey
|
|
115
|
+
if (payload.type === 'apiKey' && authentication.apiKey?.token)
|
|
116
|
+
payload.value = authentication.apiKey.token;
|
|
117
|
+
// HTTP
|
|
118
|
+
else if (payload.type === 'http') {
|
|
119
|
+
if (payload.scheme === 'basic' && authentication.http?.basic) {
|
|
120
|
+
payload.username = authentication.http.basic.username ?? '';
|
|
121
|
+
payload.password = authentication.http.basic.password ?? '';
|
|
122
|
+
}
|
|
123
|
+
else if (payload.scheme === 'bearer' && authentication.http?.bearer)
|
|
124
|
+
payload.token = authentication.http.bearer.token ?? '';
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
const scheme = schemaModel(payload, securitySchemeSchema, false);
|
|
161
128
|
if (!scheme)
|
|
162
129
|
importWarnings.push(`Security scheme ${nameKey} is invalid.`);
|
|
163
130
|
return scheme;
|
|
@@ -299,14 +266,6 @@ async function importSpecToWorkspace(spec, { authentication, baseServerURL, docu
|
|
|
299
266
|
});
|
|
300
267
|
// ---------------------------------------------------------------------------
|
|
301
268
|
// Generate Collection
|
|
302
|
-
// Create the auth examples
|
|
303
|
-
const auth = securitySchemes?.reduce((prev, s) => {
|
|
304
|
-
const baseValues = getBaseAuthValues(s, authentication);
|
|
305
|
-
const example = authExampleFromSchema(s, baseValues);
|
|
306
|
-
if (example)
|
|
307
|
-
prev[s.uid] = example;
|
|
308
|
-
return prev;
|
|
309
|
-
}, {});
|
|
310
269
|
const securityKeys = Object.keys(security);
|
|
311
270
|
let selectedSecuritySchemeUids = [];
|
|
312
271
|
/** Selected security scheme UIDs for the collection, defaults to the first key */
|
|
@@ -319,7 +278,6 @@ async function importSpecToWorkspace(spec, { authentication, baseServerURL, docu
|
|
|
319
278
|
...schema,
|
|
320
279
|
watchMode,
|
|
321
280
|
documentUrl,
|
|
322
|
-
auth,
|
|
323
281
|
requests: requests.map((r) => r.uid),
|
|
324
282
|
servers: servers.map((s) => s.uid),
|
|
325
283
|
tags: tags.map((t) => t.uid),
|
|
@@ -350,4 +308,4 @@ async function importSpecToWorkspace(spec, { authentication, baseServerURL, docu
|
|
|
350
308
|
};
|
|
351
309
|
}
|
|
352
310
|
|
|
353
|
-
export {
|
|
311
|
+
export { importSpecToWorkspace, parseSchema };
|
package/dist/transforms/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { importSpecToWorkspace, parseSchema } from './import-spec.js';
|
|
2
2
|
export { exportSpecFromWorkspace } from './export-spec.js';
|
package/package.json
CHANGED
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
"specification",
|
|
17
17
|
"yaml"
|
|
18
18
|
],
|
|
19
|
-
"version": "0.2.
|
|
19
|
+
"version": "0.2.78",
|
|
20
20
|
"engines": {
|
|
21
21
|
"node": ">=18"
|
|
22
22
|
},
|
|
@@ -38,6 +38,11 @@
|
|
|
38
38
|
"types": "./dist/migrations/index.d.ts",
|
|
39
39
|
"default": "./dist/migrations/index.js"
|
|
40
40
|
},
|
|
41
|
+
"./migrations/v-2.2.0": {
|
|
42
|
+
"import": "./dist/migrations/v-2.2.0/index.js",
|
|
43
|
+
"types": "./dist/migrations/v-2.2.0/index.d.ts",
|
|
44
|
+
"default": "./dist/migrations/v-2.2.0/index.js"
|
|
45
|
+
},
|
|
41
46
|
"./migrations/v-2.1.0": {
|
|
42
47
|
"import": "./dist/migrations/v-2.1.0/index.js",
|
|
43
48
|
"types": "./dist/migrations/v-2.1.0/index.d.ts",
|
|
@@ -106,19 +111,19 @@
|
|
|
106
111
|
"nanoid": "^5.0.7",
|
|
107
112
|
"yaml": "^2.4.5",
|
|
108
113
|
"zod": "^3.23.8",
|
|
109
|
-
"@scalar/openapi-types": "0.1.5",
|
|
110
114
|
"@scalar/object-utils": "1.1.12",
|
|
111
|
-
"@scalar/themes": "0.9.
|
|
112
|
-
"@scalar/types": "0.0.
|
|
115
|
+
"@scalar/themes": "0.9.49",
|
|
116
|
+
"@scalar/types": "0.0.20",
|
|
117
|
+
"@scalar/openapi-types": "0.1.5"
|
|
113
118
|
},
|
|
114
119
|
"devDependencies": {
|
|
115
120
|
"type-fest": "^4.20.0",
|
|
116
121
|
"vite": "^5.4.10",
|
|
117
122
|
"vitest": "^1.6.0",
|
|
118
|
-
"zod-to-ts": "
|
|
123
|
+
"zod-to-ts": "github:amritk/zod-to-ts#build",
|
|
119
124
|
"@scalar/build-tooling": "0.1.12",
|
|
120
|
-
"@scalar/openapi-
|
|
121
|
-
"@scalar/openapi-
|
|
125
|
+
"@scalar/openapi-types": "0.1.5",
|
|
126
|
+
"@scalar/openapi-parser": "0.8.10"
|
|
122
127
|
},
|
|
123
128
|
"scripts": {
|
|
124
129
|
"build": "scalar-build-rollup",
|