@nestia/fetcher 1.0.0 → 1.1.0-dev.20230413

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/lib/Fetcher.d.ts CHANGED
@@ -16,7 +16,7 @@ import { Primitive } from "./Primitive";
16
16
  */
17
17
  export declare class Fetcher {
18
18
  /**
19
- * Fetch function for the `GET` or `DELETE` methods.
19
+ * Fetch function for the `GET` methods.
20
20
  *
21
21
  * @param connection Connection information for the remote HTTP server
22
22
  * @param encrypted Whether the request/response body be encrypted or not
@@ -24,9 +24,9 @@ export declare class Fetcher {
24
24
  * @param path Path of the HTTP request
25
25
  * @return Response body data from the remote HTTP server
26
26
  */
27
- static fetch<Output>(connection: IConnection, encrypted: Fetcher.IEncrypted, method: "GET" | "DELETE", path: string): Promise<Primitive<Output>>;
27
+ static fetch<Output>(connection: IConnection, encrypted: Fetcher.IEncrypted, method: "GET", path: string): Promise<Primitive<Output>>;
28
28
  /**
29
- * Fetch function for the `POST`, `PUT` and `PATCH` methods.
29
+ * Fetch function for the `POST`, `PUT`, `PATCH` and `DELETE` methods.
30
30
  *
31
31
  * @param connection Connection information for the remote HTTP server
32
32
  * @param encrypted Whether the request/response body be encrypted or not
@@ -36,7 +36,7 @@ export declare class Fetcher {
36
36
  * @param stringify JSON string conversion function, default is the `JSON.stringify`
37
37
  * @return Response body data from the remote HTTP server
38
38
  */
39
- static fetch<Input, Output>(connection: IConnection, encrypted: Fetcher.IEncrypted, method: "POST" | "PUT" | "PATCH", path: string, input: Input, stringify?: (input: Input) => string): Promise<Primitive<Output>>;
39
+ static fetch<Input, Output>(connection: IConnection, encrypted: Fetcher.IEncrypted, method: "POST" | "PUT" | "PATCH" | "DELETE", path: string, input?: Input, stringify?: (input: Input) => string): Promise<Primitive<Output>>;
40
40
  }
41
41
  export declare namespace Fetcher {
42
42
  /**
@@ -18,4 +18,29 @@ export declare class HttpError extends Error {
18
18
  * @param message Error message from the remote HTTP server.
19
19
  */
20
20
  constructor(method: "GET" | "DELETE" | "POST" | "PUT" | "PATCH", path: string, status: number, message: string);
21
+ /**
22
+ * `HttpError` to JSON.
23
+ *
24
+ * When you call `JSON.stringify()` function on current `HttpError` instance,
25
+ * this `HttpError.toJSON()` method would be automatically called.
26
+ *
27
+ * Also, if response body from the remote HTTP server forms a JSON object,
28
+ * this `HttpError.toJSON()` method would be useful because it returns the
29
+ * parsed JSON object about the {@link message} property.
30
+ *
31
+ * @template T Expected type of the response body.
32
+ * @returns JSON object of the `HttpError`.
33
+ */
34
+ toJSON<T>(): HttpError.IProps<T>;
35
+ }
36
+ export declare namespace HttpError {
37
+ /**
38
+ * Returned type of {@link HttpError.toJSON} method.
39
+ */
40
+ interface IProps<T> {
41
+ method: "GET" | "DELETE" | "POST" | "PUT" | "PATCH";
42
+ path: string;
43
+ status: number;
44
+ message: T;
45
+ }
21
46
  }
