@naverpay/nurl 0.0.12 → 0.1.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.
@@ -1 +1,77 @@
1
- export { default } from './nurl';
1
+ type Query = Record<string, string | number | boolean | (string | number | boolean)[]>;
2
+
3
+ interface URLOptions extends Partial<Pick<URL, 'href' | 'protocol' | 'host' | 'hostname' | 'port' | 'pathname' | 'search' | 'hash' | 'username' | 'password'>> {
4
+ baseUrl?: string;
5
+ query?: Query;
6
+ basePath?: string;
7
+ }
8
+ declare class NURL implements URL {
9
+ private _href;
10
+ private _protocol;
11
+ private _host;
12
+ private _hostname;
13
+ private _port;
14
+ private _pathname;
15
+ private _search;
16
+ private _hash;
17
+ private _origin;
18
+ private _username;
19
+ private _password;
20
+ private _baseUrl;
21
+ private _searchParams;
22
+ private _basePath;
23
+ constructor(input?: string | URL | URLOptions);
24
+ static withBasePath(basePath: string): (urlOptions?: string | URL | URLOptions) => NURL;
25
+ static create(input?: string | URL | URLOptions): NURL;
26
+ static canParse(input: string): boolean;
27
+ get baseUrl(): string;
28
+ set baseUrl(value: string);
29
+ get href(): string;
30
+ set href(value: string);
31
+ static parseStringToURLLike(value: string): {
32
+ href: string;
33
+ protocol: string;
34
+ host: string;
35
+ hostname: string;
36
+ port: string;
37
+ pathname: string;
38
+ search: string;
39
+ hash: string;
40
+ origin: string;
41
+ username: string;
42
+ password: string;
43
+ searchParams: URLSearchParams;
44
+ } | null;
45
+ get protocol(): string;
46
+ set protocol(value: string);
47
+ get host(): string;
48
+ set host(value: string);
49
+ get hostname(): string;
50
+ set hostname(value: string);
51
+ get port(): string;
52
+ set port(value: string);
53
+ get pathname(): string;
54
+ set pathname(inputPathname: string);
55
+ get search(): string;
56
+ set search(search: string);
57
+ setSearchParams(_params: Record<string, string>): void;
58
+ appendSearchParams(_params: Record<string, string>): void;
59
+ removeSearchParams(..._keys: string[]): void;
60
+ get searchParams(): URLSearchParams;
61
+ get hash(): string;
62
+ set hash(value: string);
63
+ get origin(): string;
64
+ get username(): string;
65
+ set username(value: string);
66
+ get password(): string;
67
+ set password(value: string);
68
+ private updateHref;
69
+ toString(): string;
70
+ toJSON(): string;
71
+ private punycodePrefix;
72
+ private encodeHostname;
73
+ get decodedIDN(): string;
74
+ get decodedHostname(): string;
75
+ }
76
+
77
+ export { NURL as default };
package/dist/cjs/nurl.js CHANGED
@@ -1 +1 @@
1
- "use strict";const i=require("./punycode.js"),r=require("./utils.js");class a{constructor(t){if(this._href="",this._protocol="",this._host="",this._hostname="",this._port="",this._pathname="",this._search="",this._hash="",this._origin="",this._username="",this._password="",this._baseUrl="",this._searchParams=new URLSearchParams,this._basePath="",this.punycodePrefix="xn--",this._searchParams=new URLSearchParams,typeof t=="string"||t instanceof URL)this.href=t.toString();else if(t){if(t.basePath&&(this._basePath=t.basePath.startsWith("/")?t.basePath:`/${t.basePath}`),t.baseUrl&&(this.baseUrl=t.baseUrl),t.href&&(this.href=t.href),t.protocol&&(this.protocol=t.protocol),t.host&&(this.host=t.host),t.hostname&&(this.hostname=t.hostname),t.port&&(this.port=t.port),t.pathname){const s=this._basePath;let e=t.pathname;s&&!e.startsWith(s)&&(e=`${s}${e.startsWith("/")?"":"/"}${e}`),this.pathname=r.refinePathnameWithQuery(e,t.query??{})}if(t.search&&(this.search=t.search),t.hash&&(this.hash=t.hash),t.username&&(this.username=t.username),t.password&&(this.password=t.password),t.query){const s=r.refineQueryWithPathname(t.pathname??"",t.query);Object.keys(s).length>0&&(this.search=new URLSearchParams(r.convertQueryToArray(s)).toString())}this.updateHref()}}static withBasePath(t){return s=>typeof s=="string"||s instanceof URL?new a({href:s.toString(),basePath:t}):new a({...s,basePath:t})}static create(t){return new a(t)}static canParse(t){if(t.startsWith("/"))return/^\/[^?#]*(\?[^#]*)?(#.*)?$/.test(t);try{return new URL(t),!0}catch{return/^[^:/?#]+(\.[^:/?#]+)+(\/[^?#]*(\?[^#]*)?(#.*)?)?$/.test(t)}}get baseUrl(){return this._baseUrl}set baseUrl(t){this._baseUrl=t;try{const s=new URL(t);this._protocol=s.protocol,this._host=s.host,this._hostname=s.hostname,this._port=s.port,this._origin=s.origin,this._username=s.username,this._password=s.password,s.pathname!=="/"&&(this._pathname=s.pathname),s.search&&(this._search=s.search,this._searchParams=new URLSearchParams(s.search)),s.hash&&(this._hash=s.hash),this.updateHref()}catch(s){console.warn(`Invalid baseUrl: ${t}`,s)}}get href(){return this.pathname.length===1?`${this._href}/`:this._href}set href(t){try{const s=new URL(t);this._href=s.href,this._protocol=s.protocol,this._host=s.host,this._hostname=s.hostname,this._port=s.port,this._pathname=s.pathname,this._search=s.search,this._hash=s.hash,this._origin=s.origin,this._username=s.username,this._password=s.password,this._searchParams=s.searchParams}catch(s){console.warn(`Can not parse ${t}`,s)}}get protocol(){return this._protocol}set protocol(t){this._protocol=t,this.updateHref()}get host(){return this._host}set host(t){const[s,e]=t.split(":"),h=this.encodeHostname(s);this._host=e?`${h}:${e}`:h,this._hostname=h,this._port=e||"",this.updateHref()}get hostname(){return this._hostname}set hostname(t){const s=this.encodeHostname(t);this._hostname=s,this._host=this._port?`${s}:${this._port}`:s,this.updateHref()}get port(){return this._port}set port(t){this._port=t,this._host=`${this._hostname}${t?":"+t:""}`,this.updateHref()}get pathname(){return this._pathname}set pathname(t){let s=t;this._basePath&&!s.startsWith(this._basePath)&&(s=`${this._basePath}${s.startsWith("/")?"":"/"}${s}`);const e=s.split("/").map(h=>r.isDynamicPath(h)?h:encodeURI(h)).join("/");this._pathname=e.startsWith("/")?e:`/${e}`,this.updateHref()}get search(){return this._search}set search(t){this._search=t.startsWith("?")?t:`?${t}`,this._searchParams=new URLSearchParams(t),this.updateHref()}setSearchParams(t){const s=new URLSearchParams(t);this._search=s.toString()?`?${s.toString()}`:"",this._searchParams=s,this.updateHref()}appendSearchParams(t){const s=new URLSearchParams(this._searchParams),e=r.getDynamicPaths(this._pathname).map(r.extractPathKey);Object.keys(t).forEach(h=>{e.includes(h)?this._pathname=r.refinePathnameWithQuery(this._pathname,{[h]:t[h]}):s.append(h,t[h])}),this._search=s.toString()?`?${s.toString()}`:"",this._searchParams=s,this.updateHref()}removeSearchParams(...t){const s=new URLSearchParams(this._searchParams);t.forEach(e=>{s.delete(e)}),this._search=s.toString()?`?${s.toString()}`:"",this._searchParams=s,this.updateHref()}get searchParams(){return new Proxy(this._searchParams,{get:(t,s,e)=>{const h=Reflect.get(t,s,e);return typeof h=="function"?(...o)=>{const n=h.apply(t,o);return this._search=this._searchParams.toString()?`?${this._searchParams.toString()}`:"",this.updateHref(),n}:h}})}get hash(){return this._hash}set hash(t){this._hash=t.startsWith("#")?t:`#${t}`,this.updateHref()}get origin(){return this._origin}get username(){return this._username}set username(t){this._username=t,this.updateHref()}get password(){return this._password}set password(t){this._password=t,this.updateHref()}updateHref(){if(this._baseUrl){const t=new URL(this._baseUrl);t.pathname=this._pathname,t.search=this._search,t.hash=this._hash,this._href=t.href,this._origin=t.origin}else this._href=`${this._protocol}${this._protocol&&"//"}${this._username}${this._password?":"+this._password:""}${this._username||this._password?"@":""}${this._hostname}${this._port?":"+this._port:""}${this._pathname==="/"?"":this._pathname}${this._search}${this._hash}`,this._origin=`${this._protocol}//${this._hostname}${this._port?":"+this._port:""}`}toString(){return this.href}toJSON(){return this.href}encodeHostname(t){return t.split(".").map(s=>{for(const e of s)if(r.isASCIICodeChar(e))return`${this.punycodePrefix}${i.encode(s)}`;return s}).join(".")}get decodedIDN(){let t=this._href;return this._hostname.split(".").forEach(s=>{t=t.replace(s,i.decode(s.replace(this.punycodePrefix,"")))}),t}get decodedHostname(){return this._hostname.split(".").map(t=>i.decode(t.replace(this.punycodePrefix,""))).join(".")}}module.exports=a;
1
+ "use strict";const m=require("./punycode.js"),r=require("./utils.js");class i{constructor(t){if(this._href="",this._protocol="",this._host="",this._hostname="",this._port="",this._pathname="",this._search="",this._hash="",this._origin="",this._username="",this._password="",this._baseUrl="",this._searchParams=new URLSearchParams,this._basePath="",this.punycodePrefix="xn--",this._searchParams=new URLSearchParams,typeof t=="string"||t instanceof URL)this.href=t.toString();else if(t){if(t.basePath&&(this._basePath=t.basePath.startsWith("/")?t.basePath:`/${t.basePath}`),t.baseUrl&&(this.baseUrl=t.baseUrl),t.href&&(this.href=t.href),t.protocol&&(this.protocol=t.protocol),t.host&&(this.host=t.host),t.hostname&&(this.hostname=t.hostname),t.port&&(this.port=t.port),t.pathname){const s=this._basePath;let e=t.pathname;s&&!e.startsWith(s)&&(e=`${s}${e.startsWith("/")?"":"/"}${e}`),this.pathname=r.refinePathnameWithQuery(e,t.query??{})}if(t.search&&(this.search=t.search),t.hash&&(this.hash=t.hash),t.username&&(this.username=t.username),t.password&&(this.password=t.password),t.query){const s=r.refineQueryWithPathname(t.pathname??"",t.query);Object.keys(s).length>0&&(this.search=new URLSearchParams(r.convertQueryToArray(s)).toString())}this.updateHref()}}static withBasePath(t){return s=>typeof s=="string"||s instanceof URL?new i({href:s.toString(),basePath:t}):new i({...s,basePath:t})}static create(t){return new i(t)}static canParse(t){if(t.startsWith("/"))return/^\/[^?#]*(\?[^#]*)?(#.*)?$/.test(t);try{return new URL(t),!0}catch{return/^[^:/?#]+(\.[^:/?#]+)+(\/[^?#]*(\?[^#]*)?(#.*)?)?$/.test(t)}}get baseUrl(){return this._baseUrl}set baseUrl(t){this._baseUrl=t;try{const s=new URL(t);this._protocol=s.protocol,this._host=s.host,this._hostname=s.hostname,this._port=s.port,this._origin=s.origin,this._username=s.username,this._password=s.password,s.pathname!=="/"&&(this._pathname=s.pathname),s.search&&(this._search=s.search,this._searchParams=new URLSearchParams(s.search)),s.hash&&(this._hash=s.hash),this.updateHref()}catch(s){console.warn(`Invalid baseUrl: ${t}`,s)}}get href(){return this.pathname.length===1?`${this._href}/`:this._href}set href(t){try{const s=new URL(t);this._href=s.href,this._protocol=s.protocol,this._host=s.host,this._hostname=s.hostname,this._port=s.port,this._pathname=s.pathname,this._search=s.search,this._hash=s.hash,this._origin=s.origin,this._username=s.username,this._password=s.password,this._searchParams=s.searchParams}catch(s){const e=i.parseStringToURLLike(t);if(!e)throw console.warn(`Can not parse ${t}`),s;this._href=e.href,this._protocol=e.protocol,this._host=e.hostname,this._hostname=e.hostname,this._port=e.port,this._pathname=e.pathname,this._search=e.search,this._hash=e.hash,this._origin=e.origin,this._username=e.username,this._password=e.password,this._searchParams=e.searchParams}}static parseStringToURLLike(t){const s=/^(?:(https?:\/\/)(?:([^:@]+)(?::([^@]+))?@)?((?:[^/:?#]+)(?::(\d+))?)?)?([/][^?#]*)?(\?[^#]*)?(#.*)?$/,e=t.match(s),[h=t,a="",n="",d="",o="",c="",p="",_="",f=""]=e||[];if(!e||a&&!o&&!p&&!_&&!f)return null;const P=a&&o?`${a}//${o}${c?`:${c}`:""}`:"";return{href:h,protocol:a,host:o,hostname:o,port:c,pathname:t?p||"/":"",search:_,hash:f,origin:P,username:n,password:d,searchParams:new URLSearchParams(_)}}get protocol(){return this._protocol}set protocol(t){this._protocol=t,this.updateHref()}get host(){return this._host}set host(t){const[s,e]=t.split(":"),h=this.encodeHostname(s);this._host=e?`${h}:${e}`:h,this._hostname=h,this._port=e||"",this.updateHref()}get hostname(){return this._hostname}set hostname(t){const s=this.encodeHostname(t);this._hostname=s,this._host=this._port?`${s}:${this._port}`:s,this.updateHref()}get port(){return this._port}set port(t){this._port=t,this._host=`${this._hostname}${t?":"+t:""}`,this.updateHref()}get pathname(){return this._pathname}set pathname(t){let s=t;this._basePath&&!s.startsWith(this._basePath)&&(s=`${this._basePath}${s.startsWith("/")?"":"/"}${s}`);const e=s.split("/").map(h=>r.isDynamicPath(h)?h:encodeURI(h)).join("/");this._pathname=e.startsWith("/")?e:`/${e}`,this.updateHref()}get search(){return this._search}set search(t){this._search=t.startsWith("?")?t:`?${t}`,this._searchParams=new URLSearchParams(t),this.updateHref()}setSearchParams(t){const s=new URLSearchParams(t);this._search=s.toString()?`?${s.toString()}`:"",this._searchParams=s,this.updateHref()}appendSearchParams(t){const s=new URLSearchParams(this._searchParams),e=r.getDynamicPaths(this._pathname).map(r.extractPathKey);Object.keys(t).forEach(h=>{e.includes(h)?this._pathname=r.refinePathnameWithQuery(this._pathname,{[h]:t[h]}):s.append(h,t[h])}),this._search=s.toString()?`?${s.toString()}`:"",this._searchParams=s,this.updateHref()}removeSearchParams(...t){const s=new URLSearchParams(this._searchParams);t.forEach(e=>{s.delete(e)}),this._search=s.toString()?`?${s.toString()}`:"",this._searchParams=s,this.updateHref()}get searchParams(){return new Proxy(this._searchParams,{get:(t,s,e)=>{const h=Reflect.get(t,s,e);return typeof h=="function"?(...a)=>{const n=h.apply(t,a);return this._search=this._searchParams.toString()?`?${this._searchParams.toString()}`:"",this.updateHref(),n}:h}})}get hash(){return this._hash}set hash(t){this._hash=t.startsWith("#")?t:`#${t}`,this.updateHref()}get origin(){return this._origin}get username(){return this._username}set username(t){this._username=t,this.updateHref()}get password(){return this._password}set password(t){this._password=t,this.updateHref()}updateHref(){if(this._baseUrl){const t=new URL(this._baseUrl);t.pathname=this._pathname,t.search=this._search,t.hash=this._hash,this._href=t.href,this._origin=t.origin}else this._href=`${this._protocol}${this._protocol&&"//"}${this._username}${this._password?":"+this._password:""}${this._username||this._password?"@":""}${this._hostname}${this._port?":"+this._port:""}${this._pathname==="/"?"":this._pathname}${this._search}${this._hash}`,this._origin=`${this._protocol}//${this._hostname}${this._port?":"+this._port:""}`}toString(){return this.href}toJSON(){return this.href}encodeHostname(t){return t.split(".").map(s=>{for(const e of s)if(r.isASCIICodeChar(e))return`${this.punycodePrefix}${m.encode(s)}`;return s}).join(".")}get decodedIDN(){let t=this._href;return this._hostname.split(".").forEach(s=>{t=t.replace(s,m.decode(s.replace(this.punycodePrefix,"")))}),t}get decodedHostname(){return this._hostname.split(".").map(t=>m.decode(t.replace(this.punycodePrefix,""))).join(".")}}module.exports=i;
@@ -1 +1,77 @@
1
- export { default } from './nurl';
1
+ type Query = Record<string, string | number | boolean | (string | number | boolean)[]>;
2
+
3
+ interface URLOptions extends Partial<Pick<URL, 'href' | 'protocol' | 'host' | 'hostname' | 'port' | 'pathname' | 'search' | 'hash' | 'username' | 'password'>> {
4
+ baseUrl?: string;
5
+ query?: Query;
6
+ basePath?: string;
7
+ }
8
+ declare class NURL implements URL {
9
+ private _href;
10
+ private _protocol;
11
+ private _host;
12
+ private _hostname;
13
+ private _port;
14
+ private _pathname;
15
+ private _search;
16
+ private _hash;
17
+ private _origin;
18
+ private _username;
19
+ private _password;
20
+ private _baseUrl;
21
+ private _searchParams;
22
+ private _basePath;
23
+ constructor(input?: string | URL | URLOptions);
24
+ static withBasePath(basePath: string): (urlOptions?: string | URL | URLOptions) => NURL;
25
+ static create(input?: string | URL | URLOptions): NURL;
26
+ static canParse(input: string): boolean;
27
+ get baseUrl(): string;
28
+ set baseUrl(value: string);
29
+ get href(): string;
30
+ set href(value: string);
31
+ static parseStringToURLLike(value: string): {
32
+ href: string;
33
+ protocol: string;
34
+ host: string;
35
+ hostname: string;
36
+ port: string;
37
+ pathname: string;
38
+ search: string;
39
+ hash: string;
40
+ origin: string;
41
+ username: string;
42
+ password: string;
43
+ searchParams: URLSearchParams;
44
+ } | null;
45
+ get protocol(): string;
46
+ set protocol(value: string);
47
+ get host(): string;
48
+ set host(value: string);
49
+ get hostname(): string;
50
+ set hostname(value: string);
51
+ get port(): string;
52
+ set port(value: string);
53
+ get pathname(): string;
54
+ set pathname(inputPathname: string);
55
+ get search(): string;
56
+ set search(search: string);
57
+ setSearchParams(_params: Record<string, string>): void;
58
+ appendSearchParams(_params: Record<string, string>): void;
59
+ removeSearchParams(..._keys: string[]): void;
60
+ get searchParams(): URLSearchParams;
61
+ get hash(): string;
62
+ set hash(value: string);
63
+ get origin(): string;
64
+ get username(): string;
65
+ set username(value: string);
66
+ get password(): string;
67
+ set password(value: string);
68
+ private updateHref;
69
+ toString(): string;
70
+ toJSON(): string;
71
+ private punycodePrefix;
72
+ private encodeHostname;
73
+ get decodedIDN(): string;
74
+ get decodedHostname(): string;
75
+ }
76
+
77
+ export { NURL as default };
package/dist/esm/nurl.mjs CHANGED
@@ -1,33 +1,33 @@
1
- import { encode as c, decode as a } from "./punycode.mjs";
2
- import { refinePathnameWithQuery as i, refineQueryWithPathname as _, convertQueryToArray as m, isDynamicPath as f, getDynamicPaths as p, extractPathKey as d, isASCIICodeChar as P } from "./utils.mjs";
3
- class r {
1
+ import { encode as l, decode as p } from "./punycode.mjs";
2
+ import { refinePathnameWithQuery as f, refineQueryWithPathname as $, convertQueryToArray as g, isDynamicPath as u, getDynamicPaths as w, extractPathKey as S, isASCIICodeChar as U } from "./utils.mjs";
3
+ class i {
4
4
  constructor(t) {
5
5
  if (this._href = "", this._protocol = "", this._host = "", this._hostname = "", this._port = "", this._pathname = "", this._search = "", this._hash = "", this._origin = "", this._username = "", this._password = "", this._baseUrl = "", this._searchParams = new URLSearchParams(), this._basePath = "", this.punycodePrefix = "xn--", this._searchParams = new URLSearchParams(), typeof t == "string" || t instanceof URL)
6
6
  this.href = t.toString();
7
7
  else if (t) {
8
8
  if (t.basePath && (this._basePath = t.basePath.startsWith("/") ? t.basePath : `/${t.basePath}`), t.baseUrl && (this.baseUrl = t.baseUrl), t.href && (this.href = t.href), t.protocol && (this.protocol = t.protocol), t.host && (this.host = t.host), t.hostname && (this.hostname = t.hostname), t.port && (this.port = t.port), t.pathname) {
9
9
  const s = this._basePath;
10
- let e = t.pathname;
11
- s && !e.startsWith(s) && (e = `${s}${e.startsWith("/") ? "" : "/"}${e}`), this.pathname = i(e, t.query ?? {});
10
+ let h = t.pathname;
11
+ s && !h.startsWith(s) && (h = `${s}${h.startsWith("/") ? "" : "/"}${h}`), this.pathname = f(h, t.query ?? {});
12
12
  }
13
13
  if (t.search && (this.search = t.search), t.hash && (this.hash = t.hash), t.username && (this.username = t.username), t.password && (this.password = t.password), t.query) {
14
- const s = _(t.pathname ?? "", t.query);
15
- Object.keys(s).length > 0 && (this.search = new URLSearchParams(m(s)).toString());
14
+ const s = $(t.pathname ?? "", t.query);
15
+ Object.keys(s).length > 0 && (this.search = new URLSearchParams(g(s)).toString());
16
16
  }
17
17
  this.updateHref();
18
18
  }
19
19
  }
20
20
  static withBasePath(t) {
21
- return (s) => typeof s == "string" || s instanceof URL ? new r({
21
+ return (s) => typeof s == "string" || s instanceof URL ? new i({
22
22
  href: s.toString(),
23
23
  basePath: t
24
- }) : new r({
24
+ }) : new i({
25
25
  ...s,
26
26
  basePath: t
27
27
  });
28
28
  }
29
29
  static create(t) {
30
- return new r(t);
30
+ return new i(t);
31
31
  }
32
32
  static canParse(t) {
33
33
  if (t.startsWith("/"))
@@ -58,9 +58,32 @@ class r {
58
58
  const s = new URL(t);
59
59
  this._href = s.href, this._protocol = s.protocol, this._host = s.host, this._hostname = s.hostname, this._port = s.port, this._pathname = s.pathname, this._search = s.search, this._hash = s.hash, this._origin = s.origin, this._username = s.username, this._password = s.password, this._searchParams = s.searchParams;
60
60
  } catch (s) {
61
- console.warn(`Can not parse ${t}`, s);
61
+ const h = i.parseStringToURLLike(t);
62
+ if (!h)
63
+ throw console.warn(`Can not parse ${t}`), s;
64
+ this._href = h.href, this._protocol = h.protocol, this._host = h.hostname, this._hostname = h.hostname, this._port = h.port, this._pathname = h.pathname, this._search = h.search, this._hash = h.hash, this._origin = h.origin, this._username = h.username, this._password = h.password, this._searchParams = h.searchParams;
62
65
  }
63
66
  }
67
+ static parseStringToURLLike(t) {
68
+ const s = /^(?:(https?:\/\/)(?:([^:@]+)(?::([^@]+))?@)?((?:[^/:?#]+)(?::(\d+))?)?)?([/][^?#]*)?(\?[^#]*)?(#.*)?$/, h = t.match(s), [e = t, r = "", o = "", d = "", a = "", n = "", _ = "", c = "", m = ""] = h || [];
69
+ if (!h || r && !a && !_ && !c && !m)
70
+ return null;
71
+ const P = r && a ? `${r}//${a}${n ? `:${n}` : ""}` : "";
72
+ return {
73
+ href: e,
74
+ protocol: r,
75
+ host: a,
76
+ hostname: a,
77
+ port: n,
78
+ pathname: t ? _ || "/" : "",
79
+ search: c,
80
+ hash: m,
81
+ origin: P,
82
+ username: o,
83
+ password: d,
84
+ searchParams: new URLSearchParams(c)
85
+ };
86
+ }
64
87
  get protocol() {
65
88
  return this._protocol;
66
89
  }
@@ -71,8 +94,8 @@ class r {
71
94
  return this._host;
72
95
  }
73
96
  set host(t) {
74
- const [s, e] = t.split(":"), h = this.encodeHostname(s);
75
- this._host = e ? `${h}:${e}` : h, this._hostname = h, this._port = e || "", this.updateHref();
97
+ const [s, h] = t.split(":"), e = this.encodeHostname(s);
98
+ this._host = h ? `${e}:${h}` : e, this._hostname = e, this._port = h || "", this.updateHref();
76
99
  }
77
100
  get hostname() {
78
101
  return this._hostname;
@@ -93,8 +116,8 @@ class r {
93
116
  set pathname(t) {
94
117
  let s = t;
95
118
  this._basePath && !s.startsWith(this._basePath) && (s = `${this._basePath}${s.startsWith("/") ? "" : "/"}${s}`);
96
- const e = s.split("/").map((h) => f(h) ? h : encodeURI(h)).join("/");
97
- this._pathname = e.startsWith("/") ? e : `/${e}`, this.updateHref();
119
+ const h = s.split("/").map((e) => u(e) ? e : encodeURI(e)).join("/");
120
+ this._pathname = h.startsWith("/") ? h : `/${h}`, this.updateHref();
98
121
  }
99
122
  get search() {
100
123
  return this._search;
@@ -107,27 +130,27 @@ class r {
107
130
  this._search = s.toString() ? `?${s.toString()}` : "", this._searchParams = s, this.updateHref();
108
131
  }
109
132
  appendSearchParams(t) {
110
- const s = new URLSearchParams(this._searchParams), e = p(this._pathname).map(d);
111
- Object.keys(t).forEach((h) => {
112
- e.includes(h) ? this._pathname = i(this._pathname, {
113
- [h]: t[h]
114
- }) : s.append(h, t[h]);
133
+ const s = new URLSearchParams(this._searchParams), h = w(this._pathname).map(S);
134
+ Object.keys(t).forEach((e) => {
135
+ h.includes(e) ? this._pathname = f(this._pathname, {
136
+ [e]: t[e]
137
+ }) : s.append(e, t[e]);
115
138
  }), this._search = s.toString() ? `?${s.toString()}` : "", this._searchParams = s, this.updateHref();
116
139
  }
117
140
  removeSearchParams(...t) {
118
141
  const s = new URLSearchParams(this._searchParams);
119
- t.forEach((e) => {
120
- s.delete(e);
142
+ t.forEach((h) => {
143
+ s.delete(h);
121
144
  }), this._search = s.toString() ? `?${s.toString()}` : "", this._searchParams = s, this.updateHref();
122
145
  }
123
146
  get searchParams() {
124
147
  return new Proxy(this._searchParams, {
125
- get: (t, s, e) => {
126
- const h = Reflect.get(t, s, e);
127
- return typeof h == "function" ? (...o) => {
128
- const n = h.apply(t, o);
129
- return this._search = this._searchParams.toString() ? `?${this._searchParams.toString()}` : "", this.updateHref(), n;
130
- } : h;
148
+ get: (t, s, h) => {
149
+ const e = Reflect.get(t, s, h);
150
+ return typeof e == "function" ? (...r) => {
151
+ const o = e.apply(t, r);
152
+ return this._search = this._searchParams.toString() ? `?${this._searchParams.toString()}` : "", this.updateHref(), o;
153
+ } : e;
131
154
  }
132
155
  });
133
156
  }
@@ -167,22 +190,22 @@ class r {
167
190
  }
168
191
  encodeHostname(t) {
169
192
  return t.split(".").map((s) => {
170
- for (const e of s)
171
- if (P(e))
172
- return `${this.punycodePrefix}${c(s)}`;
193
+ for (const h of s)
194
+ if (U(h))
195
+ return `${this.punycodePrefix}${l(s)}`;
173
196
  return s;
174
197
  }).join(".");
175
198
  }
176
199
  get decodedIDN() {
177
200
  let t = this._href;
178
201
  return this._hostname.split(".").forEach((s) => {
179
- t = t.replace(s, a(s.replace(this.punycodePrefix, "")));
202
+ t = t.replace(s, p(s.replace(this.punycodePrefix, "")));
180
203
  }), t;
181
204
  }
182
205
  get decodedHostname() {
183
- return this._hostname.split(".").map((t) => a(t.replace(this.punycodePrefix, ""))).join(".");
206
+ return this._hostname.split(".").map((t) => p(t.replace(this.punycodePrefix, ""))).join(".");
184
207
  }
185
208
  }
186
209
  export {
187
- r as default
210
+ i as default
188
211
  };
package/package.json CHANGED
@@ -1,9 +1,10 @@
1
1
  {
2
2
  "name": "@naverpay/nurl",
3
- "version": "0.0.12",
3
+ "version": "0.1.0",
4
4
  "description": "URL build library",
5
5
  "main": "./dist/cjs/index.js",
6
6
  "module": "./dist/esm/index.mjs",
7
+ "types": "./dist/cjs/index.d.ts",
7
8
  "exports": {
8
9
  ".": {
9
10
  "import": {
@@ -28,7 +29,7 @@
28
29
  "author": "@NaverPayDev/frontend",
29
30
  "repository": {
30
31
  "type": "git",
31
- "url": "https://github.com/NaverPayDev/nurl.git"
32
+ "url": "git+https://github.com/NaverPayDev/nurl.git"
32
33
  },
33
34
  "license": "MIT",
34
35
  "devDependencies": {
@@ -37,7 +38,7 @@
37
38
  "@naverpay/editorconfig": "^0.0.4",
38
39
  "@naverpay/eslint-config": "^1.0.7",
39
40
  "@naverpay/markdown-lint": "^0.0.3",
40
- "@naverpay/pite": "^0.0.6",
41
+ "@naverpay/pite": "^2.1.0",
41
42
  "@naverpay/prettier-config": "^1.0.0",
42
43
  "typescript": "^5.5.4",
43
44
  "vite": "^6.0.7",
@@ -1,61 +0,0 @@
1
- import { Query } from './utils';
2
- interface URLOptions extends Partial<Pick<URL, 'href' | 'protocol' | 'host' | 'hostname' | 'port' | 'pathname' | 'search' | 'hash' | 'username' | 'password'>> {
3
- baseUrl?: string;
4
- query?: Query;
5
- basePath?: string;
6
- }
7
- export default class NURL implements URL {
8
- private _href;
9
- private _protocol;
10
- private _host;
11
- private _hostname;
12
- private _port;
13
- private _pathname;
14
- private _search;
15
- private _hash;
16
- private _origin;
17
- private _username;
18
- private _password;
19
- private _baseUrl;
20
- private _searchParams;
21
- private _basePath;
22
- constructor(input?: string | URL | URLOptions);
23
- static withBasePath(basePath: string): (urlOptions?: string | URL | URLOptions) => NURL;
24
- static create(input?: string | URL | URLOptions): NURL;
25
- static canParse(input: string): boolean;
26
- get baseUrl(): string;
27
- set baseUrl(value: string);
28
- get href(): string;
29
- set href(value: string);
30
- get protocol(): string;
31
- set protocol(value: string);
32
- get host(): string;
33
- set host(value: string);
34
- get hostname(): string;
35
- set hostname(value: string);
36
- get port(): string;
37
- set port(value: string);
38
- get pathname(): string;
39
- set pathname(inputPathname: string);
40
- get search(): string;
41
- set search(search: string);
42
- setSearchParams(_params: Record<string, string>): void;
43
- appendSearchParams(_params: Record<string, string>): void;
44
- removeSearchParams(..._keys: string[]): void;
45
- get searchParams(): URLSearchParams;
46
- get hash(): string;
47
- set hash(value: string);
48
- get origin(): string;
49
- get username(): string;
50
- set username(value: string);
51
- get password(): string;
52
- set password(value: string);
53
- private updateHref;
54
- toString(): string;
55
- toJSON(): string;
56
- private punycodePrefix;
57
- private encodeHostname;
58
- get decodedIDN(): string;
59
- get decodedHostname(): string;
60
- }
61
- export {};
@@ -1,55 +0,0 @@
1
- interface PunycodeStatic {
2
- version: string;
3
- ucs2: {
4
- decode(string: string): number[];
5
- encode(codePoints: number[]): string;
6
- };
7
- decode(input: string): string;
8
- encode(input: string): string;
9
- toASCII(input: string): string;
10
- toUnicode(input: string): string;
11
- }
12
- /**
13
- * Creates an array containing the numeric code points of each Unicode
14
- * character in the string.
15
- * @param {string} string The Unicode input string (UCS-2).
16
- * @returns {number[]} The new array of code points.
17
- */
18
- declare function ucs2decode(string: string): number[];
19
- /**
20
- * Creates a string based on an array of numeric code points.
21
- * @param {number[]} codePoints The array of numeric code points.
22
- * @returns {string} The new Unicode string (UCS-2).
23
- */
24
- declare const ucs2encode: (codePoints: number[]) => string;
25
- /**
26
- * Converts a Punycode string of ASCII-only symbols to a string of Unicode symbols.
27
- * @param {string} input The Punycode string of ASCII-only symbols.
28
- * @returns {string} The resulting string of Unicode symbols.
29
- */
30
- declare const decode: (input: string) => string;
31
- /**
32
- * Converts a string of Unicode symbols to a Punycode string of ASCII-only symbols.
33
- * @param {string} input The string of Unicode symbols.
34
- * @returns {string} The resulting Punycode string of ASCII-only symbols.
35
- */
36
- declare const encode: (input: string) => string;
37
- /**
38
- * Converts a Punycode string representing a domain name or an email address to Unicode.
39
- * @param {string} input The Punycoded domain name or email address.
40
- * @returns {string} The Unicode representation of the given Punycode string.
41
- */
42
- declare const toUnicode: (input: string) => string;
43
- /**
44
- * Converts a Unicode string representing a domain name or an email address to Punycode.
45
- * @param {string} input The domain name or email address to convert.
46
- * @returns {string} The Punycode representation of the given domain name or email address.
47
- */
48
- declare const toASCII: (input: string) => string;
49
- /** Define the public API */
50
- /**
51
- * @description originated from https://unpkg.com/browse/punycode@2.3.1/
52
- */
53
- declare const punycode: PunycodeStatic;
54
- export { ucs2decode, ucs2encode, decode, encode, toASCII, toUnicode };
55
- export default punycode;
@@ -1,26 +0,0 @@
1
- export type Query = Record<string, string | number | boolean | (string | number | boolean)[]>;
2
- export declare function isDynamicPath(path: string): boolean;
3
- export declare function getDynamicPaths(pathname: string): string[];
4
- export declare function extractPathKey(path: string): string;
5
- /**
6
- * Replaces dynamic paths in the pathname with values from the query
7
- * @param {string} pathname
8
- * @param {Query} query
9
- * @returns {string} refined pathname
10
- */
11
- export declare function refinePathnameWithQuery(pathname: string, query: Query): string;
12
- /**
13
- * Removes queries that have already been used in the pathname.
14
- * @param {string} pathname
15
- * @param {Query} query
16
- * @returns {Query} refined query
17
- */
18
- export declare function refineQueryWithPathname(pathname: string, query: Query): Query;
19
- export declare function isASCIICodeChar(char: string): boolean;
20
- /**
21
- * Convert queries to array, if they are not of the defined primitive types, they will not be included in the array.
22
- * @param {string} pathname
23
- * @param {Query} query
24
- * @returns {string[][]} refined query
25
- */
26
- export declare function convertQueryToArray(query: Query): string[][];
@@ -1,61 +0,0 @@
1
- import { Query } from './utils';
2
- interface URLOptions extends Partial<Pick<URL, 'href' | 'protocol' | 'host' | 'hostname' | 'port' | 'pathname' | 'search' | 'hash' | 'username' | 'password'>> {
3
- baseUrl?: string;
4
- query?: Query;
5
- basePath?: string;
6
- }
7
- export default class NURL implements URL {
8
- private _href;
9
- private _protocol;
10
- private _host;
11
- private _hostname;
12
- private _port;
13
- private _pathname;
14
- private _search;
15
- private _hash;
16
- private _origin;
17
- private _username;
18
- private _password;
19
- private _baseUrl;
20
- private _searchParams;
21
- private _basePath;
22
- constructor(input?: string | URL | URLOptions);
23
- static withBasePath(basePath: string): (urlOptions?: string | URL | URLOptions) => NURL;
24
- static create(input?: string | URL | URLOptions): NURL;
25
- static canParse(input: string): boolean;
26
- get baseUrl(): string;
27
- set baseUrl(value: string);
28
- get href(): string;
29
- set href(value: string);
30
- get protocol(): string;
31
- set protocol(value: string);
32
- get host(): string;
33
- set host(value: string);
34
- get hostname(): string;
35
- set hostname(value: string);
36
- get port(): string;
37
- set port(value: string);
38
- get pathname(): string;
39
- set pathname(inputPathname: string);
40
- get search(): string;
41
- set search(search: string);
42
- setSearchParams(_params: Record<string, string>): void;
43
- appendSearchParams(_params: Record<string, string>): void;
44
- removeSearchParams(..._keys: string[]): void;
45
- get searchParams(): URLSearchParams;
46
- get hash(): string;
47
- set hash(value: string);
48
- get origin(): string;
49
- get username(): string;
50
- set username(value: string);
51
- get password(): string;
52
- set password(value: string);
53
- private updateHref;
54
- toString(): string;
55
- toJSON(): string;
56
- private punycodePrefix;
57
- private encodeHostname;
58
- get decodedIDN(): string;
59
- get decodedHostname(): string;
60
- }
61
- export {};
@@ -1,55 +0,0 @@
1
- interface PunycodeStatic {
2
- version: string;
3
- ucs2: {
4
- decode(string: string): number[];
5
- encode(codePoints: number[]): string;
6
- };
7
- decode(input: string): string;
8
- encode(input: string): string;
9
- toASCII(input: string): string;
10
- toUnicode(input: string): string;
11
- }
12
- /**
13
- * Creates an array containing the numeric code points of each Unicode
14
- * character in the string.
15
- * @param {string} string The Unicode input string (UCS-2).
16
- * @returns {number[]} The new array of code points.
17
- */
18
- declare function ucs2decode(string: string): number[];
19
- /**
20
- * Creates a string based on an array of numeric code points.
21
- * @param {number[]} codePoints The array of numeric code points.
22
- * @returns {string} The new Unicode string (UCS-2).
23
- */
24
- declare const ucs2encode: (codePoints: number[]) => string;
25
- /**
26
- * Converts a Punycode string of ASCII-only symbols to a string of Unicode symbols.
27
- * @param {string} input The Punycode string of ASCII-only symbols.
28
- * @returns {string} The resulting string of Unicode symbols.
29
- */
30
- declare const decode: (input: string) => string;
31
- /**
32
- * Converts a string of Unicode symbols to a Punycode string of ASCII-only symbols.
33
- * @param {string} input The string of Unicode symbols.
34
- * @returns {string} The resulting Punycode string of ASCII-only symbols.
35
- */
36
- declare const encode: (input: string) => string;
37
- /**
38
- * Converts a Punycode string representing a domain name or an email address to Unicode.
39
- * @param {string} input The Punycoded domain name or email address.
40
- * @returns {string} The Unicode representation of the given Punycode string.
41
- */
42
- declare const toUnicode: (input: string) => string;
43
- /**
44
- * Converts a Unicode string representing a domain name or an email address to Punycode.
45
- * @param {string} input The domain name or email address to convert.
46
- * @returns {string} The Punycode representation of the given domain name or email address.
47
- */
48
- declare const toASCII: (input: string) => string;
49
- /** Define the public API */
50
- /**
51
- * @description originated from https://unpkg.com/browse/punycode@2.3.1/
52
- */
53
- declare const punycode: PunycodeStatic;
54
- export { ucs2decode, ucs2encode, decode, encode, toASCII, toUnicode };
55
- export default punycode;
@@ -1,26 +0,0 @@
1
- export type Query = Record<string, string | number | boolean | (string | number | boolean)[]>;
2
- export declare function isDynamicPath(path: string): boolean;
3
- export declare function getDynamicPaths(pathname: string): string[];
4
- export declare function extractPathKey(path: string): string;
5
- /**
6
- * Replaces dynamic paths in the pathname with values from the query
7
- * @param {string} pathname
8
- * @param {Query} query
9
- * @returns {string} refined pathname
10
- */
11
- export declare function refinePathnameWithQuery(pathname: string, query: Query): string;
12
- /**
13
- * Removes queries that have already been used in the pathname.
14
- * @param {string} pathname
15
- * @param {Query} query
16
- * @returns {Query} refined query
17
- */
18
- export declare function refineQueryWithPathname(pathname: string, query: Query): Query;
19
- export declare function isASCIICodeChar(char: string): boolean;
20
- /**
21
- * Convert queries to array, if they are not of the defined primitive types, they will not be included in the array.
22
- * @param {string} pathname
23
- * @param {Query} query
24
- * @returns {string[][]} refined query
25
- */
26
- export declare function convertQueryToArray(query: Query): string[][];