util方法的Firebase代码组织

时间:2018-07-08 07:25:20

标签: node.js typescript firebase google-cloud-functions

我的index.js如下所示

import * as functions from 'firebase-functions';
import * as rp from "request-promise-native";
import * as btoa from "btoa";
import * as admin from 'firebase-admin';
import * as SendGrid from '@sendgrid/mail';
admin.initializeApp(functions.config().firebase)

export const dialog_handler = functions.https.onRequest(async (request, response) => {
    console.log("in dialog_handler request payload is:" + request.body.payload)
    const reqParams = JSON.parse(request.body.payload)

    const callbackId = reqParams.callback_id
    console.log(":::::::::::::::Callback Id:" + callbackId)
    const submission = reqParams.submission
    console.log(submission )
    let payload = {}

    if(callbackId === 'send_feedback'){
        await admin.database().ref("feedback").push(submission);
        payload = { "attachments": [
            {
                "fallback": "Feedback submitted",
                "title": "Feedback submitted. We will get back to you as soon as possible."
            }]
       }

    }
}

上面的实际代码无关紧要。最主要的是有效负载下方,这对我来说是一种可重用的代码。我如何将其移出index.ts并仅引用它并使我的index.tx保持较小。

 payload = { "attachments": [
                {
                    "fallback": "Feedback submitted",
                    "title": "Feedback submitted. We will get back to you as soon as possible."
                }]
           }

1 个答案:

答案 0 :(得分:1)

这实际上与Cloud Functions无关,与TypeScript模块的语法和解析无关。

您可以有一个如下所示的相邻文件payload.ts

export const payload = { "attachments": [{
                "fallback": "Feedback submitted",
                "title": "Feedback submitted. We will get back to you as soon as possible."
            }]
       }

然后您可以从另一个文件中保留其值:

import { payload } from "./payload"

您可能想阅读module resolution in TypeScript