-
Notifications
You must be signed in to change notification settings - Fork 31
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
Labels
good first issue
Good for newcomers
Comments
I would gladly take a PR for this! |
My above comment still stands. If anyone wants to do that I would be very happy! |
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;
} |
Would you open this as a PR? |
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
Would be fine to add Typescript definitions to the library
The text was updated successfully, but these errors were encountered: