📜  lamda multiples api calss - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:04:15.053000             🧑  作者: Mango

代码示例1
async function generateToken(req) {
    // try {
    //     throw new Error("custom erorr created");
    //     const { channelName, uID, isPublisher } = req;
    //     const role = isPublisher ? Agora.RtcRole.PUBLISHER : Agora.RtcRole.SUBSCRIBER;
    //     const currentTimestamp = Math.floor(Date.now() / 1000);
    //     const expirationTimestamp = currentTimestamp + config.TOKEN_EXPIRY;
    //     const token = await Agora.RtcTokenBuilder.buildTokenWithUid(config.APP_ID, config.APP_CERT, channelName, uID, role, expirationTimestamp);
    //     const data = { token };
    //     return data
    // } catch (err) {
    //     console.log("error in generate-token catch: ");
    //     console.log(err);

    //      const payload = {
    //         to: "amir.farooq@jjtestsite.us",
    //         from:"amir.farooq@jjtestsite.us",
    //         subject:"Custom Agora API Erorr",
    //         text: JSON.stringify(req)
    //     };
    //     email.sendEmail(payload)
    //     return helper.getStandardResponse(500, false, "Token error", req);
    // }

    // const promise = new Promise(function(resolve, reject) {
    //     console.log("req : ", req)
    //     throw new Error("returning custom error")
    //     const { channelName, uID, isPublisher } = req;
    //     const role = isPublisher ? Agora.RtcRole.PUBLISHER : Agora.RtcRole.SUBSCRIBER;
    //     const currentTimestamp = Math.floor(Date.now() / 1000);
    //     const expirationTimestamp = currentTimestamp + config.TOKEN_EXPIRY;
    //     const token = Agora.RtcTokenBuilder.buildTokenWithUid(config.APP_ID, config.APP_CERT, channelName, uID, role, expirationTimestamp);
    //     const data = {
    //         token
    //     };
    //     return data;
    // });

    // promise.then((data) => {
    //     console.log(data);
    //     return data;
    //     // helper.getStandardResponse(200, true, "Token Success", data);
    // }).catch(async(error) => {
    //     console.log('email call');
    //     console.log('email call erorr', error);
    //     try {
    //         let payload = {
    //             to: "amir.farooq@jjtestsite.us",
    //             from: "amir.farooq@jjtestsite.us",
    //             subject: "custom Agora API Erorr",
    //             text: JSON.stringify(req)
    //         };
    //         let resp = await sendEmail(payload);
    //         console.log('send email resp:', resp);
    //     }
    //     catch (e) {
    //         console.log("Nested Try catch erorr", e);
    //     }
    //     //return error;
    // });
    
    
    try {
        throw new Error("returning custom error");
        const { channelName, uID, isPublisher } = req;
        const role = isPublisher ? Agora.RtcRole.PUBLISHER : Agora.RtcRole.SUBSCRIBER;
        const currentTimestamp = Math.floor(Date.now() / 1000);
        const expirationTimestamp = currentTimestamp + config.TOKEN_EXPIRY;
        const token = await Agora.RtcTokenBuilder.buildTokenWithUid(config.APP_ID, config.APP_CERT, channelName, uID, role, expirationTimestamp);
        const data = {
            token
        };
        return data;
    } catch (e) {
        try {
             let payload = {
                 to: "amir.farooq@jjtestsite.us",
                 from: "amir.farooq@jjtestsite.us",
                 subject: "custom Agora API Erorr",
                 text: JSON.stringify(req)
             };
             let resp = await sendEmail(payload);
             console.log('send email resp:', resp);
         }
         catch (e) {
            console.log("Nested Try catch erorr", e);
         }
    }
}




async function sendEmail(payload) {
    try {
        sgMail.setApiKey(config.SENDGRID_API_KEY)
        const msg = {
            to: payload.to, //  Change to your recipient
            from: payload.from, //  Change to your verified sender
            subject: payload.subject, // 'Sending with SendGrid is Fun',
            text: payload.text // 'and easy to do anywhere, even with Node.js'
        }

        const data = await sgMail.send(msg);
        console.log("send grid response data : ", data)
        return data;
    }
    catch (e) {
        console.log("2nd nested try catch: ", e)
    }
};