supaapps-api-kit-client 0.3.0 → 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.
package/README.md CHANGED
@@ -37,7 +37,13 @@ const unauthorizationCallback = () => {
37
37
  // Implement redirection to login page here
38
38
  };
39
39
 
40
- ApiKitClient.initialize(BASE_URL, authTokenCallback, unauthorizationCallback);
40
+ const useAuth: boolean = // Optional: default is false, use true if you want to use auth;
41
+
42
+ // in case of using auth
43
+ ApiKitClient.initialize(BASE_URL, authTokenCallback, unauthorizationCallback, true);
44
+
45
+ // in case of not using auth
46
+ ApiKitClient.initialize(BASE_URL);
41
47
  ```
42
48
 
43
49
  ## Making Requests
@@ -4,9 +4,10 @@ type UnauthorizationCallback = () => void;
4
4
  type AuthTokenCallback = () => Promise<string>;
5
5
  export declare class ApiKitClient {
6
6
  private static instance;
7
- private static authTokenCallback;
8
- private static unauthorizationCallback;
9
- static initialize(baseURL: string, authTokenCallback: AuthTokenCallback, unauthorizationCallback?: UnauthorizationCallback): void;
7
+ private static authTokenCallback?;
8
+ private static unauthorizationCallback?;
9
+ private static useAuth;
10
+ static initialize(baseURL: string, authTokenCallback?: AuthTokenCallback, unauthorizationCallback?: UnauthorizationCallback, useAuth?: boolean): void;
10
11
  private static setupInterceptors;
11
12
  private static checkInitialization;
12
13
  static get<T>(endpoint: string, params?: URLSearchParams): Promise<AxiosResponse<T>>;
@@ -12,15 +12,19 @@ Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.ApiKitClient = void 0;
13
13
  const axios_1 = require("axios");
14
14
  class ApiKitClient {
15
- static initialize(baseURL, authTokenCallback, unauthorizationCallback) {
15
+ static initialize(baseURL, authTokenCallback, unauthorizationCallback, useAuth = false) {
16
+ if (useAuth && !authTokenCallback) {
17
+ throw new Error("authTokenCallback must be provided if useAuth is true.");
18
+ }
16
19
  this.authTokenCallback = authTokenCallback;
17
20
  this.unauthorizationCallback = unauthorizationCallback;
21
+ this.useAuth = useAuth;
18
22
  this.instance = axios_1.default.create({ baseURL });
19
23
  this.setupInterceptors();
20
24
  }
21
25
  static setupInterceptors() {
22
26
  this.instance.interceptors.request.use((config) => __awaiter(this, void 0, void 0, function* () {
23
- if (this.authTokenCallback) {
27
+ if (this.useAuth && this.authTokenCallback) {
24
28
  const authToken = yield this.authTokenCallback();
25
29
  config.headers.Authorization = `Bearer ${authToken}`;
26
30
  }
@@ -83,3 +87,4 @@ class ApiKitClient {
83
87
  }
84
88
  }
85
89
  exports.ApiKitClient = ApiKitClient;
90
+ ApiKitClient.useAuth = false;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "supaapps-api-kit-client",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "description": "A versatile, type-safe API kit client designed for TypeScript applications.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -6,12 +6,18 @@ type AuthTokenCallback = () => Promise<string>;
6
6
 
7
7
  export class ApiKitClient {
8
8
  private static instance: AxiosInstance;
9
- private static authTokenCallback: AuthTokenCallback;
10
- private static unauthorizationCallback: UnauthorizationCallback;
9
+ private static authTokenCallback?: AuthTokenCallback;
10
+ private static unauthorizationCallback?: UnauthorizationCallback;
11
+ private static useAuth: boolean = false;
12
+
13
+ public static initialize(baseURL: string, authTokenCallback?: AuthTokenCallback, unauthorizationCallback?: UnauthorizationCallback, useAuth: boolean = false): void {
14
+ if (useAuth && !authTokenCallback) {
15
+ throw new Error("authTokenCallback must be provided if useAuth is true.");
16
+ }
11
17
 
12
- public static initialize(baseURL: string, authTokenCallback: AuthTokenCallback, unauthorizationCallback?: UnauthorizationCallback): void {
13
18
  this.authTokenCallback = authTokenCallback;
14
19
  this.unauthorizationCallback = unauthorizationCallback;
20
+ this.useAuth = useAuth;
15
21
 
16
22
  this.instance = axios.create({ baseURL });
17
23
 
@@ -20,7 +26,7 @@ export class ApiKitClient {
20
26
 
21
27
  private static setupInterceptors(): void {
22
28
  this.instance.interceptors.request.use(async (config) => {
23
- if (this.authTokenCallback) {
29
+ if (this.useAuth && this.authTokenCallback) {
24
30
  const authToken = await this.authTokenCallback();
25
31
  config.headers.Authorization = `Bearer ${authToken}`;
26
32
  }