2016-04-07 13:43:43 +02:00
|
|
|
(function (global, factory) {
|
|
|
|
if (typeof define === "function" && define.amd) {
|
2016-04-18 02:05:36 +02:00
|
|
|
define('ss.silverstripe-backend', ['exports', 'isomorphic-fetch', 'es6-promise', 'qs', 'merge'], factory);
|
2016-04-07 13:43:43 +02:00
|
|
|
} else if (typeof exports !== "undefined") {
|
2016-04-18 02:05:36 +02:00
|
|
|
factory(exports, require('isomorphic-fetch'), require('es6-promise'), require('qs'), require('merge'));
|
2016-04-07 13:43:43 +02:00
|
|
|
} else {
|
|
|
|
var mod = {
|
|
|
|
exports: {}
|
|
|
|
};
|
2016-04-18 02:05:36 +02:00
|
|
|
factory(mod.exports, global.isomorphicFetch, global.es6Promise, global.qs, global.merge);
|
2016-04-07 13:43:43 +02:00
|
|
|
global.ssSilverstripeBackend = mod.exports;
|
|
|
|
}
|
2016-04-18 02:05:36 +02:00
|
|
|
})(this, function (exports, _isomorphicFetch, _es6Promise, _qs, _merge) {
|
2016-04-07 13:43:43 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
Object.defineProperty(exports, "__esModule", {
|
|
|
|
value: true
|
|
|
|
});
|
|
|
|
|
|
|
|
var _isomorphicFetch2 = _interopRequireDefault(_isomorphicFetch);
|
|
|
|
|
|
|
|
var _es6Promise2 = _interopRequireDefault(_es6Promise);
|
|
|
|
|
2016-04-11 08:21:33 +02:00
|
|
|
var _qs2 = _interopRequireDefault(_qs);
|
|
|
|
|
2016-04-18 02:05:36 +02:00
|
|
|
var _merge2 = _interopRequireDefault(_merge);
|
|
|
|
|
2016-04-07 13:43:43 +02:00
|
|
|
function _interopRequireDefault(obj) {
|
|
|
|
return obj && obj.__esModule ? obj : {
|
|
|
|
default: obj
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-04-12 04:31:14 +02:00
|
|
|
function _defineProperty(obj, key, value) {
|
|
|
|
if (key in obj) {
|
|
|
|
Object.defineProperty(obj, key, {
|
|
|
|
value: value,
|
|
|
|
enumerable: true,
|
|
|
|
configurable: true,
|
|
|
|
writable: true
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
obj[key] = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2016-04-07 13:43:43 +02:00
|
|
|
function _classCallCheck(instance, Constructor) {
|
|
|
|
if (!(instance instanceof Constructor)) {
|
|
|
|
throw new TypeError("Cannot call a class as a function");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var _createClass = function () {
|
|
|
|
function defineProperties(target, props) {
|
|
|
|
for (var i = 0; i < props.length; i++) {
|
|
|
|
var descriptor = props[i];
|
|
|
|
descriptor.enumerable = descriptor.enumerable || false;
|
|
|
|
descriptor.configurable = true;
|
|
|
|
if ("value" in descriptor) descriptor.writable = true;
|
|
|
|
Object.defineProperty(target, descriptor.key, descriptor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return function (Constructor, protoProps, staticProps) {
|
|
|
|
if (protoProps) defineProperties(Constructor.prototype, protoProps);
|
|
|
|
if (staticProps) defineProperties(Constructor, staticProps);
|
|
|
|
return Constructor;
|
|
|
|
};
|
|
|
|
}();
|
|
|
|
|
|
|
|
_es6Promise2.default.polyfill();
|
|
|
|
|
|
|
|
function checkStatus(response) {
|
|
|
|
var ret = void 0;
|
|
|
|
var error = void 0;
|
|
|
|
if (response.status >= 200 && response.status < 300) {
|
|
|
|
ret = response;
|
|
|
|
} else {
|
|
|
|
error = new Error(response.statusText);
|
|
|
|
error.response = response;
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
var SilverStripeBackend = function () {
|
|
|
|
function SilverStripeBackend() {
|
|
|
|
_classCallCheck(this, SilverStripeBackend);
|
|
|
|
|
|
|
|
this.fetch = _isomorphicFetch2.default;
|
|
|
|
}
|
|
|
|
|
|
|
|
_createClass(SilverStripeBackend, [{
|
2016-04-11 08:21:33 +02:00
|
|
|
key: 'createEndpointFetcher',
|
|
|
|
value: function createEndpointFetcher(endpointSpec) {
|
|
|
|
var _this = this;
|
|
|
|
|
|
|
|
function encode(contentType, data) {
|
|
|
|
switch (contentType) {
|
2016-04-18 02:05:36 +02:00
|
|
|
case 'application/x-www-form-urlencoded':
|
2016-04-11 08:21:33 +02:00
|
|
|
return _qs2.default.stringify(data);
|
|
|
|
|
|
|
|
case 'application/json':
|
|
|
|
case 'application/x-json':
|
|
|
|
case 'application/x-javascript':
|
|
|
|
case 'text/javascript':
|
|
|
|
case 'text/x-javascript':
|
|
|
|
case 'text/x-json':
|
|
|
|
return JSON.stringify(data);
|
|
|
|
|
|
|
|
default:
|
|
|
|
throw new Error('Can\'t encode format: ' + contentType);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function decode(contentType, text) {
|
|
|
|
switch (contentType) {
|
2016-04-18 02:05:36 +02:00
|
|
|
case 'application/x-www-form-urlencoded':
|
2016-04-11 08:21:33 +02:00
|
|
|
return _qs2.default.parse(text);
|
|
|
|
|
|
|
|
case 'application/json':
|
|
|
|
case 'application/x-json':
|
|
|
|
case 'application/x-javascript':
|
|
|
|
case 'text/javascript':
|
|
|
|
case 'text/x-javascript':
|
|
|
|
case 'text/x-json':
|
|
|
|
return JSON.parse(text);
|
|
|
|
|
|
|
|
default:
|
|
|
|
throw new Error('Can\'t decode format: ' + contentType);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function addQuerystring(url, querystring) {
|
2016-04-12 04:31:14 +02:00
|
|
|
if (querystring === '') {
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (url.match(/\?/)) {
|
|
|
|
return url + '&' + querystring;
|
|
|
|
}
|
|
|
|
|
2016-04-11 08:21:33 +02:00
|
|
|
return url + '?' + querystring;
|
|
|
|
}
|
|
|
|
|
|
|
|
function parseResponse(response) {
|
|
|
|
return response.text().then(function (body) {
|
|
|
|
return decode(response.headers.get('Content-Type'), body);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-04-12 04:31:14 +02:00
|
|
|
function applySchemaToData(payloadSchema, data) {
|
|
|
|
return Object.keys(data).reduce(function (prev, key) {
|
|
|
|
var schema = payloadSchema[key];
|
|
|
|
|
|
|
|
if (schema && (schema.remove === true || schema.querystring === true)) {
|
|
|
|
return prev;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Object.assign(prev, _defineProperty({}, key, data[key]));
|
|
|
|
}, {});
|
|
|
|
}
|
|
|
|
|
|
|
|
function applySchemaToUrl(payloadSchema, url, data) {
|
|
|
|
var opts = arguments.length <= 3 || arguments[3] === undefined ? { setFromData: false } : arguments[3];
|
|
|
|
|
|
|
|
var newUrl = url;
|
|
|
|
|
|
|
|
var queryData = Object.keys(data).reduce(function (prev, key) {
|
|
|
|
var schema = payloadSchema[key];
|
|
|
|
var includeThroughSetFromData = opts.setFromData === true && !(schema && schema.remove === true);
|
|
|
|
var includeThroughSpec = schema && schema.querystring === true && schema.remove !== true;
|
|
|
|
if (includeThroughSetFromData || includeThroughSpec) {
|
|
|
|
return Object.assign(prev, _defineProperty({}, key, data[key]));
|
|
|
|
}
|
|
|
|
|
|
|
|
return prev;
|
|
|
|
}, {});
|
|
|
|
|
2016-04-18 02:05:36 +02:00
|
|
|
newUrl = addQuerystring(newUrl, encode('application/x-www-form-urlencoded', queryData));
|
2016-04-12 04:31:14 +02:00
|
|
|
|
|
|
|
newUrl = Object.keys(payloadSchema).reduce(function (prev, key) {
|
|
|
|
var replacement = payloadSchema[key].urlReplacement;
|
|
|
|
if (replacement) {
|
|
|
|
return prev.replace(replacement, data[key]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return prev;
|
|
|
|
}, newUrl);
|
|
|
|
|
|
|
|
return newUrl;
|
|
|
|
}
|
|
|
|
|
2016-04-11 08:21:33 +02:00
|
|
|
var refinedSpec = Object.assign({
|
|
|
|
method: 'get',
|
2016-04-18 02:05:36 +02:00
|
|
|
payloadFormat: 'application/x-www-form-urlencoded',
|
2016-04-12 04:31:14 +02:00
|
|
|
responseFormat: 'application/json',
|
2016-04-18 02:05:36 +02:00
|
|
|
payloadSchema: {},
|
|
|
|
defaultData: {}
|
2016-04-11 08:21:33 +02:00
|
|
|
}, endpointSpec);
|
|
|
|
|
|
|
|
var formatShortcuts = {
|
|
|
|
json: 'application/json',
|
2016-04-18 02:05:36 +02:00
|
|
|
urlencoded: 'application/x-www-form-urlencoded'
|
2016-04-11 08:21:33 +02:00
|
|
|
};
|
|
|
|
['payloadFormat', 'responseFormat'].forEach(function (key) {
|
|
|
|
if (formatShortcuts[refinedSpec[key]]) refinedSpec[key] = formatShortcuts[refinedSpec[key]];
|
|
|
|
});
|
|
|
|
|
2016-04-18 02:05:36 +02:00
|
|
|
return function () {
|
|
|
|
var data = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];
|
|
|
|
|
2016-04-11 08:21:33 +02:00
|
|
|
var headers = {
|
|
|
|
Accept: refinedSpec.responseFormat,
|
|
|
|
'Content-Type': refinedSpec.payloadFormat
|
|
|
|
};
|
|
|
|
|
2016-04-18 02:05:36 +02:00
|
|
|
var mergedData = _merge2.default.recursive({}, refinedSpec.defaultData, data);
|
|
|
|
|
|
|
|
var url = applySchemaToUrl(refinedSpec.payloadSchema, refinedSpec.url, mergedData, { setFromData: refinedSpec.method.toLowerCase() === 'get' });
|
2016-04-12 04:31:14 +02:00
|
|
|
|
2016-04-18 02:05:36 +02:00
|
|
|
var encodedData = encode(refinedSpec.payloadFormat, applySchemaToData(refinedSpec.payloadSchema, mergedData));
|
2016-04-12 04:31:14 +02:00
|
|
|
|
2016-04-18 02:05:36 +02:00
|
|
|
var args = refinedSpec.method.toLowerCase() === 'get' ? [url, headers] : [url, encodedData, headers];
|
2016-04-11 08:21:33 +02:00
|
|
|
|
2016-04-12 04:31:14 +02:00
|
|
|
return _this[refinedSpec.method].apply(_this, args).then(parseResponse);
|
2016-04-11 08:21:33 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}, {
|
2016-04-07 13:43:43 +02:00
|
|
|
key: 'get',
|
|
|
|
value: function get(url) {
|
2016-04-12 01:21:49 +02:00
|
|
|
var headers = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
|
2016-04-12 00:25:02 +02:00
|
|
|
|
|
|
|
return this.fetch(url, {
|
|
|
|
method: 'get',
|
|
|
|
credentials: 'same-origin',
|
2016-04-12 01:21:49 +02:00
|
|
|
headers: headers
|
2016-04-12 00:25:02 +02:00
|
|
|
}).then(checkStatus);
|
2016-04-07 13:43:43 +02:00
|
|
|
}
|
|
|
|
}, {
|
|
|
|
key: 'post',
|
2016-04-12 00:25:02 +02:00
|
|
|
value: function post(url) {
|
|
|
|
var data = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
|
|
|
|
var headers = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2];
|
|
|
|
|
|
|
|
var defaultHeaders = { 'Content-Type': 'application/x-www-form-urlencoded' };
|
2016-04-03 11:04:59 +02:00
|
|
|
return this.fetch(url, {
|
|
|
|
method: 'post',
|
2016-04-12 00:25:02 +02:00
|
|
|
headers: Object.assign({}, defaultHeaders, headers),
|
2016-04-03 11:04:59 +02:00
|
|
|
credentials: 'same-origin',
|
|
|
|
body: data
|
|
|
|
}).then(checkStatus);
|
2016-04-07 13:43:43 +02:00
|
|
|
}
|
|
|
|
}, {
|
|
|
|
key: 'put',
|
2016-04-12 00:25:02 +02:00
|
|
|
value: function put(url) {
|
|
|
|
var data = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
|
|
|
|
var headers = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2];
|
|
|
|
|
|
|
|
return this.fetch(url, { method: 'put', credentials: 'same-origin', body: data, headers: headers }).then(checkStatus);
|
2016-04-07 13:43:43 +02:00
|
|
|
}
|
|
|
|
}, {
|
|
|
|
key: 'delete',
|
2016-04-12 00:25:02 +02:00
|
|
|
value: function _delete(url) {
|
|
|
|
var data = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
|
|
|
|
var headers = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2];
|
|
|
|
|
|
|
|
return this.fetch(url, { method: 'delete', credentials: 'same-origin', body: data, headers: headers }).then(checkStatus);
|
2016-04-07 13:43:43 +02:00
|
|
|
}
|
|
|
|
}]);
|
|
|
|
|
|
|
|
return SilverStripeBackend;
|
|
|
|
}();
|
|
|
|
|
|
|
|
var backend = new SilverStripeBackend();
|
|
|
|
|
|
|
|
exports.default = backend;
|
|
|
|
});
|