@colisweb/rescript-toolkit 5.8.0 → 5.9.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@colisweb/rescript-toolkit",
3
- "version": "5.8.0",
3
+ "version": "5.9.0",
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
  ```
@@ -0,0 +1,99 @@
1
+ type coordinates = {
2
+ lat: float,
3
+ lng: float,
4
+ }
5
+ module Layers = {
6
+ type map
7
+
8
+ type rec t = {vector: vector}
9
+ and vector = {normal: normalVector}
10
+ and normalVector = {map: map}
11
+ }
12
+ module Platform = {
13
+ type t
14
+ type options = {apikey: string}
15
+
16
+ @new
17
+ external make: options => t = "H.service.Platform"
18
+
19
+ type layersOptions = {pois: bool}
20
+
21
+ @send
22
+ external createDefaultLayers: (t, layersOptions) => Layers.t = "createDefaultLayers"
23
+ }
24
+
25
+ module Map = {
26
+ type instance
27
+
28
+ type options = {
29
+ zoom: int,
30
+ center: coordinates,
31
+ }
32
+ @new
33
+ external make: (Dom.element, Layers.map, options) => instance = "H.Map"
34
+
35
+ module Marker = {
36
+ type t
37
+
38
+ @new
39
+ external make: coordinates => t = "H.map.Marker"
40
+
41
+ @send
42
+ external setData: (t, string) => unit = "setData"
43
+ }
44
+
45
+ module Viewport = {
46
+ type t
47
+
48
+ @send
49
+ external resize: (t, unit) => unit = "resize"
50
+ }
51
+
52
+ module Group = {
53
+ type t
54
+ @new
55
+ external make: unit => t = "H.map.Group"
56
+
57
+ @send
58
+ external addEventListener: (t, string, 'event => unit) => unit = "addEventListener"
59
+
60
+ @send
61
+ external addMarker: (t, Marker.t) => unit = "addObject"
62
+ }
63
+
64
+ @send
65
+ external addMarker: (instance, Marker.t) => unit = "addObject"
66
+ @send
67
+ external addGroup: (instance, Group.t) => unit = "addObject"
68
+
69
+ @send
70
+ external getViewPort: (instance, unit) => Viewport.t = "getViewPort"
71
+ }
72
+
73
+ module MapEvents = {
74
+ type t
75
+
76
+ @new
77
+ external make: Map.instance => t = "H.mapevents.MapEvents"
78
+ }
79
+ module Behavior = {
80
+ type t
81
+
82
+ @new
83
+ external make: MapEvents.t => t = "H.mapevents.Behavior"
84
+ }
85
+ module Ui = {
86
+ type t
87
+
88
+ @new
89
+ external createDefault: (Map.instance, Layers.t) => t = "H.ui.UI.createDefault"
90
+
91
+ module InfoBubble = {
92
+ type t
93
+ @new
94
+ external make: ('geometry, 'data) => t = "H.ui.InfoBubble"
95
+ }
96
+
97
+ @send
98
+ external addBubble: (t, InfoBubble.t) => unit = "addBubble"
99
+ }