|
| 1 | +/* tslint:disable */ |
| 2 | + |
| 3 | +import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http'; |
| 4 | +import { Inject, Injectable, InjectionToken, Optional } from '@angular/core'; |
| 5 | +import { Observable } from 'rxjs'; |
| 6 | +import { DefaultHttpOptions, HttpOptions } from './'; |
| 7 | + |
| 8 | +import * as models from './models'; |
| 9 | + |
| 10 | +export const USE_DOMAIN = new InjectionToken<string>('USE_DOMAIN'); |
| 11 | +export const USE_HTTP_OPTIONS = new InjectionToken<HttpOptions>('USE_HTTP_OPTIONS'); |
| 12 | + |
| 13 | +type APIHttpOptions = HttpOptions & { |
| 14 | + headers: HttpHeaders; |
| 15 | + params: HttpParams; |
| 16 | +}; |
| 17 | + |
| 18 | +/** |
| 19 | + * Created with https://github.com/flowup/api-client-generator |
| 20 | + */ |
| 21 | +@Injectable() |
| 22 | +export class APIClient { |
| 23 | + |
| 24 | + readonly options: APIHttpOptions; |
| 25 | + |
| 26 | + private readonly domain: string = `//${window.location.hostname}${window.location.port ? ':'+window.location.port : ''}`; |
| 27 | + |
| 28 | + constructor(private http: HttpClient, |
| 29 | + @Optional() @Inject(USE_DOMAIN) domain: string, |
| 30 | + @Optional() @Inject(USE_HTTP_OPTIONS) options: DefaultHttpOptions) { |
| 31 | + |
| 32 | + if (domain) { |
| 33 | + this.domain = domain; |
| 34 | + } |
| 35 | + |
| 36 | + this.options = { |
| 37 | + headers: new HttpHeaders(options && options.headers ? options.headers : {}), |
| 38 | + params: new HttpParams(options && options.params ? options.params : {}), |
| 39 | + ...(options && options.reportProgress ? { reportProgress: options.reportProgress } : {}), |
| 40 | + ...(options && options.withCredentials ? { withCredentials: options.withCredentials } : {}) |
| 41 | + }; |
| 42 | + } |
| 43 | + |
| 44 | + getItems( |
| 45 | + args: { |
| 46 | + pageSize: number, |
| 47 | + page: number, |
| 48 | + }, |
| 49 | + requestHttpOptions?: HttpOptions |
| 50 | + ): Observable<models.ItemList> { |
| 51 | + const path = `/items`; |
| 52 | + const options: APIHttpOptions = {...this.options, ...requestHttpOptions}; |
| 53 | + |
| 54 | + if ('pageSize' in args) { |
| 55 | + options.params = options.params.set('pageSize', String(args.pageSize)); |
| 56 | + } |
| 57 | + if ('page' in args) { |
| 58 | + options.params = options.params.set('page', String(args.page)); |
| 59 | + } |
| 60 | + return this.sendRequest<models.ItemList>('GET', path, options); |
| 61 | + } |
| 62 | + |
| 63 | + getItemModels( |
| 64 | + args: { |
| 65 | + pageSize: number, |
| 66 | + page: number, |
| 67 | + }, |
| 68 | + requestHttpOptions?: HttpOptions |
| 69 | + ): Observable<any> { |
| 70 | + const path = `/itemModels`; |
| 71 | + const options: APIHttpOptions = {...this.options, ...requestHttpOptions}; |
| 72 | + |
| 73 | + if ('pageSize' in args) { |
| 74 | + options.params = options.params.set('pageSize', String(args.pageSize)); |
| 75 | + } |
| 76 | + if ('page' in args) { |
| 77 | + options.params = options.params.set('page', String(args.page)); |
| 78 | + } |
| 79 | + return this.sendRequest<any>('GET', path, options); |
| 80 | + } |
| 81 | + |
| 82 | + private sendRequest<T>(method: string, path: string, options: HttpOptions, body?: any): Observable<T> { |
| 83 | + switch (method) { |
| 84 | + case 'DELETE': |
| 85 | + return this.http.delete<T>(`${this.domain}${path}`, options); |
| 86 | + case 'GET': |
| 87 | + return this.http.get<T>(`${this.domain}${path}`, options); |
| 88 | + case 'HEAD': |
| 89 | + return this.http.head<T>(`${this.domain}${path}`, options); |
| 90 | + case 'OPTIONS': |
| 91 | + return this.http.options<T>(`${this.domain}${path}`, options); |
| 92 | + case 'PATCH': |
| 93 | + return this.http.patch<T>(`${this.domain}${path}`, body, options); |
| 94 | + case 'POST': |
| 95 | + return this.http.post<T>(`${this.domain}${path}`, body, options); |
| 96 | + case 'PUT': |
| 97 | + return this.http.put<T>(`${this.domain}${path}`, body, options); |
| 98 | + default: |
| 99 | + console.error(`Unsupported request: ${method}`); |
| 100 | + return Observable.throw(`Unsupported request: ${method}`); |
| 101 | + } |
| 102 | + } |
| 103 | +} |
0 commit comments