@brandondunc/typescript-rest

中文文档
3.0.7发布于 2024/11/09MITTypeScriptCommonJS近 30 天下载 1.1万

A Library to create RESTFul APIs with Typescript, forked from typscript-rest

原始使用文档
文档目录7 节
  1. Preface
  2. Installation
  3. Configuration
  4. Basic Usage
  5. Using with an IoC Container
  6. Complete Guide
  7. Boilerplate Project

npm version Master Workflow Coverage Status Known Vulnerabilities

REST Services for Typescript

This is a lightweight annotation-based expressjs extension for typescript.

It can be used to define your APIs using decorators.

Table of Contents

Preface

This repository was forked in August 2022 to address critical security updates since the last official typescript-rest release for those still using the library.

Installation

This library only works with typescript. Ensure it is installed:

npm install typescript -g

To install typescript-rest:

npm install typescript-rest --save

Configuration

Typescript-rest requires the following TypeScript compilation options in your tsconfig.json file:

{
  "compilerOptions": {
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "target": "es6" // or anything newer like esnext
  }
}

Basic Usage

import * as express from "express";
import {Server, Path, GET, PathParam} from "typescript-rest";

@Path("/hello")
class HelloService {
  @Path(":name")
  @GET
  sayHello( @PathParam('name') name: string ): string {
    return "Hello " + name;
  }
}

let app: express.Application = express();
Server.buildServices(app);

app.listen(3000, function() {
  console.log('Rest Server listening on port 3000!');
});

That's it. You can just call now:

GET http://localhost:3000/hello/joe

Using with an IoC Container

Install the IoC container and the serviceFactory for the IoC Container

npm install typescript-rest --save
npm install typescript-ioc --save
npm install typescript-rest-ioc --save

Then add a rest.config file in the root of your project:

{
  "serviceFactory": "typescript-rest-ioc"
}

And you can use Injections, Request scopes and all the features of the IoC Container. It is possible to use it with any other IoC Container, like Inversify.

Example:

class HelloService {
  sayHello(name: string) {
    return "Hello " + name;
  }
}

@Path("/hello")
class HelloRestService {
  @Inject
  private helloService: HelloService;

  @Path(":name")
  @GET
  sayHello( @PathParam('name') name: string): string {
    return this.sayHello(name);
  }
}

Complete Guide

Check our documentation.

Boilerplate Project

You can check this project to get started.

版本与发布记录

@brandondunc/typescript-rest 共发布 3 个版本,首次发布于 2022/08/27;过去 12 个月发布了 0 个版本。

版本发布日期距今
3.0.7latest2024/11/092 年前
3.0.62023/07/183 年前
3.0.52022/08/274 年前

关于 @brandondunc/typescript-rest 的常见问题

@brandondunc/typescript-rest 是什么?

A Library to create RESTFul APIs with Typescript, forked from typscript-rest

如何安装 @brandondunc/typescript-rest?

在项目目录运行 npm install @brandondunc/typescript-rest,或使用 pnpm add / yarn add / bun add。

@brandondunc/typescript-rest 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@brandondunc/typescript-rest 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@brandondunc/typescript-rest 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=6.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@brandondunc/typescript-rest 可以商用吗?

@brandondunc/typescript-rest 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@brandondunc/typescript-rest 的下载量有多少?

根据 npm 官方下载统计,@brandondunc/typescript-rest 在 2026/10/09 – 2026/11/07 这 30 天内被下载 10,713 次,日均约 357 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@brandondunc/typescript-rest 还在维护吗?

最新版本 3.0.7 发布于 2024/11/09(2 年前);至今共发布 3 个版本,其中过去 12 个月发布了 0 个;首次发布于 2022/08/27。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。