@colisweb/rescript-toolkit 5.8.0 → 5.8.1

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": "@colisweb/rescript-toolkit",
3
- "version": "5.8.0",
3
+ "version": "5.8.1",
4
4
  "type": "module",
5
5
  "scripts": {
6
6
  "clean": "rescript clean",
@@ -29,7 +29,7 @@
29
29
  "dependencies": {
30
30
  "@colisweb/bs-react-intl-extractor-bin": "0.12.2",
31
31
  "@datadog/browser-rum": "5.8.0",
32
- "@dck/rescript-ky": "2.0.3",
32
+ "@dck/rescript-ky": "2.0.4",
33
33
  "@dck/rescript-promise": "1.1.0",
34
34
  "@dck/restorative": "1.1.0",
35
35
  "@greenlabs/ppx-spice": "0.2.1",
@@ -4,100 +4,97 @@
4
4
 
5
5
  ```rescript
6
6
  module API = {
7
- module FetchUsers = {
8
- module Config = {
9
- type argument = unit;
10
-
11
- [@spice]
12
- type response = array(user)
13
- [@spice]
14
- and user = {
15
- name: string
16
- };
17
-
18
- type error = string;
19
-
20
- let exec = clientId => {
21
- request->Request.get(baseUrl(~clientId), response_decode);
22
- };
23
- };
7
+ let kyInstance = Ky.Instance.create({
8
+ prefixUrl: `${ColiswebApi__Env.v6ApiUrl}`,
9
+ timeout: ColiswebApi__Env.isNodeDevelopment ? 5000 : 20000,
10
+ })
24
11
 
25
- module Request = Toolkit.Request.Make(Config);
12
+ module FetchUsers = {
13
+ type argument = ()
14
+
15
+ @spice
16
+ type user = {
17
+ id: string,
18
+ name: string
19
+ }
20
+
21
+ @spice
22
+ type response = {
23
+ users: array<user>
24
+ }
25
+
26
+ type error = unit
27
+
28
+ let config = () => {
29
+ kyInstance,
30
+ requestOptions: {
31
+ method: Ky.HttpMethod.GET,
32
+ },
33
+ key: [
34
+ "FetchUser"
35
+ ],
36
+ path: `/users`,
37
+ }
26
38
  };
27
39
  };
28
40
 
29
- API.FetchUsers.Request.exec();
41
+ let (data, isLoading, _) = Request.useFetcher(module(API.FetchUsers), Some())
42
+
43
+ Request.fetchAPI(module(API.FetchUsers), Some())
44
+ ->Promise.tapOk(({users}) => Js.log(users))
30
45
  ```
31
46
 
32
47
  ### Handle custom error
33
48
 
34
49
  ```rescript
35
50
  module API = {
51
+ let kyInstance = Ky.Instance.create({
52
+ prefixUrl: `${ColiswebApi__Env.v6ApiUrl}`,
53
+ timeout: ColiswebApi__Env.isNodeDevelopment ? 5000 : 20000,
54
+ })
55
+
36
56
  module FetchUsers = {
37
- module Error = {
38
- type errorType = [
39
- | `overlappingActivations(array(activation))
40
- | `compileError(string)
41
- | `accessRefused
42
- | `unknown
43
- ]
44
- [@spice]
45
- and activation = {ruleTitle: string};
46
-
47
- let encoder: Spice.encoder(errorType) = _v => ""->Spice.stringToJson;
48
-
49
- let decoder: Spice.decoder(errorType) =
50
- json => {
51
- let error = json->Obj.magic;
52
- let value =
53
- switch (error##code) {
54
- | Some("OVERLAPPING_ACTIVATIONS") =>
55
- `overlappingActivations(
56
- error##overlapped
57
- ->Obj.magic
58
- ->Array.map(a => a->activation_decode->Result.getExn),
59
- )
60
- | Some("COMPILE_ERROR") =>
61
- `compileError(error##compileError->Obj.magic)
62
- | Some("ACCESS_REFUSED") => `accessRefused
63
- | _ => `unknown
64
- };
65
-
66
- value->Ok;
67
- };
68
-
69
- let codec: Spice.codec(errorType) = (encoder, decoder);
70
-
71
- [@spice]
72
- type t = [@spice.codec codec] errorType;
73
- };
74
-
75
- module Config = {
76
- type argument = unit;
77
-
78
- [@spice]
79
- type response = array(user)
80
- [@spice]
81
- and user = {
82
- name: string
83
- };
84
-
85
- [@spice]
86
- type error = Error.t;
87
-
88
- let exec = clientId => {
89
- request->Request.get(baseUrl(~clientId), ~errorDecoder=error_decode,response_decode);
90
- };
91
- };
92
-
93
- module Request = Toolkit.Request.Make(Config);
57
+ type argument = ()
58
+
59
+ @spice
60
+ type user = {
61
+ id: string,
62
+ name: string
63
+ }
64
+
65
+ @spice
66
+ type response = {
67
+ users: array<user>
68
+ }
69
+
70
+ type error = Anything
71
+
72
+ let config = () => {
73
+ kyInstance,
74
+ requestOptions: {
75
+ method: Ky.HttpMethod.GET,
76
+ },
77
+ key: [
78
+ "FetchUser"
79
+ ],
80
+ path: `/users`,
81
+ mapCustomErrors: error =>
82
+ switch error.response {
83
+ | Some({status: 409}) => Custom(Anything)
84
+ | _ => Unknown(error)
85
+ }->Promise.resolved,
86
+ }
94
87
  };
95
88
  };
96
89
 
97
- API.FetchUsers.Request.exec()
98
- ->Promise.tapError(err => switch (err) {
99
- | Custom(`accessRefused) => "handle refused access"
100
- | Custom(`compileError(msg)) => msg
101
- | _ => "else"
102
- });
90
+
91
+ Request.fetchAPI(module(API.FetchUsers), Some())
92
+ ->Promise.tapOk(({users}) => Js.log(users))
93
+ ->Promise.tapError(error => {
94
+ switch error {
95
+ | Custom(Anything) => ...
96
+ | DecodeError(spiceError) => ...
97
+ | Unknown(kyError) => ...
98
+ }
99
+ })
103
100
  ```