ngx-core-business
Version:
A comprehensive solution designed to streamline the development of enterprise-level Angular applications.
193 lines (187 loc) • 6.29 kB
JavaScript
import * as i0 from '@angular/core';
import { Injectable } from '@angular/core';
import * as i1 from '@angular/common/http';
import { HttpClient } from '@angular/common/http';
import { Router } from '@angular/router';
import { map } from 'rxjs/operators';
class HttpCollectionService {
constructor(http) {
this.http = http;
this.pageParam = 'page';
this.limitParam = 'limit';
this.queryParam = 'query';
this.sortParam = 'sort';
this.filterParam = 'filter';
this.fieldsParam = '$fields';
}
/**
* Realiza una petición GET
*/
get(url, options = {
page: 1,
limit: 25,
}) {
const me = this;
const queryParams = [];
if (options.page) {
queryParams.push(`${me.pageParam}=${options.page}`);
}
if (options.limit) {
queryParams.push(`${me.limitParam}=${options.limit}`);
}
if (options.query) {
queryParams.push(`${me.queryParam}=${options.query}`);
}
if (options.sorters) {
queryParams.push(`${me.sortParam}=${me.encodeSorters(options.sorters)}`);
}
if (options.filters) {
queryParams.push(`${me.filterParam}=${me.encodeFilters(options.filters)}`);
}
if (options.fields) {
queryParams.push(`${me.fieldsParam}=${options.fields}`);
}
url = me.urlAppend(url, queryParams.join('&'));
return me.http.get(url);
}
urlAppend(url, str) {
if (str) {
return url + (url.indexOf('?') == -1 ? '?' : '&') + str;
}
return url;
}
encodeSorters(sorters) {
return encodeURI(JSON.stringify(sorters));
}
encodeFilters(filters) {
return encodeURI(JSON.stringify(filters.map((filter) => {
return {
type: filter.type,
field: filter.field,
operator: filter.operator,
value: filter.value,
};
})));
}
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.0.6", ngImport: i0, type: HttpCollectionService, deps: [{ token: i1.HttpClient }], target: i0.ɵɵFactoryTarget.Injectable }); }
static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.0.6", ngImport: i0, type: HttpCollectionService, providedIn: 'root' }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.0.6", ngImport: i0, type: HttpCollectionService, decorators: [{
type: Injectable,
args: [{
providedIn: 'root',
}]
}], ctorParameters: () => [{ type: i1.HttpClient }] });
class BaseRestService {
constructor(injector) {
this.collectionUrl = '';
/**
* Usa estrictamene la url señalada.
*/
this.strictApiUrl = '';
this.http = injector.get(HttpClient);
this.router = injector.get(Router);
this.httpList = injector.get(HttpCollectionService);
}
getCollectionUrl() {
const me = this;
return (me.collectionUrl ||
(me.collectionUrl = `${me.getApiUrl()}/${me.collectionPath}`));
}
/**
* GET List
*/
list(options) {
return this.httpList.get(this.getCollectionUrl(), options);
}
/**
* GET
* @param id The resource ID
*/
get(id) {
const me = this;
return me.http
.get(`${me.getCollectionUrl()}/${id}`)
.pipe(map((res) => res.data));
}
/**
* POST
*/
create(model) {
const me = this;
return me.http
.post(`${me.getCollectionUrl()}`, model)
.pipe(map((res) => res.data));
}
/**
* PUT
*/
update(model, id) {
const me = this;
return me.http
.put(`${me.getCollectionUrl()}/${id || model.id}`, model)
.pipe(map((res) => res.data));
}
save(model, id) {
return id || model.id ? this.update(model, id) : this.create(model);
}
/**
* DELETE
*/
delete(id) {
const me = this;
return me.http.delete(`${me.getCollectionUrl()}/${id}`);
}
buildUrl(segments, baseUrl = this.getCollectionUrl()) {
return baseUrl + this.router.createUrlTree(segments).toString();
}
getBlob(url) {
const me = this;
return me.http
.get(url, {
observe: 'response',
responseType: 'blob',
})
.pipe(map((res) => res.body));
}
getFile(url) {
const me = this;
return me.http
.get(url, {
observe: 'response',
responseType: 'blob',
})
.pipe(map((res) => {
res.body;
if (res.body) {
// Get the file name from the response.
const fileName = res.headers.get('Content-Disposition')?.split('filename=')[1];
if (!fileName) {
console.error('El servidor no ha devuelto el nombre del archivo. en la cabecera Content-Disposition, Access-Control-Expose-Headers tambien debe ser añadido.');
return;
}
return {
file: res.body,
fileName,
};
}
return res.body;
}));
}
getCollectionPath() {
return this.collectionPath;
}
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.0.6", ngImport: i0, type: BaseRestService, deps: [{ token: i0.Injector }], target: i0.ɵɵFactoryTarget.Injectable }); }
static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.0.6", ngImport: i0, type: BaseRestService }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.0.6", ngImport: i0, type: BaseRestService, decorators: [{
type: Injectable
}], ctorParameters: () => [{ type: i0.Injector }] });
/*
* Public API Surface of ngx-core-business
*/
/**
* Generated bundle index. Do not edit.
*/
export { BaseRestService, HttpCollectionService };
//# sourceMappingURL=ngx-core-business-http.mjs.map