| 
									
										
										
										
											2022-07-27 00:40:18 +02:00
										 |  |  | import * as plugins from './smarturl.plugins.js'; | 
					
						
							| 
									
										
										
										
											2021-04-12 19:18:36 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-02 16:32:24 +00:00
										 |  |  | export interface IUrlObject { | 
					
						
							| 
									
										
										
										
											2021-04-12 20:09:19 +00:00
										 |  |  |   href: string; | 
					
						
							|  |  |  |   origin: string; | 
					
						
							|  |  |  |   protocol: string; | 
					
						
							|  |  |  |   username: string; | 
					
						
							|  |  |  |   password: string; | 
					
						
							|  |  |  |   host: string; | 
					
						
							|  |  |  |   hostname: string; | 
					
						
							|  |  |  |   port: string; | 
					
						
							| 
									
										
										
										
											2021-05-02 16:32:24 +00:00
										 |  |  |   path: string; | 
					
						
							| 
									
										
										
										
											2021-04-12 20:09:19 +00:00
										 |  |  |   pathname: string; | 
					
						
							|  |  |  |   search: string; | 
					
						
							| 
									
										
										
										
											2021-05-02 16:32:24 +00:00
										 |  |  |   searchParams: ISearchParams; | 
					
						
							| 
									
										
										
										
											2021-04-12 20:09:19 +00:00
										 |  |  |   hash: string; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-02 16:32:24 +00:00
										 |  |  | export interface ISearchParams { | 
					
						
							|  |  |  |   [key: string]: string; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export class Smarturl implements IUrlObject { | 
					
						
							|  |  |  |   public static createFromUrl( | 
					
						
							|  |  |  |     urlArg: string, | 
					
						
							|  |  |  |     optionsArg?: { | 
					
						
							|  |  |  |       searchParams?: ISearchParams; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   ): Smarturl { | 
					
						
							| 
									
										
										
										
											2022-08-06 22:17:41 +02:00
										 |  |  |     const parsedUrlInstance = new URL(urlArg); | 
					
						
							| 
									
										
										
										
											2021-05-02 16:32:24 +00:00
										 |  |  |     const searchParams: ISearchParams = {}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // enrichment
 | 
					
						
							| 
									
										
										
										
											2022-07-28 15:10:34 +02:00
										 |  |  |     const searchParamPairs: { | 
					
						
							|  |  |  |       key: string; | 
					
						
							|  |  |  |       value: string; | 
					
						
							| 
									
										
										
										
											2024-10-03 12:49:53 +02:00
										 |  |  |     }[] = []; | 
					
						
							| 
									
										
										
										
											2022-07-28 15:10:34 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (parsedUrlInstance.search) { | 
					
						
							|  |  |  |       parsedUrlInstance.search | 
					
						
							| 
									
										
										
										
											2024-10-03 12:49:53 +02:00
										 |  |  |         .replace('?', '') | 
					
						
							|  |  |  |         .split('&') | 
					
						
							|  |  |  |         .map((searchParamPair) => { | 
					
						
							|  |  |  |           searchParamPairs.push({ | 
					
						
							|  |  |  |             key: searchParamPair.split('=')[0], | 
					
						
							|  |  |  |             value: searchParamPair.split('=')[1], | 
					
						
							|  |  |  |           }); | 
					
						
							| 
									
										
										
										
											2022-07-28 15:10:34 +02:00
										 |  |  |         }); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-07-27 01:06:16 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     for (const searchParamPair of searchParamPairs) { | 
					
						
							|  |  |  |       searchParams[searchParamPair.key] = searchParamPair.value; | 
					
						
							| 
									
										
										
										
											2021-05-02 16:32:24 +00:00
										 |  |  |     } | 
					
						
							|  |  |  |     if (optionsArg?.searchParams) { | 
					
						
							|  |  |  |       for (const key of Object.keys(optionsArg.searchParams)) { | 
					
						
							|  |  |  |         searchParams[key] = optionsArg.searchParams[key]; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-28 15:10:34 +02:00
										 |  |  |     let path = parsedUrlInstance.pathname || ''; | 
					
						
							| 
									
										
										
										
											2021-05-02 16:32:24 +00:00
										 |  |  |     if (Object.keys(searchParams).length > 0) { | 
					
						
							|  |  |  |       path += '?'; | 
					
						
							|  |  |  |       let first = true; | 
					
						
							| 
									
										
										
										
											2022-07-27 00:40:18 +02:00
										 |  |  |       for (const key of Object.keys(searchParams)) { | 
					
						
							| 
									
										
										
										
											2021-05-02 16:32:24 +00:00
										 |  |  |         if (first) { | 
					
						
							|  |  |  |           first = false; | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |           path += '&'; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         path += `${key}=${searchParams[key]}`; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-12 20:09:19 +00:00
										 |  |  |     const parsedUrl: IUrlObject = { | 
					
						
							| 
									
										
										
										
											2022-08-06 22:28:16 +02:00
										 |  |  |       ...parsedUrlInstance, | 
					
						
							| 
									
										
										
										
											2021-04-12 20:09:19 +00:00
										 |  |  |       href: parsedUrlInstance.href, | 
					
						
							| 
									
										
										
										
											2022-08-06 22:17:41 +02:00
										 |  |  |       origin: parsedUrlInstance.origin, | 
					
						
							| 
									
										
										
										
											2021-04-12 20:09:19 +00:00
										 |  |  |       protocol: parsedUrlInstance.protocol, | 
					
						
							| 
									
										
										
										
											2022-08-06 22:17:41 +02:00
										 |  |  |       username: parsedUrlInstance.username, | 
					
						
							|  |  |  |       password: parsedUrlInstance.password, | 
					
						
							|  |  |  |       host: parsedUrlInstance.host, | 
					
						
							|  |  |  |       hostname: parsedUrlInstance.hostname, | 
					
						
							| 
									
										
										
										
											2021-04-12 20:09:19 +00:00
										 |  |  |       port: parsedUrlInstance.port, | 
					
						
							| 
									
										
										
										
											2021-05-02 16:32:24 +00:00
										 |  |  |       path, | 
					
						
							| 
									
										
										
										
											2021-04-12 20:09:19 +00:00
										 |  |  |       pathname: parsedUrlInstance.pathname, | 
					
						
							|  |  |  |       search: parsedUrlInstance.search, | 
					
						
							| 
									
										
										
										
											2021-05-02 16:32:24 +00:00
										 |  |  |       searchParams, | 
					
						
							| 
									
										
										
										
											2021-04-12 20:09:19 +00:00
										 |  |  |       hash: parsedUrlInstance.hash, | 
					
						
							|  |  |  |     }; | 
					
						
							| 
									
										
										
										
											2021-05-02 16:32:24 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-12 20:09:19 +00:00
										 |  |  |     if (!parsedUrl.port && parsedUrl.protocol === 'https:') { | 
					
						
							| 
									
										
										
										
											2021-04-13 08:10:09 +00:00
										 |  |  |       // console.log(`inferring port 443 for "https:"`);
 | 
					
						
							| 
									
										
										
										
											2021-04-12 20:09:19 +00:00
										 |  |  |       parsedUrl.port = '443'; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-04-12 20:09:53 +00:00
										 |  |  |     if (!parsedUrl.port && parsedUrl.protocol === 'http:') { | 
					
						
							| 
									
										
										
										
											2021-04-13 08:10:09 +00:00
										 |  |  |       // console.log(`inferring port 80 for "http:"`);
 | 
					
						
							| 
									
										
										
										
											2021-04-12 20:09:53 +00:00
										 |  |  |       parsedUrl.port = '80'; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-05-02 16:32:24 +00:00
										 |  |  |     const returnSmarturl = new Smarturl(); | 
					
						
							|  |  |  |     Object.assign(returnSmarturl, parsedUrl); | 
					
						
							|  |  |  |     return returnSmarturl; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2024-10-03 12:49:53 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-02 16:33:50 +00:00
										 |  |  |   public static createFromParsedUrl(parsedUrlArg: IUrlObject) { | 
					
						
							|  |  |  |     const returnSmarturl = new Smarturl(); | 
					
						
							|  |  |  |     Object.assign(returnSmarturl, parsedUrlArg); | 
					
						
							|  |  |  |     return returnSmarturl; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2021-05-02 16:32:24 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // INSTANCE
 | 
					
						
							|  |  |  |   href: string; | 
					
						
							|  |  |  |   origin: string; | 
					
						
							|  |  |  |   protocol: string; | 
					
						
							|  |  |  |   username: string; | 
					
						
							|  |  |  |   password: string; | 
					
						
							|  |  |  |   host: string; | 
					
						
							|  |  |  |   hostname: string; | 
					
						
							|  |  |  |   port: string; | 
					
						
							|  |  |  |   path: string; | 
					
						
							|  |  |  |   pathname: string; | 
					
						
							|  |  |  |   search: string; | 
					
						
							|  |  |  |   searchParams: ISearchParams; | 
					
						
							|  |  |  |   hash: string; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-17 14:41:23 +02:00
										 |  |  |   constructor() { | 
					
						
							|  |  |  |     this.searchParams = {}; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2021-05-02 16:32:24 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-03 12:49:53 +02:00
										 |  |  |   clone(): Smarturl { | 
					
						
							|  |  |  |     const clonedInstance = new Smarturl(); | 
					
						
							|  |  |  |     Object.assign(clonedInstance, this); | 
					
						
							|  |  |  |     clonedInstance.searchParams = { ...this.searchParams }; | 
					
						
							|  |  |  |     return clonedInstance; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-02 16:32:24 +00:00
										 |  |  |   toString() { | 
					
						
							| 
									
										
										
										
											2022-07-28 15:10:34 +02:00
										 |  |  |     let userpart = ``; | 
					
						
							|  |  |  |     if (this.username && !this.password) { | 
					
						
							|  |  |  |       userpart = `${this.username}@`; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (this.username && this.password) { | 
					
						
							|  |  |  |       userpart = `${this.username}:${this.password}@`; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-21 13:56:34 +02:00
										 |  |  |     return `${this.protocol}//${userpart}${this.hostname}:${this.port}${this.path}`; | 
					
						
							| 
									
										
										
										
											2021-04-12 19:18:36 +00:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2024-10-03 12:49:53 +02:00
										 |  |  | } |