@knocklabs/client 0.8.17 → 0.8.19

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 (130) hide show
  1. package/README.md +17 -0
  2. package/dist/api.d.ts +25 -0
  3. package/dist/api.d.ts.map +1 -0
  4. package/dist/api.js +84 -0
  5. package/dist/cjs/api.js +2 -133
  6. package/dist/cjs/api.js.map +1 -1
  7. package/dist/cjs/clients/feed/feed.js +2 -937
  8. package/dist/cjs/clients/feed/feed.js.map +1 -1
  9. package/dist/cjs/clients/feed/index.js +2 -34
  10. package/dist/cjs/clients/feed/index.js.map +1 -1
  11. package/dist/cjs/clients/feed/store.js +2 -111
  12. package/dist/cjs/clients/feed/store.js.map +1 -1
  13. package/dist/cjs/clients/feed/utils.js +2 -26
  14. package/dist/cjs/clients/feed/utils.js.map +1 -1
  15. package/dist/cjs/clients/preferences/index.js +2 -373
  16. package/dist/cjs/clients/preferences/index.js.map +1 -1
  17. package/dist/cjs/clients/users/index.js +2 -185
  18. package/dist/cjs/clients/users/index.js.map +1 -1
  19. package/dist/cjs/index.js +2 -102
  20. package/dist/cjs/index.js.map +1 -1
  21. package/dist/cjs/knock.js +6 -89
  22. package/dist/cjs/knock.js.map +1 -1
  23. package/dist/cjs/networkStatus.js +2 -18
  24. package/dist/cjs/networkStatus.js.map +1 -1
  25. package/dist/clients/feed/feed.d.ts +64 -0
  26. package/dist/clients/feed/feed.d.ts.map +1 -0
  27. package/dist/clients/feed/feed.js +572 -0
  28. package/dist/clients/feed/index.d.ts +15 -0
  29. package/dist/clients/feed/index.d.ts.map +1 -0
  30. package/dist/clients/feed/index.js +34 -0
  31. package/dist/clients/feed/interfaces.d.ts +60 -0
  32. package/dist/clients/feed/interfaces.d.ts.map +1 -0
  33. package/dist/clients/feed/interfaces.js +2 -0
  34. package/dist/clients/feed/store.d.ts +3 -0
  35. package/dist/clients/feed/store.d.ts.map +1 -0
  36. package/dist/clients/feed/store.js +72 -0
  37. package/dist/clients/feed/types.d.ts +34 -0
  38. package/dist/clients/feed/types.d.ts.map +1 -0
  39. package/dist/clients/feed/types.js +2 -0
  40. package/dist/clients/feed/utils.d.ts +4 -0
  41. package/dist/clients/feed/utils.d.ts.map +1 -0
  42. package/dist/clients/feed/utils.js +21 -0
  43. package/dist/clients/preferences/index.d.ts +46 -0
  44. package/dist/clients/preferences/index.d.ts.map +1 -0
  45. package/dist/clients/preferences/index.js +129 -0
  46. package/dist/clients/preferences/interfaces.d.ts +26 -0
  47. package/dist/clients/preferences/interfaces.d.ts.map +1 -0
  48. package/dist/clients/preferences/interfaces.js +2 -0
  49. package/dist/clients/users/index.d.ts +16 -0
  50. package/dist/clients/users/index.d.ts.map +1 -0
  51. package/dist/clients/users/index.js +56 -0
  52. package/dist/clients/users/interfaces.d.ts +8 -0
  53. package/dist/clients/users/interfaces.d.ts.map +1 -0
  54. package/dist/clients/users/interfaces.js +2 -0
  55. package/dist/esm/api.js +44 -84
  56. package/dist/esm/api.js.map +1 -1
  57. package/dist/esm/clients/feed/feed.js +296 -601
  58. package/dist/esm/clients/feed/feed.js.map +1 -1
  59. package/dist/esm/clients/feed/index.js +28 -12
  60. package/dist/esm/clients/feed/index.js.map +1 -1
  61. package/dist/esm/clients/feed/store.js +37 -71
  62. package/dist/esm/clients/feed/store.js.map +1 -1
  63. package/dist/esm/clients/feed/utils.js +10 -15
  64. package/dist/esm/clients/feed/utils.js.map +1 -1
  65. package/dist/esm/clients/preferences/index.js +79 -146
  66. package/dist/esm/clients/preferences/index.js.map +1 -1
  67. package/dist/esm/clients/users/index.js +52 -76
  68. package/dist/esm/clients/users/index.js.map +1 -1
  69. package/dist/esm/index.js +12 -11
  70. package/dist/esm/index.js.map +1 -1
  71. package/dist/esm/knock.js +72 -51
  72. package/dist/esm/knock.js.map +1 -1
  73. package/dist/esm/networkStatus.js +14 -10
  74. package/dist/esm/networkStatus.js.map +1 -1
  75. package/dist/index.d.ts +11 -0
  76. package/dist/index.d.ts.map +1 -0
  77. package/dist/index.js +43 -0
  78. package/dist/interfaces.d.ts +41 -0
  79. package/dist/interfaces.d.ts.map +1 -0
  80. package/dist/interfaces.js +2 -0
  81. package/dist/knock.d.ts +30 -0
  82. package/dist/knock.d.ts.map +1 -0
  83. package/dist/knock.js +135 -0
  84. package/dist/networkStatus.d.ts +8 -0
  85. package/dist/networkStatus.d.ts.map +1 -0
  86. package/dist/networkStatus.js +18 -0
  87. package/dist/types/api.d.ts +0 -2
  88. package/dist/types/api.d.ts.map +1 -1
  89. package/dist/types/clients/feed/feed.d.ts +12 -4
  90. package/dist/types/clients/feed/feed.d.ts.map +1 -1
  91. package/dist/types/clients/feed/index.d.ts +4 -0
  92. package/dist/types/clients/feed/index.d.ts.map +1 -1
  93. package/dist/types/clients/feed/interfaces.d.ts +2 -1
  94. package/dist/types/clients/feed/interfaces.d.ts.map +1 -1
  95. package/dist/types/clients/feed/store.d.ts.map +1 -1
  96. package/dist/types/clients/feed/types.d.ts +1 -2
  97. package/dist/types/clients/feed/types.d.ts.map +1 -1
  98. package/dist/types/clients/feed/utils.d.ts +1 -1
  99. package/dist/types/clients/feed/utils.d.ts.map +1 -1
  100. package/dist/types/clients/preferences/index.d.ts +2 -1
  101. package/dist/types/clients/preferences/index.d.ts.map +1 -1
  102. package/dist/types/clients/preferences/interfaces.d.ts +1 -1
  103. package/dist/types/clients/preferences/interfaces.d.ts.map +1 -1
  104. package/dist/types/index.d.ts +1 -1
  105. package/dist/types/index.d.ts.map +1 -1
  106. package/dist/types/interfaces.d.ts +8 -8
  107. package/dist/types/interfaces.d.ts.map +1 -1
  108. package/dist/types/knock.d.ts +12 -4
  109. package/dist/types/knock.d.ts.map +1 -1
  110. package/package.json +15 -10
  111. package/dist/cjs/clients/feed/interfaces.js +0 -6
  112. package/dist/cjs/clients/feed/interfaces.js.map +0 -1
  113. package/dist/cjs/clients/feed/types.js +0 -6
  114. package/dist/cjs/clients/feed/types.js.map +0 -1
  115. package/dist/cjs/clients/preferences/interfaces.js +0 -6
  116. package/dist/cjs/clients/preferences/interfaces.js.map +0 -1
  117. package/dist/cjs/clients/users/interfaces.js +0 -6
  118. package/dist/cjs/clients/users/interfaces.js.map +0 -1
  119. package/dist/cjs/interfaces.js +0 -6
  120. package/dist/cjs/interfaces.js.map +0 -1
  121. package/dist/esm/clients/feed/interfaces.js +0 -2
  122. package/dist/esm/clients/feed/interfaces.js.map +0 -1
  123. package/dist/esm/clients/feed/types.js +0 -2
  124. package/dist/esm/clients/feed/types.js.map +0 -1
  125. package/dist/esm/clients/preferences/interfaces.js +0 -2
  126. package/dist/esm/clients/preferences/interfaces.js.map +0 -1
  127. package/dist/esm/clients/users/interfaces.js +0 -2
  128. package/dist/esm/clients/users/interfaces.js.map +0 -1
  129. package/dist/esm/interfaces.js +0 -2
  130. package/dist/esm/interfaces.js.map +0 -1
