Last active
March 10, 2024 09:05
-
-
Save darylteo/38c357441110a661e9fc04cfb36c8af0 to your computer and use it in GitHub Desktop.
Magical incantation for nestjs swagger plugin generics nestjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"openapi": "3.0.0", | |
"paths": { | |
"/listings": { | |
"get": { | |
"operationId": "ListingsController_findAll", | |
"parameters": [], | |
"responses": { | |
"200": { | |
"description": "", | |
"content": { | |
"application/json": { | |
"schema": { | |
"$ref": "#/components/schemas/FindAllListingsResponse" | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
}, | |
... | |
"components": { | |
"schemas": { | |
"FindAllListingsResponse_Listing": { | |
"type": "object", | |
"properties": { | |
"listingId": { | |
"format": "int64", | |
"type": "integer" | |
}, | |
"address": { | |
"type": "string" | |
}, | |
"deviceId": { | |
"format": "int64", | |
"type": "integer", | |
"nullable": true | |
} | |
}, | |
"required": [ | |
"listingId", | |
"address", | |
"deviceId" | |
] | |
}, | |
"FindAllListingsResponse": { | |
"type": "object", | |
"properties": { | |
"data": { | |
"type": "array", | |
"items": { | |
"$ref": "#/components/schemas/FindAllListingsResponse_Listing" | |
} | |
} | |
}, | |
"required": [ | |
"data" | |
] | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "api", | |
"version": "0.0.1", | |
"description": "", | |
"author": "", | |
"private": true, | |
"license": "UNLICENSED", | |
"scripts": { | |
"build": "nest build", | |
"openapi": "nest start -b swc --type-check --config nest-swagger.json", | |
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"", | |
"start": "nest start -b swc", | |
"start:dev": "nest start --watch -b swc", | |
"start:debug": "nest start --debug --watch -b swc", | |
"start:prod": "node dist/main", | |
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix", | |
"test": "jest", | |
"test:watch": "jest --watch", | |
"test:cov": "jest --coverage", | |
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand", | |
"test:e2e": "jest --config ./test/jest-e2e.json" | |
}, | |
"dependencies": { | |
"@nestjs/common": "^10.0.0", | |
"@nestjs/config": "^3.2.0", | |
"@nestjs/core": "^10.0.0", | |
"@nestjs/jwt": "^10.2.0", | |
"@nestjs/passport": "^10.0.3", | |
"@nestjs/platform-express": "^10.0.0", | |
"@prisma/client": "^5.10.2", | |
"passport": "^0.7.0", | |
"passport-jwt": "^4.0.1", | |
"passport-local": "^1.0.0", | |
"reflect-metadata": "^0.2.0", | |
"rxjs": "^7.8.1" | |
}, | |
"devDependencies": { | |
"@nestjs/cli": "^10.0.0", | |
"@nestjs/schematics": "^10.0.0", | |
"@nestjs/swagger": "^7.3.0", | |
"@nestjs/testing": "^10.0.0", | |
"@swc/cli": "^0.3.9", | |
"@swc/core": "^1.4.2", | |
"@types/express": "^4.17.17", | |
"@types/jest": "^29.5.2", | |
"@types/node": "^20.3.1", | |
"@types/passport-jwt": "^4.0.1", | |
"@types/passport-local": "^1.0.38", | |
"@types/supertest": "^6.0.0", | |
"@typescript-eslint/eslint-plugin": "^6.0.0", | |
"@typescript-eslint/parser": "^6.0.0", | |
"eslint": "^8.42.0", | |
"eslint-config-prettier": "^9.0.0", | |
"eslint-plugin-prettier": "^5.0.0", | |
"jest": "^29.5.0", | |
"prettier": "^3.0.0", | |
"prisma": "^5.10.2", | |
"source-map-support": "^0.5.21", | |
"supertest": "^6.3.3", | |
"ts-jest": "^29.1.0", | |
"ts-loader": "^9.4.3", | |
"ts-node": "^10.9.1", | |
"tsconfig-paths": "^4.2.0", | |
"typescript": "^5.1.3" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Code derived from https://github.com/nestjs/swagger/issues/86 | |
import {ApiProperty} from "@nestjs/swagger"; | |
// used to infer the Resource type | |
export type ClassType<T = any> = new (...args: any[]) => T | |
// apply the appropriate nestjs decorator to signal an array type | |
export function PaginatedResponse<R, T extends ClassType<R>>(cls: T) { | |
class Paginated implements IPaginatedResponse<R> { | |
@ApiProperty({ | |
type: [cls], | |
}) | |
public data: Array<R>; | |
public paging: { | |
pages: number | |
page: number | |
} | |
} | |
return Paginated; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Get() | |
async findAll(): Promise<FindAllListingsResponse> { | |
const result = await ... // db query exercise for dev | |
const listings = result.map(item => ({ | |
listingId: item.listingId, | |
address: item.address, | |
deviceId: item.deviceId, | |
})) | |
return { | |
data: listings, | |
paging: { | |
page: 1, | |
pages: 1, | |
}, | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {PaginatedResponse} from "@src/utils/api.dto"; | |
export class FindAllListingsResponse_Listing { | |
listingId: bigint | |
address: string | |
deviceId: bigint | null | |
} | |
// pass the resource you want to return a collection of | |
// the generics will take care of the rest | |
export class FindAllListingsResponse extends PaginatedResponse(FindAllListingsResponse_Listing) {} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment