@justins-home/http-client 1.0.6 → 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 +8 -1
- package/dist/index.d.ts +8 -1
- package/dist/index.js +37 -14
- package/dist/index.mjs +33 -13
- package/package.json +9 -2
package/dist/index.d.mts
CHANGED
|
@@ -1,14 +1,21 @@
|
|
|
1
1
|
import * as axios from 'axios';
|
|
2
2
|
import { AxiosInstance } from 'axios';
|
|
3
3
|
|
|
4
|
+
declare function setApiBaseURL(baseURL: string): void;
|
|
5
|
+
declare function getApiBaseURL(): string;
|
|
6
|
+
|
|
4
7
|
declare const publicClient: axios.AxiosInstance;
|
|
5
8
|
|
|
6
9
|
declare const authClient: axios.AxiosInstance;
|
|
7
10
|
|
|
8
11
|
declare function request<T>(client: AxiosInstance, promise: Promise<any>): Promise<T>;
|
|
9
12
|
|
|
13
|
+
declare function configureHttpClient(options: {
|
|
14
|
+
baseURL?: string;
|
|
15
|
+
}): void;
|
|
10
16
|
declare function initApiToken(options: {
|
|
11
17
|
getToken: () => string | null;
|
|
18
|
+
baseURL?: string;
|
|
12
19
|
}): void;
|
|
13
20
|
|
|
14
21
|
declare function createHttpClient(baseURL: string): AxiosInstance;
|
|
@@ -17,4 +24,4 @@ declare function registerAuthInterceptor(getToken: () => string | null): void;
|
|
|
17
24
|
|
|
18
25
|
declare function registerErrorInterceptor(client: any): void;
|
|
19
26
|
|
|
20
|
-
export { authClient, createHttpClient, initApiToken, publicClient, registerAuthInterceptor, registerErrorInterceptor, request };
|
|
27
|
+
export { authClient, configureHttpClient, createHttpClient, getApiBaseURL, initApiToken, publicClient, registerAuthInterceptor, registerErrorInterceptor, request, setApiBaseURL };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,14 +1,21 @@
|
|
|
1
1
|
import * as axios from 'axios';
|
|
2
2
|
import { AxiosInstance } from 'axios';
|
|
3
3
|
|
|
4
|
+
declare function setApiBaseURL(baseURL: string): void;
|
|
5
|
+
declare function getApiBaseURL(): string;
|
|
6
|
+
|
|
4
7
|
declare const publicClient: axios.AxiosInstance;
|
|
5
8
|
|
|
6
9
|
declare const authClient: axios.AxiosInstance;
|
|
7
10
|
|
|
8
11
|
declare function request<T>(client: AxiosInstance, promise: Promise<any>): Promise<T>;
|
|
9
12
|
|
|
13
|
+
declare function configureHttpClient(options: {
|
|
14
|
+
baseURL?: string;
|
|
15
|
+
}): void;
|
|
10
16
|
declare function initApiToken(options: {
|
|
11
17
|
getToken: () => string | null;
|
|
18
|
+
baseURL?: string;
|
|
12
19
|
}): void;
|
|
13
20
|
|
|
14
21
|
declare function createHttpClient(baseURL: string): AxiosInstance;
|
|
@@ -17,4 +24,4 @@ declare function registerAuthInterceptor(getToken: () => string | null): void;
|
|
|
17
24
|
|
|
18
25
|
declare function registerErrorInterceptor(client: any): void;
|
|
19
26
|
|
|
20
|
-
export { authClient, createHttpClient, initApiToken, publicClient, registerAuthInterceptor, registerErrorInterceptor, request };
|
|
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,16 +1,23 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@justins-home/http-client",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
7
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
|
+
},
|
|
8
15
|
"files": [
|
|
9
16
|
"dist"
|
|
10
17
|
],
|
|
11
18
|
"dependencies": {
|
|
12
19
|
"axios": "^1.13.6",
|
|
13
|
-
"@justins-home/types": "1.0
|
|
20
|
+
"@justins-home/types": "1.1.0"
|
|
14
21
|
},
|
|
15
22
|
"publishConfig": {
|
|
16
23
|
"access": "public"
|