mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-21 00:52:55 +00:00
feat: add the app
This commit is contained in:
81
src/app/modules/data/data.provider.ts
Normal file
81
src/app/modules/data/data.provider.ts
Normal file
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
* Copyright (C) 2018 StApps
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the Free
|
||||
* Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with
|
||||
* this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
import {HttpClient, HttpResponse} from '@angular/common/http';
|
||||
import {Injectable} from '@angular/core';
|
||||
import {HttpClientInterface, HttpClientRequest} from '@openstapps/api/lib/httpClientInterface';
|
||||
|
||||
/**
|
||||
* Response with generic for the type of body that is returned from the request
|
||||
*/
|
||||
export interface Response<TYPE_OF_BODY> extends HttpResponse<TYPE_OF_BODY> {
|
||||
body: TYPE_OF_BODY;
|
||||
statusCode: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* HttpClient that is based on angular's HttpClient (@TODO: move it to provider or independent package)
|
||||
*/
|
||||
@Injectable()
|
||||
export class StAppsWebHttpClient implements HttpClientInterface {
|
||||
constructor(private http: HttpClient) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Make a request
|
||||
* @param requestConfig Configuration of the request
|
||||
*/
|
||||
async request<TYPE_OF_BODY>(
|
||||
requestConfig: HttpClientRequest,
|
||||
): Promise<Response<TYPE_OF_BODY>> {
|
||||
const options: {
|
||||
[key: string]: any;
|
||||
observe: 'response';
|
||||
} = {
|
||||
body: {},
|
||||
observe: 'response',
|
||||
responseType: 'json',
|
||||
};
|
||||
|
||||
if (typeof requestConfig.body !== 'undefined') {
|
||||
options.body = requestConfig.body;
|
||||
}
|
||||
|
||||
if (typeof requestConfig.headers !== 'undefined') {
|
||||
options.headers = requestConfig.headers;
|
||||
}
|
||||
|
||||
try {
|
||||
const response: HttpResponse<TYPE_OF_BODY> = await this.http.request<TYPE_OF_BODY>(
|
||||
requestConfig.method || 'GET', requestConfig.url.toString(), options)
|
||||
.toPromise();
|
||||
return Object.assign(response, {statusCode: response.status, body: response.body || {}});
|
||||
} catch (err) {
|
||||
throw Error(err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Generated class for the DataProvider provider.
|
||||
|
||||
See https://angular.io/guide/dependency-injection for more info on providers
|
||||
and Angular DI.
|
||||
*/
|
||||
@Injectable()
|
||||
export class DataProvider {
|
||||
constructor() {
|
||||
// @TODO
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user