package/README.md CHANGED
@@ -191,3 +191,20 @@ await knockClient.user.setChannelData({
191
191
  ```
192
192
 
193
193
  See provider requirements for setting channel data [here]("https://docs.knock.app/managing-recipients/setting-channel-data#provider-data-requirements").
194
+
195
+ ### Automatically disconnect sockets from inactive tabs
196
+
197
+ Optionally, you can configure the client to disconnect socket connections with inactive tabs after a brief delay. If the tab becomes active again, the socket will reconnect to continue receiving real-time notification updates.
198
+
199
+ ```typescript
200
+ // Initialize the feed and configure the automatic disconnect settings
201
+ const feedClient = knockClient.feeds.initialize(
202
+ process.env.KNOCK_FEED_CHANNEL_ID,
203
+ {
204
+ // Turn on the automatic connection manager
205
+ auto_manage_socket_connection: true,
206
+ // Optionally, customize the delay amount in milliseconds. Defaults to 2000ms or 2s
207
+ auto_manage_socket_connection_delay: 2500,
208
+ },
209
+ );
210
+ ```
package/dist/api.d.ts ADDED
@@ -0,0 +1,25 @@
1
+ import { AxiosRequestConfig } from "axios";
2
+ import { Socket } from "phoenix";
3
+ type ApiClientOptions = {
4
+ host: string;
5
+ apiKey: string;
6
+ userToken: string | undefined;
7
+ };
8
+ export interface ApiResponse {
9
+ error?: any;
10
+ body?: any;
11
+ statusCode: "ok" | "error";
12
+ status: number;
13
+ }
14
+ declare class ApiClient {
15
+ private host;
16
+ private apiKey;
17
+ private userToken;
18
+ private axiosClient;
19
+ socket: Socket | undefined;
20
+ constructor(options: ApiClientOptions);
21
+ makeRequest(req: AxiosRequestConfig): Promise<ApiResponse>;
22
+ private canRetryRequest;
23
+ }
24
+ export default ApiClient;
25
+ //# sourceMappingURL=api.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"api.d.ts","sourceRoot":"","sources":["../src/api.ts"],"names":[],"mappings":"AAAA,OAAc,EAA6B,kBAAkB,EAAE,MAAM,OAAO,CAAC;AAE7E,OAAO,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AAEjC,KAAK,gBAAgB,GAAG;IACtB,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,MAAM,CAAC;IACf,SAAS,EAAE,MAAM,GAAG,SAAS,CAAC;CAC/B,CAAC;AAEF,MAAM,WAAW,WAAW;IAE1B,KAAK,CAAC,EAAE,GAAG,CAAC;IAEZ,IAAI,CAAC,EAAE,GAAG,CAAC;IACX,UAAU,EAAE,IAAI,GAAG,OAAO,CAAC;IAC3B,MAAM,EAAE,MAAM,CAAC;CAChB;AAED,cAAM,SAAS;IACb,OAAO,CAAC,IAAI,CAAS;IACrB,OAAO,CAAC,MAAM,CAAS;IACvB,OAAO,CAAC,SAAS,CAAgB;IACjC,OAAO,CAAC,WAAW,CAAgB;IAE5B,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC;gBAEtB,OAAO,EAAE,gBAAgB;IAgC/B,WAAW,CAAC,GAAG,EAAE,kBAAkB,GAAG,OAAO,CAAC,WAAW,CAAC;IAwBhE,OAAO,CAAC,eAAe;CAuBxB;AAED,eAAe,SAAS,CAAC"}
package/dist/api.js ADDED
@@ -0,0 +1,84 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const axios_1 = __importDefault(require("axios"));
7
+ const axios_retry_1 = __importDefault(require("axios-retry"));
8
+ const phoenix_1 = require("phoenix");
9
+ class ApiClient {
10
+ host;
11
+ apiKey;
12
+ userToken;
13
+ axiosClient;
14
+ socket;
15
+ constructor(options) {
16
+ this.host = options.host;
17
+ this.apiKey = options.apiKey;
18
+ this.userToken = options.userToken || null;
19
+ // Create a retryable axios client
20
+ this.axiosClient = axios_1.default.create({
21
+ baseURL: this.host,
22
+ headers: {
23
+ Accept: "application/json",
24
+ "Content-Type": "application/json",
25
+ Authorization: `Bearer ${this.apiKey}`,
26
+ "X-Knock-User-Token": this.userToken,
27
+ },
28
+ });
29
+ if (typeof window !== "undefined") {
30
+ this.socket = new phoenix_1.Socket(`${this.host.replace("http", "ws")}/ws/v1`, {
31
+ params: {
32
+ user_token: this.userToken,
33
+ api_key: this.apiKey,
34
+ },
35
+ });
36
+ }
37
+ (0, axios_retry_1.default)(this.axiosClient, {
38
+ retries: 3,
39
+ retryCondition: this.canRetryRequest,
40
+ retryDelay: axios_retry_1.default.exponentialDelay,
41
+ });
42
+ }
43
+ async makeRequest(req) {
44
+ try {
45
+ const result = await this.axiosClient(req);
46
+ return {
47
+ statusCode: result.status < 300 ? "ok" : "error",
48
+ body: result.data,
49
+ error: undefined,
50
+ status: result.status,
51
+ };
52
+ // eslint:disable-next-line
53
+ }
54
+ catch (e) {
55
+ console.error(e);
56
+ return {
57
+ statusCode: "error",
58
+ status: 500,
59
+ body: undefined,
60
+ error: e,
61
+ };
62
+ }
63
+ }
64
+ canRetryRequest(error) {
65
+ // Retry Network Errors.
66
+ if (axios_retry_1.default.isNetworkError(error)) {
67
+ return true;
68
+ }
69
+ if (!error.response) {
70
+ // Cannot determine if the request can be retried
71
+ return false;
72
+ }
73
+ // Retry Server Errors (5xx).
74
+ if (error.response.status >= 500 && error.response.status <= 599) {
75
+ return true;
76
+ }
77
+ // Retry if rate limited.
78
+ if (error.response.status === 429) {
79
+ return true;
80
+ }
81
+ return false;
82
+ }
83
+ }
84
+ exports.default = ApiClient;
package/dist/cjs/api.js CHANGED
@@ -1,133 +1,2 @@
1
- "use strict";
2
-
3
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
- Object.defineProperty(exports, "__esModule", {
5
- value: true
6
- });
7
- exports["default"] = void 0;
8
- var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
9
- var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
10
- var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
11
- var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
12
- var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
13
- var _axios = _interopRequireDefault(require("axios"));
14
- var _axiosRetry = _interopRequireDefault(require("axios-retry"));
15
- var _phoenix = require("phoenix");
16
- var ApiClient = /*#__PURE__*/function () {
17
- function ApiClient(options) {
18
- (0, _classCallCheck2["default"])(this, ApiClient);
19
- (0, _defineProperty2["default"])(this, "host", void 0);
20
- (0, _defineProperty2["default"])(this, "apiKey", void 0);
21
- (0, _defineProperty2["default"])(this, "userToken", void 0);
22
- (0, _defineProperty2["default"])(this, "axiosClient", void 0);
23
- (0, _defineProperty2["default"])(this, "socket", void 0);
24
- this.host = options.host;
25
- this.apiKey = options.apiKey;
26
- this.userToken = options.userToken || null;
27
-
28
- // Create a retryable axios client
29
- this.axiosClient = _axios["default"].create({
30
- baseURL: this.host,
31
- headers: {
32
- Accept: "application/json",
33
- "Content-Type": "application/json",
34
- Authorization: "Bearer ".concat(this.apiKey),
35
- "X-Knock-User-Token": this.userToken
36
- }
37
- });
38
- if (typeof window !== "undefined") {
39
- this.socket = new _phoenix.Socket("".concat(this.host.replace("http", "ws"), "/ws/v1"), {
40
- params: {
41
- user_token: this.userToken,
42
- api_key: this.apiKey
43
- }
44
- });
45
- }
46
- (0, _axiosRetry["default"])(this.axiosClient, {
47
- retries: 3,
48
- retryCondition: this.canRetryRequest,
49
- retryDelay: _axiosRetry["default"].exponentialDelay
50
- });
51
- }
52
- (0, _createClass2["default"])(ApiClient, [{
53
- key: "makeRequest",
54
- value: function () {
55
- var _makeRequest = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(req) {
56
- var result;
57
- return _regenerator["default"].wrap(function _callee$(_context) {
58
- while (1) switch (_context.prev = _context.next) {
59
- case 0:
60
- _context.prev = 0;
61
- _context.next = 3;
62
- return this.axiosClient(req);
63
- case 3:
64
- result = _context.sent;
65
- return _context.abrupt("return", {
66
- statusCode: result.status < 300 ? "ok" : "error",
67
- body: result.data,
68
- error: undefined,
69
- status: result.status
70
- });
71
- case 7:
72
- _context.prev = 7;
73
- _context.t0 = _context["catch"](0);
74
- console.error(_context.t0);
75
- return _context.abrupt("return", {
76
- statusCode: "error",
77
- status: 500,
78
- body: undefined,
79
- error: _context.t0
80
- });
81
- case 11:
82
- case "end":
83
- return _context.stop();
84
- }
85
- }, _callee, this, [[0, 7]]);
86
- }));
87
- function makeRequest(_x) {
88
- return _makeRequest.apply(this, arguments);
89
- }
90
- return makeRequest;
91
- }()
92
- }, {
93
- key: "reconnectSocket",
94
- value: function reconnectSocket() {
95
- if (this.socket && !this.socket.isConnected()) {
96
- this.socket.connect();
97
- }
98
- }
99
- }, {
100
- key: "disconnectSocket",
101
- value: function disconnectSocket() {
102
- if (this.socket) {
103
- this.socket.disconnect();
104
- }
105
- }
106
- }, {
107
- key: "canRetryRequest",
108
- value: function canRetryRequest(error) {
109
- // Retry Network Errors.
110
- if (_axiosRetry["default"].isNetworkError(error)) {
111
- return true;
112
- }
113
- if (!error.response) {
114
- // Cannot determine if the request can be retried
115
- return false;
116
- }
117
-
118
- // Retry Server Errors (5xx).
119
- if (error.response.status >= 500 && error.response.status <= 599) {
120
- return true;
121
- }
122
-
123
- // Retry if rate limited.
124
- if (error.response.status === 429) {
125
- return true;
126
- }
127
- return false;
128
- }
129
- }]);
130
- return ApiClient;
131
- }();
132
- var _default = exports["default"] = ApiClient;
133
- //# sourceMappingURL=api.js.map
1
+ "use strict";var i=Object.defineProperty;var n=(t,e,s)=>e in t?i(t,e,{enumerable:!0,configurable:!0,writable:!0,value:s}):t[e]=s;var r=(t,e,s)=>(n(t,typeof e!="symbol"?e+"":e,s),s);Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const u=require("axios"),l=require("axios-retry"),c=require("phoenix"),a=t=>t&&typeof t=="object"&&"default"in t?t:{default:t},p=a(u),o=a(l);class d{constructor(e){r(this,"host");r(this,"apiKey");r(this,"userToken");r(this,"axiosClient");r(this,"socket");this.host=e.host,this.apiKey=e.apiKey,this.userToken=e.userToken||null,this.axiosClient=p.default.create({baseURL:this.host,headers:{Accept:"application/json","Content-Type":"application/json",Authorization:`Bearer ${this.apiKey}`,"X-Knock-User-Token":this.userToken}}),typeof window<"u"&&(this.socket=new c.Socket(`${this.host.replace("http","ws")}/ws/v1`,{params:{user_token:this.userToken,api_key:this.apiKey}})),o.default(this.axiosClient,{retries:3,retryCondition:this.canRetryRequest,retryDelay:o.default.exponentialDelay})}async makeRequest(e){try{const s=await this.axiosClient(e);return{statusCode:s.status<300?"ok":"error",body:s.data,error:void 0,status:s.status}}catch(s){return console.error(s),{statusCode:"error",status:500,body:void 0,error:s}}}canRetryRequest(e){return o.default.isNetworkError(e)?!0:e.response?e.response.status>=500&&e.response.status<=599||e.response.status===429:!1}}exports.default=d;
2
+ //# sourceMappingURL=api.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"api.js","names":["_axios","_interopRequireDefault","require","_axiosRetry","_phoenix","ApiClient","options","_classCallCheck2","_defineProperty2","host","apiKey","userToken","axiosClient","axios","create","baseURL","headers","Accept","Authorization","concat","window","socket","Socket","replace","params","user_token","api_key","axiosRetry","retries","retryCondition","canRetryRequest","retryDelay","exponentialDelay","_createClass2","key","value","_makeRequest","_asyncToGenerator2","_regenerator","mark","_callee","req","result","wrap","_callee$","_context","prev","next","sent","abrupt","statusCode","status","body","data","error","undefined","t0","console","stop","makeRequest","_x","apply","arguments","reconnectSocket","isConnected","connect","disconnectSocket","disconnect","isNetworkError","response","_default","exports"],"sources":["../../src/api.ts"],"sourcesContent":["import axios, { AxiosInstance, AxiosRequestConfig } from \"axios\";\nimport axiosRetry from \"axios-retry\";\nimport { Socket } from \"phoenix\";\nimport { AxiosError } from \"axios\";\n\ntype ApiClientOptions = {\n host: string;\n apiKey: string;\n userToken: string | undefined;\n};\n\nexport interface ApiResponse {\n // eslint-disable-next-line\n error?: any;\n // eslint-disable-next-line\n body?: any;\n statusCode: \"ok\" | \"error\";\n status: number;\n}\n\nclass ApiClient {\n private host: string;\n private apiKey: string;\n private userToken: string | null;\n private axiosClient: AxiosInstance;\n\n public socket: Socket | undefined;\n\n constructor(options: ApiClientOptions) {\n this.host = options.host;\n this.apiKey = options.apiKey;\n this.userToken = options.userToken || null;\n\n // Create a retryable axios client\n this.axiosClient = axios.create({\n baseURL: this.host,\n headers: {\n Accept: \"application/json\",\n \"Content-Type\": \"application/json\",\n Authorization: `Bearer ${this.apiKey}`,\n \"X-Knock-User-Token\": this.userToken,\n },\n });\n\n if (typeof window !== \"undefined\") {\n this.socket = new Socket(`${this.host.replace(\"http\", \"ws\")}/ws/v1`, {\n params: {\n user_token: this.userToken,\n api_key: this.apiKey,\n },\n });\n }\n\n axiosRetry(this.axiosClient, {\n retries: 3,\n retryCondition: this.canRetryRequest,\n retryDelay: axiosRetry.exponentialDelay,\n });\n }\n\n async makeRequest(req: AxiosRequestConfig): Promise<ApiResponse> {\n try {\n const result = await this.axiosClient(req);\n\n return {\n statusCode: result.status < 300 ? \"ok\" : \"error\",\n body: result.data,\n error: undefined,\n status: result.status,\n };\n\n // eslint:disable-next-line\n } catch (e: unknown) {\n console.error(e);\n\n return {\n statusCode: \"error\",\n status: 500,\n body: undefined,\n error: e,\n };\n }\n }\n\n reconnectSocket() {\n if (this.socket && !this.socket.isConnected()) {\n this.socket.connect();\n }\n }\n\n disconnectSocket() {\n if (this.socket) {\n this.socket.disconnect();\n }\n }\n\n private canRetryRequest(error: AxiosError) {\n // Retry Network Errors.\n if (axiosRetry.isNetworkError(error)) {\n return true;\n }\n\n if (!error.response) {\n // Cannot determine if the request can be retried\n return false;\n }\n\n // Retry Server Errors (5xx).\n if (error.response.status >= 500 && error.response.status <= 599) {\n return true;\n }\n\n // Retry if rate limited.\n if (error.response.status === 429) {\n return true;\n }\n\n return false;\n }\n}\n\nexport default ApiClient;\n"],"mappings":";;;;;;;;;;;;AAAA,IAAAA,MAAA,GAAAC,sBAAA,CAAAC,OAAA;AACA,IAAAC,WAAA,GAAAF,sBAAA,CAAAC,OAAA;AACA,IAAAE,QAAA,GAAAF,OAAA;AAAiC,IAkB3BG,SAAS;EAQb,SAAAA,UAAYC,OAAyB,EAAE;IAAA,IAAAC,gBAAA,mBAAAF,SAAA;IAAA,IAAAG,gBAAA;IAAA,IAAAA,gBAAA;IAAA,IAAAA,gBAAA;IAAA,IAAAA,gBAAA;IAAA,IAAAA,gBAAA;IACrC,IAAI,CAACC,IAAI,GAAGH,OAAO,CAACG,IAAI;IACxB,IAAI,CAACC,MAAM,GAAGJ,OAAO,CAACI,MAAM;IAC5B,IAAI,CAACC,SAAS,GAAGL,OAAO,CAACK,SAAS,IAAI,IAAI;;IAE1C;IACA,IAAI,CAACC,WAAW,GAAGC,iBAAK,CAACC,MAAM,CAAC;MAC9BC,OAAO,EAAE,IAAI,CAACN,IAAI;MAClBO,OAAO,EAAE;QACPC,MAAM,EAAE,kBAAkB;QAC1B,cAAc,EAAE,kBAAkB;QAClCC,aAAa,YAAAC,MAAA,CAAY,IAAI,CAACT,MAAM,CAAE;QACtC,oBAAoB,EAAE,IAAI,CAACC;MAC7B;IACF,CAAC,CAAC;IAEF,IAAI,OAAOS,MAAM,KAAK,WAAW,EAAE;MACjC,IAAI,CAAC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AAAiE,QAAA,GAAAC,OAAA,cAGYlE,SAAS"}
1
+ {"version":3,"file":"api.js","sources":["../../src/api.ts"],"sourcesContent":["import axios, { AxiosError, AxiosInstance, AxiosRequestConfig } from \"axios\";\nimport axiosRetry from \"axios-retry\";\nimport { Socket } from \"phoenix\";\n\ntype ApiClientOptions = {\n host: string;\n apiKey: string;\n userToken: string | undefined;\n};\n\nexport interface ApiResponse {\n // eslint-disable-next-line\n error?: any;\n // eslint-disable-next-line\n body?: any;\n statusCode: \"ok\" | \"error\";\n status: number;\n}\n\nclass ApiClient {\n private host: string;\n private apiKey: string;\n private userToken: string | null;\n private axiosClient: AxiosInstance;\n\n public socket: Socket | undefined;\n\n constructor(options: ApiClientOptions) {\n this.host = options.host;\n this.apiKey = options.apiKey;\n this.userToken = options.userToken || null;\n\n // Create a retryable axios client\n this.axiosClient = axios.create({\n baseURL: this.host,\n headers: {\n Accept: \"application/json\",\n \"Content-Type\": \"application/json\",\n Authorization: `Bearer ${this.apiKey}`,\n \"X-Knock-User-Token\": this.userToken,\n },\n });\n\n if (typeof window !== \"undefined\") {\n this.socket = new Socket(`${this.host.replace(\"http\", \"ws\")}/ws/v1`, {\n params: {\n user_token: this.userToken,\n api_key: this.apiKey,\n },\n });\n }\n\n axiosRetry(this.axiosClient, {\n retries: 3,\n retryCondition: this.canRetryRequest,\n retryDelay: axiosRetry.exponentialDelay,\n });\n }\n\n async makeRequest(req: AxiosRequestConfig): Promise<ApiResponse> {\n try {\n const result = await this.axiosClient(req);\n\n return {\n statusCode: result.status < 300 ? \"ok\" : \"error\",\n body: result.data,\n error: undefined,\n status: result.status,\n };\n\n // eslint:disable-next-line\n } catch (e: unknown) {\n console.error(e);\n\n return {\n statusCode: \"error\",\n status: 500,\n body: undefined,\n error: e,\n };\n }\n }\n\n private canRetryRequest(error: AxiosError) {\n // Retry Network Errors.\n if (axiosRetry.isNetworkError(error)) {\n return true;\n }\n\n if (!error.response) {\n // Cannot determine if the request can be retried\n return false;\n }\n\n // Retry Server Errors (5xx).\n if (error.response.status >= 500 && error.response.status <= 599) {\n return true;\n }\n\n // Retry if rate limited.\n if (error.response.status === 429) {\n return true;\n }\n\n return false;\n }\n}\n\nexport default ApiClient;\n"],"names":["ApiClient","options","__publicField","axios","Socket","axiosRetry","req","result","e","error"],"mappings":"iaAmBA,MAAMA,CAAU,CAQd,YAAYC,EAA2B,CAP/BC,EAAA,aACAA,EAAA,eACAA,EAAA,kBACAA,EAAA,oBAEDA,EAAA,eAGL,KAAK,KAAOD,EAAQ,KACpB,KAAK,OAASA,EAAQ,OACjB,KAAA,UAAYA,EAAQ,WAAa,KAGjC,KAAA,YAAcE,UAAM,OAAO,CAC9B,QAAS,KAAK,KACd,QAAS,CACP,OAAQ,mBACR,eAAgB,mBAChB,cAAe,UAAU,KAAK,MAAM,GACpC,qBAAsB,KAAK,SAC7B,CAAA,CACD,EAEG,OAAO,OAAW,MACf,KAAA,OAAS,IAAIC,EAAA,OAAO,GAAG,KAAK,KAAK,QAAQ,OAAQ,IAAI,CAAC,SAAU,CACnE,OAAQ,CACN,WAAY,KAAK,UACjB,QAAS,KAAK,MAChB,CAAA,CACD,GAGHC,EAAA,QAAW,KAAK,YAAa,CAC3B,QAAS,EACT,eAAgB,KAAK,gBACrB,WAAYA,EAAW,QAAA,gBAAA,CACxB,CACH,CAEA,MAAM,YAAYC,EAA+C,CAC3D,GAAA,CACF,MAAMC,EAAS,MAAM,KAAK,YAAYD,CAAG,EAElC,MAAA,CACL,WAAYC,EAAO,OAAS,IAAM,KAAO,QACzC,KAAMA,EAAO,KACb,MAAO,OACP,OAAQA,EAAO,MAAA,QAIVC,EAAY,CACnB,eAAQ,MAAMA,CAAC,EAER,CACL,WAAY,QACZ,OAAQ,IACR,KAAM,OACN,MAAOA,CAAA,CAEX,CACF,CAEQ,gBAAgBC,EAAmB,CAErC,OAAAJ,EAAA,QAAW,eAAeI,CAAK,EAC1B,GAGJA,EAAM,SAMPA,EAAM,SAAS,QAAU,KAAOA,EAAM,SAAS,QAAU,KAKzDA,EAAM,SAAS,SAAW,IATrB,EAcX,CACF"}