itty-router-help
v0.3.2
Published
Creates help middleware for itty-router.
Downloads
7
Maintainers
Readme
Experimental library to make browsable, documented REST APIs extremely easy (requires itty-router). This is currently in pre-alpha, so use with care - the API will certainly be evolving. While itty-router API code tends to be very simple/readable, I still found myself forgeting endpoints and having to open code to see what was available. This aims to solve that, by allowing APIs to be explored in-place, live in your browser.
Features
- easily add documentation to specific routes or indexes
- auto-documents method, route, params, demo links (on GET routes without params)
- add anything you like into the help notes (format agnostic)
How to Use
- create middleware via
createHelp(router)
function - use
withHelp
middleware on individual routes - use
withHelpIndex
middleware on upstream index route to summarize - add
?help
to any documented route or index to see live documentation
Installation
npm install itty-router@next itty-router-help
Example
import {
Router,
withParams,
} from 'itty-router'
import { withHelp, withHelpIndex } from 'itty-router-help'
// create a new Router
const router = Router({ base: '/v1' })
router
// add some global middleware
.all('*', withParams)
// embed the help index upstream
.get('/', withHelpIndex(router))
.get('/simple/endpoint',
withHelp(),
() => 'Default help example'
}
// It can be this easy... this will document the method, route, and each param
.get('/foo/bar/:baz/:extra?',
withHelp(),
() => 'Foo Bar Baz!'
)
// This route help will only be accessible directly, not visible in the index.
.get('/secret/route',
withHelp({ indexed: false })
() => ''
)
// Add any payload you like... this will be merged with the automatic output.
.post('/add/anything',
withHelp({
'description': 'Adding a description to your help routes is a nice touch.',
'query_params': {
'awesome': {
'description': 'Make things awesome!',
'required': false,
}
}
})
({ query }) => query.awesome ? 'AWESOME!' : 'normal'
)
// 404 for everything else
.all('*', () => error(404))
Browsing
// Example: /v1?help
{
"endpoints": {
"GET /simple/endpoint", {
"demo": "/v1/simple/endpoint"
},
"GET /foo/bar/:baz/:extra?": {
"params": {
"baz": {
"required": true
},
"extra": {
"required": false
}
}
},
"POST add/anything": {
"description": "Adding a description to your help routes is a nice touch.",
"query_params": {
"awesome": {
"description": "Make things awesome!",
"required": false
}
}
}
}
}
// Example /v1/secret/route?help
{
"GET /secret/route": {
"demo": "/v1/secret/route"
}
}
Join the Discussion!
Have a question? Suggestion? Complaint? Want to send me a gift basket?
Join us on Discord!
Testing and Contributing
- Fork repo
- Install dev dependencies via
yarn
- Start test runner/dev mode
yarn dev
- Add your code and tests if needed - do NOT remove/alter existing tests
- Verify that tests pass once minified
yarn verify
- Commit files
- Submit PR with a detailed description of what you're doing
- I'll add you to the credits! :)