typed-rest-client
is a library for , which provides a type-safe way to interact with the RESTful API. Its main functions include:
Install typed-rest-client
To usetyped-rest-client
, first you need to install it, you can install it through npm:
$ npm install typed-rest-client
Use typed-rest-client
Here it is assumed that there is an express server that provides two REST APIs, one is to obtain user list and the other is to obtain user information.
import express, { Express, Request, Response } from "express"; const app: Express = express(); const port = || 3000; ("/", (req: Request, res: Response) => { ("Express + TypeScript Server"); }); ("/users", (req: Request, res: Response) => { const users = [ { name: 'kongxx', password: 'password', email: 'kongxx@' }, { name: 'Mandy', password: 'password', email: 'mandy@' } ] (users); }); ("/users/:id", (req: Request, res: Response) => { const user = { name: 'kongxx', password: 'password', email: 'kongxx@' } (user); }); (port, () => { (`[server]: Server is running at http://localhost:${port}`); });
Below is the test procedure
import {RestClient, IRestResponse} from 'typed-rest-client/RestClient'; interface User { name: string; password: string; email: string; } async function test() { const rc: RestClient = new RestClient('test', 'http://localhost:3000'); const resUsers: IRestResponse<User[]> = await <User[]>('/users'); ('get users ...'); ('response: ', resUsers); ('statusCode: ', ); ('name: ', [0]?.name); ('email: ', [0]?.email); const resUser: IRestResponse<User> = await <User>('/users/1'); ('get user ...'); ('response: ', resUser); ('statusCode: ', ); ('name: ', ?.name); ('email: ', ?.email); } test();
Here we first define an interface, describing the data structure used by the REST API.
- Call
RestClient
ofget
Method, pass in URL and return data type, return aIRestResponse
Object,IRestResponse
The object contains the status code, response header, and response body of the HTTP response. - pass
statusCode
The attribute can obtain the status code of the HTTP response. - pass
headers
The attribute can obtain the HTTP response header. - pass
result
Attributes can obtain data in the response body.
test
First start the express server.
$ npm run dev
Run the test program
$ npm install -g typescript $ tsc src/ && node src/ get users ... response: { statusCode: 200, result: [ { name: 'kongxx', password: 'password', email: 'kongxx@' }, { name: 'Mandy', password: 'password', email: 'mandy@' } ], headers: { 'x-powered-by': 'Express', 'content-type': 'application/json; charset=utf-8', 'content-length': '137', etag: 'W/"89-50ejbxheoPkdk58Nm75VjrVs3YE"', date: 'Mon, 23 Sep 2024 01:01:04 GMT', connection: 'close' } } statusCode: 200 name: kongxx email: kongxx@ get user ... response: { statusCode: 200, result: { name: 'kongxx', password: 'password', email: 'kongxx@' }, headers: { 'x-powered-by': 'Express', 'content-type': 'application/json; charset=utf-8', 'content-length': '68', etag: 'W/"44-WML8FV1wUhoW//8kQuCB8B/FWaQ"', date: 'Mon, 23 Sep 2024 01:01:04 GMT', connection: 'close' } } statusCode: 200 name: kongxx email: kongxx@
This is the article about how to use typed-rest-client to make REST API calls. For more related typed-rest-client REST API calls, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!