@pihanga2/core 0.3.7 → 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/package.json +3 -2
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/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": [
|