@coveo/relay 0.0.6 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/relay.cjs ADDED
@@ -0,0 +1,181 @@
1
+ 'use strict';
2
+
3
+ var crypto = require('crypto');
4
+
5
+ /**
6
+ * @todo LENS-1059: The clientId should be a value that is persisted over time on a device.
7
+ */
8
+ function getClientId(environment) {
9
+ return environment.generateUUID();
10
+ }
11
+ function createClientIdManager(environment) {
12
+ return {
13
+ clientId: getClientId(environment),
14
+ };
15
+ }
16
+
17
+ const rnds8Pool = new Uint8Array(256); // # of random values to pre-allocate
18
+
19
+ let poolPtr = rnds8Pool.length;
20
+ function rng() {
21
+ if (poolPtr > rnds8Pool.length - 16) {
22
+ crypto.randomFillSync(rnds8Pool);
23
+ poolPtr = 0;
24
+ }
25
+
26
+ return rnds8Pool.slice(poolPtr, poolPtr += 16);
27
+ }
28
+
29
+ /**
30
+ * Convert array of 16 byte values to UUID string format of the form:
31
+ * XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
32
+ */
33
+
34
+ const byteToHex = [];
35
+
36
+ for (let i = 0; i < 256; ++i) {
37
+ byteToHex.push((i + 0x100).toString(16).slice(1));
38
+ }
39
+
40
+ function unsafeStringify(arr, offset = 0) {
41
+ // Note: Be careful editing this code! It's been tuned for performance
42
+ // and works in ways you may not expect. See https://github.com/uuidjs/uuid/pull/434
43
+ return (byteToHex[arr[offset + 0]] + byteToHex[arr[offset + 1]] + byteToHex[arr[offset + 2]] + byteToHex[arr[offset + 3]] + '-' + byteToHex[arr[offset + 4]] + byteToHex[arr[offset + 5]] + '-' + byteToHex[arr[offset + 6]] + byteToHex[arr[offset + 7]] + '-' + byteToHex[arr[offset + 8]] + byteToHex[arr[offset + 9]] + '-' + byteToHex[arr[offset + 10]] + byteToHex[arr[offset + 11]] + byteToHex[arr[offset + 12]] + byteToHex[arr[offset + 13]] + byteToHex[arr[offset + 14]] + byteToHex[arr[offset + 15]]).toLowerCase();
44
+ }
45
+
46
+ var native = {
47
+ randomUUID: crypto.randomUUID
48
+ };
49
+
50
+ function v4(options, buf, offset) {
51
+ if (native.randomUUID && !buf && !options) {
52
+ return native.randomUUID();
53
+ }
54
+
55
+ options = options || {};
56
+ const rnds = options.random || (options.rng || rng)(); // Per 4.4, set bits for version and `clock_seq_hi_and_reserved`
57
+
58
+ rnds[6] = rnds[6] & 0x0f | 0x40;
59
+ rnds[8] = rnds[8] & 0x3f | 0x80; // Copy bytes to buffer, if provided
60
+
61
+ if (buf) {
62
+ offset = offset || 0;
63
+
64
+ for (let i = 0; i < 16; ++i) {
65
+ buf[offset + i] = rnds[i];
66
+ }
67
+
68
+ return buf;
69
+ }
70
+
71
+ return unsafeStringify(rnds);
72
+ }
73
+
74
+ function getReferrerUrl() {
75
+ const referrer = document.referrer;
76
+ return referrer === "" ? null : referrer;
77
+ }
78
+ function buildBrowserEnvironment() {
79
+ return {
80
+ runtime: "browser",
81
+ fetch: (url, init) => fetch(url, init),
82
+ getReferrerUrl: () => getReferrerUrl(),
83
+ getUrl: () => window.location.href,
84
+ getUserAgent: () => navigator.userAgent,
85
+ generateUUID: () => v4(),
86
+ };
87
+ }
88
+
89
+ function buildNodeEnvironment() {
90
+ return {
91
+ runtime: "node",
92
+ fetch: (url, init) => fetch(url, init),
93
+ getReferrerUrl: () => null,
94
+ getUrl: () => null,
95
+ getUserAgent: () => null,
96
+ generateUUID: () => v4(),
97
+ };
98
+ }
99
+
100
+ function currentEnvironment() {
101
+ return isBrowser() ? buildBrowserEnvironment() : buildNodeEnvironment();
102
+ }
103
+ function isBrowser() {
104
+ try {
105
+ return typeof window === "object";
106
+ }
107
+ catch (e) {
108
+ return false;
109
+ }
110
+ }
111
+
112
+ const version = "0.1.0" ;
113
+
114
+ function getConfig(options) {
115
+ const { trackingId } = options;
116
+ return { trackingId };
117
+ }
118
+ function getSource() {
119
+ return `relay@${version}`;
120
+ }
121
+ function createMeta(type, options, environment, clientIdManager) {
122
+ const { getReferrerUrl, getUrl, getUserAgent } = environment;
123
+ const config = getConfig(options);
124
+ const { clientId } = clientIdManager;
125
+ return {
126
+ type,
127
+ config,
128
+ ts: Date.now(),
129
+ source: getSource(),
130
+ clientId,
131
+ userAgent: getUserAgent(),
132
+ referrerUrl: getReferrerUrl(),
133
+ url: getUrl(),
134
+ };
135
+ }
136
+
137
+ function createRelayEvent(type, payload, options, environment, clientIdManager) {
138
+ return {
139
+ ...payload,
140
+ meta: createMeta(type, options, environment, clientIdManager),
141
+ };
142
+ }
143
+
144
+ async function validate({ event, options, environment, }) {
145
+ const { host, token, organizationId } = options;
146
+ const headers = {
147
+ "Content-Type": "application/json",
148
+ Authorization: `Bearer ${token}`,
149
+ };
150
+ const url = `${host}/rest/organizations/${organizationId}/events/v1/validate`;
151
+ const response = await environment.fetch(url, {
152
+ method: "POST",
153
+ body: JSON.stringify([event]),
154
+ headers,
155
+ });
156
+ const data = await response.json();
157
+ if (!response.ok) {
158
+ return {
159
+ valid: false,
160
+ responseType: "serviceError",
161
+ ...data,
162
+ };
163
+ }
164
+ const { valid, errors } = data[0];
165
+ return { valid, errors: errors ?? [], responseType: "validation" };
166
+ }
167
+
168
+ function createRelay(options) {
169
+ const environment = currentEnvironment();
170
+ const clientIdManager = createClientIdManager(environment);
171
+ return {
172
+ validate: (type, payload) => validate({
173
+ options,
174
+ environment,
175
+ event: createRelayEvent(type, payload, options, environment, clientIdManager),
176
+ }),
177
+ version,
178
+ };
179
+ }
180
+
181
+ exports.createRelay = createRelay;
package/lib/relay.js ADDED
@@ -0,0 +1,185 @@
1
+ /**
2
+ * @todo LENS-1059: The clientId should be a value that is persisted over time on a device.
3
+ */
4
+ function getClientId(environment) {
5
+ return environment.generateUUID();
6
+ }
7
+ function createClientIdManager(environment) {
8
+ return {
9
+ clientId: getClientId(environment),
10
+ };
11
+ }
12
+
13
+ // Unique ID creation requires a high quality random # generator. In the browser we therefore
14
+ // require the crypto API and do not support built-in fallback to lower quality random number
15
+ // generators (like Math.random()).
16
+ let getRandomValues;
17
+ const rnds8 = new Uint8Array(16);
18
+ function rng() {
19
+ // lazy load so that environments that need to polyfill have a chance to do so
20
+ if (!getRandomValues) {
21
+ // getRandomValues needs to be invoked in a context where "this" is a Crypto implementation.
22
+ getRandomValues = typeof crypto !== 'undefined' && crypto.getRandomValues && crypto.getRandomValues.bind(crypto);
23
+
24
+ if (!getRandomValues) {
25
+ throw new Error('crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported');
26
+ }
27
+ }
28
+
29
+ return getRandomValues(rnds8);
30
+ }
31
+
32
+ /**
33
+ * Convert array of 16 byte values to UUID string format of the form:
34
+ * XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
35
+ */
36
+
37
+ const byteToHex = [];
38
+
39
+ for (let i = 0; i < 256; ++i) {
40
+ byteToHex.push((i + 0x100).toString(16).slice(1));
41
+ }
42
+
43
+ function unsafeStringify(arr, offset = 0) {
44
+ // Note: Be careful editing this code! It's been tuned for performance
45
+ // and works in ways you may not expect. See https://github.com/uuidjs/uuid/pull/434
46
+ return (byteToHex[arr[offset + 0]] + byteToHex[arr[offset + 1]] + byteToHex[arr[offset + 2]] + byteToHex[arr[offset + 3]] + '-' + byteToHex[arr[offset + 4]] + byteToHex[arr[offset + 5]] + '-' + byteToHex[arr[offset + 6]] + byteToHex[arr[offset + 7]] + '-' + byteToHex[arr[offset + 8]] + byteToHex[arr[offset + 9]] + '-' + byteToHex[arr[offset + 10]] + byteToHex[arr[offset + 11]] + byteToHex[arr[offset + 12]] + byteToHex[arr[offset + 13]] + byteToHex[arr[offset + 14]] + byteToHex[arr[offset + 15]]).toLowerCase();
47
+ }
48
+
49
+ const randomUUID = typeof crypto !== 'undefined' && crypto.randomUUID && crypto.randomUUID.bind(crypto);
50
+ var native = {
51
+ randomUUID
52
+ };
53
+
54
+ function v4(options, buf, offset) {
55
+ if (native.randomUUID && !buf && !options) {
56
+ return native.randomUUID();
57
+ }
58
+
59
+ options = options || {};
60
+ const rnds = options.random || (options.rng || rng)(); // Per 4.4, set bits for version and `clock_seq_hi_and_reserved`
61
+
62
+ rnds[6] = rnds[6] & 0x0f | 0x40;
63
+ rnds[8] = rnds[8] & 0x3f | 0x80; // Copy bytes to buffer, if provided
64
+
65
+ if (buf) {
66
+ offset = offset || 0;
67
+
68
+ for (let i = 0; i < 16; ++i) {
69
+ buf[offset + i] = rnds[i];
70
+ }
71
+
72
+ return buf;
73
+ }
74
+
75
+ return unsafeStringify(rnds);
76
+ }
77
+
78
+ function getReferrerUrl() {
79
+ const referrer = document.referrer;
80
+ return referrer === "" ? null : referrer;
81
+ }
82
+ function buildBrowserEnvironment() {
83
+ return {
84
+ runtime: "browser",
85
+ fetch: (url, init) => fetch(url, init),
86
+ getReferrerUrl: () => getReferrerUrl(),
87
+ getUrl: () => window.location.href,
88
+ getUserAgent: () => navigator.userAgent,
89
+ generateUUID: () => v4(),
90
+ };
91
+ }
92
+
93
+ function buildNodeEnvironment() {
94
+ return {
95
+ runtime: "node",
96
+ fetch: (url, init) => fetch(url, init),
97
+ getReferrerUrl: () => null,
98
+ getUrl: () => null,
99
+ getUserAgent: () => null,
100
+ generateUUID: () => v4(),
101
+ };
102
+ }
103
+
104
+ function currentEnvironment() {
105
+ return isBrowser() ? buildBrowserEnvironment() : buildNodeEnvironment();
106
+ }
107
+ function isBrowser() {
108
+ try {
109
+ return typeof window === "object";
110
+ }
111
+ catch (e) {
112
+ return false;
113
+ }
114
+ }
115
+
116
+ const version = "0.1.0" ;
117
+
118
+ function getConfig(options) {
119
+ const { trackingId } = options;
120
+ return { trackingId };
121
+ }
122
+ function getSource() {
123
+ return `relay@${version}`;
124
+ }
125
+ function createMeta(type, options, environment, clientIdManager) {
126
+ const { getReferrerUrl, getUrl, getUserAgent } = environment;
127
+ const config = getConfig(options);
128
+ const { clientId } = clientIdManager;
129
+ return {
130
+ type,
131
+ config,
132
+ ts: Date.now(),
133
+ source: getSource(),
134
+ clientId,
135
+ userAgent: getUserAgent(),
136
+ referrerUrl: getReferrerUrl(),
137
+ url: getUrl(),
138
+ };
139
+ }
140
+
141
+ function createRelayEvent(type, payload, options, environment, clientIdManager) {
142
+ return {
143
+ ...payload,
144
+ meta: createMeta(type, options, environment, clientIdManager),
145
+ };
146
+ }
147
+
148
+ async function validate({ event, options, environment, }) {
149
+ const { host, token, organizationId } = options;
150
+ const headers = {
151
+ "Content-Type": "application/json",
152
+ Authorization: `Bearer ${token}`,
153
+ };
154
+ const url = `${host}/rest/organizations/${organizationId}/events/v1/validate`;
155
+ const response = await environment.fetch(url, {
156
+ method: "POST",
157
+ body: JSON.stringify([event]),
158
+ headers,
159
+ });
160
+ const data = await response.json();
161
+ if (!response.ok) {
162
+ return {
163
+ valid: false,
164
+ responseType: "serviceError",
165
+ ...data,
166
+ };
167
+ }
168
+ const { valid, errors } = data[0];
169
+ return { valid, errors: errors ?? [], responseType: "validation" };
170
+ }
171
+
172
+ function createRelay(options) {
173
+ const environment = currentEnvironment();
174
+ const clientIdManager = createClientIdManager(environment);
175
+ return {
176
+ validate: (type, payload) => validate({
177
+ options,
178
+ environment,
179
+ event: createRelayEvent(type, payload, options, environment, clientIdManager),
180
+ }),
181
+ version,
182
+ };
183
+ }
184
+
185
+ export { createRelay };
package/lib/relay.mjs ADDED
@@ -0,0 +1,179 @@
1
+ import crypto from 'crypto';
2
+
3
+ /**
4
+ * @todo LENS-1059: The clientId should be a value that is persisted over time on a device.
5
+ */
6
+ function getClientId(environment) {
7
+ return environment.generateUUID();
8
+ }
9
+ function createClientIdManager(environment) {
10
+ return {
11
+ clientId: getClientId(environment),
12
+ };
13
+ }
14
+
15
+ const rnds8Pool = new Uint8Array(256); // # of random values to pre-allocate
16
+
17
+ let poolPtr = rnds8Pool.length;
18
+ function rng() {
19
+ if (poolPtr > rnds8Pool.length - 16) {
20
+ crypto.randomFillSync(rnds8Pool);
21
+ poolPtr = 0;
22
+ }
23
+
24
+ return rnds8Pool.slice(poolPtr, poolPtr += 16);
25
+ }
26
+
27
+ /**
28
+ * Convert array of 16 byte values to UUID string format of the form:
29
+ * XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
30
+ */
31
+
32
+ const byteToHex = [];
33
+
34
+ for (let i = 0; i < 256; ++i) {
35
+ byteToHex.push((i + 0x100).toString(16).slice(1));
36
+ }
37
+
38
+ function unsafeStringify(arr, offset = 0) {
39
+ // Note: Be careful editing this code! It's been tuned for performance
40
+ // and works in ways you may not expect. See https://github.com/uuidjs/uuid/pull/434
41
+ return (byteToHex[arr[offset + 0]] + byteToHex[arr[offset + 1]] + byteToHex[arr[offset + 2]] + byteToHex[arr[offset + 3]] + '-' + byteToHex[arr[offset + 4]] + byteToHex[arr[offset + 5]] + '-' + byteToHex[arr[offset + 6]] + byteToHex[arr[offset + 7]] + '-' + byteToHex[arr[offset + 8]] + byteToHex[arr[offset + 9]] + '-' + byteToHex[arr[offset + 10]] + byteToHex[arr[offset + 11]] + byteToHex[arr[offset + 12]] + byteToHex[arr[offset + 13]] + byteToHex[arr[offset + 14]] + byteToHex[arr[offset + 15]]).toLowerCase();
42
+ }
43
+
44
+ var native = {
45
+ randomUUID: crypto.randomUUID
46
+ };
47
+
48
+ function v4(options, buf, offset) {
49
+ if (native.randomUUID && !buf && !options) {
50
+ return native.randomUUID();
51
+ }
52
+
53
+ options = options || {};
54
+ const rnds = options.random || (options.rng || rng)(); // Per 4.4, set bits for version and `clock_seq_hi_and_reserved`
55
+
56
+ rnds[6] = rnds[6] & 0x0f | 0x40;
57
+ rnds[8] = rnds[8] & 0x3f | 0x80; // Copy bytes to buffer, if provided
58
+
59
+ if (buf) {
60
+ offset = offset || 0;
61
+
62
+ for (let i = 0; i < 16; ++i) {
63
+ buf[offset + i] = rnds[i];
64
+ }
65
+
66
+ return buf;
67
+ }
68
+
69
+ return unsafeStringify(rnds);
70
+ }
71
+
72
+ function getReferrerUrl() {
73
+ const referrer = document.referrer;
74
+ return referrer === "" ? null : referrer;
75
+ }
76
+ function buildBrowserEnvironment() {
77
+ return {
78
+ runtime: "browser",
79
+ fetch: (url, init) => fetch(url, init),
80
+ getReferrerUrl: () => getReferrerUrl(),
81
+ getUrl: () => window.location.href,
82
+ getUserAgent: () => navigator.userAgent,
83
+ generateUUID: () => v4(),
84
+ };
85
+ }
86
+
87
+ function buildNodeEnvironment() {
88
+ return {
89
+ runtime: "node",
90
+ fetch: (url, init) => fetch(url, init),
91
+ getReferrerUrl: () => null,
92
+ getUrl: () => null,
93
+ getUserAgent: () => null,
94
+ generateUUID: () => v4(),
95
+ };
96
+ }
97
+
98
+ function currentEnvironment() {
99
+ return isBrowser() ? buildBrowserEnvironment() : buildNodeEnvironment();
100
+ }
101
+ function isBrowser() {
102
+ try {
103
+ return typeof window === "object";
104
+ }
105
+ catch (e) {
106
+ return false;
107
+ }
108
+ }
109
+
110
+ const version = "0.1.0" ;
111
+
112
+ function getConfig(options) {
113
+ const { trackingId } = options;
114
+ return { trackingId };
115
+ }
116
+ function getSource() {
117
+ return `relay@${version}`;
118
+ }
119
+ function createMeta(type, options, environment, clientIdManager) {
120
+ const { getReferrerUrl, getUrl, getUserAgent } = environment;
121
+ const config = getConfig(options);
122
+ const { clientId } = clientIdManager;
123
+ return {
124
+ type,
125
+ config,
126
+ ts: Date.now(),
127
+ source: getSource(),
128
+ clientId,
129
+ userAgent: getUserAgent(),
130
+ referrerUrl: getReferrerUrl(),
131
+ url: getUrl(),
132
+ };
133
+ }
134
+
135
+ function createRelayEvent(type, payload, options, environment, clientIdManager) {
136
+ return {
137
+ ...payload,
138
+ meta: createMeta(type, options, environment, clientIdManager),
139
+ };
140
+ }
141
+
142
+ async function validate({ event, options, environment, }) {
143
+ const { host, token, organizationId } = options;
144
+ const headers = {
145
+ "Content-Type": "application/json",
146
+ Authorization: `Bearer ${token}`,
147
+ };
148
+ const url = `${host}/rest/organizations/${organizationId}/events/v1/validate`;
149
+ const response = await environment.fetch(url, {
150
+ method: "POST",
151
+ body: JSON.stringify([event]),
152
+ headers,
153
+ });
154
+ const data = await response.json();
155
+ if (!response.ok) {
156
+ return {
157
+ valid: false,
158
+ responseType: "serviceError",
159
+ ...data,
160
+ };
161
+ }
162
+ const { valid, errors } = data[0];
163
+ return { valid, errors: errors ?? [], responseType: "validation" };
164
+ }
165
+
166
+ function createRelay(options) {
167
+ const environment = currentEnvironment();
168
+ const clientIdManager = createClientIdManager(environment);
169
+ return {
170
+ validate: (type, payload) => validate({
171
+ options,
172
+ environment,
173
+ event: createRelayEvent(type, payload, options, environment, clientIdManager),
174
+ }),
175
+ version,
176
+ };
177
+ }
178
+
179
+ export { createRelay };
@@ -0,0 +1,6 @@
1
+ import { Environment } from "../environment/environment";
2
+ export interface ClientIdManager {
3
+ clientId: string;
4
+ }
5
+ export declare function createClientIdManager(environment: Environment): ClientIdManager;
6
+ //# sourceMappingURL=client-id.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client-id.d.ts","sourceRoot":"","sources":["../../../src/client-id/client-id.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,4BAA4B,CAAC;AAEzD,MAAM,WAAW,eAAe;IAC9B,QAAQ,EAAE,MAAM,CAAC;CAClB;AASD,wBAAgB,qBAAqB,CACnC,WAAW,EAAE,WAAW,GACvB,eAAe,CAIjB"}
@@ -0,0 +1,3 @@
1
+ import { Environment } from "../environment";
2
+ export declare function buildBrowserEnvironment(): Environment;
3
+ //# sourceMappingURL=browser.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"browser.d.ts","sourceRoot":"","sources":["../../../../src/environment/browser/browser.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAQ7C,wBAAgB,uBAAuB,IAAI,WAAW,CASrD"}
@@ -0,0 +1,10 @@
1
+ export interface Environment {
2
+ runtime: "browser" | "node";
3
+ fetch: (url: string, init?: RequestInit) => Promise<Response>;
4
+ getReferrerUrl: () => string | null;
5
+ getUrl: () => string | null;
6
+ getUserAgent: () => string | null;
7
+ generateUUID: () => string;
8
+ }
9
+ export declare function currentEnvironment(): Environment;
10
+ //# sourceMappingURL=environment.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"environment.d.ts","sourceRoot":"","sources":["../../../src/environment/environment.ts"],"names":[],"mappings":"AAGA,MAAM,WAAW,WAAW;IAC1B,OAAO,EAAE,SAAS,GAAG,MAAM,CAAC;IAC5B,KAAK,EAAE,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,WAAW,KAAK,OAAO,CAAC,QAAQ,CAAC,CAAC;IAC9D,cAAc,EAAE,MAAM,MAAM,GAAG,IAAI,CAAC;IACpC,MAAM,EAAE,MAAM,MAAM,GAAG,IAAI,CAAC;IAC5B,YAAY,EAAE,MAAM,MAAM,GAAG,IAAI,CAAC;IAClC,YAAY,EAAE,MAAM,MAAM,CAAC;CAC5B;AAED,wBAAgB,kBAAkB,IAAI,WAAW,CAEhD"}
@@ -0,0 +1,3 @@
1
+ import { Environment } from "../environment";
2
+ export declare function buildNodeEnvironment(): Environment;
3
+ //# sourceMappingURL=node.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"node.d.ts","sourceRoot":"","sources":["../../../../src/environment/node/node.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAE7C,wBAAgB,oBAAoB,IAAI,WAAW,CASlD"}
@@ -0,0 +1,19 @@
1
+ import { ClientIdManager } from "../../client-id/client-id";
2
+ import { Environment } from "../../environment/environment";
3
+ import { RelayOptions } from "../../relay";
4
+ interface Config {
5
+ trackingId: string;
6
+ }
7
+ export interface Meta {
8
+ type: string;
9
+ config: Config;
10
+ ts: number;
11
+ source: string;
12
+ clientId: string;
13
+ userAgent: string | null;
14
+ referrerUrl: string | null;
15
+ url: string | null;
16
+ }
17
+ export declare function createMeta(type: string, options: RelayOptions, environment: Environment, clientIdManager: ClientIdManager): Readonly<Meta>;
18
+ export {};
19
+ //# sourceMappingURL=meta.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"meta.d.ts","sourceRoot":"","sources":["../../../../src/event/meta/meta.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,2BAA2B,CAAC;AAC5D,OAAO,EAAE,WAAW,EAAE,MAAM,+BAA+B,CAAC;AAC5D,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAG3C,UAAU,MAAM;IACd,UAAU,EAAE,MAAM,CAAC;CACpB;AAED,MAAM,WAAW,IAAI;IACnB,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,MAAM,CAAC;IACf,EAAE,EAAE,MAAM,CAAC;IACX,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,EAAE,MAAM,CAAC;IACjB,SAAS,EAAE,MAAM,GAAG,IAAI,CAAC;IACzB,WAAW,EAAE,MAAM,GAAG,IAAI,CAAC;IAC3B,GAAG,EAAE,MAAM,GAAG,IAAI,CAAC;CACpB;AAWD,wBAAgB,UAAU,CACxB,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,YAAY,EACrB,WAAW,EAAE,WAAW,EACxB,eAAe,EAAE,eAAe,GAC/B,QAAQ,CAAC,IAAI,CAAC,CAehB"}
@@ -0,0 +1,9 @@
1
+ import { ClientIdManager } from "../client-id/client-id";
2
+ import { Environment } from "../environment/environment";
3
+ import { RelayOptions, RelayPayload } from "../relay";
4
+ import { Meta } from "./meta/meta";
5
+ export interface RelayEvent extends RelayPayload {
6
+ meta: Meta;
7
+ }
8
+ export declare function createRelayEvent(type: string, payload: RelayPayload, options: RelayOptions, environment: Environment, clientIdManager: ClientIdManager): Readonly<RelayEvent>;
9
+ //# sourceMappingURL=relay-event.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"relay-event.d.ts","sourceRoot":"","sources":["../../../src/event/relay-event.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AACzD,OAAO,EAAE,WAAW,EAAE,MAAM,4BAA4B,CAAC;AACzD,OAAO,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AACtD,OAAO,EAAc,IAAI,EAAE,MAAM,aAAa,CAAC;AAE/C,MAAM,WAAW,UAAW,SAAQ,YAAY;IAC9C,IAAI,EAAE,IAAI,CAAC;CACZ;AAED,wBAAgB,gBAAgB,CAC9B,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,YAAY,EACrB,OAAO,EAAE,YAAY,EACrB,WAAW,EAAE,WAAW,EACxB,eAAe,EAAE,eAAe,GAC/B,QAAQ,CAAC,UAAU,CAAC,CAKtB"}
@@ -0,0 +1,15 @@
1
+ import { ServiceErrorResponse, ValidationError, ValidationReport, ValidationResponse } from "./validate/validate";
2
+ type RelayPayload = Record<string, unknown>;
3
+ interface RelayOptions {
4
+ host: string;
5
+ organizationId: string;
6
+ token: string;
7
+ trackingId: string;
8
+ }
9
+ interface Relay {
10
+ validate: (type: string, payload: RelayPayload) => Promise<ValidationReport>;
11
+ version: string;
12
+ }
13
+ export declare function createRelay(options: RelayOptions): Relay;
14
+ export type { RelayPayload, RelayOptions, ServiceErrorResponse, ValidationError, ValidationReport, ValidationResponse, };
15
+ //# sourceMappingURL=relay.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"relay.d.ts","sourceRoot":"","sources":["../../src/relay.ts"],"names":[],"mappings":"AAGA,OAAO,EACL,oBAAoB,EAEpB,eAAe,EACf,gBAAgB,EAChB,kBAAkB,EACnB,MAAM,qBAAqB,CAAC;AAG7B,KAAK,YAAY,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AAE5C,UAAU,YAAY;IACpB,IAAI,EAAE,MAAM,CAAC;IACb,cAAc,EAAE,MAAM,CAAC;IACvB,KAAK,EAAE,MAAM,CAAC;IACd,UAAU,EAAE,MAAM,CAAC;CACpB;AAED,UAAU,KAAK;IACb,QAAQ,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,YAAY,KAAK,OAAO,CAAC,gBAAgB,CAAC,CAAC;IAC7E,OAAO,EAAE,MAAM,CAAC;CACjB;AAED,wBAAgB,WAAW,CAAC,OAAO,EAAE,YAAY,GAAG,KAAK,CAmBxD;AAED,YAAY,EACV,YAAY,EACZ,YAAY,EACZ,oBAAoB,EACpB,eAAe,EACf,gBAAgB,EAChB,kBAAkB,GACnB,CAAC"}
@@ -0,0 +1,29 @@
1
+ import { Environment } from "../environment/environment";
2
+ import { RelayOptions } from "../relay";
3
+ import { RelayEvent } from "../event/relay-event";
4
+ interface ValidationParams {
5
+ event: Readonly<RelayEvent>;
6
+ options: RelayOptions;
7
+ environment: Environment;
8
+ }
9
+ export interface ValidationError {
10
+ type: string;
11
+ message: string;
12
+ path: string;
13
+ }
14
+ export interface ValidationResponse {
15
+ valid: boolean;
16
+ responseType: "validation";
17
+ errors: ValidationError[];
18
+ }
19
+ export interface ServiceErrorResponse {
20
+ valid: false;
21
+ responseType: "serviceError";
22
+ errorCode: string;
23
+ message: string;
24
+ requestID: string;
25
+ }
26
+ export type ValidationReport = ValidationResponse | ServiceErrorResponse;
27
+ export declare function validate({ event, options, environment, }: ValidationParams): Promise<Readonly<ValidationReport>>;
28
+ export {};
29
+ //# sourceMappingURL=validate.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validate.d.ts","sourceRoot":"","sources":["../../../src/validate/validate.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,4BAA4B,CAAC;AACzD,OAAO,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AACxC,OAAO,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC;AAElD,UAAU,gBAAgB;IACxB,KAAK,EAAE,QAAQ,CAAC,UAAU,CAAC,CAAC;IAC5B,OAAO,EAAE,YAAY,CAAC;IACtB,WAAW,EAAE,WAAW,CAAC;CAC1B;AAED,MAAM,WAAW,eAAe;IAC9B,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,EAAE,MAAM,CAAC;CACd;AAED,MAAM,WAAW,kBAAkB;IACjC,KAAK,EAAE,OAAO,CAAC;IACf,YAAY,EAAE,YAAY,CAAC;IAC3B,MAAM,EAAE,eAAe,EAAE,CAAC;CAC3B;AAED,MAAM,WAAW,oBAAoB;IACnC,KAAK,EAAE,KAAK,CAAC;IACb,YAAY,EAAE,cAAc,CAAC;IAC7B,SAAS,EAAE,MAAM,CAAC;IAClB,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,EAAE,MAAM,CAAC;CACnB;AAED,MAAM,MAAM,gBAAgB,GAAG,kBAAkB,GAAG,oBAAoB,CAAC;AAEzE,wBAAsB,QAAQ,CAAC,EAC7B,KAAK,EACL,OAAO,EACP,WAAW,GACZ,EAAE,gBAAgB,GAAG,OAAO,CAAC,QAAQ,CAAC,gBAAgB,CAAC,CAAC,CA4BxD"}
@@ -0,0 +1,2 @@
1
+ export declare const version: string;
2
+ //# sourceMappingURL=version.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"version.d.ts","sourceRoot":"","sources":["../../src/version.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,OAAO,EAAE,MAAkC,CAAC"}
package/package.json CHANGED
@@ -1,7 +1,27 @@
1
1
  {
2
2
  "name": "@coveo/relay",
3
- "version": "0.0.6",
3
+ "version": "0.1.0",
4
4
  "description": "A library for sending analytics events using Coveo's Event protocol.",
5
+ "files": [
6
+ "lib/**/*"
7
+ ],
8
+ "types": "./lib/types/relay.d.ts",
9
+ "exports": {
10
+ "node": {
11
+ "import": {
12
+ "types": "./lib/types/relay.d.ts",
13
+ "default": "./lib/relay.mjs"
14
+ },
15
+ "require": {
16
+ "types": "./lib/types/relay.d.ts",
17
+ "default": "./lib/relay.cjs"
18
+ }
19
+ },
20
+ "default": {
21
+ "types": "./lib/types/relay.d.ts",
22
+ "default": "./lib/relay.js"
23
+ }
24
+ },
5
25
  "repository": {
6
26
  "type": "git",
7
27
  "url": "git+https://github.com/coveo/relay.git"
@@ -18,17 +38,28 @@
18
38
  },
19
39
  "homepage": "https://github.com/coveo/relay#readme",
20
40
  "devDependencies": {
41
+ "@rollup/plugin-node-resolve": "^15.2.1",
42
+ "@rollup/plugin-replace": "^5.0.2",
43
+ "@rollup/plugin-typescript": "^11.1.2",
21
44
  "@types/jest": "^29.5.3",
45
+ "@types/uuid": "^9.0.2",
22
46
  "jest": "^29.6.2",
23
47
  "jest-environment-jsdom": "^29.6.2",
48
+ "rollup": "^3.28.0",
24
49
  "ts-jest": "^29.1.1",
25
50
  "typescript": "^5.1.6",
26
- "eslint-config-custom": "0.0.6",
27
- "tsconfig": "0.0.0"
51
+ "tsconfig": "0.0.0",
52
+ "eslint-config-custom": "0.0.0"
53
+ },
54
+ "dependencies": {
55
+ "uuid": "^9.0.0"
28
56
  },
29
57
  "scripts": {
58
+ "build": "npm run clean && rollup --config ./config/rollup.config.mjs",
59
+ "clean": "rm -rf ./lib",
30
60
  "test": "jest",
31
61
  "test:watch": "jest --watch",
32
- "publish:npm": "node ./scripts/publish.mjs"
62
+ "npm:publish": "node ./scripts/publish.mjs",
63
+ "npm:tag": "node ./scripts/update-npm-tag.mjs"
33
64
  }
34
65
  }
