Skip to content

Commit 614a24e

Browse files
Initial commit
0 parents  commit 614a24e

40 files changed

+7407
-0
lines changed

.editorconfig

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Editor configuration, see https://editorconfig.org
2+
root = true
3+
4+
[*]
5+
indent_style = space
6+
indent_size = 2
7+
charset = utf-8
8+
trim_trailing_whitespace = true
9+
insert_final_newline = true
10+
11+
[*.md]
12+
max_line_length = off
13+
trim_trailing_whitespace = false

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.DS_Store
2+
.history
3+
node_modules
4+
dist
5+
build
6+
output

.npmignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.DS_Store
2+
.history
3+
node_modules
4+
dist
5+
output

.prettierignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Ignore everything:
2+
/*
3+
# Except web-app:
4+
!/**/src
5+
6+
/src/templates

.prettierrc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"tabWidth": 2,
3+
"useTabs": false,
4+
"bracketSpacing": true,
5+
"semi": true,
6+
"singleQuote": true,
7+
"backtick": true,
8+
"jsxSingleQuote": true,
9+
"arrowParens": "always",
10+
"trailingComma": "all",
11+
"printWidth": 80,
12+
"avoidEscape": true,
13+
"endOfLine": "auto"
14+
}

ApiBase/Readme.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# API Base - TypeScript OpenApi/Swagger Generator
2+
3+
This folder contains the main dependences required by the generated folders.
4+
5+
You can adapt this code to your project.

ApiBase/api-base.native.service.ts

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import { Injectable } from '@angular/core';
2+
import { HttpClient, HttpErrorResponse } from '@angular/common/http';
3+
import { Observable } from 'rxjs/Observable';
4+
import { catchError, map } from 'rxjs/operators';
5+
6+
import { Store } from '@ngrx/store';
7+
import { AppState } from '@app/store/app.reducers';
8+
import { HTTP } from '@ionic-native/http/ngx';
9+
import { ApiBaseService } from './api-base.service';
10+
import { from } from 'rxjs/internal/observable/from';
11+
12+
export interface APIError {
13+
error: any;
14+
errorText: string;
15+
}
16+
17+
/**
18+
* Implementation of angular-http
19+
* https://angular.io/guide/http
20+
*
21+
* TODO: Improve retry with: https://www.learnrxjs.io/operators/error_handling/retrywhen.html
22+
*/
23+
@Injectable()
24+
export class ApiBaseNativeService extends ApiBaseService {
25+
constructor(
26+
protected http: HttpClient,
27+
protected store$: Store<AppState>,
28+
protected httpNative: HTTP
29+
) {
30+
super(http, store$);
31+
}
32+
33+
public doGet(url, params = null, data = null): Observable<any> {
34+
// const options = this.getOptions(url, params, data);
35+
url = this.parseURLParams(url, params);
36+
return from(
37+
this.httpNative.get(this.serverURL + url, {}, this.httpHeadersPlain)
38+
).pipe(
39+
map((response) => JSON.parse(response.data)),
40+
catchError((error) => this.handleError(error))
41+
);
42+
}
43+
44+
public doPost(url, params, data): Observable<any> {
45+
// const options = this.getOptions(url, params, data);
46+
this.setHeaders();
47+
url = this.parseURLParams(url, params);
48+
return from(this.httpNative.post(this.serverURL + url, data, {})).pipe(
49+
catchError((error) => this.handleError(error))
50+
);
51+
}
52+
53+
public doPut(url, params, data): Observable<any> {
54+
// const options = this.getOptions(url, params, data);
55+
url = this.parseURLParams(url, params);
56+
return from(
57+
this.httpNative.put(this.serverURL + url, data, this.httpHeadersPlain)
58+
).pipe(catchError((error) => this.handleError(error)));
59+
}
60+
61+
public doDel(url, params, data = null): Observable<any> {
62+
// const options = this.getOptions(url, params, data);
63+
url = this.parseURLParams(url, params);
64+
return from(
65+
this.httpNative.delete(this.serverURL + url, {}, this.httpHeadersPlain)
66+
).pipe(catchError((error) => this.handleError(error)));
67+
}
68+
69+
public generateFormData(values: any): FormData {
70+
const form = new FormData();
71+
for (const key in values) {
72+
if (values[key]) {
73+
form.append(key, values[key]);
74+
}
75+
}
76+
return form;
77+
}
78+
79+
/**
80+
* Generate the httpHeaders property
81+
*/
82+
protected setHeaders() {
83+
for (const key in this.httpHeadersPlain) {
84+
if (!this.httpHeadersPlain[key]) {
85+
continue;
86+
}
87+
this.httpNative.setHeader(
88+
'*',
89+
String(key),
90+
String(this.httpHeadersPlain[key])
91+
);
92+
}
93+
}
94+
95+
protected handleError(error: HttpErrorResponse): Observable<APIError> {
96+
console.error('ERROR CATCHED', error);
97+
return super.handleError(error);
98+
}
99+
100+
protected getOptions(url, params = null, data = null) {
101+
return {
102+
headers: this.httpHeaders,
103+
// params: this.parseURLParams(url, params),
104+
};
105+
}
106+
}

ApiBase/api-base.service.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { TestBed } from '@angular/core/testing';
2+
3+
import { ApiBaseService } from './api-base.service';
4+
5+
describe('ApiBaseService', () => {
6+
beforeEach(() => TestBed.configureTestingModule({}));
7+
8+
it('should be created', () => {
9+
const service: ApiBaseService = TestBed.inject(ApiBaseService);
10+
expect(service).toBeTruthy();
11+
});
12+
});

0 commit comments

Comments
 (0)