cd ./todo-app
git init
git add .; git commit -m "Initial commit";
  1. 將 express-openapi 引入我們的程序:

npm i express-openapi -s

// ./app.js

...

app.listen(3030);

...

// OpenAPI routes
initialize({
app,
apiDoc: require("./api/api-doc"),
paths: "./api/paths",
});

module.exports = app;
  1. 添加 OpenAPI 基礎(chǔ)模型。

請(qǐng)注意,模型中定義了?Todo?的類(lèi)型,將在路由處理程序中引用。

// ./api/api-doc.js

const apiDoc = {
swagger: "2.0",
basePath: "/",
info: {
title: "Todo app API.",
version: "1.0.0",
},
definitions: {
Todo: {
type: "object",
properties: {
id: {
type: "number",
},
message: {
type: "string",
},
},
required: ["id", "message"],
},
},
paths: {},
};

module.exports = apiDoc;
  1. 添加路由處理程序。

每個(gè)處理程序都聲明它支持哪些操作(GET、POST …),對(duì)每個(gè)操作的回調(diào),以及該處理程序的?apiDoc?OpenAPI 模型。

// ./api/paths/todos/index.js
module.exports = function () {
let operations = {
GET,
POST,
PUT,
DELETE,
};

function GET(req, res, next) {
res.status(200).json([
{ id: 0, message: "First todo" },
{ id: 1, message: "Second todo" },
]);
}

function POST(req, res, next) {
console.log(About to create todo: ${JSON.stringify(req.body)}); res.status(201).send(); } function PUT(req, res, next) { console.log(About to update todo id: ${req.query.id}); res.status(200).send(); } function DELETE(req, res, next) { console.log(About to delete todo id: ${req.query.id}); res.status(200).send(); } GET.apiDoc = { summary: "Fetch todos.", operationId: "getTodos", responses: { 200: { description: "List of todos.", schema: { type: "array", items: { $ref: "#/definitions/Todo", }, }, }, }, }; POST.apiDoc = { summary: "Create todo.", operationId: "createTodo", consumes: ["application/json"], parameters: [ { in: "body", name: "todo", schema: { $ref: "#/definitions/Todo", }, }, ], responses: { 201: { description: "Created", }, }, }; PUT.apiDoc = { summary: "Update todo.", operationId: "updateTodo", parameters: [ { in: "query", name: "id", required: true, type: "string", }, { in: "body", name: "todo", schema: { $ref: "#/definitions/Todo", }, }, ], responses: { 200: { description: "Updated ok", }, }, }; DELETE.apiDoc = { summary: "Delete todo.", operationId: "deleteTodo", consumes: ["application/json"], parameters: [ { in: "query", name: "id", required: true, type: "string", }, ], responses: { 200: { description: "Delete", }, }, }; return operations; };

  1. 添加自動(dòng)生成的文檔,swagger-ui-express

npm i swagger-ui-express -s
// ./app.js

...

// OpenAPI UI
app.use(
"/api-documentation",
swaggerUi.serve,
swaggerUi.setup(null, {
swaggerOptions: {
url: "http://localhost:3030/api-docs",
},
})
);

module.exports = app;

這就是我們最終獲得的效果:

這個(gè) SwaggerUi 是自動(dòng)生成的,你可以在 http://localhost:3030/api-documentation 訪問(wèn)它。

?? 恭喜!

當(dāng)你進(jìn)行到文章的這里時(shí),你應(yīng)該創(chuàng)建好了一個(gè)完全可運(yùn)行的 Express 應(yīng)用程序,其與 OpenAPI 完全集成。

現(xiàn)在,通過(guò)使用在 http://localhost:3030/api-docs 中定義的模型,我們可以輕松生成測(cè)試、模擬服務(wù)器、類(lèi)型,甚至客戶(hù)端!

總結(jié)

我們只是淺淺涉獵了 OpenAPI 所能做到的事情。但是我希望這篇文章能夠讓你了解標(biāo)準(zhǔn) API 定義模式是如何在可見(jiàn)性、測(cè)試、文檔和整體置信度方面幫助構(gòu)建 REST API 的。

謝謝你看到最后,happy coding!


原文鏈接:https://www.freecodecamp.org/news/how-to-build-explicit-apis-with-openapi/

本文章轉(zhuǎn)載微信公眾號(hào)@若川視野

上一篇:

使用 Node 創(chuàng)建 RESTful API 服務(wù)

下一篇:

構(gòu)建現(xiàn)代RESTful API:C#中的關(guān)鍵標(biāo)準(zhǔn)和最佳實(shí)踐
#你可能也喜歡這些API文章!

我們有何不同?

API服務(wù)商零注冊(cè)

多API并行試用

數(shù)據(jù)驅(qū)動(dòng)選型,提升決策效率

查看全部API→
??

熱門(mén)場(chǎng)景實(shí)測(cè),選對(duì)API

#AI文本生成大模型API

對(duì)比大模型API的內(nèi)容創(chuàng)意新穎性、情感共鳴力、商業(yè)轉(zhuǎn)化潛力

25個(gè)渠道
一鍵對(duì)比試用API 限時(shí)免費(fèi)

#AI深度推理大模型API

對(duì)比大模型API的邏輯推理準(zhǔn)確性、分析深度、可視化建議合理性

10個(gè)渠道
一鍵對(duì)比試用API 限時(shí)免費(fèi)