V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
爱意满满的作品展示区。
wenerme
V2EX  ›  分享创造

封装了一个阿里云 NodeJS 客户端

  •  
  •   wenerme ·
    wenerme · 166 天前 · 1083 次点击
    这是一个创建于 166 天前的主题,其中的信息可能已经有所发展或是发生改变。

    • 简单易用的 阿里云 客户端
    • 可在浏览器端使用 - 需要自行处理 cors
      • 测试、工具为主
      • 表明不需要什么依赖
    • 接口定义基于代码生成
      • 默认以 Proxy+Interface 为主 - size 非常小
      • 之后可以考虑 stub method 方式 - 有 metadata

    Note Why?

    1. 阿里云的客户端质量非常的差
      • @alicloud/openapi-client 一个文件、一个包
      • @alicloud/openapi-utils 一个文件、一个包
    2. 阿里云的 API 文档质量非常的差
    3. 我只想要发起一些简单的请求,按需封装结构,请求简单透明,客户端易用

    使用 request

    • 支持任意请求
    • 极少的代码量,最小的 bundle size
    import { request } from '@wener/cleint/alicloud';
    
    console.log(
      await request({
        endpoint: 'dytnsapi.aliyuncs.com',
        action: 'QueryTagInfoBySelection',
        version: '2020-02-17',
        accessKeyId: process.env.ALIBABA_CLOUD_ACCESS_KEY_ID,
        accessKeySecret: process.env.ALIBABA_CLOUD_ACCESS_KEY_SECRET,
      }),
    );
    

    使用客户端

    • 基于 Proxy 的客户端
    • 支持类型推导
    import { AliCloudClient } from '@wener/cleint/alicloud';
    
    const aliCloudClient = new AliCloudClient({
      accessKeyId: process.env.ALIBABA_CLOUD_ACCESS_KEY_ID,
      accessKeySecret: process.env.ALIBABA_CLOUD_ACCESS_KEY_SECRET,
    });
    
    const api = aliCloudClient.getServiceClient({
      // 输入 product 和 version 会有补全
      // 更多的接口待生成
      product: 'Dytnsapi',
      version: '2020-02-17',
    });
    console.log(await api.QueryTagListPage({}));
    
    3 条回复    2023-11-13 13:52:20 +08:00
    entro
        1
    entro  
       166 天前
    支持下
    BTW ,@wener/cleint 的 cleint 看的难受
    wenerme
        2
    wenerme  
    OP
       166 天前
    @entro sorry, 写错了,是 client ,包名本身没错
    ByteCat
        3
    ByteCat  
       166 天前
    不错,支持一下,还是 TS 写的,上次用阿里 SDK 被恶心到了,直接换了腾讯😅
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1856 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 16:17 · PVG 00:17 · LAX 09:17 · JFK 12:17
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.