Skip to content

Commit

Permalink
feat: add t helper
Browse files Browse the repository at this point in the history
  • Loading branch information
glebbash committed Jan 17, 2022
1 parent 62ed714 commit e745e67
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 8 deletions.
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Decorate endpoints:
*/
import './overrides';

import { Method, NotFound, Ok } from 'typed-http-decorators';
import { Method, NotFound, Ok, t } from 'typed-http-decorators';

class NotFoundDto {
constructor(public message: string) {}
Expand All @@ -42,8 +42,7 @@ class ResourceDto {
export class ResourceController {
@Method.Get('resource/:resourceId', {
permissions: ['resource.get'],
// !!! Do not forget to use `as const`, otherwise TypeScript reports type errors
responses: [Ok.Type(ResourceDto), NotFound.Type(NotFoundDto)] as const,
responses: t(Ok.Type(ResourceDto), NotFound.Type(NotFoundDto)),
})
// You can remove return type annotation and still have type safety
async getResource(): Promise<Ok<ResourceDto> | NotFound<NotFoundDto>> {
Expand Down Expand Up @@ -85,7 +84,7 @@ Controller example:
import { TypedResponseInterceptor } from '../common/typed-response.interceptor'
import { Controller, UseInterceptors } from '@nestjs/common'
import { ApiTags } from '@nestjs/swagger'
import { Method, Ok } from 'typed-http-decorators'
import { Method, Ok, t } from 'typed-http-decorators'
import { FilmsDto } from './dto/films.dto'
import { FilmsService } from './films.service'

Expand All @@ -99,7 +98,7 @@ export class FilmsController {
@Method.Get('', {
summary: 'Get all films',
description: 'Gets all films from the database',
responses: [Ok.Type(FilmsDto)] as const,
responses: t(Ok.Type(FilmsDto)),
})
async getAllFilms() {
return Ok(
Expand Down
7 changes: 4 additions & 3 deletions src/typed-http-decorators.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
NotFound,
Ok,
setEndpointDecorator,
t,
} from './typed-http-decorators';

describe('typed-http-decorators', () => {
Expand All @@ -20,7 +21,7 @@ describe('typed-http-decorators', () => {

class ResourceController {
@Method.Post('resource', {
responses: [Created.Type(ResourceDto), Conflict.Type(ConflictDto)] as const,
responses: t(Created.Type(ResourceDto), Conflict.Type(ConflictDto)),
})
async createResource(id: string) {
if (Math.random() > 0.5) {
Expand All @@ -31,7 +32,7 @@ describe('typed-http-decorators', () => {
}

@Method.Get('resource/:resourceId', {
responses: [Ok.Type(ResourceDto), NotFound.Type(NotFoundDto)] as const,
responses: t(Ok.Type(ResourceDto), NotFound.Type(NotFoundDto)),
})
async getResource(id: string) {
if (Math.random() > 0.5) {
Expand All @@ -58,7 +59,7 @@ describe('typed-http-decorators', () => {
const decorateClass = () => {
class ResourceController {
@Method.Post('resource', {
responses: [Created.Type(ResourceDto)] as const,
responses: t(Created.Type(ResourceDto)),
})
async createResource() {
return Created(new ResourceDto('id', 'name'));
Expand Down
1 change: 1 addition & 0 deletions src/typed-http-decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ export {
export * as Method from './methods';
export * from './responses';
export * from './types';
export const t = <XS extends readonly unknown[]>(...values: XS): XS => values;

0 comments on commit e745e67

Please sign in to comment.