UNPKG

proactive-http-fetch

Version:

Proactive http fetch package

95 lines 3.94 kB
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); }; import { autoinject } from 'aurelia-framework'; import { EventAggregator } from 'aurelia-event-aggregator'; import { HttpClient } from 'aurelia-fetch-client'; import { AntiforgeryService } from './antiforgery-service'; import { BrowserLocation } from './browser-location'; let HttpFetch = class HttpFetch { constructor(httpClient, antiforgeryService, browserLocation, eventAggregator) { this.httpClient = httpClient; this.antiforgeryService = antiforgeryService; this.browserLocation = browserLocation; this.eventAggregator = eventAggregator; this.httpClient.configure(config => { config .rejectErrorResponses() .withDefaults({ credentials: 'same-origin', headers: { 'X-Requested-With': 'Fetch' } }) .withInterceptor({ request(request) { request.headers.append(antiforgeryService.headerName, antiforgeryService.headerValue); return request; } }) .withInterceptor({ response(response) { antiforgeryService.storeAntiforgery(response); return response; } }); }); } get(url, param) { if (typeof param === 'undefined') return this.send(`${url}`, 'GET'); if (typeof param === 'string') return this.send(`${url}?${param}`, 'GET'); if (typeof param === 'object') return this.send(`${url}?${encodeURI(JSON.stringify(param))}`, 'GET'); } post(url, param) { return this.send(url, 'POST', param); } put(url, param) { return this.send(url, 'PUT', param); } send(url, method, param) { return this.httpClient .fetch(url, this.createRequest(method, param)) .then(response => this.handleResponse(response)) .catch(error => this.handleError(error)); } createRequest(method, param) { if (typeof param === 'undefined') return { method }; if (typeof param === 'string') return { method, body: param }; if (typeof param === 'object') return { method, body: JSON.stringify(param) }; return { method, body: param }; } handleResponse(response) { if (!!response.body) return response.json(); return Promise.resolve(); } handleError(response) { this.browserLocation.redirect(response); let message = ''; if (response instanceof Response) message = response.statusText; else message = 'Network error.'; this.eventAggregator.publish('error', message); return Promise.reject(message); } }; HttpFetch = __decorate([ autoinject(), __metadata("design:paramtypes", [HttpClient, AntiforgeryService, BrowserLocation, EventAggregator]) ], HttpFetch); export { HttpFetch }; //# sourceMappingURL=http-fetch.js.map