Skip to content

Commit

Permalink
feat: add HttpContext support for Angular 12+ (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael M authored Oct 27, 2023
2 parents 16b2491 + 9a07fb2 commit 489ffae
Show file tree
Hide file tree
Showing 131 changed files with 290 additions and 6,097 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ An OpenAPI 3 code generator for Angular
## Overview

This package generates interfaces and services from an [OpenApi 3 specification](https://www.openapis.org/).
The generated artifacts follow the [Angular style guide](https://angular.io/guide/styleguide) and are compatible with Angular 7+.
The generated artifacts follow the [Angular style guide](https://angular.io/guide/styleguide) and are compatible with Angular 12+.

This project is built on top of [ng-openapi-gen](https://github.com/cyclosproject/ng-openapi-gen) and features a lot of improvements listed below.

Expand Down
5 changes: 4 additions & 1 deletion packages/ng-openapi-gen/src/lib/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ export class Generator {
protected outDir: string;
protected tempDir: string;

constructor(public openApi: OpenAPIObject, public options: Options) {
constructor(
public openApi: OpenAPIObject,
public options: Options,
) {
this.outDir = trimTrailingSlash(options.output);
this.tempDir = this.outDir + '$';
}
Expand Down
6 changes: 5 additions & 1 deletion packages/ng-openapi-gen/src/lib/oa-request-body.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ export class OaRequestBody {
public required: boolean;
public tsComments: string;

constructor(public spec: RequestBodyObject, public content: OaContent[], public options: Options) {
constructor(
public spec: RequestBodyObject,
public content: OaContent[],
public options: Options,
) {
this.required = spec.required === true;
this.tsComments = tsComments(spec.description, 2);
}
Expand Down
6 changes: 5 additions & 1 deletion packages/ng-openapi-gen/src/lib/oa-security.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ export class OaSecurity {
public in: string;
public type: string;

constructor(key: string, public spec: SecuritySchemeObject, public scope: string[] = []) {
constructor(
key: string,
public spec: SecuritySchemeObject,
public scope: string[] = [],
) {
this.name = spec.name || '';
this.var = methodName(key);
this.tsComments = tsComments(spec.description || '', 2);
Expand Down
6 changes: 5 additions & 1 deletion packages/ng-openapi-gen/src/lib/oa-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ import { serviceClass } from './utils/open-api.js';
import { tsComments } from './utils/string.js';

export class OaService extends OaBase {
constructor(tag: TagObject, public readonly operations: OaOperation[], options: Options) {
constructor(
tag: TagObject,
public readonly operations: OaOperation[],
options: Options,
) {
super(tag.name, options, serviceClass);

this.fileName = this.fileName.replace(/-service$/, '.service');
Expand Down
7 changes: 6 additions & 1 deletion packages/ng-openapi-gen/src/lib/templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ export class Templates {
protected templates: Record<string, Handlebars.TemplateDelegate> = {};
protected globals: Record<string, unknown> = {};

constructor(builtInDir: string, customDir: string, globals: Globals, protected handlebars: typeof Handlebars) {
constructor(
builtInDir: string,
customDir: string,
globals: Globals,
protected handlebars: typeof Handlebars,
) {
const customTemplates = customDir ? readdirSync(customDir) : [];
for (const file of customTemplates) {
this.loadTemplate(customDir, file);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{{{bodyMethodTsComments}}}public {{methodName}}({{>operationParameters}}): Observable<{{{resultType}}}> {
return this.{{responseMethodName}}(params).pipe(
return this.{{responseMethodName}}(params, context).pipe(
map((r: {{@root.responseClass}}<{{{resultType}}}>) => r.body as {{{resultType}}}),
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ params{{^operation.parametersRequired}}?{{/operation.parametersRequired}}: {
{{{../operation.requestBody.tsComments}}}body{{^../operation.requestBody.required}}?{{/../operation.requestBody.required
}}: {{{reIndent type 2}}};
{{/requestBody}}
}
},
context?: HttpContext
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
return this.http.request(rb.build({
responseType: '{{responseType}}',
accept: '{{accept}}',
context: context,
})).pipe(
filter((r: any) => r instanceof HttpResponse),
map((r: HttpResponse<any>) =>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* tslint:disable */
/* eslint-disable */
{{autoGenerationNotice}}
import { HttpHeaders, HttpParameterCodec, HttpParams, HttpRequest, HttpResponse } from '@angular/common/http';
import { HttpHeaders, HttpParameterCodec, HttpParams, HttpRequest, HttpResponse, HttpContext } from '@angular/common/http';

/** Constrains the HTTP response body to be non-null */
export type {{responseClass}}<T> = HttpResponse<T> & { readonly body: T };
Expand Down Expand Up @@ -308,6 +308,9 @@ export class {{requestBuilderClass}} {

/** Whether to report progress on uploads / downloads */
reportProgress?: boolean;

/** Allow passing HttpContext for HttpClient */
context?: HttpContext;
}): HttpRequest<T> {
options = options || {};

Expand Down Expand Up @@ -346,6 +349,7 @@ export class {{requestBuilderClass}} {
headers: httpHeaders,
responseType: options.responseType,
reportProgress: options.reportProgress,
context: options.context,
});
}
}
2 changes: 1 addition & 1 deletion packages/ng-openapi-gen/src/templates/service.handlebars
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* tslint:disable */
/* eslint-disable */
{{autoGenerationNotice}}
import { HttpClient, HttpResponse } from '@angular/common/http';
import { HttpClient, HttpResponse, HttpContext } from '@angular/common/http';
import { Inject, Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { filter, map } from 'rxjs/operators';
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit 489ffae

Please sign in to comment.