@knocklabs/node 0.2.2 → 0.4.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.
Files changed (30) hide show
  1. package/README.md +18 -12
  2. package/dist/package.json +1 -1
  3. package/dist/src/common/interfaces.d.ts +8 -0
  4. package/dist/src/knock.d.ts +6 -2
  5. package/dist/src/knock.js +8 -3
  6. package/dist/src/resources/bulk_operations/index.d.ts +7 -0
  7. package/dist/src/resources/{identify → bulk_operations}/index.js +12 -10
  8. package/dist/src/resources/bulk_operations/interfaces.d.ts +12 -0
  9. package/dist/src/resources/{identify → bulk_operations}/interfaces.js +0 -0
  10. package/dist/src/resources/objects/index.d.ts +15 -0
  11. package/dist/src/resources/objects/index.js +66 -0
  12. package/dist/src/resources/objects/interfaces.d.ts +21 -0
  13. package/dist/src/resources/{notify → objects}/interfaces.js +0 -0
  14. package/dist/src/resources/preferences/helpers.d.ts +7 -0
  15. package/dist/src/resources/preferences/helpers.js +11 -0
  16. package/dist/src/resources/preferences/index.d.ts +29 -1
  17. package/dist/src/resources/preferences/index.js +37 -33
  18. package/dist/src/resources/preferences/interfaces.d.ts +5 -6
  19. package/dist/src/resources/users/index.d.ts +22 -4
  20. package/dist/src/resources/users/index.js +125 -0
  21. package/dist/src/resources/users/interfaces.d.ts +21 -0
  22. package/dist/src/resources/workflows/index.d.ts +1 -1
  23. package/dist/src/resources/workflows/index.js +2 -1
  24. package/dist/src/resources/workflows/interfaces.d.ts +5 -3
  25. package/package.json +1 -1
  26. package/dist/src/resources/identify/index.d.ts +0 -3
  27. package/dist/src/resources/identify/interfaces.d.ts +0 -5
  28. package/dist/src/resources/notify/index.d.ts +0 -2
  29. package/dist/src/resources/notify/index.js +0 -8
  30. package/dist/src/resources/notify/interfaces.d.ts +0 -11
package/README.md CHANGED
@@ -61,6 +61,8 @@ await knockClient.notify("dinosaurs-loose", {
61
61
  type: "trex",
62
62
  priority: 1,
63
63
  },
64
+ // an optional identifier for the tenant that the notifications belong to
65
+ tenant: "jurassic-park",
64
66
  // an optional key to provide to cancel a notify
65
67
  cancellationKey: triggerAlert.id,
66
68
  });
@@ -91,7 +93,7 @@ const { Knock } = require("@knocklabs/node");
91
93
  const knockClient = new Knock("sk_12345");
92
94
 
93
95
  // Set an entire preference set
