Skip to content

Commit dfdf90d

Browse files
committed
fix(models): model no longer stripped from the name of file if explicitly named that way
fixes #49 Signed-off-by: Vojtech Masek <[email protected]>
1 parent f650d7c commit dfdf90d

File tree

11 files changed

+334
-1
lines changed

11 files changed

+334
-1
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
"build": "rimraf dist && tsc",
4949
"lint": "tslint -p tsconfig.json -c tslint.json 'src/**/*.ts' 'src/tests.ts'",
5050
"gen": "ts-node ./src/main.ts",
51+
"gen-custom": "rimraf ./tests/custom/api && ts-node ./src/main.ts -s ./tests/custom/swagger.yaml -o ./tests/custom/api",
5152
"gen-sports": "rimraf ./tests/sports/api && ts-node ./src/main.ts -s ./tests/sports/swagger.yaml -o ./tests/sports/api",
5253
"gen-esquare": "rimraf ./tests/esquare/api && ts-node ./src/main.ts -s ./tests/esquare/swagger.yaml -o ./tests/esquare/api",
5354
"gen-gcloud-firestore": "rimraf ./tests/gcloud-firestore/api && ts-node ./src/main.ts -s ./tests/gcloud-firestore/swagger.yaml -o ./tests/gcloud-firestore/api",

src/helper.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ export function typeName(name: string = 'any', isArray: boolean = false): string
7878
}
7979

8080
export function fileName(name: string = '', type: 'model' | 'enum' = 'model'): string {
81-
return `${dashCase(name.replace(/model|enum/i, ''))}.${type}`;
81+
return `${dashCase(name)}.${type}`;
8282
}
8383

8484
export function prefixImportedModels(type: string = ''): string {
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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+
}

tests/custom/api/index.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/* tslint:disable */
2+
3+
import { NgModule, ModuleWithProviders } from '@angular/core';
4+
import { HttpHeaders, HttpParams } from '@angular/common/http';
5+
import { APIClient, USE_DOMAIN, USE_HTTP_OPTIONS } from './api-client.service';
6+
7+
export * from './models';
8+
9+
export { APIClient } from './api-client.service';
10+
11+
/**
12+
* provided options, headers and params will be used as default for each request
13+
*/
14+
export interface DefaultHttpOptions {
15+
headers?: {[key: string]: string};
16+
params?: {[key: string]: string};
17+
reportProgress?: boolean;
18+
withCredentials?: boolean;
19+
}
20+
21+
export interface HttpOptions {
22+
headers?: HttpHeaders;
23+
params?: HttpParams;
24+
reportProgress?: boolean;
25+
withCredentials?: boolean;
26+
}
27+
28+
export interface APIClientModuleConfig {
29+
domain?: string;
30+
httpOptions?: DefaultHttpOptions;
31+
}
32+
33+
@NgModule({})
34+
export class APIClientModule {
35+
/**
36+
* Use this method in your root module to provide the APIClientModule
37+
*
38+
* If you are not providing
39+
* @param { APIClientModuleConfig } config
40+
* @returns { ModuleWithProviders }
41+
*/
42+
static forRoot(config: APIClientModuleConfig = {}): ModuleWithProviders {
43+
return {
44+
ngModule: APIClientModule,
45+
providers: [
46+
...(config.domain ? [{provide: USE_DOMAIN, useValue: config.domain}] : []),
47+
...(config.httpOptions ? [{provide: USE_HTTP_OPTIONS, useValue: config.httpOptions}] : []),
48+
APIClient
49+
]
50+
};
51+
}
52+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/* tslint:disable */
2+
3+
export interface DataModel {
4+
entities: number[];
5+
id: number;
6+
imageData: string;
7+
imageUrl: string;
8+
roleId: number;
9+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/* tslint:disable */
2+
3+
export interface Data {
4+
email: string;
5+
firstName: string;
6+
id: number;
7+
lastName: string;
8+
phone: string;
9+
title: string;
10+
}

tests/custom/api/models/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/* tslint:disable */
2+
3+
export { ItemList } from './item-list.model';
4+
export { ItemModelList } from './item-model-list.model';
5+
export { Data } from './data.model';
6+
export { DataModel } from './data-model.model';
7+
export { TestModel } from './test-model.model';
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/* tslint:disable */
2+
import {
3+
Data,
4+
} from './..';
5+
6+
export interface ItemList {
7+
data: Data[];
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/* tslint:disable */
2+
import {
3+
DataModel,
4+
} from './..';
5+
6+
export interface ItemModelList {
7+
data: DataModel[];
8+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/* tslint:disable */
2+
3+
export interface TestModel {
4+
count: number;
5+
}

0 commit comments

Comments
 (0)