npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

yan-client

v0.2.0

Published

Yan Client 是一种声明式的 NodeJS Http客户端。默认基于 (request)[]。使用了 ES7 decorators 语法,更加简介,方便。

Downloads

16

Readme

Yan Client

Yan Client 是一种声明式的 NodeJS Http客户端。默认基于 (request)[]。使用了 ES7 decorators 语法,更加简介,方便。

使用

npm install yan-client --save

例子

import YanClient, {GetMapping, PostMapping, PutMapping, DeleteMapping, Params} from 'yan-client';
export default new class UserClient {
    @GetMapping('http://example.com/users')
    @YanClient()
    getUsers() {}
    
    @GetMapping('http://example.com/users/:userId')
    @Params('params:userId')
    @YanClient()
    getUser(userId) {}
    
    @PostMapping('http://example.com/users')
    @YanClient()
    createUser(user) {}
    
    @DeleteMapping('http://example.com/users/:userId')
    @Params('params:userId')
    @YanClient()
    deleteUser(userId) {}
    
    @PutMapping('http://example.com/users/:userId')
    @Params('params:userId', 'body')
    @YanClient()
    updateUser(userId, user) {}
}

文档

@RequestMapping(method, url)

@***Mapping(url)

用来设置请求的 URL。

@Header(key, value)

用来设置请求的 header。

@Params(...params)

用来确定函数的参数和请求的参数的映射关系,每个参数和函数的参数按照顺序一一对应; 其中每个参数都是由 [prefix]:[postfix] 这样的表达式组成,postfix 可以为空, 当 postfix 为空的时候,会替换 request 对象上某个属性。例如:

class Test {
    @Params("body")
    addUser(user) {} // body: {user: {username: 'test', password: 'password'}}
}

当 postfix 不为空的时候会替换 request 对象某个属性的具体值, postfix 支持按照路径的方式比如 (body:user.username)。例如:

class Test {
    @Params("params:userId", "body:user.enable")
    updateUser(userId, enable) {} // body: {user: {enable: ""}}
}

目前支持的 prefix 有:

  • endpoint
  • params
  • qs
  • headers
  • body

@ResponseBody

如果有这个装饰器,返回的结果就是 response 对象的 body。如果自定义 client 没有返回 body,最后仍然会返回 response。

@ResponseHeader

如果有这个装饰器,返回的是 response 对象的 header。如果自定义 client 没有返回 header,最后仍然会返回 response。

@YanClient(client)

必须位于所有的装饰器之后,用来最终发送 http 请求,如果不想使用 默认的 request 库来发送请求, 可以通过 client 参数来进行自定义。

const customClient = {
    send(options) {
        /*
            options.url
            options.method
            options.body
            options.params
            options.headers
            options.qs
         */
        
        //full response eg {body: {}, header: {}, status: 200}
        //if you return a body object, the @ResponseBody and @ResponseHeader will be unused.
        return response;
    }
}

class Test {
    @YanClient(customClient)
    test() {}
}