@drawbridge/drawbridge-utils 0.0.13 → 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 ADDED
@@ -0,0 +1,117 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
+ // If the importer is in node compatibility mode or this is not an ESM
21
+ // file that has been converted to a CommonJS file using a Babel-
22
+ // compatible transform (i.e. "__esModule" has not been set), then set
23
+ // "default" to the CommonJS "module.exports" for node compatibility.
24
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
25
+ mod
26
+ ));
27
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
28
+
29
+ // fetch.js
30
+ var fetch_exports = {};
31
+ __export(fetch_exports, {
32
+ queryString: () => queryString,
33
+ request: () => request,
34
+ setMemoryToken: () => setMemoryToken
35
+ });
36
+ module.exports = __toCommonJS(fetch_exports);
37
+ var import_qs = __toESM(require("qs"), 1);
38
+ var queryString = import_qs.default;
39
+ var memoryToken = null;
40
+ var setMemoryToken = (value) => {
41
+ if (typeof window === "undefined") return;
42
+ memoryToken = value || null;
43
+ };
44
+ var readCsrf = () => {
45
+ if (typeof document === "undefined") return null;
46
+ const match = document.cookie.match(/(?:^|;\s*)drawbridge\.csrf=([^;]+)/);
47
+ return match ? decodeURIComponent(match[1]) : null;
48
+ };
49
+ var request = async ({
50
+ body,
51
+ endpoint,
52
+ headers = {
53
+ "accept": "application/json",
54
+ "content-type": "application/json"
55
+ },
56
+ method = "GET",
57
+ params,
58
+ token,
59
+ url = process.env.NEXT_PUBLIC_API_URI,
60
+ ...options
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
+ }
73
+ const isFormData = typeof FormData !== "undefined" && body instanceof FormData;
74
+ const authToken = token || (typeof window !== "undefined" ? memoryToken : null);
75
+ const csrf = readCsrf();
76
+ const requestHeaders = {
77
+ "origin": process.env.NEXT_PUBLIC_APP_URI,
78
+ ...headers,
79
+ ...authToken && {
80
+ "authorization": "Bearer " + authToken
81
+ },
82
+ ...csrf && {
83
+ "x-drawbridge-csrf": csrf
84
+ }
85
+ };
86
+ if (isFormData) {
87
+ delete requestHeaders["content-type"];
88
+ }
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;
111
+ };
112
+ // Annotate the CommonJS export names for ESM import in node:
113
+ 0 && (module.exports = {
114
+ queryString,
115
+ request,
116
+ setMemoryToken
117
+ });
@@ -0,0 +1,115 @@
1
+ import qs from 'qs';
2
+
3
+ const queryString = qs;
4
+
5
+ // In-memory token store for client-side authenticated calls. Guarded
6
+ // against server writes — Next.js shares module state across requests on
7
+ // the server, which would leak tokens between users. Server-side callers
8
+ // must pass `token` explicitly via the request() options.
9
+ let memoryToken = null;
10
+
11
+ const setMemoryToken = ( value ) => {
12
+
13
+ if( typeof window === 'undefined' ) return;
14
+
15
+ memoryToken = value || null;
16
+
17
+ };
18
+
19
+ // Reads the double-submit CSRF cookie set server-side at session creation
20
+ // (drawbridge.csrf — non-HttpOnly so JS can read it). Returns null on the
21
+ // server or when the cookie isn't present.
22
+ const readCsrf = () => {
23
+
24
+ if( typeof document === 'undefined' ) return null;
25
+
26
+ const match = document.cookie.match( /(?:^|;\s*)drawbridge\.csrf=([^;]+)/ );
27
+
28
+ return match ? decodeURIComponent( match[ 1 ] ) : null;
29
+
30
+ };
31
+
32
+ const request = async ({
33
+ body,
34
+ endpoint,
35
+ headers = {
36
+ 'accept' : 'application/json',
37
+ 'content-type' : 'application/json'
38
+ },
39
+ method = 'GET',
40
+ params,
41
+ token,
42
+ url = process.env.NEXT_PUBLIC_API_URI,
43
+ ...options
44
+ }) => {
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
+
61
+ const isFormData = ( typeof FormData !== 'undefined' && body instanceof FormData );
62
+
63
+ const authToken = token || ( typeof window !== 'undefined' ? memoryToken : null );
64
+ const csrf = readCsrf();
65
+
66
+ const requestHeaders = {
67
+ 'origin' : process.env.NEXT_PUBLIC_APP_URI,
68
+ ...headers,
69
+ ...( authToken && {
70
+ 'authorization' : 'Bearer ' + authToken
71
+ }),
72
+ ...( csrf && {
73
+ 'x-drawbridge-csrf' : csrf
74
+ })
75
+ };
76
+
77
+ if( isFormData ){
78
+
79
+ delete requestHeaders[ 'content-type' ];
80
+
81
+ }
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;
112
+
113
+ };
114
+
115
+ export { queryString, request, setMemoryToken };
@@ -0,0 +1,115 @@
1
+ import qs from 'qs';
2
+
3
+ const queryString = qs;
4
+
5
+ // In-memory token store for client-side authenticated calls. Guarded
6
+ // against server writes — Next.js shares module state across requests on
7
+ // the server, which would leak tokens between users. Server-side callers
8
+ // must pass `token` explicitly via the request() options.
9
+ let memoryToken = null;
10
+
11
+ const setMemoryToken = ( value ) => {
12
+
13
+ if( typeof window === 'undefined' ) return;
14
+
15
+ memoryToken = value || null;
16
+
17
+ };
18
+
19
+ // Reads the double-submit CSRF cookie set server-side at session creation
20
+ // (drawbridge.csrf — non-HttpOnly so JS can read it). Returns null on the
21
+ // server or when the cookie isn't present.
22
+ const readCsrf = () => {
23
+
24
+ if( typeof document === 'undefined' ) return null;
25
+
26
+ const match = document.cookie.match( /(?:^|;\s*)drawbridge\.csrf=([^;]+)/ );
27
+
28
+ return match ? decodeURIComponent( match[ 1 ] ) : null;
29
+
30
+ };
31
+
32
+ const request = async ({
33
+ body,
34
+ endpoint,
35
+ headers = {
36
+ 'accept' : 'application/json',
37
+ 'content-type' : 'application/json'
38
+ },
39
+ method = 'GET',
40
+ params,
41
+ token,
42
+ url = process.env.NEXT_PUBLIC_API_URI,
43
+ ...options
44
+ }) => {
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
+
61
+ const isFormData = ( typeof FormData !== 'undefined' && body instanceof FormData );
62
+
63
+ const authToken = token || ( typeof window !== 'undefined' ? memoryToken : null );
64
+ const csrf = readCsrf();
65
+
66
+ const requestHeaders = {
67
+ 'origin' : process.env.NEXT_PUBLIC_APP_URI,
68
+ ...headers,
69
+ ...( authToken && {
70
+ 'authorization' : 'Bearer ' + authToken
71
+ }),
72
+ ...( csrf && {
73
+ 'x-drawbridge-csrf' : csrf
74
+ })
75
+ };
76
+
77
+ if( isFormData ){
78
+
79
+ delete requestHeaders[ 'content-type' ];
80
+
81
+ }
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;
112
+
113
+ };
114
+
115
+ export { queryString, request, setMemoryToken };
package/dist/fetch.js ADDED
@@ -0,0 +1,81 @@
1
+ // fetch.js
2
+ import qs from "qs";
3
+ var queryString = qs;
4
+ var memoryToken = null;
5
+ var setMemoryToken = (value) => {
6
+ if (typeof window === "undefined") return;
7
+ memoryToken = value || null;
8
+ };
9
+ var readCsrf = () => {
10
+ if (typeof document === "undefined") return null;
11
+ const match = document.cookie.match(/(?:^|;\s*)drawbridge\.csrf=([^;]+)/);
12
+ return match ? decodeURIComponent(match[1]) : null;
13
+ };
14
+ var request = async ({
15
+ body,
16
+ endpoint,
17
+ headers = {
18
+ "accept": "application/json",
19
+ "content-type": "application/json"
20
+ },
21
+ method = "GET",
22
+ params,
23
+ token,
24
+ url = process.env.NEXT_PUBLIC_API_URI,
25
+ ...options
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
+ }
38
+ const isFormData = typeof FormData !== "undefined" && body instanceof FormData;
39
+ const authToken = token || (typeof window !== "undefined" ? memoryToken : null);
40
+ const csrf = readCsrf();
41
+ const requestHeaders = {
42
+ "origin": process.env.NEXT_PUBLIC_APP_URI,
43
+ ...headers,
44
+ ...authToken && {
45
+ "authorization": "Bearer " + authToken
46
+ },
47
+ ...csrf && {
48
+ "x-drawbridge-csrf": csrf
49
+ }
50
+ };
51
+ if (isFormData) {
52
+ delete requestHeaders["content-type"];
53
+ }
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;
76
+ };
77
+ export {
78
+ queryString,
79
+ request,
80
+ setMemoryToken
81
+ };
@@ -0,0 +1,52 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
+ // If the importer is in node compatibility mode or this is not an ESM
21
+ // file that has been converted to a CommonJS file using a Babel-
22
+ // compatible transform (i.e. "__esModule" has not been set), then set
23
+ // "default" to the CommonJS "module.exports" for node compatibility.
24
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
25
+ mod
26
+ ));
27
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
28
+
29
+ // slugify.js
30
+ var slugify_exports = {};
31
+ __export(slugify_exports, {
32
+ slugify: () => slugify
33
+ });
34
+ module.exports = __toCommonJS(slugify_exports);
35
+ var import_slugify = __toESM(require("slugify"), 1);
36
+ var slugify = (value, nochars = false) => {
37
+ const remove = nochars ? /[-?*+~.()'"!:@#^_{}\[\];,/\\]/g : /[?*+~.()'"!:@#^_{}\[\];,/\\]/g;
38
+ const replacement = nochars ? "" : "-";
39
+ return (0, import_slugify.default)(
40
+ value,
41
+ {
42
+ remove,
43
+ replacement,
44
+ lower: true,
45
+ trim: true
46
+ }
47
+ );
48
+ };
49
+ // Annotate the CommonJS export names for ESM import in node:
50
+ 0 && (module.exports = {
51
+ slugify
52
+ });
@@ -0,0 +1,20 @@
1
+ import slug from 'slugify';
2
+
3
+ const slugify = ( value, nochars = false ) => {
4
+
5
+ const remove = nochars ? /[-?*+~.()'"!:@#^_{}\[\];,/\\]/g : /[?*+~.()'"!:@#^_{}\[\];,/\\]/g;
6
+ const replacement = nochars ? '' : '-';
7
+
8
+ return slug(
9
+ value,
10
+ {
11
+ remove,
12
+ replacement,
13
+ lower : true,
14
+ trim : true
15
+ }
16
+ );
17
+
18
+ };
19
+
20
+ export { slugify };
@@ -0,0 +1,20 @@
1
+ import slug from 'slugify';
2
+
3
+ const slugify = ( value, nochars = false ) => {
4
+
5
+ const remove = nochars ? /[-?*+~.()'"!:@#^_{}\[\];,/\\]/g : /[?*+~.()'"!:@#^_{}\[\];,/\\]/g;
6
+ const replacement = nochars ? '' : '-';
7
+
8
+ return slug(
9
+ value,
10
+ {
11
+ remove,
12
+ replacement,
13
+ lower : true,
14
+ trim : true
15
+ }
16
+ );
17
+
18
+ };
19
+
20
+ export { slugify };
@@ -0,0 +1,18 @@
1
+ // slugify.js
2
+ import slug from "slugify";
3
+ var slugify = (value, nochars = false) => {
4
+ const remove = nochars ? /[-?*+~.()'"!:@#^_{}\[\];,/\\]/g : /[?*+~.()'"!:@#^_{}\[\];,/\\]/g;
5
+ const replacement = nochars ? "" : "-";
6
+ return slug(
7
+ value,
8
+ {
9
+ remove,
10
+ replacement,
11
+ lower: true,
12
+ trim: true
13
+ }
14
+ );
15
+ };
16
+ export {
17
+ slugify
18
+ };
package/package.json CHANGED
@@ -4,6 +4,8 @@
4
4
  "axios": "1.16.0",
5
5
  "currency-codes": "2.2.0",
6
6
  "nanoid": "3.3.8",
7
+ "qs": "6.15.1",
8
+ "slugify": "1.6.6",
7
9
  "tsup": "8.5.1",
8
10
  "typescript": "5.9.3"
9
11
  },
@@ -47,6 +49,16 @@
47
49
  "types": "./dist/cdn.d.ts",
48
50
  "import": "./dist/cdn.js",
49
51
  "require": "./dist/cdn.cjs"
52
+ },
53
+ "./fetch": {
54
+ "types": "./dist/fetch.d.ts",
55
+ "import": "./dist/fetch.js",
56
+ "require": "./dist/fetch.cjs"
57
+ },
58
+ "./slugify": {
59
+ "types": "./dist/slugify.d.ts",
60
+ "import": "./dist/slugify.js",
61
+ "require": "./dist/slugify.cjs"
50
62
  }
51
63
  },
52
64
  "files": [
@@ -63,5 +75,5 @@
63
75
  "build": "tsup && npm publish"
64
76
  },
65
77
  "types": "dist/index.d.ts",
66
- "version": "0.0.13"
78
+ "version": "0.0.15"
67
79
  }