@server/next 0.15.1 → 0.15.2

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@server/next",
3
- "version": "0.15.1",
3
+ "version": "0.15.2",
4
4
  "description": "An experimental reimplementation of server.js focused on the DX",
5
5
  "homepage": "https://node-server.com/",
6
6
  "repository": "https://github.com/franciscop/server-next.git",
package/src/ServerUrl.js CHANGED
@@ -1,21 +1,53 @@
1
- export default class ServerUrl {
1
+ import { inspect } from "util";
2
+
3
+ const colors = {
4
+ string: process.env.NO_COLOR ? "" : "\x1b[32m",
5
+ number: process.env.NO_COLOR ? "" : "\x1b[33m",
6
+ };
7
+
8
+ const properties = [
9
+ "hash",
10
+ "host",
11
+ "hostname",
12
+ "href",
13
+ "origin",
14
+ "params",
15
+ "password",
16
+ "path",
17
+ "pathname",
18
+ "port",
19
+ "protocol",
20
+ "query",
21
+ "search",
22
+ "searchParams",
23
+ "username",
24
+ ];
25
+
26
+ export default class ServerUrl extends URL {
2
27
  constructor(urlString) {
3
- const url = new URL(urlString);
4
- this.href = url.href;
5
- this.origin = url.origin;
6
- this.protocol = url.protocol;
7
- this.username = url.username;
8
- this.password = url.password;
9
- this.host = url.host;
10
- this.hostname = url.hostname;
11
- this.port = url.port ? +url.port : null; // make it an intege
12
- this.pathname = url.pathname;
13
- this.path = url.pathname; // nicknam
14
- this.params = {}; // The URL parameter
15
- this.search = url.search;
16
- this.searchParams = url.searchParams;
17
- this.query = this.getQuery(url.searchParams.entries()); // As a plain object
18
- this.hash = url.hash;
28
+ super(urlString);
29
+
30
+ const custom = {
31
+ port: +this.port || null,
32
+ path: this.pathname,
33
+ params: {},
34
+ query: this.getQuery(this.searchParams.entries()),
35
+ };
36
+
37
+ for (let key of properties) {
38
+ const value = key in custom ? custom[key] : this[key];
39
+ Object.defineProperty(this, key, { value, enumerable: true });
40
+ }
41
+ }
42
+
43
+ [inspect.custom]() {
44
+ const props = Object.keys(this)
45
+ .map((key) => {
46
+ const color = colors[typeof this[key]] || "";
47
+ return ` ${key}: ${color}${inspect(this[key])}\x1b[0m`;
48
+ })
49
+ .join("\n");
50
+ return `ServerUrl {\n${props}\n}`;
19
51
  }
20
52
 
21
53
  getQuery(entries) {
@@ -17,6 +17,24 @@ describe("getUrl()", () => {
17
17
  expect(url.host).toBe("example.com");
18
18
  expect(url.hostname).toBe("example.com");
19
19
  expect(url.port).toBe(null);
20
+
21
+ expect(Object.keys(url)).toEqual([
22
+ "hash",
23
+ "host",
24
+ "hostname",
25
+ "href",
26
+ "origin",
27
+ "params",
28
+ "password",
29
+ "path",
30
+ "pathname",
31
+ "port",
32
+ "protocol",
33
+ "query",
34
+ "search",
35
+ "searchParams",
36
+ "username",
37
+ ]);
20
38
  });
21
39
 
22
40
  it("can parse localhost", () => {