欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

如何使用?typed-rest-client?進(jìn)行?REST?API?調(diào)用

 更新時(shí)間:2024年09月24日 11:33:39   作者:kongxx  
typed-rest-client是專(zhuān)為Node.js設(shè)計(jì)的庫(kù),用于以類(lèi)型安全的方式與RESTful?API進(jìn)行交互,用戶(hù)可以方便地訪(fǎng)問(wèn)和處理API返回的數(shù)據(jù),此外,還需要定義相應(yīng)的接口來(lái)描述API返回的數(shù)據(jù)結(jié)構(gòu),確保類(lèi)型安全,感興趣的朋友跟隨小編一起看看吧

typed-rest-client 是一個(gè)用于 Node.js 的庫(kù),它提供了一種類(lèi)型安全的方式來(lái)與 RESTful API 進(jìn)行交互。其主要功能包括:

安裝 typed-rest-client

要使用 typed-rest-client,首先需要安裝它,可以通過(guò) npm 來(lái)安裝:

$ npm install typed-rest-client

使用 typed-rest-client

這里假定有個(gè) express 的 server 提供了兩個(gè) REST API,一個(gè)是獲取用戶(hù)列表,一個(gè)是獲取用戶(hù)信息。

index.ts

import express, { Express, Request, Response } from "express";
const app: Express = express();
const port = process.env.PORT || 3000;
app.get("/", (req: Request, res: Response) => {
    res.send("Express + TypeScript Server");
});
app.get("/users", (req: Request, res: Response) => {
    const users = [
        {
            name: 'kongxx',
            password: 'password',
            email: 'kongxx@example.com'
        },
        {
            name: 'Mandy',
            password: 'password',
            email: 'mandy@example.com'
        }
    ]
    res.json(users);
});
app.get("/users/:id", (req: Request, res: Response) => {
    const user = {
        name: 'kongxx',
        password: 'password',
        email: 'kongxx@example.com'
    }
    res.json(user);
});
app.listen(port, () => {
    console.log(`[server]: Server is running at http://localhost:${port}`);
});

下面是測(cè)試程序

test.ts

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 rc.get<User[]>('/users');
    console.log('get users ...');
    console.log('response: ', resUsers);
    console.log('statusCode: ', resUsers.statusCode);
    console.log('name: ', resUsers.result[0]?.name);
    console.log('email: ', resUsers.result[0]?.email);
    const resUser: IRestResponse<User> = await rc.get<User>('/users/1');
    console.log('get user ...');
    console.log('response: ', resUser);
    console.log('statusCode: ', resUser.statusCode);
    console.log('name: ', resUser.result?.name);
    console.log('email: ', resUser.result?.email);
}
test();

這里首先定義了一個(gè) interface,描述了 REST API 返回使用的數(shù)據(jù)結(jié)構(gòu)。

  • 調(diào)用 RestClientget 方法,傳入 URL 和返回的數(shù)據(jù)類(lèi)型,返回一個(gè) IRestResponse 對(duì)象,IRestResponse 對(duì)象包含了 HTTP 響應(yīng)的狀態(tài)碼、響應(yīng)頭和響應(yīng)體。
  • 通過(guò) statusCode 屬性可以獲取到 HTTP 響應(yīng)的狀態(tài)碼。
  • 通過(guò) headers 屬性可以獲取到 HTTP 響應(yīng)頭。
  • 通過(guò) result 屬性可以獲取到響應(yīng)體中的數(shù)據(jù)。

測(cè)試

首先啟動(dòng)express server。

$ npm run dev

運(yùn)行測(cè)試程序

$ npm install -g typescript
$ tsc src/test.ts  && node src/test.js
get users ...
response:  {
  statusCode: 200,
  result: [
    {
      name: 'kongxx',
      password: 'password',
      email: 'kongxx@example.com'
    },
    {
      name: 'Mandy',
      password: 'password',
      email: 'mandy@example.com'
    }
  ],
  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@example.com
get user ...
response:  {
  statusCode: 200,
  result: { name: 'kongxx', password: 'password', email: 'kongxx@example.com' },
  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@example.com

到此這篇關(guān)于如何使用 typed-rest-client 進(jìn)行 REST API 調(diào)用的文章就介紹到這了,更多相關(guān)typed-rest-client REST API 調(diào)用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論