Skip to content

Commit 7eaf673

Browse files
committed
feat(module): API client exported as module
closes #23 BREAKING CHANGES: - renamed to APIClient - domain and configuration are now provided using `.forRoot` method ``` APIClientModule.forRoot({ domain: 'https://api.url', }), ``` - cli command renamed to `api-client-generator`
1 parent d385f3b commit 7eaf673

File tree

4 files changed

+49
-15
lines changed

4 files changed

+49
-15
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,9 @@ output
6262
│ ├─ some.enum.ts
6363
│ ├─ some.model.ts
6464
│ │ ...
65-
│ └─ another.model.ts
66-
├─ api-client-service.ts
65+
│ ├─ another.model.ts
66+
│ └─ index.ts
67+
├─ api-client.service.ts
6768
└─ index.ts
6869
```
6970

src/generator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ export class Generator {
309309
const clientTemplate = (await promisify(fs.readFile)(__dirname + '/../templates/ngx-service.mustache')).toString();
310310

311311
const result = Mustache.render(clientTemplate, viewContext);
312-
const outfile = join(this.outputPath, 'api-client-service.ts');
312+
const outfile = join(this.outputPath, 'api-client.service.ts');
313313

314314
await promisify(fs.writeFile)(outfile, result, 'utf-8');
315315
}
Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,41 @@
11
/* tslint:disable */
22

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+
37
export * from './models';
4-
export { ApiClientService } from './api-client-service';
8+
export { APIClient } from './models';
9+
10+
export interface HttpOptions {
11+
headers?: HttpHeaders, // provided headers will be used as default for each request
12+
params?: HttpParams, // provided params will be used as default for each request
13+
reportProgress?: boolean,
14+
withCredentials?: boolean,
15+
}
16+
17+
export interface APIClientModuleConfig {
18+
domain?: string;
19+
httpOptions?: HttpOptions;
20+
}
21+
22+
@NgModule({})
23+
export class APIClientModule {
24+
/**
25+
* Use this method in your root module to provide the APIClientModule
26+
*
27+
* If you are not providing
28+
* @param { APIClientModuleConfig } config
29+
* @returns { ModuleWithProviders }
30+
*/
31+
static forRoot(config: APIClientModuleConfig = {}): ModuleWithProviders {
32+
return {
33+
ngModule: APIClientModule,
34+
providers: [
35+
...(config.domain ? [{provide: USE_DOMAIN, useValue: config.domain}] : []),
36+
...(config.httpOptions ? [{provide: USE_HTTP_OPTIONS, useValue: config.httpOptions}] : []),
37+
APIClient
38+
]
39+
};
40+
}
41+
}

templates/ngx-service.mustache

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,26 @@
11
/* tslint:disable */
22

33
import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
4-
import { Inject, Injectable, Optional } from '@angular/core';
4+
import { Inject, Injectable, InjectionToken, Optional } from '@angular/core';
55
import { Observable } from 'rxjs/Observable';
6-
6+
import { HttpOptions } from './';
77
import * as models from './models';
88

9-
interface HttpOptions {
10-
headers?: HttpHeaders,
11-
params?: HttpParams,
12-
reportProgress?: boolean,
13-
withCredentials?: boolean,
14-
}
9+
export const USE_DOMAIN = new InjectionToken<string>('USE_DOMAIN');
10+
export const USE_HTTP_OPTIONS = new InjectionToken<HttpOptions>('USE_HTTP_OPTIONS');
1511

1612
/**
1713
* Created with https://github.com/flowup/api-client-generator
1814
*/
1915
@Injectable()
20-
export class ApiClientService {
16+
export class APIClient {
2117
2218
readonly options: HttpOptions;
2319
private domain: string = `{{&domain}}`;
2420

2521
constructor(private http: HttpClient,
26-
@Optional() @Inject('domain') domain: string,
27-
@Optional() @Inject('httpOptions') options: HttpOptions) {
22+
@Optional() @Inject(USE_DOMAIN) domain: string,
23+
@Optional() @Inject(USE_HTTP_OPTIONS) options: HttpOptions) {
2824
2925
if (domain) {
3026
this.domain = domain;

0 commit comments

Comments
 (0)