{"size": 1509, "ext": "js", "max_stars_repo_path": "test/helpers/authresponse.js", "max_stars_repo_name": "Severino/sikka-buya", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "test/helpers/authresponse.js", "max_issues_repo_name": "Severino/sikka-buya", "max_issues_repo_issues_event_min_datetime": "2021-05-21T06:28:45.000Z", "max_issues_repo_issues_event_max_datetime": "2022-02-03T10:36:02.000Z", "max_forks_repo_path": "test/helpers/authresponse.js", "max_forks_repo_name": "Severino/sikka-buya", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 11, "max_forks_count": null, "avg_line_length": 29.0192307692, "max_line_length": 113, "alphanum_fraction": 0.5453943009}
class AuthResponse {

    static isAuthResponse(obj) {

        let keys = ["success",
            "message",
            "token",
            "user"]

        for (let key of Object.keys(obj)) {
            let idx = keys.indexOf(key)
            if (idx != -1)
                key.splice(idx, 1)
        }

        if (key.length > 0) throw new Error(`Invalid auth response these keys were missing: ${keys.join(", ")}.`)

        return keys.length == 0
    }

    static isInvalidResponse(obj, {
        message = null,
        user = null
    } = {}) {
        let options = Object.assign(arguments[1], { success: false, tokenValidator: (token) => token == null })
        return this._isResponse(obj, options)
    }

    static isValidResponse(obj, {
        message = null,
        user = null
    } = {}) {
        let options = Object.assign(arguments[1], { success: true, tokenValidator: (token) => token != null })
        return this._isResponse(obj, options)
    }

    static _isResponse(obj, {
        message = null,
        user = null,
        success = true,
        tokenValidator = function () { return true }
    } = {}) {
        if (!this.isAuthResponse) return false

        let messageValid = (message == null) ? true : (message == obj.message) ? true : false
        let userValid = (user == null) ? true : (user == message.user) ? false : true

        return (messageValid && userValid && tokenValidator(obj.token) && obj.success == success)
    }
}

module.exports = AuthResponse