-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtemplate.js
147 lines (125 loc) · 4.12 KB
/
template.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
const JSON = require('JSON');
const getRequestHeader = require('getRequestHeader');
const getAllEventData = require('getAllEventData');
const getTimestampMillis = require('getTimestampMillis');
const logToConsole = require('logToConsole');
const getContainerVersion = require('getContainerVersion');
const getType = require('getType');
const sendHttpRequest = require('sendHttpRequest');
const encodeUriComponent = require('encodeUriComponent');
const getGoogleAuth = require('getGoogleAuth');
const toBase64 = require('toBase64');
const isLoggingEnabled = determinateIsLoggingEnabled();
const traceId = isLoggingEnabled ? getRequestHeader('trace-id') : undefined;
let publishUrl = 'https://pubsub.googleapis.com/v1/projects/'+enc(data.project)+'/topics/'+enc(data.topic)+':publish';
let method = 'POST';
let input = data.addEventData ? getAllEventData() : {};
let attributes = {};
if (data.addTimestamp) input[data.timestampFieldName] = getTimestampMillis();
if (data.customDataList) {
data.customDataList.forEach((d) => {
if (data.skipNilValues) {
const dType = getType(d.value);
if (dType === 'undefined' || dType === 'null') return;
}
if (getType(d.name) === 'string' && d.name.indexOf('.') !== -1) {
const nameParts = d.name.split('.');
let obj = input;
for (let i = 0; i < nameParts.length - 1; i++) {
const part = nameParts[i];
if (!obj[part]) {
obj[part] = {};
}
obj = obj[part];
}
obj[nameParts[nameParts.length - 1]] = d.value;
} else {
input[d.name] = d.value;
}
});
}
if (data.attributesDataList) {
data.attributesDataList.forEach((d) => {
if (data.skipNilValues) {
const dType = getType(d.value);
if (dType === 'undefined' || dType === 'null') return;
} else {
attributes[d.name] = d.value;
}
});
}
// Only send `data` and `attributes` keys if their inputs are not empty objects
let message = {};
if (input && JSON.stringify(input) !== '{}') {
message.data = toBase64(JSON.stringify(input));
}
if (attributes && JSON.stringify(attributes) !== '{}') {
message.attributes = attributes;
}
input = { messages: [message] };
if (isLoggingEnabled) {
logToConsole(
JSON.stringify({
Name: 'PubSub',
Type: 'Request',
TraceId: traceId,
EventName: 'Publish',
RequestMethod: method,
RequestUrl: publishUrl,
RequestBody: input,
})
);
}
const auth = getGoogleAuth({
scopes: ['https://www.googleapis.com/auth/pubsub']
});
sendHttpRequest(publishUrl, {method: method, headers: { 'Content-Type': 'application/json' }, authorization: auth}, JSON.stringify(input))
.then(() => {
if (isLoggingEnabled) {
logToConsole(
JSON.stringify({
Name: 'PubSub',
Type: 'Response',
TraceId: traceId,
EventName: 'Publish',
ResponseStatusCode: 200,
ResponseHeaders: {},
ResponseBody: {},
})
);
}
data.gtmOnSuccess();
}, function () {
if (isLoggingEnabled) {
logToConsole(
JSON.stringify({
Name: 'PubSub',
Type: 'Response',
TraceId: traceId,
EventName: 'Publish',
ResponseStatusCode: 500,
ResponseHeaders: {},
ResponseBody: {},
})
);
}
data.gtmOnFailure();
});
function enc(data) {
data = data || '';
return encodeUriComponent(data);
}
function determinateIsLoggingEnabled() {
const containerVersion = getContainerVersion();
const isDebug = !!(containerVersion && (containerVersion.debugMode || containerVersion.previewMode));
if (!data.logType) {
return isDebug;
}
if (data.logType === 'no') {
return false;
}
if (data.logType === 'debug') {
return isDebug;
}
return data.logType === 'always';
}