@coveo/relay 0.1.0 → 0.1.2

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 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.1.0" ;
137
+ const version = "0.1.2" ;
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({ 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
- }
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,11 @@ 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
+ }),
177
190
  version,
178
191
  };
179
192
  }
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.1.0" ;
141
+ const version = "0.1.2" ;
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({ 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
- }
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,11 @@ 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
+ }),
181
194
  version,
182
195
  };
183
196
  }
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.1.0" ;
135
+ const version = "0.1.2" ;
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({ 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
- }
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,11 @@ 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
+ }),
175
188
  version,
176
189
  };
177
190
  }
@@ -0,0 +1,3 @@
1
+ import { EventApiCallParams } from "../event-api-call/event-api-caller";
2
+ export declare function emit(params: EventApiCallParams): Promise<void>;
3
+ //# sourceMappingURL=emit.d.ts.map
@@ -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"}
@@ -1,4 +1,4 @@
1
- import { ServiceErrorResponse, ValidationError, ValidationReport, ValidationResponse } from "./validate/validate";
1
+ import { ValidationError, ValidationResponse } from "./validate/validate";
2
2
  type RelayPayload = Record<string, unknown>;
3
3
  interface RelayOptions {
4
4
  host: string;
@@ -7,9 +7,10 @@ interface RelayOptions {
7
7
  trackingId: string;
8
8
  }
9
9
  interface Relay {
10
- validate: (type: string, payload: RelayPayload) => Promise<ValidationReport>;
10
+ validate: (type: string, payload: RelayPayload) => Promise<ValidationResponse>;
11
+ emit: (type: string, payload: RelayPayload) => Promise<void>;
11
12
  version: string;
12
13
  }
13
14
  export declare function createRelay(options: RelayOptions): Relay;
14
- export type { RelayPayload, RelayOptions, ServiceErrorResponse, ValidationError, ValidationReport, ValidationResponse, };
15
+ export type { RelayPayload, RelayOptions, ValidationError, ValidationResponse };
15
16
  //# sourceMappingURL=relay.d.ts.map
@@ -1 +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"}
1
+ {"version":3,"file":"relay.d.ts","sourceRoot":"","sources":["../../src/relay.ts"],"names":[],"mappings":"AAIA,OAAO,EAEL,eAAe,EACf,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,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,MAAM,CAAC;CACjB;AAED,wBAAgB,WAAW,CAAC,OAAO,EAAE,YAAY,GAAG,KAAK,CA+BxD;AAED,YAAY,EAAE,YAAY,EAAE,YAAY,EAAE,eAAe,EAAE,kBAAkB,EAAE,CAAC"}
@@ -1,11 +1,4 @@
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
- }
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 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 {};
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,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"}
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.1.0",
3
+ "version": "0.1.2",
4
4
  "description": "A library for sending analytics events using Coveo's Event protocol.",
5
5
  "files": [
6
6
  "lib/**/*"
@@ -48,8 +48,8 @@
48
48
  "rollup": "^3.28.0",
49
49
  "ts-jest": "^29.1.1",
50
50
  "typescript": "^5.1.6",
51
- "tsconfig": "0.0.0",
52
- "eslint-config-custom": "0.0.0"
51
+ "eslint-config-custom": "0.0.0",
52
+ "tsconfig": "0.0.0"
53
53
  },
54
54
  "dependencies": {
55
55
  "uuid": "^9.0.0"
@@ -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
  }