@rzl-zone/utils-js 3.0.2-beta.2 → 3.1.1-beta.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/NumberRangeUnion-DC-C3_Kq.d.ts +23 -23
- package/dist/any-BmdI8UbK.d.ts +56 -56
- package/dist/arrays-normalize-recursive-CnjYJ9xg.d.ts +66 -66
- package/dist/assertions/index.cjs +1 -1
- package/dist/assertions/index.d.ts +398 -418
- package/dist/assertions/index.js +1 -1
- package/dist/chunk-26W3VZIK.cjs +1 -0
- package/dist/chunk-2KRCHXFG.cjs +2 -0
- package/dist/chunk-33ZLH266.js +1 -0
- package/dist/{chunk-EXZ47NOW.js → chunk-4L4EJTNZ.js} +1 -1
- package/dist/chunk-5Q37AP7P.js +1 -0
- package/dist/chunk-7CHFAMS2.js +1 -0
- package/dist/chunk-7MMJZ6HS.js +1 -0
- package/dist/chunk-7TC6DE3J.cjs +1 -0
- package/dist/{chunk-XH6MCRLP.js → chunk-7VX33E3V.js} +1 -1
- package/dist/chunk-BEZPD44N.cjs +1 -0
- package/dist/chunk-BI263OHW.js +1 -0
- package/dist/chunk-CII6WWLO.cjs +1 -0
- package/dist/chunk-D5TYPCWU.cjs +1 -0
- package/dist/chunk-EEQY5QUG.js +1 -0
- package/dist/chunk-ESJYBIYH.js +1 -0
- package/dist/chunk-EWYPSKKM.js +1 -0
- package/dist/chunk-EXCOQLWX.cjs +1 -0
- package/dist/chunk-FWQVTMYA.cjs +1 -0
- package/dist/chunk-G4EWCRKS.js +1 -0
- package/dist/chunk-G7R5XZRM.cjs +1 -0
- package/dist/chunk-GGEV7LXX.cjs +1 -0
- package/dist/chunk-HJOZOENU.cjs +1 -0
- package/dist/{chunk-BYZAD3XN.cjs → chunk-IRDZVLNZ.cjs} +1 -1
- package/dist/chunk-IW73G523.cjs +1 -0
- package/dist/chunk-IY5OZAB3.cjs +1 -0
- package/dist/chunk-J4NCYYGS.js +1 -0
- package/dist/chunk-J7JAZAJD.js +1 -0
- package/dist/{chunk-FF76ISQ2.js → chunk-JYNSO7ZY.js} +1 -1
- package/dist/chunk-K3UVKYEU.js +1 -0
- package/dist/chunk-LQ76EDZG.js +1 -0
- package/dist/chunk-MROH35V3.cjs +1 -0
- package/dist/chunk-NJSAV73S.js +1 -0
- package/dist/chunk-NR44SNYE.js +1 -0
- package/dist/chunk-NXMTWY77.js +1 -0
- package/dist/chunk-OZCQZO2V.cjs +4 -0
- package/dist/chunk-Q3NTKN5I.cjs +1 -0
- package/dist/chunk-QDNSC57Z.js +4 -0
- package/dist/chunk-QGXTL5ZH.js +2 -0
- package/dist/{chunk-JWHM3WZQ.cjs → chunk-QSQDP7L2.cjs} +1 -1
- package/dist/{chunk-RN3TP3S3.js → chunk-S5M33A4O.js} +1 -1
- package/dist/chunk-SGTHOQQ2.js +1 -0
- package/dist/chunk-SL3RQOWA.js +1 -0
- package/dist/chunk-SZPWZZYG.cjs +1 -0
- package/dist/chunk-UA4B7JBO.cjs +1 -0
- package/dist/chunk-UQDJF5RR.cjs +1 -0
- package/dist/chunk-UTQUYABU.cjs +1 -0
- package/dist/chunk-VPW3DRV5.js +1 -0
- package/dist/chunk-W32WALNU.js +1 -0
- package/dist/chunk-XGZ6K753.cjs +1 -0
- package/dist/chunk-YFTR64N5.cjs +1 -0
- package/dist/chunk-ZFDS7W6B.cjs +1 -0
- package/dist/chunk-ZQOLZE6Z.cjs +1 -0
- package/dist/conversions/index.cjs +1 -1
- package/dist/conversions/index.d.ts +1307 -1504
- package/dist/conversions/index.js +1 -1
- package/dist/env/index.d.ts +14 -17
- package/dist/events/index.cjs +1 -1
- package/dist/events/index.d.ts +111 -108
- package/dist/events/index.js +1 -1
- package/dist/extends-Bk_SBGdT.d.ts +367 -0
- package/dist/formatting/index.cjs +1 -1
- package/dist/formatting/index.d.ts +878 -840
- package/dist/formatting/index.js +1 -1
- package/dist/generator/index.cjs +1 -1
- package/dist/generator/index.d.ts +173 -150
- package/dist/generator/index.js +1 -1
- package/dist/if-CvT4R7Kh.d.ts +16 -16
- package/dist/index.cjs +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/is-array--YjXV-Wx.d.ts +1207 -0
- package/dist/isPlainObject-BVhBAPHX.d.ts +169 -0
- package/dist/never-BfayMBF9.d.ts +64 -64
- package/dist/next/index.cjs +6 -6
- package/dist/next/index.d.ts +196 -176
- package/dist/next/index.js +5 -5
- package/dist/next/server/index.cjs +1 -1
- package/dist/next/server/index.d.ts +26 -40
- package/dist/next/server/index.js +1 -1
- package/dist/nils-DMz3kU7M.d.ts +155 -155
- package/dist/omit-VvmIsZmX.d.ts +25 -25
- package/dist/operations/index.cjs +1 -1
- package/dist/operations/index.d.ts +121 -176
- package/dist/operations/index.js +1 -1
- package/dist/parsers/index.cjs +1 -1
- package/dist/parsers/index.d.ts +35 -36
- package/dist/parsers/index.js +1 -1
- package/dist/predicates/index.cjs +1 -1
- package/dist/predicates/index.d.ts +1701 -1675
- package/dist/predicates/index.js +1 -1
- package/dist/prettify-C4xLcYOP.d.ts +328 -328
- package/dist/promise/index.cjs +1 -1
- package/dist/promise/index.d.ts +29 -38
- package/dist/promise/index.js +1 -1
- package/dist/rzl-utils.global.js +3 -3
- package/dist/string-XA-til3C.d.ts +351 -0
- package/dist/strings/index.cjs +1 -1
- package/dist/strings/index.d.ts +712 -787
- package/dist/strings/index.js +1 -1
- package/dist/stylings/index.cjs +1 -1
- package/dist/stylings/index.d.ts +37 -45
- package/dist/stylings/index.js +1 -1
- package/dist/type-data-DDs-u2kq.d.ts +241 -241
- package/dist/types/index.d.ts +3266 -3266
- package/dist/urls/index.cjs +1 -1
- package/dist/urls/index.d.ts +384 -409
- package/dist/urls/index.js +1 -1
- package/package.json +53 -11
- package/dist/array-CIZRbqTF.d.ts +0 -223
- package/dist/chunk-2TRAPBZ7.cjs +0 -1
- package/dist/chunk-3D74QO5D.cjs +0 -1
- package/dist/chunk-4DK3RCC4.js +0 -2
- package/dist/chunk-4JOQ45HL.js +0 -1
- package/dist/chunk-55CZALRS.js +0 -1
- package/dist/chunk-5JFV3GDE.cjs +0 -4
- package/dist/chunk-62HX5Z45.cjs +0 -1
- package/dist/chunk-7NA6DUHR.cjs +0 -2
- package/dist/chunk-BVPMMWDL.cjs +0 -1
- package/dist/chunk-CEFYHEM4.cjs +0 -1
- package/dist/chunk-CN53M4QZ.cjs +0 -1
- package/dist/chunk-D47MHBSD.cjs +0 -1
- package/dist/chunk-DGH75GJD.js +0 -1
- package/dist/chunk-E5NUI7PN.js +0 -1
- package/dist/chunk-FDITZ5C6.cjs +0 -1
- package/dist/chunk-FSSV24W7.cjs +0 -1
- package/dist/chunk-GCGU2WB7.js +0 -1
- package/dist/chunk-GECI2YBP.js +0 -1
- package/dist/chunk-GQE4OVHC.cjs +0 -1
- package/dist/chunk-GRVZXQXL.cjs +0 -1
- package/dist/chunk-I4AVNHPA.cjs +0 -1
- package/dist/chunk-JFRL7NKU.js +0 -4
- package/dist/chunk-KCQDDZJE.cjs +0 -1
- package/dist/chunk-L54ZPSYJ.js +0 -1
- package/dist/chunk-LVKAYEZ4.js +0 -1
- package/dist/chunk-MBDWTK54.cjs +0 -1
- package/dist/chunk-MNGGDB2G.js +0 -1
- package/dist/chunk-MY7BA4GI.cjs +0 -1
- package/dist/chunk-N2IJPIND.cjs +0 -1
- package/dist/chunk-NIMNTEGV.js +0 -1
- package/dist/chunk-PVJF2JHM.js +0 -1
- package/dist/chunk-QCFXEUKL.js +0 -1
- package/dist/chunk-QFCGBBSY.js +0 -1
- package/dist/chunk-QQYAUPSK.cjs +0 -1
- package/dist/chunk-TC4VBE4Y.cjs +0 -1
- package/dist/chunk-TUXDINHF.cjs +0 -1
- package/dist/chunk-U5Y2FXMN.cjs +0 -1
- package/dist/chunk-UUPQI6ND.cjs +0 -1
- package/dist/chunk-VYCGZ2S6.js +0 -1
- package/dist/chunk-W5EDKJK3.js +0 -1
- package/dist/chunk-WHAVUFEU.js +0 -1
- package/dist/chunk-XCFIOTCV.js +0 -1
- package/dist/chunk-XE27XPJR.js +0 -1
- package/dist/chunk-XVB3ZZEB.js +0 -1
- package/dist/chunk-ZBSIGJFO.cjs +0 -1
- package/dist/chunk-ZYAML74V.js +0 -1
- package/dist/extends-Mp81Hq9-.d.ts +0 -145
- package/dist/is-array-Ckm_47hw.d.ts +0 -1557
- package/dist/isPlainObject-BKYaI6a8.d.ts +0 -182
package/dist/next/index.d.ts
CHANGED
|
@@ -1,184 +1,204 @@
|
|
|
1
1
|
import{I as IsAny}from'../any-BmdI8UbK.js';import'../if-CvT4R7Kh.js';
|
|
2
|
-
/**
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
|
|
9
|
-
*
|
|
10
|
-
* @template T - The route string containing potential dynamic segments.
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
* type
|
|
18
|
-
* //
|
|
19
|
-
*
|
|
20
|
-
|
|
21
|
-
* // Result: {} (no dynamic parameters)
|
|
22
|
-
* ```
|
|
23
|
-
*/
|
|
2
|
+
/** ---------------------------------------------------------
|
|
3
|
+
* * ***Extracts dynamic route parameters from a given route string.***
|
|
4
|
+
* ---------------------------------------------------------
|
|
5
|
+
* **This utility type recursively searches for dynamic segments within a route,
|
|
6
|
+
* extracting each parameter and constructing an object where each key represents
|
|
7
|
+
* a dynamic segment and its value is of type `string`.**
|
|
8
|
+
* - ***⚠️ Warning:***
|
|
9
|
+
* - ***This types only support when using ***[`NextJS`](https://nextjs.org/)***.***
|
|
10
|
+
* @template T - The route string containing potential dynamic segments.
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* type Params1 = ExtractRouteParams<"/user/[id]">;
|
|
14
|
+
* // ➔ { id: string }
|
|
15
|
+
* type Params2 = ExtractRouteParams<"/post/[slug]/comment/[commentId]">;
|
|
16
|
+
* // ➔ { slug: string; commentId: string }
|
|
17
|
+
* type Params3 = ExtractRouteParams<"/dashboard">;
|
|
18
|
+
* // ➔ {} (no dynamic parameters)
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
24
21
|
type ExtractRouteParams<T>=T extends string?HasDynamicSegments<T>extends true?T extends`${infer _Start}[${infer Param}]${infer Rest}`?{[K in Param|keyof ExtractRouteParams<Rest>]:string;}:unknown:unknown:unknown;
|
|
25
|
-
/**
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
|
|
31
|
-
*
|
|
32
|
-
* @template T - The route string to be evaluated.
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
* type
|
|
40
|
-
* //
|
|
41
|
-
*
|
|
42
|
-
|
|
43
|
-
* // Result: true
|
|
44
|
-
* ```
|
|
45
|
-
*/
|
|
22
|
+
/** ---------------------------------------------------------
|
|
23
|
+
* * ***Determines whether a given route contains dynamic segments.***
|
|
24
|
+
* ---------------------------------------------------------
|
|
25
|
+
* **This type checks if the route includes at least one `[param]` pattern.
|
|
26
|
+
* If it does, the result is `true`, otherwise `false`.**
|
|
27
|
+
* - ***⚠️ Warning:***
|
|
28
|
+
* - ***This types only support when using ***[`NextJS`](https://nextjs.org/)***.***
|
|
29
|
+
* @template T - The route string to be evaluated.
|
|
30
|
+
* @example
|
|
31
|
+
* ```ts
|
|
32
|
+
* type HasParams1 = HasDynamicSegments<"/user/[id]">;
|
|
33
|
+
* // ➔ true
|
|
34
|
+
* type HasParams2 = HasDynamicSegments<"/settings/profile">;
|
|
35
|
+
* // ➔ false
|
|
36
|
+
* type HasParams3 = HasDynamicSegments<"/blog/[category]/[slug]">;
|
|
37
|
+
* // ➔ true
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
46
40
|
type HasDynamicSegments<T>=T extends`${string}[${string}]${string}`?true:false;type GenerateRouteResult<T>=true extends IsAny<T>?unknown:T extends string?string:unknown;
|
|
47
41
|
/** ---------------------------------
|
|
48
|
-
* * ***Generates a URL by replacing dynamic route parameters with provided values.***
|
|
49
|
-
* ---------------------------------
|
|
50
|
-
*
|
|
51
|
-
|
|
52
|
-
*
|
|
53
|
-
* @
|
|
54
|
-
*
|
|
55
|
-
* @
|
|
56
|
-
* @
|
|
57
|
-
*
|
|
58
|
-
* @
|
|
59
|
-
*
|
|
60
|
-
* @
|
|
61
|
-
*
|
|
62
|
-
*
|
|
63
|
-
*
|
|
64
|
-
*
|
|
65
|
-
*
|
|
66
|
-
*
|
|
67
|
-
*
|
|
68
|
-
*
|
|
69
|
-
*
|
|
70
|
-
*
|
|
71
|
-
* //
|
|
72
|
-
*
|
|
73
|
-
* //
|
|
74
|
-
*
|
|
75
|
-
*
|
|
76
|
-
*
|
|
77
|
-
*
|
|
78
|
-
*
|
|
79
|
-
*
|
|
80
|
-
*
|
|
81
|
-
* //
|
|
82
|
-
* generateRoute("/
|
|
83
|
-
* // ❌ Error: ❌ Parameter "
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
*
|
|
87
|
-
*
|
|
88
|
-
*
|
|
89
|
-
|
|
90
|
-
*
|
|
91
|
-
*
|
|
92
|
-
*
|
|
93
|
-
|
|
94
|
-
*/
|
|
95
|
-
declare function generateRoute<T extends string>(route:T extends string?(HasDynamicSegments<T>extends true?T:never):never,params:T extends string?ExtractRouteParams<T>:undefined):GenerateRouteResult<T>;declare function generateRoute<T extends string>(route:T extends string?T:never,params?:Extract<ExtractRouteParams<T>,Record<string,unknown>>):GenerateRouteResult<T>;declare function generateRoute<T=unknown>(route:T extends string?(HasDynamicSegments<T>extends true?T:unknown):unknown,params?:T extends string?ExtractRouteParams<T>:undefined):unknown;
|
|
42
|
+
* * ***Generates a URL by replacing dynamic route parameters with provided values.***
|
|
43
|
+
* ---------------------------------
|
|
44
|
+
* - ***⚠️ Warning:***
|
|
45
|
+
* - ***This function only support when using ***[`NextJS`](https://nextjs.org/)***.***
|
|
46
|
+
* @template T - The route string containing dynamic segments in the format `[param]`.
|
|
47
|
+
* @param {T} route - The route string containing dynamic segments.
|
|
48
|
+
* @param {ExtractRouteParams<T>} [params] - An object containing key-value pairs that match the dynamic segments in the route.
|
|
49
|
+
* @returns {string} The formatted URL with all dynamic segments replaced.
|
|
50
|
+
* @throws {Error} If the route contains dynamic segments but no parameters object is provided.
|
|
51
|
+
* @throws {Error} If a required parameter is missing from the `params` object.
|
|
52
|
+
* @throws {Error} If a parameter value is an empty string.
|
|
53
|
+
* @throws {Error} If any parameter contains invalid characters like `?`, `&`, `=`, `#`, `/`, spaces, `'`, `"`, `(`, `)`, `+`, `;`, `%`, `@`, or `:`, which can cause URL issues.
|
|
54
|
+
* @example
|
|
55
|
+
* // Basic usage
|
|
56
|
+
* generateRoute("/user/[id]", { id: "123" });
|
|
57
|
+
* // ➔ "/user/123"
|
|
58
|
+
*
|
|
59
|
+
* // No dynamic segments, returns as-is
|
|
60
|
+
* generateRoute("/dashboard");
|
|
61
|
+
* // ➔ "/dashboard"
|
|
62
|
+
*
|
|
63
|
+
* // Throws an error due to missing parameters object
|
|
64
|
+
* generateRoute("/profile/[username]");
|
|
65
|
+
* // ➔ ❌ Error: ❌ Missing parameters object for route: "/profile/[username]"
|
|
66
|
+
*
|
|
67
|
+
* // Throws an error due to an empty parameter value
|
|
68
|
+
* generateRoute("/post/[category]/[slug]", { category: "tech", slug: "" });
|
|
69
|
+
* // ➔ ❌ Error: ❌ Parameter "slug" cannot be empty in route: "/post/[category]/[slug]"
|
|
70
|
+
*
|
|
71
|
+
* // Throws an error due to parameter containing invalid characters
|
|
72
|
+
* generateRoute("/search/[query]", { query: "how to?learn" });
|
|
73
|
+
* // ➔ ❌ Error: ❌ Parameter "query" contains invalid character "?" in route: "/search/[query]"
|
|
74
|
+
*
|
|
75
|
+
* // Handles leading/trailing slashes correctly
|
|
76
|
+
* generateRoute("/blog/[category]/[slug]", { category: "/news/", slug: "/latest-update/" });
|
|
77
|
+
* // ➔ ❌ Error: ❌ Parameter "category" and "slug" contains slashes "/" which is not allowed.
|
|
78
|
+
*/
|
|
79
|
+
declare function generateRoute<T extends string>(route:T extends string?(HasDynamicSegments<T>extends true?T:never):never,params:T extends string?ExtractRouteParams<T>:undefined):GenerateRouteResult<T>;declare function generateRoute<T extends string>(route:T extends string?T:never,params?:Extract<ExtractRouteParams<T>,Record<string,unknown>>):GenerateRouteResult<T>;declare function generateRoute<T=unknown>(route:T extends string?(HasDynamicSegments<T>extends true?T:unknown):unknown,params?:T extends string?ExtractRouteParams<T>:undefined):unknown;type OptionsCreateBeApiUrl={
|
|
80
|
+
/** * The prefix pathname api url, e.g:`"http://localhost.com/your-target-prefix-entri-point-api-is-here"`, default: `"/api"`.
|
|
81
|
+
*
|
|
82
|
+
* @default "/api" */
|
|
83
|
+
prefix?:string;
|
|
84
|
+
/** * Option to getting `prefix` and `pathname` of api url only `(removing origin base api url)`, default: `true`.
|
|
85
|
+
*
|
|
86
|
+
* @default true */
|
|
87
|
+
withOrigin?:boolean;};
|
|
96
88
|
/** ---------------------------------
|
|
97
|
-
* * ***Constructs a backend API URL by appending a given pathname to the base API URL.***
|
|
98
|
-
* ---------------------------------
|
|
99
|
-
* This function builds on top of `getBeApiUrl()
|
|
100
|
-
* -
|
|
101
|
-
*
|
|
102
|
-
*
|
|
103
|
-
* Features of this function
|
|
104
|
-
*
|
|
105
|
-
*
|
|
106
|
-
*
|
|
107
|
-
*
|
|
108
|
-
|
|
109
|
-
*
|
|
110
|
-
* @param {
|
|
111
|
-
* @param {
|
|
112
|
-
* @param {
|
|
113
|
-
* @
|
|
114
|
-
* @
|
|
115
|
-
*
|
|
116
|
-
* @throws {
|
|
117
|
-
* @
|
|
118
|
-
*
|
|
119
|
-
*
|
|
120
|
-
*
|
|
121
|
-
*
|
|
122
|
-
*
|
|
123
|
-
*
|
|
124
|
-
* createBeApiUrl("/users"
|
|
125
|
-
* //
|
|
126
|
-
|
|
89
|
+
* * ***Constructs a backend API URL by appending a given pathname to the base API URL.***
|
|
90
|
+
* ---------------------------------
|
|
91
|
+
* This function builds on top of `getBeApiUrl()`.
|
|
92
|
+
* - **Determines the base API URL from:**
|
|
93
|
+
* - `NEXT_PUBLIC_BACKEND_API_URL` environment variable (or defaults to `"http://localhost:8000"`).
|
|
94
|
+
* - Automatically appends `NEXT_PUBLIC_PORT_BE` if the base URL does not already include a port.
|
|
95
|
+
* - **Features of this function:**
|
|
96
|
+
* - Allows customizing the API path with an optional `prefix` (defaults to `"/api"`).
|
|
97
|
+
* - Can include or exclude the origin (protocol + host) via `withOrigin`.
|
|
98
|
+
* - Normalizes paths to avoid duplicate slashes.
|
|
99
|
+
* - ***⚠️ Warning:***
|
|
100
|
+
* - ***This function only support when using ***[`NextJS`](https://nextjs.org/)***.***
|
|
101
|
+
* @param {string|null|undefined} pathname - The API endpoint path (e.g., `/users` or `/v1/posts`), defaultValue: `""`.
|
|
102
|
+
* @param {OptionsCreateBeApiUrl} [options] - Configuration options.
|
|
103
|
+
* @param {OptionsCreateBeApiUrl["prefix"]} [options.prefix="/api"] - The prefix for the API path (default is `"/api"`).
|
|
104
|
+
* @param {OptionsCreateBeApiUrl["withOrigin"]} [options.withOrigin=true] - Whether to include the full base URL or return only the API path.
|
|
105
|
+
* @returns {string} The formatted API URL.
|
|
106
|
+
* @throws {TypeError} If `withOrigin` is not a boolean.
|
|
107
|
+
* @throws {TypeError} If `prefix` and `pathname` is not a string.
|
|
108
|
+
* @throws {Error} If constructing the API URL fails due to an invalid base URL.
|
|
109
|
+
* @example
|
|
110
|
+
* createBeApiUrl("/users")
|
|
111
|
+
* // ➔ "http://localhost:8000/api/users"
|
|
112
|
+
* createBeApiUrl("/api/users")
|
|
113
|
+
* // ➔ "http://localhost:8000/api/users"
|
|
114
|
+
* createBeApiUrl("/v1", { prefix: "/v1" })
|
|
115
|
+
* // ➔ "http://localhost:8000/v1"
|
|
116
|
+
* createBeApiUrl("/v1/users")
|
|
117
|
+
* // ➔ "http://localhost:8000/api/v1/users"
|
|
118
|
+
* createBeApiUrl("/v1/users", { prefix: "/v1" })
|
|
119
|
+
* // ➔ "http://localhost:8000/v1/users"
|
|
120
|
+
* createBeApiUrl("/users", { withOrigin: false })
|
|
121
|
+
* // ➔ "/api/users"
|
|
122
|
+
* createBeApiUrl(null, { withOrigin: false })
|
|
123
|
+
* // ➔ "/api"
|
|
124
|
+
* createBeApiUrl(undefined, { withOrigin: false })
|
|
125
|
+
* // ➔ "/api"
|
|
126
|
+
*/
|
|
127
127
|
declare const createBeApiUrl:(
|
|
128
128
|
/** * The pathname api url, e.g:`"http://localhost.com/your-target-prefix-entri-point-api-is-here/your-target-pathname-is-here"`.
|
|
129
|
-
*
|
|
130
|
-
* @default ""
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
*
|
|
134
|
-
*
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
*
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
*
|
|
143
|
-
*
|
|
144
|
-
*
|
|
145
|
-
*
|
|
146
|
-
*
|
|
147
|
-
*
|
|
148
|
-
|
|
149
|
-
*
|
|
150
|
-
*
|
|
151
|
-
*
|
|
152
|
-
*
|
|
153
|
-
*
|
|
154
|
-
*
|
|
155
|
-
*
|
|
156
|
-
* @
|
|
157
|
-
*
|
|
158
|
-
*
|
|
159
|
-
*
|
|
160
|
-
*
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
*
|
|
165
|
-
*
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
*
|
|
170
|
-
*
|
|
171
|
-
*
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
*
|
|
176
|
-
*
|
|
177
|
-
*
|
|
178
|
-
* -
|
|
179
|
-
*
|
|
180
|
-
*
|
|
181
|
-
*
|
|
182
|
-
*
|
|
183
|
-
|
|
129
|
+
*
|
|
130
|
+
* @default ""
|
|
131
|
+
*/
|
|
132
|
+
pathname:string|null|undefined,options?:OptionsCreateBeApiUrl)=>string;type OptionsGetBeApiUrl={
|
|
133
|
+
/** * The Suffix origin base api url, e.g:`http://localhost.com/api`, default: `"/"`.
|
|
134
|
+
*
|
|
135
|
+
* @default "/" */
|
|
136
|
+
suffix?:string;};
|
|
137
|
+
/** ---------------------------------------------------
|
|
138
|
+
* * ***Retrieves the base API URL of the backend.***
|
|
139
|
+
* ---------------------------------------------------
|
|
140
|
+
* **This function determines the backend API base URL from the `NEXT_PUBLIC_BACKEND_API_URL` environment variable.**
|
|
141
|
+
* - **Behavior:**
|
|
142
|
+
* - If the variable is not set, it defaults to `"http://localhost:8000"`.
|
|
143
|
+
* - It also allows adding an optional suffix to the returned URL.
|
|
144
|
+
* - ***⚠️ Warning:***
|
|
145
|
+
* - ***This function only support when using ***[`NextJS`](https://nextjs.org/)***.***
|
|
146
|
+
* @description
|
|
147
|
+
* This function determines the backend API base URL from the `NEXT_PUBLIC_BACKEND_API_URL` environment variable.
|
|
148
|
+
* - If `NEXT_PUBLIC_BACKEND_API_URL` is not set, it defaults to `"http://localhost:8000"`.
|
|
149
|
+
* - If `NEXT_PUBLIC_BACKEND_API_URL` does **not** contain a port, it appends one from `NEXT_PUBLIC_PORT_BE` if available.
|
|
150
|
+
* - Supports appending optional suffix (like "/api").
|
|
151
|
+
* @param {OptionsGetBeApiUrl|undefined} options - Configuration options.
|
|
152
|
+
* @param {OptionsGetBeApiUrl["suffix"]} [options.suffix="/"] - The suffix to append to the base API URL.
|
|
153
|
+
* @returns {string} The formatted backend API base URL.
|
|
154
|
+
* @throws {TypeError} If `suffix` is not a `string`.
|
|
155
|
+
* @throws {Error} If `NEXT_PUBLIC_BACKEND_API_URL` is invalid.
|
|
156
|
+
* @example
|
|
157
|
+
* // With NEXT_PUBLIC_BACKEND_API_URL set at `*.env` file
|
|
158
|
+
* NEXT_PUBLIC_BACKEND_API_URL = "https://api.example.com";
|
|
159
|
+
* getBeApiUrl();
|
|
160
|
+
* // ➔ "https://api.example.com/"
|
|
161
|
+
*
|
|
162
|
+
* // With NEXT_PUBLIC_BACKEND_API_URL but no port, using NEXT_PUBLIC_PORT_BE at `*.env` file
|
|
163
|
+
* NEXT_PUBLIC_BACKEND_API_URL = "http://localhost";
|
|
164
|
+
* NEXT_PUBLIC_PORT_BE = "5000";
|
|
165
|
+
* getBeApiUrl({ suffix: "/api" });
|
|
166
|
+
* // ➔ "http://localhost:5000/api"
|
|
167
|
+
*
|
|
168
|
+
* // Without NEXT_PUBLIC_BACKEND_API_URL at `*.env` file (defaults to localhost:8000)
|
|
169
|
+
* delete NEXT_PUBLIC_BACKEND_API_URL;
|
|
170
|
+
* getBeApiUrl({ suffix: "/v1" });
|
|
171
|
+
* // ➔ "http://localhost:8000/v1"
|
|
172
|
+
*/
|
|
173
|
+
declare const getBeApiUrl:({suffix}?:OptionsGetBeApiUrl)=>string;
|
|
174
|
+
/** ---------------------------------------------------
|
|
175
|
+
* * ***Retrieves the base URL of the application.***
|
|
176
|
+
* ---------------------------------------------------
|
|
177
|
+
* **This function is designed to be used within `Next.js` applications.**
|
|
178
|
+
* - **Behavior:**
|
|
179
|
+
* - It determines the base URL from the `NEXT_PUBLIC_BASE_URL` environment variable.
|
|
180
|
+
* - If `NEXT_PUBLIC_BASE_URL` is not set, it defaults to `"http://localhost:3000"`.
|
|
181
|
+
* - If `NEXT_PUBLIC_BASE_URL` does **not** contain a port, it appends one from `NEXT_PUBLIC_PORT_FE` if available.
|
|
182
|
+
* - Ensures the final URL is valid and normalized (no trailing slashes).
|
|
183
|
+
* - ***⚠️ Warning:***
|
|
184
|
+
* - ***This function only support when using ***[`NextJS`](https://nextjs.org/)***.***
|
|
185
|
+
* @returns {string} The resolved base URL of the application.
|
|
186
|
+
* @throws {Error} If the constructed URL is invalid or malformed.
|
|
187
|
+
* @example
|
|
188
|
+
* // With environment variable set at `*.env` file
|
|
189
|
+
* NEXT_PUBLIC_BASE_URL = "https://example.com";
|
|
190
|
+
* getBaseUrl();
|
|
191
|
+
* // ➔ "https://example.com"
|
|
192
|
+
*
|
|
193
|
+
* // With custom port via NEXT_PUBLIC_PORT_FE at `*.env` file
|
|
194
|
+
* NEXT_PUBLIC_BASE_URL = "http://localhost";
|
|
195
|
+
* NEXT_PUBLIC_PORT_FE = "4000";
|
|
196
|
+
* getBaseUrl();
|
|
197
|
+
* // ➔ "http://localhost:4000"
|
|
198
|
+
*
|
|
199
|
+
* // Without environment variable at `*.env` file
|
|
200
|
+
* delete NEXT_PUBLIC_BASE_URL;
|
|
201
|
+
* getBaseUrl();
|
|
202
|
+
* // ➔ "http://localhost:3000"
|
|
203
|
+
*/
|
|
184
204
|
declare const getBaseUrl:()=>string;export{type ExtractRouteParams,type HasDynamicSegments,createBeApiUrl,generateRoute,getBaseUrl,getBeApiUrl};
|
package/dist/next/index.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import {c as c$2,d}from'../chunk-
|
|
1
|
+
import {c as c$2,d}from'../chunk-QDNSC57Z.js';import {c as c$3}from'../chunk-NR44SNYE.js';import {a}from'../chunk-7CHFAMS2.js';import {a as a$1}from'../chunk-NJSAV73S.js';import'../chunk-ELDDNERR.js';import {c}from'../chunk-J4NCYYGS.js';import'../chunk-5SZUSNGZ.js';import {g,E,j,c as c$1,r,p,f,m,G,C}from'../chunk-EWYPSKKM.js';function N(e,t){if(!g(e)||a(e))throw new TypeError(`\u274C 'generateRoute' Failed:
|
|
2
2
|
- Invalid 'route' value.
|
|
3
|
-
- Must be of type \`string\` and non-empty string, but received: "${E(e)}": \`${c(e)}\`.`);if(!/[\\[\]]/.test(e))return e;if(
|
|
3
|
+
- Must be of type \`string\` and non-empty string, but received: "${E(e)}": \`${c(e)}\`.`);if(!/[\\[\]]/.test(e))return e;if(j(t,{message:({validType:o})=>`\u274C 'generateRoute' Failed cause in route "${e}":
|
|
4
4
|
- Missing or invalid parameters \`${o}\` for route: "${e}", must be of type \`${o}\` mapping parameters.`}),c$1(t))throw new TypeError(`\u274C 'generateRoute' Failed cause in route "${e}":
|
|
5
|
-
- Missing parameters \`plain-object\` for route: "${e}".`);let n=["?","&","#","=","/","'",'"',"(",")","+",";","%","@",":"],r$1=[],s=Array.from(e.matchAll(/\[(\w+)\]/g)).map(o=>o[1]);for(let o of s){let i=t[o];if(!
|
|
5
|
+
- Missing parameters \`plain-object\` for route: "${e}".`);let n=["?","&","#","=","/","'",'"',"(",")","+",";","%","@",":"],r$1=[],s=Array.from(e.matchAll(/\[(\w+)\]/g)).map(o=>o[1]);for(let o of s){let i=t[o];if(!g(i)){r$1.push(`- Invalid parameter: "${o}" must be of type \`string\`, but received: \`${E(i)}\`.`);continue}if(a(i)){r$1.push(`- Parameter "${o}" cannot be empty string.`);continue}let p=n.filter(g=>i.includes(g));if(/\s/.test(i)&&p.push("white-space(s)"),p.length>0){let g=p.map(a=>a==="`"?"backtick - (`)":`\`${a}\``);n.includes("white-space(s)")||n.push("white-space(s)");let m=n.map(a=>a==="`"?"backtick - (`)":`\`${a}\``);r$1.push(`- Parameter "${o}" contains invalid characters (${g.join(", ")}). These characters are not allowed because they could cause issues in URL structure. The following characters are forbidden in route parameters: (${m.join(", ")}).`);}}if(r(r$1))throw new Error(`\u274C 'generateRoute' Failed cause in route "${e}":
|
|
6
6
|
${r$1.join(`
|
|
7
|
-
`)}.`);return e.replace(/\[(\w+)\]/g,(o,i)=>(p(t[i])?t[i]:"").trim().replace(/^\/+|\/+$/g,"")).replace(/\/+/g,"/")}var
|
|
8
|
-
export{
|
|
7
|
+
`)}.`);return e.replace(/\[(\w+)\]/g,(o,i)=>(p(t[i])?t[i]:"").trim().replace(/^\/+|\/+$/g,"")).replace(/\/+/g,"/")}var q=(e,t={})=>{try{let p=function(m,a){return `${m.replace(/\/+$/,"")}/${a.replace(/^\/+/,"")}`};var n=p;a$1(c$1(e)?"":e,{message({currentType:m,validType:a}){return `First parameter (\`pathname\`) must be of type \`${a}\`, but received: \`${m}\`.`}}),f(t)||(t={});let{prefix:r="/api",withOrigin:s=!0}=t;if(!m(r)&&!g(r))throw new TypeError(`Parameter \`prefix\` property of the \`options\` (second parameter) must be of type \`string\`, but received: \`${E(r)}\`.`);G(s,{message:({currentType:m,validType:a})=>`Parameter \`withOrigin\` property of the \`options\` (second parameter) must be of type \`${a}\`, but received: \`${m}\`.`}),e=c$2(e),r=c$2(r);let o=r.endsWith("/")?r:r+"/";(e===r||e===r+"/"||e.startsWith(o))&&(e=e.slice(r.length),e=c$2(e));let i=b({suffix:r});return p(s?i:new URL(i).pathname,e).replace(/\/+$/,"")}catch(r){throw C(r)?r:new Error("Failed to generate backend API URL in `createBeApiUrl()`, Error: "+new Error(String(r)).message.trim())}};var b=({suffix:e="/"}={})=>{a$1(e,{message({currentType:t,validType:n}){return `Parameter \`suffix\` property of the first parameter must be of type \`${n}\`, but received: \`${t}\`.`}});try{let t=process.env.NEXT_PUBLIC_BACKEND_API_URL?.trim();if(t){t=c$3(t);let s=new URL(t);!!!s.port&&process.env.NEXT_PUBLIC_PORT_BE&&(t=s.origin+d(process.env.NEXT_PUBLIC_PORT_BE,{prefixColon:!0}));}else t="http://localhost"+d(process.env.NEXT_PUBLIC_PORT_BE||"8000",{prefixColon:!0});e=c$3(e).length?c$3(e):"/";let n=new URL(t.replace(/\/+$/,"")).origin,r=e==="/"?"/":`${e.startsWith("/")?"":"/"}${e.replace(/\/+$/,"")}`;return `${n}${r}`}catch(t){throw new Error("Invalid `NEXT_PUBLIC_BACKEND_API_URL`, failed to generate from `getBeApiUrl()`, Error:"+t)}};var te=()=>{try{let e=process.env.NEXT_PUBLIC_BASE_URL?.trim(),t=process.env.NEXT_PUBLIC_PORT_FE?.trim(),n=e||"http://localhost";n=c$3(n).replace(/\/+$/,"");let r=/:\/\/[^/]+:\d+/.test(n);!r&&t?n+=d(t,{prefixColon:!0}):!r&&!e&&(n+=":3000");let s=new URL(n);return `${s.protocol}//${s.hostname}${s.port?`:${s.port}`:""}`}catch(e){throw new Error("Invalid `NEXT_PUBLIC_BASE_URL`, failed to generate from `getBaseUrl()`, Error:"+e)}};
|
|
8
|
+
export{q as createBeApiUrl,N as generateRoute,te as getBaseUrl,b as getBeApiUrl};
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
'use strict';var
|
|
1
|
+
'use strict';var chunkCII6WWLO_cjs=require('../../chunk-CII6WWLO.cjs');require('server-only');var server=require('next/server');var g=(t,r=true)=>{if(!chunkCII6WWLO_cjs.b(server.NextRequest))throw new Error("Function `getClientIpOrUrl` is designed to be used in a `NextJS` environment.");if(!(t instanceof server.NextRequest))throw new TypeError("First parameter (`request`) must be an `instance of NextRequest` from `NextJS`.");chunkCII6WWLO_cjs.G(r,{message:({currentType:a,validType:d})=>`Second parameter (\`includeFullUrl\`) must be of type \`${d}\`, but received: \`${a}\`.`});let e=(t.headers.get("x-forwarded-for")??"127.0.0.1").trim().split(",");(e[0]==="::ffff:127.0.0.1"||e[0]==="::1")&&(e[0]="127.0.0.1");let o=e.length>1?e[e.length-1].trim():e[0];if(!r)return o;let p=t.headers.get("x-forwarded-proto")||"http",f=t.headers.get("x-forwarded-port")||"3000";return `${process.env.NODE_ENV==="production"?p:"http"}://${o}:${f}`};
|
|
2
2
|
exports.getClientIpOrUrl=g;
|
|
@@ -1,42 +1,28 @@
|
|
|
1
1
|
import{NextRequest}from'next/server';
|
|
2
2
|
/** ---------------------------------
|
|
3
|
-
* * ***
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
* @
|
|
13
|
-
* @
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
*
|
|
30
|
-
* @example
|
|
31
|
-
* // Get full URL
|
|
32
|
-
* const url = getClientIpOrUrl(request);
|
|
33
|
-
* console.log("Client full URL:", url);
|
|
34
|
-
*/
|
|
35
|
-
declare const getClientIpOrUrl:(
|
|
36
|
-
/** * The incoming Next.js request object. */
|
|
37
|
-
request:NextRequest,
|
|
38
|
-
/** * Whether to return the full URL (protocol, IP, and port) or just the IP address.
|
|
39
|
-
*
|
|
40
|
-
* @default true
|
|
41
|
-
*/
|
|
42
|
-
includeFullUrl?:boolean)=>string;export{getClientIpOrUrl};
|
|
3
|
+
* * ***Utility for NextJS Server: `getClientIpOrUrl`.***
|
|
4
|
+
* ---------------------------------
|
|
5
|
+
* **Retrieves the real client IP address and constructs the full URL using headers like `x-forwarded-for`, `x-forwarded-proto`, and `x-forwarded-port`.**
|
|
6
|
+
* - **ℹ️ Note:**
|
|
7
|
+
* - Only supported in **Next.js** environments (specifically in `server-only` contexts).
|
|
8
|
+
* - Should be used in **middleware** or **server actions** that have access to ***[`NextRequest - NextJS`](https://nextjs.org/docs/app/api-reference/functions/next-request)***.
|
|
9
|
+
* @param {NextRequest} request - The incoming ***`NextJS`*** request object.
|
|
10
|
+
* @param {boolean|undefined} [includeFullUrl=true] - Whether to return the full URL (`protocol`, `IP`, and `port` like `protocol://ip:port`) or just the IP address, defaultValue: `true`.
|
|
11
|
+
* @returns {string} The extracted client IP address or the full constructed URL.
|
|
12
|
+
* @throws {Error} If the function is used outside a Next.js server environment.
|
|
13
|
+
* @throws {TypeError} If the arguments do not match the expected types.
|
|
14
|
+
* @example
|
|
15
|
+
* // Basic usage in Next.js middleware
|
|
16
|
+
* import { NextRequest } from "next/server";
|
|
17
|
+
* import { getClientIpOrUrl } from "@rzl-zone/utils-js/next/server";
|
|
18
|
+
*
|
|
19
|
+
* export function middleware(request: NextRequest) {
|
|
20
|
+
* const clientIp = getClientIpOrUrl(request, false);
|
|
21
|
+
* console.log("Client IP:", clientIp);
|
|
22
|
+
* }
|
|
23
|
+
*
|
|
24
|
+
* // Get full URL
|
|
25
|
+
* const url = getClientIpOrUrl(request);
|
|
26
|
+
* console.log("Client full URL:", url);
|
|
27
|
+
*/
|
|
28
|
+
declare const getClientIpOrUrl:(request:NextRequest,includeFullUrl?:boolean)=>string;export{getClientIpOrUrl};
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import {b,G}from'../../chunk-
|
|
1
|
+
import {b,G}from'../../chunk-EWYPSKKM.js';import'server-only';import {NextRequest}from'next/server';var g=(t,r=true)=>{if(!b(NextRequest))throw new Error("Function `getClientIpOrUrl` is designed to be used in a `NextJS` environment.");if(!(t instanceof NextRequest))throw new TypeError("First parameter (`request`) must be an `instance of NextRequest` from `NextJS`.");G(r,{message:({currentType:a,validType:d})=>`Second parameter (\`includeFullUrl\`) must be of type \`${d}\`, but received: \`${a}\`.`});let e=(t.headers.get("x-forwarded-for")??"127.0.0.1").trim().split(",");(e[0]==="::ffff:127.0.0.1"||e[0]==="::1")&&(e[0]="127.0.0.1");let o=e.length>1?e[e.length-1].trim():e[0];if(!r)return o;let p=t.headers.get("x-forwarded-proto")||"http",f=t.headers.get("x-forwarded-port")||"3000";return `${process.env.NODE_ENV==="production"?p:"http"}://${o}:${f}`};
|
|
2
2
|
export{g as getClientIpOrUrl};
|