安全的關(guān)鍵.png)
ASP.NET Web API快速入門(mén)介紹
cd ./todo-app
git init
git add .; git commit -m "Initial commit";
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;
請(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;
每個(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;
};
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ù)端!
我們只是淺淺涉獵了 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)@若川視野
ASP.NET Web API快速入門(mén)介紹
2024年在線市場(chǎng)平臺(tái)的11大最佳支付解決方案
完整指南:如何在應(yīng)用程序中集成和使用ChatGPT API
選擇AI API的指南:ChatGPT、Gemini或Claude,哪一個(gè)最適合你?
用ASP.NET Core 2.1 建立規(guī)范的 REST API — 緩存和并發(fā)
企業(yè)工商數(shù)據(jù)API用哪種?
2024年創(chuàng)建社交媒體帖子的最佳圖像工具API
2024年小型企業(yè)的7個(gè)最佳短信應(yīng)用API
用gin寫(xiě)簡(jiǎn)單的crud后端API接口
對(duì)比大模型API的內(nèi)容創(chuàng)意新穎性、情感共鳴力、商業(yè)轉(zhuǎn)化潛力
一鍵對(duì)比試用API 限時(shí)免費(fèi)