1.1.1 • Published 5 months ago

@typed-web-api/common v1.1.1

Weekly downloads
-
License
ISC
Repository
github
Last release
5 months ago

@typed-web-api/common

Base types to generate an opinionated type declaration for a nodeJS web API. Such type declaration can later be used in both client (@typed-web-api/client) and server (@typed-web-api/express or @typed-web-api/nestjs) sides to validate the payload and response of Http requests.

Example

Given a sample web API with three endpoints (POST /login, GET /users, GET /users/:userId), this is what the type declaration would look like:

import { EndpointDefinition } from '@typed-web-api/common';
import { LoginResponse, User } from '...';

export type WebApiTypes = {
  '/login_post': EndpointDefinition<LoginResponse>;
  '/users_get': EndpointDefinition<User[]>;
  '/users/:userId_get': EndpointDefinition<User>;
};

Optionally the payload of the Http requests can be validated as well by using additional helper types:

import { EndpointDefinition, JsonBody, QueryString, UrlParams } from '@typed-web-api/common';
import { LoginResponse, User } from '...';

export type WebApiTypes = {
  '/login_post': EndpointDefinition<LoginResponse, JsonBody<{ email: string; password: string }>>;
  '/users_get': EndpointDefinition<User[], QueryString<{ limit?: string; skip?: string }>>;
  '/users/:userId_get': EndpointDefinition<User, UrlParams<{ userId: string }>>;
};