Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Improvement] Multiple returns and status codes #17

Open
ricardo-devis-agullo opened this issue Apr 7, 2020 · 1 comment
Open

[Improvement] Multiple returns and status codes #17

ricardo-devis-agullo opened this issue Apr 7, 2020 · 1 comment
Labels
enhancement New feature or request question Further information is requested

Comments

@ricardo-devis-agullo
Copy link

ricardo-devis-agullo commented Apr 7, 2020

Right now, every endpoint can only return one thing, but what if I want to type multiple things, linked to status codes?

Like, instead of just

    listPublications: GET `/publications/${'category'}`
        .response(PublicationsList),

It would be nice if we could do something like

import { StatusCodes } from 'rest-ts-core';

const ErrorModel = rt.Record({
  message: rt.String,
  code: rt.Number,
});
/// ....
    listPublications: GET `/publications/${'category'}`
        .response(StatusCodes.OK, PublicationsList)
        .response(StatusCodes.NOT_FOUND, rt.Void)
        .response(StatusCodes.INTERNAL_ERROR, ErrorModel),

So then, in my api implementation, I could have some helpers to type the returns to status codes like

.listPublications((req, res) => {
  try {
    const publicationsList = await getPublicationsList();
    if (publicationsList) {
      return StatusCodes.ok(publicationsList);
    } else {
      return StatusCodes.notFound();
    }
  } catch (err) {
    return StatusCodes.internalError({message: 'Something failed', code: 123})
  }
})
@hmil
Copy link
Owner

hmil commented May 13, 2020

This is a niche use case, although it makes complete sense.

There is some other work I'd like to prioritise before getting into this kind of refinement.

@hmil hmil added enhancement New feature or request question Further information is requested labels May 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants