@colisweb/rescript-toolkit 2.9.0 → 2.11.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.9.0",
3
+ "version": "2.11.0",
4
4
  "scripts": {
5
5
  "clean": "rescript clean",
6
6
  "build": "rescript build",
@@ -0,0 +1,28 @@
1
+ module type Config = {
2
+ module Request: Toolkit__Request2.Config
3
+ let key: Request.argument => array<string>
4
+ }
5
+
6
+ module Make = (Config: Config) => {
7
+ let key: Config.Request.argument => array<string> = Config.key
8
+
9
+ let use = (
10
+ ~options: option<Swr.fetcherOptions>=?,
11
+ key: option<Config.Request.argument>,
12
+ ): Toolkit__Hooks.fetcher<Config.Request.response> =>
13
+ Toolkit__Hooks.useFetcher(~options?, key->Option.map(Config.key), () => {
14
+ let key = key->Option.getExn
15
+
16
+ Config.Request.exec(key)->Promise.Js.fromResult
17
+ })
18
+
19
+ let useOptional = (
20
+ ~options: option<Swr.fetcherOptions>=?,
21
+ key: option<Config.Request.argument>,
22
+ ): Toolkit__Hooks.fetcher<option<Config.Request.response>> =>
23
+ Toolkit__Hooks.useOptionalFetcher(~options?, key->Option.map(Config.key), () => {
24
+ let key = key->Option.getExn
25
+
26
+ Config.Request.exec(key)->Promise.Js.fromResult
27
+ })
28
+ }
@@ -203,37 +203,3 @@ let delete = (
203
203
  | Some(c) =>
204
204
  request->Axios.Instance.deletec(url, c)->toResult(~mapError?, ~errorDecoder?, mapData)
205
205
  }
206
-
207
- module Error = {
208
- type rec errorType = [
209
- | #overlappingActivations(array<activation>)
210
- | #compileError(string)
211
- | #accessRefused
212
- | #unknown
213
- ]
214
- @decco and activation = {ruleTitle: string}
215
-
216
- let encoder: Decco.encoder<errorType> = _v => ""->Decco.stringToJson
217
-
218
- let decoder: Decco.decoder<errorType> = json => {
219
- let error = json->Obj.magic
220
- let value = switch error["code"] {
221
- | Some("OVERLAPPING_ACTIVATIONS") =>
222
- #overlappingActivations(
223
- error["overlapped"]->Obj.magic->Array.map(a => a->activation_decode->Result.getExn),
224
- )
225
-
226
- | Some("COMPILE_ERROR") => #compileError(error["compileError"]->Obj.magic)
227
- | Some("ACCESS_REFUSED") => #accessRefused
228
-
229
- | _ => #unknown
230
- }
231
-
232
- value->Ok
233
- }
234
-
235
- let codec: Decco.codec<errorType> = (encoder, decoder)
236
-
237
- @decco
238
- type t = @decco.codec(codec) errorType
239
- }
@@ -0,0 +1,12 @@
1
+ module type Config = {
2
+ type argument
3
+ type response
4
+ type error
5
+ let exec: argument => Promise.promise<
6
+ result<response, Axios2.WithResult.customError<error, Js.Json.t, 'header, 'req>>,
7
+ >
8
+ }
9
+
10
+ module Make = (Config: Config) => {
11
+ include Config
12
+ }
@@ -27,19 +27,10 @@ module MakeFeature = (C: Config) => {
27
27
  type t = config<C.argument>
28
28
 
29
29
  let exec = (var: C.input) =>
30
- (Axios.get(C.envUrl ++ C.featureName)
31
- |> Js.Promise.then_(res => res["data"]->Js.Promise.resolve)
32
- |> Js.Promise.then_(data =>
33
- switch t_decode(data) {
34
- | Result.Ok(feat) => Js.Promise.resolve(feat)
35
- | Result.Error(_err) =>
36
- %log.error(
37
- "unleash decode"
38
- ("err", _err)
39
- )
40
- Js.Promise.reject(Js.Exn.raiseError(C.featureName ++ " : parse error"))
41
- }
42
- )
43
- |> Promise.Js.fromBsPromise
44
- |> Promise.Js.toResult)->Promise.mapOk(C.exec(var))
30
+ Axios2.get(C.envUrl ++ C.featureName, ())
31
+ ->Promise.Js.toResult
32
+ ->Promise.flatMapOk(({data}) => {
33
+ Promise.resolved(t_decode(data))
34
+ })
35
+ ->Promise.mapOk(C.exec(var))
45
36
  }
