forked from snowplow/snowplow-javascript-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpayload.ts
More file actions
112 lines (100 loc) · 2.95 KB
/
Copy pathpayload.ts
File metadata and controls
112 lines (100 loc) · 2.95 KB
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
/*
* JavaScript tracker core for Snowplow: payload.js
*
* Copyright (c) 2014-2016 Snowplow Analytics Ltd. All rights reserved.
*
* This program is licensed to you under the Apache License Version 2.0,
* and you may not use this file except in compliance with the Apache License Version 2.0.
* You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the Apache License Version 2.0 is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
*/
import * as base64 from './base64';
/**
* Interface for mutable object encapsulating tracker payload
*/
export interface PayloadData {
add: (key: string, value?: string) => void,
addDict: (dict: Object) => void,
addJson: (keyIfEncoded: string, keyIfNotEncoded: string, json: Object) => void,
build: () => Object;
}
/**
* Bas64 encode data with URL and Filename Safe Alphabet (base64url)
*
* See: http://tools.ietf.org/html/rfc4648#page-7
*/
function base64urlencode(data: string): string {
if (!data) {
return data;
}
var enc = base64.base64encode(data);
return enc.replace(/=/g, '').replace(/\+/g, '-').replace(/\//g, '_');
}
/**
* Is property a non-empty JSON?
*/
export function isNonEmptyJson(property): boolean {
if (!isJson(property)) {
return false;
}
for (var key in property) {
if (property.hasOwnProperty(key)) {
return true;
}
}
return false;
}
/**
* Is property a JSON?
*/
export function isJson(property: Object): boolean {
return (typeof property !== 'undefined' && property !== null &&
(property.constructor === {}.constructor || property.constructor === [].constructor));
}
/**
* A helper to build a Snowplow request string from an
* an optional initial value plus a set of individual
* name-value pairs, provided using the add method.
*
* @param base64Encode boolean Whether or not JSONs should be
* Base64-URL-safe-encoded
*
* @return object The request string builder, with add, addRaw and build methods
*/
export function payloadBuilder(base64Encode: boolean): PayloadData {
var dict = {};
var add = function (key: string, value?: string): void {
if (value != null && value !== '') { // null also checks undefined
dict[key] = value;
}
};
var addDict = function (dict: Object) {
for (var key in dict) {
if (dict.hasOwnProperty(key)) {
add(key, dict[key]);
}
}
};
var addJson = function (keyIfEncoded: string, keyIfNotEncoded: string, json?: Object) {
if (isNonEmptyJson(json)) {
var str = JSON.stringify(json);
if (base64Encode) {
add(keyIfEncoded, base64urlencode(str));
} else {
add(keyIfNotEncoded, str);
}
}
};
return {
add: add,
addDict: addDict,
addJson: addJson,
build: function () {
return dict;
}
};
}