94
- await knockClient.preferences.set("jhammond", {
96
+ await knockClient.users.setPreferences("jhammond", {
95
97
  channel_types: { email: true, sms: false },
96
98
  workflows: {
97
99
  "dinosaurs-loose": {
@@ -101,21 +103,25 @@ await knockClient.preferences.set("jhammond", {
101
103
  });
102
104
 
103
105
  // Retrieve a whole preference set
104
- const preferences = await knockClient.preferences.get("jhammond");
106
+ const preferences = await knockClient.users.getPreferences("jhammond");
107
+ ```
105
108
 
106
- // Granular preference setting for channel types
107
- await knockClient.preferences.setChannelType("jhammond", "email", false);
109
+ ### Getting and setting channel data
108
110
 
109
- // Granular preference setting for workflows
110
- await knockClient.preferences.setWorkflow("jhammond", "dinosaurs-loose", {
111
- channel_types: {
112
- email: true,
113
- in_app_feed: false,
114
- },
111
+ ```javascript
112
+ const { Knock } = require("@knocklabs/node");
113
+ const knockClient = new Knock("sk_12345");
114
+
115
+ // Setting channel data for an APNS
116
+ await knockClient.users.setChannelData("jhammond", KNOCK_APNS_CHANNEL_ID, {
117
+ tokens: [apnsToken],
115
118
  });
119
+
120
+ // Getting channel data for the APNS channel
121
+ await knockClient.users.getChannelData("jhammond", KNOCK_APNS_CHANNEL_ID);
116
122
  ```
117
123
 
118
- ### Cancelling workflows
124
+ ### Canceling workflows
119
125
 
120
126
  ```javascript
121
127
  const { Knock } = require("@knocklabs/node");
@@ -133,7 +139,7 @@ You can use the `jsonwebtoken` package to [sign JWTs easily](https://www.npmjs.c
133
139
  You will need to generate an environment specific signing key, which you can find in the Knock dashboard.
134
140
 
135
141
  If you're using a signing token you will need to pass this to your client to perform authentication.
136
- You can read more about [clientside authentication here](https://docs.knock.app/client-integration/authenticating-users).
142
+ You can read more about [client-side authentication here](https://docs.knock.app/client-integration/authenticating-users).
137
143
 
138
144
  ```javascript
139
145
  const jwt = require("jsonwebtoken");
package/dist/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@knocklabs/node",
3
- "version": "0.2.2",
3
+ "version": "0.4.0",
4
4
  "description": "Library for interacting with the Knock API",
5
5
  "homepage": "https://github.com/knocklabs/knock-node",
6
6
  "author": "@knocklabs",
@@ -15,3 +15,11 @@ export interface UnprocessableEntityError {
15
15
  type: string;
16
16
  field: string;
17
17
  }
18
+ export declare type ChannelType = "email" | "in_app_feed" | "sms" | "push" | "chat";
19
+ export declare type CommonMetadata = Record<string, any>;
20
+ export interface ChannelData<T = CommonMetadata> {
21
+ channel_id: string;
22
+ data: T;
23
+ }
24
+ export interface SetChannelDataProperties {
25
+ }
@@ -1,9 +1,11 @@
1
- import { AxiosError, AxiosResponse } from "axios";
1
+ import { AxiosResponse } from "axios";
2
2
  import { KnockOptions, PostAndPutOptions } from "./common/interfaces";
3
3
  import { Users } from "./resources/users";
4
4
  import { Preferences } from "./resources/preferences";
5
5
  import { Workflows } from "./resources/workflows";
6
6
  import { TriggerWorkflowProperties } from "./resources/workflows/interfaces";
7
+ import { BulkOperations } from "./resources/bulk_operations";
8
+ import { Objects } from "./resources/objects";
7
9
  declare class Knock {
8
10
  readonly key?: string | undefined;
9
11
  readonly options: KnockOptions;
@@ -12,13 +14,15 @@ declare class Knock {
12
14
  readonly users: Users;
13
15
  readonly preferences: Preferences;
14
16
  readonly workflows: Workflows;
17
+ readonly bulkOperations: BulkOperations;
18
+ readonly objects: Objects;
15
19
  constructor(key?: string | undefined, options?: KnockOptions);
16
20
  notify(workflowKey: string, properties: TriggerWorkflowProperties): Promise<import("./resources/workflows/interfaces").WorkflowRun>;
17
21
  post(path: string, entity: any, options?: PostAndPutOptions): Promise<AxiosResponse>;
18
22
  put(path: string, entity: any, options?: PostAndPutOptions): Promise<AxiosResponse>;
19
23
  delete(path: string, entity?: any): Promise<AxiosResponse>;
20
24
  get(path: string, query?: any): Promise<AxiosResponse>;
21
- handleErrorResponse(path: string, { response }: AxiosError): void;
25
+ handleErrorResponse(path: string, error: any): void;
22
26
  emitWarning(warning: string): void;
23
27
  }
24
28
  export { Knock };
package/dist/src/knock.js CHANGED
@@ -19,14 +19,19 @@ const exceptions_1 = require("./common/exceptions");
19
19
  const users_1 = require("./resources/users");
20
20
  const preferences_1 = require("./resources/preferences");
21
21
  const workflows_1 = require("./resources/workflows");
22
+ const bulk_operations_1 = require("./resources/bulk_operations");
23
+ const objects_1 = require("./resources/objects");
22
24
  const DEFAULT_HOSTNAME = "https://api.knock.app";
23
25
  class Knock {
24
26
  constructor(key, options = {}) {
25
27
  this.key = key;
26
28
  this.options = options;
29
+ // Service accessors
27
30
  this.users = new users_1.Users(this);
28
31
  this.preferences = new preferences_1.Preferences(this);
29
32
  this.workflows = new workflows_1.Workflows(this);
33
+ this.bulkOperations = new bulk_operations_1.BulkOperations(this);
34
+ this.objects = new objects_1.Objects(this);
30
35
  if (!key) {
31
36
  this.key = process.env.KNOCK_API_KEY;
32
37
  if (!this.key) {
@@ -100,9 +105,9 @@ class Knock {
100
105
  }
101
106
  });
102
107
  }
103
- handleErrorResponse(path, { response }) {
104
- if (response) {
105
- const { status, data, headers } = response;
108
+ handleErrorResponse(path, error) {
109
+ if (axios_1.default.isAxiosError(error) && error.response) {
110
+ const { status, data, headers } = error.response;
106
111
  const requestID = headers["X-Request-ID"];
107
112
  switch (status) {
108
113
  case 401: {
@@ -0,0 +1,7 @@
1
+ import { Knock } from "../../knock";
2
+ import { BulkOperation } from "../bulk_operations/interfaces";
3
+ export declare class BulkOperations {
4
+ readonly knock: Knock;
5
+ constructor(knock: Knock);
6
+ get(id: string): Promise<BulkOperation>;
7
+ }
@@ -9,14 +9,16 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
9
  });
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.identify = void 0;
13
- function identify(knock) {
14
- return (userId, properties = {}) => __awaiter(this, void 0, void 0, function* () {
15
- if (!userId) {
16
- throw new Error(`Incomplete arguments. At a minimum you need to specify 'userId'.`);
17
- }
18
- const { data } = yield knock.put(`/v1/users/${userId}`, properties);
19
- return data;
20
- });
12
+ exports.BulkOperations = void 0;
13
+ class BulkOperations {
14
+ constructor(knock) {
15
+ this.knock = knock;
16
+ }
17
+ get(id) {
18
+ return __awaiter(this, void 0, void 0, function* () {
19
+ const { data } = yield this.knock.get(`/v1/bulk_operations/${id}`);
20
+ return data;
21
+ });
22
+ }
21
23
  }
22
- exports.identify = identify;
24
+ exports.BulkOperations = BulkOperations;
@@ -0,0 +1,12 @@
1
+ export interface BulkOperation {
2
+ id: string;
3
+ name: string;
4
+ status: "queued" | "processing" | "completed" | "failed";
5
+ processed_rows: number;
6
+ estimated_total_rows: number;
7
+ started_at?: string;
8
+ completed_at?: string;
9
+ failed_at?: string;
10
+ inserted_at: string;
11
+ updated_at: string;
12
+ }
@@ -0,0 +1,15 @@
1
+ import { ChannelData, CommonMetadata, SetChannelDataProperties } from "../../common/interfaces";
2
+ import { Knock } from "../../knock";
3
+ import { BulkSetObjectOption, Object, SetObjectProperties } from "./interfaces";
4
+ import { BulkOperation } from "../bulk_operations/interfaces";
5
+ export declare class Objects {
6
+ readonly knock: Knock;
7
+ constructor(knock: Knock);
8
+ get<T = CommonMetadata>(collection: string, id: string): Promise<Object<T>>;
9
+ set<T = CommonMetadata>(collection: string, id: string, properties: SetObjectProperties): Promise<Object<T>>;
10
+ bulkSet(collection: string, objects: BulkSetObjectOption[]): Promise<BulkOperation>;
11
+ delete(collection: string, id: string): Promise<null>;
12
+ bulkDelete(collection: string, objectIds: string[]): Promise<BulkOperation>;
13
+ getChannelData<T = CommonMetadata>(collection: string, id: string, channelId: string): Promise<ChannelData<T>>;
14
+ setChannelData<T = CommonMetadata>(collection: string, id: string, channelId: string, channelData: SetChannelDataProperties): Promise<ChannelData<T>>;
15
+ }
@@ -0,0 +1,66 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.Objects = void 0;
13
+ class Objects {
14
+ constructor(knock) {
15
+ this.knock = knock;
16
+ }
17
+ get(collection, id) {
18
+ return __awaiter(this, void 0, void 0, function* () {
19
+ const { data } = yield this.knock.get(`/v1/objects/${collection}/${id}`);
20
+ return data;
21
+ });
22
+ }
23
+ set(collection, id, properties) {
24
+ return __awaiter(this, void 0, void 0, function* () {
25
+ const { data } = yield this.knock.put(`/v1/objects/${collection}/${id}`, properties);
26
+ return data;
27
+ });
28
+ }
29
+ bulkSet(collection, objects) {
30
+ return __awaiter(this, void 0, void 0, function* () {
31
+ const attrs = { objects };
32
+ const { data } = yield this.knock.post(`/v1/objects/${collection}/bulk/set`, attrs);
33
+ return data;
34
+ });
35
+ }
36
+ delete(collection, id) {
37
+ return __awaiter(this, void 0, void 0, function* () {
38
+ const { data } = yield this.knock.delete(`/v1/objects/${collection}/${id}`);
39
+ return data;
40
+ });
41
+ }
42
+ bulkDelete(collection, objectIds) {
43
+ return __awaiter(this, void 0, void 0, function* () {
44
+ const attrs = {
45
+ object_ids: objectIds,
46
+ };
47
+ const { data } = yield this.knock.post(`/v1/objects/${collection}/bulk/delete`, attrs);
48
+ return data;
49
+ });
50
+ }
51
+ // Channel data
52
+ getChannelData(collection, id, channelId) {
53
+ return __awaiter(this, void 0, void 0, function* () {
54
+ const { data } = yield this.knock.get(`/v1/objects/${collection}/${id}/channel_data/${channelId}`);
55
+ return data;
56
+ });
57
+ }
58
+ setChannelData(collection, id, channelId, channelData) {
59
+ return __awaiter(this, void 0, void 0, function* () {
60
+ const attrs = { data: channelData };
61
+ const { data } = yield this.knock.put(`/v1/objects/${collection}/${id}/channel_data/${channelId}`, attrs);
62
+ return data;
63
+ });
64
+ }
65
+ }
66
+ exports.Objects = Objects;
@@ -0,0 +1,21 @@
1
+ import { CommonMetadata } from "../../common/interfaces";
2
+ export interface ObjectRef {
3
+ collection: string;
4
+ id: string;
5
+ }
6
+ export interface Object<T = CommonMetadata> {
7
+ id: string;
8
+ collection: string;
9
+ properties: T;
10
+ created_at?: string;
11
+ updated_at: string;
12
+ }
13
+ export interface SetObjectProperties {
14
+ name?: string;
15
+ [key: string]: any;
16
+ }
17
+ export interface BulkSetObjectOption {
18
+ id: string;
19
+ name?: string;
20
+ [key: string]: any;
21
+ }
@@ -0,0 +1,7 @@
1
+ import { WorkflowPreferenceSetting } from "./interfaces";
2
+ export declare function buildUpdateParam(param: WorkflowPreferenceSetting): {
3
+ channel_types: import("./interfaces").ChannelTypePreferences;
4
+ } | {
5
+ subscribed: boolean;
6
+ };
7
+ export declare const DEFAULT_PREFERENCE_SET_ID = "default";
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DEFAULT_PREFERENCE_SET_ID = exports.buildUpdateParam = void 0;
4
+ function buildUpdateParam(param) {
5
+ if (typeof param === "object") {
6
+ return param;
7
+ }
8
+ return { subscribed: param };
9
+ }
10
+ exports.buildUpdateParam = buildUpdateParam;
11
+ exports.DEFAULT_PREFERENCE_SET_ID = "default";
@@ -1,15 +1,43 @@
1
+ import { ChannelType } from "../../common/interfaces";
1
2
  import { Knock } from "../../knock";
2
- import { ChannelType, ChannelTypePreferences, WorkflowPreferenceSetting, WorkflowPreferences, PreferenceOptions, SetPreferencesProperties, PreferenceSet } from "./interfaces";
3
+ import { ChannelTypePreferences, WorkflowPreferenceSetting, WorkflowPreferences, PreferenceOptions, SetPreferencesProperties, PreferenceSet } from "./interfaces";
3
4
  export declare class Preferences {
4
5
  readonly knock: Knock;
5
6
  constructor(knock: Knock);
7
+ /**
8
+ * @deprecated Use `users.getAllPreferences` instead
9
+ */
6
10
  getAll(userId: string): Promise<PreferenceSet[]>;
11
+ /**
12
+ * @deprecated Use `users.getPreferences` instead
13
+ */
7
14
  get(userId: string, options?: PreferenceOptions): Promise<PreferenceSet>;
15
+ /**
16
+ * @deprecated Use `users.setPreferences` instead
17
+ */
8
18
  set(userId: string, preferenceSet: SetPreferencesProperties, options?: PreferenceOptions): Promise<PreferenceSet>;
19
+ /**
20
+ * @deprecated Use `users.setChannelTypesPreferences` instead
21
+ */
9
22
  setChannelTypes(userId: string, channelTypePreferences: ChannelTypePreferences, options?: PreferenceOptions): Promise<PreferenceSet>;
23
+ /**
24
+ * @deprecated Use `users.setChannelTypePreferences` instead
25
+ */
10
26
  setChannelType(userId: string, channelType: ChannelType, setting: boolean, options?: PreferenceOptions): Promise<PreferenceSet>;
27
+ /**
28
+ * @deprecated Use `users.setWorkflowsPreferences` instead
29
+ */
11
30
  setWorkflows(userId: string, workflowPreferences: WorkflowPreferences, options?: PreferenceOptions): Promise<PreferenceSet>;
31
+ /**
32
+ * @deprecated Use `users.setWorkflowPreferences` instead
33
+ */
12
34
  setWorkflow(userId: string, workflowKey: string, setting: WorkflowPreferenceSetting, options?: PreferenceOptions): Promise<PreferenceSet>;
35
+ /**
36
+ * @deprecated Use `users.setCategoriesPreferences` instead
37
+ */
13
38
  setCategories(userId: string, categoryPreferences: WorkflowPreferences, options?: PreferenceOptions): Promise<PreferenceSet>;
39
+ /**
40
+ * @deprecated Use `users.setCategoryPreferences` instead
41
+ */
14
42
  setCategory(userId: string, categoryKey: string, setting: WorkflowPreferenceSetting, options?: PreferenceOptions): Promise<PreferenceSet>;
15
43
  }
@@ -10,77 +10,81 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.Preferences = void 0;
13
- const DEFAULT_PREFERENCE_SET_ID = "default";
14
- function buildUpdateParam(param) {
15
- if (typeof param === "object") {
16
- return param;
17
- }
18
- return { subscribed: param };
19
- }
13
+ // Deprecated all of the methods in this to be removed in `0.5.0`
20
14
  class Preferences {
21
15
  constructor(knock) {
22
16
  this.knock = knock;
23
17
  }
18
+ /**
19
+ * @deprecated Use `users.getAllPreferences` instead
20
+ */
24
21
  getAll(userId) {
25
22
  return __awaiter(this, void 0, void 0, function* () {
26
- const { data } = yield this.knock.get(`/v1/users/${userId}/preferences`);
27
- return data;
23
+ return this.knock.users.getAllPreferences(userId);
28
24
  });
29
25
  }
26
+ /**
27
+ * @deprecated Use `users.getPreferences` instead
28
+ */
30
29
  get(userId, options = {}) {
31
30
  return __awaiter(this, void 0, void 0, function* () {
32
- const preferenceSetId = options.preferenceSet || DEFAULT_PREFERENCE_SET_ID;
33
- const { data } = yield this.knock.get(`/v1/users/${userId}/preferences/${preferenceSetId}`);
34
- return data;
31
+ return this.knock.users.getPrefences(userId, options);
35
32
  });
36
33
  }
34
+ /**
35
+ * @deprecated Use `users.setPreferences` instead
36
+ */
37
37
  set(userId, preferenceSet, options = {}) {
38
38
  return __awaiter(this, void 0, void 0, function* () {
39
- const preferenceSetId = options.preferenceSet || DEFAULT_PREFERENCE_SET_ID;
40
- const { data } = yield this.knock.put(`/v1/users/${userId}/preferences/${preferenceSetId}`, preferenceSet);
41
- return data;
39
+ return this.knock.users.setPreferences(userId, preferenceSet, options);
42
40
  });
43
41
  }
42
+ /**
43
+ * @deprecated Use `users.setChannelTypesPreferences` instead
44
+ */
44
45
  setChannelTypes(userId, channelTypePreferences, options = {}) {
45
46
  return __awaiter(this, void 0, void 0, function* () {
46
- const preferenceSetId = options.preferenceSet || DEFAULT_PREFERENCE_SET_ID;
47
- const { data } = yield this.knock.put(`/v1/users/${userId}/preferences/${preferenceSetId}/channel_types`, channelTypePreferences);
48
- return data;
47
+ return this.knock.users.setChannelTypesPreferences(userId, channelTypePreferences, options);
49
48
  });
50
49
  }
50
+ /**
51
+ * @deprecated Use `users.setChannelTypePreferences` instead
52
+ */
51
53
  setChannelType(userId, channelType, setting, options = {}) {
52
54
  return __awaiter(this, void 0, void 0, function* () {
53
- const preferenceSetId = options.preferenceSet || DEFAULT_PREFERENCE_SET_ID;
54
- const { data, } = yield this.knock.put(`/v1/users/${userId}/preferences/${preferenceSetId}/channel_types/${channelType}`, { subscribed: setting });
55
- return data;
55
+ return this.knock.users.setChannelTypePreferences(userId, channelType, setting, options);
56
56
  });
57
57
  }
58
+ /**
59
+ * @deprecated Use `users.setWorkflowsPreferences` instead
60
+ */
58
61
  setWorkflows(userId, workflowPreferences, options = {}) {
59
62
  return __awaiter(this, void 0, void 0, function* () {
60
- const preferenceSetId = options.preferenceSet || DEFAULT_PREFERENCE_SET_ID;
61
- const { data } = yield this.knock.put(`/v1/users/${userId}/preferences/${preferenceSetId}/workflows`, workflowPreferences);
62
- return data;
63
+ return this.knock.users.setWorkflowsPreferences(userId, workflowPreferences, options);
63
64
  });
64
65
  }
66
+ /**
67
+ * @deprecated Use `users.setWorkflowPreferences` instead
68
+ */
65
69
  setWorkflow(userId, workflowKey, setting, options = {}) {
66
70
  return __awaiter(this, void 0, void 0, function* () {
67
- const preferenceSetId = options.preferenceSet || DEFAULT_PREFERENCE_SET_ID;
68
- const { data } = yield this.knock.put(`/v1/users/${userId}/preferences/${preferenceSetId}/workflows/${workflowKey}`, buildUpdateParam(setting));
69
- return data;
71
+ return this.knock.users.setWorkflowPreferences(userId, workflowKey, setting, options);
70
72
  });
71
73
  }
74
+ /**
75
+ * @deprecated Use `users.setCategoriesPreferences` instead
76
+ */
72
77
  setCategories(userId, categoryPreferences, options = {}) {
73
78
  return __awaiter(this, void 0, void 0, function* () {
74
- const preferenceSetId = options.preferenceSet || DEFAULT_PREFERENCE_SET_ID;
75
- const { data } = yield this.knock.put(`/v1/users/${userId}/preferences/${preferenceSetId}/categories`, categoryPreferences);
76
- return data;
79
+ return this.knock.users.setCategoriesPreferences(userId, categoryPreferences, options);
77
80
  });
78
81
  }
82
+ /**
83
+ * @deprecated Use `users.setCategoryPreferences` instead
84
+ */
79
85
  setCategory(userId, categoryKey, setting, options = {}) {
80
86
  return __awaiter(this, void 0, void 0, function* () {
81
- const preferenceSetId = options.preferenceSet || DEFAULT_PREFERENCE_SET_ID;
82
- const { data } = yield this.knock.put(`/v1/users/${userId}/preferences/${preferenceSetId}/categories/${categoryKey}`, buildUpdateParam(setting));
83
- return data;
87
+ return this.knock.users.setCategoryPreferences(userId, categoryKey, setting, options);
84
88
  });
85
89
  }
86
90
  }
@@ -1,7 +1,6 @@
1
- export declare type ChannelType = "email" | "in_app_feed";
1
+ import { ChannelType } from "../../common/interfaces";
2
2
  export declare type ChannelTypePreferences = {
3
- email: boolean;
4
- in_app_feed: boolean;
3
+ [K in ChannelType]: boolean;
5
4
  };
6
5
  export declare type WorkflowPreferenceSetting = boolean | {
7
6
  channel_types: ChannelTypePreferences;
@@ -10,9 +9,9 @@ export interface WorkflowPreferences {
10
9
  [key: string]: WorkflowPreferenceSetting;
11
10
  }
12
11
  export interface SetPreferencesProperties {
13
- workflows: WorkflowPreferences;
14
- categories: WorkflowPreferences;
15
- channel_types: ChannelTypePreferences;
12
+ workflows?: WorkflowPreferences;
13
+ categories?: WorkflowPreferences;
14
+ channel_types?: ChannelTypePreferences;
16
15
  }
17
16
  export interface PreferenceSet {
18
17
  id: string;
@@ -1,9 +1,27 @@
1
+ import { ChannelData, ChannelType, CommonMetadata } from "../../common/interfaces";
2
+ import { BulkOperation } from "../bulk_operations/interfaces";
3
+ import { ChannelTypePreferences, PreferenceOptions, PreferenceSet, SetPreferencesProperties, WorkflowPreferences, WorkflowPreferenceSetting } from "../preferences/interfaces";
1
4
  import { Knock } from "../../knock";
2
- import { IdentifyProperties } from "./interfaces";
5
+ import { BulkIdentifyUser, IdentifyProperties, User, UserFeedOptions } from "./interfaces";
3
6
  export declare class Users {
4
7
  readonly knock: Knock;
5
8
  constructor(knock: Knock);
6
- identify(userId: string, properties?: IdentifyProperties): Promise<any>;
7
- get(userId: string): Promise<any>;
8
- delete(userId: string): Promise<any>;
9
+ identify(userId: string, properties?: IdentifyProperties): Promise<User>;
10
+ bulkIdentify(users: BulkIdentifyUser[]): Promise<BulkOperation>;
11
+ get(userId: string): Promise<User>;
12
+ delete(userId: string): Promise<null>;
13
+ bulkDelete(userIds: string[]): Promise<BulkOperation>;
14
+ getFeed(userId: string, channelId: string, feedOptions?: UserFeedOptions): Promise<any>;
15
+ getAllPreferences(userId: string): Promise<any>;
16
+ getPrefences(userId: string, options?: PreferenceOptions): Promise<PreferenceSet>;
17
+ setPreferences(userId: string, preferenceSet: SetPreferencesProperties, options?: PreferenceOptions): Promise<PreferenceSet>;
18
+ bulkSetPreferences(userIds: string[], preferenceSet: SetPreferencesProperties, options?: PreferenceOptions): Promise<BulkOperation>;
19
+ setChannelTypesPreferences(userId: string, channelTypePreferences: ChannelTypePreferences, options?: PreferenceOptions): Promise<PreferenceSet>;
20
+ setChannelTypePreferences(userId: string, channelType: ChannelType, setting: boolean, options?: PreferenceOptions): Promise<PreferenceSet>;
21
+ setWorkflowsPreferences(userId: string, workflowPreferences: WorkflowPreferences, options?: PreferenceOptions): Promise<PreferenceSet>;
22
+ setWorkflowPreferences(userId: string, workflowKey: string, setting: WorkflowPreferenceSetting, options?: PreferenceOptions): Promise<PreferenceSet>;
23
+ setCategoriesPreferences(userId: string, categoryPreferences: WorkflowPreferences, options?: PreferenceOptions): Promise<PreferenceSet>;
24
+ setCategoryPreferences(userId: string, categoryKey: string, setting: WorkflowPreferenceSetting, options?: PreferenceOptions): Promise<PreferenceSet>;
25
+ getChannelData<T = CommonMetadata>(userId: string, channelId: string): Promise<ChannelData<T>>;
26
+ setChannelData<T = CommonMetadata>(userId: string, channelId: string, channelData: Record<string, string>): Promise<ChannelData<T>>;
9
27
  }
@@ -10,10 +10,14 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.Users = void 0;
13
+ const helpers_1 = require("../preferences/helpers");
13
14
  class Users {
14
15
  constructor(knock) {
15
16
  this.knock = knock;
16
17
  }
18
+ //
19
+ // User management
20
+ //
17
21
  identify(userId, properties = {}) {
18
22
  return __awaiter(this, void 0, void 0, function* () {
19
23
  if (!userId) {
@@ -23,6 +27,13 @@ class Users {
23
27
  return data;
24
28
  });
25
29
  }
30
+ bulkIdentify(users) {
31
+ return __awaiter(this, void 0, void 0, function* () {
32
+ const attrs = { users };
33
+ const { data } = yield this.knock.post(`/v1/users/bulk/identify`, attrs);
34
+ return data;
35
+ });
36
+ }
26
37
  get(userId) {
27
38
  return __awaiter(this, void 0, void 0, function* () {
28
39
  if (!userId) {
@@ -41,5 +52,119 @@ class Users {
41
52
  return data;
42
53
  });
43
54
  }
55
+ bulkDelete(userIds) {
56
+ return __awaiter(this, void 0, void 0, function* () {
57
+ const attrs = { user_ids: userIds };
58
+ const { data } = yield this.knock.post(`/v1/users/bulk/delete`, attrs);
59
+ return data;
60
+ });
61
+ }
62
+ //
63
+ // Feeds
64
+ //
65
+ getFeed(userId, channelId, feedOptions = {}) {
66
+ return __awaiter(this, void 0, void 0, function* () {
67
+ const { data } = yield this.knock.get(`/v1/users/${userId}/feeds/${channelId}`, feedOptions);
68
+ return data;
69
+ });
70
+ }
71
+ //
72
+ // Preferences
73
+ //
74
+ getAllPreferences(userId) {
75
+ return __awaiter(this, void 0, void 0, function* () {
76
+ const { data } = yield this.knock.get(`/v1/users/${userId}/preferences`);
77
+ return data;
78
+ });
79
+ }
80
+ getPrefences(userId, options = {}) {
81
+ return __awaiter(this, void 0, void 0, function* () {
82
+ const preferenceSetId = options.preferenceSet || helpers_1.DEFAULT_PREFERENCE_SET_ID;
83
+ const { data } = yield this.knock.get(`/v1/users/${userId}/preferences/${preferenceSetId}`);
84
+ return data;
85
+ });
86
+ }
87
+ setPreferences(userId, preferenceSet, options = {}) {
88
+ return __awaiter(this, void 0, void 0, function* () {
89
+ const preferenceSetId = options.preferenceSet || helpers_1.DEFAULT_PREFERENCE_SET_ID;
90
+ const { data } = yield this.knock.put(`/v1/users/${userId}/preferences/${preferenceSetId}`, preferenceSet);
91
+ return data;
92
+ });
93
+ }
94
+ bulkSetPreferences(userIds, preferenceSet, options = {}) {
95
+ return __awaiter(this, void 0, void 0, function* () {
96
+ const preferenceSetId = options.preferenceSet || helpers_1.DEFAULT_PREFERENCE_SET_ID;
97
+ const attrs = {
98
+ user_ids: userIds,
99
+ preferences: Object.assign(Object.assign({}, preferenceSet), { id: preferenceSetId }),
100
+ };
101
+ const { data } = yield this.knock.post(`/v1/users/bulk/preferences`, attrs);
102
+ return data;
103
+ });
104
+ }
105
+ setChannelTypesPreferences(userId, channelTypePreferences, options = {}) {
106
+ return __awaiter(this, void 0, void 0, function* () {
107
+ const preferenceSetId = options.preferenceSet || helpers_1.DEFAULT_PREFERENCE_SET_ID;
108
+ const { data } = yield this.knock.put(`/v1/users/${userId}/preferences/${preferenceSetId}/channel_types`, channelTypePreferences);
109
+ return data;
110
+ });
111
+ }
112
+ setChannelTypePreferences(userId, channelType, setting, options = {}) {
113
+ return __awaiter(this, void 0, void 0, function* () {
114
+ const preferenceSetId = options.preferenceSet || helpers_1.DEFAULT_PREFERENCE_SET_ID;
115
+ const { data, } = yield this.knock.put(`/v1/users/${userId}/preferences/${preferenceSetId}/channel_types/${channelType}`, { subscribed: setting });
116
+ return data;
117
+ });
118
+ }
119
+ setWorkflowsPreferences(userId, workflowPreferences, options = {}) {
120
+ return __awaiter(this, void 0, void 0, function* () {
121
+ const preferenceSetId = options.preferenceSet || helpers_1.DEFAULT_PREFERENCE_SET_ID;
122
+ const { data } = yield this.knock.put(`/v1/users/${userId}/preferences/${preferenceSetId}/workflows`, workflowPreferences);
123
+ return data;
124
+ });
125
+ }
126
+ setWorkflowPreferences(userId, workflowKey, setting, options = {}) {
127
+ return __awaiter(this, void 0, void 0, function* () {
128
+ const preferenceSetId = options.preferenceSet || helpers_1.DEFAULT_PREFERENCE_SET_ID;
129
+ const { data } = yield this.knock.put(`/v1/users/${userId}/preferences/${preferenceSetId}/workflows/${workflowKey}`, helpers_1.buildUpdateParam(setting));
130
+ return data;
131
+ });
132
+ }
133
+ setCategoriesPreferences(userId, categoryPreferences, options = {}) {
134
+ return __awaiter(this, void 0, void 0, function* () {
135
+ const preferenceSetId = options.preferenceSet || helpers_1.DEFAULT_PREFERENCE_SET_ID;
136
+ const { data } = yield this.knock.put(`/v1/users/${userId}/preferences/${preferenceSetId}/categories`, categoryPreferences);
137
+ return data;
138
+ });
139
+ }
140
+ setCategoryPreferences(userId, categoryKey, setting, options = {}) {
141
+ return __awaiter(this, void 0, void 0, function* () {
142
+ const preferenceSetId = options.preferenceSet || helpers_1.DEFAULT_PREFERENCE_SET_ID;
143
+ const { data } = yield this.knock.put(`/v1/users/${userId}/preferences/${preferenceSetId}/categories/${categoryKey}`, helpers_1.buildUpdateParam(setting));
144
+ return data;
145
+ });
146
+ }
147
+ //
148
+ // Channel data
149
+ //
150
+ getChannelData(userId, channelId) {
151
+ return __awaiter(this, void 0, void 0, function* () {
152
+ if (!userId || !channelId) {
153
+ throw new Error(`Incomplete arguments. You must provide a 'userId' and a 'channelId'`);
154
+ }
155
+ const { data } = yield this.knock.get(`/v1/users/${userId}/channel_data/${channelId}`);
156
+ return data;
157
+ });
158
+ }
159
+ setChannelData(userId, channelId, channelData) {
160
+ return __awaiter(this, void 0, void 0, function* () {
161
+ if (!userId || !channelId) {
162
+ throw new Error(`Incomplete arguments. You must provide a 'userId' and a 'channelId'`);
163
+ }
164
+ const args = { data: channelData };
165
+ const { data } = yield this.knock.put(`/v1/users/${userId}/channel_data/${channelId}`, args);
166
+ return data;
167
+ });
168
+ }
44
169
  }
45
170
  exports.Users = Users;
@@ -3,3 +3,24 @@ export interface IdentifyProperties {
3
3
  email?: string;
4
4
  [key: string]: any;
5
5
  }
6
+ export interface User {
7
+ id: string;
8
+ name: string;
9
+ email: string;
10
+ phone_number?: string;
11
+ avatar?: string;
12
+ created_at?: string;
13
+ updated_at?: string;
14
+ [key: string]: any;
15
+ }
16
+ export interface BulkIdentifyUser extends IdentifyProperties {
17
+ id: string;
18
+ }
19
+ export interface UserFeedOptions {
20
+ page_size?: number;
21
+ after?: string;
22
+ before?: string;
23
+ source?: string;
24
+ tenant?: string;
25
+ status?: "unread" | "unseen" | "all";
26
+ }
@@ -3,6 +3,6 @@ import { CancelWorkflowProperties, TriggerWorkflowProperties, WorkflowRun } from
3
3
  export declare class Workflows {
4
4
  readonly knock: Knock;
5
5
  constructor(knock: Knock);
6
- trigger(workflowKey: string, { actor, recipients, cancellationKey, data: notifyData, }: TriggerWorkflowProperties): Promise<WorkflowRun>;
6
+ trigger(workflowKey: string, { actor, recipients, cancellationKey, tenant, data: notifyData, }: TriggerWorkflowProperties): Promise<WorkflowRun>;
7
7
  cancel(workflowKey: string, cancellationKey: string, { recipients }?: CancelWorkflowProperties): Promise<any>;
8
8
  }
@@ -14,7 +14,7 @@ class Workflows {
14
14
  constructor(knock) {
15
15
  this.knock = knock;
16
16
  }
17
- trigger(workflowKey, { actor, recipients, cancellationKey, data: notifyData, }) {
17
+ trigger(workflowKey, { actor, recipients, cancellationKey, tenant, data: notifyData, }) {
18
18
  return __awaiter(this, void 0, void 0, function* () {
19
19
  if (!workflowKey && !actor && !recipients) {
20
20
  throw new Error(`Incomplete arguments. At a minimum you need to specify 'workflowKey', 'actor', and 'recipients'.`);
@@ -23,6 +23,7 @@ class Workflows {
23
23
  actor,
24
24
  recipients,
25
25
  cancellation_key: cancellationKey,
26
+ tenant,
26
27
  data: notifyData,
27
28
  });
28
29
  return data;
@@ -1,13 +1,15 @@
1
+ import { ObjectRef } from "../objects/interfaces";
1
2
  export interface TriggerWorkflowProperties {
2
- actor: string;
3
- recipients?: string[];
3
+ actor?: string | ObjectRef;
4
+ recipients?: string[] | ObjectRef[];
4
5
  cancellationKey?: string;
6
+ tenant?: string;
5
7
  data?: {
6
8
  [key: string]: any;
7
9
  };
8
10
  }
9
11
  export interface CancelWorkflowProperties {
10
- recipients?: string[];
12
+ recipients?: string[] | ObjectRef;
11
13
  }
12
14
  export interface WorkflowRun {
13
15
  workflow_run_id: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@knocklabs/node",
3
- "version": "0.2.2",
3
+ "version": "0.4.0",
4
4
  "description": "Library for interacting with the Knock API",
5
5
  "homepage": "https://github.com/knocklabs/knock-node",
6
6
  "author": "@knocklabs",
@@ -1,3 +0,0 @@
1
- import { Knock } from "../../knock";
2
- import { IdentifyProperties } from "./interfaces";
3
- export declare function identify(knock: Knock): (userId: string, properties?: IdentifyProperties) => Promise<any>;
@@ -1,5 +0,0 @@
1
- export interface IdentifyProperties {
2
- name?: string;
3
- email?: string;
4
- [key: string]: any;
5
- }
@@ -1,2 +0,0 @@
1
- import { Knock } from "../../knock";
2
- export declare function notify(knock: Knock): (workflowKey: string, { actor, recipients, cancellationKey, data: notifyData, }: import("../..").TriggerWorkflowProperties) => Promise<import("../..").WorkflowRun>;
@@ -1,8 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.notify = void 0;
4
- function notify(knock) {
5
- // This is just an alias to triggering the workflow
6
- return knock.workflows.trigger;
7
- }
8
- exports.notify = notify;
@@ -1,11 +0,0 @@
1
- export interface NotifyProperties {
2
- actor: string;
3
- recipients?: string[];
4
- cancelationKey?: string;
5
- data?: {
6
- [key: string]: any;
7
- };
8
- }
9
- export interface CancelNotifyProperties {
10
- recipients?: string[];
11
- }