This repository was archived by the owner on Nov 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathparser.js
254 lines (195 loc) · 6.78 KB
/
parser.js
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
const url = require('url');
// eslint-disable-next-line import/no-unresolved
const ApiaryBlueprintParser = require('./apiary-blueprint-parser');
ApiaryBlueprintParser.ast = require('./ast');
class Parser {
constructor({ namespace, source }) {
this.namespace = namespace;
this.source = source;
}
parse() {
const {
Annotation, Category, Copy, ParseResult, Link,
} = this.namespace.elements;
this.result = new ParseResult();
try {
this.blueprint = ApiaryBlueprintParser.parse(this.source);
} catch (err) {
const annotation = new Annotation(err.message);
annotation.classes.push('error');
this.result.push(annotation);
if (err.offset) {
const { SourceMap } = this.namespace.elements;
annotation.attributes.set('sourceMap', [
new SourceMap([[err.offset, 1]]),
]);
}
return this.result;
}
const link = new Link();
link.title = 'Apiary Blueprint';
link.relation = 'via';
link.href = 'https://apiary.io/blueprint';
this.result.links.push(link);
this.api = new Category();
this.api.classes.push('api');
this.api.title = this.blueprint.name;
if (this.blueprint.location) {
const { Member: MemberElement } = this.namespace.elements;
const member = new MemberElement('HOST', this.blueprint.location);
member.meta.set('classes', ['user']);
this.api.attributes.set('metadata', [member]);
}
if (this.blueprint.description) {
const description = new Copy(this.blueprint.description);
this.api.push(description);
}
this.blueprint.sections.forEach((section) => {
const group = this.handleSection(section);
this.api.content.push(group);
});
this.result.push(this.api);
return this.result;
}
// Parses a URL from the Apiary Blueprint AST
// Depending on the contents of the blueprint location it may need removing paths
parseURL(path) {
if (this.blueprint.location) {
const host = url.parse(this.blueprint.location);
if (host.path && host.path !== '/' && path.startsWith(host.path)) {
const newPath = path.slice(host.path.length);
if (!newPath.startsWith('/')) {
return `/${newPath}`;
}
return newPath;
}
}
return path;
}
handleSection(section) {
// A "resource" in Apiary blueprint isn't the same as a "resource" in API
// Elements. It maps closer to an "action".
const { Copy, Category, Resource } = this.namespace.elements;
const group = new Category();
if (section.name) {
group.title = section.name;
}
group.classes.push('resourceGroup');
if (section.description) {
group.content.push(new Copy(section.description));
}
if (section.resources.length > 0) {
// A section in Apiary Blueprint actually contains "actions" not "resources"
// We will use the first "resource" (actually "action") for the name.
const resource = new Resource();
resource.href = this.parseURL(section.resources[0].url);
section.resources.forEach((action) => {
const transition = this.handleResource(action);
if (action.url !== resource.href) {
transition.href = this.parseURL(action.url);
}
resource.push(transition);
});
group.push(resource);
}
return group;
}
handleResource(resource) {
const { Copy, Transition, HttpTransaction } = this.namespace.elements;
const transition = new Transition();
transition.title = resource.method;
const schema = this.retrieveSchema(resource.method, this.parseURL(resource.url));
if (resource.description) {
transition.push(new Copy(resource.description));
}
const request = this.handleRequest(resource, resource.request, schema.request);
resource.responses.forEach((response) => {
const transaction = new HttpTransaction();
transaction.push(request);
transaction.push(this.handleResponse(response, schema.response));
transition.push(transaction);
});
return transition;
}
handleRequest(resource, request, schema) {
const { Asset, HttpRequest } = this.namespace.elements;
const httpRequest = new HttpRequest();
httpRequest.method = resource.method;
const headers = this.handleHeaders(request.headers);
if (headers) {
httpRequest.headers = headers;
}
if (request.body) {
const bodyAsset = new Asset(request.body);
bodyAsset.classes.push('messageBody');
httpRequest.push(bodyAsset);
if (schema) {
httpRequest.push(this.handleSchema(schema));
}
}
return httpRequest;
}
handleResponse(response, schema) {
const { Asset, HttpResponse } = this.namespace.elements;
const httpResponse = new HttpResponse();
httpResponse.statusCode = response.status;
const headers = this.handleHeaders(response.headers);
if (headers) {
httpResponse.headers = headers;
}
if (response.body) {
const bodyAsset = new Asset(response.body);
bodyAsset.classes.push('messageBody');
httpResponse.push(bodyAsset);
if (schema) {
httpResponse.push(this.handleSchema(schema));
}
}
return httpResponse;
}
handleHeaders(headers) {
if (Object.keys(headers).length === 0) {
return null;
}
const { HttpHeaders } = this.namespace.elements;
const { Member: MemberElement } = this.namespace.elements;
const httpHeaders = new HttpHeaders();
Object.keys(headers).forEach((header) => {
httpHeaders.push(new MemberElement(header, headers[header]));
});
return httpHeaders;
}
// Create schema asset
handleSchema(body) {
const { Asset } = this.namespace.elements;
const asset = new Asset(body);
asset.classes.push('messageBodySchema');
asset.contentType = 'application/schema+json';
return asset;
}
// Search for a schema by method and path in the blueprints validations
retrieveSchema(method, path) {
const schema = { request: null, response: null };
this.blueprint.validations.forEach((validation) => {
if (validation.method === method && validation.url === path) {
let validationSchema;
try {
validationSchema = JSON.parse(validation.body);
} catch (error) {
// Handle the invalid schema for schema.request
}
if (validationSchema && validationSchema.request) {
schema.request = JSON.stringify(validationSchema.request, null, 2);
}
if (validationSchema && validationSchema.response) {
schema.response = JSON.stringify(validationSchema.response, null, 2);
}
if (schema.request === null && schema.response === null) {
schema.request = validation.body;
}
}
});
return schema;
}
}
module.exports = Parser;