0.0.3 • Published 1 year ago

@hoseung.json/schema-openapi v0.0.3

Weekly downloads
-
License
MIT
Repository
-
Last release
1 year ago

Install

$ npm install @hoseung.json/schema-openapi

Example

import { HTTP, OpenAPI, Param, S } from "@hoseung.json/schema-openapi";

const Locale = S.Union([S.Literal("ko"), S.Literal("en")]);
const Post = S.Object({
  id: S.Number(),
  title: S.String(),
  description: S.Optional(S.String()),
  content: S.String(),
});
const PostList = S.Object({
  items: S.Array(Post),
  paging: S.Object({
    before: S.Number(),
    after: S.Number(),
  }),
});
const Schemas = { Locale, Post, PostList };

OpenAPI({
  info: { title: "Blog API", version: "1.0.0" },
  router: HTTP.Router(
    "/:locale",
    {
      locale: Param.Path(Schemas.Locale),
    },
    [
      HTTP.Router("/posts", {}, [
        HTTP.GET(
          "/",
          "getPosts",
          {
            page: Param.Query(S.Optional(S.Number())),
            size: Param.Query(S.Number()),
          },
          Schemas.PostList
        ),

        HTTP.GET(
          "/:postId",
          "getPostById",
          {
            postId: Param.Path(S.Number()),
          },
          Schemas.Post
        ),

        HTTP.POST(
          "/",
          "createPost",
          {
            title: Param.Body(S.String()),
            description: Param.Body(S.Optional(S.String())),
            content: Param.Body(S.String()),
          },
          S.Object({
            success: S.Boolean(),
          })
        ),
      ]),
    ]
  ),
  schemas: Schemas,
});

// {
//   openapi: "3.0.1",
//   info: { title: "Blog API", version: "1.0.0" },
//   paths: {
//     "/{locale}/posts": {
//       get: {
//         operationId: "getPosts",
//         parameters: [
//           {
//             in: "path",
//             name: "locale",
//             required: true,
//             schema: {
//               $ref: "#/components/schemas/Locale",
//             },
//           },
//           {
//             in: "query",
//             name: "page",
//             required: false,
//             schema: {
//               type: "number",
//             },
//           },
//           {
//             in: "query",
//             name: "size",
//             required: true,
//             schema: {
//               type: "number",
//             },
//           },
//         ],
//         responses: {
//           "200": {
//             content: {
//               "application/json": {
//                 schema: {
//                   $ref: "#/components/schemas/PostList",
//                 },
//               },
//             },
//           },
//         },
//       },
//       post: {
//         operationId: "createPost",
//         parameters: [
//           {
//             in: "path",
//             name: "locale",
//             required: true,
//             schema: {
//               $ref: "#/components/schemas/Locale",
//             },
//           },
//         ],
//         requestBody: {
//           content: {
//             "application/json": {
//               schema: {
//                 type: "object",
//                 properties: {
//                   title: {
//                     type: "string",
//                   },
//                   description: {
//                     type: "string",
//                   },
//                   content: {
//                     type: "string",
//                   },
//                 },
//                 required: ["title", "content"],
//               },
//             },
//           },
//         },
//         responses: {
//           "200": {
//             content: {
//               "application/json": {
//                 schema: {
//                   type: "object",
//                   properties: {
//                     success: {
//                       type: "boolean",
//                     },
//                   },
//                   required: ["success"],
//                 },
//               },
//             },
//           },
//         },
//       },
//     },
//     "/{locale}/posts/{postId}": {
//       get: {
//         operationId: "getPostById",
//         parameters: [
//           {
//             in: "path",
//             name: "locale",
//             required: true,
//             schema: {
//               $ref: "#/components/schemas/Locale",
//             },
//           },
//           {
//             in: "path",
//             name: "postId",
//             required: true,
//             schema: {
//               type: "number",
//             },
//           },
//         ],
//         responses: {
//           "200": {
//             content: {
//               "application/json": {
//                 schema: {
//                   $ref: "#/components/schemas/Post",
//                 },
//               },
//             },
//           },
//         },
//       },
//     },
//   },
//   components: {
//     schemas: {
//       Locale: {
//         anyOf: [
//           {
//             type: "string",
//             const: "ko"
//           },
//           {
//             type: "string",
//             const: "en"
//           },
//         ]
//       },
//       Post: {
//         type: "object",
//         properties: {
//           id: {
//             type: "number",
//           },
//           title: {
//             type: "string",
//           },
//           description: {
//             type: "string",
//           },
//           content: {
//             type: "string",
//           },
//         },
//         required: ["id", "title", "content"],
//       },
//       PostList: {
//         type: "object",
//         properties: {
//           items: {
//             type: "array",
//             items: {
//               type: "object",
//               properties: {
//                 id: {
//                   type: "number",
//                 },
//                 title: {
//                   type: "string",
//                 },
//                 description: {
//                   type: "string",
//                 },
//                 content: {
//                   type: "string",
//                 },
//               },
//               required: ["id", "title", "content"],
//             },
//           },
//           paging: {
//             type: "object",
//             properties: {
//               before: {
//                 type: "number",
//               },
//               after: {
//                 type: "number",
//               },
//             },
//             required: ["before", "after"],
//           },
//         },
//         required: ["items", "paging"],
//       },
//     },
//   },
//   "x-created-by": "@hoseung.json/schema-openapi",
// }
0.0.3

1 year ago

0.0.2

1 year ago

0.0.1

1 year ago

0.0.0

1 year ago