Skip to content

Commit

Permalink
Replace ApinaModule with provideApina
Browse files Browse the repository at this point in the history
  • Loading branch information
komu committed Nov 9, 2023
1 parent 5008db8 commit a2ab2f0
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 40 deletions.
16 changes: 15 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
## Unreleased
## 0.22.0 (2023-11-09)

### Breaking changes

- Use `{providedIn: 'root'}` for Angular endpoints. This facilitates tree-shaking of unused code.
- Don't create `ApinaModule` for Angular, but a `provideApina`-function instead that you can use to create.
a provider. Also, `HttpClient` is not automatically imported anymore, you should use Angular's `provideHttpClient`
to register a client. To migrate, remove `ApinaModule from imports` and use this instead:
```typescript
{
providers: [
provideHttpClient(),
provideApina()
]
}
```
To customize Apina configuration, don't add additional providers manually, but pass parameters to `provideApina`.

## 0.21.0 (2023-08-02)

Expand Down
24 changes: 14 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,30 +58,34 @@ tasks.findByPath(":frontend:setup").dependsOn(tasks.apina)

## Using generated code

First make your own module dependent on Apina:
Make your application dependent on Apina and HttpClient:

```typescript
import { ApinaModule } from 'apina';

@NgModule({
imports: [ApinaModule],
providers: [MyService]
})
class MyModule { }
import { provideHttpClient } from "@angular/common/http";
import { provideApina } from 'apina';

bootstrapApplication(AppComponent, {
providers: [
provideHttpClient(),
provideApina()
]
});
```

Then just inject the generated endpoint and use it:

```typescript
import { DocumentsEndpoint } from 'apina';

@Injectable()
@Injectable({providedIn: 'root'})
class MyService {

constructor(private readonly documentsEndpoint: DocumentsEndpoint) { }

load() {
this.documentsEndpoint.findDocument(42).forEach(doc => this.document = doc);
this.documentsEndpoint.findDocument(42).subscribe(doc =>
console.log("loaded", doc);
);
}
}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ class TypeScriptAngularGenerator(api: ApiDefinition, settings: TranslationSettin
classDecorator = "@Injectable({providedIn: 'root'})",
platformRuntimeCodePath = "typescript/runtime-angular.ts",
platformSpecificImports = mapOf(
"@angular/core" to listOf("Injectable", "NgModule"),
"@angular/common/http" to listOf("HttpClient", "HttpClientModule", "HttpParams"),
"@angular/core" to listOf("Injectable", "Provider", "Type"),
"@angular/common/http" to listOf("HttpClient", "HttpParams"),
"rxjs" to listOf("Observable"),
"rxjs/operators" to listOf("map"),
)
Expand Down
26 changes: 12 additions & 14 deletions apina-core/src/main/resources/typescript/runtime-angular.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export abstract class ApinaEndpointContext {

constructor(protected config: ApinaConfig) {
constructor(protected readonly config: ApinaConfig) {
}

abstract request(data: RequestData): Observable<any>
Expand All @@ -19,14 +19,14 @@ export abstract class ApinaEndpointContext {
}

protected buildUrl(uriTemplate: String, pathVariables: any): string {
return this.config.baseUrl + uriTemplate.replace(/{([^}]+)}/g, (match, name) => pathVariables[name]);
return this.config.baseUrl + uriTemplate.replace(/{([^}]+)}/g, (_match, name) => pathVariables[name]);
}
}

@Injectable()
export class DefaultApinaEndpointContext extends ApinaEndpointContext {

constructor(private httpClient: HttpClient, config: ApinaConfig) {
constructor(private readonly httpClient: HttpClient, config: ApinaConfig) {
super(config);
}

Expand All @@ -46,21 +46,19 @@ export class DefaultApinaEndpointContext extends ApinaEndpointContext {
params = new HttpParams({fromObject: filteredParams});
}


return this.httpClient.request(data.method, url, { params: params, body: data.requestBody })
.pipe(map(r => data.responseType ? this.config.deserialize(r, data.responseType) : r));
}
}

function apinaConfigFactory() {
return new ApinaConfig();
interface ProvideParams {
config?: ApinaConfig;
endpointContextClass?: Type<ApinaEndpointContext>;
}

@NgModule({
imports: [HttpClientModule],
providers: [
{ provide: ApinaEndpointContext, useClass: DefaultApinaEndpointContext },
{ provide: ApinaConfig, useFactory: apinaConfigFactory }
]
})
export class ApinaModule {}
export function provideApina(params: ProvideParams = {}): Provider[] {
return [
{ provide: ApinaConfig, useValue: params.config ?? new ApinaConfig() },
{ provide: ApinaEndpointContext, useClass: params.endpointContextClass ?? DefaultApinaEndpointContext },
];
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ class TypeScriptWriterTest {

@Test
fun imports() {
writer.writeImport("@angular/common/http", listOf("HttpClient", "HttpClientModule", "HttpParams"))
writer.writeImport("@angular/common/http", listOf("HttpClient", "HttpParams"))

assertEquals("import { HttpClient, HttpClientModule, HttpParams } from '@angular/common/http';\n", writer.output)
assertEquals("import { HttpClient, HttpParams } from '@angular/common/http';\n", writer.output)
}
}
21 changes: 10 additions & 11 deletions manual/src/asciidoc/_01-introduction.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,19 @@ tasks.apina {
}
----

Next, import generated `ApinaModule` from your Angular module:
Next, use `provideApina` and `provideHttpClient` in your Angular setup:

[source,typescript]
----
import { ApinaModule } from '../gen/apina-api';
@NgModule({
imports: [
...
ApinaModule
],
...
})
class MyModule { }
import { provideHttpClient } from "@angular/common/http";
import { provideApina } from '../gen/apina-api';
bootstrapApplication(AppComponent, {
providers: [
provideHttpClient(),
provideApina()
]
});
----

Finally, inject the service generated by Apina and use it your own code:
Expand Down

0 comments on commit a2ab2f0

Please sign in to comment.