@@ -0,0 +1,203 @@
1
+ type config
2
+
3
+ module Headers = {
4
+ type t
5
+
6
+ external fromObj: Js.t<{..}> => t = "%identity"
7
+ external fromDict: Js.Dict.t<string> => t = "%identity"
8
+ }
9
+
10
+ module CancelToken = {
11
+ type t
12
+
13
+ @module("axios")
14
+ external source: unit => t = "source"
15
+ }
16
+
17
+ type requestTransformer<'data, 'a, 'transformedData> = ('data, Js.t<'a>) => 'transformedData
18
+ type responseTransformer<'data, 'transformedData> = 'data => 'transformedData
19
+ type paramsSerializer<'params, 'serializedParams> = 'params => 'serializedParams
20
+ type auth = {
21
+ username: string,
22
+ password: string,
23
+ }
24
+ type proxy = {host: int, port: int, auth: auth}
25
+
26
+ type response<'data, 'header> = {
27
+ data: 'data,
28
+ status: int,
29
+ statusText: string,
30
+ headers: Js.t<'header>,
31
+ config: config,
32
+ }
33
+
34
+ type error<'responseData, 'responseHeader, 'request> = {
35
+ request: option<'request>,
36
+ response: option<response<'responseData, 'responseHeader>>,
37
+ message: string,
38
+ }
39
+
40
+ type adapter<'a, 'b, 'err> = config => Promise.Js.t<response<'a, 'b>, 'err>
41
+
42
+ @module("axios")
43
+ external isAxiosError: error<'a, 'b, 'c> => bool = "isAxiosError"
44
+
45
+ @obj
46
+ external makeConfig: (
47
+ ~url: string=?,
48
+ ~_method: string=?,
49
+ ~baseURL: string=?,
50
+ ~transformRequest: array<requestTransformer<'data, Js.t<'a>, 'tranformedData>>=?,
51
+ ~transformResponse: array<responseTransformer<'data, 'tranformedData>>=?,
52
+ ~headers: Headers.t=?,
53
+ ~params: Js.t<'params>=?,
54
+ ~paramsSerializer: paramsSerializer<'params, 'serializedParams>=?,
55
+ ~data: Js.t<'data>=?,
56
+ ~timeout: int=?,
57
+ ~withCredentials: bool=?,
58
+ ~adapter: adapter<'a, 'b, 'err>=?,
59
+ ~auth: auth=?,
60
+ ~responseType: string=?,
61
+ ~xsrfCookieName: string=?,
62
+ ~xsrfHeaderName: string=?,
63
+ ~onUploadProgress: 'uploadProgress => unit=?,
64
+ ~onDownloadProgress: 'downloadProgress => unit=?,
65
+ ~maxContentLength: int=?,
66
+ ~validateStatus: int => bool=?,
67
+ ~maxRedirects: int=?,
68
+ ~socketPath: string=?,
69
+ ~proxy: proxy=?,
70
+ ~cancelToken: CancelToken.t=?,
71
+ unit,
72
+ ) => config = ""
73
+
74
+ @module("axios")
75
+ external get: (string, ~config: config=?, unit) => Promise.Js.t<response<'data, 'headers>, 'err> =
76
+ "get"
77
+
78
+ @module("axios")
79
+ external post: (
80
+ string,
81
+ ~data: 'a,
82
+ ~config: config=?,
83
+ unit,
84
+ ) => Promise.Js.t<response<'data, 'headers>, 'err> = "post"
85
+
86
+ @module("axios")
87
+ external put: (
88
+ string,
89
+ ~data: 'a,
90
+ ~config: config=?,
91
+ unit,
92
+ ) => Promise.Js.t<response<'data, 'headers>, 'err> = "put"
93
+
94
+ @module("axios")
95
+ external patch: (
96
+ string,
97
+ ~data: 'a,
98
+ ~config: config=?,
99
+ unit,
100
+ ) => Promise.Js.t<response<'data, 'headers>, 'err> = "patch"
101
+
102
+ @module("axios")
103
+ external delete: (
104
+ string,
105
+ ~config: config=?,
106
+ unit,
107
+ ) => Promise.Js.t<response<'data, 'headers>, 'err> = "delete"
108
+
109
+ @module("axios")
110
+ external options: (
111
+ string,
112
+ ~config: config=?,
113
+ unit,
114
+ ) => Promise.Js.t<response<'data, 'headers>, 'err> = "options"
115
+
116
+ module Interceptors = {
117
+ @module("axios") @scope(("default", "interceptors", "request"))
118
+ external requestInterceptor: ('config => Promise.Js.t<'updatedConfig, 'error>) => unit = "use"
119
+
120
+ @module("axios") @scope(("default", "interceptors", "response"))
121
+ external responseInterceptor: ('response => Promise.Js.t<'updatedResponse, 'error>) => unit =
122
+ "use"
123
+ }
124
+
125
+ module WithResult = {
126
+ type customError<'apiError, 'response, 'headers, 'request> = [
127
+ | #default(error<'response, 'headers, 'request>)
128
+ | #decodeError(Decco.decodeError)
129
+ | #custom('apiError)
130
+ ]
131
+
132
+ type decodeData<'newData, 'err> = Js.Json.t => result<'newData, 'err>
133
+
134
+ type mapError<'a, 'response, 'headers, 'request> = error<
135
+ 'response,
136
+ 'headers,
137
+ 'request,
138
+ > => customError<'a, 'response, 'headers, 'request>
139
+
140
+ let toResult = (promise, ~mapError, ~decodeData) =>
141
+ promise
142
+ ->Promise.Js.toResult
143
+ ->Promise.mapError(err => mapError->Option.mapWithDefault(#default(err), fn => fn(err)))
144
+ ->Promise.flatMapOk(response => {
145
+ Promise.resolved(
146
+ switch decodeData(response.data) {
147
+ | Ok(_) as ok => ok
148
+ | Error(decodeError) => Error(#decodeError(decodeError))
149
+ },
150
+ )
151
+ })
152
+
153
+ let get = (
154
+ string,
155
+ ~config=?,
156
+ ~decodeData: decodeData<'newData, 'err>,
157
+ ~mapError: option<mapError<'a, 'response, 'headers, 'request>>=?,
158
+ unit,
159
+ ) => get(string, ~config?, unit)->toResult(~decodeData, ~mapError)
160
+
161
+ let post = (
162
+ string,
163
+ ~data,
164
+ ~config=?,
165
+ ~decodeData: decodeData<'newData, 'err>,
166
+ ~mapError: option<mapError<'a, 'response, 'headers, 'request>>=?,
167
+ unit,
168
+ ) => post(string, ~data, ~config?, unit)->toResult(~decodeData, ~mapError)
169
+
170
+ let put = (
171
+ string,
172
+ ~data,
173
+ ~config=?,
174
+ ~decodeData: decodeData<'newData, 'err>,
175
+ ~mapError: option<mapError<'a, 'response, 'headers, 'request>>=?,
176
+ unit,
177
+ ) => put(string, ~data, ~config?, unit)->toResult(~decodeData, ~mapError)
178
+
179
+ let patch = (
180
+ string,
181
+ ~data,
182
+ ~config=?,
183
+ ~decodeData: decodeData<'newData, 'err>,
184
+ ~mapError: option<mapError<'a, 'response, 'headers, 'request>>=?,
185
+ unit,
186
+ ) => patch(string, ~data, ~config?, unit)->toResult(~decodeData, ~mapError)
187
+
188
+ let delete = (
189
+ string,
190
+ ~config=?,
191
+ ~decodeData: decodeData<'newData, 'err>,
192
+ ~mapError: option<mapError<'a, 'response, 'headers, 'request>>=?,
193
+ unit,
194
+ ) => delete(string, ~config?, unit)->toResult(~decodeData, ~mapError)
195
+
196
+ let options = (
197
+ string,
198
+ ~config=?,
199
+ ~decodeData: decodeData<'newData, 'err>,
200
+ ~mapError: option<mapError<'a, 'response, 'headers, 'request>>=?,
201
+ unit,
202
+ ) => options(string, ~config?, unit)->toResult(~decodeData, ~mapError)
203
+ }