Firebase云功能移至另一个打字稿文件

时间:2018-07-08 16:55:38

标签: firebase coding-style google-cloud-functions

我有一个很大的index.ts文件,这显然是不希望的,并且很难维护。我有一些绝对可以像oauth一样移出的方法。我知道如何使用模块导出功能移动到其他.ts文件,但是

的最佳做法是什么
admin.initializeApp(functions.config().firebase)

我应该通过管理员作为参考还是其他方式? 我希望移动的代码如下所示

/**
 * Handles Oauth flow to add the OEC app to the slack team
 */
export const oauth = functions.https.onRequest(async (request, response) => {
    const options = {
        uri: "https://slack.com/api/oauth.access",
        method: "GET",
        json: true,
        qs: {
            code: request.query.code,
            client_id: functions.config().slack.id,
            client_secret: functions.config().slack.secret,
            redirect_uri: `https://us-central1-${process.env.GCLOUD_PROJECT}.cloudfunctions.net/oauth`
        }
    };

    const result = await rp(options);
    if (!result.ok) {
        console.error("The request was not ok: " + JSON.stringify(result));
        return response.header("Location", `https://${process.env.GCLOUD_PROJECT}.firebaseapp.com`).send(302);
    }

    await admin.database().ref("installations").child(result.team_id).set({
        team: result.team_id,
        teamName: result.team_name,
        user_id: result.user_id,
        webhook: {
            url: result.incoming_webhook.url,
            channel: result.incoming_webhook.channel_id
        }
    });

    await admin.database().ref("users").child(result.team_id + '_' + result.user_id).set({
        team: result.team_id,
        teamName: result.team_name,
        user_id: result.user_id,
        accessToken: result.access_token
    });

    return response.header("Location", `https://${process.env.GCLOUD_PROJECT}.firebaseapp.com`).send(302);    
});

0 个答案:

没有答案