使用打字稿的Firebase函数组织

时间:2019-12-10 07:03:26

标签: typescript firebase

我正在将typescript用于我的firebase函数,并且卡在代码组织上。我现在不需要移动触发器函数,也可以将它们保留在index.ts文件中。我需要的只是将其他实用程序功能从index.ts移开的帮助。

所以当前index.ts是

import * as functions from 'firebase-functions';
import * as rp from "request-promise-native";
import * as admin from 'firebase-admin';


admin.initializeApp(functions.config().firebase)

async function func1(){
  return 'blaw'
}

async function func2(){
  return 'blaw2'
}

module.exports = func1
module.exports = func2

index.ts 从'./comoncode'导入{func1,func2}

export const on_plan_upgrade_request = functions.database.ref("plan_upgrades/{id}").onWrite(async (change, context) => {
    console.log("start of on_feedback_received")

    const rsp = await func1()
    const rsp2 = await func2()

    const command = change.after.val();
    const data = {
        from: from,
        subject: "Plan upgrade request received",
        html: "Hi " + command.name + "<br/>Thank you for your for your upgrade request.<br/><br/>We will get back to you shortly." +
        "<br/><br/>Moblize.IT LLC<br/>http://moblize.it",
        templateId: 'f9f96c8d-7515-40fb-ba34-787f0d34693e',
        to: command.email,
        cc: cc
    }

    sendEmail(data)

    return ""
});

从上面的代码中,我想将func1()和func2()放在另一个文件中,例如Utility_code.ts

utility_code.ts会是什么样子,以及如何在index.ts中引用它们。

更新:现在我得到错误commoncode.ts不是模块

1 个答案:

答案 0 :(得分:1)

如果要在<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/colorBlue" android:orientation="vertical"> <androidx.recyclerview.widget.RecyclerView android:id="@+id/recyclerView" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <TextView android:id="@+id/userNames" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:background="@color/colorPrimary" android:ellipsize="end" android:gravity="start|center_vertical" android:maxLines="1" android:padding="10dp" android:scrollbars="horizontal" android:scrollHorizontally="true" android:text="Hello Nilu Pilu" android:textColor="@color/colorGreen" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" /> <com.google.android.material.floatingactionbutton.FloatingActionButton android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginBottom="10dp" android:contentDescription="@null" android:src="@drawable/ic_log_out" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout> 中同时调用func1()func2(),则需要导出它们:

utility_code.ts

然后在export async function func1(){ return 'blaw' } export async function func2(){ return 'blaw2' } 中,可以将其导入:

utility_code.ts