File without changes
package/CHANGELOG.md DELETED
@@ -1,4 +0,0 @@
1
- ## 0.0.6 (2023-08-08)
2
-
3
-
4
-
package/jest.config.js DELETED
@@ -1,5 +0,0 @@
1
- /** @type {import('ts-jest').JestConfigWithTsJest} */
2
- module.exports = {
3
- preset: "ts-jest",
4
- testEnvironment: "node",
5
- };
@@ -1,28 +0,0 @@
1
- import {context} from '@actions/github';
2
- import {
3
- describeNpmTag,
4
- getLastTag,
5
- pnpmPublish,
6
- } from "@coveo/semantic-monorepo-tools";
7
-
8
- const VERSION_PREFIX = "@coveo/relay/v";
9
-
10
- const publishNpm = async () => {
11
- try {
12
- const refBranchName = context.payload.pull_request.head.ref;
13
- const lastTag = await getLastTag(VERSION_PREFIX);
14
- const version = lastTag.replace(VERSION_PREFIX, "");
15
- const currentAlphaVersion = await describeNpmTag("@coveo/relay", "alpha");
16
- console.info("The current version of the package is %s", version);
17
- console.info("the describe npm tag is %s", currentAlphaVersion);
18
-
19
- if (version > currentAlphaVersion) {
20
- console.info("needs to publish");
21
- await pnpmPublish(undefined, "alpha", refBranchName);
22
- }
23
- } catch (e) {
24
- console.log(e);
25
- }
26
- };
27
-
28
- publishNpm();
@@ -1,16 +0,0 @@
1
- /**
2
- * @jest-environment jsdom
3
- */
4
-
5
- import { buildBrowserEnvironment } from "./browser";
6
-
7
- describe("buildBrowserEnvironment", () => {
8
- it("retrieves the referrer", () => {
9
- Object.defineProperty(window.document, "referrer", {
10
- value: "https://www.coveo.com/",
11
- });
12
- expect(buildBrowserEnvironment().getReferrer()).toBe(
13
- "https://www.coveo.com/"
14
- );
15
- });
16
- });
@@ -1,7 +0,0 @@
1
- import { Environment } from "../environment";
2
-
3
- export function buildBrowserEnvironment(): Environment {
4
- return {
5
- getReferrer: () => document.referrer,
6
- };
7
- }
@@ -1,3 +0,0 @@
1
- export interface Environment {
2
- getReferrer: () => string | null;
3
- }
@@ -1,7 +0,0 @@
1
- import { buildNodeEnvironment } from "./node";
2
-
3
- describe("buildNodeEnvironment", () => {
4
- it("getReferrer returns null", () => {
5
- expect(buildNodeEnvironment().getReferrer()).toBe(null);
6
- });
7
- });
@@ -1,7 +0,0 @@
1
- import { Environment } from "../environment";
2
-
3
- export function buildNodeEnvironment(): Environment {
4
- return {
5
- getReferrer: () => null,
6
- };
7
- }
package/tsconfig.json DELETED
@@ -1,5 +0,0 @@
1
- {
2
- "extends": "tsconfig/base.json",
3
- "include": ["**/*.ts"],
4
- "exclude": ["node_modules"]
5
- }