@stryke/http 0.0.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/LICENSE +201 -0
- package/README.md +297 -0
- package/dist/cookie.cjs +187 -0
- package/dist/cookie.d.ts +39 -0
- package/dist/cookie.mjs +1 -0
- package/dist/format-data-uri.cjs +8 -0
- package/dist/format-data-uri.d.ts +8 -0
- package/dist/format-data-uri.mjs +1 -0
- package/dist/index.cjs +38 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.mjs +1 -0
- package/dist/types.cjs +1 -0
- package/dist/types.d.ts +193 -0
- package/dist/types.mjs +0 -0
- package/dist/url-builder.cjs +96 -0
- package/dist/url-builder.d.ts +138 -0
- package/dist/url-builder.mjs +1 -0
- package/package.json +134 -0
package/dist/cookie.d.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { CookieParseOptions, CookieSerializeOptions, SetCookie, SetCookieParseOptions } from "./types";
|
|
2
|
+
/**
|
|
3
|
+
* Parse an HTTP Cookie header string and returning an object of all cookie
|
|
4
|
+
* name-value pairs.
|
|
5
|
+
*
|
|
6
|
+
* @param strCookie - the string representing a `Cookie` header value
|
|
7
|
+
* @param options - object containing parsing options
|
|
8
|
+
* @returns an object with the parsed cookies
|
|
9
|
+
*/
|
|
10
|
+
export declare function parseCookie(strCookie: string, options?: CookieParseOptions): Record<string, string>;
|
|
11
|
+
/**
|
|
12
|
+
* Parse a [Set-Cookie](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie) header string into an object.
|
|
13
|
+
*
|
|
14
|
+
* @param setCookieValue - the string representing a `Set-Cookie` header value
|
|
15
|
+
* @param options - object containing parsing options
|
|
16
|
+
* @returns an object with the parsed cookie
|
|
17
|
+
*/
|
|
18
|
+
export declare function parseSetCookie(setCookieValue: string, options?: SetCookieParseOptions): SetCookie;
|
|
19
|
+
/**
|
|
20
|
+
* Serialize a cookie name-value pair into a `Set-Cookie` header string.
|
|
21
|
+
*
|
|
22
|
+
* @param name - the name for the cookie
|
|
23
|
+
* @param value - value to set the cookie to
|
|
24
|
+
* @param options - object containing serialization options
|
|
25
|
+
* @returns a `Set-Cookie` header string
|
|
26
|
+
*
|
|
27
|
+
* @throws TypeError when `maxAge` options is invalid
|
|
28
|
+
*/
|
|
29
|
+
export declare function serializeCookie(name: string, value: string, options?: CookieSerializeOptions): string;
|
|
30
|
+
/**
|
|
31
|
+
* Set-Cookie header field-values are sometimes comma joined in one string. This splits them without choking on commas
|
|
32
|
+
* that are within a single set-cookie field-value, such as in the Expires portion.
|
|
33
|
+
*
|
|
34
|
+
* See https://tools.ietf.org/html/rfc2616#section-4.2
|
|
35
|
+
*
|
|
36
|
+
* @param strCookie - The string representing a `Set-Cookie` header value
|
|
37
|
+
* @returns An array of strings representing individual `Set-Cookie` header values
|
|
38
|
+
*/
|
|
39
|
+
export declare function splitSetCookieString(strCookie: string | string[]): string[];
|
package/dist/cookie.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{isDate as h,isFunction as d,isSet as g,isString as p}from"@stryke/types/type-checks";export function parseCookie(r,c){if(!p(r))throw new TypeError("argument str must be a string");const i={},e=c??{},o=e.decode??(t=>t.includes("%")?decodeURIComponent(t):t);let a=0;for(;a<r.length;){const t=r.indexOf("=",a);if(t===-1)break;let n=r.indexOf(";",a);if(n===-1)n=r.length;else if(n<t){a=r.lastIndexOf(";",t-1)+1;continue}const s=r.slice(a,t).trim();if(e?.filter&&!e?.filter(s)){a=n+1;continue}if(i[s]===void 0){let l=r.slice(t+1,n).trim();l.codePointAt(0)===34&&(l=l.slice(1,-1));try{i[s]=o(l)}catch{i[s]=l}}a=n+1}return i}export function parseSetCookie(r,c){const i=(r||"").split(";").filter(s=>p(s)&&!!s.trim()),e=i.shift()||"";let o="",a="";const t=e.split("=");t.length>1?(o=t.shift(),a=t.join("=")):a=e;try{a=c?.decode===!1?a:(c?.decode??decodeURIComponent)(a)}catch{}const n={name:o,value:a};for(const s of i){const l=s.split("="),f=(l.shift()||"").trimStart().toLowerCase(),m=l.join("=");switch(f){case"expires":{n.expires=new Date(m);break}case"max-age":{n.maxAge=Number.parseInt(m,10);break}case"secure":{n.secure=!0;break}case"httponly":{n.httpOnly=!0;break}case"samesite":{n.sameSite=m;break}default:n[f]=m}}return n}const u=/^[\u0009\u0020-\u007E\u0080-\u00FF]+$/;export function serializeCookie(r,c,i){const e=i??{},o=e.encode??encodeURIComponent;if(!d(o))throw new TypeError("option encode is invalid");if(!u.test(r))throw new TypeError("argument name is invalid");const a=o(c);if(a&&!u.test(a))throw new TypeError("argument val is invalid");let t=r+"="+a;if(!g(e.maxAge)){const n=Number(e.maxAge);if(Number.isNaN(n)||!Number.isFinite(n))throw new TypeError("option maxAge is invalid");t+="; Max-Age="+Math.floor(n)}if(e.domain){if(!u.test(e.domain))throw new TypeError("option domain is invalid");t+="; Domain="+e.domain}if(e.path){if(!u.test(e.path))throw new TypeError("option path is invalid");t+="; Path="+e.path}if(e.expires){if(!h(e.expires)||Number.isNaN(e.expires.valueOf()))throw new TypeError("option expires is invalid");t+="; Expires="+e.expires.toUTCString()}if(e.httpOnly&&(t+="; HttpOnly"),e.secure&&(t+="; Secure"),e.priority)switch(p(e.priority)?e.priority.toLowerCase():e.priority){case"low":{t+="; Priority=Low";break}case"medium":{t+="; Priority=Medium";break}case"high":{t+="; Priority=High";break}default:throw new TypeError("option priority is invalid")}if(e.sameSite)switch(p(e.sameSite)?e.sameSite.toLowerCase():e.sameSite){case!0:{t+="; SameSite=Strict";break}case"lax":{t+="; SameSite=Lax";break}case"strict":{t+="; SameSite=Strict";break}case"none":{t+="; SameSite=None";break}default:throw new TypeError("option sameSite is invalid")}return e.partitioned&&(t+="; Partitioned"),t}export function splitSetCookieString(r){if(Array.isArray(r))return r.flatMap(f=>splitSetCookieString(f));if(!p(r))return[];const c=[];let i=0,e,o,a,t,n;const s=()=>{for(;i<r.length&&/\s/.test(r.charAt(i));)i+=1;return i<r.length},l=()=>(o=r.charAt(i),o!=="="&&o!==";"&&o!==",");for(;i<r.length;){for(e=i,n=!1;s();)if(o=r.charAt(i),o===","){for(a=i,i+=1,s(),t=i;i<r.length&&l();)i+=1;i<r.length&&r.charAt(i)==="="?(n=!0,i=t,c.push(r.slice(e,a)),e=i):i=a+1}else i+=1;(!n||i>=r.length)&&c.push(r.slice(e))}return c}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const formatDataURI=(t,n)=>`data:${n};utf8,${encodeURIComponent(t)}`;
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
var _cookie = require("./cookie.cjs");
|
|
7
|
+
Object.keys(_cookie).forEach(function (key) {
|
|
8
|
+
if (key === "default" || key === "__esModule") return;
|
|
9
|
+
if (key in exports && exports[key] === _cookie[key]) return;
|
|
10
|
+
Object.defineProperty(exports, key, {
|
|
11
|
+
enumerable: true,
|
|
12
|
+
get: function () {
|
|
13
|
+
return _cookie[key];
|
|
14
|
+
}
|
|
15
|
+
});
|
|
16
|
+
});
|
|
17
|
+
var _formatDataUri = require("./format-data-uri.cjs");
|
|
18
|
+
Object.keys(_formatDataUri).forEach(function (key) {
|
|
19
|
+
if (key === "default" || key === "__esModule") return;
|
|
20
|
+
if (key in exports && exports[key] === _formatDataUri[key]) return;
|
|
21
|
+
Object.defineProperty(exports, key, {
|
|
22
|
+
enumerable: true,
|
|
23
|
+
get: function () {
|
|
24
|
+
return _formatDataUri[key];
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
var _urlBuilder = require("./url-builder.cjs");
|
|
29
|
+
Object.keys(_urlBuilder).forEach(function (key) {
|
|
30
|
+
if (key === "default" || key === "__esModule") return;
|
|
31
|
+
if (key in exports && exports[key] === _urlBuilder[key]) return;
|
|
32
|
+
Object.defineProperty(exports, key, {
|
|
33
|
+
enumerable: true,
|
|
34
|
+
get: function () {
|
|
35
|
+
return _urlBuilder[key];
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
});
|
package/dist/index.d.ts
ADDED
package/dist/index.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export*from"./cookie";export*from"./format-data-uri";export*from"./url-builder";
|
package/dist/types.cjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
import type { ParsedURL } from "ufo";
|
|
2
|
+
export type StormURL = ParsedURL & {
|
|
3
|
+
__typename: "StormURL";
|
|
4
|
+
query: Record<string, any>;
|
|
5
|
+
username?: string;
|
|
6
|
+
password?: string;
|
|
7
|
+
hostname?: string;
|
|
8
|
+
port?: string;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Additional serialization options
|
|
12
|
+
*/
|
|
13
|
+
export interface CookieSerializeOptions {
|
|
14
|
+
/**
|
|
15
|
+
* Specifies the value for the {@link https://tools.ietf.org/html/rfc6265#section-5.2.3 | Domain Set-Cookie attribute}. By default, no
|
|
16
|
+
* domain is set, and most clients will consider the cookie to apply to only
|
|
17
|
+
* the current domain.
|
|
18
|
+
*/
|
|
19
|
+
domain?: string | undefined;
|
|
20
|
+
/**
|
|
21
|
+
* Specifies a function that will be used to encode a cookie's value. Since
|
|
22
|
+
* value of a cookie has a limited character set (and must be a simple
|
|
23
|
+
* string), this function can be used to encode a value into a string suited
|
|
24
|
+
* for a cookie's value.
|
|
25
|
+
*
|
|
26
|
+
* The default function is the global `encodeURIComponent`, which will
|
|
27
|
+
* encode a JavaScript string into UTF-8 byte sequences and then URL-encode
|
|
28
|
+
* any that fall outside of the cookie range.
|
|
29
|
+
*/
|
|
30
|
+
encode?(value: string): string;
|
|
31
|
+
/**
|
|
32
|
+
* Specifies the `Date` object to be the value for the {@link https://tools.ietf.org/html/rfc6265#section-5.2.1 | `Expires` `Set-Cookie` attribute}. By default,
|
|
33
|
+
* no expiration is set, and most clients will consider this a "non-persistent cookie" and will delete
|
|
34
|
+
* it on a condition like exiting a web browser application.
|
|
35
|
+
*
|
|
36
|
+
* *Note* the {@link https://tools.ietf.org/html/rfc6265#section-5.3 | cookie storage model specification}
|
|
37
|
+
* states that if both `expires` and `maxAge` are set, then `maxAge` takes precedence, but it is
|
|
38
|
+
* possible not all clients by obey this, so if both are set, they should
|
|
39
|
+
* point to the same date and time.
|
|
40
|
+
*/
|
|
41
|
+
expires?: Date | undefined;
|
|
42
|
+
/**
|
|
43
|
+
* Specifies the boolean value for the {@link https://tools.ietf.org/html/rfc6265#section-5.2.6 | `HttpOnly` `Set-Cookie` attribute}.
|
|
44
|
+
* When truthy, the `HttpOnly` attribute is set, otherwise it is not. By
|
|
45
|
+
* default, the `HttpOnly` attribute is not set.
|
|
46
|
+
*
|
|
47
|
+
* *Note* be careful when setting this to true, as compliant clients will
|
|
48
|
+
* not allow client-side JavaScript to see the cookie in `document.cookie`.
|
|
49
|
+
*/
|
|
50
|
+
httpOnly?: boolean | undefined;
|
|
51
|
+
/**
|
|
52
|
+
* Specifies the number (in seconds) to be the value for the `Max-Age`
|
|
53
|
+
* `Set-Cookie` attribute. The given number will be converted to an integer
|
|
54
|
+
* by rounding down. By default, no maximum age is set.
|
|
55
|
+
*
|
|
56
|
+
* *Note* the {@link https://tools.ietf.org/html/rfc6265#section-5.3 | cookie storage model specification}
|
|
57
|
+
* states that if both `expires` and `maxAge` are set, then `maxAge` takes precedence, but it is
|
|
58
|
+
* possible not all clients by obey this, so if both are set, they should
|
|
59
|
+
* point to the same date and time.
|
|
60
|
+
*/
|
|
61
|
+
maxAge?: number | undefined;
|
|
62
|
+
/**
|
|
63
|
+
* Specifies the value for the {@link https://tools.ietf.org/html/rfc6265#section-5.2.4|`Path` `Set-Cookie` attribute}.
|
|
64
|
+
* By default, the path is considered the "default path".
|
|
65
|
+
*/
|
|
66
|
+
path?: string | undefined;
|
|
67
|
+
/**
|
|
68
|
+
* Specifies the `string` to be the value for the [`Priority` `Set-Cookie` attribute][rfc-west-cookie-priority-00-4.1].
|
|
69
|
+
*
|
|
70
|
+
* - `'low'` will set the `Priority` attribute to `Low`.
|
|
71
|
+
* - `'medium'` will set the `Priority` attribute to `Medium`, the default priority when not set.
|
|
72
|
+
* - `'high'` will set the `Priority` attribute to `High`.
|
|
73
|
+
*
|
|
74
|
+
* More information about the different priority levels can be found in
|
|
75
|
+
* [the specification][rfc-west-cookie-priority-00-4.1].
|
|
76
|
+
*
|
|
77
|
+
* **note** This is an attribute that has not yet been fully standardized, and may change in the future.
|
|
78
|
+
* This also means many clients may ignore this attribute until they understand it.
|
|
79
|
+
*/
|
|
80
|
+
priority?: "low" | "medium" | "high" | undefined;
|
|
81
|
+
/**
|
|
82
|
+
* Specifies the boolean or string to be the value for the {@link https://tools.ietf.org/html/draft-ietf-httpbis-rfc6265bis-03#section-4.1.2.7|`SameSite` `Set-Cookie` attribute}.
|
|
83
|
+
*
|
|
84
|
+
* - `true` will set the `SameSite` attribute to `Strict` for strict same
|
|
85
|
+
* site enforcement.
|
|
86
|
+
* - `false` will not set the `SameSite` attribute.
|
|
87
|
+
* - `'lax'` will set the `SameSite` attribute to Lax for lax same site
|
|
88
|
+
* enforcement.
|
|
89
|
+
* - `'strict'` will set the `SameSite` attribute to Strict for strict same
|
|
90
|
+
* site enforcement.
|
|
91
|
+
* - `'none'` will set the SameSite attribute to None for an explicit
|
|
92
|
+
* cross-site cookie.
|
|
93
|
+
*
|
|
94
|
+
* More information about the different enforcement levels can be found in {@link https://tools.ietf.org/html/draft-ietf-httpbis-rfc6265bis-03#section-4.1.2.7|the specification}.
|
|
95
|
+
*
|
|
96
|
+
* *note* This is an attribute that has not yet been fully standardized, and may change in the future. This also means many clients may ignore this attribute until they understand it.
|
|
97
|
+
*/
|
|
98
|
+
sameSite?: true | false | "lax" | "strict" | "none" | undefined;
|
|
99
|
+
/**
|
|
100
|
+
* Specifies the boolean value for the {@link https://tools.ietf.org/html/rfc6265#section-5.2.5 | `Secure` `Set-Cookie` attribute}. When truthy, the
|
|
101
|
+
* `Secure` attribute is set, otherwise it is not. By default, the `Secure` attribute is not set.
|
|
102
|
+
*
|
|
103
|
+
* *Note* be careful when setting this to `true`, as compliant clients will
|
|
104
|
+
* not send the cookie back to the server in the future if the browser does
|
|
105
|
+
* not have an HTTPS connection.
|
|
106
|
+
*/
|
|
107
|
+
secure?: boolean | undefined;
|
|
108
|
+
/**
|
|
109
|
+
* Specifies the `boolean` value for the [`Partitioned` `Set-Cookie`](https://datatracker.ietf.org/doc/html/draft-cutler-httpbis-partitioned-cookies#section-2.1)
|
|
110
|
+
* attribute. When truthy, the `Partitioned` attribute is set, otherwise it is not. By default, the
|
|
111
|
+
* `Partitioned` attribute is not set.
|
|
112
|
+
*
|
|
113
|
+
* **note** This is an attribute that has not yet been fully standardized, and may change in the future.
|
|
114
|
+
* This also means many clients may ignore this attribute until they understand it.
|
|
115
|
+
*
|
|
116
|
+
* More information can be found in the [proposal](https://github.com/privacycg/CHIPS).
|
|
117
|
+
*/
|
|
118
|
+
partitioned?: boolean;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Additional parsing options
|
|
122
|
+
*/
|
|
123
|
+
export interface CookieParseOptions {
|
|
124
|
+
/**
|
|
125
|
+
* Specifies a function that will be used to decode a cookie's value. Since
|
|
126
|
+
* the value of a cookie has a limited character set (and must be a simple
|
|
127
|
+
* string), this function can be used to decode a previously-encoded cookie
|
|
128
|
+
* value into a JavaScript string or other object.
|
|
129
|
+
*
|
|
130
|
+
* The default function is the global `decodeURIComponent`, which will decode
|
|
131
|
+
* any URL-encoded sequences into their byte representations.
|
|
132
|
+
*
|
|
133
|
+
* *Note* if an error is thrown from this function, the original, non-decoded
|
|
134
|
+
* cookie value will be returned as the cookie's value.
|
|
135
|
+
*/
|
|
136
|
+
decode?(value: string): string;
|
|
137
|
+
/**
|
|
138
|
+
* Custom function to filter parsing specific keys.
|
|
139
|
+
*/
|
|
140
|
+
filter?(key: string): boolean;
|
|
141
|
+
}
|
|
142
|
+
export interface SetCookieParseOptions {
|
|
143
|
+
/**
|
|
144
|
+
* Custom decode function to use on cookie values.
|
|
145
|
+
*
|
|
146
|
+
* By default, `decodeURIComponent` is used.
|
|
147
|
+
*
|
|
148
|
+
* **Note:** If decoding fails, the original (undecoded) value will be used
|
|
149
|
+
*/
|
|
150
|
+
decode?: false | ((value: string) => string);
|
|
151
|
+
}
|
|
152
|
+
export interface SetCookie {
|
|
153
|
+
/**
|
|
154
|
+
* Cookie name
|
|
155
|
+
*/
|
|
156
|
+
name: string;
|
|
157
|
+
/**
|
|
158
|
+
* Cookie value
|
|
159
|
+
*/
|
|
160
|
+
value: string;
|
|
161
|
+
/**
|
|
162
|
+
* Cookie path
|
|
163
|
+
*/
|
|
164
|
+
path?: string | undefined;
|
|
165
|
+
/**
|
|
166
|
+
* Absolute expiration date for the cookie
|
|
167
|
+
*/
|
|
168
|
+
expires?: Date | undefined;
|
|
169
|
+
/**
|
|
170
|
+
* Relative max age of the cookie in seconds from when the client receives it (integer or undefined)
|
|
171
|
+
*
|
|
172
|
+
* Note: when using with express's res.cookie() method, multiply maxAge by 1000 to convert to milliseconds
|
|
173
|
+
*/
|
|
174
|
+
maxAge?: number | undefined;
|
|
175
|
+
/**
|
|
176
|
+
* Domain for the cookie,
|
|
177
|
+
* May begin with "." to indicate the named domain or any subdomain of it
|
|
178
|
+
*/
|
|
179
|
+
domain?: string | undefined;
|
|
180
|
+
/**
|
|
181
|
+
* Indicates that this cookie should only be sent over HTTPs
|
|
182
|
+
*/
|
|
183
|
+
secure?: boolean | undefined;
|
|
184
|
+
/**
|
|
185
|
+
* Indicates that this cookie should not be accessible to client-side JavaScript
|
|
186
|
+
*/
|
|
187
|
+
httpOnly?: boolean | undefined;
|
|
188
|
+
/**
|
|
189
|
+
* Indicates a cookie ought not to be sent along with cross-site requests
|
|
190
|
+
*/
|
|
191
|
+
sameSite?: true | false | "lax" | "strict" | "none" | undefined;
|
|
192
|
+
[key: string]: unknown;
|
|
193
|
+
}
|
package/dist/types.mjs
ADDED
|
File without changes
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.StormURLBuilder = void 0;
|
|
7
|
+
var _stormJson = require("@stryke/json/storm-json");
|
|
8
|
+
var _isString = require("@stryke/types/type-checks/is-string");
|
|
9
|
+
var _ufo = require("ufo");
|
|
10
|
+
class StormURLBuilder {
|
|
11
|
+
#t;
|
|
12
|
+
static create(t, r) {
|
|
13
|
+
return new StormURLBuilder(t, r);
|
|
14
|
+
}
|
|
15
|
+
constructor(t, r) {
|
|
16
|
+
const s = r?.decode ?? !0,
|
|
17
|
+
o = (0, _isString.isString)(t) ? s ? (0, _ufo.parseURL)((0, _ufo.decode)(t)) : (0, _ufo.parseURL)(t) : t;
|
|
18
|
+
this.#t = {
|
|
19
|
+
__typename: "StormURL",
|
|
20
|
+
query: {},
|
|
21
|
+
...o
|
|
22
|
+
}, this.#t.host && this.withHost(this.#t.host), this.#t.auth && this.withAuth(this.#t.auth);
|
|
23
|
+
}
|
|
24
|
+
get _url() {
|
|
25
|
+
return this.#t;
|
|
26
|
+
}
|
|
27
|
+
withProtocol(t) {
|
|
28
|
+
return this.#t.protocol = t, this;
|
|
29
|
+
}
|
|
30
|
+
withHostname(t) {
|
|
31
|
+
return this.#t.hostname = t, this;
|
|
32
|
+
}
|
|
33
|
+
withPort(t) {
|
|
34
|
+
return this.#t.port = String(t), this;
|
|
35
|
+
}
|
|
36
|
+
withUsername(t) {
|
|
37
|
+
return this.#t.username = t, this;
|
|
38
|
+
}
|
|
39
|
+
withPassword(t) {
|
|
40
|
+
return this.#t.password = t, this;
|
|
41
|
+
}
|
|
42
|
+
withHost(t) {
|
|
43
|
+
if ((0, _isString.isString)(t)) {
|
|
44
|
+
this.#t.host = t;
|
|
45
|
+
const r = (0, _ufo.parseAuth)(t);
|
|
46
|
+
this.#t.username = r.username, this.#t.password = r.password;
|
|
47
|
+
} else this.#t.hostname = t.hostname, this.#t.port = t.port, this.#t.auth = `${t.hostname}${t.port ? `:${t.port}` : ""}`;
|
|
48
|
+
return this;
|
|
49
|
+
}
|
|
50
|
+
withPath(t) {
|
|
51
|
+
const r = (0, _ufo.parsePath)(t);
|
|
52
|
+
return this.#t = {
|
|
53
|
+
...this.#t,
|
|
54
|
+
...r
|
|
55
|
+
}, this;
|
|
56
|
+
}
|
|
57
|
+
withHash(t) {
|
|
58
|
+
return this.#t.hash = t, this;
|
|
59
|
+
}
|
|
60
|
+
withAuth(t) {
|
|
61
|
+
if ((0, _isString.isString)(t)) {
|
|
62
|
+
this.#t.auth = t;
|
|
63
|
+
const r = (0, _ufo.parseAuth)(t);
|
|
64
|
+
this.#t.username = r.username, this.#t.password = r.password;
|
|
65
|
+
} else this.#t.username = t.username, this.#t.password = t.password, this.#t.auth = `${t.username}:${t.password}`;
|
|
66
|
+
return this;
|
|
67
|
+
}
|
|
68
|
+
withQuery(t) {
|
|
69
|
+
return this.#t.query = {}, this.addQueryParam(t), this;
|
|
70
|
+
}
|
|
71
|
+
addQueryParam(t) {
|
|
72
|
+
if ((0, _isString.isString)(t)) {
|
|
73
|
+
const r = (0, _ufo.parseQuery)(t);
|
|
74
|
+
for (const s in Object.entries(r)) s[0] && (this.#t.query[s[0]] = this.parseQueryParamValue(s[1]));
|
|
75
|
+
} else if (Array.isArray(t) && t.length === 2) this.#t.query[t[0]] = this.parseQueryParamValue(t[1]);else for (const r in Object.entries(t)) r[0] && (this.#t.query[r[0]] = this.parseQueryParamValue(r[1]));
|
|
76
|
+
return this;
|
|
77
|
+
}
|
|
78
|
+
build() {
|
|
79
|
+
return this.#t;
|
|
80
|
+
}
|
|
81
|
+
toString() {
|
|
82
|
+
return (0, _ufo.cleanDoubleSlashes)((0, _ufo.stringifyParsedURL)(this.#t));
|
|
83
|
+
}
|
|
84
|
+
toEncoded() {
|
|
85
|
+
return (0, _ufo.encode)(this.toString());
|
|
86
|
+
}
|
|
87
|
+
parseQueryParamValue(t) {
|
|
88
|
+
if (Array.isArray(t)) {
|
|
89
|
+
const r = [];
|
|
90
|
+
for (const s in t) r.push(this.parseQueryParamValue(s));
|
|
91
|
+
return r;
|
|
92
|
+
}
|
|
93
|
+
return _stormJson.StormJSON.parse(t);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
exports.StormURLBuilder = StormURLBuilder;
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { type ParsedAuth, type ParsedHost } from "ufo";
|
|
2
|
+
import type { StormURL } from "./types";
|
|
3
|
+
export type StormURLBuilderOptions = {
|
|
4
|
+
/**
|
|
5
|
+
* Should the URL be decoded
|
|
6
|
+
*
|
|
7
|
+
* @defaultValue `true`
|
|
8
|
+
*/
|
|
9
|
+
decode: boolean;
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* A class used to build URLs
|
|
13
|
+
*
|
|
14
|
+
* @remarks
|
|
15
|
+
* This class is used to build URLs with a fluent API.
|
|
16
|
+
*
|
|
17
|
+
* The [UFO](https://github.com/unjs/ufo) library is used under the hood to parse and stringify URLs.
|
|
18
|
+
*
|
|
19
|
+
* @class StormURLBuilder
|
|
20
|
+
*/
|
|
21
|
+
export declare class StormURLBuilder {
|
|
22
|
+
#private;
|
|
23
|
+
/**
|
|
24
|
+
* Create a new URL builder
|
|
25
|
+
*
|
|
26
|
+
* @param url - The URL to build
|
|
27
|
+
* @param options - The options for the URL builder
|
|
28
|
+
* @returns The URL builder
|
|
29
|
+
*/
|
|
30
|
+
static create(url: string | StormURL, options?: StormURLBuilderOptions): StormURLBuilder;
|
|
31
|
+
/**
|
|
32
|
+
* Create a new URL builder
|
|
33
|
+
*/
|
|
34
|
+
protected constructor(url: string | StormURL, options?: StormURLBuilderOptions);
|
|
35
|
+
get _url(): StormURL;
|
|
36
|
+
/**
|
|
37
|
+
* Set the protocol of the URL
|
|
38
|
+
*
|
|
39
|
+
* @param protocol - The protocol to set
|
|
40
|
+
* @returns The URL builder
|
|
41
|
+
*/
|
|
42
|
+
withProtocol(protocol: string): StormURLBuilder;
|
|
43
|
+
/**
|
|
44
|
+
* Set the hostname of the URL
|
|
45
|
+
*
|
|
46
|
+
* @param hostname - The hostname to set
|
|
47
|
+
* @returns The URL builder
|
|
48
|
+
*/
|
|
49
|
+
withHostname(hostname: string): StormURLBuilder;
|
|
50
|
+
/**
|
|
51
|
+
* Set the port of the URL
|
|
52
|
+
*
|
|
53
|
+
* @param port - The port to set
|
|
54
|
+
* @returns The URL builder
|
|
55
|
+
*/
|
|
56
|
+
withPort(port: number): StormURLBuilder;
|
|
57
|
+
/**
|
|
58
|
+
* Set the username of the URL
|
|
59
|
+
*
|
|
60
|
+
* @param username - The username to set
|
|
61
|
+
* @returns The URL builder
|
|
62
|
+
*/
|
|
63
|
+
withUsername(username: string): StormURLBuilder;
|
|
64
|
+
/**
|
|
65
|
+
* Set the password of the URL
|
|
66
|
+
*
|
|
67
|
+
* @param password - The password to set
|
|
68
|
+
* @returns The URL builder
|
|
69
|
+
*/
|
|
70
|
+
withPassword(password: string): StormURLBuilder;
|
|
71
|
+
/**
|
|
72
|
+
* Set the pathname of the URL
|
|
73
|
+
*
|
|
74
|
+
* @param pathname - The pathname to set
|
|
75
|
+
* @returns The URL builder
|
|
76
|
+
*/
|
|
77
|
+
withHost(host: string | ParsedHost): StormURLBuilder;
|
|
78
|
+
/**
|
|
79
|
+
* Set the path of the URL
|
|
80
|
+
*
|
|
81
|
+
* @param path - The path to set
|
|
82
|
+
* @returns The URL builder
|
|
83
|
+
*/
|
|
84
|
+
withPath(path: string): StormURLBuilder;
|
|
85
|
+
/**
|
|
86
|
+
* Set the hash of the URL
|
|
87
|
+
*
|
|
88
|
+
* @param hash - The hash to set
|
|
89
|
+
* @returns The URL builder
|
|
90
|
+
*/
|
|
91
|
+
withHash(hash: string): StormURLBuilder;
|
|
92
|
+
/**
|
|
93
|
+
* Set the auth of the URL
|
|
94
|
+
*
|
|
95
|
+
* @param auth - The auth to set
|
|
96
|
+
* @returns The URL builder
|
|
97
|
+
*/
|
|
98
|
+
withAuth(auth: string | ParsedAuth): StormURLBuilder;
|
|
99
|
+
/**
|
|
100
|
+
* Set the query of the URL
|
|
101
|
+
*
|
|
102
|
+
* @param query - The query to set
|
|
103
|
+
* @returns The URL builder
|
|
104
|
+
*/
|
|
105
|
+
withQuery(query: string | [string, any] | Record<string, any>): StormURLBuilder;
|
|
106
|
+
/**
|
|
107
|
+
* Add a query parameter to the URL
|
|
108
|
+
*
|
|
109
|
+
* @param query - The query parameter to add
|
|
110
|
+
* @returns The URL builder
|
|
111
|
+
*/
|
|
112
|
+
addQueryParam(query: string | [string, any] | Record<string, any>): StormURLBuilder;
|
|
113
|
+
/**
|
|
114
|
+
* Returns the built URL
|
|
115
|
+
*
|
|
116
|
+
* @returns The built URL
|
|
117
|
+
*/
|
|
118
|
+
build(): StormURL;
|
|
119
|
+
/**
|
|
120
|
+
* Returns the string representation of the URL
|
|
121
|
+
*
|
|
122
|
+
* @returns The string representation of the URL
|
|
123
|
+
*/
|
|
124
|
+
toString(): string;
|
|
125
|
+
/**
|
|
126
|
+
* Returns the encoded string representation of the URL
|
|
127
|
+
*
|
|
128
|
+
* @returns The encoded string representation of the URL
|
|
129
|
+
*/
|
|
130
|
+
toEncoded(): string;
|
|
131
|
+
/**
|
|
132
|
+
* Parse a query parameter value
|
|
133
|
+
*
|
|
134
|
+
* @param value - The value to parse
|
|
135
|
+
* @returns The parsed value
|
|
136
|
+
*/
|
|
137
|
+
private parseQueryParamValue;
|
|
138
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{StormJSON as a}from"@stryke/json/storm-json";import{isString as i}from"@stryke/types/type-checks/is-string";import{cleanDoubleSlashes as u,decode as h,encode as p,parseAuth as e,parsePath as d,parseQuery as m,parseURL as n,stringifyParsedURL as c}from"ufo";export class StormURLBuilder{#t;static create(t,r){return new StormURLBuilder(t,r)}constructor(t,r){const s=r?.decode??!0,o=i(t)?s?n(h(t)):n(t):t;this.#t={__typename:"StormURL",query:{},...o},this.#t.host&&this.withHost(this.#t.host),this.#t.auth&&this.withAuth(this.#t.auth)}get _url(){return this.#t}withProtocol(t){return this.#t.protocol=t,this}withHostname(t){return this.#t.hostname=t,this}withPort(t){return this.#t.port=String(t),this}withUsername(t){return this.#t.username=t,this}withPassword(t){return this.#t.password=t,this}withHost(t){if(i(t)){this.#t.host=t;const r=e(t);this.#t.username=r.username,this.#t.password=r.password}else this.#t.hostname=t.hostname,this.#t.port=t.port,this.#t.auth=`${t.hostname}${t.port?`:${t.port}`:""}`;return this}withPath(t){const r=d(t);return this.#t={...this.#t,...r},this}withHash(t){return this.#t.hash=t,this}withAuth(t){if(i(t)){this.#t.auth=t;const r=e(t);this.#t.username=r.username,this.#t.password=r.password}else this.#t.username=t.username,this.#t.password=t.password,this.#t.auth=`${t.username}:${t.password}`;return this}withQuery(t){return this.#t.query={},this.addQueryParam(t),this}addQueryParam(t){if(i(t)){const r=m(t);for(const s in Object.entries(r))s[0]&&(this.#t.query[s[0]]=this.parseQueryParamValue(s[1]))}else if(Array.isArray(t)&&t.length===2)this.#t.query[t[0]]=this.parseQueryParamValue(t[1]);else for(const r in Object.entries(t))r[0]&&(this.#t.query[r[0]]=this.parseQueryParamValue(r[1]));return this}build(){return this.#t}toString(){return u(c(this.#t))}toEncoded(){return p(this.toString())}parseQueryParamValue(t){if(Array.isArray(t)){const r=[];for(const s in t)r.push(this.parseQueryParamValue(s));return r}return a.parse(t)}}
|