@colisweb/rescript-toolkit 2.25.1 → 2.26.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": "2.25.1",
3
+ "version": "2.26.0",
4
4
  "scripts": {
5
5
  "clean": "rescript clean",
6
6
  "build": "rescript build",
@@ -0,0 +1,43 @@
1
+ let localStorageKey = "@colisweb/mock"
2
+
3
+ @react.component
4
+ let make = (~worker: Msw.worker, ~children) => {
5
+ let (mockEnabled, setMock) = React.useState(() =>
6
+ Browser.LocalStorage.getItem(localStorageKey)
7
+ ->Js.Nullable.toOption
8
+ ->Option.mapWithDefault(false, bool_of_string)
9
+ )
10
+
11
+ React.useEffect1(() => {
12
+ if mockEnabled {
13
+ worker->Msw.start
14
+ Browser.LocalStorage.setItem(localStorageKey, Js.Nullable.return("true"))
15
+ } else {
16
+ worker->Msw.stop
17
+ Browser.LocalStorage.removeItem(localStorageKey)
18
+ }
19
+
20
+ None
21
+ }, [mockEnabled])
22
+
23
+ <React.Suspense fallback={<Toolkit__Ui_SpinnerFullScreen />}>
24
+ <label
25
+ className={`fixed top-2 left-1/2 flex flex-row gap-2 items-center justify-center ${mockEnabled
26
+ ? "bg-success-100 border-success-400 text-success-700"
27
+ : "bg-neutral-100"} p-1 border rounded-lg z-50 cursor-pointer`}>
28
+ <span> {"Mock"->React.string} </span>
29
+ <input
30
+ type_={"checkbox"}
31
+ checked={mockEnabled}
32
+ onChange={event => {
33
+ let checked = (event->ReactEvent.Form.target)["checked"]
34
+
35
+ setMock(_ => checked)
36
+ }}
37
+ />
38
+ </label>
39
+ {children}
40
+ </React.Suspense>
41
+ }
42
+
43
+ let defaul = make
@@ -0,0 +1,2 @@
1
+ @react.component
2
+ let make: (~worker: Msw.worker, ~children: React.element) => React.element
@@ -0,0 +1,65 @@
1
+ # Mock
2
+
3
+ We use [msw](https://mswjs.io) for mocking API content.
4
+
5
+ ## Usage
6
+
7
+ Install the package
8
+
9
+ ```
10
+ yarn add msw --dev
11
+ ```
12
+
13
+ Define the mock
14
+
15
+ ```rescript
16
+ /* Mock_Page_X.res */
17
+
18
+ open Identifiers
19
+
20
+ let mocks = [
21
+ Msw.Rest.get(
22
+ "https://api.url",
23
+ (req, res, ctx) => {
24
+ open ColiswebApi.V5.Transporter.GetContactsWithParameters.Config
25
+ let _emptyResponse = []
26
+ let defaultResponse = [
27
+ {
28
+ id: ContactIdString.make("1"),
29
+ firstName: "Thomas",
30
+ lastName: "Deconinck",
31
+ email: "dck@colisweb.com",
32
+ primaryPhone: "0123456789",
33
+ parameters: {
34
+ id: ParameterId.make("1"),
35
+ email: true,
36
+ sms: true,
37
+ call: true,
38
+ },
39
+ carrier: None,
40
+ }
41
+ ]
42
+
43
+ let transporterId = req.params["transporterId"]
44
+
45
+ switch transporterId {
46
+ | "9" => res(Msw.Ctx.json(ctx, defaultResponse))
47
+ | _ => ()
48
+ }
49
+ },
50
+ ),
51
+ Msw.Rest.put(
52
+ "https://api.url",
53
+ (_req, res, ctx) => {
54
+ res(Msw.Ctx.status(ctx, 200))
55
+ },
56
+ ),
57
+ ]
58
+ ```
59
+
60
+ Define the worker with the mock at the top of your app.
61
+
62
+ ```rescript
63
+ /* Mock.res */
64
+ let worker = Msw.setupWorker([]->Array.concat(Mock_Page_X.mocks))
65
+ ```
@@ -3,6 +3,8 @@ module LocalStorage = {
3
3
  external getItem: string => Js.Nullable.t<string> = "localStorage.getItem"
4
4
  @val
5
5
  external setItem: (string, Js.Nullable.t<string>) => unit = "localStorage.setItem"
6
+ @val
7
+ external removeItem: string => unit = "localStorage.removeItem"
6
8
  }
7
9
 
8
10
  module Location = {
@@ -1,6 +1,7 @@
1
1
  module LocalStorage: {
2
2
  let getItem: string => Js.Nullable.t<string>
3
3
  let setItem: (string, Js.Nullable.t<string>) => unit
4
+ let removeItem: string => unit
4
5
  }
5
6
 
6
7
  module Location: {
@@ -0,0 +1,32 @@
1
+ type mock
2
+ type worker
3
+
4
+ @module("msw") @variadic
5
+ external setupWorker: array<mock> => worker = "setupWorker"
6
+
7
+ @send
8
+ external start: worker => unit = "start"
9
+ @send
10
+ external stop: worker => unit = "stop"
11
+
12
+ module Ctx = {
13
+ type t
14
+
15
+ @send
16
+ external json: (t, 'a) => 'b = "json"
17
+ @send
18
+ external status: (t, 'a) => 'b = "status"
19
+ }
20
+
21
+ module Rest = {
22
+ type req<'params> = {params: 'params}
23
+
24
+ @module("msw") @scope("rest")
25
+ external get: (string, (req<'params>, @uncurry ('z => 't), Ctx.t) => 'a) => mock = "get"
26
+ @module("msw") @scope("rest")
27
+ external put: (string, (req<'params>, @uncurry ('z => 't), Ctx.t) => 'a) => mock = "put"
28
+ @module("msw") @scope("rest")
29
+ external post: (string, (req<'params>, @uncurry ('z => 't), Ctx.t) => 'a) => mock = "post"
30
+ @module("msw") @scope("rest")
31
+ external delete: (string, (req<'params>, @uncurry ('z => 't), Ctx.t) => 'a) => mock = "delete"
32
+ }