durandal-es6
Version:
Durandal is a cross-device, cross-platform client framework written in JavaScript and designed to make Single Page Applications (SPAs) easy to create and maintain. This framework replaces the dependency on RequireJS and instead uses ES modules.
127 lines (121 loc) • 5.29 kB
JavaScript
import $ from "jquery";
import ko from "knockout";
/**
* Enables common http request scenarios.
* @module http
* @requires jquery
* @requires knockout
*/
function HttpModule() {
/**
* @class HTTPModule
* @static
*/
return {
/**
* The name of the callback parameter to inject into jsonp requests by default.
* @property {string} callbackParam
* @default callback
*/
callbackParam: "callback",
/**
* Converts the data to JSON.
* @method toJSON
* @param {object} data The data to convert to JSON.
* @return {string} JSON.
*/
toJSON(data) {
return ko.toJSON(data);
},
/**
* Makes an HTTP GET request.
* @method get
* @param {string} url The url to send the get request to.
* @param {object} [query] An optional key/value object to transform into query string parameters.
* @param {object} [headers] The data to add to the request header. It will be converted to JSON. If the data contains Knockout observables, they will be converted into normal properties before serialization.
* @return {Promise} A promise of the get response data.
*/
get(url, query, headers) {
return $.ajax(url, { data: query, headers: ko.toJS(headers) });
},
/**
* Makes an JSONP request.
* @method jsonp
* @param {string} url The url to send the get request to.
* @param {object} [query] An optional key/value object to transform into query string parameters.
* @param {string} [callbackParam] The name of the callback parameter the api expects (overrides the default callbackParam).
* @param {object} [headers] The data to add to the request header. It will be converted to JSON. If the data contains Knockout observables, they will be converted into normal properties before serialization.
* @return {Promise} A promise of the response data.
*/
jsonp(url, query, callbackParam, headers) {
if (url.indexOf("=?") == -1) {
callbackParam = callbackParam || this.callbackParam;
if (url.indexOf("?") == -1) {
url += "?";
} else {
url += "&";
}
url += `${callbackParam}=?`;
}
return $.ajax({
url,
dataType: "jsonp",
data: query,
headers: ko.toJS(headers),
});
},
/**
* Makes an HTTP PUT request.
* @method put
* @param {string} url The url to send the put request to.
* @param {object} data The data to put. It will be converted to JSON. If the data contains Knockout observables, they will be converted into normal properties before serialization.
* @param {object} [headers] The data to add to the request header. It will be converted to JSON. If the data contains Knockout observables, they will be converted into normal properties before serialization.
* @return {Promise} A promise of the response data.
*/
put(url, data, headers) {
return $.ajax({
url,
data: this.toJSON(data),
type: "PUT",
contentType: "application/json",
dataType: "json",
headers: ko.toJS(headers),
});
},
/**
* Makes an HTTP POST request.
* @method post
* @param {string} url The url to send the post request to.
* @param {object} data The data to post. It will be converted to JSON. If the data contains Knockout observables, they will be converted into normal properties before serialization.
* @param {object} [headers] The data to add to the request header. It will be converted to JSON. If the data contains Knockout observables, they will be converted into normal properties before serialization.
* @return {Promise} A promise of the response data.
*/
post(url, data, headers) {
return $.ajax({
url,
data: this.toJSON(data),
type: "POST",
contentType: "application/json",
dataType: "json",
headers: ko.toJS(headers),
});
},
/**
* Makes an HTTP DELETE request.
* @method remove
* @param {string} url The url to send the delete request to.
* @param {object} [query] An optional key/value object to transform into query string parameters.
* @param {object} [headers] The data to add to the request header. It will be converted to JSON. If the data contains Knockout observables, they will be converted into normal properties before serialization.
* @return {Promise} A promise of the get response data.
*/
remove(url, query, headers) {
return $.ajax({
url,
data: query,
type: "DELETE",
headers: ko.toJS(headers),
});
},
};
}
export default HttpModule();