-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathjson2wadl.js
More file actions
171 lines (153 loc) · 6.43 KB
/
json2wadl.js
File metadata and controls
171 lines (153 loc) · 6.43 KB
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
//
// Transform a wadl description in json to a wadl description in xml
// NOTE: the json representation supports only a subset of WADL
//
// The following is an example of WADL xml
//<?xml version="1.0" encoding="UTF-8"?>
//<application xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
// xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:apigee="http://api.apigee.com/wadl/2010/07/"
// xmlns="http://wadl.dev.java.net/2009/02" xmlns:t="urn:github:githubresponse"
// xsi:schemaLocation="http://wadl.dev.java.net/2009/02 http://apigee.com/schemas/wadl-schema.xsd http://api.apigee.com/wadl/2010/07/ http://apigee.com/schemas/apigee-wadl-extensions.xsd">
// <resources base="http://github.com/">
// <resource path="{format}/user/search/{username}">
// <param name="format" type="xsd:string" style="template" required="true" default="json">
// <option value="xml" mediaType="application/xml"/>
// <option value="json" mediaType="application/json"/>
// <option value="yaml" mediaType="application/yaml"/>
// </param>
// <param name="username" required="true" type="xsd:string" style="query" default="chacon"/>
// <method id="GetUsers" apigee:displayName="GetUsers" name="GET">
// <apigee:tags>
// <apigee:tag primary="true">Users</apigee:tag>
// </apigee:tags>
// <apigee:authentication required="false" />
// <apigee:example url="api/v2/{format}/user/search/{userName}"/>
// <doc title="" apigee:url="http://develop.github.com/p/users">
// Returns the users using their username.
// </doc>
// </method>
// </resource>
// </resources>
//</application>
//var desc = {
// path: ':format/user/search/:username',
// method: 'GET',
// id: 'GetUsers',
// doc: 'Returns the users using their username.',
// params: {
// format: {
// style: 'template',
// type: 'string',
// required: true,
// defaultValue: 'json',
// options: {
// 'json': '',
// 'xml': ''
// }
// },
// username: {
// style: 'query',
// type: 'string',
// required: true,
// defaultValue: 'chacon'
// }
// }
//}
var builder = require('xmlbuilder');
var sinatraPathRegex = /:(.*?)(\/|$)/g;
var WadlHelper = {
sinatraPathToWADL: function (path) {
return path.replace(sinatraPathRegex, "{$1}$2");
},
toXsdType: function (type) {
return "xsd:" + type.toLowerCase();
},
createWADLDoc: function (baseUrl) {
return builder.create('application', { 'version': '1.0', 'encoding': 'UTF-8', 'standalone': true })
.att("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance")
.att("xmlns:xsd", "http://www.w3.org/2001/XMLSchema")
.att("xmlns:apigee", "http://api.apigee.com/wadl/2010/07/")
.att("xmlns", "http://wadl.dev.java.net/2009/02")
.att("xsi:schemaLocation", "http://wadl.dev.java.net/2009/02 http://apigee.com/schemas/wadl-schema.xsd http://api.apigee.com/wadl/2010/07/ http://apigee.com/schemas/apigee-wadl-extensions.xsd")
.ele('resources')
.att('base', baseUrl);
},
addParamsToElement: function (params, element) {
var param,
paramDesc,
option,
paramEle;
for (param in params) {
paramDesc = params[param];
paramEle = element.ele('param', { 'name': param });
if (paramDesc.type) paramEle.att('type', this.toXsdType(paramDesc.type));
if (paramDesc.style) paramEle.att('style', paramDesc.style);
if (paramDesc.required) paramEle.att('required', paramDesc.required);
if (paramDesc.defaultValue) paramEle.att('default', paramDesc.defaultValue);
if (paramDesc.doc) paramEle.ele('doc', paramDesc.doc);
if (paramDesc.options) {
for (option in paramDesc.options) {
paramEle.ele('option', { 'value': option });
}
}
}
},
addMethodToWADLDoc: function (methodJson, resoucesElement) {
var path = this.sinatraPathToWADL(methodJson.path),
resource = resoucesElement.ele('resource'),
representation,
representationEle,
requestEle,
responseEle,
methodEle,
i,
l;
resource.att('path', path);
if (methodJson.params) {
this.addParamsToElement(methodJson.params, resource);
}
methodEle = resource.ele('method');
if (methodJson.id) methodEle.att('id', methodJson.id);
methodEle.att('name', methodJson.method); // required
if (methodJson.doc) methodEle.ele('doc', methodJson.doc);
if (methodJson.request) {
requestEle = methodEle.ele('request');
if (methodJson.request.doc) requestEle.ele('doc', methodJson.request.doc);
if (methodJson.request.params) {
this.addParamsToElement(methodJson.request.params, requestEle);
}
if (methodJson.request.representations) {
for(i = 0, l = methodJson.request.representations.length; i < l; i++) {
representation = methodJson.request.representations[i];
representationEle = requestEle.ele('representation ').att('mediaType',representation );
}
}
}
if (methodJson.response) {
responseEle = methodEle.ele('response');
if (methodJson.response.doc) responseEle.ele('doc', methodJson.response.doc);
if (methodJson.response.representations) {
for(i = 0, l = methodJson.response.representations.length; i < l; i++) {
representation = methodJson.response.representations[i];
representationEle = responseEle.ele('representation ').att('mediaType',representation );
}
}
}
return resource.up();
}
};
exports.toWadl = function (methodJsons, baseUrl, format) {
var resoucesElement = WadlHelper.createWADLDoc(baseUrl),
doc = resoucesElement.doc(),
methodJson,
i,
l;
for (i = 0, l = methodJsons.length; i < l; i++) {
methodJson = methodJsons[i];
WadlHelper.addMethodToWADLDoc(methodJson, resoucesElement);
}
if (!format) {
format = { 'pretty': true, 'indent': ' ', 'newline': '\n' };
}
return doc.toString(format);
};