Firebase Cloud Functions:如何将函数拆分为多个文件?

rslzwgfq  于 2023-11-21  发布在  其他
关注(0)|答案(1)|浏览(98)

Firebase云函数:TypeScript如何将函数拆分为多个文件?
我想在不同的文件中使用admin和searchClient。

Index.ts

import * as admin from "firebase-admin";
import * as functions from "firebase-functions";

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

const searchClient = algoliasearch(env.algolia.appid, env.algolia.apikey);

....

exports.query = require('./query');
exports.products = require('./products');

字符串

query.ts

import * as functions from 'firebase-functions';
import {searchClient} from "./index";

const requestIndex = searchClient.initIndex("allRequests");

产品.ts

import * as functions from 'firebase-functions';
import {admin} from "./index";

const firestoreDb = admin.firestore();

n6lpvg4x

n6lpvg4x1#

为了在项目的任何其他地方使用admin和searchClient,您首先需要从index.ts文件中导出它们:

index.ts

import * as admin from "firebase-admin";
import * as functions from "firebase-functions";

admin.initializeApp(functions.config().firebase);
export { admin }

const searchClient = algoliasearch(env.algolia.appid, env.algolia.apikey);
export { searchClient }

字符串
然后,您可以将它们导入到您的query.ts和products.ts文件中:

query.ts

import { searchClient } from "./index";

产品.ts

import { admin } from "./index";

相关问题