@knocklabs/client 0.9.2 → 0.9.4
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/CHANGELOG.md +12 -0
- package/dist/cjs/clients/users/index.js +1 -1
- package/dist/cjs/clients/users/index.js.map +1 -1
- package/dist/esm/clients/users/index.mjs +15 -0
- package/dist/esm/clients/users/index.mjs.map +1 -1
- package/dist/types/clients/users/index.d.ts +3 -1
- package/dist/types/clients/users/index.d.ts.map +1 -1
- package/package.json +4 -1
- package/src/api.ts +110 -0
- package/src/clients/feed/feed.ts +774 -0
- package/src/clients/feed/index.ts +39 -0
- package/src/clients/feed/interfaces.ts +105 -0
- package/src/clients/feed/store.ts +93 -0
- package/src/clients/feed/types.ts +64 -0
- package/src/clients/feed/utils.ts +23 -0
- package/src/clients/objects/constants.ts +1 -0
- package/src/clients/objects/index.ts +61 -0
- package/src/clients/preferences/index.ts +196 -0
- package/src/clients/preferences/interfaces.ts +34 -0
- package/src/clients/slack/index.ts +89 -0
- package/src/clients/slack/interfaces.ts +36 -0
- package/src/clients/users/index.ts +110 -0
- package/src/clients/users/interfaces.ts +8 -0
- package/src/index.ts +16 -0
- package/src/interfaces.ts +52 -0
- package/src/knock.ts +182 -0
- package/src/networkStatus.ts +19 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.9.4
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- f58371c: Added user get and identify methods
|
|
8
|
+
|
|
9
|
+
## 0.9.3
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- bc69618: Add react-native to package.json files to fix a bug in our React Native SDK
|
|
14
|
+
|
|
3
15
|
## 0.9.2
|
|
4
16
|
|
|
5
17
|
### Patch Changes
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var c=Object.defineProperty;var u=(s,e,t)=>e in s?c(s,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):s[e]=t;var a=(s,e,t)=>(u(s,typeof e!="symbol"?e+"":e,t),t);Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const r="default";class l{constructor(e){a(this,"instance");this.instance=e}async get(){const e=await this.instance.client().makeRequest({method:"GET",url:`/v1/users/${this.instance.userId}`});return this.handleResponse(e)}async identify(e={}){const t=await this.instance.client().makeRequest({method:"PUT",url:`/v1/users/${this.instance.userId}`,params:e});return this.handleResponse(t)}async getAllPreferences(){const e=await this.instance.client().makeRequest({method:"GET",url:`/v1/users/${this.instance.userId}/preferences`});return this.handleResponse(e)}async getPreferences(e={}){const t=e.preferenceSet||r,n=await this.instance.client().makeRequest({method:"GET",url:`/v1/users/${this.instance.userId}/preferences/${t}`,params:{tenant:e.tenant}});return this.handleResponse(n)}async setPreferences(e,t={}){const n=t.preferenceSet||r,i=await this.instance.client().makeRequest({method:"PUT",url:`/v1/users/${this.instance.userId}/preferences/${n}`,data:e});return this.handleResponse(i)}async getChannelData(e){const t=await this.instance.client().makeRequest({method:"GET",url:`/v1/users/${this.instance.userId}/channel_data/${e.channelId}`});return this.handleResponse(t)}async setChannelData({channelId:e,channelData:t}){const n=await this.instance.client().makeRequest({method:"PUT",url:`/v1/users/${this.instance.userId}/channel_data/${e}`,data:{data:t}});return this.handleResponse(n)}handleResponse(e){if(e.statusCode==="error")throw new Error(e.error||e.body);return e.body}}exports.default=l;
|
|
2
2
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../../../../src/clients/users/index.ts"],"sourcesContent":["import { ApiResponse } from \"../../api\";\nimport { ChannelData } from \"../../interfaces\";\nimport Knock from \"../../knock\";\nimport {\n GetPreferencesOptions,\n PreferenceOptions,\n PreferenceSet,\n SetPreferencesProperties,\n} from \"../preferences/interfaces\";\nimport { GetChannelDataInput, SetChannelDataInput } from \"./interfaces\";\n\nconst DEFAULT_PREFERENCE_SET_ID = \"default\";\n\nclass UserClient {\n private instance: Knock;\n\n constructor(instance: Knock) {\n this.instance = instance;\n }\n\n async getAllPreferences() {\n const result = await this.instance.client().makeRequest({\n method: \"GET\",\n url: `/v1/users/${this.instance.userId}/preferences`,\n });\n\n return this.handleResponse<PreferenceSet[]>(result);\n }\n\n async getPreferences(\n options: GetPreferencesOptions = {},\n ): Promise<PreferenceSet> {\n const preferenceSetId = options.preferenceSet || DEFAULT_PREFERENCE_SET_ID;\n\n const result = await this.instance.client().makeRequest({\n method: \"GET\",\n url: `/v1/users/${this.instance.userId}/preferences/${preferenceSetId}`,\n params: { tenant: options.tenant },\n });\n\n return this.handleResponse<PreferenceSet>(result);\n }\n\n async setPreferences(\n preferenceSet: SetPreferencesProperties,\n options: PreferenceOptions = {},\n ): Promise<PreferenceSet> {\n const preferenceSetId = options.preferenceSet || DEFAULT_PREFERENCE_SET_ID;\n\n const result = await this.instance.client().makeRequest({\n method: \"PUT\",\n url: `/v1/users/${this.instance.userId}/preferences/${preferenceSetId}`,\n data: preferenceSet,\n });\n\n return this.handleResponse<PreferenceSet>(result);\n }\n\n async getChannelData<T = any>(params: GetChannelDataInput) {\n const result = await this.instance.client().makeRequest({\n method: \"GET\",\n url: `/v1/users/${this.instance.userId}/channel_data/${params.channelId}`,\n });\n\n return this.handleResponse<ChannelData<T>>(result);\n }\n\n async setChannelData<T = any>({\n channelId,\n channelData,\n }: SetChannelDataInput) {\n const result = await this.instance.client().makeRequest({\n method: \"PUT\",\n url: `/v1/users/${this.instance.userId}/channel_data/${channelId}`,\n data: { data: channelData },\n });\n\n return this.handleResponse<ChannelData<T>>(result);\n }\n\n private handleResponse<T>(response: ApiResponse) {\n if (response.statusCode === \"error\") {\n throw new Error(response.error || response.body);\n }\n\n return response.body as T;\n }\n}\n\nexport default UserClient;\n"],"names":["DEFAULT_PREFERENCE_SET_ID","UserClient","instance","__publicField","result","options","preferenceSetId","preferenceSet","params","channelId","channelData","response"],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../../../../src/clients/users/index.ts"],"sourcesContent":["import { ApiResponse } from \"../../api\";\nimport { ChannelData, User } from \"../../interfaces\";\nimport Knock from \"../../knock\";\nimport {\n GetPreferencesOptions,\n PreferenceOptions,\n PreferenceSet,\n SetPreferencesProperties,\n} from \"../preferences/interfaces\";\n\nimport { GetChannelDataInput, SetChannelDataInput } from \"./interfaces\";\n\nconst DEFAULT_PREFERENCE_SET_ID = \"default\";\n\nclass UserClient {\n private instance: Knock;\n\n constructor(instance: Knock) {\n this.instance = instance;\n }\n\n async get() {\n const result = await this.instance.client().makeRequest({\n method: \"GET\",\n url: `/v1/users/${this.instance.userId}`,\n });\n\n return this.handleResponse<User>(result);\n }\n\n async identify(props: Record<string, any> = {}) {\n const result = await this.instance.client().makeRequest({\n method: \"PUT\",\n url: `/v1/users/${this.instance.userId}`,\n params: props,\n });\n\n return this.handleResponse<User>(result);\n }\n\n async getAllPreferences() {\n const result = await this.instance.client().makeRequest({\n method: \"GET\",\n url: `/v1/users/${this.instance.userId}/preferences`,\n });\n\n return this.handleResponse<PreferenceSet[]>(result);\n }\n\n async getPreferences(\n options: GetPreferencesOptions = {},\n ): Promise<PreferenceSet> {\n const preferenceSetId = options.preferenceSet || DEFAULT_PREFERENCE_SET_ID;\n\n const result = await this.instance.client().makeRequest({\n method: \"GET\",\n url: `/v1/users/${this.instance.userId}/preferences/${preferenceSetId}`,\n params: { tenant: options.tenant },\n });\n\n return this.handleResponse<PreferenceSet>(result);\n }\n\n async setPreferences(\n preferenceSet: SetPreferencesProperties,\n options: PreferenceOptions = {},\n ): Promise<PreferenceSet> {\n const preferenceSetId = options.preferenceSet || DEFAULT_PREFERENCE_SET_ID;\n\n const result = await this.instance.client().makeRequest({\n method: \"PUT\",\n url: `/v1/users/${this.instance.userId}/preferences/${preferenceSetId}`,\n data: preferenceSet,\n });\n\n return this.handleResponse<PreferenceSet>(result);\n }\n\n async getChannelData<T = any>(params: GetChannelDataInput) {\n const result = await this.instance.client().makeRequest({\n method: \"GET\",\n url: `/v1/users/${this.instance.userId}/channel_data/${params.channelId}`,\n });\n\n return this.handleResponse<ChannelData<T>>(result);\n }\n\n async setChannelData<T = any>({\n channelId,\n channelData,\n }: SetChannelDataInput) {\n const result = await this.instance.client().makeRequest({\n method: \"PUT\",\n url: `/v1/users/${this.instance.userId}/channel_data/${channelId}`,\n data: { data: channelData },\n });\n\n return this.handleResponse<ChannelData<T>>(result);\n }\n\n private handleResponse<T>(response: ApiResponse) {\n if (response.statusCode === \"error\") {\n throw new Error(response.error || response.body);\n }\n\n return response.body as T;\n }\n}\n\nexport default UserClient;\n"],"names":["DEFAULT_PREFERENCE_SET_ID","UserClient","instance","__publicField","result","props","options","preferenceSetId","preferenceSet","params","channelId","channelData","response"],"mappings":"oRAYA,MAAMA,EAA4B,UAElC,MAAMC,CAAW,CAGf,YAAYC,EAAiB,CAFrBC,EAAA,iBAGN,KAAK,SAAWD,CAClB,CAEA,MAAM,KAAM,CACV,MAAME,EAAS,MAAM,KAAK,SAAS,OAAA,EAAS,YAAY,CACtD,OAAQ,MACR,IAAK,aAAa,KAAK,SAAS,MAAM,EAAA,CACvC,EAEM,OAAA,KAAK,eAAqBA,CAAM,CACzC,CAEA,MAAM,SAASC,EAA6B,GAAI,CAC9C,MAAMD,EAAS,MAAM,KAAK,SAAS,OAAA,EAAS,YAAY,CACtD,OAAQ,MACR,IAAK,aAAa,KAAK,SAAS,MAAM,GACtC,OAAQC,CAAA,CACT,EAEM,OAAA,KAAK,eAAqBD,CAAM,CACzC,CAEA,MAAM,mBAAoB,CACxB,MAAMA,EAAS,MAAM,KAAK,SAAS,OAAA,EAAS,YAAY,CACtD,OAAQ,MACR,IAAK,aAAa,KAAK,SAAS,MAAM,cAAA,CACvC,EAEM,OAAA,KAAK,eAAgCA,CAAM,CACpD,CAEA,MAAM,eACJE,EAAiC,GACT,CACl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
12
|
+
method: "GET",
|
|
13
|
+
url: `/v1/users/${this.instance.userId}`
|
|
14
|
+
});
|
|
15
|
+
return this.handleResponse(e);
|
|
16
|
+
}
|
|
17
|
+
async identify(e = {}) {
|
|
18
|
+
const t = await this.instance.client().makeRequest({
|
|
19
|
+
method: "PUT",
|
|
20
|
+
url: `/v1/users/${this.instance.userId}`,
|
|
21
|
+
params: e
|
|
22
|
+
});
|
|
23
|
+
return this.handleResponse(t);
|
|
24
|
+
}
|
|
10
25
|
async getAllPreferences() {
|
|
11
26
|
const e = await this.instance.client().makeRequest({
|
|
12
27
|
method: "GET",
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","sources":["../../../../src/clients/users/index.ts"],"sourcesContent":["import { ApiResponse } from \"../../api\";\nimport { ChannelData } from \"../../interfaces\";\nimport Knock from \"../../knock\";\nimport {\n GetPreferencesOptions,\n PreferenceOptions,\n PreferenceSet,\n SetPreferencesProperties,\n} from \"../preferences/interfaces\";\nimport { GetChannelDataInput, SetChannelDataInput } from \"./interfaces\";\n\nconst DEFAULT_PREFERENCE_SET_ID = \"default\";\n\nclass UserClient {\n private instance: Knock;\n\n constructor(instance: Knock) {\n this.instance = instance;\n }\n\n async getAllPreferences() {\n const result = await this.instance.client().makeRequest({\n method: \"GET\",\n url: `/v1/users/${this.instance.userId}/preferences`,\n });\n\n return this.handleResponse<PreferenceSet[]>(result);\n }\n\n async getPreferences(\n options: GetPreferencesOptions = {},\n ): Promise<PreferenceSet> {\n const preferenceSetId = options.preferenceSet || DEFAULT_PREFERENCE_SET_ID;\n\n const result = await this.instance.client().makeRequest({\n method: \"GET\",\n url: `/v1/users/${this.instance.userId}/preferences/${preferenceSetId}`,\n params: { tenant: options.tenant },\n });\n\n return this.handleResponse<PreferenceSet>(result);\n }\n\n async setPreferences(\n preferenceSet: SetPreferencesProperties,\n options: PreferenceOptions = {},\n ): Promise<PreferenceSet> {\n const preferenceSetId = options.preferenceSet || DEFAULT_PREFERENCE_SET_ID;\n\n const result = await this.instance.client().makeRequest({\n method: \"PUT\",\n url: `/v1/users/${this.instance.userId}/preferences/${preferenceSetId}`,\n data: preferenceSet,\n });\n\n return this.handleResponse<PreferenceSet>(result);\n }\n\n async getChannelData<T = any>(params: GetChannelDataInput) {\n const result = await this.instance.client().makeRequest({\n method: \"GET\",\n url: `/v1/users/${this.instance.userId}/channel_data/${params.channelId}`,\n });\n\n return this.handleResponse<ChannelData<T>>(result);\n }\n\n async setChannelData<T = any>({\n channelId,\n channelData,\n }: SetChannelDataInput) {\n const result = await this.instance.client().makeRequest({\n method: \"PUT\",\n url: `/v1/users/${this.instance.userId}/channel_data/${channelId}`,\n data: { data: channelData },\n });\n\n return this.handleResponse<ChannelData<T>>(result);\n }\n\n private handleResponse<T>(response: ApiResponse) {\n if (response.statusCode === \"error\") {\n throw new Error(response.error || response.body);\n }\n\n return response.body as T;\n }\n}\n\nexport default UserClient;\n"],"names":["DEFAULT_PREFERENCE_SET_ID","UserClient","instance","__publicField","result","options","preferenceSetId","preferenceSet","params","channelId","channelData","response"],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"index.mjs","sources":["../../../../src/clients/users/index.ts"],"sourcesContent":["import { ApiResponse } from \"../../api\";\nimport { ChannelData, User } from \"../../interfaces\";\nimport Knock from \"../../knock\";\nimport {\n GetPreferencesOptions,\n PreferenceOptions,\n PreferenceSet,\n SetPreferencesProperties,\n} from \"../preferences/interfaces\";\n\nimport { GetChannelDataInput, SetChannelDataInput } from \"./interfaces\";\n\nconst DEFAULT_PREFERENCE_SET_ID = \"default\";\n\nclass UserClient {\n private instance: Knock;\n\n constructor(instance: Knock) {\n this.instance = instance;\n }\n\n async get() {\n const result = await this.instance.client().makeRequest({\n method: \"GET\",\n url: `/v1/users/${this.instance.userId}`,\n });\n\n return this.handleResponse<User>(result);\n }\n\n async identify(props: Record<string, any> = {}) {\n const result = await this.instance.client().makeRequest({\n method: \"PUT\",\n url: `/v1/users/${this.instance.userId}`,\n params: props,\n });\n\n return this.handleResponse<User>(result);\n }\n\n async getAllPreferences() {\n const result = await this.instance.client().makeRequest({\n method: \"GET\",\n url: `/v1/users/${this.instance.userId}/preferences`,\n });\n\n return this.handleResponse<PreferenceSet[]>(result);\n }\n\n async getPreferences(\n options: GetPreferencesOptions = {},\n ): Promise<PreferenceSet> {\n const preferenceSetId = options.preferenceSet || DEFAULT_PREFERENCE_SET_ID;\n\n const result = await this.instance.client().makeRequest({\n method: \"GET\",\n url: `/v1/users/${this.instance.userId}/preferences/${preferenceSetId}`,\n params: { tenant: options.tenant },\n });\n\n return this.handleResponse<PreferenceSet>(result);\n }\n\n async setPreferences(\n preferenceSet: SetPreferencesProperties,\n options: PreferenceOptions = {},\n ): Promise<PreferenceSet> {\n const preferenceSetId = options.preferenceSet || DEFAULT_PREFERENCE_SET_ID;\n\n const result = await this.instance.client().makeRequest({\n method: \"PUT\",\n url: `/v1/users/${this.instance.userId}/preferences/${preferenceSetId}`,\n data: preferenceSet,\n });\n\n return this.handleResponse<PreferenceSet>(result);\n }\n\n async getChannelData<T = any>(params: GetChannelDataInput) {\n const result = await this.instance.client().makeRequest({\n method: \"GET\",\n url: `/v1/users/${this.instance.userId}/channel_data/${params.channelId}`,\n });\n\n return this.handleResponse<ChannelData<T>>(result);\n }\n\n async setChannelData<T = any>({\n channelId,\n channelData,\n }: SetChannelDataInput) {\n const result = await this.instance.client().makeRequest({\n method: \"PUT\",\n url: `/v1/users/${this.instance.userId}/channel_data/${channelId}`,\n data: { data: channelData },\n });\n\n return this.handleResponse<ChannelData<T>>(result);\n }\n\n private handleResponse<T>(response: ApiResponse) {\n if (response.statusCode === \"error\") {\n throw new Error(response.error || response.body);\n }\n\n return response.body as T;\n }\n}\n\nexport default UserClient;\n"],"names":["DEFAULT_PREFERENCE_SET_ID","UserClient","instance","__publicField","result","props","options","preferenceSetId","preferenceSet","params","channelId","channelData","response"],"mappings":";;;AAYA,MAAMA,IAA4B;AAElC,MAAMC,EAAW;AAAA,EAGf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E,MAAMC;AAAA,IAAA,CACP;AAEM,WAAA,KAAK,eAA8BJ,CAAM;AAAA,EAClD;AAAA,EAEA,MAAM,eAAwBK,GAA6B;AACzD,UAAML,IAAS,MAAM,KAAK,SAAS,OAAA,EAAS,YAAY;AAAA,MACtD,QAAQ;AAAA,MACR,KAAK,aAAa,KAAK,SAAS,MAAM,iBAAiBK,EAAO,SAAS;AAAA,IAAA,CACxE;AAEM,WAAA,KAAK,eAA+BL,CAAM;AAAA,EACnD;AAAA,EAEA,MAAM,eAAwB;AAAA,IAC5B,WAAAM;AAAA,IACA,aAAAC;AAAA,EAAA,GACsB;AACtB,UAAMP,IAAS,MAAM,KAAK,SAAS,OAAA,EAAS,YAAY;AAAA,MACtD,QAAQ;AAAA,MACR,KAAK,aAAa,KAAK,SAAS,MAAM,iBAAiBM,CAAS;AAAA,MAChE,MAAM,EAAE,MAAMC,EAAY;AAAA,IAAA,CAC3B;AAEM,WAAA,KAAK,eAA+BP,CAAM;AAAA,EACnD;AAAA,EAEQ,eAAkBQ,GAAuB;AAC3C,QAAAA,EAAS,eAAe;AAC1B,YAAM,IAAI,MAAMA,EAAS,SAASA,EAAS,IAAI;AAGjD,WAAOA,EAAS;AAAA,EAClB;AACF;"}
|
|
@@ -1,10 +1,12 @@
|
|
|
1
|
-
import { ChannelData } from "../../interfaces";
|
|
1
|
+
import { ChannelData, User } from "../../interfaces";
|
|
2
2
|
import Knock from "../../knock";
|
|
3
3
|
import { GetPreferencesOptions, PreferenceOptions, PreferenceSet, SetPreferencesProperties } from "../preferences/interfaces";
|
|
4
4
|
import { GetChannelDataInput, SetChannelDataInput } from "./interfaces";
|
|
5
5
|
declare class UserClient {
|
|
6
6
|
private instance;
|
|
7
7
|
constructor(instance: Knock);
|
|
8
|
+
get(): Promise<User>;
|
|
9
|
+
identify(props?: Record<string, any>): Promise<User>;
|
|
8
10
|
getAllPreferences(): Promise<PreferenceSet[]>;
|
|
9
11
|
getPreferences(options?: GetPreferencesOptions): Promise<PreferenceSet>;
|
|
10
12
|
setPreferences(preferenceSet: SetPreferencesProperties, options?: PreferenceOptions): Promise<PreferenceSet>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/clients/users/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/clients/users/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AACrD,OAAO,KAAK,MAAM,aAAa,CAAC;AAChC,OAAO,EACL,qBAAqB,EACrB,iBAAiB,EACjB,aAAa,EACb,wBAAwB,EACzB,MAAM,2BAA2B,CAAC;AAEnC,OAAO,EAAE,mBAAmB,EAAE,mBAAmB,EAAE,MAAM,cAAc,CAAC;AAIxE,cAAM,UAAU;IACd,OAAO,CAAC,QAAQ,CAAQ;gBAEZ,QAAQ,EAAE,KAAK;IAIrB,GAAG;IASH,QAAQ,CAAC,KAAK,GAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAM;IAUxC,iBAAiB;IASjB,cAAc,CAClB,OAAO,GAAE,qBAA0B,GAClC,OAAO,CAAC,aAAa,CAAC;IAYnB,cAAc,CAClB,aAAa,EAAE,wBAAwB,EACvC,OAAO,GAAE,iBAAsB,GAC9B,OAAO,CAAC,aAAa,CAAC;IAYnB,cAAc,CAAC,CAAC,GAAG,GAAG,EAAE,MAAM,EAAE,mBAAmB;IASnD,cAAc,CAAC,CAAC,GAAG,GAAG,EAAE,EAC5B,SAAS,EACT,WAAW,GACZ,EAAE,mBAAmB;IAUtB,OAAO,CAAC,cAAc;CAOvB;AAED,eAAe,UAAU,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@knocklabs/client",
|
|
3
|
-
"version": "0.9.
|
|
3
|
+
"version": "0.9.4",
|
|
4
4
|
"description": "The clientside library for interacting with Knock",
|
|
5
5
|
"homepage": "https://github.com/knocklabs/javascript/tree/main/packages/client",
|
|
6
6
|
"author": "@knocklabs",
|
|
@@ -9,16 +9,19 @@
|
|
|
9
9
|
"module": "dist/esm/index.mjs",
|
|
10
10
|
"types": "dist/types/index.d.ts",
|
|
11
11
|
"typings": "dist/types/index.d.ts",
|
|
12
|
+
"react-native": "./src/index.ts",
|
|
12
13
|
"exports": {
|
|
13
14
|
".": {
|
|
14
15
|
"require": "./dist/cjs/index.js",
|
|
15
16
|
"import": "./dist/esm/index.mjs",
|
|
16
17
|
"types": "./dist/types/index.d.ts",
|
|
18
|
+
"react-native": "./src/index.ts",
|
|
17
19
|
"default": "./dist/esm/index.mjs"
|
|
18
20
|
}
|
|
19
21
|
},
|
|
20
22
|
"files": [
|
|
21
23
|
"dist",
|
|
24
|
+
"src",
|
|
22
25
|
"README.md"
|
|
23
26
|
],
|
|
24
27
|
"repository": {
|
package/src/api.ts
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import axios, { AxiosError, AxiosInstance, AxiosRequestConfig } from "axios";
|
|
2
|
+
import axiosRetry from "axios-retry";
|
|
3
|
+
import { Socket } from "phoenix";
|
|
4
|
+
|
|
5
|
+
type ApiClientOptions = {
|
|
6
|
+
host: string;
|
|
7
|
+
apiKey: string;
|
|
8
|
+
userToken: string | undefined;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export interface ApiResponse {
|
|
12
|
+
// eslint-disable-next-line
|
|
13
|
+
error?: any;
|
|
14
|
+
// eslint-disable-next-line
|
|
15
|
+
body?: any;
|
|
16
|
+
statusCode: "ok" | "error";
|
|
17
|
+
status: number;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
class ApiClient {
|
|
21
|
+
private host: string;
|
|
22
|
+
private apiKey: string;
|
|
23
|
+
private userToken: string | null;
|
|
24
|
+
private axiosClient: AxiosInstance;
|
|
25
|
+
|
|
26
|
+
public socket: Socket | undefined;
|
|
27
|
+
|
|
28
|
+
constructor(options: ApiClientOptions) {
|
|
29
|
+
this.host = options.host;
|
|
30
|
+
this.apiKey = options.apiKey;
|
|
31
|
+
this.userToken = options.userToken || null;
|
|
32
|
+
|
|
33
|
+
// Create a retryable axios client
|
|
34
|
+
this.axiosClient = axios.create({
|
|
35
|
+
baseURL: this.host,
|
|
36
|
+
headers: {
|
|
37
|
+
Accept: "application/json",
|
|
38
|
+
"Content-Type": "application/json",
|
|
39
|
+
Authorization: `Bearer ${this.apiKey}`,
|
|
40
|
+
"X-Knock-User-Token": this.userToken,
|
|
41
|
+
},
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
if (typeof window !== "undefined") {
|
|
45
|
+
this.socket = new Socket(`${this.host.replace("http", "ws")}/ws/v1`, {
|
|
46
|
+
params: {
|
|
47
|
+
user_token: this.userToken,
|
|
48
|
+
api_key: this.apiKey,
|
|
49
|
+
},
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// @ts-ignore
|
|
54
|
+
axiosRetry(this.axiosClient, {
|
|
55
|
+
retries: 3,
|
|
56
|
+
retryCondition: this.canRetryRequest,
|
|
57
|
+
retryDelay: axiosRetry.exponentialDelay,
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
async makeRequest(req: AxiosRequestConfig): Promise<ApiResponse> {
|
|
62
|
+
try {
|
|
63
|
+
const result = await this.axiosClient(req);
|
|
64
|
+
|
|
65
|
+
return {
|
|
66
|
+
statusCode: result.status < 300 ? "ok" : "error",
|
|
67
|
+
body: result.data,
|
|
68
|
+
error: undefined,
|
|
69
|
+
status: result.status,
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
// eslint:disable-next-line
|
|
73
|
+
} catch (e: unknown) {
|
|
74
|
+
console.error(e);
|
|
75
|
+
|
|
76
|
+
return {
|
|
77
|
+
statusCode: "error",
|
|
78
|
+
status: 500,
|
|
79
|
+
body: undefined,
|
|
80
|
+
error: e,
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
private canRetryRequest(error: AxiosError) {
|
|
86
|
+
// Retry Network Errors.
|
|
87
|
+
if (axiosRetry.isNetworkError(error)) {
|
|
88
|
+
return true;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
if (!error.response) {
|
|
92
|
+
// Cannot determine if the request can be retried
|
|
93
|
+
return false;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// Retry Server Errors (5xx).
|
|
97
|
+
if (error.response.status >= 500 && error.response.status <= 599) {
|
|
98
|
+
return true;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// Retry if rate limited.
|
|
102
|
+
if (error.response.status === 429) {
|
|
103
|
+
return true;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
return false;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export default ApiClient;
|