@knocklabs/client 0.9.2 → 0.9.3

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 CHANGED
@@ -1,5 +1,11 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.9.3
4
+
5
+ ### Patch Changes
6
+
7
+ - bc69618: Add react-native to package.json files to fix a bug in our React Native SDK
8
+
3
9
  ## 0.9.2
4
10
 
5
11
  ### Patch Changes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@knocklabs/client",
3
- "version": "0.9.2",
3
+ "version": "0.9.3",
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;