supaapps-api-kit-client 0.8.0 → 0.9.2

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.
@@ -8,6 +8,9 @@ on:
8
8
  jobs:
9
9
  build:
10
10
  runs-on: ubuntu-latest
11
+ permissions:
12
+ id-token: write
13
+ contents: read
11
14
  steps:
12
15
  - name: Checkout code
13
16
  uses: actions/checkout@v2
@@ -28,6 +31,4 @@ jobs:
28
31
  run: npm run build
29
32
 
30
33
  - name: Publish to npm
31
- run: npm publish
32
- env:
33
- NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
34
+ run: npm publish --provenance --access public
@@ -36,17 +36,41 @@ class ApiKitClient {
36
36
  apiClient.axiosInstance.interceptors.request.use((config) => __awaiter(this, void 0, void 0, function* () {
37
37
  if (apiClient.useAuth && apiClient.authTokenCallback) {
38
38
  const authToken = yield apiClient.authTokenCallback();
39
- config.headers.Authorization = `Bearer ${authToken}`;
39
+ const headers = axios_1.AxiosHeaders.from(config.headers);
40
+ headers.set('Authorization', `Bearer ${authToken}`);
41
+ config.headers = headers;
40
42
  }
41
43
  return config;
42
44
  }));
43
- apiClient.axiosInstance.interceptors.response.use((response) => response, (error) => {
44
- var _a;
45
- if (((_a = error.response) === null || _a === void 0 ? void 0 : _a.status) === 401 && apiClient.unauthorizedCallback) {
45
+ apiClient.axiosInstance.interceptors.response.use((response) => response, (error) => __awaiter(this, void 0, void 0, function* () {
46
+ var _a, _b;
47
+ const originalRequest = error.config;
48
+ const shouldRetry = ((_a = error.response) === null || _a === void 0 ? void 0 : _a.status) === 401 &&
49
+ apiClient.useAuth &&
50
+ apiClient.authTokenCallback &&
51
+ originalRequest &&
52
+ !originalRequest._retry;
53
+ if (shouldRetry) {
54
+ originalRequest._retry = true;
55
+ try {
56
+ const authToken = yield apiClient.authTokenCallback();
57
+ const headers = axios_1.AxiosHeaders.from(originalRequest.headers);
58
+ headers.set('Authorization', `Bearer ${authToken}`);
59
+ originalRequest.headers = headers;
60
+ return apiClient.axiosInstance.request(originalRequest);
61
+ }
62
+ catch (refreshError) {
63
+ if (apiClient.unauthorizedCallback) {
64
+ apiClient.unauthorizedCallback();
65
+ }
66
+ return Promise.reject(refreshError);
67
+ }
68
+ }
69
+ if (((_b = error.response) === null || _b === void 0 ? void 0 : _b.status) === 401 && apiClient.unauthorizedCallback) {
46
70
  apiClient.unauthorizedCallback();
47
71
  }
48
72
  return Promise.reject(error);
49
- });
73
+ }));
50
74
  }
51
75
  checkInitialization() {
52
76
  if (!ApiKitClient.apiClients[this.apiClientKey]) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "supaapps-api-kit-client",
3
- "version": "0.8.0",
3
+ "version": "0.9.2",
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",
@@ -8,6 +8,10 @@
8
8
  "test": "jest",
9
9
  "build": "tsc"
10
10
  },
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "git+https://github.com/supaapps/supaapps-api-kit-client.git"
14
+ },
11
15
  "author": "Supaapps GmbH",
12
16
  "license": "MIT",
13
17
  "dependencies": {
@@ -1,4 +1,10 @@
1
- import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse, AxiosError } from 'axios';
1
+ import axios, {
2
+ AxiosError,
3
+ AxiosHeaders,
4
+ AxiosInstance,
5
+ AxiosRequestConfig,
6
+ AxiosResponse,
7
+ } from 'axios';
2
8
  import { PaginatedResponse } from './types';
3
9
 
4
10
  type UnAuthorizedCallback = () => void;
@@ -48,12 +54,38 @@ export class ApiKitClient {
48
54
  apiClient.axiosInstance.interceptors.request.use(async (config) => {
49
55
  if (apiClient.useAuth && apiClient.authTokenCallback) {
50
56
  const authToken = await apiClient.authTokenCallback();
51
- config.headers.Authorization = `Bearer ${authToken}`;
57
+ const headers = AxiosHeaders.from(config.headers);
58
+ headers.set('Authorization', `Bearer ${authToken}`);
59
+ config.headers = headers;
52
60
  }
53
61
  return config;
54
62
  });
55
63
 
56
- apiClient.axiosInstance.interceptors.response.use((response: AxiosResponse) => response, (error: AxiosError) => {
64
+ apiClient.axiosInstance.interceptors.response.use((response: AxiosResponse) => response, async (error: AxiosError) => {
65
+ const originalRequest = error.config;
66
+ const shouldRetry =
67
+ error.response?.status === 401 &&
68
+ apiClient.useAuth &&
69
+ apiClient.authTokenCallback &&
70
+ originalRequest &&
71
+ !(originalRequest as { _retry?: boolean })._retry;
72
+
73
+ if (shouldRetry) {
74
+ (originalRequest as { _retry?: boolean })._retry = true;
75
+ try {
76
+ const authToken = await apiClient.authTokenCallback();
77
+ const headers = AxiosHeaders.from(originalRequest.headers);
78
+ headers.set('Authorization', `Bearer ${authToken}`);
79
+ originalRequest.headers = headers;
80
+ return apiClient.axiosInstance.request(originalRequest);
81
+ } catch (refreshError) {
82
+ if (apiClient.unauthorizedCallback) {
83
+ apiClient.unauthorizedCallback();
84
+ }
85
+ return Promise.reject(refreshError);
86
+ }
87
+ }
88
+
57
89
  if (error.response?.status === 401 && apiClient.unauthorizedCallback) {
58
90
  apiClient.unauthorizedCallback();
59
91
  }