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

@runapi/decorators

v12.3.1

Published

@runapi/decorators

Downloads

9

Readme


sidebarDepth: 2

指南

Decorators

@runapi/requestor: 基于"@runapi/requestor"的装饰器,使用装饰模式定义接口请求函数。

Service Decorator

使用 Service 装饰器将 class 定义为 Service

import { createFetchRequestor, createRequestContext } from "@runapi/requestor";
import { Service } from "@runapi/decorators";

const requestor = createFetchRequestor(createRequestContext().setBaseUrl("https://xxx.com"));

@Service(requestor)
class UserService {}

export const userService = new UserService();

Http Decorator

使用 Http 装饰器定义请求方法

import { createFetchRequestor, createRequestContext } from "@runapi/requestor";
import { Http, ResponseContextPromise, Service } from "@runapi/decorators";

const requestor = createFetchRequestor(createRequestContext().setBaseUrl("https://xxx.com"));

interface User {
  userId: string;
  username: string;
  gender: string;
}

@Service(requestor)
class UserService {
  @Http("GET", "/user/:userId", { params: { userId: "1" } })
  findUserByUserId(): ResponseContextPromise<User> {}
}

export const userService = new UserService();

Get Decorator

使用 Get 装饰器定义获取资源请求方法

import { createFetchRequestor, createRequestContext } from "@runapi/requestor";
import { Get, ResponseContextPromise, Service } from "@runapi/decorators";

const requestor = createFetchRequestor(createRequestContext().setBaseUrl("https://xxx.com"));

interface User {
  userId: string;
  username: string;
  gender: string;
}

@Service(requestor)
class UserService {
  @Get("/users", { query: { username: "jake", page: 1, size: 10 } })
  findUsersByPage(): ResponseContextPromise<User[]> {}
}

export const userService = new UserService();

Post Decorator

使用 Post 装饰器定义创建资源请求方法

import { createFetchRequestor, createRequestContext } from "@runapi/requestor";
import { Post, ResponseContextPromise, Service } from "@runapi/decorators";

const requestor = createFetchRequestor(createRequestContext().setBaseUrl("https://xxx.com"));

interface User {
  userId: string;
  username: string;
  gender: string;
}

@Service(requestor)
class UserService {
  @Post("/user", { body: { username: "jake", gender: "male" } })
  createUser(): ResponseContextPromise<User> {}
}

export const userService = new UserService();

Put Decorator

使用 Put 装饰器定义替换资源请求方法

import { createFetchRequestor, createRequestContext } from "@runapi/requestor";
import { Put, ResponseContextPromise, Service } from "@runapi/decorators";

const requestor = createFetchRequestor(createRequestContext().setBaseUrl("https://xxx.com"));

interface User {
  userId: string;
  username: string;
  gender: string;
}

@Service(requestor)
class UserService {
  @Put("/user", { body: { userId: "1", username: "jake", gender: "female" } })
  updateUser(): ResponseContextPromise<User> {}
}

export const userService = new UserService();

Patch Decorator

使用 Patch 装饰器定义更新资源请求方法

import { createFetchRequestor, createRequestContext } from "@runapi/requestor";
import { Patch, ResponseContextPromise, Service } from "@runapi/decorators";

const requestor = createFetchRequestor(createRequestContext().setBaseUrl("https://xxx.com"));

interface User {
  userId: string;
  username: string;
  gender: string;
}

@Service(requestor)
class UserService {
  @Patch("/user", { body: { userId: "2", username: "rose" } })
  updateUser(): ResponseContextPromise<User> {}
}

export const userService = new UserService();

Delete Decorator

使用 Delete 装饰器定义删除资源请求方法

import { createFetchRequestor, createRequestContext } from "@runapi/requestor";
import { Delete, ResponseContextPromise, Service } from "@runapi/decorators";

const requestor = createFetchRequestor(createRequestContext().setBaseUrl("https://xxx.com"));

interface User {
  userId: string;
  username: string;
  gender: string;
}

@Service(requestor)
class UserService {
  @Delete("/user/:userId", { params: { userId: "1" } })
  deleteUser(): ResponseContextPromise<User> {}
}

export const userService = new UserService();

Params Decorator

使用 Params 装饰器定义路径参数

import { createFetchRequestor, createRequestContext } from "@runapi/requestor";
import { Get, Params, ResponseContextPromise, Service } from "@runapi/decorators";

const requestor = createFetchRequestor(createRequestContext().setBaseUrl("https://xxx.com"));

interface User {
  userId: string;
  username: string;
  gender: string;
}

@Service(requestor)
class UserService {
  @Get("/user/:userId")
  findUserByUserId(@Params() params: { userId: string }): ResponseContextPromise<User> {}
}

