samarjit / koa-router-ajv-swaggergen

koa-router wrapper for adding schema for validation and generates swagger(openapi3) json
MIT License
6 stars 2 forks source link

Typing #1

Open jneuendorf-i4h opened 1 year ago

jneuendorf-i4h commented 1 year ago

Hi,

while working with your router, I came up with the following type declaration:


declare module "koa-router-ajv-swaggergen" {
    import KoaRouter from "@koa/router"
    import { JSONSchemaType } from "ajv"

    // type SchemaRouterMethod = "del" | "get" | "post" | "put"

    type Handler =
        | ((
              this: Router,
              path: string,
              schema: JSONSchemaType<any>,
              ...handlers: KoaRouter.Middleware[]
          ) => ReturnType<KoaRouter.Middleware>)
        // | ((
        //       this: Router,
        //       path: string,
        //       config: { schema?: object; pre?: KoaRouter.Middleware[] },
        //       ...handlers: KoaRouter.Middleware[]
        //   ) => ReturnType<KoaRouter.Middleware>)

    export default class Router {
        rootDoc: {
            openapi: string
            info: {
                title: string
                description: string
                version: string
            }
            tags: string[]
            servers: string[]
        }

        constructor(router: KoaRouter, prefix: string)

        routes: KoaRouter["routes"]
        use: KoaRouter["use"]
        prefix: KoaRouter["prefix"]
        param: KoaRouter["param"]
        allowedMethods: KoaRouter["allowedMethods"]

        get(
            path: string,
            schema: JSONSchemaType<any>,
            ...handlers: KoaRouter.Middleware[]
        ): ReturnType<KoaRouter.Middleware>
        // get: Handler
        post: Handler
        put: Handler
        del: Handler
    }
    export function setupJsonErrors(app: any): void
}

Maybe, this is useful for you. ;)

samarjit commented 10 months ago

I really appreciate your help. I hope to integrate this soon and perhaps add some documentation.