@akala/core
Version:
175 lines • 7.71 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
var FetchHttp_1;
Object.defineProperty(exports, "__esModule", { value: true });
const global_injector_1 = require("./global-injector");
const parser_1 = require("./parser");
const each_1 = require("./each");
const helpers_1 = require("./helpers");
const service_1 = require("./service");
const uri = __importStar(require("url"));
const qs = __importStar(require("querystring"));
require("isomorphic-fetch");
let FetchHttp = FetchHttp_1 = class FetchHttp {
constructor() {
}
get(url, params) {
return this.call({ url: url, method: 'GET', queryString: params });
}
post(url, body) {
return this.call({ method: 'POST', url: url, body: body }).then((r) => {
return r.formData();
});
}
postJSON(url, body) {
return this.call({ method: 'POST', url: url, body: body, contentType: 'json', type: 'json' }).then((r) => {
return r.json();
});
}
getJSON(url, params) {
return this.call({ method: 'GET', url: url, queryString: params, type: 'json' }).then((r) => {
return r.json();
});
}
invokeSOAP(namespace, action, url, params) {
var body = '<?xml version="1.0" encoding="utf-8"?><s:Envelope s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body>' +
'<u:' + action + ' xmlns:u="' + namespace + '">';
each_1.each(params, function (paramValue, paramName) {
body += '<' + paramName + '><![CDATA[' + paramValue + ']]></' + paramName + '>';
});
body += '</u:' + action + '></s:Body></s:Envelope>';
return this.call({ method: 'POST', url: url, type: 'xml', headers: { SOAPAction: namespace + '#' + action }, body: body });
}
call(options) {
var init = { method: options.method || 'GET', body: options.body };
if (typeof (options.url) == 'string')
options.url = uri.parse(options.url, true);
if (options.queryString) {
if (typeof (options.queryString) == 'string')
options.queryString = qs.parse(options.queryString);
options.url.query = helpers_1.extend(options.url.query, options.queryString);
}
if (options.headers) {
init.headers = {};
each_1.each(options.headers, function (value, key) {
if (value instanceof Date)
init.headers[key] = value.toJSON();
else
init.headers[key] = value && value.toString();
});
}
if (options.type) {
init.headers = init.headers || {};
switch (options.type) {
case 'json':
init.headers['Accept'] = 'application/json, text/json';
if (!options.contentType && typeof (init.body) !== 'string')
init.body = JSON.stringify(init.body);
break;
case 'xml':
init.headers['Accept'] = 'text/xml';
break;
case 'text':
init.headers['Accept'] = 'text/plain';
break;
}
}
if (options.contentType && options.body) {
init.headers = init.headers || {};
switch (options.contentType) {
case 'json':
init.headers['Content-Type'] = 'application/json; charset=UTF-8';
if (typeof (init.body) !== 'string')
init.body = JSON.stringify(init.body);
break;
case 'form':
init.headers['Content-Type'] = 'multipart/form-data';
if (!(init.body instanceof FormData) && typeof init.body == 'undefined')
init.body = FetchHttp_1.serialize(init.body);
break;
}
}
return fetch(uri.format(options.url), init);
}
static serialize(obj, prefix) {
return each_1.map(obj, function (value, key) {
if (typeof (value) == 'object') {
var keyPrefix = prefix;
if (prefix) {
if (typeof (key) == 'number')
keyPrefix = prefix.substring(0, prefix.length - 1) + '[' + key + '].';
else
keyPrefix = prefix + encodeURIComponent(key) + '.';
}
return FetchHttp_1.serialize(value, keyPrefix);
}
else {
return (prefix || '') + encodeURIComponent(key) + '=' + encodeURIComponent(value);
}
}, true);
}
};
FetchHttp = FetchHttp_1 = __decorate([
service_1.service('$http'),
__metadata("design:paramtypes", [])
], FetchHttp);
exports.FetchHttp = FetchHttp;
class HttpCallFormatterFactory {
constructor() { }
parse(expression) {
var method = /^ *(\w+)/.exec(expression);
if (method)
return { method: method[1], $$length: method[0].length };
return parser_1.Parser.parseAny(expression, false);
}
build(formatter, settings) {
if (!settings)
settings = { method: 'getJSON' };
return function (scope) {
var settingsValue = settings;
if (settings instanceof Function)
settingsValue = settings(scope);
return global_injector_1.injectWithName(['$http'], function (http) {
var formattedValue = formatter(scope);
if (typeof (formattedValue) == 'string')
return http[settingsValue.method || 'getJSON'](formattedValue, each_1.grep(settingsValue, function (value, key) {
return key != 'method';
}));
if (Array.isArray(formattedValue)) {
return http[settingsValue.method || 'getJSON'].apply(http, formattedValue);
}
return http[settingsValue.method || 'getJSON'](formattedValue);
});
};
}
}
exports.HttpCallFormatterFactory = HttpCallFormatterFactory;
class HttpFormatterFactory extends HttpCallFormatterFactory {
constructor() {
super();
}
build(formatter, settings) {
return (value) => {
return super.build(formatter, settings)(value)();
};
}
}
exports.HttpFormatterFactory = HttpFormatterFactory;
helpers_1.module('$formatters').register('#http', new HttpFormatterFactory());
helpers_1.module('$formatters').register('#httpCall', new HttpCallFormatterFactory());
//# sourceMappingURL=http.js.map