@antelopejs/api

中文文档
1.7.0发布于 2026/11/05Apache-2.0TypeScriptCommonJS构建来源已验证近 30 天下载 5809

HTTP and WebSocket server module for AntelopeJS, implementing the API interfaces with the Node.js http, https and ws servers

原始使用文档
文档目录10 节
  1. Interfaces
  2. Install
  3. Configuration
  4. Config variables
  5. Startup gate
  6. Quick start
  7. Documentation
  8. Compatibility
  9. Contributing
  10. License

@antelopejs/api

npm License Discord Documentation

The HTTP and WebSocket server module of AntelopeJS. It implements the API interfaces on the Node.js http, https, and ws servers, so the controllers and routes that other modules declare with @antelopejs/interface-api answer requests.

Part of AntelopeJS. Used by AntelopeJS DMS, the open-source back office for your PostgreSQL, MongoDB or RethinkDB database.

Interfaces

InterfaceDocumentation
@antelopejs/interface-apiAPI
@antelopejs/interface-api-utilAPI Util

The interface pages document controllers, routes, parameter injection, prefix and postfix handlers, and WebSocket handlers.

Install

ajs project modules add @antelopejs/api

The command writes the module's default configuration, which is one HTTP server on localhost:8080. In a project whose modules import @antelopejs/interface-api, ajs project modules install also adds this module when nothing implements the interface yet.

Configuration

import { defineConfig } from "@antelopejs/interface-core/config";

export default defineConfig({
  name: "my-project",
  modules: {
    api: {
      source: {
        type: "package",
        package: "@antelopejs/api",
        version: "^1.3.4",
      },
      config: {
        servers: [
          { protocol: "http", host: "localhost", port: 8080 },
          { protocol: "https", port: 8443, cert: "<pem>", key: "<pem>" },
        ],
        publicBaseUrl: "https://api.example.com",
        strictPort: false,
        autoListen: true,
        cors: {
          allowedOrigins: ["https://app.example.com", /\.example\.com$/],
          allowedMethods: ["GET", "POST", "PUT", "DELETE"],
          allowedHeaders: ["content-type", "authorization"],
          credentials: true,
          maxAge: 600,
        },
      },
    },
  },
});
KeyDefaultDescription
serversone HTTP server on port 80Servers to start. Each entry takes protocol (http or https), an optional host and port, and any Node.js http.ServerOptions or https.ServerOptions.
publicBaseUrlAPI_LOCAL_BASE_URL in devOrigin that browsers use to reach the API. Outside development it is required, and the boot fails without it.
strictPortfalseIn development, fail instead of falling back to the next free port. Outside development, the module always binds the exact port.
autoListentrueSet to false to create the servers and serve them only when a module calls Listen().
corsnoneCORS policy. allowedMethods defaults to GET,HEAD,PUT,PATCH,POST,DELETE, and credentials defaults to true. Change it at run time with SetCorsConfig().

In development, loopback origins (localhost, 127.0.0.1, [::1], any port) pass CORS without configuration. List every other origin in allowedOrigins.

Config variables

The module publishes three config variables. Each one comes from the first entry of servers. Other modules read them with ${@api.<NAME>}, where api is the module's key in modules.

VariableValue
API_PORTThe port that the first server bound.
API_LOCAL_BASE_URLThe same-host origin, for example http://127.0.0.1:8080.
API_PUBLIC_BASE_URLpublicBaseUrl, without trailing slashes.
modules: {
  "file-storage-local": {
    config: { baseUrl: "${@api.API_PUBLIC_BASE_URL}" },
  },
},

The module binds its listening sockets before any other module constructs, so API_PORT is always the port the server serves.

The package names the function that computes them, configVars in dist/config-vars.js, in antelopeJs.configVarsFile. A tool that reads the project without running it, such as ajs dms build, resolves ${@api.<NAME>} with that function. In that case API_PORT is the configured port, and API_PUBLIC_BASE_URL falls back to API_LOCAL_BASE_URL when publicBaseUrl is not set.

