0.3.2 • Published 3 years ago

kisa-typegen v0.3.2

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

kisa-typgen

Generate api.ts for kisa

Get Started

  1. install tool
npm i -g kisa-typegen
  1. prepare openapi file
{
  @openapi({
    servers: [
      {
        url: "http://localhost:3000",
      }
    ],
    security: [
      { jwt: [] }
    ]
  })
  login: { @endpoint({summary:"user login",security:[]})
    route: 'POST /login',
    req: {
      body: {
        name: 'admin',
        pass: 'a123456'
      }
    },
    res: {
      200: {
        id: 1,
        name: 'user1',
        token: '<token>',
        expireAt: '<timestamp>'
      }
    }
  },
  createPost: { @endpoint({summary:"create post", "x-middlewares": ["ratelimit"]})
    route: 'POST /posts',
    req: {
      body: {
        title: 'How to write blog?',
        description: 'Lorem Ipsum is simply dummy text of the printing and typesetting industry', @optional
        content: `Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever`,
      }
    },
    res: {
      200: { @save("Post")
        id: 1,
        userId: 1,
        title: 'How to write blog?',
        description: 'Lorem Ipsum is simply dummy text of the printing and typesetting industry',
        status: 0,
        content: `Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever`,
        createdAt: "<datetime>",
        updateAt: "<datetime>",
      }
    },
  },
  publishPost: { @endpoint({summary:"publish post"})
    route: "PUT /posts/{}/publish",
    req: {
      params: {
        id: 1
      }
    },
    res: {
      200: {
        msg: "OK",
      }
    },
  },
  listMyPosts: { @endpoint({summary:"list my posts"})
    route: "GET /posts/my",
    req: {
      query: {
        pageSize: 0, @optional
        pageNum: 10, @optional
      }
    },
    res: {
      200: [
        { @use("Post")
        }
      ]
    }
  },
  listPosts: { @endpoint({summary:"list posts",security:[]})
    route: "GET /posts",
    req: {
      query: {
        pageSize: 0, @optional
        pageNum: 10, @optional
      }
    },
    res: {
      200: [
        { @use("Post")
        }
      ]
    }
  },
  deletePost: { @endpoint({summary:"delete post"})
    route: "DELETE /posts/{}",
    req: {
      params: {
        id: 1
      }
    },
    res: {
      200: {
        msg: "OK"
      }
    }
  }
}
  1. generate api.ts
kisa-typgen api.jsona api.ts

content of api.ts

/* Autogenerated file. Do not edit manually. */
/* tslint:disable */
/* eslint-disable */

import {
  Handler,
  Middleware,
  Operation,
  Handlers as KisaHandlers,
  Middlewares as KisaMiddlewares,
  SecurityHandlers as KisaSecurityHandlers,
} from "kisa";

export namespace ReqTypes {
  export interface Login {
    body: {
      name: string;
      pass: string;
    };
  }
  export interface ListPosts {
    query: {
      pageSize?: number;
      pageNum?: number;
    };
  }
  export interface CreatePost {
    body: {
      title: string;
      description?: string;
      content: string;
    };
  }
  export interface PublishPost {
    params: {
      id: number;
    };
  }
  export interface ListMyPosts {
    query: {
      pageSize?: number;
      pageNum?: number;
    };
  }
  export interface DeletePost {
    params: {
      id: number;
    };
  }
  export interface Post {
    id: number;
    userId: number;
    title: string;
    description: string;
    status: number;
    content: string;
    createdAt: string;
    updateAt: string;
  }
}

export interface Handlers<S> extends KisaHandlers<S> {
  login: Handler<S, ReqTypes.Login>;
  listPosts: Handler<S, ReqTypes.ListPosts>;
  createPost: Handler<S, ReqTypes.CreatePost>;
  publishPost: Handler<S, ReqTypes.PublishPost>;
  listMyPosts: Handler<S, ReqTypes.ListMyPosts>;
  deletePost: Handler<S, ReqTypes.DeletePost>;
}

export interface Middlewares<S> extends KisaMiddlewares<S> {
  ratelimit: Middleware<S>;
}

export interface SecurityHandlers<S> extends KisaSecurityHandlers<S> {
  jwt: (config: string[]) => Middleware<S>;
}

export const OPERATIONS: Operation[] = [....];