Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 | 21x 21x 21x 21x 21x 1x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 16x 16x 14x 25x 25x 25x 25x 24x 24x 24x 16x 8x 24x 24x 1x 1x 1x 1x 25x 25x 1x 25x 25x 8x 8x 16x 16x 16x 16x 16x 16x 16x 1x 1x 3x 3x 2x 1x 1x 1x 1x 1x 1x 4x 4x 2x 6x 2x | import qs from 'qs';
import { Config } from '../config';
import { Http } from '../http';
import { IDictionary } from '../utils';
import { Datasource } from './datasource';
import { ArrayFormatType, IRestDatasource } from './interfaces';
/**
* Base class to perform CRUD methods using HTTP requests.
*/
export class RestDatasource extends Datasource implements IRestDatasource {
public getLoadedData(): IDictionary[] {
return this.data;
}
/**
* HTTP route used on CRUD requests
*/
public route = '';
/**
* Defines if should wait and not execute GET method when Datasource is created
*/
public lazyLoad = true;
/**
* Defines the format used for query arrays
*/
public arrayFormat: ArrayFormatType = Config.datasourceArrayFormat;
public find: IDictionary = {};
/**
* Defines if the search should be executed only inside the data in memory
*/
public localSearch = false;
private localData: IDictionary<any>[] = [];
/**
* Creates a new Rest Datasource
* @param datasource Datasource structure
*/
constructor(datasource: IRestDatasource) {
super(datasource);
this.route = this.getInitValue('route', datasource.route, this.route);
this.lazyLoad = this.getInitValue('lazyLoad', datasource.lazyLoad, this.lazyLoad);
this.arrayFormat = this.getInitValue('arrayFormat', datasource.arrayFormat, this.arrayFormat);
this.find = this.getInitValue('find', datasource.find, this.find);
this.localSearch = this.getInitValue('localSearch', datasource.localSearch, this.localSearch);
}
public async initialize() {
super.initialize();
if (!this.lazyLoad) {
await this.get();
}
}
/**
* Retrieves a promise with data value ordered by order value
* and filtered by search, filter, limit and page
* @return Promise with data value
* @throws Will throws if request fail
*/
public async get() {
try {
this.error = undefined;
this.loading = true;
const response = (await this.request(this.getRequestParams())).data;
this.loading = false;
this.currentRow = {};
let data: IDictionary | IDictionary[];
if (Array.isArray(response.data)) {
data = await this.updateAndRetrieveCollection(response);
} else {
data = this.updateAndRetrieveRow(response);
}
this.runGetCallbacks();
return data;
} catch (e) {
this.error = e;
this.loading = false;
this.currentRow = {};
throw e;
}
}
/**
* Calls http request and retrieves http response
* @param params Datasource params
*/
protected request(params?: IDictionary<string>) {
const { arrayFormat } = this;
return Http.get(this.route, {
params,
paramsSerializer: (parameters?: any) => qs.stringify(parameters, { arrayFormat }) as any,
});
}
/**
* Retrieves request params
*/
protected getRequestParams(): any {
this.updateInternalProperties();
return {
page: this.loadAll ? undefined : this.page,
limit: this.loadAll ? undefined : this.limit,
order: this.order,
search: this.search || undefined,
search_in: this.search ? this.searchIn : [],
find: Object.keys(this.find).length > 0 ? this.find : undefined,
in: Object.keys(this.filter),
...this.filter,
};
}
/**
* Updates and retrieves datasource current row
* @param response Http response
* @returns Row
*/
protected updateAndRetrieveRow(response: { data: IDictionary<any> }) {
this.currentRow = response.data;
return this.currentRow;
}
/**
* Updates and retrieves datasource data collection
* @param response Http response
* @returns Data collection
*/
protected async updateAndRetrieveCollection(response: any): Promise<any> {
const { pagination } = response;
this.limit = Number(pagination.limit);
this.total = Number(pagination.total);
this.data = response.data;
this.localData = this.data;
this.page = Number(pagination.page);
return this.updateSelectedPage();
}
/**
* Adds a new row to datasource based on route
* @param row Row to create
* @returns HTTP response
*/
public async post(row: IDictionary<any>): Promise<any> {
return Http.post(this.route, row);
}
/**
* Updates a datasource row based on route
* @param row Row to update
* @returns HTTP Response
*/
public async put(row: IDictionary<any>): Promise<any> {
return Http.put(row.uri, row);
}
/**
* Deletes a datasource row uri
* @param row Row to delete
* @returns HTTP Response
*/
public async delete(row: IDictionary<any>): Promise<any> {
return Http.delete(row.uri).then(() => {
if (this.currentRow[this.uniqueKey] === row[this.uniqueKey]) {
this.currentRow = {};
}
});
}
/**
* Update datasource data
*/
public async updateData(data: IDictionary<any>[]): Promise<any> {
this.data = data;
this.localData = this.data;
this.total = this.data.length;
this.page = 1;
return Promise.resolve(this.data);
}
public clone(): IRestDatasource {
return {
...super.clone(),
lazyLoad: this.lazyLoad,
arrayFormat: this.arrayFormat,
route: this.route,
type: 'rest',
find: this.find,
};
}
/**
* Updates search value
* @param search Search value
* @returns Promise with data collection
*/
public setSearch(search: string) {
this.search = search;
if (!this.localSearch) {
return super.setSearch(search);
}
this.data = this.search ? this.localData.filter((row) => this.getRowBySearch(row)) : this.localData;
return Promise.resolve(this.data);
}
}
|