Skip to content

Commit

Permalink
fix(angular): add request overloads based on observe property (#1633)
Browse files Browse the repository at this point in the history
  • Loading branch information
QuietNatu authored Sep 19, 2024
1 parent f523b7c commit 398b667
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
11 changes: 10 additions & 1 deletion packages/angular/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ const ANGULAR_DEPENDENCIES: GeneratorDependency[] = [
{ name: 'HttpHeaders' },
{ name: 'HttpParams' },
{ name: 'HttpContext' },
{ name: 'HttpResponse', alias: 'AngularHttpResponse' }, // alias to prevent naming conflict with msw
{ name: 'HttpEvent' },
],
dependency: '@angular/common/http',
},
Expand Down Expand Up @@ -212,7 +214,14 @@ const generateImplementation = (
hasSignal: false,
});

return ` ${operationName}<TData = ${dataType}>(\n ${toObjectString(
const propsDefinition = toObjectString(props, 'definition');
const overloads = isRequestOptions
? `${operationName}<TData = ${dataType}>(\n ${propsDefinition} options?: Omit<HttpClientOptions, 'observe'> & { observe?: 'body' }\n ): Observable<TData>;
${operationName}<TData = ${dataType}>(\n ${propsDefinition} options?: Omit<HttpClientOptions, 'observe'> & { observe?: 'response' }\n ): Observable<AngularHttpResponse<TData>>;
${operationName}<TData = ${dataType}>(\n ${propsDefinition} options?: Omit<HttpClientOptions, 'observe'> & { observe?: 'events' }\n ): Observable<HttpEvent<TData>>;`
: '';

return ` ${overloads}${operationName}<TData = ${dataType}>(\n ${toObjectString(
props,
'implementation',
)} ${
Expand Down
32 changes: 32 additions & 0 deletions samples/angular-app/src/api/endpoints/pets/pets.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
import { HttpClient } from '@angular/common/http';
import type {
HttpContext,
HttpEvent,
HttpHeaders,
HttpParams,
HttpResponse as AngularHttpResponse,
} from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
Expand Down Expand Up @@ -51,6 +53,21 @@ export class PetsService {
/**
* @summary Create a pet
*/
createPets<TData = void>(
createPetsBody: CreatePetsBody,
version?: number,
options?: Omit<HttpClientOptions, 'observe'> & { observe?: 'body' },
): Observable<TData>;
createPets<TData = void>(
createPetsBody: CreatePetsBody,
version?: number,
options?: Omit<HttpClientOptions, 'observe'> & { observe?: 'response' },
): Observable<AngularHttpResponse<TData>>;
createPets<TData = void>(
createPetsBody: CreatePetsBody,
version?: number,
options?: Omit<HttpClientOptions, 'observe'> & { observe?: 'events' },
): Observable<HttpEvent<TData>>;
createPets<TData = void>(
createPetsBody: CreatePetsBody,
version: number = 1,
Expand All @@ -61,6 +78,21 @@ export class PetsService {
/**
* @summary Info for a specific pet
*/
showPetById<TData = Pet>(
petId: string,
version?: number,
options?: Omit<HttpClientOptions, 'observe'> & { observe?: 'body' },
): Observable<TData>;
showPetById<TData = Pet>(
petId: string,
version?: number,
options?: Omit<HttpClientOptions, 'observe'> & { observe?: 'response' },
): Observable<AngularHttpResponse<TData>>;
showPetById<TData = Pet>(
petId: string,
version?: number,
options?: Omit<HttpClientOptions, 'observe'> & { observe?: 'events' },
): Observable<HttpEvent<TData>>;
showPetById<TData = Pet>(
petId: string,
version: number = 1,
Expand Down

0 comments on commit 398b667

Please sign in to comment.