@lottojs/ciapa
Version:
Sto bibliotecheto fato in JavaScript, te permete de farghe le chiamate HTTP e ciapar tute le informazioni che te serve par el to progèto.
116 lines • 4.97 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Ciapa = void 0;
const url_1 = require("./url");
const headers_1 = require("./headers");
const is_json_1 = require("../utils/is-json");
class Ciapa {
constructor(config) {
this.defaults = config;
const httpMethods = [
'get',
'post',
'put',
'patch',
'delete',
'head',
'all',
];
const mixin = {};
httpMethods.forEach((method) => {
mixin[method] = (url, request) => {
return this.request(url, Object.assign(Object.assign({}, request), { method }));
};
});
Object.assign(this, mixin);
}
setRequestInterceptor(requestInterceptor) {
this.requestInterceptor = requestInterceptor;
}
setResponseInterceptor(responseInterceptor) {
this.responseInterceptor = responseInterceptor;
}
static create(config) {
return new this(config);
}
preRequest(url, request) {
const buildedHeaders = (0, headers_1.buildHeaders)([], Object.assign(Object.assign({}, this.defaults.headers), request.headers));
const constructedUrl = (0, url_1.constructUrl)(url, Object.assign(Object.assign({}, request.params), this.defaults.params), this.defaults.baseUrl);
return {
url: constructedUrl,
headers: buildedHeaders,
body: request.body,
};
}
request(url, request) {
var _a;
return __awaiter(this, void 0, void 0, function* () {
if (this.requestInterceptor) {
request = yield this.requestInterceptor(url, request);
}
const { body, headers, url: finalUrl } = this.preRequest(url, request);
const isJsonBody = (0, is_json_1.isJson)(body);
if (isJsonBody) {
headers['Content-Type'] = 'application/json';
}
const response = yield fetch(finalUrl, Object.assign(Object.assign(Object.assign({}, request), { headers,
body }), (this.defaults.withCredentials && { credentials: 'include' }))).then((response) => __awaiter(this, void 0, void 0, function* () {
if (this.responseInterceptor) {
response = yield this.responseInterceptor(response);
}
return response;
}));
const responseObject = {
text: response.text,
json: response.json,
arrayBuffer: response.arrayBuffer,
headers: response.headers,
blob: response.blob,
body: response.body,
bodyUsed: response.bodyUsed,
clone: response.clone,
formData: response.formData,
ok: response.ok,
redirected: response.redirected,
status: response.status,
statusText: response.statusText,
type: response.type,
url: response.url,
};
let responseData;
const contentType = response.headers.get('Content-Type');
if (contentType && contentType.includes('application/json')) {
responseData = yield response.json();
}
else {
const textResponse = yield response.text();
try {
responseData = JSON.parse(textResponse);
}
catch (_b) {
responseData = textResponse;
}
}
if (typeof responseData === 'string') {
return Object.assign(Object.assign({}, responseObject), { data: responseData });
}
if (!request.responseSchema) {
return Object.assign(Object.assign({}, responseObject), { data: responseData });
}
return Object.assign(Object.assign({}, responseObject), (((_a = request.method) === null || _a === void 0 ? void 0 : _a.toLowerCase()) !== 'delete' && {
data: request.responseSchema.parse(responseData),
}));
});
}
}
exports.Ciapa = Ciapa;
//# sourceMappingURL=ciapa.js.map