-
Notifications
You must be signed in to change notification settings - Fork 0
/
channel-resource-parser.js
41 lines (37 loc) · 1.12 KB
/
channel-resource-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
let channelViewParamsRegex = /^([^\(]*)\((.*)\):([^:]*)$/;
let crudChannelRegex = /^crud>(.*)$/;
module.exports.parseChannelResourceQuery = function (channelName) {
if (typeof channelName !== 'string') {
return null;
}
let channelMatches = channelName.match(crudChannelRegex);
if (channelMatches && channelMatches[1]) {
let resourceString = channelMatches[1];
if (resourceString.indexOf(':') !== -1) {
// If resource is a view.
let viewMatches = resourceString.match(channelViewParamsRegex);
if (!viewMatches) return null;
let viewResource = {
view: viewMatches[1],
type: viewMatches[3]
}
try {
viewResource.viewParams = JSON.parse(viewMatches[2]);
} catch (e) {}
return viewResource;
}
// If resource is a simple model.
let resourceParts = resourceString.split('/');
let modelResource = {
type: resourceParts[0]
};
if (resourceParts[1]) {
modelResource.id = resourceParts[1];
}
if (resourceParts[2]) {
modelResource.field = resourceParts[2];
}
return modelResource;
}
return null;
};