@nu-art/http-client 0.401.1
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/core/HttpClient.d.ts +92 -0
- package/core/HttpClient.js +157 -0
- package/core/HttpRequest.d.ts +205 -0
- package/core/HttpRequest.js +494 -0
- package/exceptions/HttpException.d.ts +79 -0
- package/exceptions/HttpException.js +99 -0
- package/index.d.ts +8 -0
- package/index.js +17 -0
- package/package.json +62 -0
- package/types/api-types.d.ts +142 -0
- package/types/api-types.js +21 -0
- package/types/error-types.d.ts +46 -0
- package/types/error-types.js +6 -0
- package/types/types.d.ts +26 -0
- package/types/types.js +6 -0
- package/utils/http-codes.d.ts +197 -0
- package/utils/http-codes.js +125 -0
- package/utils/utils.d.ts +42 -0
- package/utils/utils.js +75 -0
package/utils/utils.js
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* @nu-art/thunderstorm-http - A robust and type-safe HTTP client for Thunderstorm applications
|
|
3
|
+
* Copyright (C) 2024 Adam van der Kruk aka TacB0sS
|
|
4
|
+
* Licensed under the Apache License, Version 2.0
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Composes a query string from parameters.
|
|
8
|
+
*
|
|
9
|
+
* Converts an object of parameters into a URL-encoded query string.
|
|
10
|
+
* - Functions are evaluated to get their return value
|
|
11
|
+
* - undefined/null values result in `key=` (empty value)
|
|
12
|
+
* - All values are URI-encoded
|
|
13
|
+
*
|
|
14
|
+
* @param params - Object with parameter keys and values
|
|
15
|
+
* @returns URL-encoded query string (e.g., "key1=value1&key2=value2")
|
|
16
|
+
*/
|
|
17
|
+
export function composeQueryParams(params = {}) {
|
|
18
|
+
return Object.keys(params).map((paramKey) => {
|
|
19
|
+
let paramValue = params[paramKey];
|
|
20
|
+
if (paramValue === undefined || paramValue === null)
|
|
21
|
+
return `${paramKey}=`;
|
|
22
|
+
if (typeof paramValue === 'function')
|
|
23
|
+
paramValue = paramValue();
|
|
24
|
+
return `${paramKey}=${encodeURIComponent(paramValue)}`;
|
|
25
|
+
}).join('&');
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Composes a complete URL with query parameters and optional hash.
|
|
29
|
+
*
|
|
30
|
+
* Combines a base URL with query parameters and/or a hash fragment.
|
|
31
|
+
* Only adds query string if there are parameters. Hash is added if provided.
|
|
32
|
+
*
|
|
33
|
+
* @param url - Base URL
|
|
34
|
+
* @param params - Query parameters object
|
|
35
|
+
* @param hash - Optional hash fragment (will add '#' if not present)
|
|
36
|
+
* @returns Complete URL with query string and/or hash
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* ```typescript
|
|
40
|
+
* composeUrl('/path', {foo: 'bar'}, 'section1')
|
|
41
|
+
* // Returns: '/path?foo=bar#section1'
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
export function composeUrl(url, params = {}, hash = '') {
|
|
45
|
+
const queryAsEncodedString = composeQueryParams(params);
|
|
46
|
+
if (queryAsEncodedString.length)
|
|
47
|
+
return `${url}?${queryAsEncodedString}`;
|
|
48
|
+
if (hash.length)
|
|
49
|
+
return `${url}${hash.startsWith('#') ? hash : `#${hash}`}`;
|
|
50
|
+
return url;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Encodes URL parameters into a StringMap with URI-encoded values.
|
|
54
|
+
*
|
|
55
|
+
* Similar to composeQueryParams but returns an object instead of a query string.
|
|
56
|
+
* Useful when you need the encoded parameters as a map rather than a string.
|
|
57
|
+
*
|
|
58
|
+
* @param params - Route parameters object
|
|
59
|
+
* @returns Object with URI-encoded parameter values
|
|
60
|
+
*/
|
|
61
|
+
export function encodeUrlParams(params = {}) {
|
|
62
|
+
const encodedQueryParams = {};
|
|
63
|
+
Object.keys(params).forEach(paramKey => {
|
|
64
|
+
const paramValue = params[paramKey];
|
|
65
|
+
let finalValue;
|
|
66
|
+
if (paramValue === undefined || paramValue === null)
|
|
67
|
+
finalValue = '';
|
|
68
|
+
else if (typeof paramValue === 'function')
|
|
69
|
+
finalValue = paramValue();
|
|
70
|
+
else
|
|
71
|
+
finalValue = paramValue;
|
|
72
|
+
encodedQueryParams[paramKey] = encodeURIComponent(finalValue);
|
|
73
|
+
});
|
|
74
|
+
return encodedQueryParams;
|
|
75
|
+
}
|