-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathresolveOpenAPI.ts
184 lines (164 loc) · 5.11 KB
/
resolveOpenAPI.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/* eslint-env node */
/* eslint import/no-nodejs-modules:0 */
/* eslint-disable no-console */
import {promises as fs} from 'fs';
import {cache} from 'react';
import {DeRefedOpenAPI} from './open-api/types';
// SENTRY_API_SCHEMA_SHA is used in the sentry-docs GHA workflow in getsentry/sentry-api-schema.
// DO NOT change variable name unless you change it in the sentry-docs GHA workflow in getsentry/sentry-api-schema.
const SENTRY_API_SCHEMA_SHA = 'deea76e7205d4e1efb45f91796b9fc73499de226';
const activeEnv = process.env.GATSBY_ENV || process.env.NODE_ENV || 'development';
let cachedResonse: DeRefedOpenAPI | null = null;
async function resolveOpenAPI(): Promise<DeRefedOpenAPI> {
if (activeEnv === 'development' && process.env.OPENAPI_LOCAL_PATH) {
try {
console.log(`Fetching from ${process.env.OPENAPI_LOCAL_PATH}`);
const data = await fs.readFile(process.env.OPENAPI_LOCAL_PATH, 'utf8');
return JSON.parse(data);
} catch (error) {
console.log(
`Failed to connect to ${process.env.OPENAPI_LOCAL_PATH}. Continuing to fetch versioned schema from GitHub.
${error}`
);
}
}
if (cachedResonse) {
return cachedResonse;
}
const response = await fetch(
`https://raw.githubusercontent.com/getsentry/sentry-api-schema/${SENTRY_API_SCHEMA_SHA}/openapi-derefed.json`,
{cache: 'no-store'}
);
cachedResonse = await response.json();
return cachedResonse!;
}
export type APIParameter = {
description: string;
name: string;
required: boolean;
schema: {
type: string;
format?: string;
items?: {
type: string;
};
};
};
export type API = {
apiPath: string;
bodyParameters: APIParameter[];
method: string;
name: string;
pathParameters: APIParameter[];
queryParameters: APIParameter[];
responses: any;
slug: string;
bodyContentType?: string;
descriptionMarkdown?: string;
requestBodyContent?: any;
security?: {[key: string]: string[]};
summary?: string;
};
export type APICategory = {
apis: API[];
name: string;
slug: string;
description?: string;
};
function slugify(s: string): string {
return s
.replace(/[^a-zA-Z0-9/ ]/g, '')
.trim()
.replace(/\s/g, '-')
.toLowerCase();
}
export const apiCategories = cache(async (): Promise<APICategory[]> => {
const data = await resolveOpenAPI();
const categoryMap: {[name: string]: APICategory} = {};
data.tags.forEach(tag => {
categoryMap[tag.name] = {
name: tag['x-sidebar-name'] || tag.name,
slug: slugify(tag.name),
description: tag.description,
apis: [],
};
});
Object.entries(data.paths).forEach(([apiPath, methods]) => {
Object.entries(methods).forEach(([method, apiData]) => {
apiData.tags.forEach(tag => {
categoryMap[tag].apis.push({
apiPath,
method,
name: apiData.operationId,
slug: slugify(apiData.operationId),
summary: apiData.summary,
descriptionMarkdown: apiData.description,
pathParameters: apiData.parameters.filter(
p => p.in === 'path'
) as APIParameter[],
queryParameters: apiData.parameters.filter(
p => p.in === 'query'
) as APIParameter[],
requestBodyContent: {
example:
apiData.requestBody?.content &&
Object.values(apiData.requestBody.content)[0].example,
},
bodyContentType: getBodyContentType(apiData),
bodyParameters: getBodyParameters(apiData),
security: apiData.security,
responses: Object.entries(apiData.responses)
.map(([status_code, response]) => ({
status_code,
...response,
}))
.map(response => {
const {content, ...rest} = response;
return {
content:
content &&
Object.entries(content).map(([content_type, contentData]) => ({
content_type,
...contentData,
}))[0],
...rest,
};
}),
});
});
});
});
const categories = Object.values(categoryMap);
categories.sort((a, b) => a.name.localeCompare(b.name));
categories.forEach(c => {
c.apis.sort((a, b) => a.name.localeCompare(b.name));
});
return categories;
});
function getBodyParameters(apiData): APIParameter[] {
const content = apiData.requestBody?.content;
const contentType = content && Object.values(content)[0];
const properties = contentType?.schema?.properties;
if (!properties) {
return [];
}
const required: string[] = contentType?.schema?.required || [];
return Object.entries(properties).map(([name, props]: [string, any]) => ({
name,
description: props.description,
required: required.includes(name),
schema: {
type: props.type,
format: '',
items: props.items,
},
}));
}
function getBodyContentType(apiData): string | undefined {
const content = apiData.requestBody?.content;
const types = content && Object.keys(content);
if (!types?.length) {
return undefined;
}
return types[0];
}