Skip to content

Proposal of evolution for the library #25

@guenoledc

Description

@guenoledc

Please accept this modest contribution to make your library support Promise format.

This is what I add after the lib import to be able to use it in async/await

const duo_api = require('@duosecurity/duo_api')

duo_api.Client.prototype.asyncApiCall = async function(method, path, params) {
    return new Promise( (resolve, reject)=>{
        try {
            this.jsonApiCall(method, path, params, resolve)
        } catch (error) {
            resolve({
                stat: 'ERROR',
                message: error.message ? error.message : error
            })
        }
    })
}

duo_api.Client.prototype.get = async function (path, params) {
    return this.asyncApiCall('GET', path, params)
}
duo_api.Client.prototype.post = async function (path, params) {
    return this.asyncApiCall('POST', path, params)
}

That can then be used nicely as follow

    const request = new duo_api.Client(integrationKey, secretKey, duoHost);

    const auth = await request.post('/auth/v2/auth', {
        user_id: user_id,
        factor: 'push',
        async: true,
        device: 'auto',
        type: 'Test'
    })
    console.log('AUTH:', auth)


    const status = await request.get('/auth/v2/auth_status', {txid: auth.response.txid})
    console.log('STATUS:', auth)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions