Skip to content

Instantly share code, notes, and snippets.

@israeleriston
Created November 10, 2017 21:10
Show Gist options
  • Save israeleriston/482db8861938400b8d70f8e0b3ba65ce to your computer and use it in GitHub Desktop.
Save israeleriston/482db8861938400b8d70f8e0b3ba65ce to your computer and use it in GitHub Desktop.
Create generic routes using fastify
const getName = (path) => (domain) => `${path}-${domain}`.replace(/[~!@#$%^&*()_|+\=?;:,.<>\{\}\[\]\\\/]/gi, '')
const version = 'v1.0.0'
const methods = ['post', 'get', 'delete', 'put']
const getMethod = (method) => methods.find((elem) => elem === method)
const create = (path) => (handler) => {
const method = getMethod('post')
const name = getName(path)('create')
return domain(method, path, name, version, handler)
}
const destroy = (path) => (handler) => {
const method = getMethod('delete')
const name = getName(path)('destroy')
return domain(method, path, name, version, handler)
}
const get = (path) => (handler) => {
const method = getMethod('get')
const name = getName(path)('search')
return domain(method, path, name, version, handler)
}
const update = (path) => (handler) => {
const method = getMethod('put')
const name = getName(path)('update')
return domain(method, path, name, version, handler)
}
const domain = (method, path, name, version, handler) => ({
name: name,
version: version,
path: path,
method: method,
handler: () => handler
})
module.exports = { create, destroy, update, get }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment