fdgt-apis/api

View on GitHub
src/structures/Route.js

Summary

Maintainability
A
0 mins
Test Coverage
A
92%
// Local imports
import router from 'structures/Router'





export default class {
    defaultOptions = {
        methods: ['get'],
    }

    constructor (options) {
        const allOptions = {
            ...this.defaultOptions,
            ...options,
        }
        this.options = allOptions

        const {
            handler,
            route,
        } = allOptions

        if (!route) {
            throw new Error('route is required')
        }

        if (!handler) {
            throw new Error('handler is required')
        }

        let methods = allOptions.methods

        if (!Array.isArray(methods)) {
            methods = [methods]
        }

        methods.forEach(method => router[method](route, handler))
    }
}