@drawbridge/drawbridge-utils 0.0.14 → 0.0.15

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/fetch.cjs CHANGED
@@ -34,7 +34,6 @@ __export(fetch_exports, {
34
34
  setMemoryToken: () => setMemoryToken
35
35
  });
36
36
  module.exports = __toCommonJS(fetch_exports);
37
- var import_axios = __toESM(require("axios"), 1);
38
37
  var import_qs = __toESM(require("qs"), 1);
39
38
  var queryString = import_qs.default;
40
39
  var memoryToken = null;
@@ -47,43 +46,6 @@ var readCsrf = () => {
47
46
  const match = document.cookie.match(/(?:^|;\s*)drawbridge\.csrf=([^;]+)/);
48
47
  return match ? decodeURIComponent(match[1]) : null;
49
48
  };
50
- var client = import_axios.default.create({
51
- paramsSerializer: {
52
- serialize: (params) => import_qs.default.stringify(
53
- params,
54
- {
55
- encode: false
56
- }
57
- )
58
- }
59
- });
60
- client.interceptors.request.use((config) => {
61
- const token = config.__token || (typeof window !== "undefined" ? memoryToken : null);
62
- if (token) {
63
- config.headers["authorization"] = "Bearer " + token;
64
- }
65
- ;
66
- const csrf = readCsrf();
67
- if (csrf) {
68
- config.headers["x-drawbridge-csrf"] = csrf;
69
- }
70
- ;
71
- delete config.__token;
72
- return config;
73
- });
74
- client.interceptors.response.use(
75
- (response) => response.data,
76
- (error) => {
77
- var _a, _b;
78
- const status = (_a = error == null ? void 0 : error.response) == null ? void 0 : _a.status;
79
- const data = (_b = error == null ? void 0 : error.response) == null ? void 0 : _b.data;
80
- const message = (data == null ? void 0 : data.message) || (error == null ? void 0 : error.message) || "Request failed";
81
- const err = new Error(message);
82
- err.status = status;
83
- err.data = data;
84
- throw err;
85
- }
86
- );
87
49
  var request = async ({
88
50
  body,
89
51
  endpoint,
@@ -97,28 +59,55 @@ var request = async ({
97
59
  url = process.env.NEXT_PUBLIC_API_URI,
98
60
  ...options
99
61
  }) => {
62
+ const uri = [url, endpoint];
63
+ if (params && Object.keys(params).length > 0) {
64
+ uri.push(
65
+ "?" + import_qs.default.stringify(
66
+ params,
67
+ {
68
+ encode: false
69
+ }
70
+ )
71
+ );
72
+ }
100
73
  const isFormData = typeof FormData !== "undefined" && body instanceof FormData;
74
+ const authToken = token || (typeof window !== "undefined" ? memoryToken : null);
75
+ const csrf = readCsrf();
101
76
  const requestHeaders = {
102
77
  "origin": process.env.NEXT_PUBLIC_APP_URI,
103
- ...headers
78
+ ...headers,
79
+ ...authToken && {
80
+ "authorization": "Bearer " + authToken
81
+ },
82
+ ...csrf && {
83
+ "x-drawbridge-csrf": csrf
84
+ }
104
85
  };
105
86
  if (isFormData) {
106
87
  delete requestHeaders["content-type"];
107
88
  }
108
- ;
109
- return client.request({
110
- ...options,
111
- url: [url, endpoint].join(""),
112
- method,
113
- headers: requestHeaders,
114
- ...body !== void 0 && {
115
- data: body
116
- },
117
- ...params && {
118
- params
119
- },
120
- __token: token
121
- });
89
+ const req = await fetch(
90
+ uri.join(""),
91
+ {
92
+ ...body !== void 0 && {
93
+ body: isFormData ? body : JSON.stringify(body)
94
+ },
95
+ headers: requestHeaders,
96
+ method,
97
+ ...!("cache" in options) && !("next" in options) && {
98
+ cache: "no-store"
99
+ },
100
+ ...options
101
+ }
102
+ );
103
+ const res = await req.json();
104
+ if (!req.ok) {
105
+ const error = new Error((res == null ? void 0 : res.message) || req.statusText);
106
+ error.status = req.status;
107
+ error.data = res;
108
+ throw error;
109
+ }
110
+ return res;
122
111
  };
123
112
  // Annotate the CommonJS export names for ESM import in node:
124
113
  0 && (module.exports = {
package/dist/fetch.d.cts CHANGED
@@ -1,4 +1,3 @@
1
- import axiosLib from 'axios';
2
1
  import qs from 'qs';
3
2
 
4
3
  const queryString = qs;
@@ -6,7 +5,7 @@ const queryString = qs;
6
5
  // In-memory token store for client-side authenticated calls. Guarded
7
6
  // against server writes — Next.js shares module state across requests on
8
7
  // the server, which would leak tokens between users. Server-side callers
9
- // must pass `token` explicitly via the rest() options.
8
+ // must pass `token` explicitly via the request() options.
10
9
  let memoryToken = null;
11
10
 
12
11
  const setMemoryToken = ( value ) => {
@@ -30,56 +29,6 @@ const readCsrf = () => {
30
29
 
31
30
  };
32
31
 
33
- const client = axiosLib.create({
34
- paramsSerializer : {
35
- serialize : ( params ) => qs.stringify(
36
- params,
37
- {
38
- encode : false
39
- }
40
- )
41
- }
42
- });
43
-
44
- client.interceptors.request.use( ( config ) => {
45
-
46
- const token = config.__token || ( typeof window !== 'undefined' ? memoryToken : null );
47
-
48
- if( token ){
49
-
50
- config.headers[ 'authorization' ] = 'Bearer ' + token;
51
-
52
- }
53
- const csrf = readCsrf();
54
-
55
- if( csrf ){
56
-
57
- config.headers[ 'x-drawbridge-csrf' ] = csrf;
58
-
59
- }
60
- delete config.__token;
61
-
62
- return config;
63
-
64
- } );
65
-
66
- client.interceptors.response.use(
67
- ( response ) => response.data,
68
- ( error ) => {
69
-
70
- const status = error?.response?.status;
71
- const data = error?.response?.data;
72
- const message = data?.message || error?.message || 'Request failed';
73
- const err = new Error( message );
74
-
75
- err.status = status;
76
- err.data = data;
77
-
78
- throw err;
79
-
80
- }
81
- );
82
-
83
32
  const request = async ({
84
33
  body,
85
34
  endpoint,
@@ -94,11 +43,35 @@ const request = async ({
94
43
  ...options
95
44
  }) => {
96
45
 
46
+ const uri = [ url, endpoint ];
47
+
48
+ if( params && Object.keys( params ).length > 0 ){
49
+
50
+ uri.push(
51
+ '?' + qs.stringify(
52
+ params,
53
+ {
54
+ encode : false
55
+ }
56
+ )
57
+ );
58
+
59
+ }
60
+
97
61
  const isFormData = ( typeof FormData !== 'undefined' && body instanceof FormData );
98
62
 
63
+ const authToken = token || ( typeof window !== 'undefined' ? memoryToken : null );
64
+ const csrf = readCsrf();
65
+
99
66
  const requestHeaders = {
100
67
  'origin' : process.env.NEXT_PUBLIC_APP_URI,
101
- ...headers
68
+ ...headers,
69
+ ...( authToken && {
70
+ 'authorization' : 'Bearer ' + authToken
71
+ }),
72
+ ...( csrf && {
73
+ 'x-drawbridge-csrf' : csrf
74
+ })
102
75
  };
103
76
 
104
77
  if( isFormData ){
@@ -106,19 +79,36 @@ const request = async ({
106
79
  delete requestHeaders[ 'content-type' ];
107
80
 
108
81
  }
109
- return client.request({
110
- ...options,
111
- url : [ url, endpoint ].join( '' ),
112
- method,
113
- headers : requestHeaders,
114
- ...( body !== undefined && {
115
- data : body
116
- }),
117
- ...( params && {
118
- params
119
- }),
120
- __token : token
121
- });
82
+
83
+ const req = await fetch(
84
+ uri.join( '' ),
85
+ {
86
+ ...( body !== undefined && {
87
+ body : isFormData ? body : JSON.stringify( body )
88
+ }),
89
+ headers : requestHeaders,
90
+ method,
91
+ ...( ! ( 'cache' in options ) && ! ( 'next' in options ) && {
92
+ cache : 'no-store'
93
+ }),
94
+ ...options
95
+ }
96
+ );
97
+
98
+ const res = await req.json();
99
+
100
+ if( ! req.ok ){
101
+
102
+ const error = new Error( res?.message || req.statusText );
103
+
104
+ error.status = req.status;
105
+ error.data = res;
106
+
107
+ throw error;
108
+
109
+ }
110
+
111
+ return res;
122
112
 
123
113
  };
124
114
 
package/dist/fetch.d.ts CHANGED
@@ -1,4 +1,3 @@
1
- import axiosLib from 'axios';
2
1
  import qs from 'qs';
3
2
 
4
3
  const queryString = qs;
@@ -6,7 +5,7 @@ const queryString = qs;
6
5
  // In-memory token store for client-side authenticated calls. Guarded
7
6
  // against server writes — Next.js shares module state across requests on
8
7
  // the server, which would leak tokens between users. Server-side callers
9
- // must pass `token` explicitly via the rest() options.
8
+ // must pass `token` explicitly via the request() options.
10
9
  let memoryToken = null;
11
10
 
12
11
  const setMemoryToken = ( value ) => {
@@ -30,56 +29,6 @@ const readCsrf = () => {
30
29
 
31
30
  };
32
31
 
33
- const client = axiosLib.create({
34
- paramsSerializer : {
35
- serialize : ( params ) => qs.stringify(
36
- params,
37
- {
38
- encode : false
39
- }
40
- )
41
- }
42
- });
43
-
44
- client.interceptors.request.use( ( config ) => {
45
-
46
- const token = config.__token || ( typeof window !== 'undefined' ? memoryToken : null );
47
-
48
- if( token ){
49
-
50
- config.headers[ 'authorization' ] = 'Bearer ' + token;
51
-
52
- }
53
- const csrf = readCsrf();
54
-
55
- if( csrf ){
56
-
57
- config.headers[ 'x-drawbridge-csrf' ] = csrf;
58
-
59
- }
60
- delete config.__token;
61
-
62
- return config;
63
-
64
- } );
65
-
66
- client.interceptors.response.use(
67
- ( response ) => response.data,
68
- ( error ) => {
69
-
70
- const status = error?.response?.status;
71
- const data = error?.response?.data;
72
- const message = data?.message || error?.message || 'Request failed';
73
- const err = new Error( message );
74
-
75
- err.status = status;
76
- err.data = data;
77
-
78
- throw err;
79
-
80
- }
81
- );
82
-
83
32
  const request = async ({
84
33
  body,
85
34
  endpoint,
@@ -94,11 +43,35 @@ const request = async ({
94
43
  ...options
95
44
  }) => {
96
45
 
46
+ const uri = [ url, endpoint ];
47
+
48
+ if( params && Object.keys( params ).length > 0 ){
49
+
50
+ uri.push(
51
+ '?' + qs.stringify(
52
+ params,
53
+ {
54
+ encode : false
55
+ }
56
+ )
57
+ );
58
+
59
+ }
60
+
97
61
  const isFormData = ( typeof FormData !== 'undefined' && body instanceof FormData );
98
62
 
63
+ const authToken = token || ( typeof window !== 'undefined' ? memoryToken : null );
64
+ const csrf = readCsrf();
65
+
99
66
  const requestHeaders = {
100
67
  'origin' : process.env.NEXT_PUBLIC_APP_URI,
101
- ...headers
68
+ ...headers,
69
+ ...( authToken && {
70
+ 'authorization' : 'Bearer ' + authToken
71
+ }),
72
+ ...( csrf && {
73
+ 'x-drawbridge-csrf' : csrf
74
+ })
102
75
  };
103
76
 
104
77
  if( isFormData ){
@@ -106,19 +79,36 @@ const request = async ({
106
79
  delete requestHeaders[ 'content-type' ];
107
80
 
108
81
  }
109
- return client.request({
110
- ...options,
111
- url : [ url, endpoint ].join( '' ),
112
- method,
113
- headers : requestHeaders,
114
- ...( body !== undefined && {
115
- data : body
116
- }),
117
- ...( params && {
118
- params
119
- }),
120
- __token : token
121
- });
82
+
83
+ const req = await fetch(
84
+ uri.join( '' ),
85
+ {
86
+ ...( body !== undefined && {
87
+ body : isFormData ? body : JSON.stringify( body )
88
+ }),
89
+ headers : requestHeaders,
90
+ method,
91
+ ...( ! ( 'cache' in options ) && ! ( 'next' in options ) && {
92
+ cache : 'no-store'
93
+ }),
94
+ ...options
95
+ }
96
+ );
97
+
98
+ const res = await req.json();
99
+
100
+ if( ! req.ok ){
101
+
102
+ const error = new Error( res?.message || req.statusText );
103
+
104
+ error.status = req.status;
105
+ error.data = res;
106
+
107
+ throw error;
108
+
109
+ }
110
+
111
+ return res;
122
112
 
123
113
  };
124
114
 
package/dist/fetch.js CHANGED
@@ -1,5 +1,4 @@
1
1
  // fetch.js
2
- import axios from "axios";
3
2
  import qs from "qs";
4
3
  var queryString = qs;
5
4
  var memoryToken = null;
@@ -12,43 +11,6 @@ var readCsrf = () => {
12
11
  const match = document.cookie.match(/(?:^|;\s*)drawbridge\.csrf=([^;]+)/);
13
12
  return match ? decodeURIComponent(match[1]) : null;
14
13
  };
15
- var client = axios.create({
16
- paramsSerializer: {
17
- serialize: (params) => qs.stringify(
18
- params,
19
- {
20
- encode: false
21
- }
22
- )
23
- }
24
- });
25
- client.interceptors.request.use((config) => {
26
- const token = config.__token || (typeof window !== "undefined" ? memoryToken : null);
27
- if (token) {
28
- config.headers["authorization"] = "Bearer " + token;
29
- }
30
- ;
31
- const csrf = readCsrf();
32
- if (csrf) {
33
- config.headers["x-drawbridge-csrf"] = csrf;
34
- }
35
- ;
36
- delete config.__token;
37
- return config;
38
- });
39
- client.interceptors.response.use(
40
- (response) => response.data,
41
- (error) => {
42
- var _a, _b;
43
- const status = (_a = error == null ? void 0 : error.response) == null ? void 0 : _a.status;
44
- const data = (_b = error == null ? void 0 : error.response) == null ? void 0 : _b.data;
45
- const message = (data == null ? void 0 : data.message) || (error == null ? void 0 : error.message) || "Request failed";
46
- const err = new Error(message);
47
- err.status = status;
48
- err.data = data;
49
- throw err;
50
- }
51
- );
52
14
  var request = async ({
53
15
  body,
54
16
  endpoint,
@@ -62,28 +24,55 @@ var request = async ({
62
24
  url = process.env.NEXT_PUBLIC_API_URI,
63
25
  ...options
64
26
  }) => {
27
+ const uri = [url, endpoint];
28
+ if (params && Object.keys(params).length > 0) {
29
+ uri.push(
30
+ "?" + qs.stringify(
31
+ params,
32
+ {
33
+ encode: false
34
+ }
35
+ )
36
+ );
37
+ }
65
38
  const isFormData = typeof FormData !== "undefined" && body instanceof FormData;
39
+ const authToken = token || (typeof window !== "undefined" ? memoryToken : null);
40
+ const csrf = readCsrf();
66
41
  const requestHeaders = {
67
42
  "origin": process.env.NEXT_PUBLIC_APP_URI,
68
- ...headers
43
+ ...headers,
44
+ ...authToken && {
45
+ "authorization": "Bearer " + authToken
46
+ },
47
+ ...csrf && {
48
+ "x-drawbridge-csrf": csrf
49
+ }
69
50
  };
70
51
  if (isFormData) {
71
52
  delete requestHeaders["content-type"];
72
53
  }
73
- ;
74
- return client.request({
75
- ...options,
76
- url: [url, endpoint].join(""),
77
- method,
78
- headers: requestHeaders,
79
- ...body !== void 0 && {
80
- data: body
81
- },
82
- ...params && {
83
- params
84
- },
85
- __token: token
86
- });
54
+ const req = await fetch(
55
+ uri.join(""),
56
+ {
57
+ ...body !== void 0 && {
58
+ body: isFormData ? body : JSON.stringify(body)
59
+ },
60
+ headers: requestHeaders,
61
+ method,
62
+ ...!("cache" in options) && !("next" in options) && {
63
+ cache: "no-store"
64
+ },
65
+ ...options
66
+ }
67
+ );
68
+ const res = await req.json();
69
+ if (!req.ok) {
70
+ const error = new Error((res == null ? void 0 : res.message) || req.statusText);
71
+ error.status = req.status;
72
+ error.data = res;
73
+ throw error;
74
+ }
75
+ return res;
87
76
  };
88
77
  export {
89
78
  queryString,
package/package.json CHANGED
@@ -75,5 +75,5 @@
75
75
  "build": "tsup && npm publish"
76
76
  },
77
77
  "types": "dist/index.d.ts",
78
- "version": "0.0.14"
78
+ "version": "0.0.15"
79
79
  }