@pihanga2/core 0.3.6 → 0.3.8
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/REST-USAGE.md +613 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -1
- package/dist/index.js.map +1 -1
- package/dist/reducer.d.ts.map +1 -1
- package/dist/reducer.js +9 -0
- package/dist/reducer.js.map +1 -1
- package/package.json +3 -6
- package/src/index.ts +29 -13
- package/src/reducer.ts +18 -10
package/REST-USAGE.md
ADDED
|
@@ -0,0 +1,613 @@
|
|
|
1
|
+
<!-- markdownlint-disable MD013 -->
|
|
2
|
+
|
|
3
|
+
# REST Usage (PiRegister.{GET|POST|PUT|PATCH|DELETE})
|
|
4
|
+
|
|
5
|
+
This document is split into two parts:
|
|
6
|
+
|
|
7
|
+
* **Usage (what most library users need day-to-day)**
|
|
8
|
+
* **Debugging / internals (useful when something goes wrong)**
|
|
9
|
+
|
|
10
|
+
## Table of contents
|
|
11
|
+
|
|
12
|
+
* [Usage](#usage)
|
|
13
|
+
* [Mental model](#mental-model)
|
|
14
|
+
* [Common registration properties](#common-registration-properties)
|
|
15
|
+
* [URL templates and bindings](#url-templates-and-bindings)
|
|
16
|
+
* [Usage: GET (start here)](#usage-get-start-here)
|
|
17
|
+
* [Minimal typed GET example (no auth/context)](#minimal-typed-get-example-no-authcontext)
|
|
18
|
+
* [Advanced GET example (auth via context + origin + headers)](#advanced-get-example-auth-via-context--origin--headers)
|
|
19
|
+
* [Usage: Request context + auth (common pattern)](#usage-request-context--auth-common-pattern)
|
|
20
|
+
* [Usage: Progress (submitted/result/error) actions](#usage-progress-submittedresulterror-actions)
|
|
21
|
+
* [Usage: POST / PUT / PATCH (request bodies)](#usage-post--put--patch-request-bodies)
|
|
22
|
+
* [Common request body shape](#common-request-body-shape)
|
|
23
|
+
* [POST](#post)
|
|
24
|
+
* [PUT](#put)
|
|
25
|
+
* [PATCH](#patch)
|
|
26
|
+
* [Usage: DELETE](#usage-delete)
|
|
27
|
+
* [Usage: Error handling](#usage-error-handling)
|
|
28
|
+
* [Debugging / internals](#debugging--internals)
|
|
29
|
+
* [Where the code lives](#where-the-code-lives)
|
|
30
|
+
* [How it hooks into Redux](#how-it-hooks-into-redux)
|
|
31
|
+
* [Internal action types](#internal-action-types)
|
|
32
|
+
* [Response parsing](#response-parsing)
|
|
33
|
+
* [Notes / gotchas](#notes--gotchas)
|
|
34
|
+
|
|
35
|
+
## Usage
|
|
36
|
+
|
|
37
|
+
### Mental model
|
|
38
|
+
|
|
39
|
+
1. You register a REST handler, e.g. `register.GET({ ... })`.
|
|
40
|
+
2. It **listens** for a Redux action type (`trigger`).
|
|
41
|
+
3. When that action is dispatched, it builds a URL from your `url` template plus bindings from `request(...)`, optionally loads `context(...)` and applies `headers(...)`, then calls `fetch`.
|
|
42
|
+
4. On success, your `reply(...)` runs and typically dispatches your domain actions.
|
|
43
|
+
|
|
44
|
+
### Common registration properties
|
|
45
|
+
|
|
46
|
+
All verbs share the properties from `RegisterGenericProps` (`packages/core/src/rest/types.ts`):
|
|
47
|
+
|
|
48
|
+
| property | type | purpose |
|
|
49
|
+
| --- | --- | --- |
|
|
50
|
+
| `name` | `string` | Logical call name (used for internal bookkeeping/debugging). |
|
|
51
|
+
| `trigger` | `string` | Redux action type that starts the call. |
|
|
52
|
+
| `url` | `string` | URL template supporting bindings like `:id` and optional bindings like `?page`. |
|
|
53
|
+
| `origin?` | `string \| (action, state, ctxt) => string \| URL` | Base origin. Default is `window.location.href`. |
|
|
54
|
+
| `context?` | `(action, state) => Promise<C> \| null` | Async context (e.g. auth token, base URL). |
|
|
55
|
+
| `guard?` | `(action, state, dispatch, ctxt) => boolean` | Return `false` to skip the request. |
|
|
56
|
+
| `headers?` | `(action, state, ctxt) => Record<string,string>` | Request headers (auth, correlation IDs, etc.). |
|
|
57
|
+
| `reply` | `(state, content, dispatch, resultAction) => void` | Called on success (HTTP < 300). Dispatch domain actions here. |
|
|
58
|
+
| `error?` | `(state, errorAction, requestAction, dispatch) => S` | Called on non-2xx responses. Dispatch domain error actions here. |
|
|
59
|
+
|
|
60
|
+
### URL templates and bindings
|
|
61
|
+
|
|
62
|
+
Bindings are substituted into the `url` **path segments** and **query string**:
|
|
63
|
+
|
|
64
|
+
* `:name` = required binding. Missing it triggers an internal error.
|
|
65
|
+
* `?name` = optional binding. Missing it omits that query parameter.
|
|
66
|
+
|
|
67
|
+
Examples:
|
|
68
|
+
|
|
69
|
+
* `/1/artifacts/:id` requires `{ id: "..." }`
|
|
70
|
+
* `/1/orders?limit=?limit&page=?page` omits `limit` and/or `page` if not provided
|
|
71
|
+
|
|
72
|
+
Path bindings are URL-encoded.
|
|
73
|
+
|
|
74
|
+
## Usage: GET (start here)
|
|
75
|
+
|
|
76
|
+
GET registrations provide optional `request(...)` bindings (no request body).
|
|
77
|
+
|
|
78
|
+
### Minimal typed GET example (no auth/context)
|
|
79
|
+
|
|
80
|
+
```ts
|
|
81
|
+
import type {
|
|
82
|
+
Bindings,
|
|
83
|
+
DispatchF,
|
|
84
|
+
ErrorAction,
|
|
85
|
+
PiRegister,
|
|
86
|
+
ResultAction,
|
|
87
|
+
ReduxAction,
|
|
88
|
+
ReduxState,
|
|
89
|
+
register,
|
|
90
|
+
} from "@pihanga2/core"
|
|
91
|
+
|
|
92
|
+
type MyState = ReduxState & {}
|
|
93
|
+
|
|
94
|
+
type LoadThingAction = ReduxAction & {
|
|
95
|
+
id: string
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
type Thing = {
|
|
99
|
+
id: string
|
|
100
|
+
name: string
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
register((r: PiRegister) => {
|
|
104
|
+
r.GET<MyState, LoadThingAction, Thing>({
|
|
105
|
+
name: "loadThing",
|
|
106
|
+
trigger: "THING/LOAD",
|
|
107
|
+
url: "/v1/things/:id",
|
|
108
|
+
|
|
109
|
+
request: (action: LoadThingAction, _state: MyState): Bindings => ({
|
|
110
|
+
id: action.id,
|
|
111
|
+
}),
|
|
112
|
+
|
|
113
|
+
reply: (
|
|
114
|
+
_state: MyState,
|
|
115
|
+
content: Thing,
|
|
116
|
+
dispatch: DispatchF,
|
|
117
|
+
_result: ResultAction<LoadThingAction>,
|
|
118
|
+
): void => {
|
|
119
|
+
dispatch({ type: "THING/LOADED", thing: content })
|
|
120
|
+
},
|
|
121
|
+
|
|
122
|
+
error: (
|
|
123
|
+
state: MyState,
|
|
124
|
+
err: ErrorAction<LoadThingAction>,
|
|
125
|
+
_requestAction: LoadThingAction,
|
|
126
|
+
dispatch: DispatchF,
|
|
127
|
+
): MyState => {
|
|
128
|
+
dispatch({ type: "THING/LOAD_FAILED", cause: err })
|
|
129
|
+
return state
|
|
130
|
+
},
|
|
131
|
+
})
|
|
132
|
+
})
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
### Advanced GET example (auth via context + origin + headers)
|
|
136
|
+
|
|
137
|
+
```ts
|
|
138
|
+
import type {
|
|
139
|
+
Bindings,
|
|
140
|
+
DispatchF,
|
|
141
|
+
ErrorAction,
|
|
142
|
+
PiRegister,
|
|
143
|
+
ResultAction,
|
|
144
|
+
ReduxAction,
|
|
145
|
+
ReduxState,
|
|
146
|
+
register,
|
|
147
|
+
} from "@pihanga2/core"
|
|
148
|
+
|
|
149
|
+
type MyState = ReduxState
|
|
150
|
+
|
|
151
|
+
type LoadThingAction = ReduxAction & {
|
|
152
|
+
id: string
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
type Thing = {
|
|
156
|
+
id: string
|
|
157
|
+
name: string
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
type MyAuthContext = {
|
|
161
|
+
apiOrigin: string
|
|
162
|
+
token: string
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
register((r: PiRegister) => {
|
|
166
|
+
r.GET<MyState, LoadThingAction, Thing, MyAuthContext>({
|
|
167
|
+
name: "loadThing",
|
|
168
|
+
trigger: "THING/LOAD",
|
|
169
|
+
url: "/v1/things/:id",
|
|
170
|
+
|
|
171
|
+
context: async (
|
|
172
|
+
_action: LoadThingAction,
|
|
173
|
+
_state: MyState,
|
|
174
|
+
): Promise<MyAuthContext> => ({
|
|
175
|
+
apiOrigin: "https://api.example.com",
|
|
176
|
+
token: "...",
|
|
177
|
+
}),
|
|
178
|
+
|
|
179
|
+
origin: (
|
|
180
|
+
_action: LoadThingAction,
|
|
181
|
+
_state: MyState,
|
|
182
|
+
ctxt: MyAuthContext,
|
|
183
|
+
): string => ctxt.apiOrigin,
|
|
184
|
+
|
|
185
|
+
headers: (
|
|
186
|
+
_action: LoadThingAction,
|
|
187
|
+
_state: MyState,
|
|
188
|
+
ctxt: MyAuthContext,
|
|
189
|
+
): Record<string, string> => ({
|
|
190
|
+
Authorization: `Bearer ${ctxt.token}`,
|
|
191
|
+
}),
|
|
192
|
+
|
|
193
|
+
request: (action: LoadThingAction, _state: MyState): Bindings => ({
|
|
194
|
+
id: action.id,
|
|
195
|
+
}),
|
|
196
|
+
|
|
197
|
+
reply: (
|
|
198
|
+
_state: MyState,
|
|
199
|
+
content: Thing,
|
|
200
|
+
dispatch: DispatchF,
|
|
201
|
+
_result: ResultAction<LoadThingAction>,
|
|
202
|
+
): void => {
|
|
203
|
+
dispatch({ type: "THING/LOADED", thing: content })
|
|
204
|
+
},
|
|
205
|
+
|
|
206
|
+
error: (
|
|
207
|
+
state: MyState,
|
|
208
|
+
err: ErrorAction<LoadThingAction>,
|
|
209
|
+
_requestAction: LoadThingAction,
|
|
210
|
+
dispatch: DispatchF,
|
|
211
|
+
): MyState => {
|
|
212
|
+
dispatch({ type: "THING/LOAD_FAILED", cause: err })
|
|
213
|
+
return state
|
|
214
|
+
},
|
|
215
|
+
})
|
|
216
|
+
})
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
### Worked example (GET with path binding) from `packages/ivcap`
|
|
220
|
+
|
|
221
|
+
From `packages/ivcap/src/artifact/artifact.get.ts`:
|
|
222
|
+
|
|
223
|
+
```ts
|
|
224
|
+
import type {
|
|
225
|
+
Bindings,
|
|
226
|
+
DispatchF,
|
|
227
|
+
PiRegister,
|
|
228
|
+
ResultAction,
|
|
229
|
+
ReduxAction,
|
|
230
|
+
ReduxState,
|
|
231
|
+
register,
|
|
232
|
+
} from "@pihanga2/core"
|
|
233
|
+
|
|
234
|
+
register((r: PiRegister) => {
|
|
235
|
+
r.GET<ReduxState, ReduxAction & LoadArtifactRecordEvent, any>({
|
|
236
|
+
...CommonProps("getArtifactRecord"),
|
|
237
|
+
url: "/1/artifacts/:id",
|
|
238
|
+
trigger: ACTION_TYPES.LOAD_RECORD,
|
|
239
|
+
request: (
|
|
240
|
+
action: ReduxAction & LoadArtifactRecordEvent,
|
|
241
|
+
_state: ReduxState,
|
|
242
|
+
): Bindings => ({ id: action.id }),
|
|
243
|
+
reply: (
|
|
244
|
+
_state: ReduxState,
|
|
245
|
+
content: any,
|
|
246
|
+
dispatch: DispatchF,
|
|
247
|
+
result: ResultAction<ReduxAction & LoadArtifactRecordEvent>,
|
|
248
|
+
): void => {
|
|
249
|
+
const ev: ArtifactRecordEvent = { artifact: toArtifactRecord(content) }
|
|
250
|
+
dispatchEvent(ev, ACTION_TYPES.RECORD, dispatch, result.request)
|
|
251
|
+
},
|
|
252
|
+
})
|
|
253
|
+
})
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
## Usage: Request context + auth (common pattern)
|
|
257
|
+
|
|
258
|
+
For authenticated APIs, a common pattern is:
|
|
259
|
+
|
|
260
|
+
* `context()` loads auth/base-url asynchronously (token, API URL)
|
|
261
|
+
* `origin()` sets the base URL from that context
|
|
262
|
+
* `headers()` adds auth headers (e.g. `Authorization: Bearer ...`)
|
|
263
|
+
|
|
264
|
+
The `packages/ivcap` module uses a reusable helper (`packages/ivcap/src/common.ts`):
|
|
265
|
+
|
|
266
|
+
```ts
|
|
267
|
+
export const CommonProps = (name: string) => ({
|
|
268
|
+
name,
|
|
269
|
+
context: () => GetOAuthContext(),
|
|
270
|
+
origin: (_a: any, _s: any, ctxt: OAuthContextT) => ctxt.ivcapURL,
|
|
271
|
+
headers: (_a: any, _s: any, ctxt: OAuthContextT) => ({
|
|
272
|
+
Authorization: `Bearer ${ctxt.token}`,
|
|
273
|
+
}),
|
|
274
|
+
error: restErrorHandling(`ivcap-api:${name}`),
|
|
275
|
+
})
|
|
276
|
+
```
|
|
277
|
+
|
|
278
|
+
## Usage: Progress (submitted/result/error) actions
|
|
279
|
+
|
|
280
|
+
Every REST registration reports lifecycle/progress via **additional Redux actions**. This is useful for:
|
|
281
|
+
|
|
282
|
+
* showing spinners (request submitted)
|
|
283
|
+
* logging / debugging
|
|
284
|
+
* building a generic request-tracker in state
|
|
285
|
+
|
|
286
|
+
The base action namespaces are defined in:
|
|
287
|
+
|
|
288
|
+
* `packages/core/src/rest/types.ts` for **POST/PUT/PATCH/DELETE** (`Domain = "pi/rest"`)
|
|
289
|
+
* `packages/core/src/rest/get.ts` for **GET** (`Domain = "pi/rest"` but `pi/rest/get` subdomain)
|
|
290
|
+
|
|
291
|
+
### Base action types (POST/PUT/PATCH/DELETE)
|
|
292
|
+
|
|
293
|
+
In `packages/core/src/rest/types.ts`:
|
|
294
|
+
|
|
295
|
+
```ts
|
|
296
|
+
export const Domain = "pi/rest"
|
|
297
|
+
export const ACTION_TYPES = registerActions(Domain, [
|
|
298
|
+
"POST_SUBMITTED",
|
|
299
|
+
"POST_RESULT",
|
|
300
|
+
"POST_ERROR",
|
|
301
|
+
"POST_INTERNAL_ERROR",
|
|
302
|
+
"PUT_SUBMITTED",
|
|
303
|
+
"PUT_RESULT",
|
|
304
|
+
"PUT_ERROR",
|
|
305
|
+
"PUT_INTERNAL_ERROR",
|
|
306
|
+
"PATCH_SUBMITTED",
|
|
307
|
+
"PATCH_RESULT",
|
|
308
|
+
"PATCH_ERROR",
|
|
309
|
+
"PATCH_INTERNAL_ERROR",
|
|
310
|
+
"DELETE_SUBMITTED",
|
|
311
|
+
"DELETE_RESULT",
|
|
312
|
+
"DELETE_ERROR",
|
|
313
|
+
"DELETE_INTERNAL_ERROR",
|
|
314
|
+
"UNAUTHORISED_ERROR",
|
|
315
|
+
"PERMISSION_DENIED_ERROR",
|
|
316
|
+
"NOT_FOUND_ERROR",
|
|
317
|
+
"ERROR",
|
|
318
|
+
"CONTEXT_ERROR",
|
|
319
|
+
])
|
|
320
|
+
```
|
|
321
|
+
|
|
322
|
+
For a specific handler registration, the REST worker **specialises** these by appending the handler’s `name`.
|
|
323
|
+
|
|
324
|
+
For POST/PUT/PATCH/DELETE, the current implementation appends using `:${name}`, e.g.:
|
|
325
|
+
|
|
326
|
+
* `pi/rest/POST_SUBMITTED:createOrder`
|
|
327
|
+
* `pi/rest/POST_RESULT:createOrder`
|
|
328
|
+
* `pi/rest/POST_ERROR:createOrder`
|
|
329
|
+
|
|
330
|
+
### Base action types (GET)
|
|
331
|
+
|
|
332
|
+
GET uses a different action namespace in `packages/core/src/rest/get.ts`:
|
|
333
|
+
|
|
334
|
+
* `pi/rest/get/submitted`
|
|
335
|
+
* `pi/rest/get/result`
|
|
336
|
+
* `pi/rest/get/error`
|
|
337
|
+
* `pi/rest/get/internal_error`
|
|
338
|
+
|
|
339
|
+
and specialises them by appending `/${name}`.
|
|
340
|
+
|
|
341
|
+
For example, from `packages/core/src/rest/get.ts`:
|
|
342
|
+
|
|
343
|
+
```ts
|
|
344
|
+
const submitType = `${ACTION_TYPES.SUBMITTED}/${name}`
|
|
345
|
+
const resultType = `${ACTION_TYPES.RESULT}/${name}`
|
|
346
|
+
const errorType = `${ACTION_TYPES.ERROR}/${name}`
|
|
347
|
+
const intErrorType = `${ACTION_TYPES.INTERNAL_ERROR}/${name}`
|
|
348
|
+
```
|
|
349
|
+
|
|
350
|
+
### Payload shapes
|
|
351
|
+
|
|
352
|
+
These lifecycle actions include useful payload:
|
|
353
|
+
|
|
354
|
+
* **submitted** actions use `SubmitAction` (includes `requestID`, `url`, `bindings`)
|
|
355
|
+
* **result** actions use `ResultAction<A>` (includes `statusCode`, `content`, `contentType`, `mimeType`, `size`, `headers`, plus `url` and original `request` action)
|
|
356
|
+
* **error** actions use `ErrorAction<A>` (similar to result, plus an `ErrorKind` classification)
|
|
357
|
+
|
|
358
|
+
If you want to track progress in your app state, these are the actions to listen for.
|
|
359
|
+
|
|
360
|
+
## Usage: POST / PUT / PATCH (request bodies)
|
|
361
|
+
|
|
362
|
+
POST/PUT/PATCH all have a request body.
|
|
363
|
+
|
|
364
|
+
They share the same `request(...)` return type:
|
|
365
|
+
|
|
366
|
+
```ts
|
|
367
|
+
type PoPuPaRequest = {
|
|
368
|
+
body: any
|
|
369
|
+
contentType?: string
|
|
370
|
+
bindings?: Bindings
|
|
371
|
+
}
|
|
372
|
+
```
|
|
373
|
+
|
|
374
|
+
### Common request body shape
|
|
375
|
+
|
|
376
|
+
Common patterns:
|
|
377
|
+
|
|
378
|
+
* Use `bindings` when your `url` contains `:id` or query bindings.
|
|
379
|
+
* For JSON bodies: set `contentType: "application/json"` (or omit it and return an object body).
|
|
380
|
+
* For binary bodies: set `contentType` appropriately and pass an `ArrayBuffer`/`Blob`.
|
|
381
|
+
|
|
382
|
+
### POST
|
|
383
|
+
|
|
384
|
+
POST creates a new server-side resource.
|
|
385
|
+
|
|
386
|
+
Minimal typed pattern:
|
|
387
|
+
|
|
388
|
+
```ts
|
|
389
|
+
import type {
|
|
390
|
+
DispatchF,
|
|
391
|
+
PiRegister,
|
|
392
|
+
ReduxAction,
|
|
393
|
+
ReduxState,
|
|
394
|
+
register,
|
|
395
|
+
} from "@pihanga2/core"
|
|
396
|
+
|
|
397
|
+
type MyState = ReduxState
|
|
398
|
+
|
|
399
|
+
type Thing = { id: string; name: string }
|
|
400
|
+
type ThingCreatePayload = { name: string }
|
|
401
|
+
|
|
402
|
+
type CreateThingAction = ReduxAction & {
|
|
403
|
+
payload: ThingCreatePayload
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
register((r: PiRegister) => {
|
|
407
|
+
r.POST<MyState, CreateThingAction, Thing>({
|
|
408
|
+
name: "createThing",
|
|
409
|
+
trigger: "THING/CREATE",
|
|
410
|
+
url: "/v1/things",
|
|
411
|
+
request: (action: CreateThingAction, _state: MyState) => ({
|
|
412
|
+
body: action.payload,
|
|
413
|
+
contentType: "application/json",
|
|
414
|
+
}),
|
|
415
|
+
reply: (_state: MyState, content: Thing, dispatch: DispatchF) => {
|
|
416
|
+
dispatch({ type: "THING/CREATED", thing: content })
|
|
417
|
+
},
|
|
418
|
+
})
|
|
419
|
+
}
|
|
420
|
+
```
|
|
421
|
+
|
|
422
|
+
Real example (POST JSON) from `packages/ivcap`:
|
|
423
|
+
|
|
424
|
+
### Example (POST JSON) from `packages/ivcap`
|
|
425
|
+
|
|
426
|
+
```ts
|
|
427
|
+
import type {
|
|
428
|
+
DispatchF,
|
|
429
|
+
PiRegister,
|
|
430
|
+
ResultAction,
|
|
431
|
+
ReduxAction,
|
|
432
|
+
ReduxState,
|
|
433
|
+
register,
|
|
434
|
+
} from "@pihanga2/core"
|
|
435
|
+
|
|
436
|
+
register((r: PiRegister) => {
|
|
437
|
+
r.POST<ReduxState, ReduxAction & CreateOrderEvent, any>({
|
|
438
|
+
...CommonProps("createOrder"),
|
|
439
|
+
url: "/1/orders",
|
|
440
|
+
trigger: ORDER_ACTION.CREATE,
|
|
441
|
+
request: (
|
|
442
|
+
action: ReduxAction & CreateOrderEvent,
|
|
443
|
+
_state: ReduxState,
|
|
444
|
+
) => ({
|
|
445
|
+
body: {
|
|
446
|
+
name: action.name,
|
|
447
|
+
service: action.serviceID,
|
|
448
|
+
parameters: action.parameters,
|
|
449
|
+
},
|
|
450
|
+
contentType: "application/json",
|
|
451
|
+
}),
|
|
452
|
+
reply: (
|
|
453
|
+
_state: ReduxState,
|
|
454
|
+
content: any,
|
|
455
|
+
dispatch: DispatchF,
|
|
456
|
+
result: ResultAction<ReduxAction & CreateOrderEvent>,
|
|
457
|
+
): void => {
|
|
458
|
+
const ev: OrderCreatedEvent = {
|
|
459
|
+
refID: result.request.refID,
|
|
460
|
+
order: toOrderRecord(content),
|
|
461
|
+
}
|
|
462
|
+
dispatchEvent(ev, ORDER_ACTION.CREATED, dispatch, result.request)
|
|
463
|
+
},
|
|
464
|
+
})
|
|
465
|
+
})
|
|
466
|
+
```
|
|
467
|
+
|
|
468
|
+
### PUT
|
|
469
|
+
|
|
470
|
+
PUT typically replaces a resource at a known URL.
|
|
471
|
+
|
|
472
|
+
```ts
|
|
473
|
+
register.PUT<MyState, UpdateThingAction, Thing>({
|
|
474
|
+
name: "updateThing",
|
|
475
|
+
trigger: "THING/UPDATE",
|
|
476
|
+
url: "/v1/things/:id",
|
|
477
|
+
request: (action: UpdateThingAction, _state: MyState) => ({
|
|
478
|
+
bindings: { id: action.id },
|
|
479
|
+
body: action.payload,
|
|
480
|
+
contentType: "application/json",
|
|
481
|
+
}),
|
|
482
|
+
reply: (_state: MyState, content: Thing, dispatch: DispatchF) => {
|
|
483
|
+
dispatch({ type: "THING/UPDATED", thing: content })
|
|
484
|
+
},
|
|
485
|
+
})
|
|
486
|
+
```
|
|
487
|
+
|
|
488
|
+
### PATCH
|
|
489
|
+
|
|
490
|
+
PATCH typically applies a partial update.
|
|
491
|
+
|
|
492
|
+
```ts
|
|
493
|
+
register((r: PiRegister) => {
|
|
494
|
+
r.PATCH<MyState, PatchThingAction, Thing>({
|
|
495
|
+
name: "patchThing",
|
|
496
|
+
trigger: "THING/PATCH",
|
|
497
|
+
url: "/v1/things/:id",
|
|
498
|
+
request: (action: PatchThingAction, _state: MyState) => ({
|
|
499
|
+
bindings: { id: action.id },
|
|
500
|
+
body: action.patch,
|
|
501
|
+
contentType: "application/json",
|
|
502
|
+
}),
|
|
503
|
+
reply: (_state: MyState, content: Thing, dispatch: DispatchF) => {
|
|
504
|
+
dispatch({ type: "THING/PATCHED", thing: content })
|
|
505
|
+
},
|
|
506
|
+
})
|
|
507
|
+
})
|
|
508
|
+
```
|
|
509
|
+
|
|
510
|
+
## Usage: DELETE
|
|
511
|
+
|
|
512
|
+
DELETE is bindings-only (like GET) but uses method `DELETE`.
|
|
513
|
+
|
|
514
|
+
```ts
|
|
515
|
+
import type {
|
|
516
|
+
Bindings,
|
|
517
|
+
DispatchF,
|
|
518
|
+
PiRegister,
|
|
519
|
+
ResultAction,
|
|
520
|
+
ReduxAction,
|
|
521
|
+
ReduxState,
|
|
522
|
+
register,
|
|
523
|
+
} from "@pihanga2/core"
|
|
524
|
+
|
|
525
|
+
type MyState = ReduxState
|
|
526
|
+
type DeleteThingAction = ReduxAction & { id: string }
|
|
527
|
+
|
|
528
|
+
register((r: PiRegister) => {
|
|
529
|
+
r.DELETE<MyState, DeleteThingAction, unknown>({
|
|
530
|
+
name: "deleteThing",
|
|
531
|
+
trigger: "THING/DELETE",
|
|
532
|
+
url: "/v1/things/:id",
|
|
533
|
+
request: (action: DeleteThingAction, _state: MyState): Bindings => ({
|
|
534
|
+
id: action.id,
|
|
535
|
+
}),
|
|
536
|
+
reply: (
|
|
537
|
+
_state: MyState,
|
|
538
|
+
_content: unknown,
|
|
539
|
+
dispatch: DispatchF,
|
|
540
|
+
result: ResultAction<DeleteThingAction>,
|
|
541
|
+
): void => {
|
|
542
|
+
dispatch({ type: "THING/DELETED", id: result.request.id })
|
|
543
|
+
},
|
|
544
|
+
})
|
|
545
|
+
})
|
|
546
|
+
```
|
|
547
|
+
|
|
548
|
+
## Usage: Error handling
|
|
549
|
+
|
|
550
|
+
On non-2xx responses, the REST module dispatches an `ErrorAction` containing:
|
|
551
|
+
|
|
552
|
+
* `statusCode`
|
|
553
|
+
* `content` (parsed body)
|
|
554
|
+
* `error: ErrorKind` (401/403/404 mapped; else `Other`)
|
|
555
|
+
* `url`
|
|
556
|
+
* `request` (the original trigger action)
|
|
557
|
+
|
|
558
|
+
You can attach an `error(...)` handler per call, or centralize handling.
|
|
559
|
+
|
|
560
|
+
If you want a reusable auth+error strategy, see [Usage: Request context + auth (common pattern)](#usage-request-context--auth-common-pattern).
|
|
561
|
+
|
|
562
|
+
## Debugging / internals
|
|
563
|
+
|
|
564
|
+
### Where the code lives
|
|
565
|
+
|
|
566
|
+
* `packages/core/src/rest/get.ts`
|
|
567
|
+
* `packages/core/src/rest/postPutPatch.ts`
|
|
568
|
+
* `packages/core/src/rest/delete.ts`
|
|
569
|
+
* shared plumbing: `packages/core/src/rest/utils.ts`
|
|
570
|
+
* types: `packages/core/src/rest/types.ts`
|
|
571
|
+
|
|
572
|
+
### How it hooks into Redux
|
|
573
|
+
|
|
574
|
+
Pihanga’s `PiReducer` is a small registration layer on top of Redux Toolkit’s store (`packages/core/src/reducer.ts`).
|
|
575
|
+
|
|
576
|
+
When you call `register.GET({...})`, internally it registers reducers for:
|
|
577
|
+
|
|
578
|
+
* the trigger action (`trigger`)
|
|
579
|
+
* the internal success action (which calls your `reply(...)`)
|
|
580
|
+
* optionally the internal error action (which calls your `error(...)`)
|
|
581
|
+
|
|
582
|
+
So the REST system is effectively “middleware implemented as reducers”: it reacts to actions and dispatches more actions.
|
|
583
|
+
|
|
584
|
+
### Internal action types
|
|
585
|
+
|
|
586
|
+
You typically don’t dispatch these directly, but they’re helpful to know when debugging Redux logs.
|
|
587
|
+
|
|
588
|
+
GET (`packages/core/src/rest/get.ts`) creates types like:
|
|
589
|
+
|
|
590
|
+
* `pi/rest/get/submitted/${name}`
|
|
591
|
+
* `pi/rest/get/result/${name}`
|
|
592
|
+
* `pi/rest/get/error/${name}`
|
|
593
|
+
* `pi/rest/get/internal_error/${name}`
|
|
594
|
+
|
|
595
|
+
POST/PUT/PATCH/DELETE currently create types like:
|
|
596
|
+
|
|
597
|
+
* `pi/rest/post_submitted:${name}`
|
|
598
|
+
* `pi/rest/put_result:${name}`
|
|
599
|
+
* `pi/rest/delete_error:${name}`
|
|
600
|
+
|
|
601
|
+
### Response parsing
|
|
602
|
+
|
|
603
|
+
Response parsing is in `packages/core/src/rest/utils.ts`:
|
|
604
|
+
|
|
605
|
+
* `application/json` => `response.json()` => `RestContentType.Object`
|
|
606
|
+
* `application/jose` or `text/*` => `response.text()` => `RestContentType.Text`
|
|
607
|
+
* otherwise => `response.blob()` => `RestContentType.Blob`
|
|
608
|
+
|
|
609
|
+
### Notes / gotchas
|
|
610
|
+
|
|
611
|
+
* `reply(...)` runs in response to an internal action; keep it fast and dispatch domain actions.
|
|
612
|
+
* If `context(...)` is async, the current implementation calls `handleEvent(null as S, ...)` (see `registerCommon`), so don’t rely on the `state` parameter inside `guard/headers/origin` when using `context`.
|
|
613
|
+
* Prefer `?name` bindings for optional query parameters so they get omitted cleanly.
|
package/dist/index.d.ts
CHANGED
|
@@ -56,6 +56,21 @@ export type StartProps = {
|
|
|
56
56
|
ignoredActions?: string[];
|
|
57
57
|
ignoredActionPaths?: string[];
|
|
58
58
|
ignoredStatePaths?: string[];
|
|
59
|
+
/**
|
|
60
|
+
* Predicate used by Redux Toolkit's `serializableCheck`.
|
|
61
|
+
*
|
|
62
|
+
* By default RTK considers only "plain" JS values/objects serializable.
|
|
63
|
+
* If your app intentionally carries extra types (e.g. `Date`) in actions/state,
|
|
64
|
+
* you can extend this function:
|
|
65
|
+
*
|
|
66
|
+
* ```ts
|
|
67
|
+
* isSerializable: (v) => isPlain(v) || v instanceof Date
|
|
68
|
+
* ```
|
|
69
|
+
*
|
|
70
|
+
* This is useful for allowing things like Luxon DateTime, Map/Set wrappers,
|
|
71
|
+
* etc. (Prefer normalizing to plain data where possible.)
|
|
72
|
+
*/
|
|
73
|
+
isSerializable?: (value: unknown) => boolean;
|
|
59
74
|
};
|
|
60
75
|
export declare function start<S extends Partial<ReduxState>>(initialState: S, inits?: ((register: PiRegister) => void)[], props?: StartProps): PiRegister;
|
|
61
76
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAGA,OAAO,EACL,SAAS,EACT,SAAS,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import { addCard, addCardComponent, registerMetacard, updateOrRegisterCard, } from "./register_cards";
|
|
3
3
|
import { createReducer } from "./reducer";
|
|
4
4
|
import { ON_INIT_ACTION, currentRoute, init as routerInit } from "./router";
|
|
5
|
-
import { configureStore } from "@reduxjs/toolkit";
|
|
5
|
+
import { configureStore, isPlain } from "@reduxjs/toolkit";
|
|
6
6
|
//import monitorReducerEnhancer from "./monitor_enhancer"
|
|
7
7
|
import { getLogger } from "./logger";
|
|
8
8
|
import { registerDELETE, registerGET, registerPATCH, registerPOST, registerPUT, } from "./rest";
|
|
@@ -81,6 +81,7 @@ export const DEFAULT_REDUX_STATE = {
|
|
|
81
81
|
pihanga: {},
|
|
82
82
|
};
|
|
83
83
|
export function start(initialState, inits = [], props = {}) {
|
|
84
|
+
var _a;
|
|
84
85
|
const state = {
|
|
85
86
|
...DEFAULT_REDUX_STATE,
|
|
86
87
|
...initialState,
|
|
@@ -108,6 +109,7 @@ export function start(initialState, inits = [], props = {}) {
|
|
|
108
109
|
"data",
|
|
109
110
|
].concat(props.ignoredActionPaths || []);
|
|
110
111
|
const ignoredPaths = ["cause.content"].concat(props.ignoredStatePaths || []);
|
|
112
|
+
const isSerializable = (_a = props.isSerializable) !== null && _a !== void 0 ? _a : isPlain;
|
|
111
113
|
const store = configureStore({
|
|
112
114
|
reducer,
|
|
113
115
|
preloadedState: state, // keep type checking happy
|
|
@@ -119,6 +121,7 @@ export function start(initialState, inits = [], props = {}) {
|
|
|
119
121
|
ignoredPaths,
|
|
120
122
|
ignoreState: props.disableSerializableStateCheck,
|
|
121
123
|
ignoreAction: props.disableSerializableActionCheck,
|
|
124
|
+
isSerializable,
|
|
122
125
|
},
|
|
123
126
|
}),
|
|
124
127
|
});
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,QAAQ,MAAM,kBAAkB,CAAC;
|
|
1
|
+
{"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,YAAY,CAAC,SAAS,CAAC;QAC7B,KAAK,EAAE,aAAa,CAAC,SAAS,CAAC;QAC/B,MAAM,EAAE,cAAc,CAAC,SAAS,CAAC;KAClC,CAAC;IACF,YAAY,CAAC,QAAQ,CAAC,CAAC;IAEvB,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC;IAElC,SAAS,CAAC,QAAQ,CAAC,EAAC,IAAI,EAAE,cAAc,EAAC,CAAC,CAAC;IAE3C,MAAM,QAAQ,GAAG,aAAa,CAAC,KAAK,CAAC,CAAC;IACtC,MAAM,IAAI,GAAG,QAAQ,CAAC,UAAU,CAAC,QAAQ,CAAC,cAAc,CAAC,MAAM,CAAE,CAAC,CAAC;IACnE,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;IAEtB,YAAY,CAAC,QAAQ,CAAC,CAAC;IACvB,OAAO,QAAQ,CAAC;AAClB,CAAC"}
|
package/dist/reducer.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"reducer.d.ts","sourceRoot":"","sources":["../src/reducer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,MAAM,EAAE,OAAO,EAAC,MAAM,kBAAkB,CAAC;AACjD,OAAO,EAEL,SAAS,EAOT,UAAU,EACX,MAAM,SAAS,CAAC;AAKjB,OAAO,EAAC,QAAQ,EAAC,MAAM,OAAO,CAAC;AAqB/B,wBAAgB,aAAa,CAC3B,YAAY,EAAE,UAAU,EACxB,UAAU,EAAE,QAAQ,CAAC,GAAG,CAAC,GACxB,CAAC,OAAO,CAAC,UAAU,EAAE,MAAM,CAAC,EAAE,SAAS,CAAC,
|
|
1
|
+
{"version":3,"file":"reducer.d.ts","sourceRoot":"","sources":["../src/reducer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,MAAM,EAAE,OAAO,EAAC,MAAM,kBAAkB,CAAC;AACjD,OAAO,EAEL,SAAS,EAOT,UAAU,EACX,MAAM,SAAS,CAAC;AAKjB,OAAO,EAAC,QAAQ,EAAC,MAAM,OAAO,CAAC;AAqB/B,wBAAgB,aAAa,CAC3B,YAAY,EAAE,UAAU,EACxB,UAAU,EAAE,QAAQ,CAAC,GAAG,CAAC,GACxB,CAAC,OAAO,CAAC,UAAU,EAAE,MAAM,CAAC,EAAE,SAAS,CAAC,CAwH1C"}
|
package/dist/reducer.js
CHANGED
|
@@ -15,10 +15,19 @@ export function createReducer(initialState, dispatcher) {
|
|
|
15
15
|
setTimeout(() => dispatcher(a), 0);
|
|
16
16
|
};
|
|
17
17
|
const reducer = (state, action) => {
|
|
18
|
+
var _a;
|
|
18
19
|
const s = state || initialState;
|
|
19
20
|
const ra = mappings[action.type];
|
|
20
21
|
const rany = mappings["*"];
|
|
21
22
|
if ((!ra || ra.length === 0) && (!rany || rany.length === 0)) {
|
|
23
|
+
const ra = (_a = s.pihanga) === null || _a === void 0 ? void 0 : _a.reducers;
|
|
24
|
+
if (ra && ra.length > 0) {
|
|
25
|
+
return produce(s, (draft) => {
|
|
26
|
+
if (draft.pihanga) {
|
|
27
|
+
draft.pihanga.reducers = [];
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
}
|
|
22
31
|
return s;
|
|
23
32
|
}
|
|
24
33
|
const nextState = produce(s, (draft) => {
|
package/dist/reducer.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"reducer.js","sourceRoot":"","sources":["../src/reducer.ts"],"names":[],"mappings":"AAYA,OAAO,EAAC,OAAO,EAAC,MAAM,OAAO,CAAC;AAC9B,OAAO,EAAC,iBAAiB,EAAE,mBAAmB,EAAC,MAAM,QAAQ,CAAC;AAC9D,OAAO,UAAU,MAAM,eAAe,CAAC;AACvC,OAAO,EAAC,SAAS,EAAC,MAAM,UAAU,CAAC;AAInC,MAAM,MAAM,GAAG,SAAS,CAAC,SAAS,CAAC,CAAC;AAkBpC,MAAM,UAAU,aAAa,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C;QACX,CAAC;IACH,CAAC;IACD,OAAO,SAAS,CAAC;AACnB,CAAC;AAED,SAAS,YAAY,CAAC,GAAuB;IAC3C,IAAI,CAAC,GAAG;QAAE,OAAO,KAAK,CAAC;IACvB,MAAM,CAAC,GAAG,GAAG,CAAC,KAAK,CAAC,gDAAgD,CAAC,CAAC;IACtE,IAAI,CAAC,EAAE,CAAC;QACN,MAAM,EAAE,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;QAChB,MAAM,IAAI,GAAG,CAAC,CAAC,EAAE,CAAC,UAAU,CAAC,GAAG,CAAC,IAAI,EAAE,KAAK,cAAc,CAAC,CAAC;QAC5D,OAAO,IAAI,CAAC;IACd,CAAC;IACD,OAAO,IAAI,CAAC;AACd,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pihanga2/core",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.8",
|
|
4
4
|
"private": false,
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"author": "max.ott@data61.csiro.au",
|
|
8
8
|
"files": [
|
|
9
9
|
"dist",
|
|
10
|
-
"src"
|
|
10
|
+
"src",
|
|
11
|
+
"REST-USAGE.md"
|
|
11
12
|
],
|
|
12
13
|
"main": "./dist/index.js",
|
|
13
14
|
"keywords": [
|
|
@@ -24,10 +25,6 @@
|
|
|
24
25
|
"test:run": "vitest run",
|
|
25
26
|
"test:watch": "vitest"
|
|
26
27
|
},
|
|
27
|
-
"publishConfig": {
|
|
28
|
-
"access": "public",
|
|
29
|
-
"registry": "https://registry.npmjs.org/"
|
|
30
|
-
},
|
|
31
28
|
"dependencies": {
|
|
32
29
|
"@reduxjs/toolkit": "^2.2.6",
|
|
33
30
|
"deep-equal": "^2.2.3",
|
package/src/index.ts
CHANGED
|
@@ -8,15 +8,12 @@ import {
|
|
|
8
8
|
ReduxAction,
|
|
9
9
|
ReduxState,
|
|
10
10
|
PiReducer,
|
|
11
|
-
RegisterCardF,
|
|
12
|
-
MetaCardMapperF,
|
|
13
11
|
PiRegisterMetaCard,
|
|
14
12
|
PiMapProps,
|
|
15
13
|
WindowProps,
|
|
16
14
|
GenericCardParameterT,
|
|
17
15
|
} from "./types";
|
|
18
16
|
import {
|
|
19
|
-
createCardDeclaration,
|
|
20
17
|
addCard,
|
|
21
18
|
addCardComponent,
|
|
22
19
|
registerMetacard,
|
|
@@ -25,7 +22,7 @@ import {
|
|
|
25
22
|
import {createReducer} from "./reducer";
|
|
26
23
|
import {ON_INIT_ACTION, currentRoute, init as routerInit} from "./router";
|
|
27
24
|
|
|
28
|
-
import {configureStore} from "@reduxjs/toolkit";
|
|
25
|
+
import {configureStore, isPlain} from "@reduxjs/toolkit";
|
|
29
26
|
|
|
30
27
|
//import monitorReducerEnhancer from "./monitor_enhancer"
|
|
31
28
|
import {getLogger} from "./logger";
|
|
@@ -78,13 +75,13 @@ export interface PiRegister {
|
|
|
78
75
|
//window(parameters: PiCardDef): PiCardRef
|
|
79
76
|
|
|
80
77
|
window<S extends ReduxState>(
|
|
81
|
-
parameters: PiMapProps<WindowProps, S, {}
|
|
78
|
+
parameters: PiMapProps<WindowProps, S, {}>,
|
|
82
79
|
): PiCardRef;
|
|
83
80
|
|
|
84
81
|
card(name: string, parameters: PiCardDef): PiCardRef;
|
|
85
82
|
updateCard(
|
|
86
83
|
name: string,
|
|
87
|
-
parameters: {[key: string]: GenericCardParameterT}
|
|
84
|
+
parameters: {[key: string]: GenericCardParameterT},
|
|
88
85
|
): PiCardRef;
|
|
89
86
|
|
|
90
87
|
cardComponent(declaration: PiRegisterComponent): void;
|
|
@@ -102,19 +99,19 @@ export interface PiRegister {
|
|
|
102
99
|
metaCard<C>(declaration: PiRegisterMetaCard): void;
|
|
103
100
|
|
|
104
101
|
GET<S extends ReduxState, A extends ReduxAction, R, C = any>(
|
|
105
|
-
props: PiRegisterGetProps<S, A, R, C
|
|
102
|
+
props: PiRegisterGetProps<S, A, R, C>,
|
|
106
103
|
): void;
|
|
107
104
|
PUT<S extends ReduxState, A extends ReduxAction, R, C = any>(
|
|
108
|
-
props: PiRegisterPoPuPaProps<S, A, R, C
|
|
105
|
+
props: PiRegisterPoPuPaProps<S, A, R, C>,
|
|
109
106
|
): void;
|
|
110
107
|
POST<S extends ReduxState, A extends ReduxAction, R, C = any>(
|
|
111
|
-
props: PiRegisterPoPuPaProps<S, A, R, C
|
|
108
|
+
props: PiRegisterPoPuPaProps<S, A, R, C>,
|
|
112
109
|
): void;
|
|
113
110
|
PATCH<S extends ReduxState, A extends ReduxAction, R, C = any>(
|
|
114
|
-
props: PiRegisterPoPuPaProps<S, A, R, C
|
|
111
|
+
props: PiRegisterPoPuPaProps<S, A, R, C>,
|
|
115
112
|
): void;
|
|
116
113
|
DELETE<S extends ReduxState, A extends ReduxAction, R, C = any>(
|
|
117
|
-
props: PiRegisterDeleteProps<S, A, R, C
|
|
114
|
+
props: PiRegisterDeleteProps<S, A, R, C>,
|
|
118
115
|
): void;
|
|
119
116
|
//registerPeriodicGET<S extends ReduxState, A extends ReduxAction, R>(props: PiRegisterPeridicGetProps<S, A, R>): void;
|
|
120
117
|
|
|
@@ -204,12 +201,28 @@ export type StartProps = {
|
|
|
204
201
|
ignoredActions?: string[];
|
|
205
202
|
ignoredActionPaths?: string[];
|
|
206
203
|
ignoredStatePaths?: string[];
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Predicate used by Redux Toolkit's `serializableCheck`.
|
|
207
|
+
*
|
|
208
|
+
* By default RTK considers only "plain" JS values/objects serializable.
|
|
209
|
+
* If your app intentionally carries extra types (e.g. `Date`) in actions/state,
|
|
210
|
+
* you can extend this function:
|
|
211
|
+
*
|
|
212
|
+
* ```ts
|
|
213
|
+
* isSerializable: (v) => isPlain(v) || v instanceof Date
|
|
214
|
+
* ```
|
|
215
|
+
*
|
|
216
|
+
* This is useful for allowing things like Luxon DateTime, Map/Set wrappers,
|
|
217
|
+
* etc. (Prefer normalizing to plain data where possible.)
|
|
218
|
+
*/
|
|
219
|
+
isSerializable?: (value: unknown) => boolean;
|
|
207
220
|
};
|
|
208
221
|
|
|
209
222
|
export function start<S extends Partial<ReduxState>>(
|
|
210
223
|
initialState: S,
|
|
211
224
|
inits: ((register: PiRegister) => void)[] = [],
|
|
212
|
-
props: StartProps = {}
|
|
225
|
+
props: StartProps = {},
|
|
213
226
|
): PiRegister {
|
|
214
227
|
const state = {
|
|
215
228
|
...DEFAULT_REDUX_STATE,
|
|
@@ -239,6 +252,8 @@ export function start<S extends Partial<ReduxState>>(
|
|
|
239
252
|
].concat(props.ignoredActionPaths || []);
|
|
240
253
|
const ignoredPaths = ["cause.content"].concat(props.ignoredStatePaths || []);
|
|
241
254
|
|
|
255
|
+
const isSerializable = props.isSerializable ?? isPlain;
|
|
256
|
+
|
|
242
257
|
const store = configureStore({
|
|
243
258
|
reducer,
|
|
244
259
|
preloadedState: state as any, // keep type checking happy
|
|
@@ -251,6 +266,7 @@ export function start<S extends Partial<ReduxState>>(
|
|
|
251
266
|
ignoredPaths,
|
|
252
267
|
ignoreState: props.disableSerializableStateCheck,
|
|
253
268
|
ignoreAction: props.disableSerializableActionCheck,
|
|
269
|
+
isSerializable,
|
|
254
270
|
},
|
|
255
271
|
}),
|
|
256
272
|
});
|
|
@@ -263,7 +279,7 @@ export function start<S extends Partial<ReduxState>>(
|
|
|
263
279
|
const card = addCard(piReducer.register, dispatchF);
|
|
264
280
|
const updateCard = updateOrRegisterCard(piReducer.register, dispatchF);
|
|
265
281
|
const window = <S extends ReduxState>(
|
|
266
|
-
p: PiMapProps<WindowProps, S, {}
|
|
282
|
+
p: PiMapProps<WindowProps, S, {}>,
|
|
267
283
|
): PiCardRef => {
|
|
268
284
|
return card("_window", {cardType: "framework", ...p});
|
|
269
285
|
};
|
package/src/reducer.ts
CHANGED
|
@@ -37,7 +37,7 @@ type Source = {
|
|
|
37
37
|
|
|
38
38
|
export function createReducer(
|
|
39
39
|
initialState: ReduxState,
|
|
40
|
-
dispatcher: Dispatch<any
|
|
40
|
+
dispatcher: Dispatch<any>,
|
|
41
41
|
): [Reducer<ReduxState, Action>, PiReducer] {
|
|
42
42
|
const mappings: {[k: string]: ReducerDef<ReduxState, Action>[]} = {};
|
|
43
43
|
mappings[UPDATE_STATE_ACTION] = [
|
|
@@ -52,12 +52,20 @@ export function createReducer(
|
|
|
52
52
|
};
|
|
53
53
|
const reducer = (
|
|
54
54
|
state: ReduxState | undefined,
|
|
55
|
-
action: Action
|
|
55
|
+
action: Action,
|
|
56
56
|
): ReduxState => {
|
|
57
57
|
const s = state || initialState;
|
|
58
58
|
const ra = mappings[action.type];
|
|
59
59
|
const rany = mappings["*"];
|
|
60
60
|
if ((!ra || ra.length === 0) && (!rany || rany.length === 0)) {
|
|
61
|
+
const ra = s.pihanga?.reducers;
|
|
62
|
+
if (ra && ra.length > 0) {
|
|
63
|
+
return produce<ReduxState, ReduxState>(s, (draft) => {
|
|
64
|
+
if (draft.pihanga) {
|
|
65
|
+
draft.pihanga.reducers = [];
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
}
|
|
61
69
|
return s;
|
|
62
70
|
}
|
|
63
71
|
|
|
@@ -81,13 +89,13 @@ export function createReducer(
|
|
|
81
89
|
|
|
82
90
|
const registerReducer: PiRegisterReducerF = <
|
|
83
91
|
S extends ReduxState,
|
|
84
|
-
A extends ReduxAction
|
|
92
|
+
A extends ReduxAction,
|
|
85
93
|
>(
|
|
86
94
|
eventType: string,
|
|
87
95
|
mapper: ReduceF<S, A>,
|
|
88
96
|
priority: number = 0,
|
|
89
97
|
key?: string,
|
|
90
|
-
targetMapper?: ReduceF<S, A
|
|
98
|
+
targetMapper?: ReduceF<S, A>,
|
|
91
99
|
): PiReducerCancelF => {
|
|
92
100
|
return addReducer(eventType, {
|
|
93
101
|
mapperMulti: mapper,
|
|
@@ -99,12 +107,12 @@ export function createReducer(
|
|
|
99
107
|
|
|
100
108
|
const registerOneShot: PiRegisterOneShotReducerF = <
|
|
101
109
|
S extends ReduxState,
|
|
102
|
-
A extends ReduxAction
|
|
110
|
+
A extends ReduxAction,
|
|
103
111
|
>(
|
|
104
112
|
eventType: string,
|
|
105
113
|
mapper: (state: S, action: A, dispatch: DispatchF) => boolean,
|
|
106
114
|
priority: number = 0,
|
|
107
|
-
key: string | undefined = undefined
|
|
115
|
+
key: string | undefined = undefined,
|
|
108
116
|
): PiReducerCancelF => {
|
|
109
117
|
return addReducer(eventType, {mapperOnce: mapper, priority, key});
|
|
110
118
|
};
|
|
@@ -113,7 +121,7 @@ export function createReducer(
|
|
|
113
121
|
|
|
114
122
|
function addReducer<S extends ReduxState, A extends ReduxAction>(
|
|
115
123
|
eventType: string,
|
|
116
|
-
reducerDef: ReducerDef<S, A
|
|
124
|
+
reducerDef: ReducerDef<S, A>,
|
|
117
125
|
): PiReducerCancelF {
|
|
118
126
|
let m = mappings[eventType] || [];
|
|
119
127
|
const key = reducerDef.key;
|
|
@@ -154,7 +162,7 @@ export function createReducer(
|
|
|
154
162
|
|
|
155
163
|
function removeReducer(
|
|
156
164
|
key: string | undefined,
|
|
157
|
-
m: ReducerDef<ReduxState, Action>[]
|
|
165
|
+
m: ReducerDef<ReduxState, Action>[],
|
|
158
166
|
) {
|
|
159
167
|
if (key) {
|
|
160
168
|
return m.filter((r) => r.key !== key);
|
|
@@ -167,7 +175,7 @@ function _reduce(
|
|
|
167
175
|
ra: ReducerDef<ReduxState, Action>[],
|
|
168
176
|
draft: ReduxState,
|
|
169
177
|
action: Action,
|
|
170
|
-
delayedDispatcher: (a: any) => void
|
|
178
|
+
delayedDispatcher: (a: any) => void,
|
|
171
179
|
): ReducerDef<ReduxState, Action<any>>[] {
|
|
172
180
|
const rout: ReducerDef<ReduxState, Action<any>>[] = [];
|
|
173
181
|
ra.forEach((m) => {
|
|
@@ -194,7 +202,7 @@ function _reduce(
|
|
|
194
202
|
}
|
|
195
203
|
|
|
196
204
|
function _get_source_frame(
|
|
197
|
-
frames: StackTrace.StackFrame[]
|
|
205
|
+
frames: StackTrace.StackFrame[],
|
|
198
206
|
): StackTrace.StackFrame | undefined {
|
|
199
207
|
// Heuristic: frame 0 = Error, 1 = getCallerSiteInBrowser, 2 = your function, 3 = its caller
|
|
200
208
|
for (let i = 3; i < frames.length; i++) {
|