package/lib/HttpError.js CHANGED
@@ -39,6 +39,10 @@ var HttpError = /** @class */ (function (_super) {
39
39
  _this.method = method;
40
40
  _this.path = path;
41
41
  _this.status = status;
42
+ /**
43
+ * @internal
44
+ */
45
+ _this.body_ = NOT_YET;
42
46
  // INHERITANCE POLYFILL
43
47
  var proto = _newTarget.prototype;
44
48
  if (Object.setPrototypeOf)
@@ -47,7 +51,31 @@ var HttpError = /** @class */ (function (_super) {
47
51
  _this.__proto__ = proto;
48
52
  return _this;
49
53
  }
54
+ /**
55
+ * `HttpError` to JSON.
56
+ *
57
+ * When you call `JSON.stringify()` function on current `HttpError` instance,
58
+ * this `HttpError.toJSON()` method would be automatically called.
59
+ *
60
+ * Also, if response body from the remote HTTP server forms a JSON object,
61
+ * this `HttpError.toJSON()` method would be useful because it returns the
62
+ * parsed JSON object about the {@link message} property.
63
+ *
64
+ * @template T Expected type of the response body.
65
+ * @returns JSON object of the `HttpError`.
66
+ */
67
+ HttpError.prototype.toJSON = function () {
68
+ if (this.body_ === NOT_YET)
69
+ this.body_ = JSON.parse(this.message);
70
+ return {
71
+ method: this.method,
72
+ path: this.path,
73
+ status: this.status,
74
+ message: this.body_,
75
+ };
76
+ };
50
77
  return HttpError;
51
78
  }(Error));
52
79
  exports.HttpError = HttpError;
80
+ var NOT_YET = {};
53
81
  //# sourceMappingURL=HttpError.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"HttpError.js","sourceRoot":"","sources":["../src/HttpError.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAAA;;;;;;GAMG;AACH;IAA+B,6BAAK;IAChC;;;;;;;OAOG;IACH,mBACoB,MAAmD,EACnD,IAAY,EACZ,MAAc,EAC9B,OAAe;;QAJnB,YAMI,kBAAM,OAAO,CAAC,SAMjB;QAXmB,YAAM,GAAN,MAAM,CAA6C;QACnD,UAAI,GAAJ,IAAI,CAAQ;QACZ,YAAM,GAAN,MAAM,CAAQ;QAK9B,uBAAuB;QACvB,IAAM,KAAK,GAAc,WAAW,SAAS,CAAC;QAC9C,IAAI,MAAM,CAAC,cAAc;YAAE,MAAM,CAAC,cAAc,CAAC,KAAI,EAAE,KAAK,CAAC,CAAC;;YACxD,KAAY,CAAC,SAAS,GAAG,KAAK,CAAC;;IACzC,CAAC;IACL,gBAAC;AAAD,CAAC,AAtBD,CAA+B,KAAK,GAsBnC;AAtBY,8BAAS"}
1
+ {"version":3,"file":"HttpError.js","sourceRoot":"","sources":["../src/HttpError.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAAA;;;;;;GAMG;AACH;IAA+B,6BAAK;IAMhC;;;;;;;OAOG;IACH,mBACoB,MAAmD,EACnD,IAAY,EACZ,MAAc,EAC9B,OAAe;;QAJnB,YAMI,kBAAM,OAAO,CAAC,SAMjB;QAXmB,YAAM,GAAN,MAAM,CAA6C;QACnD,UAAI,GAAJ,IAAI,CAAQ;QACZ,YAAM,GAAN,MAAM,CAAQ;QAhBlC;;WAEG;QACK,WAAK,GAAQ,OAAO,CAAC;QAkBzB,uBAAuB;QACvB,IAAM,KAAK,GAAc,WAAW,SAAS,CAAC;QAC9C,IAAI,MAAM,CAAC,cAAc;YAAE,MAAM,CAAC,cAAc,CAAC,KAAI,EAAE,KAAK,CAAC,CAAC;;YACxD,KAAY,CAAC,SAAS,GAAG,KAAK,CAAC;;IACzC,CAAC;IAED;;;;;;;;;;;;OAYG;IACI,0BAAM,GAAb;QACI,IAAI,IAAI,CAAC,KAAK,KAAK,OAAO;YAAE,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QAClE,OAAO;YACH,MAAM,EAAE,IAAI,CAAC,MAAM;YACnB,IAAI,EAAE,IAAI,CAAC,IAAI;YACf,MAAM,EAAE,IAAI,CAAC,MAAM;YACnB,OAAO,EAAE,IAAI,CAAC,KAAK;SACtB,CAAC;IACN,CAAC;IACL,gBAAC;AAAD,CAAC,AAlDD,CAA+B,KAAK,GAkDnC;AAlDY,8BAAS;AA+DtB,IAAM,OAAO,GAAG,EAAS,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nestia/fetcher",
3
- "version": "1.0.0",
3
+ "version": "1.1.0-dev.20230413",
4
4
  "description": "Fetcher library of Nestia SDK",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
package/src/Fetcher.ts CHANGED
@@ -24,7 +24,7 @@ import { Singleton } from "./internal/Singleton";
24
24
  */
25
25
  export class Fetcher {
26
26
  /**
27
- * Fetch function for the `GET` or `DELETE` methods.
27
+ * Fetch function for the `GET` methods.
28
28
  *
29
29
  * @param connection Connection information for the remote HTTP server
30
30
  * @param encrypted Whether the request/response body be encrypted or not
@@ -35,12 +35,12 @@ export class Fetcher {
35
35
  public static fetch<Output>(
36
36
  connection: IConnection,
37
37
  encrypted: Fetcher.IEncrypted,
38
- method: "GET" | "DELETE",
38
+ method: "GET",
39
39
  path: string,
40
40
  ): Promise<Primitive<Output>>;
41
41
 
42
42
  /**
43
- * Fetch function for the `POST`, `PUT` and `PATCH` methods.
43
+ * Fetch function for the `POST`, `PUT`, `PATCH` and `DELETE` methods.
44
44
  *
45
45
  * @param connection Connection information for the remote HTTP server
46
46
  * @param encrypted Whether the request/response body be encrypted or not
@@ -53,9 +53,9 @@ export class Fetcher {
53
53
  public static fetch<Input, Output>(
54
54
  connection: IConnection,
55
55
  encrypted: Fetcher.IEncrypted,
56
- method: "POST" | "PUT" | "PATCH",
56
+ method: "POST" | "PUT" | "PATCH" | "DELETE",
57
57
  path: string,
58
- input: Input,
58
+ input?: Input,
59
59
  stringify?: (input: Input) => string,
60
60
  ): Promise<Primitive<Output>>;
61
61
 
package/src/HttpError.ts CHANGED
@@ -6,6 +6,11 @@
6
6
  * @author Jeongho Nam - https://github.com/samchon
7
7
  */
8
8
  export class HttpError extends Error {
9
+ /**
10
+ * @internal
11
+ */
12
+ private body_: any = NOT_YET;
13
+
9
14
  /**
10
15
  * Initializer Constructor.
11
16
  *
@@ -27,4 +32,40 @@ export class HttpError extends Error {
27
32
  if (Object.setPrototypeOf) Object.setPrototypeOf(this, proto);
28
33
  else (this as any).__proto__ = proto;
29
34
  }
35
+
36
+ /**
37
+ * `HttpError` to JSON.
38
+ *
39
+ * When you call `JSON.stringify()` function on current `HttpError` instance,
40
+ * this `HttpError.toJSON()` method would be automatically called.
41
+ *
42
+ * Also, if response body from the remote HTTP server forms a JSON object,
43
+ * this `HttpError.toJSON()` method would be useful because it returns the
44
+ * parsed JSON object about the {@link message} property.
45
+ *
46
+ * @template T Expected type of the response body.
47
+ * @returns JSON object of the `HttpError`.
48
+ */
49
+ public toJSON<T>(): HttpError.IProps<T> {
50
+ if (this.body_ === NOT_YET) this.body_ = JSON.parse(this.message);
51
+ return {
52
+ method: this.method,
53
+ path: this.path,
54
+ status: this.status,
55
+ message: this.body_,
56
+ };
57
+ }
30
58
  }
59
+ export namespace HttpError {
60
+ /**
61
+ * Returned type of {@link HttpError.toJSON} method.
62
+ */
63
+ export interface IProps<T> {
64
+ method: "GET" | "DELETE" | "POST" | "PUT" | "PATCH";
65
+ path: string;
66
+ status: number;
67
+ message: T;
68
+ }
69
+ }
70
+
71
+ const NOT_YET = {} as any;