Code Monkey home page Code Monkey logo

Comments (8)

dmitryrogozhny avatar dmitryrogozhny commented on August 11, 2024 5

Hi @scottie1984,

Just to let you know, I've added TypeScript type declarations for swagger-ui-express.
This does not require any changes into the project. Types can be added to a TS project with : npm install --save-dev @types/swagger-ui-express

JFYI, types are managed by Microsoft as the separate npm package. Type declarations for the swagger-ui-express are available here: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/swagger-ui-express/index.d.ts

from swagger-ui-express.

dmitryrogozhny avatar dmitryrogozhny commented on August 11, 2024 1

Hi Stephen,

I think you can leave the readme as it is and close the issue.

The TypeScript support is managed by a separate npm package. For example, the process of using swagger-ui-express in a TypeScript project would be like this:

  1. use npm install swagger-ui-express to add this package.
  2. use npm install --save-dev @types/swagger-ui-express to add typings to the project.

The process is familiar for developers that are using TypeScript, and doesn't require additional documentation.

Dmitry.

from swagger-ui-express.

scottie1984 avatar scottie1984 commented on August 11, 2024

hey - not something that I have considered. Happy to take a PR on adding support as I am not overly familiar with typescript.

from swagger-ui-express.

scottie1984 avatar scottie1984 commented on August 11, 2024

Hi @dmitryrogozhny

Is there anything I should add to the readme to help with this? I am not overly familiar with the typescript ecosystem.

I will go ahead and close if there is no action.

Thanks
Stephen

from swagger-ui-express.

vit100 avatar vit100 commented on August 11, 2024

Hi @dmitryrogozhny ,
Somehow I am struggling with this type definition.. can you pls provide sample typescript code?
Either I miss something or typescript definition doesn't match code.
For example:
node_modules\swagger-ui-express\index.js
line 109:
var serve = [swaggerInitFn, swaggerAssetMiddleware()];

but
node_modules@types\swagger-ui-express\index.d.ts
line 41:
serve(): RequestHandler[];

In my understanding serve is array according to JS source file, but TS mark it as function..

etc...

from swagger-ui-express.

dmitryrogozhny avatar dmitryrogozhny commented on August 11, 2024

Hi @vit100,

The definition looks fine to me.

The serve function got passed to an Express application, so it should be an array of request handlers (i.e. RequestHandler[]). The call to the Express application would look something like that:
app.use('/api-docs', swaggerUi.serve).

Here's how you can quickly setup the working sample:

  1. create a folder for a new project and navigate into it.
  2. run npm install express swagger-ui-express @types/swagger-ui-express to get express, swagger-ui-express and typings.
  3. create index.ts file and copy its content from this gist: https://gist.github.com/dmitryrogozhny/df7e6adb97f4e50e171b646a90b0ab01
  4. run tsc index.ts to compile. You should get the index.js file in the same folder as the result (note: depending on your environment you may get warning messages, but you should get the index.js file nonetheless).
  5. run node index.js to start a local server on port 3000. You should see a message "Example app listening on port 3000!" in your console.
  6. in your browser navigate to http://localhost:3000/api-docs-split/ or other path configured to see swagger info. You can check paths available in the index.ts code.

Hope that helps.

from swagger-ui-express.

vit100 avatar vit100 commented on August 11, 2024

Thank @dmitryrogozhny for your attention.

Source of my confusion is this. Look at the this image image

swaggerUiExpress.serve is function returning array of middleware functions. So my understanding I need to invoke that function in order to pass array of middleware functions to app.use().

in node_modules@types\swagger-ui-express\index.d.ts it is indeed defined as function
serve(): RequestHandler[]; returning array of middlewares.

On the contrary in Javascript code it is defined as pointer to array: var serve = [swaggerInitFn, swaggerAssetMiddleware()];, so I don't need to invoke it, just pass it as pointer.

I believe in order to avoid this confusion it make sense to define serve as property, like this
serve: RequestHandler[];. In this case intellisense shows it differently as property so no need to invoke it:

Make sense?

from swagger-ui-express.

dmitryrogozhny avatar dmitryrogozhny commented on August 11, 2024

Oh, now I see. Thank you for a clarification!

You're right - serve should be a property. I've approved your changes to the typings.

from swagger-ui-express.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.