Natural Performance Manager

    validated-service
    TypeScript icon, indicating that this package has built-in type declarations

    1.0.0 • Public • Published

    validated-service

    Easily build reliable service with Typescript.

    Build Test Coverage Maintainability

    Motivation

    I love working on software architecture, quality and scaling.

    Note : In my softwares architecture, I gather requests / API calls under what I call a service.

    I've recently been working on a web and mobile project where I made tons of API call, and I came to some conclusions :

    • My way of handling API calls was terrible.
    • Not only was it terrible, it's way too common.

    What am I talking about ? What do you think of :

    api.ts

    import axios from 'axios';
     
    export const api = axios.create({
      baseURL: 'http://localhost:80',
    });

    login.service.ts

    import { api } from './api.ts';
     
    type UserToLoginDto = {
      email: string;
      password: string;
    };
     
    export async function loginService({ email, password }: UserToLoginDto) {
      return api
        .post('/auth/login', { email, password })
        .then((response) => ({ user: response.data.user, token: response.data.token.accessToken }))
        .catch((e) => {
          if (e.response.status === 401) throw new Error('bad-password');
          else throw new Error('unknown');
        });
    }

    This is what I've been taught, what I've doing and what I see most people do around me.

    This is fundamentally terrible for the following reasons :

    • If the data sent by the server doesn't have the format I expect, my whole application might crash or display bad informations.
    • These problems come with a very low level of monitoring possible as to : why is this happening ?
    • If my strategy is to throw an error "unknown" when I don't understand what's happening, i'll need to duplicate this everywhere
    • This example lack strict typing

    From this point, my questions were :

    • How do I validate data sent by an endpoint ?
    • What do I do if it's not valid ?
    • How to make this process easy and straightforward
    • How to make this process modulable if :
      • I want to monitor invalid responses
      • On some occasions, I'm able and wish to make an invalid response work.
      • Monitor it differently if I make and invalid response work.

    Features

    • Made with TypeScript
    • @mrtd/services allow you to generate services :
      • Where you can validate responses with your own strategies.
      • Using your own error handling.
      • With a straightforward builder class.
      • Is shipped with default strategies I would recommend.
    • Comes bundled with a CLI : service-build-dto.
      • Allow you to generate json-schemas from the *response.dto.ts definitions you have to validate.

    Code Example

    Installation

    npm install --save validated-service

    Or

    yarn add validated-service

    Add the following scripts in your package.json and run it when you need to compile *response.do.ts to files to json-schemas :

    "schema""service-build-dto"

    License

    MIT

    Keywords

    none

    Install

    npm i validated-service

    DownloadsWeekly Downloads

    1

    Version

    1.0.0

    License

    MIT

    Unpacked Size

    60.5 kB

    Total Files

    41

    Last publish

    Collaborators

    • morintd