Files
openapi-clean-arch-gen/templates/api.repository.impl.mustache
didavila d47afb6ff1
All checks were successful
Lint / lint (pull_request) Successful in 31s
fix: body param and 2xx response codes in repository generation
Bug 1 - Body as positional argument (api.repository.impl.mustache):
MRepository HTTP methods accept a single RequestOptions object as second
argument. The template was incorrectly passing body as a separate positional
argument (e.g. this.post('/url', body)), causing:
  'Type X has no properties in common with type RequestOptions'
Fix: merge body into the options object using ES6 shorthand { body }, and
introduce hasOptions / hasBothParamsAndBody flags to build a single unified
options literal covering all scenarios:
  - no options    → this.post('/url')
  - params only   → this.get('/url', { params: { search } })
  - body only     → this.post('/url', { body })
  - params + body → this.post('/url', { params: { search }, body })

Bug 2 - Only 200 responses read (clean-arch.generator.ts):
The generator was hardcoded to read op.responses['200'], silently ignoring
201 Created, 202 Accepted, etc. POST endpoints returning 201 were generated
as Observable<void> instead of their actual return type.
Fix: resolve the first available success code from [200, 201, 202, 203].

New fields added to TagOperation type:
  - uppercaseHttpMethod: string
  - hasOptions: boolean
  - hasBothParamsAndBody: boolean

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-03-26 11:17:37 +01:00

59 lines
2.6 KiB
Plaintext

{{#apiInfo}}
{{#apis}}
{{#operations}}
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { environment } from '@environment';
import { MRepository } from '@mercadona/core/utils/repository';
import { {{classname}}Repository } from '@/domain/repositories/{{classFilename}}.repository.contract';
{{#returnImports}}
import { {{classname}}Dto } from '@/dtos/{{classFilename}}.dto';
import { {{classname}} } from '@/entities/models/{{classFilename}}.model';
import { {{classVarName}}Mapper } from '@/mappers/{{classFilename}}.mapper';
{{/returnImports}}
{{#paramImports}}
import { {{classname}} } from '@/entities/models/{{classFilename}}.model';
{{/paramImports}}
/**
* {{classname}} Repository Implementation
* Generated from OpenAPI tag: {{classname}}
*/
@Injectable()
export class {{classname}}RepositoryImpl extends MRepository implements {{classname}}Repository {
constructor() {
super(`${environment.{{environmentApiKey}}.url}`);
}
{{#operation}}
{{nickname}}({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}{{^-last}}, {{/-last}}{{/allParams}}): Observable<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/returnType}}> {
{{#isListContainer}}
return this.{{httpMethod}}<{{{returnBaseType}}}Dto>('{{path}}'{{#hasOptions}}, { {{#hasQueryParams}}params: { {{#queryParams}}{{paramName}}{{^-last}}, {{/-last}}{{/queryParams}} }{{/hasQueryParams}}{{#hasBothParamsAndBody}}, {{/hasBothParamsAndBody}}{{#hasBodyParam}}body{{/hasBodyParam}} }{{/hasOptions}})
.pipe(
map((response) => response.{{#vendorExtensions}}{{x-response-property}}{{/vendorExtensions}}{{^vendorExtensions}}items{{/vendorExtensions}}.map({{{returnBaseTypeVarName}}}Mapper))
);
{{/isListContainer}}
{{^isListContainer}}
{{#returnType}}
return this.{{httpMethod}}<{{{returnType}}}Dto>('{{path}}'{{#hasOptions}}, { {{#hasQueryParams}}params: { {{#queryParams}}{{paramName}}{{^-last}}, {{/-last}}{{/queryParams}} }{{/hasQueryParams}}{{#hasBothParamsAndBody}}, {{/hasBothParamsAndBody}}{{#hasBodyParam}}body{{/hasBodyParam}} }{{/hasOptions}})
.pipe(
map({{{returnTypeVarName}}}Mapper)
);
{{/returnType}}
{{^returnType}}
return this.{{httpMethod}}<void>('{{path}}'{{#hasOptions}}, { {{#hasQueryParams}}params: { {{#queryParams}}{{paramName}}{{^-last}}, {{/-last}}{{/queryParams}} }{{/hasQueryParams}}{{#hasBothParamsAndBody}}, {{/hasBothParamsAndBody}}{{#hasBodyParam}}body{{/hasBodyParam}} }{{/hasOptions}});
{{/returnType}}
{{/isListContainer}}
}
{{/operation}}
}
{{/operations}}
{{/apis}}
{{/apiInfo}}