Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add typescript definition #25

Open
TheLox95 opened this issue Nov 11, 2021 · 5 comments
Open

Add typescript definition #25

TheLox95 opened this issue Nov 11, 2021 · 5 comments
Labels
good first issue Good for newcomers

Comments

@TheLox95
Copy link

Would be fine to add Typescript definitions to the library

@wesleytodd
Copy link
Owner

I would gladly take a PR for this!

@wesleytodd
Copy link
Owner

My above comment still stands. If anyone wants to do that I would be very happy!

@wesleytodd wesleytodd added the good first issue Good for newcomers label Aug 25, 2023
This was referenced Nov 2, 2023
@blipk
Copy link

blipk commented Aug 26, 2024

I used GPT to generate some types for this, not sure how correct they are, but its stopped enough complaints for me.

declare module "@wesleytodd/openapi" {
    import type { Request, Response, NextFunction } from "express"
    import type { OpenAPIV3 } from "openapi-types"

    type Middleware = ( req: Request, res: Response, next: NextFunction ) => void;

    interface OpenApiOptions {
      openapi?: string;
      info?: {title: string, description: string, version: string};
    }

    interface OpenApiMiddleware extends Middleware {
      routePrefix: string;
      document: OpenAPIV3.Document;
      generateDocument: ( options: {
        paths?: string | string[];
        doc?: Partial<OpenAPIV3.Document>;
        basePath?: string;
      } ) => OpenAPIV3.Document;
      options: OpenApiOptions;
      path: ( schema?: OpenAPIV3.SchemaObject ) => Middleware;
      validPath: ( schema?: OpenAPIV3.SchemaObject, pathOpts?: object ) => Middleware;
      component: ( type: ComponentType, name?: string, description?: object ) => ComponentReturnType;
      schema: ( name: string, description: OpenAPIV3.SchemaObject ) => OpenAPIV3.SchemaObject;
      response: ( name: string, description: OpenAPIV3.ResponseObject ) => OpenAPIV3.ResponseObject;
      parameters: ( name: string, description: OpenAPIV3.ParameterObject ) => OpenAPIV3.ParameterObject;
      examples: ( name: string, description: OpenAPIV3.ExampleObject ) => OpenAPIV3.ExampleObject;
      requestBodies: ( name: string, description: OpenAPIV3.RequestBodyObject ) => OpenAPIV3.RequestBodyObject;
      headers: ( name: string, description: OpenAPIV3.HeaderObject ) => OpenAPIV3.HeaderObject;
      securitySchemes: ( name: string, description: OpenAPIV3.SecuritySchemeObject ) => OpenAPIV3.SecuritySchemeObject;
      links: ( name: string, description: OpenAPIV3.LinkObject ) => OpenAPIV3.LinkObject;
      callbacks: ( name: string, description: OpenAPIV3.CallbackObject ) => OpenAPIV3.CallbackObject;
      swaggerui: ( options?: OpenApiOptions ) => Middleware[];
    }

    type ComponentType =
      | "schemas"
      | "responses"
      | "parameters"
      | "examples"
      | "requestBodies"
      | "headers"
      | "securitySchemes"
      | "links"
      | "callbacks";

    type ComponentReturnType =
      | OpenAPIV3.ReferenceObject
      | OpenAPIV3.SchemaObject
      | OpenAPIV3.ResponseObject
      | OpenAPIV3.ParameterObject
      | OpenAPIV3.ExampleObject
      | OpenAPIV3.RequestBodyObject
      | OpenAPIV3.HeaderObject
      | OpenAPIV3.SecuritySchemeObject
      | OpenAPIV3.LinkObject
      | OpenAPIV3.CallbackObject;

    function ExpressOpenApi( routePrefix?: string, doc?: OpenAPIV3.Document, opts?: OpenApiOptions ): OpenApiMiddleware;
    function ExpressOpenApi( doc?: OpenAPIV3.Document, opts?: OpenApiOptions ): OpenApiMiddleware;
    function ExpressOpenApi( opts?: OpenApiOptions ): OpenApiMiddleware;


    namespace ExpressOpenApi {
      const minimumViableDocument: OpenAPIV3.Document
      const defaultRoutePrefix: string
    }

    export = ExpressOpenApi;
}

@wesleytodd
Copy link
Owner

Would you open this as a PR?

@blipk
Copy link

blipk commented Aug 27, 2024

@wesleytodd

No, I'm not actually sure how correct it is or how to bundle it, and I decided to just use TSOA instead.

erikjuhani added a commit to erikjuhani/express-openapi that referenced this issue Sep 6, 2024
This commit includes typing all the public facing signatures. The types
also include API documentation in JSDoc format that is available in the
root README of the repository. Most of the types were deduced by hand
from the source code and tested with actual runtime data to make sure
that the written types match with the runtime data.

The component methods use function overloading to provide better code
completion of the expected parameters and return values. For example
giving a type and a name to `component("examples", "FooExample")`
returns the expected type of `ReferenceObject` conforming to the API
documentation.

Similarly setting the type for the component function affects what it
will return or take as an input. The input parameter and return type
will conform to the expected type. For example:

```ts
const schemas = openapi.component("schemas");
//    ^? { [key: string]: SchemaObject | ReferenceObject }

const examples = openapi.component("examples");
//    ^? { [key: string]: ExampleObject | ReferenceObject }
```

The types utilize existing types defined in "openapi-types" and most of
the public facing API functions rely on these types.

The initial typings file was derived from the work presented here: wesleytodd#25 (comment)
erikjuhani added a commit to erikjuhani/express-openapi that referenced this issue Sep 17, 2024
This commit includes typing all the public facing signatures. The types
also include API documentation in JSDoc format that is available in the
root README of the repository. Most of the types were deduced by hand
from the source code and tested with actual runtime data to make sure
that the written types match with the runtime data.

The component methods use function overloading to provide better code
completion of the expected parameters and return values. For example
giving a type and a name to `component("examples", "FooExample")`
returns the expected type of `ReferenceObject` conforming to the API
documentation.

Similarly setting the type for the component function affects what it
will return or take as an input. The input parameter and return type
will conform to the expected type. For example:

```ts
const schemas = openapi.component("schemas");
//    ^? { [key: string]: SchemaObject | ReferenceObject }

const examples = openapi.component("examples");
//    ^? { [key: string]: ExampleObject | ReferenceObject }
```

The types utilize existing types defined in "openapi-types" and most of
the public facing API functions rely on these types.

The initial typings file was derived from the work presented here: wesleytodd#25 (comment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

3 participants