Moblize IT
Moblize IT

Reputation: 1328

firebase function organization using typescript

i am using typescript for my firebase functions and bit stuck on code organization. I do not need to move trigger functions at this point and ok to keep them in index.ts file. All i need is the help to put other utility functions away from index.ts.

so current index.ts is

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 import { func1, func2 } from './comoncode'

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 ""
});

from above code i want to take func1() and func2() in another file say utility_code.ts

what would the utility_code.ts will look like and how they will be referenced in index.ts.

UPDATE: now i get error commoncode.ts is not a module

Upvotes: 0

Views: 144

Answers (1)

Peter Haddad
Peter Haddad

Reputation: 80914

If you want to call both func1() and func2() in utility_code.ts, then you need to export them:

export async function func1(){
  return 'blaw'
}

export async function func2(){
  return 'blaw2'
}

Then in the utility_code.ts, you can import it:

import { func1, func2} from "./utility_code"; //add your path

Upvotes: 1

Related Questions