This repository has been archived by the owner on Jun 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
core.js
182 lines (157 loc) · 4.7 KB
/
core.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
var $ = require('zepto-browserify').$;
var name = 'WP REST API';
var baseUrl = 'https://public-api.wordpress.com/';
var namespaces = [ 'wp/v2', 'wpcom/v2' ];
function getRequestUrl(request) {
return baseUrl + request.version + request.path;
}
function getDiscoveryUrl(version) {
return baseUrl + version;
}
function parseEndpoints(data) {
var endpoints = [];
$.each(data.routes, function(url, route) {
// Drop the /wp/v2 or /wpcom/v2
var rawpath = url.substr(data.namespace.length + 1);
$.each(route.endpoints, function(_, rawEndpoint) {
$.each(rawEndpoint.methods, function(_, method) {
// Parsing Query
var query = {};
$.each(rawEndpoint.args, function(key, arg) {
query[key] = {
description: {
view: arg.description
}
};
});
// Parsing path
var path = {};
var paramRegex = /\([^\(\)]*\)/g;
var parameters = rawpath.match(paramRegex) || [];
var computedPath = rawpath;
$.each(parameters, function(_, param) {
var paramDetailsRegex = /[^\<]*<([^\>]*)>\[([^\]]*)\][^]*/;
var explodedParameter = param.match(paramDetailsRegex);
var paramName = '$' + explodedParameter[1];
path[paramName] = {
description: '',
type: explodedParameter[2]
};
computedPath = computedPath.replace(param, paramName);
});
computedPath = computedPath || '/';
var docs = guessEndpointDocumentation(method, data.namespace, computedPath);
var endpoint = {
group: docs.group,
description: docs.description,
method: method,
path_format: computedPath,
path_labeled: computedPath,
request: {
body: [],
query: query,
path: path
}
};
endpoints.push(endpoint);
});
});
});
return endpoints;
}
function loadVersions(send, callback) {
callback(undefined, namespaces, namespaces[0]);
}
function guessEndpointDocumentation(method, namespace, computedPath) {
// Try to guess some info about the endpoints
var group = '';
var groupPlural = '';
var groupSingular = '';
var description = '';
var verbMatch = computedPath.match(/^(\/?sites\/[\$\w.]+)?\/([\w-]*)(\/|$)/);
if (verbMatch) {
group = verbMatch[2];
switch (group) {
case 'media':
groupPlural = 'media items';
break;
case 'feedback':
groupPlural = 'feedback posts';
break;
case 'types':
groupPlural = 'post types';
break;
case 'statuses':
groupPlural = 'post statuses';
break;
default:
groupPlural = group;
break;
}
if (group === 'statuses') {
groupSingular = 'post status';
} else if (group === 'sites' || computedPath === '/') {
group = 'auto-discovery';
} else {
groupSingular = groupPlural.replace(/ies$/, 'y').replace(/s$/, '');
}
function getDescription() {
if (group === 'auto-discovery') {
if (computedPath === '/') {
return 'List endpoints in the ' + namespace + ' namespace';
} else {
return 'List endpoints in the ' + namespace + ' namespace (site-specific)';
}
}
if (namespace === 'wp/v2') {
if (group === 'settings') {
switch (method) {
case 'GET':
return 'Get site settings';
default:
return 'Edit site settings';
}
}
if (/\/users\/me$/.test(computedPath)) {
return 'Get the current user';
}
if (/\/revisions(\/|$)/.test(computedPath)) {
groupPlural = 'revisions of a ' + groupSingular;
groupSingular = 'revision of a ' + groupSingular;
}
if (/\$(id|status|taxonomy|type)$/.test(computedPath)) {
switch (method) {
case 'GET':
return 'Get a ' + groupSingular;
case 'POST':
case 'PUT':
case 'PATCH':
return 'Edit a ' + groupSingular;
case 'DELETE':
return 'Delete a ' + groupSingular;
}
} else {
switch (method) {
case 'GET':
return 'List ' + groupPlural;
case 'POST':
return 'Create a ' + groupSingular;
}
}
}
}
description = getDescription() || '';
}
return {
group: group,
description: description,
};
}
module.exports = {
name: name,
baseUrl: baseUrl,
getRequestUrl: getRequestUrl,
getDiscoveryUrl: getDiscoveryUrl,
parseEndpoints: parseEndpoints,
loadVersions: loadVersions
};