点
点 (单位:千美元)"真理的单一点")是一种简洁,开发者友好的方式来描述你的API合同.
使用 TypeScript 语法,它允许您描述您的 API 并生成您需要的其他 API 合同格式( OpenAPI, Swagger, JSON Schema) .
您不需要使用代码库中的 TypeScript 来获益于使用 Spot .
API 定义文件示例 api.ts 定义单个 POST 创建用户的结束点 :
import { api, endpoint, request, response, body } from "@airtasker/spot";
@api({
name: "My API"
})
class Api {}
@endpoint({
method: "POST",
path: "/users"
})
class CreateUser {
@request
request(@body body: CreateUserRequest) {}
@response({ status: 201 })
response(@body body: CreateUserResponse) {}
}
interface CreateUserRequest {
firstName: string;
lastName: string;
}
interface CreateUserResponse {
firstName: string;
lastName: string;
role: string;
}
开始
从写Spot合同开始 点点指南
关于所有可用的语法,参见 斑点语法
所需资源
- 节点.js >= 18.12.0
- pnpm( 建议) 或 npm
安装
与 小时 安装并初始化 @airtasker/spot 对您的项目:
pnpm add @airtasker/spot
只需运行即可将上面的定义传递给生成器 :
npx @airtasker/spot generate --contract api.ts
为什么我们造出斑点
乍一看,你可能会想知道 为什么我们打扰建筑点。 为什么不使用OpenAPI(形式上称为Swagger)来描述您的API?
在核心,我们建造了Spot,因为我们想要更好的开发者经验.
书面合同
OpenAPI文档作为YAML文件存储,遵循非常具体的计划. 您不会知道您使用错误的字段名称, 或忘记将类型定义包装到一个计划对象中, 除非您运行一个好的 OpenAPI linter 。 大多数不熟悉 OpenAPI 规格的开发者最终使用Swagger 编辑器或 Stoplight 等视觉编辑器.
由于 Spot 调用 TypeScript 语法,你只需要写出有效的 TypeScript 代码. 当您的代码无效时, 您的编辑会立即告诉你 。 它会告诉你缺少什么,你甚至可以免费自动完成。 我们本来可以选择任何其他类型的语言——TypeScript恰好是我们最简洁和无所不在的语言之一.
审查合同
我们认为API合同应该被检查到Git,或者任何你使用的代码版本系统. 此外,应系统地审查API合同。 后端工程师很难错误地假设客户工程师对终点的期望.
由于其复杂的嵌入式结构以及OpenAPI规格的丰富性,OpenAPI文档在拉出请求中可能难以审查. 他们擅长机器.
Spot旨在尽可能能被人类读取. 与我们先前的OpenAPI文件相比.
不同格式的互操作性
根据您想要达到的目标(测试,文档,客户端代码生成...),您会找到只和OpenAPI 2(Swagger)合作的工具,以及仅支持OpenAPI 3的更新的工具. 您也可以为不同的 API 生态系统找到工具, 如 JSON Schema 或 API 蓝图 .
我们建Spot就是考虑到这一点。 Spot不需将各种API格式转换器相拼,而是可以生成出每个主要的API文档格式. 这就是为什么我们称之为"真理的单一点".
使用量
要在当前目录中启动并设置 API 声明, 运行 :
npx @airtasker/spot init
然后,您可以用 :
npx @airtasker/spot generate --contract api.ts
在记忆使用中
import { Spot } from "@airtasker/spot";
const contract = Spot.parseContract("./api.ts")
const openApi = Spot.OpenApi3.generateOpenAPI3(contract);
console.log(openApi);
/*
{
openapi: '3.0.2',
info: { title: 'my-api', description: undefined, version: '0.0.0' },
paths: { '/users': { post: [Object] } },
components: {
schemas: { CreateUserRequest: [Object], CreateUserResponse: [Object] },
securitySchemes: undefined
},
security: undefined
}
*/
图标
spot checksum SPOT_CONTRACTspot docs SPOT_CONTRACTspot generatespot help [COMMAND]spot initspot lint SPOT_CONTRACTspot mock SPOT_CONTRACTspot validate SPOT_CONTRACTspot validation-server SPOT_CONTRACT
spot checksum SPOT_CONTRACT
为 Spot 合同生成支票和
USAGE
$ spot checksum SPOT_CONTRACT
ARGUMENTS
SPOT_CONTRACT path to Spot contract
OPTIONS
-h, --help show CLI help
EXAMPLE
$ spot checksum api.ts
参见代码 : 构建/cli/src/命令/校验和.js
spot docs SPOT_CONTRACT
预览 Spot 合同作为 OpenAPI3 文档 。 文件服务器将在http://localhost:8080上启动.
USAGE
$ spot docs SPOT_CONTRACT
ARGUMENTS
SPOT_CONTRACT path to Spot contract
OPTIONS
-h, --help show CLI help
-p, --port=port [default: 8080] Documentation server port
EXAMPLE
$ spot docs api.ts
参见代码 : 构建/cli/src/命令/docs.js
spot generate
运行 API 上的生成器 。 曾出产客户端库,服务器锅炉板和OpenAPI等知名API合同格式.
USAGE
$ spot generate
OPTIONS
-c, --contract=contract (required) Path to a TypeScript Contract definition
-g, --generator=generator Generator to run
-h, --help show CLI help
-l, --language=language Language to generate
-o, --out=out Directory in which to output generated files
EXAMPLE
$ spot generate --contract api.ts --language yaml --generator openapi3 --out output/
参见代码 : 构建/ cli/ src/ commands/ generate.js
spot help [COMMAND]
显示点名帮助
USAGE
$ spot help [COMMAND]
ARGUMENTS
COMMAND command to show help for
OPTIONS
--all see all commands in CLI
参见代码 : @oclif/plugin-帮助
spot init
生成锅炉板用于API.
USAGE
$ spot init
OPTIONS
-h, --help show CLI help
EXAMPLE
$ spot init
Generated the following files:
- api.ts
- tsconfig.json
- package.json
参见代码 : 构建/cli/src/命令/init.js
spot lint SPOT_CONTRACT
定点合同
USAGE
$ spot lint SPOT_CONTRACT
ARGUMENTS
SPOT_CONTRACT path to Spot contract
OPTIONS
-h, --help show CLI help
--has-discriminator=(error|warn|off) Setting for has-discriminator
--has-request-payload=(error|warn|off) Setting for has-request-payload
--has-response=(error|warn|off) Setting for has-response
--has-response-payload=(error|warn|off) Setting for has-response-payload
--no-inline-objects-within-unions=(error|warn|off) Setting for no-inline-objects-within-unions
--no-nullable-arrays=(error|warn|off) Setting for no-nullable-arrays
--no-nullable-fields-within-request-bodies=(error|warn|off) Setting for no-nullable-fields-within-request-bodies
--no-omittable-fields-within-response-bodies=(error|warn|off) Setting for no-omittable-fields-within-response-bodies
--no-trailing-forward-slash=(error|warn|off) Setting for no-trailing-forward-slash
EXAMPLES
$ spot lint api.ts
$ spot lint --has-descriminator=error
$ spot lint --no-nullable-arrays=off
参见代码 : 构建/cli/src/命令/lint.js
spot mock SPOT_CONTRACT
根据 Spot 合同运行一个模拟服务器
USAGE
$ spot mock SPOT_CONTRACT
ARGUMENTS
SPOT_CONTRACT path to Spot contract
OPTIONS
-h, --help show CLI help
-p, --port=port (required) [default: 3010] Port on which to run the mock server
--pathPrefix=pathPrefix Prefix to prepend to each endpoint path
--proxyBaseUrl=proxyBaseUrl If set, the server will act as a proxy and fetch data from the given
remote server instead of mocking it
--proxyFallbackBaseUrl=proxyFallbackBaseUrl Like proxyBaseUrl, except used when the requested API does not match
defined SPOT contract. If unset, 404 will always be returned.
--proxyMockBaseUrl=proxyMockBaseUrl Like proxyBaseUrl, except used to proxy draft endpoints instead of
returning mocked responses.
EXAMPLE
$ spot mock api.ts
参见代码 : 构建/cli/src/命令/mock.js
spot validate SPOT_CONTRACT
验证Spot合同
USAGE
$ spot validate SPOT_CONTRACT
ARGUMENTS
SPOT_CONTRACT path to Spot contract
OPTIONS
-h, --help show CLI help
EXAMPLE
$ spot validate api.ts
参见代码 : 构建/ cli/ src/ 命令/ 验证.js
spot validation-server SPOT_CONTRACT
启动现场合同验证服务器
USAGE
$ spot validation-server SPOT_CONTRACT
ARGUMENTS
SPOT_CONTRACT path to Spot contract
OPTIONS
-h, --help show CLI help
-p, --port=port [default: 5907] The port where application will be available
EXAMPLE
$ spot validation-server api.ts
参见代码 : 构建/cli/src/命令/验证-服务器.js