Startup gate

The module serves the port from construct on. Until the core announces that every module has started, it answers every request with 503, the body error.server_starting, and Retry-After: 1. @antelopejs/core sends that announcement as Events.ModulesStarted of @antelopejs/interface-core/modules. When the whole project starts to stop at shutdown (Events.ModulesStopping), the module answers 503 again. Stopping, reloading, or removing a single module at run time leaves the gate open.

A module that fails to start at launch leaves the project answering 503. So does a start() that has not settled after 120 seconds, which makes the start fail. Nothing runs for a request the gate holds, so a client can retry it. The module answers CORS preflights while the gate is closed, so a browser can read the 503.

GET /_live always answers 200. Use it as the liveness probe, and use a normal route as the readiness probe.

Quick start

In the module that serves the routes, add the interface.

pnpm add @antelopejs/interface-api

Declare a controller in the module entry. Its decorators register the routes when the module loads.

import { Controller, Get, Parameter } from "@antelopejs/interface-api";

export class HelloController extends Controller("/greetings") {
  @Get()
  hello(@Parameter("name", "query") name = "world") {
    return { message: `Hello ${name}` };
  }
}

export function construct(): void {}
export function destroy(): void {}
export function start(): void {}
export function stop(): void {}

@Get() without a location uses the method name, so the route is /greetings/hello. Run the project with ajs project dev, then call GET http://localhost:8080/greetings/hello?name=Ada.

Documentation

  • API documents the @antelopejs/interface-api contract that this module implements.
  • API Util documents the @antelopejs/interface-api-util contract that this module implements.
  • Configuration documents module sources, config blocks, and config variables in antelope.config.ts.

Compatibility

PackageSupportedTested with
@antelopejs/core>=1.17.0 <21.20.0
@antelopejs/interface-api>=0.2.0 <0.4.00.2.0
@antelopejs/interface-api-util>=0.3.0 <1.0.00.3.0
@antelopejs/interface-core>=0.4.0 <1.0.00.4.0

The generated table lists @antelopejs/core, which this module requires as a peer dependency, and the interface packages it loads, with their ranges read from package.json. If another loaded module declares a range that excludes the copy this module loads, @antelopejs/core refuses to start the project. It reports Incompatible interface package resolution and names the module, its range, and the copy it found.

Contributing

pnpm install
pnpm dev            # run the playground/ project in watch mode
pnpm lint
pnpm format:check
pnpm knip
pnpm test           # build, then ajs module test

Read the contribution guidelines before opening a pull request.

License

Apache-2.0

版本与发布记录

@antelopejs/api 共发布 28 个版本,首次发布于 2025/05/09;过去 12 个月发布了 26 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
1.7.0latest2026/11/051 天前
2.0.02026/11/051 天前
1.6.02026/10/297 天前
1.5.02026/10/2413 天前
1.4.02026/10/2314 天前
1.3.52026/10/1720 天前

其他发布通道:next → 1.3.0-next.2。

关于 @antelopejs/api 的常见问题

@antelopejs/api 是什么?

HTTP and WebSocket server module for AntelopeJS, implementing the API interfaces with the Node.js http, https and ws servers

如何安装 @antelopejs/api?

在项目目录运行 npm install @antelopejs/api,或使用 pnpm add / yarn add / bun add。

@antelopejs/api 支持 TypeScript 吗?

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

@antelopejs/api 支持 ESM 还是 CommonJS?

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

@antelopejs/api 可以商用吗?

@antelopejs/api 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@antelopejs/api 的下载量有多少?

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

@antelopejs/api 还在维护吗?

最新版本 1.7.0 发布于 2026/11/05(1 天前);至今共发布 28 个版本,其中过去 12 个月发布了 26 个;首次发布于 2025/05/09。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。