@kodaris/krubble-http 1.0.8 → 1.0.10
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/http.d.ts +4 -9
- package/dist/http.d.ts.map +1 -1
- package/dist/http.js +29 -9
- package/dist/http.js.map +1 -1
- package/dist/krubble-http.bundled.js +29 -9
- package/dist/krubble-http.bundled.js.map +1 -1
- package/dist/krubble-http.bundled.min.js +1 -1
- package/dist/krubble-http.bundled.min.js.map +1 -1
- package/dist/krubble-http.umd.js +29 -9
- package/dist/krubble-http.umd.js.map +1 -1
- package/dist/krubble-http.umd.min.js +1 -1
- package/dist/krubble-http.umd.min.js.map +1 -1
- package/package.json +1 -1
package/dist/http.d.ts
CHANGED
|
@@ -57,20 +57,15 @@
|
|
|
57
57
|
*/
|
|
58
58
|
/**
|
|
59
59
|
* Options for making an HTTP request.
|
|
60
|
+
* Extends the native browser RequestInit so any standard fetch option is accepted.
|
|
60
61
|
*/
|
|
61
|
-
export interface KRHttpRequest {
|
|
62
|
+
export interface KRHttpRequest extends Omit<RequestInit, 'body'> {
|
|
62
63
|
/** The URL path (e.g., '/api/customers') or full URL */
|
|
63
64
|
url: string;
|
|
64
|
-
/** HTTP method (GET, POST, PUT, DELETE, etc.). Defaults to GET */
|
|
65
|
-
method?: string;
|
|
66
|
-
/** Request headers */
|
|
67
|
-
headers?: Record<string, string>;
|
|
68
|
-
/** Request body (string or object - objects are auto-stringified) */
|
|
69
|
-
body?: string | object;
|
|
70
65
|
/** URL query parameters */
|
|
71
66
|
params?: Record<string, string>;
|
|
72
|
-
/**
|
|
73
|
-
|
|
67
|
+
/** Request body — objects are auto-stringified to JSON */
|
|
68
|
+
body?: BodyInit | object | null;
|
|
74
69
|
}
|
|
75
70
|
/**
|
|
76
71
|
* Configuration options for the HTTP client.
|
package/dist/http.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"http.d.ts","sourceRoot":"","sources":["../src/http.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwDG;AAEH
|
|
1
|
+
{"version":3,"file":"http.d.ts","sourceRoot":"","sources":["../src/http.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwDG;AAEH;;;GAGG;AACH,MAAM,WAAW,aAAc,SAAQ,IAAI,CAAC,WAAW,EAAE,MAAM,CAAC;IAC5D,wDAAwD;IACxD,GAAG,EAAE,MAAM,CAAC;IACZ,2BAA2B;IAC3B,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAChC,0DAA0D;IAC1D,IAAI,CAAC,EAAE,QAAQ,GAAG,MAAM,GAAG,IAAI,CAAC;CACnC;AAED;;GAEG;AACH,MAAM,WAAW,YAAY;IACzB,uCAAuC;IACvC,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,wDAAwD;IACxD,MAAM,CAAC,EAAE,MAAM,CAAC;CACnB;AAED;;;GAGG;AACH,qBAAa,MAAM;IAEf,uCAAuC;IACvC,MAAM,CAAC,QAAQ,EAAE,MAAM,CAAM;IAC7B,2CAA2C;IAC3C,MAAM,CAAC,OAAO,EAAE,MAAM,CAAM;IAE5B;;;;;;;;;;OAUG;IACH,MAAM,CAAC,SAAS,CAAC,MAAM,EAAE,YAAY,GAAG,IAAI;IAS5C;;;;;;;;;;;;;;OAcG;IACH,MAAM,CAAC,KAAK,CAAC,OAAO,EAAE,aAAa,GAAG,OAAO,CAAC,QAAQ,CAAC;CA+E1D"}
|
package/dist/http.js
CHANGED
|
@@ -96,8 +96,9 @@ export class KRHttp {
|
|
|
96
96
|
*/
|
|
97
97
|
static fetch(options) {
|
|
98
98
|
let url = options.url;
|
|
99
|
+
var isDev = window.location.hostname === 'localhost' && window.location.port.startsWith('4');
|
|
99
100
|
let finalBaseUrl = KRHttp._baseUrl;
|
|
100
|
-
if (
|
|
101
|
+
if (isDev && KRHttp._devUrl) {
|
|
101
102
|
finalBaseUrl = KRHttp._devUrl;
|
|
102
103
|
}
|
|
103
104
|
// Add on the default base url if user
|
|
@@ -114,15 +115,34 @@ export class KRHttp {
|
|
|
114
115
|
url += `${key}=${options.params[key]}`;
|
|
115
116
|
});
|
|
116
117
|
}
|
|
117
|
-
|
|
118
|
-
|
|
118
|
+
// Normalize headers to a plain object so we can set defaults
|
|
119
|
+
var headers = {};
|
|
120
|
+
if (options.headers) {
|
|
121
|
+
if (options.headers instanceof Headers) {
|
|
122
|
+
options.headers.forEach((v, k) => { headers[k] = v; });
|
|
123
|
+
}
|
|
124
|
+
else if (Array.isArray(options.headers)) {
|
|
125
|
+
options.headers.forEach(([k, v]) => { headers[k] = v; });
|
|
126
|
+
}
|
|
127
|
+
else {
|
|
128
|
+
Object.assign(headers, options.headers);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
if (!headers['Content-Type']) {
|
|
132
|
+
headers['Content-Type'] = 'application/json';
|
|
119
133
|
}
|
|
120
|
-
|
|
121
|
-
|
|
134
|
+
options.headers = headers;
|
|
135
|
+
// Default credentials to include in dev mode
|
|
136
|
+
if (isDev && !options.credentials) {
|
|
137
|
+
options.credentials = 'include';
|
|
122
138
|
}
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
139
|
+
// Auto-stringify plain object bodies (skip native body types)
|
|
140
|
+
if (options.body && typeof options.body === 'object'
|
|
141
|
+
&& !(options.body instanceof ReadableStream)
|
|
142
|
+
&& !(options.body instanceof Blob)
|
|
143
|
+
&& !(options.body instanceof ArrayBuffer)
|
|
144
|
+
&& !(options.body instanceof FormData)
|
|
145
|
+
&& !(options.body instanceof URLSearchParams)) {
|
|
126
146
|
options.body = JSON.stringify(options.body);
|
|
127
147
|
}
|
|
128
148
|
// User and Customer apis require CSRF tokens on non-GET requests
|
|
@@ -137,7 +157,7 @@ export class KRHttp {
|
|
|
137
157
|
.then(res => res?.json())
|
|
138
158
|
.then(res => {
|
|
139
159
|
if (res?.data) {
|
|
140
|
-
|
|
160
|
+
headers['X-CSRF-TOKEN'] = res.data;
|
|
141
161
|
}
|
|
142
162
|
return fetch(url, options);
|
|
143
163
|
});
|
package/dist/http.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"http.js","sourceRoot":"","sources":["../src/http.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwDG;
|
|
1
|
+
{"version":3,"file":"http.js","sourceRoot":"","sources":["../src/http.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwDG;AAyBH;;;GAGG;AACH,MAAM,OAAO,MAAM;IAOf;;;;;;;;;;OAUG;IACH,MAAM,CAAC,SAAS,CAAC,MAAoB;QACjC,IAAI,MAAM,CAAC,OAAO,KAAK,SAAS,EAAE,CAAC;YAC/B,MAAM,CAAC,QAAQ,GAAG,MAAM,CAAC,OAAO,CAAC;QACrC,CAAC;QACD,IAAI,MAAM,CAAC,MAAM,KAAK,SAAS,EAAE,CAAC;YAC9B,MAAM,CAAC,OAAO,GAAG,MAAM,CAAC,MAAM,CAAC;QACnC,CAAC;IACL,CAAC;IAED;;;;;;;;;;;;;;OAcG;IACH,MAAM,CAAC,KAAK,CAAC,OAAsB;QAE/B,IAAI,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC;QAEtB,IAAI,KAAK,GAAG,MAAM,CAAC,QAAQ,CAAC,QAAQ,KAAK,WAAW,IAAI,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC;QAE7F,IAAI,YAAY,GAAG,MAAM,CAAC,QAAQ,CAAC;QACnC,IAAI,KAAK,IAAI,MAAM,CAAC,OAAO,EAAE,CAAC;YAC1B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AAE,IAAI,EAAE,CAAC;aACxB,IAAI,CAAC,GAAG,CAAC,EAAE;YACR,IAAI,GAAG,EAAE,IAAI,EAAE,CAAC;gBACZ,OAAO,CAAC,cAAc,CAAC,GAAG,GAAG,CAAC,IAAI,CAAC;YACvC,CAAC;YAED,OAAO,KAAK,CAAC,GAAG,EAAE,OAAsB,CAAC,CAAC;QAC9C,CAAC,CAAC,CAAC;IACX,CAAC;;AAtHD,uCAAuC;AAChC,eAAQ,GAAW,EAAE,CAAC;AAC7B,2CAA2C;AACpC,cAAO,GAAW,EAAE,CAAC"}
|
|
@@ -96,8 +96,9 @@ class KRHttp {
|
|
|
96
96
|
*/
|
|
97
97
|
static fetch(options) {
|
|
98
98
|
let url = options.url;
|
|
99
|
+
var isDev = window.location.hostname === 'localhost' && window.location.port.startsWith('4');
|
|
99
100
|
let finalBaseUrl = KRHttp._baseUrl;
|
|
100
|
-
if (
|
|
101
|
+
if (isDev && KRHttp._devUrl) {
|
|
101
102
|
finalBaseUrl = KRHttp._devUrl;
|
|
102
103
|
}
|
|
103
104
|
// Add on the default base url if user
|
|
@@ -114,15 +115,34 @@ class KRHttp {
|
|
|
114
115
|
url += `${key}=${options.params[key]}`;
|
|
115
116
|
});
|
|
116
117
|
}
|
|
117
|
-
|
|
118
|
-
|
|
118
|
+
// Normalize headers to a plain object so we can set defaults
|
|
119
|
+
var headers = {};
|
|
120
|
+
if (options.headers) {
|
|
121
|
+
if (options.headers instanceof Headers) {
|
|
122
|
+
options.headers.forEach((v, k) => { headers[k] = v; });
|
|
123
|
+
}
|
|
124
|
+
else if (Array.isArray(options.headers)) {
|
|
125
|
+
options.headers.forEach(([k, v]) => { headers[k] = v; });
|
|
126
|
+
}
|
|
127
|
+
else {
|
|
128
|
+
Object.assign(headers, options.headers);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
if (!headers['Content-Type']) {
|
|
132
|
+
headers['Content-Type'] = 'application/json';
|
|
119
133
|
}
|
|
120
|
-
|
|
121
|
-
|
|
134
|
+
options.headers = headers;
|
|
135
|
+
// Default credentials to include in dev mode
|
|
136
|
+
if (isDev && !options.credentials) {
|
|
137
|
+
options.credentials = 'include';
|
|
122
138
|
}
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
139
|
+
// Auto-stringify plain object bodies (skip native body types)
|
|
140
|
+
if (options.body && typeof options.body === 'object'
|
|
141
|
+
&& !(options.body instanceof ReadableStream)
|
|
142
|
+
&& !(options.body instanceof Blob)
|
|
143
|
+
&& !(options.body instanceof ArrayBuffer)
|
|
144
|
+
&& !(options.body instanceof FormData)
|
|
145
|
+
&& !(options.body instanceof URLSearchParams)) {
|
|
126
146
|
options.body = JSON.stringify(options.body);
|
|
127
147
|
}
|
|
128
148
|
// User and Customer apis require CSRF tokens on non-GET requests
|
|
@@ -137,7 +157,7 @@ class KRHttp {
|
|
|
137
157
|
.then(res => res?.json())
|
|
138
158
|
.then(res => {
|
|
139
159
|
if (res?.data) {
|
|
140
|
-
|
|
160
|
+
headers['X-CSRF-TOKEN'] = res.data;
|
|
141
161
|
}
|
|
142
162
|
return fetch(url, options);
|
|
143
163
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"krubble-http.bundled.js","sources":["http.js"],"sourcesContent":["/**\n * @kodaris/krubble-http\n *\n * HTTP client for making API requests with automatic base URL handling\n * and Kodaris API conveniences (CSRF tokens, credentials).\n *\n * ## Usage\n *\n * ### Web App (UMD)\n * ```html\n * <script src=\"krubble-http.umd.js\"></script>\n * <script>\n * // Configure once at startup\n * kr.http.configure({\n * baseUrl: 'https://kodaris.com',\n * devUrl: 'https://localhost:4201'\n * });\n *\n * // Make requests\n * kr.http.fetch({\n * url: '/api/system/customer/list',\n * method: 'POST',\n * body: JSON.stringify({ page: 0, size: 20 })\n * })\n * .then(response => response.json())\n * .then(data => console.log(data));\n * </script>\n * ```\n *\n * ### Angular / ESM\n * ```typescript\n * // app.config.ts - configure once at startup\n * import { KRHttp } from '@kodaris/krubble-http';\n *\n * KRHttp.configure({\n * baseUrl: 'https://kodaris.com',\n * devUrl: 'https://localhost:4201'\n * });\n *\n * // In a service or component\n * import { KRHttp } from '@kodaris/krubble-http';\n *\n * getCustomers() {\n * return KRHttp.fetch({\n * url: '/api/system/customer/list',\n * method: 'POST',\n * body: JSON.stringify({ page: 0, size: 20 })\n * })\n * .then(response => response.json());\n * }\n * ```\n *\n * ## URL Resolution\n * - On localhost: uses `devUrl` if configured, otherwise `baseUrl`\n * - On production: always uses `baseUrl`\n * - If URL starts with 'http', it's used as-is (no base URL prepended)\n */\n/**\n * HTTP client with static methods for making API requests.\n * Automatically handles base URL resolution and Kodaris API conveniences.\n */\nexport class KRHttp {\n /**\n * Configure the HTTP client. Call once at application startup.\n *\n * @param config - Configuration options\n *\n * @example\n * KRHttp.configure({\n * baseUrl: 'https://kodaris.com',\n * devUrl: 'https://localhost:4201'\n * });\n */\n static configure(config) {\n if (config.baseUrl !== undefined) {\n KRHttp._baseUrl = config.baseUrl;\n }\n if (config.devUrl !== undefined) {\n KRHttp._devUrl = config.devUrl;\n }\n }\n /**\n * Make an HTTP request.\n *\n * @param options - Request options\n * @returns Promise resolving to the Response object\n *\n * @example\n * KRHttp.fetch({\n * url: '/api/system/customer/list',\n * method: 'POST',\n * body: JSON.stringify({ page: 0, size: 20 })\n * })\n * .then(response => response.json())\n * .then(data => console.log(data));\n */\n static fetch(options) {\n let url = options.url;\n
|
|
1
|
+
{"version":3,"file":"krubble-http.bundled.js","sources":["http.js"],"sourcesContent":["/**\n * @kodaris/krubble-http\n *\n * HTTP client for making API requests with automatic base URL handling\n * and Kodaris API conveniences (CSRF tokens, credentials).\n *\n * ## Usage\n *\n * ### Web App (UMD)\n * ```html\n * <script src=\"krubble-http.umd.js\"></script>\n * <script>\n * // Configure once at startup\n * kr.http.configure({\n * baseUrl: 'https://kodaris.com',\n * devUrl: 'https://localhost:4201'\n * });\n *\n * // Make requests\n * kr.http.fetch({\n * url: '/api/system/customer/list',\n * method: 'POST',\n * body: JSON.stringify({ page: 0, size: 20 })\n * })\n * .then(response => response.json())\n * .then(data => console.log(data));\n * </script>\n * ```\n *\n * ### Angular / ESM\n * ```typescript\n * // app.config.ts - configure once at startup\n * import { KRHttp } from '@kodaris/krubble-http';\n *\n * KRHttp.configure({\n * baseUrl: 'https://kodaris.com',\n * devUrl: 'https://localhost:4201'\n * });\n *\n * // In a service or component\n * import { KRHttp } from '@kodaris/krubble-http';\n *\n * getCustomers() {\n * return KRHttp.fetch({\n * url: '/api/system/customer/list',\n * method: 'POST',\n * body: JSON.stringify({ page: 0, size: 20 })\n * })\n * .then(response => response.json());\n * }\n * ```\n *\n * ## URL Resolution\n * - On localhost: uses `devUrl` if configured, otherwise `baseUrl`\n * - On production: always uses `baseUrl`\n * - If URL starts with 'http', it's used as-is (no base URL prepended)\n */\n/**\n * HTTP client with static methods for making API requests.\n * Automatically handles base URL resolution and Kodaris API conveniences.\n */\nexport class KRHttp {\n /**\n * Configure the HTTP client. Call once at application startup.\n *\n * @param config - Configuration options\n *\n * @example\n * KRHttp.configure({\n * baseUrl: 'https://kodaris.com',\n * devUrl: 'https://localhost:4201'\n * });\n */\n static configure(config) {\n if (config.baseUrl !== undefined) {\n KRHttp._baseUrl = config.baseUrl;\n }\n if (config.devUrl !== undefined) {\n KRHttp._devUrl = config.devUrl;\n }\n }\n /**\n * Make an HTTP request.\n *\n * @param options - Request options\n * @returns Promise resolving to the Response object\n *\n * @example\n * KRHttp.fetch({\n * url: '/api/system/customer/list',\n * method: 'POST',\n * body: JSON.stringify({ page: 0, size: 20 })\n * })\n * .then(response => response.json())\n * .then(data => console.log(data));\n */\n static fetch(options) {\n let url = options.url;\n var isDev = window.location.hostname === 'localhost' && window.location.port.startsWith('4');\n let finalBaseUrl = KRHttp._baseUrl;\n if (isDev && KRHttp._devUrl) {\n finalBaseUrl = KRHttp._devUrl;\n }\n // Add on the default base url if user\n // has not provided the full url\n if (url.indexOf('http') !== 0) {\n url = finalBaseUrl + url;\n }\n if (options.params) {\n url += '?';\n Object.keys(options.params).forEach((key, keyIdx) => {\n if (keyIdx > 0) {\n url += '&';\n }\n url += `${key}=${options.params[key]}`;\n });\n }\n // Normalize headers to a plain object so we can set defaults\n var headers = {};\n if (options.headers) {\n if (options.headers instanceof Headers) {\n options.headers.forEach((v, k) => { headers[k] = v; });\n }\n else if (Array.isArray(options.headers)) {\n options.headers.forEach(([k, v]) => { headers[k] = v; });\n }\n else {\n Object.assign(headers, options.headers);\n }\n }\n if (!headers['Content-Type']) {\n headers['Content-Type'] = 'application/json';\n }\n options.headers = headers;\n // Default credentials to include in dev mode\n if (isDev && !options.credentials) {\n options.credentials = 'include';\n }\n // Auto-stringify plain object bodies (skip native body types)\n if (options.body && typeof options.body === 'object'\n && !(options.body instanceof ReadableStream)\n && !(options.body instanceof Blob)\n && !(options.body instanceof ArrayBuffer)\n && !(options.body instanceof FormData)\n && !(options.body instanceof URLSearchParams)) {\n options.body = JSON.stringify(options.body);\n }\n // User and Customer apis require CSRF tokens on non-GET requests\n let token;\n if (url.indexOf('/api/user/') > -1 || url.indexOf('/api/customer/') > -1) {\n token = fetch(`${finalBaseUrl}/api/user/customer/authToken`, { credentials: 'include' });\n }\n else {\n token = Promise.resolve(null);\n }\n return token\n .then(res => res?.json())\n .then(res => {\n if (res?.data) {\n headers['X-CSRF-TOKEN'] = res.data;\n }\n return fetch(url, options);\n });\n }\n}\n/** Base URL for production requests */\nKRHttp._baseUrl = '';\n/** Base URL for development (localhost) */\nKRHttp._devUrl = '';\n//# sourceMappingURL=http.js.map"],"names":[],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;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rl),void 0!==a.devUrl&&(e._devUrl=a.devUrl)}static fetch(a){let
|
|
1
|
+
class e{static configure(a){void 0!==a.baseUrl&&(e._baseUrl=a.baseUrl),void 0!==a.devUrl&&(e._devUrl=a.devUrl)}static fetch(a){let r=a.url;var t="localhost"===window.location.hostname&&window.location.port.startsWith("4");let s=e._baseUrl;t&&e._devUrl&&(s=e._devUrl),0!==r.indexOf("http")&&(r=s+r),a.params&&(r+="?",Object.keys(a.params).forEach((e,t)=>{t>0&&(r+="&"),r+=`${e}=${a.params[e]}`}));var o={};let n;return a.headers&&(a.headers instanceof Headers?a.headers.forEach((e,a)=>{o[a]=e}):Array.isArray(a.headers)?a.headers.forEach(([e,a])=>{o[e]=a}):Object.assign(o,a.headers)),o["Content-Type"]||(o["Content-Type"]="application/json"),a.headers=o,t&&!a.credentials&&(a.credentials="include"),!a.body||"object"!=typeof a.body||a.body instanceof ReadableStream||a.body instanceof Blob||a.body instanceof ArrayBuffer||a.body instanceof FormData||a.body instanceof URLSearchParams||(a.body=JSON.stringify(a.body)),n=r.indexOf("/api/user/")>-1||r.indexOf("/api/customer/")>-1?fetch(`${s}/api/user/customer/authToken`,{credentials:"include"}):Promise.resolve(null),n.then(e=>e?.json()).then(e=>(e?.data&&(o["X-CSRF-TOKEN"]=e.data),fetch(r,a)))}}e._baseUrl="",e._devUrl="";export{e as KRHttp,e as http};
|
|
2
2
|
//# sourceMappingURL=krubble-http.bundled.min.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"krubble-http.bundled.min.js","sources":["http.js"],"sourcesContent":["/**\n * @kodaris/krubble-http\n *\n * HTTP client for making API requests with automatic base URL handling\n * and Kodaris API conveniences (CSRF tokens, credentials).\n *\n * ## Usage\n *\n * ### Web App (UMD)\n * ```html\n * <script src=\"krubble-http.umd.js\"></script>\n * <script>\n * // Configure once at startup\n * kr.http.configure({\n * baseUrl: 'https://kodaris.com',\n * devUrl: 'https://localhost:4201'\n * });\n *\n * // Make requests\n * kr.http.fetch({\n * url: '/api/system/customer/list',\n * method: 'POST',\n * body: JSON.stringify({ page: 0, size: 20 })\n * })\n * .then(response => response.json())\n * .then(data => console.log(data));\n * </script>\n * ```\n *\n * ### Angular / ESM\n * ```typescript\n * // app.config.ts - configure once at startup\n * import { KRHttp } from '@kodaris/krubble-http';\n *\n * KRHttp.configure({\n * baseUrl: 'https://kodaris.com',\n * devUrl: 'https://localhost:4201'\n * });\n *\n * // In a service or component\n * import { KRHttp } from '@kodaris/krubble-http';\n *\n * getCustomers() {\n * return KRHttp.fetch({\n * url: '/api/system/customer/list',\n * method: 'POST',\n * body: JSON.stringify({ page: 0, size: 20 })\n * })\n * .then(response => response.json());\n * }\n * ```\n *\n * ## URL Resolution\n * - On localhost: uses `devUrl` if configured, otherwise `baseUrl`\n * - On production: always uses `baseUrl`\n * - If URL starts with 'http', it's used as-is (no base URL prepended)\n */\n/**\n * HTTP client with static methods for making API requests.\n * Automatically handles base URL resolution and Kodaris API conveniences.\n */\nexport class KRHttp {\n /**\n * Configure the HTTP client. Call once at application startup.\n *\n * @param config - Configuration options\n *\n * @example\n * KRHttp.configure({\n * baseUrl: 'https://kodaris.com',\n * devUrl: 'https://localhost:4201'\n * });\n */\n static configure(config) {\n if (config.baseUrl !== undefined) {\n KRHttp._baseUrl = config.baseUrl;\n }\n if (config.devUrl !== undefined) {\n KRHttp._devUrl = config.devUrl;\n }\n }\n /**\n * Make an HTTP request.\n *\n * @param options - Request options\n * @returns Promise resolving to the Response object\n *\n * @example\n * KRHttp.fetch({\n * url: '/api/system/customer/list',\n * method: 'POST',\n * body: JSON.stringify({ page: 0, size: 20 })\n * })\n * .then(response => response.json())\n * .then(data => console.log(data));\n */\n static fetch(options) {\n let url = options.url;\n
|
|
1
|
+
{"version":3,"file":"krubble-http.bundled.min.js","sources":["http.js"],"sourcesContent":["/**\n * @kodaris/krubble-http\n *\n * HTTP client for making API requests with automatic base URL handling\n * and Kodaris API conveniences (CSRF tokens, credentials).\n *\n * ## Usage\n *\n * ### Web App (UMD)\n * ```html\n * <script src=\"krubble-http.umd.js\"></script>\n * <script>\n * // Configure once at startup\n * kr.http.configure({\n * baseUrl: 'https://kodaris.com',\n * devUrl: 'https://localhost:4201'\n * });\n *\n * // Make requests\n * kr.http.fetch({\n * url: '/api/system/customer/list',\n * method: 'POST',\n * body: JSON.stringify({ page: 0, size: 20 })\n * })\n * .then(response => response.json())\n * .then(data => console.log(data));\n * </script>\n * ```\n *\n * ### Angular / ESM\n * ```typescript\n * // app.config.ts - configure once at startup\n * import { KRHttp } from '@kodaris/krubble-http';\n *\n * KRHttp.configure({\n * baseUrl: 'https://kodaris.com',\n * devUrl: 'https://localhost:4201'\n * });\n *\n * // In a service or component\n * import { KRHttp } from '@kodaris/krubble-http';\n *\n * getCustomers() {\n * return KRHttp.fetch({\n * url: '/api/system/customer/list',\n * method: 'POST',\n * body: JSON.stringify({ page: 0, size: 20 })\n * })\n * .then(response => response.json());\n * }\n * ```\n *\n * ## URL Resolution\n * - On localhost: uses `devUrl` if configured, otherwise `baseUrl`\n * - On production: always uses `baseUrl`\n * - If URL starts with 'http', it's used as-is (no base URL prepended)\n */\n/**\n * HTTP client with static methods for making API requests.\n * Automatically handles base URL resolution and Kodaris API conveniences.\n */\nexport class KRHttp {\n /**\n * Configure the HTTP client. Call once at application startup.\n *\n * @param config - Configuration options\n *\n * @example\n * KRHttp.configure({\n * baseUrl: 'https://kodaris.com',\n * devUrl: 'https://localhost:4201'\n * });\n */\n static configure(config) {\n if (config.baseUrl !== undefined) {\n KRHttp._baseUrl = config.baseUrl;\n }\n if (config.devUrl !== undefined) {\n KRHttp._devUrl = config.devUrl;\n }\n }\n /**\n * Make an HTTP request.\n *\n * @param options - Request options\n * @returns Promise resolving to the Response object\n *\n * @example\n * KRHttp.fetch({\n * url: '/api/system/customer/list',\n * method: 'POST',\n * body: JSON.stringify({ page: 0, size: 20 })\n * })\n * .then(response => response.json())\n * .then(data => console.log(data));\n */\n static fetch(options) {\n let url = options.url;\n var isDev = window.location.hostname === 'localhost' && window.location.port.startsWith('4');\n let finalBaseUrl = KRHttp._baseUrl;\n if (isDev && KRHttp._devUrl) {\n finalBaseUrl = KRHttp._devUrl;\n }\n // Add on the default base url if user\n // has not provided the full url\n if (url.indexOf('http') !== 0) {\n url = finalBaseUrl + url;\n }\n if (options.params) {\n url += '?';\n Object.keys(options.params).forEach((key, keyIdx) => {\n if (keyIdx > 0) {\n url += '&';\n }\n url += `${key}=${options.params[key]}`;\n });\n }\n // Normalize headers to a plain object so we can set defaults\n var headers = {};\n if (options.headers) {\n if (options.headers instanceof Headers) {\n options.headers.forEach((v, k) => { headers[k] = v; });\n }\n else if (Array.isArray(options.headers)) {\n options.headers.forEach(([k, v]) => { headers[k] = v; });\n }\n else {\n Object.assign(headers, options.headers);\n }\n }\n if (!headers['Content-Type']) {\n headers['Content-Type'] = 'application/json';\n }\n options.headers = headers;\n // Default credentials to include in dev mode\n if (isDev && !options.credentials) {\n options.credentials = 'include';\n }\n // Auto-stringify plain object bodies (skip native body types)\n if (options.body && typeof options.body === 'object'\n && !(options.body instanceof ReadableStream)\n && !(options.body instanceof Blob)\n && !(options.body instanceof ArrayBuffer)\n && !(options.body instanceof FormData)\n && !(options.body instanceof URLSearchParams)) {\n options.body = JSON.stringify(options.body);\n }\n // User and Customer apis require CSRF tokens on non-GET requests\n let token;\n if (url.indexOf('/api/user/') > -1 || url.indexOf('/api/customer/') > -1) {\n token = fetch(`${finalBaseUrl}/api/user/customer/authToken`, { credentials: 'include' });\n }\n else {\n token = Promise.resolve(null);\n }\n return token\n .then(res => res?.json())\n .then(res => {\n if (res?.data) {\n headers['X-CSRF-TOKEN'] = res.data;\n }\n return fetch(url, options);\n });\n }\n}\n/** Base URL for production requests */\nKRHttp._baseUrl = '';\n/** Base URL for development (localhost) */\nKRHttp._devUrl = '';\n//# sourceMappingURL=http.js.map"],"names":["KRHttp","configure","config","undefined","baseUrl","_baseUrl","devUrl","_devUrl","fetch","options","url","isDev","window","location","hostname","port","startsWith","finalBaseUrl","indexOf","params","Object","keys","forEach","key","keyIdx","headers","token","Headers","v","k","Array","isArray","assign","credentials","body","ReadableStream","Blob","ArrayBuffer","FormData","URLSearchParams","JSON","stringify","Promise","resolve","then","res","json","data"],"mappings":"AA6DO,MAAMA,EAYT,gBAAOC,CAAUC,QACUC,IAAnBD,EAAOE,UACPJ,EAAOK,SAAWH,EAAOE,cAEPD,IAAlBD,EAAOI,SACPN,EAAOO,QAAUL,EAAOI,OAEhC,CAgBA,YAAOE,CAAMC,GACT,IAAIC,EAAMD,EAAQC,IAClB,IAAIC,EAAqC,cAA7BC,OAAOC,SAASC,UAA4BF,OAAOC,SAASE,KAAKC,WAAW,KACxF,IAAIC,EAAejB,EAAOK,SACtBM,GAASX,EAAOO,UAChBU,EAAejB,EAAOO,SAIE,IAAxBG,EAAIQ,QAAQ,UACZR,EAAMO,EAAeP,GAErBD,EAAQU,SACRT,GAAO,IACPU,OAAOC,KAAKZ,EAAQU,QAAQG,QAAQ,CAACC,EAAKC,KAClCA,EAAS,IACTd,GAAO,KAEXA,GAAO,GAAGa,KAAOd,EAAQU,OAAOI,QAIxC,IAAIE,EAAU,CAAA,EA8Bd,IAAIC,EAOJ,OApCIjB,EAAQgB,UACJhB,EAAQgB,mBAAmBE,QAC3BlB,EAAQgB,QAAQH,QAAQ,CAACM,EAAGC,KAAQJ,EAAQI,GAAKD,IAE5CE,MAAMC,QAAQtB,EAAQgB,SAC3BhB,EAAQgB,QAAQH,QAAQ,EAAEO,EAAGD,MAASH,EAAQI,GAAKD,IAGnDR,OAAOY,OAAOP,EAAShB,EAAQgB,UAGlCA,EAAQ,kBACTA,EAAQ,gBAAkB,oBAE9BhB,EAAQgB,QAAUA,EAEdd,IAAUF,EAAQwB,cAClBxB,EAAQwB,YAAc,YAGtBxB,EAAQyB,MAAgC,iBAAjBzB,EAAQyB,MAC1BzB,EAAQyB,gBAAgBC,gBACxB1B,EAAQyB,gBAAgBE,MACxB3B,EAAQyB,gBAAgBG,aACxB5B,EAAQyB,gBAAgBI,UACxB7B,EAAQyB,gBAAgBK,kBAC7B9B,EAAQyB,KAAOM,KAAKC,UAAUhC,EAAQyB,OAKtCR,EADAhB,EAAIQ,QAAQ,eAAgB,GAAMR,EAAIQ,QAAQ,qBACtCV,MAAM,GAAGS,gCAA4C,CAAEgB,YAAa,YAGpES,QAAQC,QAAQ,MAErBjB,EACFkB,KAAKC,GAAOA,GAAKC,QACjBF,KAAKC,IACFA,GAAKE,OACLtB,EAAQ,gBAAkBoB,EAAIE,MAE3BvC,MAAME,EAAKD,IAE1B,EAGJT,EAAOK,SAAW,GAElBL,EAAOO,QAAU"}
|
package/dist/krubble-http.umd.js
CHANGED
|
@@ -102,8 +102,9 @@
|
|
|
102
102
|
*/
|
|
103
103
|
static fetch(options) {
|
|
104
104
|
let url = options.url;
|
|
105
|
+
var isDev = window.location.hostname === 'localhost' && window.location.port.startsWith('4');
|
|
105
106
|
let finalBaseUrl = KRHttp._baseUrl;
|
|
106
|
-
if (
|
|
107
|
+
if (isDev && KRHttp._devUrl) {
|
|
107
108
|
finalBaseUrl = KRHttp._devUrl;
|
|
108
109
|
}
|
|
109
110
|
// Add on the default base url if user
|
|
@@ -120,15 +121,34 @@
|
|
|
120
121
|
url += `${key}=${options.params[key]}`;
|
|
121
122
|
});
|
|
122
123
|
}
|
|
123
|
-
|
|
124
|
-
|
|
124
|
+
// Normalize headers to a plain object so we can set defaults
|
|
125
|
+
var headers = {};
|
|
126
|
+
if (options.headers) {
|
|
127
|
+
if (options.headers instanceof Headers) {
|
|
128
|
+
options.headers.forEach((v, k) => { headers[k] = v; });
|
|
129
|
+
}
|
|
130
|
+
else if (Array.isArray(options.headers)) {
|
|
131
|
+
options.headers.forEach(([k, v]) => { headers[k] = v; });
|
|
132
|
+
}
|
|
133
|
+
else {
|
|
134
|
+
Object.assign(headers, options.headers);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
if (!headers['Content-Type']) {
|
|
138
|
+
headers['Content-Type'] = 'application/json';
|
|
125
139
|
}
|
|
126
|
-
|
|
127
|
-
|
|
140
|
+
options.headers = headers;
|
|
141
|
+
// Default credentials to include in dev mode
|
|
142
|
+
if (isDev && !options.credentials) {
|
|
143
|
+
options.credentials = 'include';
|
|
128
144
|
}
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
145
|
+
// Auto-stringify plain object bodies (skip native body types)
|
|
146
|
+
if (options.body && typeof options.body === 'object'
|
|
147
|
+
&& !(options.body instanceof ReadableStream)
|
|
148
|
+
&& !(options.body instanceof Blob)
|
|
149
|
+
&& !(options.body instanceof ArrayBuffer)
|
|
150
|
+
&& !(options.body instanceof FormData)
|
|
151
|
+
&& !(options.body instanceof URLSearchParams)) {
|
|
132
152
|
options.body = JSON.stringify(options.body);
|
|
133
153
|
}
|
|
134
154
|
// User and Customer apis require CSRF tokens on non-GET requests
|
|
@@ -143,7 +163,7 @@
|
|
|
143
163
|
.then(res => res?.json())
|
|
144
164
|
.then(res => {
|
|
145
165
|
if (res?.data) {
|
|
146
|
-
|
|
166
|
+
headers['X-CSRF-TOKEN'] = res.data;
|
|
147
167
|
}
|
|
148
168
|
return fetch(url, options);
|
|
149
169
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"krubble-http.umd.js","sources":["http.js"],"sourcesContent":["/**\n * @kodaris/krubble-http\n *\n * HTTP client for making API requests with automatic base URL handling\n * and Kodaris API conveniences (CSRF tokens, credentials).\n *\n * ## Usage\n *\n * ### Web App (UMD)\n * ```html\n * <script src=\"krubble-http.umd.js\"></script>\n * <script>\n * // Configure once at startup\n * kr.http.configure({\n * baseUrl: 'https://kodaris.com',\n * devUrl: 'https://localhost:4201'\n * });\n *\n * // Make requests\n * kr.http.fetch({\n * url: '/api/system/customer/list',\n * method: 'POST',\n * body: JSON.stringify({ page: 0, size: 20 })\n * })\n * .then(response => response.json())\n * .then(data => console.log(data));\n * </script>\n * ```\n *\n * ### Angular / ESM\n * ```typescript\n * // app.config.ts - configure once at startup\n * import { KRHttp } from '@kodaris/krubble-http';\n *\n * KRHttp.configure({\n * baseUrl: 'https://kodaris.com',\n * devUrl: 'https://localhost:4201'\n * });\n *\n * // In a service or component\n * import { KRHttp } from '@kodaris/krubble-http';\n *\n * getCustomers() {\n * return KRHttp.fetch({\n * url: '/api/system/customer/list',\n * method: 'POST',\n * body: JSON.stringify({ page: 0, size: 20 })\n * })\n * .then(response => response.json());\n * }\n * ```\n *\n * ## URL Resolution\n * - On localhost: uses `devUrl` if configured, otherwise `baseUrl`\n * - On production: always uses `baseUrl`\n * - If URL starts with 'http', it's used as-is (no base URL prepended)\n */\n/**\n * HTTP client with static methods for making API requests.\n * Automatically handles base URL resolution and Kodaris API conveniences.\n */\nexport class KRHttp {\n /**\n * Configure the HTTP client. Call once at application startup.\n *\n * @param config - Configuration options\n *\n * @example\n * KRHttp.configure({\n * baseUrl: 'https://kodaris.com',\n * devUrl: 'https://localhost:4201'\n * });\n */\n static configure(config) {\n if (config.baseUrl !== undefined) {\n KRHttp._baseUrl = config.baseUrl;\n }\n if (config.devUrl !== undefined) {\n KRHttp._devUrl = config.devUrl;\n }\n }\n /**\n * Make an HTTP request.\n *\n * @param options - Request options\n * @returns Promise resolving to the Response object\n *\n * @example\n * KRHttp.fetch({\n * url: '/api/system/customer/list',\n * method: 'POST',\n * body: JSON.stringify({ page: 0, size: 20 })\n * })\n * .then(response => response.json())\n * .then(data => console.log(data));\n */\n static fetch(options) {\n let url = options.url;\n
|
|
1
|
+
{"version":3,"file":"krubble-http.umd.js","sources":["http.js"],"sourcesContent":["/**\n * @kodaris/krubble-http\n *\n * HTTP client for making API requests with automatic base URL handling\n * and Kodaris API conveniences (CSRF tokens, credentials).\n *\n * ## Usage\n *\n * ### Web App (UMD)\n * ```html\n * <script src=\"krubble-http.umd.js\"></script>\n * <script>\n * // Configure once at startup\n * kr.http.configure({\n * baseUrl: 'https://kodaris.com',\n * devUrl: 'https://localhost:4201'\n * });\n *\n * // Make requests\n * kr.http.fetch({\n * url: '/api/system/customer/list',\n * method: 'POST',\n * body: JSON.stringify({ page: 0, size: 20 })\n * })\n * .then(response => response.json())\n * .then(data => console.log(data));\n * </script>\n * ```\n *\n * ### Angular / ESM\n * ```typescript\n * // app.config.ts - configure once at startup\n * import { KRHttp } from '@kodaris/krubble-http';\n *\n * KRHttp.configure({\n * baseUrl: 'https://kodaris.com',\n * devUrl: 'https://localhost:4201'\n * });\n *\n * // In a service or component\n * import { KRHttp } from '@kodaris/krubble-http';\n *\n * getCustomers() {\n * return KRHttp.fetch({\n * url: '/api/system/customer/list',\n * method: 'POST',\n * body: JSON.stringify({ page: 0, size: 20 })\n * })\n * .then(response => response.json());\n * }\n * ```\n *\n * ## URL Resolution\n * - On localhost: uses `devUrl` if configured, otherwise `baseUrl`\n * - On production: always uses `baseUrl`\n * - If URL starts with 'http', it's used as-is (no base URL prepended)\n */\n/**\n * HTTP client with static methods for making API requests.\n * Automatically handles base URL resolution and Kodaris API conveniences.\n */\nexport class KRHttp {\n /**\n * Configure the HTTP client. Call once at application startup.\n *\n * @param config - Configuration options\n *\n * @example\n * KRHttp.configure({\n * baseUrl: 'https://kodaris.com',\n * devUrl: 'https://localhost:4201'\n * });\n */\n static configure(config) {\n if (config.baseUrl !== undefined) {\n KRHttp._baseUrl = config.baseUrl;\n }\n if (config.devUrl !== undefined) {\n KRHttp._devUrl = config.devUrl;\n }\n }\n /**\n * Make an HTTP request.\n *\n * @param options - Request options\n * @returns Promise resolving to the Response object\n *\n * @example\n * KRHttp.fetch({\n * url: '/api/system/customer/list',\n * method: 'POST',\n * body: JSON.stringify({ page: 0, size: 20 })\n * })\n * .then(response => response.json())\n * .then(data => console.log(data));\n */\n static fetch(options) {\n let url = options.url;\n var isDev = window.location.hostname === 'localhost' && window.location.port.startsWith('4');\n let finalBaseUrl = KRHttp._baseUrl;\n if (isDev && KRHttp._devUrl) {\n finalBaseUrl = KRHttp._devUrl;\n }\n // Add on the default base url if user\n // has not provided the full url\n if (url.indexOf('http') !== 0) {\n url = finalBaseUrl + url;\n }\n if (options.params) {\n url += '?';\n Object.keys(options.params).forEach((key, keyIdx) => {\n if (keyIdx > 0) {\n url += '&';\n }\n url += `${key}=${options.params[key]}`;\n });\n }\n // Normalize headers to a plain object so we can set defaults\n var headers = {};\n if (options.headers) {\n if (options.headers instanceof Headers) {\n options.headers.forEach((v, k) => { headers[k] = v; });\n }\n else if (Array.isArray(options.headers)) {\n options.headers.forEach(([k, v]) => { headers[k] = v; });\n }\n else {\n Object.assign(headers, options.headers);\n }\n }\n if (!headers['Content-Type']) {\n headers['Content-Type'] = 'application/json';\n }\n options.headers = headers;\n // Default credentials to include in dev mode\n if (isDev && !options.credentials) {\n options.credentials = 'include';\n }\n // Auto-stringify plain object bodies (skip native body types)\n if (options.body && typeof options.body === 'object'\n && !(options.body instanceof ReadableStream)\n && !(options.body instanceof Blob)\n && !(options.body instanceof ArrayBuffer)\n && !(options.body instanceof FormData)\n && !(options.body instanceof URLSearchParams)) {\n options.body = JSON.stringify(options.body);\n }\n // User and Customer apis require CSRF tokens on non-GET requests\n let token;\n if (url.indexOf('/api/user/') > -1 || url.indexOf('/api/customer/') > -1) {\n token = fetch(`${finalBaseUrl}/api/user/customer/authToken`, { credentials: 'include' });\n }\n else {\n token = Promise.resolve(null);\n }\n return token\n .then(res => res?.json())\n .then(res => {\n if (res?.data) {\n headers['X-CSRF-TOKEN'] = res.data;\n }\n return fetch(url, options);\n });\n }\n}\n/** Base URL for production requests */\nKRHttp._baseUrl = '';\n/** Base URL for development (localhost) */\nKRHttp._devUrl = '';\n//# sourceMappingURL=http.js.map"],"names":[],"mappings":";;;;;;IAAA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA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
|
|
|
1
|
-
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).kr=e.kr||{})}(this,function(e){"use strict";class t{static configure(e){void 0!==e.baseUrl&&(t._baseUrl=e.baseUrl),void 0!==e.devUrl&&(t._devUrl=e.devUrl)}static fetch(e){let
|
|
1
|
+
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).kr=e.kr||{})}(this,function(e){"use strict";class t{static configure(e){void 0!==e.baseUrl&&(t._baseUrl=e.baseUrl),void 0!==e.devUrl&&(t._devUrl=e.devUrl)}static fetch(e){let a=e.url;var o="localhost"===window.location.hostname&&window.location.port.startsWith("4");let r=t._baseUrl;o&&t._devUrl&&(r=t._devUrl),0!==a.indexOf("http")&&(a=r+a),e.params&&(a+="?",Object.keys(e.params).forEach((t,o)=>{o>0&&(a+="&"),a+=`${t}=${e.params[t]}`}));var s={};let n;return e.headers&&(e.headers instanceof Headers?e.headers.forEach((e,t)=>{s[t]=e}):Array.isArray(e.headers)?e.headers.forEach(([e,t])=>{s[e]=t}):Object.assign(s,e.headers)),s["Content-Type"]||(s["Content-Type"]="application/json"),e.headers=s,o&&!e.credentials&&(e.credentials="include"),!e.body||"object"!=typeof e.body||e.body instanceof ReadableStream||e.body instanceof Blob||e.body instanceof ArrayBuffer||e.body instanceof FormData||e.body instanceof URLSearchParams||(e.body=JSON.stringify(e.body)),n=a.indexOf("/api/user/")>-1||a.indexOf("/api/customer/")>-1?fetch(`${r}/api/user/customer/authToken`,{credentials:"include"}):Promise.resolve(null),n.then(e=>e?.json()).then(t=>(t?.data&&(s["X-CSRF-TOKEN"]=t.data),fetch(a,e)))}}t._baseUrl="",t._devUrl="",e.KRHttp=t,e.http=t}),function(e){Object.keys(e).forEach(function(t){t.startsWith("KR")&&"function"==typeof e[t]&&(e[t.slice(2)]=e[t])})}(kr);
|
|
2
2
|
//# sourceMappingURL=krubble-http.umd.min.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"krubble-http.umd.min.js","sources":["http.js"],"sourcesContent":["/**\n * @kodaris/krubble-http\n *\n * HTTP client for making API requests with automatic base URL handling\n * and Kodaris API conveniences (CSRF tokens, credentials).\n *\n * ## Usage\n *\n * ### Web App (UMD)\n * ```html\n * <script src=\"krubble-http.umd.js\"></script>\n * <script>\n * // Configure once at startup\n * kr.http.configure({\n * baseUrl: 'https://kodaris.com',\n * devUrl: 'https://localhost:4201'\n * });\n *\n * // Make requests\n * kr.http.fetch({\n * url: '/api/system/customer/list',\n * method: 'POST',\n * body: JSON.stringify({ page: 0, size: 20 })\n * })\n * .then(response => response.json())\n * .then(data => console.log(data));\n * </script>\n * ```\n *\n * ### Angular / ESM\n * ```typescript\n * // app.config.ts - configure once at startup\n * import { KRHttp } from '@kodaris/krubble-http';\n *\n * KRHttp.configure({\n * baseUrl: 'https://kodaris.com',\n * devUrl: 'https://localhost:4201'\n * });\n *\n * // In a service or component\n * import { KRHttp } from '@kodaris/krubble-http';\n *\n * getCustomers() {\n * return KRHttp.fetch({\n * url: '/api/system/customer/list',\n * method: 'POST',\n * body: JSON.stringify({ page: 0, size: 20 })\n * })\n * .then(response => response.json());\n * }\n * ```\n *\n * ## URL Resolution\n * - On localhost: uses `devUrl` if configured, otherwise `baseUrl`\n * - On production: always uses `baseUrl`\n * - If URL starts with 'http', it's used as-is (no base URL prepended)\n */\n/**\n * HTTP client with static methods for making API requests.\n * Automatically handles base URL resolution and Kodaris API conveniences.\n */\nexport class KRHttp {\n /**\n * Configure the HTTP client. Call once at application startup.\n *\n * @param config - Configuration options\n *\n * @example\n * KRHttp.configure({\n * baseUrl: 'https://kodaris.com',\n * devUrl: 'https://localhost:4201'\n * });\n */\n static configure(config) {\n if (config.baseUrl !== undefined) {\n KRHttp._baseUrl = config.baseUrl;\n }\n if (config.devUrl !== undefined) {\n KRHttp._devUrl = config.devUrl;\n }\n }\n /**\n * Make an HTTP request.\n *\n * @param options - Request options\n * @returns Promise resolving to the Response object\n *\n * @example\n * KRHttp.fetch({\n * url: '/api/system/customer/list',\n * method: 'POST',\n * body: JSON.stringify({ page: 0, size: 20 })\n * })\n * .then(response => response.json())\n * .then(data => console.log(data));\n */\n static fetch(options) {\n let url = options.url;\n
|
|
1
|
+
{"version":3,"file":"krubble-http.umd.min.js","sources":["http.js"],"sourcesContent":["/**\n * @kodaris/krubble-http\n *\n * HTTP client for making API requests with automatic base URL handling\n * and Kodaris API conveniences (CSRF tokens, credentials).\n *\n * ## Usage\n *\n * ### Web App (UMD)\n * ```html\n * <script src=\"krubble-http.umd.js\"></script>\n * <script>\n * // Configure once at startup\n * kr.http.configure({\n * baseUrl: 'https://kodaris.com',\n * devUrl: 'https://localhost:4201'\n * });\n *\n * // Make requests\n * kr.http.fetch({\n * url: '/api/system/customer/list',\n * method: 'POST',\n * body: JSON.stringify({ page: 0, size: 20 })\n * })\n * .then(response => response.json())\n * .then(data => console.log(data));\n * </script>\n * ```\n *\n * ### Angular / ESM\n * ```typescript\n * // app.config.ts - configure once at startup\n * import { KRHttp } from '@kodaris/krubble-http';\n *\n * KRHttp.configure({\n * baseUrl: 'https://kodaris.com',\n * devUrl: 'https://localhost:4201'\n * });\n *\n * // In a service or component\n * import { KRHttp } from '@kodaris/krubble-http';\n *\n * getCustomers() {\n * return KRHttp.fetch({\n * url: '/api/system/customer/list',\n * method: 'POST',\n * body: JSON.stringify({ page: 0, size: 20 })\n * })\n * .then(response => response.json());\n * }\n * ```\n *\n * ## URL Resolution\n * - On localhost: uses `devUrl` if configured, otherwise `baseUrl`\n * - On production: always uses `baseUrl`\n * - If URL starts with 'http', it's used as-is (no base URL prepended)\n */\n/**\n * HTTP client with static methods for making API requests.\n * Automatically handles base URL resolution and Kodaris API conveniences.\n */\nexport class KRHttp {\n /**\n * Configure the HTTP client. Call once at application startup.\n *\n * @param config - Configuration options\n *\n * @example\n * KRHttp.configure({\n * baseUrl: 'https://kodaris.com',\n * devUrl: 'https://localhost:4201'\n * });\n */\n static configure(config) {\n if (config.baseUrl !== undefined) {\n KRHttp._baseUrl = config.baseUrl;\n }\n if (config.devUrl !== undefined) {\n KRHttp._devUrl = config.devUrl;\n }\n }\n /**\n * Make an HTTP request.\n *\n * @param options - Request options\n * @returns Promise resolving to the Response object\n *\n * @example\n * KRHttp.fetch({\n * url: '/api/system/customer/list',\n * method: 'POST',\n * body: JSON.stringify({ page: 0, size: 20 })\n * })\n * .then(response => response.json())\n * .then(data => console.log(data));\n */\n static fetch(options) {\n let url = options.url;\n var isDev = window.location.hostname === 'localhost' && window.location.port.startsWith('4');\n let finalBaseUrl = KRHttp._baseUrl;\n if (isDev && KRHttp._devUrl) {\n finalBaseUrl = KRHttp._devUrl;\n }\n // Add on the default base url if user\n // has not provided the full url\n if (url.indexOf('http') !== 0) {\n url = finalBaseUrl + url;\n }\n if (options.params) {\n url += '?';\n Object.keys(options.params).forEach((key, keyIdx) => {\n if (keyIdx > 0) {\n url += '&';\n }\n url += `${key}=${options.params[key]}`;\n });\n }\n // Normalize headers to a plain object so we can set defaults\n var headers = {};\n if (options.headers) {\n if (options.headers instanceof Headers) {\n options.headers.forEach((v, k) => { headers[k] = v; });\n }\n else if (Array.isArray(options.headers)) {\n options.headers.forEach(([k, v]) => { headers[k] = v; });\n }\n else {\n Object.assign(headers, options.headers);\n }\n }\n if (!headers['Content-Type']) {\n headers['Content-Type'] = 'application/json';\n }\n options.headers = headers;\n // Default credentials to include in dev mode\n if (isDev && !options.credentials) {\n options.credentials = 'include';\n }\n // Auto-stringify plain object bodies (skip native body types)\n if (options.body && typeof options.body === 'object'\n && !(options.body instanceof ReadableStream)\n && !(options.body instanceof Blob)\n && !(options.body instanceof ArrayBuffer)\n && !(options.body instanceof FormData)\n && !(options.body instanceof URLSearchParams)) {\n options.body = JSON.stringify(options.body);\n }\n // User and Customer apis require CSRF tokens on non-GET requests\n let token;\n if (url.indexOf('/api/user/') > -1 || url.indexOf('/api/customer/') > -1) {\n token = fetch(`${finalBaseUrl}/api/user/customer/authToken`, { credentials: 'include' });\n }\n else {\n token = Promise.resolve(null);\n }\n return token\n .then(res => res?.json())\n .then(res => {\n if (res?.data) {\n headers['X-CSRF-TOKEN'] = res.data;\n }\n return fetch(url, options);\n });\n }\n}\n/** Base URL for production requests */\nKRHttp._baseUrl = '';\n/** Base URL for development (localhost) */\nKRHttp._devUrl = '';\n//# sourceMappingURL=http.js.map"],"names":["KRHttp","configure","config","undefined","baseUrl","_baseUrl","devUrl","_devUrl","fetch","options","url","isDev","window","location","hostname","port","startsWith","finalBaseUrl","indexOf","params","Object","keys","forEach","key","keyIdx","headers","token","Headers","v","k","Array","isArray","assign","credentials","body","ReadableStream","Blob","ArrayBuffer","FormData","URLSearchParams","JSON","stringify","Promise","resolve","then","res","json","data"],"mappings":"+OA6DO,MAAMA,EAYT,gBAAOC,CAAUC,QACUC,IAAnBD,EAAOE,UACPJ,EAAOK,SAAWH,EAAOE,cAEPD,IAAlBD,EAAOI,SACPN,EAAOO,QAAUL,EAAOI,OAEhC,CAgBA,YAAOE,CAAMC,GACT,IAAIC,EAAMD,EAAQC,IAClB,IAAIC,EAAqC,cAA7BC,OAAOC,SAASC,UAA4BF,OAAOC,SAASE,KAAKC,WAAW,KACxF,IAAIC,EAAejB,EAAOK,SACtBM,GAASX,EAAOO,UAChBU,EAAejB,EAAOO,SAIE,IAAxBG,EAAIQ,QAAQ,UACZR,EAAMO,EAAeP,GAErBD,EAAQU,SACRT,GAAO,IACPU,OAAOC,KAAKZ,EAAQU,QAAQG,QAAQ,CAACC,EAAKC,KAClCA,EAAS,IACTd,GAAO,KAEXA,GAAO,GAAGa,KAAOd,EAAQU,OAAOI,QAIxC,IAAIE,EAAU,CAAA,EA8Bd,IAAIC,EAOJ,OApCIjB,EAAQgB,UACJhB,EAAQgB,mBAAmBE,QAC3BlB,EAAQgB,QAAQH,QAA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