@coveo/relay 0.1.0 → 0.2.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 +35 -21
- package/lib/relay.js +35 -21
- package/lib/relay.mjs +35 -21
- package/lib/types/emit/emit.d.ts +3 -0
- package/lib/types/emit/emit.d.ts.map +1 -0
- package/lib/types/event-api-call/event-api-caller.d.ts +11 -0
- package/lib/types/event-api-call/event-api-caller.d.ts.map +1 -0
- package/lib/types/relay.d.ts +6 -3
- package/lib/types/relay.d.ts.map +1 -1
- package/lib/types/validate/validate.d.ts +2 -18
- package/lib/types/validate/validate.d.ts.map +1 -1
- package/package.json +1 -2
package/lib/relay.cjs
CHANGED
|
@@ -2,6 +2,31 @@
|
|
|
2
2
|
|
|
3
3
|
var crypto = require('crypto');
|
|
4
4
|
|
|
5
|
+
async function callEventApi({ event, options, environment, validate, }) {
|
|
6
|
+
const { token, host, organizationId } = options;
|
|
7
|
+
const headers = {
|
|
8
|
+
"Content-Type": "application/json",
|
|
9
|
+
Authorization: `Bearer ${token}`,
|
|
10
|
+
};
|
|
11
|
+
const response = await environment.fetch(`${host}/rest/organizations/${organizationId}/events/v1${validate ? "/validate" : ""}`, {
|
|
12
|
+
method: "POST",
|
|
13
|
+
body: JSON.stringify([event]),
|
|
14
|
+
headers,
|
|
15
|
+
});
|
|
16
|
+
const data = await response.json();
|
|
17
|
+
if (!response.ok) {
|
|
18
|
+
throw new Error({
|
|
19
|
+
responseType: "serviceError",
|
|
20
|
+
...data,
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
return data;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
async function emit(params) {
|
|
27
|
+
await callEventApi(params);
|
|
28
|
+
}
|
|
29
|
+
|
|
5
30
|
/**
|
|
6
31
|
* @todo LENS-1059: The clientId should be a value that is persisted over time on a device.
|
|
7
32
|
*/
|
|
@@ -109,7 +134,7 @@ function isBrowser() {
|
|
|
109
134
|
}
|
|
110
135
|
}
|
|
111
136
|
|
|
112
|
-
const version = "0.
|
|
137
|
+
const version = "0.2.0" ;
|
|
113
138
|
|
|
114
139
|
function getConfig(options) {
|
|
115
140
|
const { trackingId } = options;
|
|
@@ -141,26 +166,9 @@ function createRelayEvent(type, payload, options, environment, clientIdManager)
|
|
|
141
166
|
};
|
|
142
167
|
}
|
|
143
168
|
|
|
144
|
-
async function validate(
|
|
145
|
-
|
|
146
|
-
const
|
|
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
|
-
}
|
|
169
|
+
async function validate(params) {
|
|
170
|
+
params.validate = true;
|
|
171
|
+
const data = await callEventApi(params);
|
|
164
172
|
const { valid, errors } = data[0];
|
|
165
173
|
return { valid, errors: errors ?? [], responseType: "validation" };
|
|
166
174
|
}
|
|
@@ -174,6 +182,12 @@ function createRelay(options) {
|
|
|
174
182
|
environment,
|
|
175
183
|
event: createRelayEvent(type, payload, options, environment, clientIdManager),
|
|
176
184
|
}),
|
|
185
|
+
emit: (type, payload) => emit({
|
|
186
|
+
options,
|
|
187
|
+
environment,
|
|
188
|
+
event: createRelayEvent(type, payload, options, environment, clientIdManager),
|
|
189
|
+
}),
|
|
190
|
+
getMeta: (type) => createMeta(type, options, environment, clientIdManager),
|
|
177
191
|
version,
|
|
178
192
|
};
|
|
179
193
|
}
|
package/lib/relay.js
CHANGED
|
@@ -1,3 +1,28 @@
|
|
|
1
|
+
async function callEventApi({ event, options, environment, validate, }) {
|
|
2
|
+
const { token, host, organizationId } = options;
|
|
3
|
+
const headers = {
|
|
4
|
+
"Content-Type": "application/json",
|
|
5
|
+
Authorization: `Bearer ${token}`,
|
|
6
|
+
};
|
|
7
|
+
const response = await environment.fetch(`${host}/rest/organizations/${organizationId}/events/v1${validate ? "/validate" : ""}`, {
|
|
8
|
+
method: "POST",
|
|
9
|
+
body: JSON.stringify([event]),
|
|
10
|
+
headers,
|
|
11
|
+
});
|
|
12
|
+
const data = await response.json();
|
|
13
|
+
if (!response.ok) {
|
|
14
|
+
throw new Error({
|
|
15
|
+
responseType: "serviceError",
|
|
16
|
+
...data,
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
return data;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
async function emit(params) {
|
|
23
|
+
await callEventApi(params);
|
|
24
|
+
}
|
|
25
|
+
|
|
1
26
|
/**
|
|
2
27
|
* @todo LENS-1059: The clientId should be a value that is persisted over time on a device.
|
|
3
28
|
*/
|
|
@@ -113,7 +138,7 @@ function isBrowser() {
|
|
|
113
138
|
}
|
|
114
139
|
}
|
|
115
140
|
|
|
116
|
-
const version = "0.
|
|
141
|
+
const version = "0.2.0" ;
|
|
117
142
|
|
|
118
143
|
function getConfig(options) {
|
|
119
144
|
const { trackingId } = options;
|
|
@@ -145,26 +170,9 @@ function createRelayEvent(type, payload, options, environment, clientIdManager)
|
|
|
145
170
|
};
|
|
146
171
|
}
|
|
147
172
|
|
|
148
|
-
async function validate(
|
|
149
|
-
|
|
150
|
-
const
|
|
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
|
-
}
|
|
173
|
+
async function validate(params) {
|
|
174
|
+
params.validate = true;
|
|
175
|
+
const data = await callEventApi(params);
|
|
168
176
|
const { valid, errors } = data[0];
|
|
169
177
|
return { valid, errors: errors ?? [], responseType: "validation" };
|
|
170
178
|
}
|
|
@@ -178,6 +186,12 @@ function createRelay(options) {
|
|
|
178
186
|
environment,
|
|
179
187
|
event: createRelayEvent(type, payload, options, environment, clientIdManager),
|
|
180
188
|
}),
|
|
189
|
+
emit: (type, payload) => emit({
|
|
190
|
+
options,
|
|
191
|
+
environment,
|
|
192
|
+
event: createRelayEvent(type, payload, options, environment, clientIdManager),
|
|
193
|
+
}),
|
|
194
|
+
getMeta: (type) => createMeta(type, options, environment, clientIdManager),
|
|
181
195
|
version,
|
|
182
196
|
};
|
|
183
197
|
}
|
package/lib/relay.mjs
CHANGED
|
@@ -1,5 +1,30 @@
|
|
|
1
1
|
import crypto from 'crypto';
|
|
2
2
|
|
|
3
|
+
async function callEventApi({ event, options, environment, validate, }) {
|
|
4
|
+
const { token, host, organizationId } = options;
|
|
5
|
+
const headers = {
|
|
6
|
+
"Content-Type": "application/json",
|
|
7
|
+
Authorization: `Bearer ${token}`,
|
|
8
|
+
};
|
|
9
|
+
const response = await environment.fetch(`${host}/rest/organizations/${organizationId}/events/v1${validate ? "/validate" : ""}`, {
|
|
10
|
+
method: "POST",
|
|
11
|
+
body: JSON.stringify([event]),
|
|
12
|
+
headers,
|
|
13
|
+
});
|
|
14
|
+
const data = await response.json();
|
|
15
|
+
if (!response.ok) {
|
|
16
|
+
throw new Error({
|
|
17
|
+
responseType: "serviceError",
|
|
18
|
+
...data,
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
return data;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
async function emit(params) {
|
|
25
|
+
await callEventApi(params);
|
|
26
|
+
}
|
|
27
|
+
|
|
3
28
|
/**
|
|
4
29
|
* @todo LENS-1059: The clientId should be a value that is persisted over time on a device.
|
|
5
30
|
*/
|
|
@@ -107,7 +132,7 @@ function isBrowser() {
|
|
|
107
132
|
}
|
|
108
133
|
}
|
|
109
134
|
|
|
110
|
-
const version = "0.
|
|
135
|
+
const version = "0.2.0" ;
|
|
111
136
|
|
|
112
137
|
function getConfig(options) {
|
|
113
138
|
const { trackingId } = options;
|
|
@@ -139,26 +164,9 @@ function createRelayEvent(type, payload, options, environment, clientIdManager)
|
|
|
139
164
|
};
|
|
140
165
|
}
|
|
141
166
|
|
|
142
|
-
async function validate(
|
|
143
|
-
|
|
144
|
-
const
|
|
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
|
-
}
|
|
167
|
+
async function validate(params) {
|
|
168
|
+
params.validate = true;
|
|
169
|
+
const data = await callEventApi(params);
|
|
162
170
|
const { valid, errors } = data[0];
|
|
163
171
|
return { valid, errors: errors ?? [], responseType: "validation" };
|
|
164
172
|
}
|
|
@@ -172,6 +180,12 @@ function createRelay(options) {
|
|
|
172
180
|
environment,
|
|
173
181
|
event: createRelayEvent(type, payload, options, environment, clientIdManager),
|
|
174
182
|
}),
|
|
183
|
+
emit: (type, payload) => emit({
|
|
184
|
+
options,
|
|
185
|
+
environment,
|
|
186
|
+
event: createRelayEvent(type, payload, options, environment, clientIdManager),
|
|
187
|
+
}),
|
|
188
|
+
getMeta: (type) => createMeta(type, options, environment, clientIdManager),
|
|
175
189
|
version,
|
|
176
190
|
};
|
|
177
191
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"emit.d.ts","sourceRoot":"","sources":["../../../src/emit/emit.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,kBAAkB,EACnB,MAAM,oCAAoC,CAAC;AAE5C,wBAAsB,IAAI,CAAC,MAAM,EAAE,kBAAkB,iBAEpD"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Environment } from "../environment/environment";
|
|
2
|
+
import { RelayOptions } from "../relay";
|
|
3
|
+
import { RelayEvent } from "../event/relay-event";
|
|
4
|
+
export interface EventApiCallParams {
|
|
5
|
+
event: Readonly<RelayEvent>;
|
|
6
|
+
options: RelayOptions;
|
|
7
|
+
environment: Environment;
|
|
8
|
+
validate?: boolean;
|
|
9
|
+
}
|
|
10
|
+
export declare function callEventApi({ event, options, environment, validate, }: EventApiCallParams): Promise<any>;
|
|
11
|
+
//# sourceMappingURL=event-api-caller.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"event-api-caller.d.ts","sourceRoot":"","sources":["../../../src/event-api-call/event-api-caller.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,MAAM,WAAW,kBAAkB;IACjC,KAAK,EAAE,QAAQ,CAAC,UAAU,CAAC,CAAC;IAC5B,OAAO,EAAE,YAAY,CAAC;IACtB,WAAW,EAAE,WAAW,CAAC;IACzB,QAAQ,CAAC,EAAE,OAAO,CAAC;CACpB;AAED,wBAAsB,YAAY,CAAC,EACjC,KAAK,EACL,OAAO,EACP,WAAW,EACX,QAAQ,GACT,EAAE,kBAAkB,GAAG,OAAO,CAAC,GAAG,CAAC,CA2BnC"}
|
package/lib/types/relay.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ValidationError, ValidationResponse } from "./validate/validate";
|
|
2
|
+
import { Meta } from "./event/meta/meta";
|
|
2
3
|
type RelayPayload = Record<string, unknown>;
|
|
3
4
|
interface RelayOptions {
|
|
4
5
|
host: string;
|
|
@@ -7,9 +8,11 @@ interface RelayOptions {
|
|
|
7
8
|
trackingId: string;
|
|
8
9
|
}
|
|
9
10
|
interface Relay {
|
|
10
|
-
validate: (type: string, payload: RelayPayload) => Promise<
|
|
11
|
+
validate: (type: string, payload: RelayPayload) => Promise<ValidationResponse>;
|
|
12
|
+
emit: (type: string, payload: RelayPayload) => Promise<void>;
|
|
13
|
+
getMeta: (type: string) => Meta;
|
|
11
14
|
version: string;
|
|
12
15
|
}
|
|
13
16
|
export declare function createRelay(options: RelayOptions): Relay;
|
|
14
|
-
export type { RelayPayload, RelayOptions,
|
|
17
|
+
export type { RelayPayload, RelayOptions, ValidationError, ValidationResponse };
|
|
15
18
|
//# sourceMappingURL=relay.d.ts.map
|
package/lib/types/relay.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"relay.d.ts","sourceRoot":"","sources":["../../src/relay.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"relay.d.ts","sourceRoot":"","sources":["../../src/relay.ts"],"names":[],"mappings":"AAIA,OAAO,EAEL,eAAe,EACf,kBAAkB,EACnB,MAAM,qBAAqB,CAAC;AAE7B,OAAO,EAAc,IAAI,EAAE,MAAM,mBAAmB,CAAC;AAErD,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,CACR,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,YAAY,KAClB,OAAO,CAAC,kBAAkB,CAAC,CAAC;IACjC,IAAI,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,YAAY,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IAC7D,OAAO,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,IAAI,CAAC;IAChC,OAAO,EAAE,MAAM,CAAC;CACjB;AAED,wBAAgB,WAAW,CAAC,OAAO,EAAE,YAAY,GAAG,KAAK,CAiCxD;AAED,YAAY,EAAE,YAAY,EAAE,YAAY,EAAE,eAAe,EAAE,kBAAkB,EAAE,CAAC"}
|
|
@@ -1,11 +1,4 @@
|
|
|
1
|
-
import {
|
|
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
|
-
}
|
|
1
|
+
import { EventApiCallParams } from "../event-api-call/event-api-caller";
|
|
9
2
|
export interface ValidationError {
|
|
10
3
|
type: string;
|
|
11
4
|
message: string;
|
|
@@ -16,14 +9,5 @@ export interface ValidationResponse {
|
|
|
16
9
|
responseType: "validation";
|
|
17
10
|
errors: ValidationError[];
|
|
18
11
|
}
|
|
19
|
-
export
|
|
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 {};
|
|
12
|
+
export declare function validate(params: EventApiCallParams): Promise<Readonly<ValidationResponse>>;
|
|
29
13
|
//# sourceMappingURL=validate.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validate.d.ts","sourceRoot":"","sources":["../../../src/validate/validate.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"validate.d.ts","sourceRoot":"","sources":["../../../src/validate/validate.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,kBAAkB,EACnB,MAAM,oCAAoC,CAAC;AAE5C,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,wBAAsB,QAAQ,CAC5B,MAAM,EAAE,kBAAkB,GACzB,OAAO,CAAC,QAAQ,CAAC,kBAAkB,CAAC,CAAC,CAOvC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@coveo/relay",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "A library for sending analytics events using Coveo's Event protocol.",
|
|
5
5
|
"files": [
|
|
6
6
|
"lib/**/*"
|
|
@@ -59,7 +59,6 @@
|
|
|
59
59
|
"clean": "rm -rf ./lib",
|
|
60
60
|
"test": "jest",
|
|
61
61
|
"test:watch": "jest --watch",
|
|
62
|
-
"npm:publish": "node ./scripts/publish.mjs",
|
|
63
62
|
"npm:tag": "node ./scripts/update-npm-tag.mjs"
|
|
64
63
|
}
|
|
65
64
|
}
|