Comments (8)
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.
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:
- use
npm install swagger-ui-express
to add this package. - 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.
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.
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.
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.
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:
- create a folder for a new project and navigate into it.
- run
npm install express swagger-ui-express @types/swagger-ui-express
to get express, swagger-ui-express and typings. - create
index.ts
file and copy its content from this gist: https://gist.github.com/dmitryrogozhny/df7e6adb97f4e50e171b646a90b0ab01 - run
tsc index.ts
to compile. You should get theindex.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). - 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. - 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.
Thank @dmitryrogozhny for your attention.
Source of my confusion is this. Look at the this 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.
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)
- Apply bearer auth to global scope is not working. HOT 3
- Cannot load two different API specs HOT 6
- Changelog please HOT 2
- I don't understand where swagger.json gets generated... HOT 3
- Getting string instead of array when using multipart form data HOT 2
- feature: ability to serve the docs as a single-file HOT 1
- totally does not load on production HOT 3
- oneOf, anyOf, allOf, not
- refused to execute in browser HOT 3
- Array of strings sent through formdata are converted to array with single string when recieved on nodejs server
- My project is having problems because of assert HOT 3
- "Could not render OperationContainer, see the console."
- Not rendering with $refs HOT 2
- No operations defined in spec!
- Support for OpenAPI Specification 3.1.0 HOT 1
- Using swagger-ui for custom UI rather than swagger-ui-dist?
- [BUG] Changing host on the fly before load is not working
- Vulnerabilities in express
- Route params not filling successfully in nested api controller files using "mergeParams: true" HOT 1
- [@types/swagger-ui-express] About the impossibility of adding custom js
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from swagger-ui-express.