@8ms/helpers 1.1.96 → 1.1.97

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,4 +1,10 @@
1
+ import { Response } from './response';
1
2
  import { State } from './states';
3
+ declare type Constructor = {
4
+ body?: any;
5
+ error?: any;
6
+ state?: State;
7
+ };
2
8
  /**
3
9
  * Class version of the API to enable quicker instances.
4
10
  */
@@ -6,31 +12,38 @@ declare class ApiResponse {
6
12
  state: State;
7
13
  body: any;
8
14
  error: any;
9
- getJson(): {
10
- body: any;
11
- error: any;
12
- state: string;
13
- };
15
+ constructor({ body, error, state }?: Constructor);
16
+ fromJson({ input }: {
17
+ input: Response;
18
+ }): this;
19
+ getBody(): any;
20
+ getBodyDefaultTo({ defaultValue, keys }: {
21
+ defaultValue: any;
22
+ keys: string | string[];
23
+ }): any;
24
+ getError(): any;
25
+ getJson(): Response;
26
+ getState(): State;
14
27
  isError(): boolean;
15
28
  isIdle(): boolean;
16
29
  isPending(): boolean;
17
30
  isSuccess(): boolean;
18
- setToIdle(): void;
19
- setToPending(): void;
31
+ setToIdle(): this;
32
+ setToPending(): this;
20
33
  setToError({ error }: {
21
34
  error: any;
22
- }): void;
35
+ }): this;
23
36
  setToSuccess({ body }: {
24
37
  body: any;
25
- }): void;
38
+ }): this;
26
39
  setBody({ body }: {
27
40
  body: any;
28
- }): void;
41
+ }): this;
29
42
  setError({ error }: {
30
43
  error: any;
31
- }): void;
44
+ }): this;
32
45
  setState({ state }: {
33
46
  state: State;
34
- }): void;
47
+ }): this;
35
48
  }
36
49
  export default ApiResponse;
@@ -3,15 +3,39 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
+ const defaultTo_1 = __importDefault(require("../util/defaultTo"));
6
7
  const states_1 = __importDefault(require("./states"));
7
8
  /**
8
9
  * Class version of the API to enable quicker instances.
9
10
  */
10
11
  class ApiResponse {
11
- constructor() {
12
+ constructor({ body, error, state } = {}) {
12
13
  this.state = states_1.default.IDLE;
13
14
  this.body = null;
14
15
  this.error = null;
16
+ this.body = body;
17
+ this.error = error;
18
+ this.state = state;
19
+ return this;
20
+ }
21
+ fromJson({ input }) {
22
+ this.body = input.body;
23
+ this.error = input.error;
24
+ this.state = input.state;
25
+ return this;
26
+ }
27
+ getBody() {
28
+ return this.body;
29
+ }
30
+ getBodyDefaultTo({ defaultValue, keys }) {
31
+ return (0, defaultTo_1.default)({
32
+ defaultValue,
33
+ instance: this.body,
34
+ keys,
35
+ });
36
+ }
37
+ getError() {
38
+ return this.state;
15
39
  }
16
40
  getJson() {
17
41
  return {
@@ -20,6 +44,9 @@ class ApiResponse {
20
44
  state: this.state,
21
45
  };
22
46
  }
47
+ getState() {
48
+ return this.state;
49
+ }
23
50
  isError() {
24
51
  return this.state === states_1.default.ERROR;
25
52
  }
@@ -34,26 +61,33 @@ class ApiResponse {
34
61
  }
35
62
  setToIdle() {
36
63
  this.state = states_1.default.IDLE;
64
+ return this;
37
65
  }
38
66
  setToPending() {
39
67
  this.state = states_1.default.PENDING;
68
+ return this;
40
69
  }
41
70
  setToError({ error }) {
42
71
  this.error = error;
43
72
  this.state = states_1.default.SUCCESS;
73
+ return this;
44
74
  }
45
75
  setToSuccess({ body }) {
46
76
  this.body = body;
47
77
  this.state = states_1.default.SUCCESS;
78
+ return this;
48
79
  }
49
80
  setBody({ body }) {
50
81
  this.body = body;
82
+ return this;
51
83
  }
52
84
  setError({ error }) {
53
85
  this.error = error;
86
+ return this;
54
87
  }
55
88
  setState({ state }) {
56
89
  this.state = state;
90
+ return this;
57
91
  }
58
92
  }
59
93
  exports.default = ApiResponse;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@8ms/helpers",
3
3
  "license": "UNLICENSED",
4
- "version": "1.1.96",
4
+ "version": "1.1.97",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "git+https://github.com/8millionstories-organisation/8ms-helpers-ts.git"