@justins-home/http-client 1.0.5 → 1.1.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/dist/index.d.mts +27 -0
- package/dist/index.d.ts +27 -0
- package/dist/index.js +37 -14
- package/dist/index.mjs +33 -13
- package/package.json +17 -5
- package/.turbo/turbo-build.log +0 -14
- package/CHANGELOG.md +0 -58
- package/src/clients/authClient.ts +0 -14
- package/src/clients/publicClient.ts +0 -14
- package/src/createHttpClient.ts +0 -10
- package/src/helpers/initClient.ts +0 -11
- package/src/helpers/request.ts +0 -13
- package/src/index.ts +0 -10
- package/src/interceptors/auth.interceptor.ts +0 -14
- package/src/interceptors/error.interceptor.ts +0 -14
- package/tsconfig.json +0 -3
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import * as axios from 'axios';
|
|
2
|
+
import { AxiosInstance } from 'axios';
|
|
3
|
+
|
|
4
|
+
declare function setApiBaseURL(baseURL: string): void;
|
|
5
|
+
declare function getApiBaseURL(): string;
|
|
6
|
+
|
|
7
|
+
declare const publicClient: axios.AxiosInstance;
|
|
8
|
+
|
|
9
|
+
declare const authClient: axios.AxiosInstance;
|
|
10
|
+
|
|
11
|
+
declare function request<T>(client: AxiosInstance, promise: Promise<any>): Promise<T>;
|
|
12
|
+
|
|
13
|
+
declare function configureHttpClient(options: {
|
|
14
|
+
baseURL?: string;
|
|
15
|
+
}): void;
|
|
16
|
+
declare function initApiToken(options: {
|
|
17
|
+
getToken: () => string | null;
|
|
18
|
+
baseURL?: string;
|
|
19
|
+
}): void;
|
|
20
|
+
|
|
21
|
+
declare function createHttpClient(baseURL: string): AxiosInstance;
|
|
22
|
+
|
|
23
|
+
declare function registerAuthInterceptor(getToken: () => string | null): void;
|
|
24
|
+
|
|
25
|
+
declare function registerErrorInterceptor(client: any): void;
|
|
26
|
+
|
|
27
|
+
export { authClient, configureHttpClient, createHttpClient, getApiBaseURL, initApiToken, publicClient, registerAuthInterceptor, registerErrorInterceptor, request, setApiBaseURL };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import * as axios from 'axios';
|
|
2
|
+
import { AxiosInstance } from 'axios';
|
|
3
|
+
|
|
4
|
+
declare function setApiBaseURL(baseURL: string): void;
|
|
5
|
+
declare function getApiBaseURL(): string;
|
|
6
|
+
|
|
7
|
+
declare const publicClient: axios.AxiosInstance;
|
|
8
|
+
|
|
9
|
+
declare const authClient: axios.AxiosInstance;
|
|
10
|
+
|
|
11
|
+
declare function request<T>(client: AxiosInstance, promise: Promise<any>): Promise<T>;
|
|
12
|
+
|
|
13
|
+
declare function configureHttpClient(options: {
|
|
14
|
+
baseURL?: string;
|
|
15
|
+
}): void;
|
|
16
|
+
declare function initApiToken(options: {
|
|
17
|
+
getToken: () => string | null;
|
|
18
|
+
baseURL?: string;
|
|
19
|
+
}): void;
|
|
20
|
+
|
|
21
|
+
declare function createHttpClient(baseURL: string): AxiosInstance;
|
|
22
|
+
|
|
23
|
+
declare function registerAuthInterceptor(getToken: () => string | null): void;
|
|
24
|
+
|
|
25
|
+
declare function registerErrorInterceptor(client: any): void;
|
|
26
|
+
|
|
27
|
+
export { authClient, configureHttpClient, createHttpClient, getApiBaseURL, initApiToken, publicClient, registerAuthInterceptor, registerErrorInterceptor, request, setApiBaseURL };
|
package/dist/index.js
CHANGED
|
@@ -31,40 +31,54 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
31
31
|
var index_exports = {};
|
|
32
32
|
__export(index_exports, {
|
|
33
33
|
authClient: () => authClient,
|
|
34
|
+
configureHttpClient: () => configureHttpClient,
|
|
34
35
|
createHttpClient: () => createHttpClient,
|
|
36
|
+
getApiBaseURL: () => getApiBaseURL,
|
|
35
37
|
initApiToken: () => initApiToken,
|
|
36
38
|
publicClient: () => publicClient,
|
|
37
39
|
registerAuthInterceptor: () => registerAuthInterceptor,
|
|
38
40
|
registerErrorInterceptor: () => registerErrorInterceptor,
|
|
39
|
-
request: () => request
|
|
41
|
+
request: () => request,
|
|
42
|
+
setApiBaseURL: () => setApiBaseURL
|
|
40
43
|
});
|
|
41
44
|
module.exports = __toCommonJS(index_exports);
|
|
42
45
|
|
|
46
|
+
// src/config.ts
|
|
47
|
+
var configuredBaseURL;
|
|
48
|
+
function setApiBaseURL(baseURL) {
|
|
49
|
+
configuredBaseURL = baseURL;
|
|
50
|
+
}
|
|
51
|
+
function getApiBaseURL() {
|
|
52
|
+
const baseURL = configuredBaseURL ?? process.env.API_URL;
|
|
53
|
+
if (!baseURL) {
|
|
54
|
+
throw new Error("API_URL is not defined");
|
|
55
|
+
}
|
|
56
|
+
return baseURL;
|
|
57
|
+
}
|
|
58
|
+
|
|
43
59
|
// src/clients/publicClient.ts
|
|
44
60
|
var import_axios = __toESM(require("axios"));
|
|
45
|
-
var baseURL = process.env.API_URL;
|
|
46
|
-
if (!baseURL) {
|
|
47
|
-
throw new Error("API_URL is not defined");
|
|
48
|
-
}
|
|
49
61
|
var publicClient = import_axios.default.create({
|
|
50
|
-
baseURL,
|
|
51
62
|
headers: {
|
|
52
63
|
"Content-Type": "application/json"
|
|
53
64
|
}
|
|
54
65
|
});
|
|
66
|
+
publicClient.interceptors.request.use((config) => {
|
|
67
|
+
config.baseURL ??= getApiBaseURL();
|
|
68
|
+
return config;
|
|
69
|
+
});
|
|
55
70
|
|
|
56
71
|
// src/clients/authClient.ts
|
|
57
72
|
var import_axios2 = __toESM(require("axios"));
|
|
58
|
-
var baseURL2 = process.env.API_URL;
|
|
59
|
-
if (!baseURL2) {
|
|
60
|
-
throw new Error("API_URL is not defined");
|
|
61
|
-
}
|
|
62
73
|
var authClient = import_axios2.default.create({
|
|
63
|
-
baseURL: baseURL2,
|
|
64
74
|
headers: {
|
|
65
75
|
"Content-Type": "application/json"
|
|
66
76
|
}
|
|
67
77
|
});
|
|
78
|
+
authClient.interceptors.request.use((config) => {
|
|
79
|
+
config.baseURL ??= getApiBaseURL();
|
|
80
|
+
return config;
|
|
81
|
+
});
|
|
68
82
|
|
|
69
83
|
// src/helpers/request.ts
|
|
70
84
|
async function request(client, promise) {
|
|
@@ -87,7 +101,13 @@ function registerAuthInterceptor(getToken) {
|
|
|
87
101
|
|
|
88
102
|
// src/helpers/initClient.ts
|
|
89
103
|
var initialized = false;
|
|
104
|
+
function configureHttpClient(options) {
|
|
105
|
+
if (options.baseURL) {
|
|
106
|
+
setApiBaseURL(options.baseURL);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
90
109
|
function initApiToken(options) {
|
|
110
|
+
configureHttpClient(options);
|
|
91
111
|
if (initialized) return;
|
|
92
112
|
registerAuthInterceptor(options.getToken);
|
|
93
113
|
initialized = true;
|
|
@@ -95,9 +115,9 @@ function initApiToken(options) {
|
|
|
95
115
|
|
|
96
116
|
// src/createHttpClient.ts
|
|
97
117
|
var import_axios3 = __toESM(require("axios"));
|
|
98
|
-
function createHttpClient(
|
|
118
|
+
function createHttpClient(baseURL) {
|
|
99
119
|
return import_axios3.default.create({
|
|
100
|
-
baseURL
|
|
120
|
+
baseURL,
|
|
101
121
|
headers: {
|
|
102
122
|
"Content-Type": "application/json"
|
|
103
123
|
}
|
|
@@ -119,10 +139,13 @@ function registerErrorInterceptor(client) {
|
|
|
119
139
|
// Annotate the CommonJS export names for ESM import in node:
|
|
120
140
|
0 && (module.exports = {
|
|
121
141
|
authClient,
|
|
142
|
+
configureHttpClient,
|
|
122
143
|
createHttpClient,
|
|
144
|
+
getApiBaseURL,
|
|
123
145
|
initApiToken,
|
|
124
146
|
publicClient,
|
|
125
147
|
registerAuthInterceptor,
|
|
126
148
|
registerErrorInterceptor,
|
|
127
|
-
request
|
|
149
|
+
request,
|
|
150
|
+
setApiBaseURL
|
|
128
151
|
});
|
package/dist/index.mjs
CHANGED
|
@@ -1,28 +1,39 @@
|
|
|
1
|
+
// src/config.ts
|
|
2
|
+
var configuredBaseURL;
|
|
3
|
+
function setApiBaseURL(baseURL) {
|
|
4
|
+
configuredBaseURL = baseURL;
|
|
5
|
+
}
|
|
6
|
+
function getApiBaseURL() {
|
|
7
|
+
const baseURL = configuredBaseURL ?? process.env.API_URL;
|
|
8
|
+
if (!baseURL) {
|
|
9
|
+
throw new Error("API_URL is not defined");
|
|
10
|
+
}
|
|
11
|
+
return baseURL;
|
|
12
|
+
}
|
|
13
|
+
|
|
1
14
|
// src/clients/publicClient.ts
|
|
2
15
|
import axios from "axios";
|
|
3
|
-
var baseURL = process.env.API_URL;
|
|
4
|
-
if (!baseURL) {
|
|
5
|
-
throw new Error("API_URL is not defined");
|
|
6
|
-
}
|
|
7
16
|
var publicClient = axios.create({
|
|
8
|
-
baseURL,
|
|
9
17
|
headers: {
|
|
10
18
|
"Content-Type": "application/json"
|
|
11
19
|
}
|
|
12
20
|
});
|
|
21
|
+
publicClient.interceptors.request.use((config) => {
|
|
22
|
+
config.baseURL ??= getApiBaseURL();
|
|
23
|
+
return config;
|
|
24
|
+
});
|
|
13
25
|
|
|
14
26
|
// src/clients/authClient.ts
|
|
15
27
|
import axios2 from "axios";
|
|
16
|
-
var baseURL2 = process.env.API_URL;
|
|
17
|
-
if (!baseURL2) {
|
|
18
|
-
throw new Error("API_URL is not defined");
|
|
19
|
-
}
|
|
20
28
|
var authClient = axios2.create({
|
|
21
|
-
baseURL: baseURL2,
|
|
22
29
|
headers: {
|
|
23
30
|
"Content-Type": "application/json"
|
|
24
31
|
}
|
|
25
32
|
});
|
|
33
|
+
authClient.interceptors.request.use((config) => {
|
|
34
|
+
config.baseURL ??= getApiBaseURL();
|
|
35
|
+
return config;
|
|
36
|
+
});
|
|
26
37
|
|
|
27
38
|
// src/helpers/request.ts
|
|
28
39
|
async function request(client, promise) {
|
|
@@ -45,7 +56,13 @@ function registerAuthInterceptor(getToken) {
|
|
|
45
56
|
|
|
46
57
|
// src/helpers/initClient.ts
|
|
47
58
|
var initialized = false;
|
|
59
|
+
function configureHttpClient(options) {
|
|
60
|
+
if (options.baseURL) {
|
|
61
|
+
setApiBaseURL(options.baseURL);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
48
64
|
function initApiToken(options) {
|
|
65
|
+
configureHttpClient(options);
|
|
49
66
|
if (initialized) return;
|
|
50
67
|
registerAuthInterceptor(options.getToken);
|
|
51
68
|
initialized = true;
|
|
@@ -53,9 +70,9 @@ function initApiToken(options) {
|
|
|
53
70
|
|
|
54
71
|
// src/createHttpClient.ts
|
|
55
72
|
import axios3 from "axios";
|
|
56
|
-
function createHttpClient(
|
|
73
|
+
function createHttpClient(baseURL) {
|
|
57
74
|
return axios3.create({
|
|
58
|
-
baseURL
|
|
75
|
+
baseURL,
|
|
59
76
|
headers: {
|
|
60
77
|
"Content-Type": "application/json"
|
|
61
78
|
}
|
|
@@ -76,10 +93,13 @@ function registerErrorInterceptor(client) {
|
|
|
76
93
|
}
|
|
77
94
|
export {
|
|
78
95
|
authClient,
|
|
96
|
+
configureHttpClient,
|
|
79
97
|
createHttpClient,
|
|
98
|
+
getApiBaseURL,
|
|
80
99
|
initApiToken,
|
|
81
100
|
publicClient,
|
|
82
101
|
registerAuthInterceptor,
|
|
83
102
|
registerErrorInterceptor,
|
|
84
|
-
request
|
|
103
|
+
request,
|
|
104
|
+
setApiBaseURL
|
|
85
105
|
};
|
package/package.json
CHANGED
|
@@ -1,19 +1,31 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@justins-home/http-client",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"private": false,
|
|
5
|
-
"main": "
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"module": "dist/index.mjs",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.mjs",
|
|
12
|
+
"require": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist"
|
|
17
|
+
],
|
|
6
18
|
"dependencies": {
|
|
7
19
|
"axios": "^1.13.6",
|
|
8
|
-
"@justins-home/types": "1.
|
|
20
|
+
"@justins-home/types": "1.1.0"
|
|
9
21
|
},
|
|
10
22
|
"publishConfig": {
|
|
11
23
|
"access": "public"
|
|
12
24
|
},
|
|
13
25
|
"scripts": {
|
|
14
26
|
"lint": "eslint src",
|
|
15
|
-
"check-types": "tsc --noEmit
|
|
16
|
-
"build": "tsup src/index.ts --format esm,cjs",
|
|
27
|
+
"check-types": "tsc --noEmit",
|
|
28
|
+
"build": "tsup src/index.ts --format esm,cjs --dts --tsconfig tsconfig.json",
|
|
17
29
|
"test": "vitest"
|
|
18
30
|
}
|
|
19
31
|
}
|
package/.turbo/turbo-build.log
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
> @justins-home/http-client@1.0.5 build /home/runner/work/justins-home-platform-ui/justins-home-platform-ui/packages/http-client
|
|
3
|
-
> tsup src/index.ts --format esm,cjs
|
|
4
|
-
|
|
5
|
-
[34mCLI[39m Building entry: src/index.ts
|
|
6
|
-
[34mCLI[39m Using tsconfig: tsconfig.json
|
|
7
|
-
[34mCLI[39m tsup v8.5.1
|
|
8
|
-
[34mCLI[39m Target: es2023
|
|
9
|
-
[34mESM[39m Build start
|
|
10
|
-
[34mCJS[39m Build start
|
|
11
|
-
[32mCJS[39m [1mdist/index.js [22m[32m3.76 KB[39m
|
|
12
|
-
[32mCJS[39m ⚡️ Build success in 57ms
|
|
13
|
-
[32mESM[39m [1mdist/index.mjs [22m[32m1.82 KB[39m
|
|
14
|
-
[32mESM[39m ⚡️ Build success in 58ms
|
package/CHANGELOG.md
DELETED
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
# @justins-home/http-client
|
|
2
|
-
|
|
3
|
-
## 1.0.5
|
|
4
|
-
|
|
5
|
-
### Patch Changes
|
|
6
|
-
|
|
7
|
-
- updated env variable name
|
|
8
|
-
|
|
9
|
-
## 1.0.4
|
|
10
|
-
|
|
11
|
-
### Patch Changes
|
|
12
|
-
|
|
13
|
-
- fix validated script
|
|
14
|
-
|
|
15
|
-
## 1.0.3
|
|
16
|
-
|
|
17
|
-
### Patch Changes
|
|
18
|
-
|
|
19
|
-
- created a setup auth handler for handling token
|
|
20
|
-
|
|
21
|
-
## 1.0.2
|
|
22
|
-
|
|
23
|
-
### Patch Changes
|
|
24
|
-
|
|
25
|
-
- added more typing to TS scribe doc
|
|
26
|
-
|
|
27
|
-
## 1.0.1
|
|
28
|
-
|
|
29
|
-
### Patch Changes
|
|
30
|
-
|
|
31
|
-
- added ApiError to normalizeError
|
|
32
|
-
|
|
33
|
-
## 1.0.0
|
|
34
|
-
|
|
35
|
-
### Major Changes
|
|
36
|
-
|
|
37
|
-
- 26690e2: release initial packages
|
|
38
|
-
|
|
39
|
-
### Minor Changes
|
|
40
|
-
|
|
41
|
-
- aced108: added minor pumb
|
|
42
|
-
|
|
43
|
-
### Patch Changes
|
|
44
|
-
|
|
45
|
-
- Updated dependencies [aced108]
|
|
46
|
-
- Updated dependencies [26690e2]
|
|
47
|
-
- @justins-home/types@1.0.0
|
|
48
|
-
|
|
49
|
-
## 0.1.0
|
|
50
|
-
|
|
51
|
-
### Minor Changes
|
|
52
|
-
|
|
53
|
-
- 3f39ece: first release
|
|
54
|
-
|
|
55
|
-
### Patch Changes
|
|
56
|
-
|
|
57
|
-
- Updated dependencies [3f39ece]
|
|
58
|
-
- @justins-home/types@0.0.2
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import axios from 'axios';
|
|
2
|
-
|
|
3
|
-
const baseURL = process.env.API_URL;
|
|
4
|
-
|
|
5
|
-
if (!baseURL) {
|
|
6
|
-
throw new Error('API_URL is not defined');
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
export const authClient = axios.create({
|
|
10
|
-
baseURL,
|
|
11
|
-
headers: {
|
|
12
|
-
'Content-Type': 'application/json',
|
|
13
|
-
},
|
|
14
|
-
});
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import axios from 'axios';
|
|
2
|
-
|
|
3
|
-
const baseURL = process.env.API_URL;
|
|
4
|
-
|
|
5
|
-
if (!baseURL) {
|
|
6
|
-
throw new Error('API_URL is not defined');
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
export const publicClient = axios.create({
|
|
10
|
-
baseURL,
|
|
11
|
-
headers: {
|
|
12
|
-
'Content-Type': 'application/json',
|
|
13
|
-
},
|
|
14
|
-
});
|
package/src/createHttpClient.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { registerAuthInterceptor } from '../interceptors/auth.interceptor';
|
|
2
|
-
|
|
3
|
-
let initialized = false;
|
|
4
|
-
|
|
5
|
-
export function initApiToken(options: { getToken: () => string | null }) {
|
|
6
|
-
if (initialized) return;
|
|
7
|
-
|
|
8
|
-
registerAuthInterceptor(options.getToken);
|
|
9
|
-
|
|
10
|
-
initialized = true;
|
|
11
|
-
}
|
package/src/helpers/request.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { AxiosInstance } from 'axios';
|
|
2
|
-
import { ApiResponse } from '@justins-home/types';
|
|
3
|
-
|
|
4
|
-
export async function request<T>(
|
|
5
|
-
client: AxiosInstance,
|
|
6
|
-
promise: Promise<any>,
|
|
7
|
-
): Promise<T> {
|
|
8
|
-
const response = await promise;
|
|
9
|
-
|
|
10
|
-
const payload = response.data as ApiResponse<T>;
|
|
11
|
-
|
|
12
|
-
return payload.data as T;
|
|
13
|
-
}
|
package/src/index.ts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
export * from './clients/publicClient';
|
|
2
|
-
export * from './clients/authClient';
|
|
3
|
-
|
|
4
|
-
export * from './helpers/request';
|
|
5
|
-
export * from './helpers/initClient';
|
|
6
|
-
|
|
7
|
-
export * from './createHttpClient';
|
|
8
|
-
|
|
9
|
-
export * from './interceptors/auth.interceptor';
|
|
10
|
-
export * from './interceptors/error.interceptor';
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { authClient } from '../clients/authClient';
|
|
2
|
-
|
|
3
|
-
export function registerAuthInterceptor(getToken: () => string | null) {
|
|
4
|
-
authClient.interceptors.request.use((config) => {
|
|
5
|
-
const token = getToken();
|
|
6
|
-
|
|
7
|
-
if (token) {
|
|
8
|
-
config.headers = config.headers ?? {};
|
|
9
|
-
config.headers.Authorization = `Bearer ${token}`;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
return config;
|
|
13
|
-
});
|
|
14
|
-
}
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { AxiosError } from 'axios';
|
|
2
|
-
|
|
3
|
-
export function registerErrorInterceptor(client: any) {
|
|
4
|
-
client.interceptors.response.use(
|
|
5
|
-
(response: any) => response,
|
|
6
|
-
(error: AxiosError) => {
|
|
7
|
-
if (error.response?.status === 401) {
|
|
8
|
-
console.warn('Unauthorized request');
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
return Promise.reject(error);
|
|
12
|
-
},
|
|
13
|
-
);
|
|
14
|
-
}
|
package/tsconfig.json
DELETED