-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswaggerparser-service.js
108 lines (92 loc) · 4.51 KB
/
swaggerparser-service.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
;(function (angular) {
'use strict';
angular.module('swaggerparser-service', [
'constants-service'
])
.factory('swaggerParser', function (constants) {
function extractSchemaName($ref) {
var captured = /^#\/definitions\/(.+)$/.exec($ref);
if (captured) {
return captured[1];
}
}
return {
/**
* @returns {string|{api:object,endpoints:[]}}
*/
parse: function (swagger) {
var spec,
api = {},
endpoints = [];
try {
spec = angular.fromJson(swagger);
} catch (ignored) {
return 'Your Swagger file is not valid JSON.';
}
try {
api.title = spec.info.title;
api.version = spec.info.version;
api.description = spec.info.description;
api.host = spec.host;
api.serviceName = spec['x-serviceName'];
if (spec.securityDefinitions.basic) {
api.authType = constants.AuthType.BASIC;
} else if (spec.securityDefinitions.oauth2) {
api.authType = constants.AuthType.OAUTH;
}
if (spec.host.indexOf('travel.cloud') > -1) {
api.platform = constants.Platform.TRAVEL_CLOUD;
} else if (spec.host.indexOf('pioneer.io') > -1) {
api.platform = constants.Platform.PIONEER;
} else {
return 'Host must be a subdomain of travel.cloud or pioneer.io';
}
angular.forEach(spec.paths, function (methods, path) {
angular.forEach(methods, function (methodDetails, method) {
var endpoint = {
path: path,
method: method.toUpperCase()
},
queryParams = [];
angular.forEach(methodDetails.parameters, function (param) {
if (param.in === 'query') {
queryParams.push(param.name + '={' + param.name + '}');
}
if (param.in === 'body') {
endpoint.request = {
schema: extractSchemaName(param.schema.$ref),
description: param.description
};
}
});
if (queryParams.length) {
endpoint.path += '?' + queryParams.join('&');
}
angular.forEach(methodDetails.responses, function (response, status) {
if (status !== 'default') {
endpoint.response = {
status: status,
description: response.description
};
if (response.schema) {
endpoint.response.schema = extractSchemaName(response.schema.$ref);
}
if (response.headers && response.headers.Location) {
endpoint.response.locationHeader = true;
}
}
});
endpoints.push(endpoint);
});
});
} catch (error) {
return 'Sorry, something went wrong: ' + error.toString();
}
return {
api: api,
endpoints: endpoints
};
}
};
});
}(angular));