validated-service
Easily build reliable service with Typescript.
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
; ;
login.service.ts
; ;
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"