@stoplight/elements 9.0.11 → 9.0.12-beta-0.2
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/.DS_Store +0 -0
- package/.storybook/main.js +6 -0
- package/.storybook/manager.js +1 -0
- package/.storybook/preview.jsx +3 -0
- package/dist/LICENSE +190 -0
- package/dist/README.md +19 -0
- package/dist/package.json +52 -0
- package/jest.config.js +7 -0
- package/package.json +76 -16
- package/src/__fixtures__/api-descriptions/Instagram.ts +1859 -0
- package/src/__fixtures__/api-descriptions/badgesForSchema.ts +36 -0
- package/src/__fixtures__/api-descriptions/simpleApiWithInternalOperations.ts +253 -0
- package/src/__fixtures__/api-descriptions/simpleApiWithoutDescription.ts +243 -0
- package/src/__fixtures__/api-descriptions/todosApiBundled.ts +430 -0
- package/src/__fixtures__/api-descriptions/zoomApiYaml.ts +6083 -0
- package/src/components/API/APIWithResponsiveSidebarLayout.tsx +125 -0
- package/src/components/API/APIWithSidebarLayout.tsx +158 -0
- package/src/components/API/APIWithStackedLayout.tsx +286 -0
- package/src/components/API/__tests__/utils.test.ts +1323 -0
- package/src/components/API/utils.ts +206 -0
- package/src/containers/API.spec.tsx +122 -0
- package/src/containers/API.stories.tsx +117 -0
- package/src/containers/API.tsx +277 -0
- package/src/containers/story-helper.tsx +53 -0
- package/src/hooks/useExportDocumentProps.spec.tsx +68 -0
- package/src/hooks/useExportDocumentProps.tsx +48 -0
- package/src/index.ts +6 -0
- package/src/styles.css +1 -0
- package/src/utils/oas/__tests__/oas.spec.ts +411 -0
- package/src/utils/oas/index.ts +192 -0
- package/src/utils/oas/oas2.ts +31 -0
- package/src/utils/oas/oas3.ts +54 -0
- package/src/utils/oas/types.ts +34 -0
- package/src/web-components/__stories__/Api.stories.tsx +63 -0
- package/src/web-components/components.ts +26 -0
- package/src/web-components/index.ts +3 -0
- package/tsconfig.build.json +18 -0
- package/tsconfig.json +7 -0
- package/web-components.config.js +1 -0
- package/styles.min.css +0 -1
- package/web-components.min.js +0 -2
- package/web-components.min.js.LICENSE.txt +0 -176
- /package/{__fixtures__ → dist/__fixtures__}/api-descriptions/Instagram.d.ts +0 -0
- /package/{__fixtures__ → dist/__fixtures__}/api-descriptions/badgesForSchema.d.ts +0 -0
- /package/{__fixtures__ → dist/__fixtures__}/api-descriptions/simpleApiWithInternalOperations.d.ts +0 -0
- /package/{__fixtures__ → dist/__fixtures__}/api-descriptions/simpleApiWithoutDescription.d.ts +0 -0
- /package/{__fixtures__ → dist/__fixtures__}/api-descriptions/todosApiBundled.d.ts +0 -0
- /package/{__fixtures__ → dist/__fixtures__}/api-descriptions/zoomApiYaml.d.ts +0 -0
- /package/{components → dist/components}/API/APIWithResponsiveSidebarLayout.d.ts +0 -0
- /package/{components → dist/components}/API/APIWithSidebarLayout.d.ts +0 -0
- /package/{components → dist/components}/API/APIWithStackedLayout.d.ts +0 -0
- /package/{components → dist/components}/API/utils.d.ts +0 -0
- /package/{containers → dist/containers}/API.d.ts +0 -0
- /package/{containers → dist/containers}/API.spec.d.ts +0 -0
- /package/{containers → dist/containers}/API.stories.d.ts +0 -0
- /package/{containers → dist/containers}/story-helper.d.ts +0 -0
- /package/{hooks → dist/hooks}/useExportDocumentProps.d.ts +0 -0
- /package/{hooks → dist/hooks}/useExportDocumentProps.spec.d.ts +0 -0
- /package/{index.d.ts → dist/index.d.ts} +0 -0
- /package/{index.esm.js → dist/index.esm.js} +0 -0
- /package/{index.js → dist/index.js} +0 -0
- /package/{index.mjs → dist/index.mjs} +0 -0
- /package/{utils → dist/utils}/oas/index.d.ts +0 -0
- /package/{utils → dist/utils}/oas/oas2.d.ts +0 -0
- /package/{utils → dist/utils}/oas/oas3.d.ts +0 -0
- /package/{utils → dist/utils}/oas/types.d.ts +0 -0
- /package/{web-components → dist/web-components}/components.d.ts +0 -0
- /package/{web-components → dist/web-components}/index.d.ts +0 -0
|
@@ -0,0 +1,411 @@
|
|
|
1
|
+
import { transformOasToServiceNode } from '../';
|
|
2
|
+
|
|
3
|
+
const oas3Document = {
|
|
4
|
+
'x-stoplight': { id: 'abc' },
|
|
5
|
+
openapi: '3.0.0',
|
|
6
|
+
info: {
|
|
7
|
+
title: 'oas3',
|
|
8
|
+
version: '1.0.0',
|
|
9
|
+
},
|
|
10
|
+
tags: [{ name: 'operation-tag' }, { name: 'model-tag' }],
|
|
11
|
+
paths: {
|
|
12
|
+
'/todos': {
|
|
13
|
+
get: {
|
|
14
|
+
summary: 'Get Todos',
|
|
15
|
+
tags: ['operation-tag'],
|
|
16
|
+
},
|
|
17
|
+
},
|
|
18
|
+
},
|
|
19
|
+
components: {
|
|
20
|
+
schemas: {
|
|
21
|
+
Todo: {
|
|
22
|
+
type: 'object',
|
|
23
|
+
properties: {
|
|
24
|
+
id: {
|
|
25
|
+
type: 'integer',
|
|
26
|
+
},
|
|
27
|
+
},
|
|
28
|
+
title: 'Todo',
|
|
29
|
+
'x-tags': ['model-tag'],
|
|
30
|
+
},
|
|
31
|
+
},
|
|
32
|
+
},
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
const oas2Document = {
|
|
36
|
+
'x-stoplight': { id: 'abc' },
|
|
37
|
+
swagger: '2.0.0',
|
|
38
|
+
info: {
|
|
39
|
+
title: 'oas2',
|
|
40
|
+
version: '1.0.0',
|
|
41
|
+
},
|
|
42
|
+
tags: [{ name: 'operation-tag' }, { name: 'model-tag' }],
|
|
43
|
+
paths: {
|
|
44
|
+
'/todos': {
|
|
45
|
+
get: {
|
|
46
|
+
summary: 'Get Todos',
|
|
47
|
+
tags: ['operation-tag'],
|
|
48
|
+
},
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
definitions: {
|
|
52
|
+
Todo: {
|
|
53
|
+
type: 'object',
|
|
54
|
+
properties: {
|
|
55
|
+
id: {
|
|
56
|
+
type: 'integer',
|
|
57
|
+
},
|
|
58
|
+
},
|
|
59
|
+
title: 'Todo',
|
|
60
|
+
'x-tags': ['model-tag'],
|
|
61
|
+
},
|
|
62
|
+
},
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
describe('computeOasNodes', () => {
|
|
66
|
+
it('should return null for invalid document', () => {
|
|
67
|
+
expect(transformOasToServiceNode({})).toBeNull();
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it('should return oas nodes for oas3 document', () => {
|
|
71
|
+
expect(transformOasToServiceNode(oas3Document)).toStrictEqual({
|
|
72
|
+
type: 'http_service',
|
|
73
|
+
uri: '/',
|
|
74
|
+
name: 'oas3',
|
|
75
|
+
data: {
|
|
76
|
+
id: 'abc',
|
|
77
|
+
version: '1.0.0',
|
|
78
|
+
name: 'oas3',
|
|
79
|
+
tags: [
|
|
80
|
+
{
|
|
81
|
+
id: '68c460dd9e97a',
|
|
82
|
+
name: 'operation-tag',
|
|
83
|
+
},
|
|
84
|
+
{
|
|
85
|
+
id: 'feec8b579ae6b',
|
|
86
|
+
name: 'model-tag',
|
|
87
|
+
},
|
|
88
|
+
],
|
|
89
|
+
extensions: {
|
|
90
|
+
'x-stoplight': {
|
|
91
|
+
id: 'abc',
|
|
92
|
+
},
|
|
93
|
+
},
|
|
94
|
+
infoExtensions: {},
|
|
95
|
+
},
|
|
96
|
+
tags: ['operation-tag', 'model-tag'],
|
|
97
|
+
children: [
|
|
98
|
+
{
|
|
99
|
+
type: 'http_operation',
|
|
100
|
+
uri: '/paths/todos/get',
|
|
101
|
+
data: {
|
|
102
|
+
id: 'c35bc6b301d97',
|
|
103
|
+
method: 'get',
|
|
104
|
+
path: '/todos',
|
|
105
|
+
summary: 'Get Todos',
|
|
106
|
+
responses: [],
|
|
107
|
+
servers: [],
|
|
108
|
+
request: {
|
|
109
|
+
headers: [],
|
|
110
|
+
query: [],
|
|
111
|
+
cookie: [],
|
|
112
|
+
path: [],
|
|
113
|
+
},
|
|
114
|
+
tags: [
|
|
115
|
+
{
|
|
116
|
+
id: '68c460dd9e97a',
|
|
117
|
+
name: 'operation-tag',
|
|
118
|
+
},
|
|
119
|
+
],
|
|
120
|
+
security: [],
|
|
121
|
+
securityDeclarationType: 'inheritedFromService',
|
|
122
|
+
extensions: {},
|
|
123
|
+
},
|
|
124
|
+
name: 'Get Todos',
|
|
125
|
+
tags: ['operation-tag'],
|
|
126
|
+
},
|
|
127
|
+
{
|
|
128
|
+
type: 'model',
|
|
129
|
+
uri: '/schemas/Todo',
|
|
130
|
+
data: {
|
|
131
|
+
type: 'object',
|
|
132
|
+
properties: {
|
|
133
|
+
id: {
|
|
134
|
+
type: 'integer',
|
|
135
|
+
},
|
|
136
|
+
},
|
|
137
|
+
title: 'Todo',
|
|
138
|
+
'x-tags': ['model-tag'],
|
|
139
|
+
},
|
|
140
|
+
name: 'Todo',
|
|
141
|
+
tags: ['model-tag'],
|
|
142
|
+
},
|
|
143
|
+
],
|
|
144
|
+
});
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
it('should return oas nodes for oas2 document', () => {
|
|
148
|
+
expect(transformOasToServiceNode(oas2Document)).toStrictEqual({
|
|
149
|
+
type: 'http_service',
|
|
150
|
+
uri: '/',
|
|
151
|
+
name: 'oas2',
|
|
152
|
+
data: {
|
|
153
|
+
id: 'abc',
|
|
154
|
+
name: 'oas2',
|
|
155
|
+
tags: [
|
|
156
|
+
{
|
|
157
|
+
id: '68c460dd9e97a',
|
|
158
|
+
name: 'operation-tag',
|
|
159
|
+
},
|
|
160
|
+
{
|
|
161
|
+
id: 'feec8b579ae6b',
|
|
162
|
+
name: 'model-tag',
|
|
163
|
+
},
|
|
164
|
+
],
|
|
165
|
+
extensions: {
|
|
166
|
+
'x-stoplight': {
|
|
167
|
+
id: 'abc',
|
|
168
|
+
},
|
|
169
|
+
},
|
|
170
|
+
infoExtensions: {},
|
|
171
|
+
version: '1.0.0',
|
|
172
|
+
},
|
|
173
|
+
tags: ['operation-tag', 'model-tag'],
|
|
174
|
+
children: [
|
|
175
|
+
{
|
|
176
|
+
type: 'http_operation',
|
|
177
|
+
uri: '/paths/todos/get',
|
|
178
|
+
data: {
|
|
179
|
+
id: 'c35bc6b301d97',
|
|
180
|
+
method: 'get',
|
|
181
|
+
path: '/todos',
|
|
182
|
+
summary: 'Get Todos',
|
|
183
|
+
responses: [],
|
|
184
|
+
servers: [],
|
|
185
|
+
request: {
|
|
186
|
+
cookie: [],
|
|
187
|
+
headers: [],
|
|
188
|
+
path: [],
|
|
189
|
+
query: [],
|
|
190
|
+
},
|
|
191
|
+
tags: [
|
|
192
|
+
{
|
|
193
|
+
id: '68c460dd9e97a',
|
|
194
|
+
name: 'operation-tag',
|
|
195
|
+
},
|
|
196
|
+
],
|
|
197
|
+
security: [],
|
|
198
|
+
securityDeclarationType: 'inheritedFromService',
|
|
199
|
+
extensions: {},
|
|
200
|
+
},
|
|
201
|
+
name: 'Get Todos',
|
|
202
|
+
tags: ['operation-tag'],
|
|
203
|
+
},
|
|
204
|
+
{
|
|
205
|
+
type: 'model',
|
|
206
|
+
uri: '/schemas/Todo',
|
|
207
|
+
data: {
|
|
208
|
+
type: 'object',
|
|
209
|
+
properties: {
|
|
210
|
+
id: {
|
|
211
|
+
type: 'integer',
|
|
212
|
+
},
|
|
213
|
+
},
|
|
214
|
+
title: 'Todo',
|
|
215
|
+
'x-tags': ['model-tag'],
|
|
216
|
+
},
|
|
217
|
+
name: 'Todo',
|
|
218
|
+
tags: ['model-tag'],
|
|
219
|
+
},
|
|
220
|
+
],
|
|
221
|
+
});
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
it('should fallback to operationId', () => {
|
|
225
|
+
expect(
|
|
226
|
+
transformOasToServiceNode({
|
|
227
|
+
'x-stoplight': { id: 'def' },
|
|
228
|
+
openapi: '3.0.0',
|
|
229
|
+
info: {
|
|
230
|
+
title: 'oas3',
|
|
231
|
+
version: '1.0.0',
|
|
232
|
+
},
|
|
233
|
+
paths: {
|
|
234
|
+
'/todos': {
|
|
235
|
+
get: {
|
|
236
|
+
operationId: 'get-todos',
|
|
237
|
+
},
|
|
238
|
+
},
|
|
239
|
+
},
|
|
240
|
+
}),
|
|
241
|
+
).toEqual({
|
|
242
|
+
type: 'http_service',
|
|
243
|
+
uri: '/',
|
|
244
|
+
name: 'oas3',
|
|
245
|
+
tags: [],
|
|
246
|
+
data: {
|
|
247
|
+
id: 'def',
|
|
248
|
+
version: '1.0.0',
|
|
249
|
+
name: 'oas3',
|
|
250
|
+
extensions: {
|
|
251
|
+
'x-stoplight': {
|
|
252
|
+
id: 'def',
|
|
253
|
+
},
|
|
254
|
+
},
|
|
255
|
+
infoExtensions: {},
|
|
256
|
+
},
|
|
257
|
+
children: [
|
|
258
|
+
{
|
|
259
|
+
type: 'http_operation',
|
|
260
|
+
uri: '/operations/get-todos',
|
|
261
|
+
data: {
|
|
262
|
+
id: 'a61f53c324669',
|
|
263
|
+
iid: 'get-todos',
|
|
264
|
+
method: 'get',
|
|
265
|
+
path: '/todos',
|
|
266
|
+
responses: [],
|
|
267
|
+
servers: [],
|
|
268
|
+
request: {
|
|
269
|
+
headers: [],
|
|
270
|
+
query: [],
|
|
271
|
+
cookie: [],
|
|
272
|
+
path: [],
|
|
273
|
+
},
|
|
274
|
+
tags: [],
|
|
275
|
+
security: [],
|
|
276
|
+
securityDeclarationType: 'inheritedFromService',
|
|
277
|
+
extensions: {},
|
|
278
|
+
},
|
|
279
|
+
tags: [],
|
|
280
|
+
name: 'get-todos',
|
|
281
|
+
},
|
|
282
|
+
],
|
|
283
|
+
});
|
|
284
|
+
});
|
|
285
|
+
|
|
286
|
+
it('should not throw error for non-common url paths', () => {
|
|
287
|
+
expect(
|
|
288
|
+
transformOasToServiceNode({
|
|
289
|
+
'x-stoplight': { id: 'def' },
|
|
290
|
+
openapi: '3.0.0',
|
|
291
|
+
info: {
|
|
292
|
+
title: 'oas3',
|
|
293
|
+
version: '1.0.0',
|
|
294
|
+
},
|
|
295
|
+
paths: {
|
|
296
|
+
'/todos/{id}/flow)': {
|
|
297
|
+
get: {
|
|
298
|
+
operationId: 'get-todos',
|
|
299
|
+
},
|
|
300
|
+
},
|
|
301
|
+
},
|
|
302
|
+
}),
|
|
303
|
+
).toEqual({
|
|
304
|
+
type: 'http_service',
|
|
305
|
+
uri: '/',
|
|
306
|
+
name: 'oas3',
|
|
307
|
+
tags: [],
|
|
308
|
+
data: {
|
|
309
|
+
id: 'def',
|
|
310
|
+
version: '1.0.0',
|
|
311
|
+
name: 'oas3',
|
|
312
|
+
extensions: {
|
|
313
|
+
'x-stoplight': {
|
|
314
|
+
id: 'def',
|
|
315
|
+
},
|
|
316
|
+
},
|
|
317
|
+
infoExtensions: {},
|
|
318
|
+
},
|
|
319
|
+
children: [
|
|
320
|
+
{
|
|
321
|
+
type: 'http_operation',
|
|
322
|
+
uri: '/operations/get-todos',
|
|
323
|
+
data: {
|
|
324
|
+
id: '7b7e36ffa6501',
|
|
325
|
+
iid: 'get-todos',
|
|
326
|
+
method: 'get',
|
|
327
|
+
path: '/todos/{id}/flow)',
|
|
328
|
+
responses: [],
|
|
329
|
+
servers: [],
|
|
330
|
+
request: {
|
|
331
|
+
headers: [],
|
|
332
|
+
query: [],
|
|
333
|
+
cookie: [],
|
|
334
|
+
path: [],
|
|
335
|
+
},
|
|
336
|
+
tags: [],
|
|
337
|
+
|
|
338
|
+
securityDeclarationType: 'inheritedFromService',
|
|
339
|
+
security: [],
|
|
340
|
+
extensions: {},
|
|
341
|
+
},
|
|
342
|
+
tags: [],
|
|
343
|
+
name: 'get-todos',
|
|
344
|
+
},
|
|
345
|
+
],
|
|
346
|
+
});
|
|
347
|
+
});
|
|
348
|
+
|
|
349
|
+
it('should filter out unused security nodes and not show duplicate nodes', () => {
|
|
350
|
+
const serviceNode = transformOasToServiceNode({
|
|
351
|
+
openapi: '3.1.0',
|
|
352
|
+
'x-stoplight': {
|
|
353
|
+
id: 'nso1sfqvio7bp',
|
|
354
|
+
},
|
|
355
|
+
info: {
|
|
356
|
+
title: 'Test',
|
|
357
|
+
version: '1.0',
|
|
358
|
+
},
|
|
359
|
+
servers: [
|
|
360
|
+
{
|
|
361
|
+
url: 'http://localhost:3000',
|
|
362
|
+
},
|
|
363
|
+
],
|
|
364
|
+
paths: {
|
|
365
|
+
'/users': {
|
|
366
|
+
get: {
|
|
367
|
+
summary: 'Your GET endpoint',
|
|
368
|
+
tags: [],
|
|
369
|
+
responses: {},
|
|
370
|
+
operationId: 'get-users',
|
|
371
|
+
'x-stoplight': {
|
|
372
|
+
id: 'oblhqa66gbqqg',
|
|
373
|
+
},
|
|
374
|
+
security: [
|
|
375
|
+
{
|
|
376
|
+
API_Key_Query: [],
|
|
377
|
+
},
|
|
378
|
+
],
|
|
379
|
+
},
|
|
380
|
+
},
|
|
381
|
+
},
|
|
382
|
+
components: {
|
|
383
|
+
schemas: {},
|
|
384
|
+
securitySchemes: {
|
|
385
|
+
API_Key_Query: {
|
|
386
|
+
name: 'API Key',
|
|
387
|
+
type: 'apiKey',
|
|
388
|
+
in: 'query',
|
|
389
|
+
},
|
|
390
|
+
API_Key_Header: {
|
|
391
|
+
name: 'API Key',
|
|
392
|
+
type: 'apiKey',
|
|
393
|
+
in: 'header',
|
|
394
|
+
},
|
|
395
|
+
API_Key_Cookie: {
|
|
396
|
+
name: 'API Key',
|
|
397
|
+
type: 'apiKey',
|
|
398
|
+
in: 'cookie',
|
|
399
|
+
},
|
|
400
|
+
},
|
|
401
|
+
},
|
|
402
|
+
security: [
|
|
403
|
+
{
|
|
404
|
+
API_Key_Query: [],
|
|
405
|
+
},
|
|
406
|
+
],
|
|
407
|
+
});
|
|
408
|
+
|
|
409
|
+
expect(serviceNode?.data.security).toHaveLength(1);
|
|
410
|
+
});
|
|
411
|
+
});
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
import { slugify } from '@stoplight/elements-core';
|
|
2
|
+
import {
|
|
3
|
+
Oas2HttpOperationTransformer,
|
|
4
|
+
Oas2HttpServiceTransformer,
|
|
5
|
+
Oas3HttpEndpointOperationTransformer,
|
|
6
|
+
Oas3HttpServiceTransformer,
|
|
7
|
+
OPERATION_CONFIG,
|
|
8
|
+
WEBHOOK_CONFIG,
|
|
9
|
+
} from '@stoplight/http-spec/oas';
|
|
10
|
+
import { transformOas2Operation, transformOas2Service } from '@stoplight/http-spec/oas2';
|
|
11
|
+
import { transformOas3Operation, transformOas3Service } from '@stoplight/http-spec/oas3';
|
|
12
|
+
import { encodePointerFragment, pointerToPath } from '@stoplight/json';
|
|
13
|
+
import { IHttpOperation, IHttpWebhookOperation, NodeType } from '@stoplight/types';
|
|
14
|
+
import { get, isObject, last } from 'lodash';
|
|
15
|
+
import { OpenAPIObject as _OpenAPIObject, PathObject } from 'openapi3-ts';
|
|
16
|
+
import { Spec } from 'swagger-schema-official';
|
|
17
|
+
|
|
18
|
+
import { oas2SourceMap } from './oas2';
|
|
19
|
+
import { oas3SourceMap } from './oas3';
|
|
20
|
+
import { ISourceNodeMap, NodeTypes, ServiceChildNode, ServiceNode } from './types';
|
|
21
|
+
|
|
22
|
+
type OpenAPIObject = _OpenAPIObject & {
|
|
23
|
+
webhooks?: PathObject;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
type SpecDocument = Spec | OpenAPIObject;
|
|
27
|
+
|
|
28
|
+
const isOas2 = (parsed: unknown): parsed is Spec =>
|
|
29
|
+
isObject(parsed) &&
|
|
30
|
+
'swagger' in parsed &&
|
|
31
|
+
Number.parseInt(String((parsed as Partial<{ swagger: unknown }>).swagger)) === 2;
|
|
32
|
+
|
|
33
|
+
const isOas3 = (parsed: unknown): parsed is OpenAPIObject =>
|
|
34
|
+
isObject(parsed) &&
|
|
35
|
+
'openapi' in parsed &&
|
|
36
|
+
Number.parseFloat(String((parsed as Partial<{ openapi: unknown }>).openapi)) >= 3;
|
|
37
|
+
|
|
38
|
+
const isOas31 = (parsed: unknown): parsed is OpenAPIObject =>
|
|
39
|
+
isObject(parsed) &&
|
|
40
|
+
'openapi' in parsed &&
|
|
41
|
+
Number.parseFloat(String((parsed as Partial<{ openapi: unknown }>).openapi)) === 3.1;
|
|
42
|
+
|
|
43
|
+
const OAS_MODEL_REGEXP = /((definitions|components)\/?(schemas)?)\//;
|
|
44
|
+
|
|
45
|
+
export function transformOasToServiceNode(apiDescriptionDocument: unknown) {
|
|
46
|
+
if (isOas31(apiDescriptionDocument)) {
|
|
47
|
+
return computeServiceNode(
|
|
48
|
+
{ ...apiDescriptionDocument, jsonSchemaDialect: 'http://json-schema.org/draft-07/schema#' },
|
|
49
|
+
oas3SourceMap,
|
|
50
|
+
transformOas3Service,
|
|
51
|
+
transformOas3Operation,
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
if (isOas3(apiDescriptionDocument)) {
|
|
55
|
+
return computeServiceNode(apiDescriptionDocument, oas3SourceMap, transformOas3Service, transformOas3Operation);
|
|
56
|
+
} else if (isOas2(apiDescriptionDocument)) {
|
|
57
|
+
return computeServiceNode(apiDescriptionDocument, oas2SourceMap, transformOas2Service, transformOas2Operation);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return null;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function computeServiceNode(
|
|
64
|
+
document: SpecDocument,
|
|
65
|
+
map: ISourceNodeMap[],
|
|
66
|
+
transformService: Oas2HttpServiceTransformer | Oas3HttpServiceTransformer,
|
|
67
|
+
transformOperation: Oas2HttpOperationTransformer | Oas3HttpEndpointOperationTransformer,
|
|
68
|
+
) {
|
|
69
|
+
const serviceDocument = transformService({ document });
|
|
70
|
+
const serviceNode: ServiceNode = {
|
|
71
|
+
type: NodeType.HttpService,
|
|
72
|
+
uri: '/',
|
|
73
|
+
name: serviceDocument.name,
|
|
74
|
+
data: serviceDocument,
|
|
75
|
+
tags: serviceDocument.tags?.map(tag => tag.name) || [],
|
|
76
|
+
children: computeChildNodes(document, document, map, transformOperation),
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
return serviceNode;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function computeChildNodes(
|
|
83
|
+
document: SpecDocument,
|
|
84
|
+
data: SpecDocument,
|
|
85
|
+
map: ISourceNodeMap[],
|
|
86
|
+
transformer: Oas2HttpOperationTransformer | Oas3HttpEndpointOperationTransformer,
|
|
87
|
+
parentUri: string = '',
|
|
88
|
+
) {
|
|
89
|
+
const nodes: ServiceChildNode[] = [];
|
|
90
|
+
|
|
91
|
+
if (!isObject(data)) return nodes;
|
|
92
|
+
|
|
93
|
+
for (const [key, value] of Object.entries(data)) {
|
|
94
|
+
const sanitizedKey = encodePointerFragment(key);
|
|
95
|
+
const match = findMapMatch(sanitizedKey, map);
|
|
96
|
+
|
|
97
|
+
if (match) {
|
|
98
|
+
const uri = `${parentUri}/${sanitizedKey}`;
|
|
99
|
+
const jsonPath = pointerToPath(`#${uri}`);
|
|
100
|
+
|
|
101
|
+
if (match.type === NodeTypes.Operation && jsonPath.length === 3) {
|
|
102
|
+
const path = String(jsonPath[1]);
|
|
103
|
+
const method = String(jsonPath[2]);
|
|
104
|
+
const operationDocument = transformer({
|
|
105
|
+
document,
|
|
106
|
+
name: path,
|
|
107
|
+
method,
|
|
108
|
+
config: OPERATION_CONFIG,
|
|
109
|
+
}) as IHttpOperation;
|
|
110
|
+
let parsedUri;
|
|
111
|
+
const encodedPath = String(encodePointerFragment(path));
|
|
112
|
+
|
|
113
|
+
if (operationDocument.iid) {
|
|
114
|
+
parsedUri = `/operations/${operationDocument.iid}`;
|
|
115
|
+
} else {
|
|
116
|
+
parsedUri = uri.replace(encodedPath, slugify(path));
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
nodes.push({
|
|
120
|
+
type: NodeType.HttpOperation,
|
|
121
|
+
uri: parsedUri,
|
|
122
|
+
data: operationDocument,
|
|
123
|
+
name: operationDocument.summary || operationDocument.iid || operationDocument.path,
|
|
124
|
+
tags: operationDocument.tags?.map(tag => tag.name) || [],
|
|
125
|
+
});
|
|
126
|
+
} else if (match.type === NodeTypes.Webhook && jsonPath.length === 3) {
|
|
127
|
+
const name = String(jsonPath[1]);
|
|
128
|
+
const method = String(jsonPath[2]);
|
|
129
|
+
const webhookDocument = transformer({
|
|
130
|
+
document,
|
|
131
|
+
name,
|
|
132
|
+
method,
|
|
133
|
+
config: WEBHOOK_CONFIG,
|
|
134
|
+
}) as IHttpWebhookOperation;
|
|
135
|
+
|
|
136
|
+
let parsedUri;
|
|
137
|
+
const encodedPath = String(encodePointerFragment(name));
|
|
138
|
+
|
|
139
|
+
if (webhookDocument.iid) {
|
|
140
|
+
parsedUri = `/webhooks/${webhookDocument.iid}`;
|
|
141
|
+
} else {
|
|
142
|
+
parsedUri = uri.replace(encodedPath, slugify(name));
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
nodes.push({
|
|
146
|
+
type: NodeType.HttpWebhook,
|
|
147
|
+
uri: parsedUri,
|
|
148
|
+
data: webhookDocument,
|
|
149
|
+
name: webhookDocument.summary || webhookDocument.name,
|
|
150
|
+
tags: webhookDocument.tags?.map(tag => tag.name) || [],
|
|
151
|
+
});
|
|
152
|
+
} else if (match.type === NodeTypes.Model) {
|
|
153
|
+
const schemaDocument = get(document, jsonPath);
|
|
154
|
+
const parsedUri = uri.replace(OAS_MODEL_REGEXP, 'schemas/');
|
|
155
|
+
|
|
156
|
+
nodes.push({
|
|
157
|
+
type: NodeType.Model,
|
|
158
|
+
uri: parsedUri,
|
|
159
|
+
data: schemaDocument,
|
|
160
|
+
name: schemaDocument.title || last(uri.split('/')) || '',
|
|
161
|
+
tags: schemaDocument['x-tags'] || [],
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
if (match.children) {
|
|
166
|
+
nodes.push(...computeChildNodes(document, value, match.children, transformer, uri));
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
return nodes;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
function findMapMatch(key: string | number, map: ISourceNodeMap[]): ISourceNodeMap | void {
|
|
175
|
+
if (typeof key === 'number') return;
|
|
176
|
+
for (const entry of map) {
|
|
177
|
+
const escapedKey = key.replace(/[\\^$*+?.()|[\]{}]/g, '\\$&');
|
|
178
|
+
|
|
179
|
+
if (!!entry.match?.match(escapedKey) || (entry.notMatch !== void 0 && !entry.notMatch.match(escapedKey))) {
|
|
180
|
+
return entry;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
export function isJson(value: string) {
|
|
186
|
+
try {
|
|
187
|
+
JSON.parse(value);
|
|
188
|
+
} catch (e) {
|
|
189
|
+
return false;
|
|
190
|
+
}
|
|
191
|
+
return true;
|
|
192
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { ISourceNodeMap, NodeTypes } from './types';
|
|
2
|
+
|
|
3
|
+
export const oas2SourceMap: ISourceNodeMap[] = [
|
|
4
|
+
{
|
|
5
|
+
match: 'paths',
|
|
6
|
+
type: NodeTypes.Paths,
|
|
7
|
+
children: [
|
|
8
|
+
{
|
|
9
|
+
notMatch: '^x-',
|
|
10
|
+
type: NodeTypes.Path,
|
|
11
|
+
children: [
|
|
12
|
+
{
|
|
13
|
+
match: 'get|post|put|delete|options|head|patch|trace',
|
|
14
|
+
type: NodeTypes.Operation,
|
|
15
|
+
},
|
|
16
|
+
],
|
|
17
|
+
},
|
|
18
|
+
],
|
|
19
|
+
},
|
|
20
|
+
|
|
21
|
+
{
|
|
22
|
+
match: 'definitions',
|
|
23
|
+
type: NodeTypes.Models,
|
|
24
|
+
children: [
|
|
25
|
+
{
|
|
26
|
+
notMatch: '^x-',
|
|
27
|
+
type: NodeTypes.Model,
|
|
28
|
+
},
|
|
29
|
+
],
|
|
30
|
+
},
|
|
31
|
+
];
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { ISourceNodeMap, NodeTypes } from './types';
|
|
2
|
+
|
|
3
|
+
export const oas3SourceMap: ISourceNodeMap[] = [
|
|
4
|
+
{
|
|
5
|
+
match: 'paths',
|
|
6
|
+
type: NodeTypes.Paths,
|
|
7
|
+
children: [
|
|
8
|
+
{
|
|
9
|
+
notMatch: '^x-',
|
|
10
|
+
type: NodeTypes.Path,
|
|
11
|
+
children: [
|
|
12
|
+
{
|
|
13
|
+
match: 'get|post|put|delete|options|head|patch|trace',
|
|
14
|
+
type: NodeTypes.Operation,
|
|
15
|
+
},
|
|
16
|
+
],
|
|
17
|
+
},
|
|
18
|
+
],
|
|
19
|
+
},
|
|
20
|
+
|
|
21
|
+
{
|
|
22
|
+
match: 'webhooks',
|
|
23
|
+
type: NodeTypes.Webhooks,
|
|
24
|
+
children: [
|
|
25
|
+
{
|
|
26
|
+
notMatch: '^x-',
|
|
27
|
+
type: NodeTypes.Webhook,
|
|
28
|
+
children: [
|
|
29
|
+
{
|
|
30
|
+
match: 'get|post|put|delete|options|head|patch|trace',
|
|
31
|
+
type: NodeTypes.Webhook,
|
|
32
|
+
},
|
|
33
|
+
],
|
|
34
|
+
},
|
|
35
|
+
],
|
|
36
|
+
},
|
|
37
|
+
|
|
38
|
+
{
|
|
39
|
+
match: 'components',
|
|
40
|
+
type: NodeTypes.Components,
|
|
41
|
+
children: [
|
|
42
|
+
{
|
|
43
|
+
match: 'schemas',
|
|
44
|
+
type: NodeTypes.Models,
|
|
45
|
+
children: [
|
|
46
|
+
{
|
|
47
|
+
notMatch: '^x-',
|
|
48
|
+
type: NodeTypes.Model,
|
|
49
|
+
},
|
|
50
|
+
],
|
|
51
|
+
},
|
|
52
|
+
],
|
|
53
|
+
},
|
|
54
|
+
];
|