@justins-home/http-client 1.0.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.
@@ -0,0 +1,15 @@
1
+  WARN  Issue while reading "/home/runner/work/justins-home-platform-ui/justins-home-platform-ui/.npmrc". Failed to replace env in config: ${NODE_AUTH_TOKEN}
2
+
3
+ > @justins-home/http-client@1.0.0 build /home/runner/work/justins-home-platform-ui/justins-home-platform-ui/packages/http-client
4
+ > tsup src/index.ts --format esm,cjs
5
+
6
+ CLI Building entry: src/index.ts
7
+ CLI Using tsconfig: tsconfig.json
8
+ CLI tsup v8.5.1
9
+ CLI Target: es2023
10
+ ESM Build start
11
+ CJS Build start
12
+ ESM dist/index.mjs 1.67 KB
13
+ ESM ⚡️ Build success in 69ms
14
+ CJS dist/index.js 3.58 KB
15
+ CJS ⚡️ Build success in 70ms
package/CHANGELOG.md ADDED
@@ -0,0 +1,28 @@
1
+ # @justins-home/http-client
2
+
3
+ ## 1.0.0
4
+
5
+ ### Major Changes
6
+
7
+ - 26690e2: release initial packages
8
+
9
+ ### Minor Changes
10
+
11
+ - aced108: added minor pumb
12
+
13
+ ### Patch Changes
14
+
15
+ - Updated dependencies [aced108]
16
+ - Updated dependencies [26690e2]
17
+ - @justins-home/types@1.0.0
18
+
19
+ ## 0.1.0
20
+
21
+ ### Minor Changes
22
+
23
+ - 3f39ece: first release
24
+
25
+ ### Patch Changes
26
+
27
+ - Updated dependencies [3f39ece]
28
+ - @justins-home/types@0.0.2
package/dist/index.js ADDED
@@ -0,0 +1,118 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/index.ts
31
+ var index_exports = {};
32
+ __export(index_exports, {
33
+ authClient: () => authClient,
34
+ createHttpClient: () => createHttpClient,
35
+ publicClient: () => publicClient,
36
+ registerAuthInterceptor: () => registerAuthInterceptor,
37
+ registerErrorInterceptor: () => registerErrorInterceptor,
38
+ request: () => request
39
+ });
40
+ module.exports = __toCommonJS(index_exports);
41
+
42
+ // src/clients/publicClient.ts
43
+ var import_axios = __toESM(require("axios"));
44
+ var baseURL = process.env.NEXT_PUBLIC_API_URL;
45
+ if (!baseURL) {
46
+ throw new Error("NEXT_PUBLIC_API_URL is not defined");
47
+ }
48
+ var publicClient = import_axios.default.create({
49
+ baseURL,
50
+ headers: {
51
+ "Content-Type": "application/json"
52
+ }
53
+ });
54
+
55
+ // src/clients/authClient.ts
56
+ var import_axios2 = __toESM(require("axios"));
57
+ var baseURL2 = process.env.NEXT_PUBLIC_API_URL;
58
+ if (!baseURL2) {
59
+ throw new Error("NEXT_PUBLIC_API_URL is not defined");
60
+ }
61
+ var authClient = import_axios2.default.create({
62
+ baseURL: baseURL2,
63
+ headers: {
64
+ "Content-Type": "application/json"
65
+ }
66
+ });
67
+
68
+ // src/helpers/request.ts
69
+ async function request(client, promise) {
70
+ const response = await promise;
71
+ const payload = response.data;
72
+ return payload.data;
73
+ }
74
+
75
+ // src/createHttpClient.ts
76
+ var import_axios3 = __toESM(require("axios"));
77
+ function createHttpClient(baseURL3) {
78
+ return import_axios3.default.create({
79
+ baseURL: baseURL3,
80
+ headers: {
81
+ "Content-Type": "application/json"
82
+ }
83
+ });
84
+ }
85
+
86
+ // src/interceptors/auth.interceptor.ts
87
+ function registerAuthInterceptor(getToken) {
88
+ authClient.interceptors.request.use((config) => {
89
+ const token = getToken();
90
+ if (token) {
91
+ config.headers = config.headers ?? {};
92
+ config.headers.Authorization = `Bearer ${token}`;
93
+ }
94
+ return config;
95
+ });
96
+ }
97
+
98
+ // src/interceptors/error.interceptor.ts
99
+ function registerErrorInterceptor(client) {
100
+ client.interceptors.response.use(
101
+ (response) => response,
102
+ (error) => {
103
+ if (error.response?.status === 401) {
104
+ console.warn("Unauthorized request");
105
+ }
106
+ return Promise.reject(error);
107
+ }
108
+ );
109
+ }
110
+ // Annotate the CommonJS export names for ESM import in node:
111
+ 0 && (module.exports = {
112
+ authClient,
113
+ createHttpClient,
114
+ publicClient,
115
+ registerAuthInterceptor,
116
+ registerErrorInterceptor,
117
+ request
118
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,76 @@
1
+ // src/clients/publicClient.ts
2
+ import axios from "axios";
3
+ var baseURL = process.env.NEXT_PUBLIC_API_URL;
4
+ if (!baseURL) {
5
+ throw new Error("NEXT_PUBLIC_API_URL is not defined");
6
+ }
7
+ var publicClient = axios.create({
8
+ baseURL,
9
+ headers: {
10
+ "Content-Type": "application/json"
11
+ }
12
+ });
13
+
14
+ // src/clients/authClient.ts
15
+ import axios2 from "axios";
16
+ var baseURL2 = process.env.NEXT_PUBLIC_API_URL;
17
+ if (!baseURL2) {
18
+ throw new Error("NEXT_PUBLIC_API_URL is not defined");
19
+ }
20
+ var authClient = axios2.create({
21
+ baseURL: baseURL2,
22
+ headers: {
23
+ "Content-Type": "application/json"
24
+ }
25
+ });
26
+
27
+ // src/helpers/request.ts
28
+ async function request(client, promise) {
29
+ const response = await promise;
30
+ const payload = response.data;
31
+ return payload.data;
32
+ }
33
+
34
+ // src/createHttpClient.ts
35
+ import axios3 from "axios";
36
+ function createHttpClient(baseURL3) {
37
+ return axios3.create({
38
+ baseURL: baseURL3,
39
+ headers: {
40
+ "Content-Type": "application/json"
41
+ }
42
+ });
43
+ }
44
+
45
+ // src/interceptors/auth.interceptor.ts
46
+ function registerAuthInterceptor(getToken) {
47
+ authClient.interceptors.request.use((config) => {
48
+ const token = getToken();
49
+ if (token) {
50
+ config.headers = config.headers ?? {};
51
+ config.headers.Authorization = `Bearer ${token}`;
52
+ }
53
+ return config;
54
+ });
55
+ }
56
+
57
+ // src/interceptors/error.interceptor.ts
58
+ function registerErrorInterceptor(client) {
59
+ client.interceptors.response.use(
60
+ (response) => response,
61
+ (error) => {
62
+ if (error.response?.status === 401) {
63
+ console.warn("Unauthorized request");
64
+ }
65
+ return Promise.reject(error);
66
+ }
67
+ );
68
+ }
69
+ export {
70
+ authClient,
71
+ createHttpClient,
72
+ publicClient,
73
+ registerAuthInterceptor,
74
+ registerErrorInterceptor,
75
+ request
76
+ };
package/package.json ADDED
@@ -0,0 +1,19 @@
1
+ {
2
+ "name": "@justins-home/http-client",
3
+ "version": "1.0.0",
4
+ "private": false,
5
+ "main": "src/index.ts",
6
+ "dependencies": {
7
+ "axios": "^1.13.6",
8
+ "@justins-home/types": "1.0.0"
9
+ },
10
+ "publishConfig": {
11
+ "access": "public"
12
+ },
13
+ "scripts": {
14
+ "lint": "eslint src",
15
+ "check-types": "tsc --noEmit -p ../../tsconfig.base.json",
16
+ "build": "tsup src/index.ts --format esm,cjs",
17
+ "test": "vitest"
18
+ }
19
+ }
@@ -0,0 +1,14 @@
1
+ import axios from 'axios';
2
+
3
+ const baseURL = process.env.NEXT_PUBLIC_API_URL;
4
+
5
+ if (!baseURL) {
6
+ throw new Error('NEXT_PUBLIC_API_URL is not defined');
7
+ }
8
+
9
+ export const authClient = axios.create({
10
+ baseURL,
11
+ headers: {
12
+ 'Content-Type': 'application/json',
13
+ },
14
+ });
@@ -0,0 +1,14 @@
1
+ import axios from 'axios';
2
+
3
+ const baseURL = process.env.NEXT_PUBLIC_API_URL;
4
+
5
+ if (!baseURL) {
6
+ throw new Error('NEXT_PUBLIC_API_URL is not defined');
7
+ }
8
+
9
+ export const publicClient = axios.create({
10
+ baseURL,
11
+ headers: {
12
+ 'Content-Type': 'application/json',
13
+ },
14
+ });
@@ -0,0 +1,10 @@
1
+ import axios, { AxiosInstance } from 'axios';
2
+
3
+ export function createHttpClient(baseURL: string): AxiosInstance {
4
+ return axios.create({
5
+ baseURL,
6
+ headers: {
7
+ 'Content-Type': 'application/json',
8
+ },
9
+ });
10
+ }
@@ -0,0 +1,13 @@
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 ADDED
@@ -0,0 +1,9 @@
1
+ export * from './clients/publicClient';
2
+ export * from './clients/authClient';
3
+
4
+ export * from './helpers/request';
5
+
6
+ export * from './createHttpClient';
7
+
8
+ export * from './interceptors/auth.interceptor';
9
+ export * from './interceptors/error.interceptor';
@@ -0,0 +1,14 @@
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
+ }
@@ -0,0 +1,14 @@
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 ADDED
@@ -0,0 +1,3 @@
1
+ {
2
+ "extends": "../../tsconfig.base.json",
3
+ }