smarturl/ts/smarturl.classes.smarturl.ts

141 lines
3.4 KiB
TypeScript
Raw Normal View History

2022-07-26 22:40:18 +00: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 20:17:41 +00:00
const parsedUrlInstance = new URL(urlArg);
2021-05-02 16:32:24 +00:00
const searchParams: ISearchParams = {};
// enrichment
2022-07-28 13:10:34 +00:00
const searchParamPairs: {
key: string;
value: string;
}[] = []
if (parsedUrlInstance.search) {
parsedUrlInstance.search
2022-07-26 23:06:16 +00:00
.replace('?', '')
.split('&')
.map((searchParamPair) => {
2022-07-28 13:10:34 +00:00
searchParamPairs.push({
2022-07-26 23:06:16 +00:00
key: searchParamPair.split('=')[0],
value: searchParamPair.split('=')[1],
2022-07-28 13:10:34 +00:00
});
2022-07-26 23:06:16 +00:00
});
2022-07-28 13:10:34 +00:00
}
2022-07-26 23:06:16 +00: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 13:10:34 +00: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-26 22:40:18 +00: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 20:28:16 +00:00
...parsedUrlInstance,
2021-04-12 20:09:19 +00:00
href: parsedUrlInstance.href,
2022-08-06 20:17:41 +00:00
origin: parsedUrlInstance.origin,
2021-04-12 20:09:19 +00:00
protocol: parsedUrlInstance.protocol,
2022-08-06 20:17:41 +00: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;
}
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 12:41:23 +00:00
constructor() {
this.searchParams = {};
}
2021-05-02 16:32:24 +00:00
toString() {
2022-07-28 13:10:34 +00:00
let userpart = ``;
if (this.username && !this.password) {
userpart = `${this.username}@`;
}
if (this.username && this.password) {
userpart = `${this.username}:${this.password}@`;
}
2022-08-21 11:56:34 +00:00
return `${this.protocol}//${userpart}${this.hostname}:${this.port}${this.path}`;
2021-04-12 19:18:36 +00:00
}
}