export const userService = new UserService();

Query Decorator

使用 Query 装饰器定义查询字符串参数

import { createFetchRequestor, createRequestContext } from "@runapi/requestor";
import { Get, Query, ResponseContextPromise, Service } from "@runapi/decorators";

const requestor = createFetchRequestor(createRequestContext().setBaseUrl("https://xxx.com"));

interface User {
  userId: string;
  username: string;
  gender: string;
}

@Service(requestor)
class UserService {
  @Get("/users")
  findUsersByPage(@Query() query: { username: string; page: number; size: number }): ResponseContextPromise<User[]> {}
}

export const userService = new UserService();

Body Decorator

使用 Body 装饰器定义请求体参数

import { createFetchRequestor, createRequestContext } from "@runapi/requestor";
import { Body, Put, ResponseContextPromise, Service } from "@runapi/decorators";

const requestor = createFetchRequestor(createRequestContext().setBaseUrl("https://xxx.com"));

interface User {
  userId: string;
  username: string;
  gender: string;
}

@Service(requestor)
class UserService {
  @Put("/user")
  updateUser(@Body() body: { userId: string; username: string; gender: string }): ResponseContextPromise<User> {}
}

export const userService = new UserService();

BaseUrl Decorator

使用 BaseUrl 装饰器定义请求基础 URL

import { createFetchRequestor, createRequestContext } from "@runapi/requestor";
import { BaseUrl, Get, Params, ResponseContextPromise, Service } from "@runapi/decorators";

const requestor = createFetchRequestor(createRequestContext());

interface User {
  userId: string;
  username: string;
  gender: string;
}

@Service(requestor)
class UserService {
  @Get("/user/:userId")
  @BaseUrl("https://xxx.com")
  findUserByUserId(@Params() params: { userId: string }): ResponseContextPromise<User> {}
}

export const userService = new UserService();

Headers Decorator

使用 Headers 装饰器定义请求头

import { createFetchRequestor, createRequestContext } from "@runapi/requestor";
import { BaseUrl, Headers, Get, Params, ResponseContextPromise, Service } from "@runapi/decorators";

const requestor = createFetchRequestor(createRequestContext());

interface User {
  userId: string;
  username: string;
  gender: string;
}

@Service(requestor)
class UserService {
  @Get("/user/:userId")
  @BaseUrl("https://xxx.com")
  @Headers({ "content-type": "application/json" })
  findUserByUserId(@Params() params: { userId: string }): ResponseContextPromise<User> {}
}

export const userService = new UserService();

Mock Decorator

使用 Mock 装饰器定义请求模拟数据模版

import { createFetchRequestor, createRequestContext } from "@runapi/requestor";
import { Get, Mock, Query, ResponseContextPromise, Service } from "@runapi/decorators";

const requestor = createFetchRequestor(createRequestContext().setBaseUrl("https://xxx.com"));

interface User {
  userId: string;
  username: string;
  gender: string;
}

@Service(requestor)
class UserService {
  @Get("/users")
  @Mock({
    "users|1-10": [
      {
        "id|+1": 1,
        username: "@cname",
        gender: "gender",
      },
    ],
  })
  findUsersByPage(@Query() query: { username: string; page: number; size: number }): ResponseContextPromise<{ users: User[] }> {}
}

export const userService = new UserService();

Model Decorator

使用 Model 装饰器定义响应数据模型

import { createFetchRequestor, createRequestContext } from "@runapi/requestor";
import { Get, Model, Params, ResponseContextPromise, Service } from "@runapi/decorators";

const requestor = createFetchRequestor(createRequestContext().setBaseUrl("https://xxx.com"));

class User {
  userId!: string;
  firstname!: string;
  lastname!: string;
  gender!: string;

  get username() {
    return this.firstname + this.lastname;
  }
}

@Service(requestor)
class UserService {
  @Get("/user/:userId")
  @Model(User)
  findUserByUserId(@Params() params: { userId: string }): ResponseContextPromise<User> {}
}

Similar Decorator

使用 Similar 装饰器定义类似请求处理方式

import { createFetchRequestor, createRequestContext } from "@runapi/requestor";
import { Get, Query, ResponseContextPromise, Service, Similar } from "@runapi/decorators";

const requestor = createFetchRequestor(createRequestContext().setBaseUrl("https://xxx.com"));

interface User {
  userId: string;
  username: string;
  gender: string;
}

@Service(requestor)
class UserService {
  @Get("/users")
  // @Similar("wait-done")
  @Similar("abort")
  findUsersByPage(@Query() query: { username: string; page: number; size: number }): ResponseContextPromise<User[]> {}
}

export const userService = new UserService();