-
Notifications
You must be signed in to change notification settings - Fork 11
/
service.ts
137 lines (117 loc) · 4.79 KB
/
service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import { isPlainObject } from '@stoplight/json';
import type { HttpSecurityScheme, IHttpOperation, IHttpWebhookOperation, Optional } from '@stoplight/types';
import { pickBy } from 'lodash';
import { withContext } from '../context';
import { isNonNullable } from '../guards';
import { createContext } from '../oas/context';
import { bundleResolveRef } from '../oas/resolver';
import { transformOasService } from '../oas/service';
import { translateToComponents } from '../oas/transformers/components';
import { translateSchemaObjectFromPair } from '../oas/transformers/schema';
import type { Oas3HttpServiceBundle, Oas3HttpServiceTransformer } from '../oas/types';
import { OasVersion } from '../oas/types';
import type { ArrayCallbackParameters } from '../types';
import { entries } from '../utils';
import { isSecurityScheme } from './guards';
import { transformOas3Operations, transformOas3WebhookOperations } from './operation';
import { translateToCallbacks } from './transformers/callbacks';
import { translateToExample } from './transformers/examples';
import { translateToSharedParameters } from './transformers/parameters';
import { translateToSharedRequestBody } from './transformers/request';
import { translateToResponse } from './transformers/responses';
import { translateToSingleSecurity } from './transformers/securities';
import { translateToServer } from './transformers/servers';
import type { Oas3TranslateFunction } from './types';
export const bundleOas3Service: Oas3HttpServiceBundle = ({ document: _document }) => {
const ctx = createContext(_document, bundleResolveRef);
const { document } = ctx;
const { securitySchemes, ...service } = transformOas3Service({ document, ctx });
const components = {
...translateToComponents.call(ctx, OasVersion.OAS3, {
responses: translateToResponse,
requestBodies: translateToSharedRequestBody,
examples: translateToExample,
schemas: translateSchemaObjectFromPair,
securitySchemes: translateSecurityScheme,
}),
callbacks: translateToCallbacks.call(ctx, document.components ? document.components['callbacks'] : []) ?? [],
...translateToSharedParameters.call(ctx, document.components),
};
const operations = transformOas3Operations(document, ctx) as unknown as IHttpOperation<true>[];
const webhooks = transformOas3WebhookOperations(document, ctx) as unknown as IHttpWebhookOperation<true>[];
return {
...service,
operations,
webhooks,
components,
};
};
export const transformOas3Service: Oas3HttpServiceTransformer = ({
document: _document,
ctx = createContext(_document),
}) => {
const { document } = ctx;
const httpService = transformOasService.call(ctx);
if (typeof document.info?.summary === 'string') {
httpService.summary = document.info.summary;
}
if (document.info?.license) {
const { name, identifier, ...license } = document.info.license;
httpService.license = {
...license,
name: typeof name === 'string' ? name : '',
...(typeof identifier === 'string' && { identifier }),
};
}
const servers = Array.isArray(document.servers)
? document.servers.map(translateToServer, ctx).filter(isNonNullable)
: [];
if (servers.length) {
httpService.servers = servers;
}
const securitySchemes = entries(document.components?.securitySchemes)
.map(translateSecurityScheme, ctx)
.filter(isNonNullable);
if (securitySchemes.length) {
httpService.securitySchemes = securitySchemes;
}
const security = (Array.isArray(document.security) ? document.security : [])
.map(sec => {
if (!isPlainObject(sec)) return null;
return Object.keys(sec)
.map(key => {
const ss = securitySchemes.find(securityScheme => securityScheme.key === key);
if (ss?.type === 'oauth2') {
const flows = {};
for (const flowKey in ss.flows) {
const flow = ss.flows[flowKey];
flows[flowKey] = {
...flow,
scopes: pickBy(flow.scopes, (_val: string, scopeKey: string) => {
const secKey = sec[key];
if (secKey) return secKey.includes(scopeKey);
return false;
}),
};
}
return {
...ss,
flows,
};
}
return ss;
})
.filter(isNonNullable);
})
.filter(isNonNullable);
if (security.length) {
httpService.security = security;
}
return httpService;
};
const translateSecurityScheme = withContext<
Oas3TranslateFunction<ArrayCallbackParameters<[name: string, scheme: unknown]>, Optional<HttpSecurityScheme>>
>(function ([key, definition], index) {
if (!isSecurityScheme(definition)) return;
return translateToSingleSecurity.call(this, 'scheme', [key, definition], index, []);
});