-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathiFC.js
69 lines (58 loc) · 2.05 KB
/
iFC.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
const axios = require('axios');
const configStore = require('./configStore');
const defaultOptions = {
url: '',
headers: {},
};
/**
* Inter-Function-Communicator
* given a service name, it can trigger http events and return responses
* technically very similar to using axios to trigger the event
* the difference is that the path to the service is generated dynamically
*
* @param {service} String - the name of the service to call
* @param {options} Object - req params including:
* {url} String to call on the service
* {headers} Object to be passed as headers
* and other properties to be attached to the req (possible body)
* @param {req} Object - the req object passed the the function calling i_f_c
* used to pass through required prams including auth information to the remote service
*/
async function ifc(service, options = defaultOptions, req = {}) {
const {
X_GOOGLE_GCP_PROJECT,
X_GOOGLE_FUNCTION_REGION,
NODE_ENV,
FIREBASE_CONFIG,
FUNCTIONS_EMULATOR,
IS_FIREBASE_CLI,
} = process.env;
const firebaseConfig =
typeof FIREBASE_CONFIG === 'string' ? JSON.parse(FIREBASE_CONFIG) : {};
const { region } = configStore.config;
const isLocalhost =
NODE_ENV !== 'production' ||
FUNCTIONS_EMULATOR === 'true' ||
IS_FIREBASE_CLI === 'true';
const port = process.env.PORT || 5000;
const { headers: { host = `localhost:${port}`, ...reqHeaders } = {} } = req;
const projectId = isLocalhost
? firebaseConfig.projectId
: X_GOOGLE_GCP_PROJECT || firebaseConfig.projectId;
const cloudResourceLocation = isLocalhost
? firebaseConfig.cloudResourceLocation || region
: X_GOOGLE_FUNCTION_REGION || region;
const reqUrl = isLocalhost
? `http://${host}/${projectId}/${cloudResourceLocation}1`
: `https://${cloudResourceLocation}-${projectId}.cloudfunctions.net`;
const { url, headers = {}, ...otherOptions } = options;
return axios({
url: `${reqUrl}/${service}/${url}`,
headers: {
...reqHeaders,
...headers,
},
...otherOptions,
});
}
module.exports = ifc;