question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ApiReponse with type is an Advanced Types

See original GitHub issue

Hi,

I have an generic class who is return on all my request

import { ApiModelProperty } from '@nestjs/swagger';

export class ServerApiResponse<T> {
  
  @ApiModelProperty()
  value: T;
  
  @ApiModelProperty({ type: Number })
  pages: number;


  @ApiModelProperty({ type: Number })
  page: number;

  @ApiModelProperty({ type: Number })
  maxByPage: number;


  @ApiModelProperty({ type: Number })
  count: number;

  @ApiModelProperty({ type: Object })
  error: any;
}

And on my endpoint i have add

@ApiResponse({
    status: 200,
    description: '',
    type: ServerApiResponse<PatientCaretrackPopulatedOutput[]>
  })

But i have this error Error TS2348: Value of type 'ServerApiResponse' is not callable.

So i tried this

@ApiResponse({
    status: 200,
    description: '',
    type:  new ServerApiResponse<PatientCaretrackPopulatedOutput[]>()
  })

But when i launch swagger i have no description of response returned.

It’s possible to do this ?

Thx

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
alisherkscommented, Jan 24, 2018

It’s not possible for the same reason as Array<T> or T[] (look at the issue Microsoft/TypeScript/issues/10576). You could try the following:

export abstract class ServiceApiResponse<T> {
  abstract get value(): T;
  
  @ApiModelProperty({ type: Number })
  pages: number;


  @ApiModelProperty({ type: Number })
  page: number;

  @ApiModelProperty({ type: Number })
  maxByPage: number;


  @ApiModelProperty({ type: Number })
  count: number;

  @ApiModelProperty({ type: Object })
  error: any;
}

export class PatientCaretrackPopulatedOutputResponse extends ServiceApiResponse<PatientCaretrackPopulatedResponse[]> {
  @ApiModelProperty({ type: PatientCaretrackPopulatedResponse, isArray: true })
  value: PatientCaretrackPopulatedResponse[];
}
3reactions
omarcommented, Mar 5, 2018

Cross posting this, does that mean a generic controller wouldn’t be able to expose the proper endpoints? For example, something like this (taken from https://github.com/nestjs/nest/issues/228):

export abstract class EntityController<T> {
    constructor(protected readonly service: EntityService<T>) { }

    @Get()
    async findAll() {
        return this.service.findAll();
    }

    @Get(':id')
    async getById(@Param() params) {
        return this.service.getById(params.id);
    }

    @Post()
    async create(@Body() data: Partial<T>) {
        return this.service.create(data);
    }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Operations - OpenAPI - A progressive Node.js framework
Advanced: Generic ApiResponse #. With the ability to provide Raw Definitions, we can define Generic schema for Swagger UI. Assume we have the...
Read more >
Advanced Typescript by Example: API Service Manager
This article will take you through the implementation of an API service manager written in Typescript to efficiently handle web services, ...
Read more >
OpenAPI for your REST APIs in NestJS - notiz.dev
Annotate your REST endpoints with the custom @ApiResponse() specifying the status code and the response type or choose a short-hand API ...
Read more >
Playground Example - Discriminate Types - TypeScript
A discriminated type union is where you use code flow analysis to reduce a ... www.typescriptlang.org/docs/handbook/advanced-types.html#discriminated-unions ...
Read more >
TypeScript: advanced and esoteric | Konstantin Lebedev blog
If you already know how to use the aforementioned advanced types, ... type ApiResponse = { data: Array<{ firstName: string lastName: string ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found