-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
139 lines (128 loc) · 3.64 KB
/
index.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
var URL = URL ||
require('urlutils');
var XMLHttpRequest = XMLHttpRequest ||
require('xmlhttprequest').XMLHttpRequest;
if (typeof Promise === 'undefined') {
require('es6-shim');
}
var urlbuilder = require('urlbuilder');
var merge = function () {
return Array.prototype.slice.call(arguments)
.reduce(function (merged, argument) {
Object.keys(argument)
.forEach(function (key) {
merged[key] = argument[key];
});
return merged;
}, {});
};
var relativeUrl = function (endpoint, base) {
var baseUrl = new URL(base);
var endpointUrl = new URL(endpoint, baseUrl.origin + baseUrl.pathname);
if (baseUrl.search) {
endpointUrl.search = '?' +
baseUrl.search.substr(1) + '&' +
endpointUrl.search.substr(1);
}
return endpointUrl;
};
var isString = function (object) {
return typeof object === 'string';
};
var isObject = function (object) {
var type = typeof object;
return object !== null &&
(type === 'object' || type === 'function') &&
Array.isArray(object) === false;
};
var Dinkel = function (base, endpoints, options) {
if (!(this instanceof Dinkel)) {
return new Dinkel(base, endpoints, options);
}
endpoints.reduce(function (memo, endpoint) {
if (isString(endpoint)) {
memo.push({
url: relativeUrl(endpoint, base),
options: options
});
} else if (isObject(endpoint)) {
Object.keys(endpoint)
.forEach(function (url) {
memo.push({
url: relativeUrl(url, base),
options: merge(options, endpoint[url])
});
});
}
return memo;
}, [])
.forEach(function (endpoint) {
var humanize = endpoint.options.humanize || Dinkel.humanize;
var label = humanize.call(this, endpoint.url, endpoint.options);
this[label] = new Handler(endpoint.url, endpoint.options);
}, this);
};
Dinkel.ajax = function (method, url, data) {
return new Promise(function (resolve, reject) {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
resolve(JSON.parse(xhr.responseText));
} else {
reject(Error(xhr.statusText));
}
}
};
xhr.open(method, url);
if (this.beforeSend(xhr) !== false) {
xhr.send(data);
}
}.bind(this));
};
Dinkel.slug = /:(\w+)/g;
Dinkel.humanize = function (url, options) {
return url.pathname
.split('/')
// Skip slug patterns
.filter(function (part) {
return part.length && !(options.slug || Dinkel.slug).test(part);
})
// TitleCase
.map(function (str) {
return str.substr(0, 1).toUpperCase() +
str.substr(1).toLowerCase();
})
.join('');
};
var Handler = function (url, options) {
this.url = url;
this.ajax = options.ajax || Dinkel.ajax;
this.beforeSend = options.beforeSend || Function();
};
'CONNECT DELETE GET HEAD OPTIONS POST PUT TRACE TRACK'
.toLowerCase()
.split(/\s+/)
.forEach(function (method) {
Handler.prototype[method] = function (params, data, response) {
if (typeof response === 'undefined') {
response = {};
}
var promise = new Promise(function (resolve, reject) {
var url = urlbuilder(this.url.href, params);
this.ajax.call(this, method, url, data)
.then(function (json) {
Object.keys(json).forEach(function (key) {
response[key] = json[key];
});
resolve(response);
})
.catch(reject);
}.bind(this));
promise.response = function () {
return response;
};
return promise;
};
});
module.exports = Dinkel;