@nordcraft/runtime 1.0.14 → 1.0.15
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/dist/api/apiUtils.d.ts +2 -0
- package/dist/api/apiUtils.js +2 -0
- package/dist/api/apiUtils.js.map +1 -0
- package/dist/api/createAPI.d.ts +2 -2
- package/dist/api/createAPIv2.d.ts +7 -3
- package/dist/api/createAPIv2.js +129 -81
- package/dist/api/createAPIv2.js.map +1 -1
- package/dist/components/createComponent.js +21 -16
- package/dist/components/createComponent.js.map +1 -1
- package/dist/components/renderComponent.d.ts +2 -5
- package/dist/components/renderComponent.js.map +1 -1
- package/dist/custom-element/ToddleComponent.js +8 -3
- package/dist/custom-element/ToddleComponent.js.map +1 -1
- package/dist/custom-element.main.esm.js +28 -28
- package/dist/custom-element.main.esm.js.map +4 -4
- package/dist/editor-preview.main.js +10 -5
- package/dist/editor-preview.main.js.map +1 -1
- package/dist/events/handleAction.js +29 -29
- package/dist/events/handleAction.js.map +1 -1
- package/dist/page.main.esm.js +3 -3
- package/dist/page.main.esm.js.map +4 -4
- package/dist/page.main.js +8 -3
- package/dist/page.main.js.map +1 -1
- package/package.json +3 -3
- package/src/api/apiUtils.ts +4 -0
- package/src/api/createAPI.ts +1 -1
- package/src/api/createAPIv2.ts +249 -139
- package/src/components/createComponent.ts +24 -23
- package/src/components/renderComponent.ts +2 -1
- package/src/custom-element/ToddleComponent.ts +10 -8
- package/src/editor-preview.main.ts +11 -5
- package/src/events/handleAction.ts +42 -46
- package/src/page.main.ts +9 -8
- package/src/types.d.ts +34 -5
package/src/api/createAPIv2.ts
CHANGED
|
@@ -18,14 +18,13 @@ import {
|
|
|
18
18
|
isJsonStreamHeader,
|
|
19
19
|
isTextHeader,
|
|
20
20
|
} from '@nordcraft/core/dist/api/headers'
|
|
21
|
-
import type {
|
|
21
|
+
import type { ComponentData } from '@nordcraft/core/dist/component/component.types'
|
|
22
22
|
import type {
|
|
23
23
|
Formula,
|
|
24
24
|
FormulaContext,
|
|
25
|
-
ValueOperationValue,
|
|
26
25
|
} from '@nordcraft/core/dist/formula/formula'
|
|
27
26
|
import { applyFormula } from '@nordcraft/core/dist/formula/formula'
|
|
28
|
-
import type { NestedOmit
|
|
27
|
+
import type { NestedOmit } from '@nordcraft/core/dist/types'
|
|
29
28
|
import {
|
|
30
29
|
omitPaths,
|
|
31
30
|
sortObjectEntries,
|
|
@@ -34,21 +33,27 @@ import { PROXY_URL_HEADER, validateUrl } from '@nordcraft/core/dist/utils/url'
|
|
|
34
33
|
import { isDefined } from '@nordcraft/core/dist/utils/util'
|
|
35
34
|
import { handleAction } from '../events/handleAction'
|
|
36
35
|
import type { Signal } from '../signal/signal'
|
|
37
|
-
import type { ComponentContext,
|
|
36
|
+
import type { ComponentContext, ContextApiV2 } from '../types'
|
|
38
37
|
|
|
39
38
|
/**
|
|
40
39
|
* Set up an api v2 for a component.
|
|
41
40
|
*/
|
|
42
|
-
export function createAPI(
|
|
43
|
-
apiRequest
|
|
44
|
-
|
|
45
|
-
|
|
41
|
+
export function createAPI({
|
|
42
|
+
apiRequest,
|
|
43
|
+
componentData: initialComponentData,
|
|
44
|
+
ctx,
|
|
45
|
+
}: {
|
|
46
|
+
apiRequest: ApiRequest
|
|
47
|
+
componentData: ComponentData
|
|
48
|
+
ctx: ComponentContext
|
|
49
|
+
}): ContextApiV2 {
|
|
46
50
|
// If `__toddle` isn't found it is in a web component context. We behave as if the page isn't loaded.
|
|
47
51
|
let timer: any = null
|
|
48
52
|
let api = { ...apiRequest }
|
|
49
53
|
|
|
50
54
|
function constructRequest(
|
|
51
55
|
api: ApiRequest,
|
|
56
|
+
componentData: ComponentData,
|
|
52
57
|
): ReturnType<typeof createApiRequest> {
|
|
53
58
|
// Get baseUrl and validate it. (It wont be in web component context)
|
|
54
59
|
let baseUrl: string | undefined = window.origin
|
|
@@ -60,7 +65,7 @@ export function createAPI(
|
|
|
60
65
|
|
|
61
66
|
const request = createApiRequest({
|
|
62
67
|
api,
|
|
63
|
-
formulaContext: getFormulaContext(api),
|
|
68
|
+
formulaContext: getFormulaContext(api, componentData),
|
|
64
69
|
baseUrl,
|
|
65
70
|
defaultHeaders: undefined,
|
|
66
71
|
})
|
|
@@ -81,7 +86,10 @@ export function createAPI(
|
|
|
81
86
|
}
|
|
82
87
|
|
|
83
88
|
// Create the formula context for the api
|
|
84
|
-
function getFormulaContext(
|
|
89
|
+
function getFormulaContext(
|
|
90
|
+
api: ApiRequest,
|
|
91
|
+
componentData: ComponentData | undefined,
|
|
92
|
+
): FormulaContext {
|
|
85
93
|
// Use the general formula context to evaluate the arguments of the api
|
|
86
94
|
const formulaContext = {
|
|
87
95
|
data: ctx.dataSignal.get(),
|
|
@@ -103,6 +111,7 @@ export function createAPI(
|
|
|
103
111
|
|
|
104
112
|
const data = {
|
|
105
113
|
...formulaContext.data,
|
|
114
|
+
...componentData,
|
|
106
115
|
ApiInputs: {
|
|
107
116
|
...evaluatedInputs,
|
|
108
117
|
},
|
|
@@ -119,15 +128,16 @@ export function createAPI(
|
|
|
119
128
|
}
|
|
120
129
|
}
|
|
121
130
|
|
|
122
|
-
function handleRedirectRules(api: ApiRequest) {
|
|
131
|
+
function handleRedirectRules(api: ApiRequest, componentData: ComponentData) {
|
|
123
132
|
for (const [ruleName, rule] of sortObjectEntries(
|
|
124
133
|
api.redirectRules ?? {},
|
|
125
134
|
([_, rule]) => rule.index,
|
|
126
135
|
)) {
|
|
136
|
+
const formulaContext = getFormulaContext(api, componentData)
|
|
127
137
|
const location = applyFormula(rule.formula, {
|
|
128
|
-
...
|
|
138
|
+
...formulaContext,
|
|
129
139
|
data: {
|
|
130
|
-
...
|
|
140
|
+
...formulaContext.data,
|
|
131
141
|
Apis: {
|
|
132
142
|
[api.name]: ctx.dataSignal.get().Apis?.[api.name] as ApiStatus,
|
|
133
143
|
},
|
|
@@ -154,15 +164,22 @@ export function createAPI(
|
|
|
154
164
|
}
|
|
155
165
|
}
|
|
156
166
|
|
|
157
|
-
function triggerActions(
|
|
158
|
-
eventName
|
|
159
|
-
api
|
|
167
|
+
function triggerActions({
|
|
168
|
+
eventName,
|
|
169
|
+
api,
|
|
170
|
+
data,
|
|
171
|
+
componentData,
|
|
172
|
+
}: {
|
|
173
|
+
eventName: 'message' | 'success' | 'failed'
|
|
174
|
+
api: ApiRequest
|
|
160
175
|
data: {
|
|
161
176
|
body: unknown
|
|
162
177
|
status?: number
|
|
163
178
|
headers?: Record<string, string>
|
|
164
|
-
}
|
|
165
|
-
|
|
179
|
+
}
|
|
180
|
+
componentData: ComponentData
|
|
181
|
+
}) {
|
|
182
|
+
const formulaContext = getFormulaContext(api, componentData)
|
|
166
183
|
switch (eventName) {
|
|
167
184
|
case 'message': {
|
|
168
185
|
const event = createApiEvent('message', data.body)
|
|
@@ -170,8 +187,7 @@ export function createAPI(
|
|
|
170
187
|
handleAction(
|
|
171
188
|
action,
|
|
172
189
|
{
|
|
173
|
-
...
|
|
174
|
-
...ctx.dataSignal.get(),
|
|
190
|
+
...formulaContext.data,
|
|
175
191
|
Event: event,
|
|
176
192
|
},
|
|
177
193
|
ctx,
|
|
@@ -186,8 +202,7 @@ export function createAPI(
|
|
|
186
202
|
handleAction(
|
|
187
203
|
action,
|
|
188
204
|
{
|
|
189
|
-
...
|
|
190
|
-
...ctx.dataSignal.get(),
|
|
205
|
+
...formulaContext.data,
|
|
191
206
|
Event: event,
|
|
192
207
|
},
|
|
193
208
|
ctx,
|
|
@@ -205,8 +220,7 @@ export function createAPI(
|
|
|
205
220
|
handleAction(
|
|
206
221
|
action,
|
|
207
222
|
{
|
|
208
|
-
...
|
|
209
|
-
...ctx.dataSignal.get(),
|
|
223
|
+
...formulaContext.data,
|
|
210
224
|
Event: event,
|
|
211
225
|
},
|
|
212
226
|
ctx,
|
|
@@ -218,15 +232,21 @@ export function createAPI(
|
|
|
218
232
|
}
|
|
219
233
|
}
|
|
220
234
|
|
|
221
|
-
function apiSuccess(
|
|
222
|
-
api
|
|
235
|
+
function apiSuccess({
|
|
236
|
+
api,
|
|
237
|
+
componentData,
|
|
238
|
+
data,
|
|
239
|
+
performance,
|
|
240
|
+
}: {
|
|
241
|
+
api: ApiRequest
|
|
242
|
+
componentData: ComponentData
|
|
223
243
|
data: {
|
|
224
244
|
body: unknown
|
|
225
245
|
status?: number
|
|
226
246
|
headers?: Record<string, string>
|
|
227
|
-
}
|
|
228
|
-
performance: ApiPerformance
|
|
229
|
-
) {
|
|
247
|
+
}
|
|
248
|
+
performance: ApiPerformance
|
|
249
|
+
}) {
|
|
230
250
|
const latestRequestStart =
|
|
231
251
|
ctx.dataSignal.get().Apis?.[api.name]?.response?.performance?.requestStart
|
|
232
252
|
if (
|
|
@@ -252,7 +272,7 @@ export function createAPI(
|
|
|
252
272
|
},
|
|
253
273
|
},
|
|
254
274
|
})
|
|
255
|
-
const appliedRedirectRule = handleRedirectRules(api)
|
|
275
|
+
const appliedRedirectRule = handleRedirectRules(api, componentData)
|
|
256
276
|
if (appliedRedirectRule) {
|
|
257
277
|
ctx.dataSignal.set({
|
|
258
278
|
...ctx.dataSignal.get(),
|
|
@@ -276,15 +296,21 @@ export function createAPI(
|
|
|
276
296
|
}
|
|
277
297
|
}
|
|
278
298
|
|
|
279
|
-
function apiError(
|
|
280
|
-
api
|
|
299
|
+
function apiError({
|
|
300
|
+
api,
|
|
301
|
+
componentData,
|
|
302
|
+
data,
|
|
303
|
+
performance,
|
|
304
|
+
}: {
|
|
305
|
+
api: ApiRequest
|
|
306
|
+
componentData: ComponentData
|
|
281
307
|
data: {
|
|
282
308
|
body: unknown
|
|
283
309
|
status?: number
|
|
284
310
|
headers?: Record<string, string>
|
|
285
|
-
}
|
|
286
|
-
performance: ApiPerformance
|
|
287
|
-
) {
|
|
311
|
+
}
|
|
312
|
+
performance: ApiPerformance
|
|
313
|
+
}) {
|
|
288
314
|
const latestRequestStart =
|
|
289
315
|
ctx.dataSignal.get().Apis?.[api.name]?.response?.performance?.requestStart
|
|
290
316
|
if (
|
|
@@ -309,7 +335,7 @@ export function createAPI(
|
|
|
309
335
|
},
|
|
310
336
|
},
|
|
311
337
|
})
|
|
312
|
-
const appliedRedirectRule = handleRedirectRules(api)
|
|
338
|
+
const appliedRedirectRule = handleRedirectRules(api, componentData)
|
|
313
339
|
if (appliedRedirectRule) {
|
|
314
340
|
ctx.dataSignal.set({
|
|
315
341
|
...ctx.dataSignal.get(),
|
|
@@ -334,11 +360,17 @@ export function createAPI(
|
|
|
334
360
|
}
|
|
335
361
|
|
|
336
362
|
// Execute the request - potentially to the cloudflare Query proxy
|
|
337
|
-
async function execute(
|
|
338
|
-
api
|
|
339
|
-
url
|
|
340
|
-
requestSettings
|
|
341
|
-
|
|
363
|
+
async function execute({
|
|
364
|
+
api,
|
|
365
|
+
url,
|
|
366
|
+
requestSettings,
|
|
367
|
+
componentData,
|
|
368
|
+
}: {
|
|
369
|
+
api: ApiRequest
|
|
370
|
+
url: URL
|
|
371
|
+
requestSettings: ToddleRequestInit
|
|
372
|
+
componentData: ComponentData
|
|
373
|
+
}) {
|
|
342
374
|
const run = async () => {
|
|
343
375
|
const performance: ApiPerformance = {
|
|
344
376
|
requestStart: Date.now(),
|
|
@@ -362,7 +394,7 @@ export function createAPI(
|
|
|
362
394
|
const proxy = api.server?.proxy
|
|
363
395
|
? (applyFormula(
|
|
364
396
|
api.server.proxy.enabled.formula,
|
|
365
|
-
getFormulaContext(api),
|
|
397
|
+
getFormulaContext(api, componentData),
|
|
366
398
|
) ?? false)
|
|
367
399
|
: false
|
|
368
400
|
|
|
@@ -384,14 +416,24 @@ export function createAPI(
|
|
|
384
416
|
}
|
|
385
417
|
|
|
386
418
|
performance.responseStart = Date.now()
|
|
387
|
-
await handleResponse(api, response, performance)
|
|
419
|
+
await handleResponse({ api, componentData, res: response, performance })
|
|
388
420
|
return
|
|
389
421
|
} catch (error: any) {
|
|
390
422
|
const body = error.cause
|
|
391
423
|
? { message: error.message, data: error.cause }
|
|
392
424
|
: error.message
|
|
393
|
-
apiError(
|
|
394
|
-
|
|
425
|
+
apiError({
|
|
426
|
+
api,
|
|
427
|
+
componentData,
|
|
428
|
+
data: { body },
|
|
429
|
+
performance: { ...performance, responseEnd: Date.now() },
|
|
430
|
+
})
|
|
431
|
+
triggerActions({
|
|
432
|
+
eventName: 'failed',
|
|
433
|
+
api,
|
|
434
|
+
data: { body },
|
|
435
|
+
componentData,
|
|
436
|
+
})
|
|
395
437
|
return Promise.reject(error)
|
|
396
438
|
}
|
|
397
439
|
}
|
|
@@ -406,7 +448,10 @@ export function createAPI(
|
|
|
406
448
|
() => {
|
|
407
449
|
run().then(resolve, reject)
|
|
408
450
|
},
|
|
409
|
-
applyFormula(
|
|
451
|
+
applyFormula(
|
|
452
|
+
api.client?.debounce?.formula,
|
|
453
|
+
getFormulaContext(api, componentData),
|
|
454
|
+
),
|
|
410
455
|
)
|
|
411
456
|
})
|
|
412
457
|
}
|
|
@@ -414,11 +459,17 @@ export function createAPI(
|
|
|
414
459
|
return run()
|
|
415
460
|
}
|
|
416
461
|
|
|
417
|
-
function handleResponse(
|
|
418
|
-
api
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
462
|
+
function handleResponse({
|
|
463
|
+
api,
|
|
464
|
+
componentData,
|
|
465
|
+
res,
|
|
466
|
+
performance,
|
|
467
|
+
}: {
|
|
468
|
+
api: ApiRequest
|
|
469
|
+
componentData: ComponentData
|
|
470
|
+
res: Response
|
|
471
|
+
performance: ApiPerformance
|
|
472
|
+
}) {
|
|
422
473
|
let parserMode = api.client?.parserMode ?? 'auto'
|
|
423
474
|
|
|
424
475
|
if (parserMode === 'auto') {
|
|
@@ -440,25 +491,31 @@ export function createAPI(
|
|
|
440
491
|
|
|
441
492
|
switch (parserMode) {
|
|
442
493
|
case 'text':
|
|
443
|
-
return textStreamResponse(api, res, performance)
|
|
494
|
+
return textStreamResponse({ api, componentData, res, performance })
|
|
444
495
|
case 'json':
|
|
445
|
-
return jsonResponse(api, res, performance)
|
|
496
|
+
return jsonResponse({ api, componentData, res, performance })
|
|
446
497
|
case 'event-stream':
|
|
447
|
-
return eventStreamingResponse(api, res, performance)
|
|
498
|
+
return eventStreamingResponse({ api, componentData, res, performance })
|
|
448
499
|
case 'json-stream':
|
|
449
|
-
return jsonStreamResponse(api, res, performance)
|
|
500
|
+
return jsonStreamResponse({ api, componentData, res, performance })
|
|
450
501
|
case 'blob':
|
|
451
|
-
return blobResponse(api, res, performance)
|
|
502
|
+
return blobResponse({ api, componentData, res, performance })
|
|
452
503
|
default:
|
|
453
|
-
return textStreamResponse(api, res, performance)
|
|
504
|
+
return textStreamResponse({ api, componentData, res, performance })
|
|
454
505
|
}
|
|
455
506
|
}
|
|
456
507
|
|
|
457
|
-
function textStreamResponse(
|
|
458
|
-
api
|
|
459
|
-
res
|
|
460
|
-
performance
|
|
461
|
-
|
|
508
|
+
function textStreamResponse({
|
|
509
|
+
api,
|
|
510
|
+
res,
|
|
511
|
+
performance,
|
|
512
|
+
componentData,
|
|
513
|
+
}: {
|
|
514
|
+
api: ApiRequest
|
|
515
|
+
res: Response
|
|
516
|
+
performance: ApiPerformance
|
|
517
|
+
componentData: ComponentData
|
|
518
|
+
}) {
|
|
462
519
|
return handleStreaming({
|
|
463
520
|
api,
|
|
464
521
|
res,
|
|
@@ -467,14 +524,21 @@ export function createAPI(
|
|
|
467
524
|
useTextDecoder: true,
|
|
468
525
|
parseChunk: (chunk) => chunk,
|
|
469
526
|
parseChunksForData: (chunks) => chunks.join(''),
|
|
527
|
+
componentData,
|
|
470
528
|
})
|
|
471
529
|
}
|
|
472
530
|
|
|
473
|
-
function jsonStreamResponse(
|
|
474
|
-
api
|
|
475
|
-
res
|
|
476
|
-
performance
|
|
477
|
-
|
|
531
|
+
function jsonStreamResponse({
|
|
532
|
+
api,
|
|
533
|
+
res,
|
|
534
|
+
performance,
|
|
535
|
+
componentData,
|
|
536
|
+
}: {
|
|
537
|
+
api: ApiRequest
|
|
538
|
+
res: Response
|
|
539
|
+
performance: ApiPerformance
|
|
540
|
+
componentData: ComponentData
|
|
541
|
+
}) {
|
|
478
542
|
const parseChunk = (chunk: any) => {
|
|
479
543
|
let parsedData = chunk
|
|
480
544
|
try {
|
|
@@ -496,14 +560,21 @@ export function createAPI(
|
|
|
496
560
|
parseChunk,
|
|
497
561
|
parseChunksForData: (chunks) => [...chunks],
|
|
498
562
|
delimiters: ['\r\n', '\n'],
|
|
563
|
+
componentData,
|
|
499
564
|
})
|
|
500
565
|
}
|
|
501
566
|
|
|
502
|
-
async function jsonResponse(
|
|
503
|
-
api
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
567
|
+
async function jsonResponse({
|
|
568
|
+
api,
|
|
569
|
+
componentData,
|
|
570
|
+
res,
|
|
571
|
+
performance,
|
|
572
|
+
}: {
|
|
573
|
+
api: ApiRequest
|
|
574
|
+
componentData: ComponentData
|
|
575
|
+
res: Response
|
|
576
|
+
performance: ApiPerformance
|
|
577
|
+
}) {
|
|
507
578
|
const body = await res.json()
|
|
508
579
|
|
|
509
580
|
const status: ApiStatus = {
|
|
@@ -515,14 +586,20 @@ export function createAPI(
|
|
|
515
586
|
headers: Object.fromEntries(res.headers.entries()),
|
|
516
587
|
},
|
|
517
588
|
}
|
|
518
|
-
return endResponse(api, status, performance)
|
|
589
|
+
return endResponse({ api, apiStatus: status, componentData, performance })
|
|
519
590
|
}
|
|
520
591
|
|
|
521
|
-
async function blobResponse(
|
|
522
|
-
api
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
592
|
+
async function blobResponse({
|
|
593
|
+
api,
|
|
594
|
+
componentData,
|
|
595
|
+
res,
|
|
596
|
+
performance,
|
|
597
|
+
}: {
|
|
598
|
+
api: ApiRequest
|
|
599
|
+
componentData: ComponentData
|
|
600
|
+
res: Response
|
|
601
|
+
performance: ApiPerformance
|
|
602
|
+
}) {
|
|
526
603
|
const blob = await res.blob()
|
|
527
604
|
|
|
528
605
|
const status: ApiStatus = {
|
|
@@ -534,14 +611,20 @@ export function createAPI(
|
|
|
534
611
|
headers: Object.fromEntries(res.headers.entries()),
|
|
535
612
|
},
|
|
536
613
|
}
|
|
537
|
-
return endResponse(api, status, performance)
|
|
614
|
+
return endResponse({ api, apiStatus: status, componentData, performance })
|
|
538
615
|
}
|
|
539
616
|
|
|
540
|
-
function eventStreamingResponse(
|
|
541
|
-
api
|
|
542
|
-
res
|
|
543
|
-
performance
|
|
544
|
-
|
|
617
|
+
function eventStreamingResponse({
|
|
618
|
+
api,
|
|
619
|
+
res,
|
|
620
|
+
performance,
|
|
621
|
+
componentData,
|
|
622
|
+
}: {
|
|
623
|
+
api: ApiRequest
|
|
624
|
+
res: Response
|
|
625
|
+
performance: ApiPerformance
|
|
626
|
+
componentData: ComponentData
|
|
627
|
+
}) {
|
|
545
628
|
const parseChunk = (chunk: string) => {
|
|
546
629
|
const event = chunk.match(/event: (.*)/)?.[1] ?? 'message'
|
|
547
630
|
const data = chunk.match(/data: (.*)/)?.[1] ?? ''
|
|
@@ -570,6 +653,7 @@ export function createAPI(
|
|
|
570
653
|
parseChunk,
|
|
571
654
|
parseChunksForData: (chunks) => [...chunks],
|
|
572
655
|
delimiters: ['\n\n', '\r\n\r\n'],
|
|
656
|
+
componentData,
|
|
573
657
|
})
|
|
574
658
|
}
|
|
575
659
|
|
|
@@ -581,7 +665,8 @@ export function createAPI(
|
|
|
581
665
|
useTextDecoder,
|
|
582
666
|
parseChunk,
|
|
583
667
|
parseChunksForData,
|
|
584
|
-
delimiters, // There can be various delimiters for the same stream. SSE might use both \n\n and \r\n\r\n
|
|
668
|
+
delimiters, // There can be various delimiters for the same stream. SSE might use both \n\n and \r\n\r\n,
|
|
669
|
+
componentData,
|
|
585
670
|
}: {
|
|
586
671
|
api: ApiRequest
|
|
587
672
|
res: Response
|
|
@@ -591,6 +676,7 @@ export function createAPI(
|
|
|
591
676
|
parseChunk: (chunk: any) => any
|
|
592
677
|
parseChunksForData: (chunks: any[]) => any
|
|
593
678
|
delimiters?: string[]
|
|
679
|
+
componentData: ComponentData
|
|
594
680
|
}) {
|
|
595
681
|
const chunks: {
|
|
596
682
|
chunks: any[]
|
|
@@ -621,7 +707,12 @@ export function createAPI(
|
|
|
621
707
|
},
|
|
622
708
|
})
|
|
623
709
|
if ((api.client?.onMessage?.actions ?? []).length > 0) {
|
|
624
|
-
triggerActions(
|
|
710
|
+
triggerActions({
|
|
711
|
+
eventName: 'message',
|
|
712
|
+
api,
|
|
713
|
+
data: { body: parsedChunk },
|
|
714
|
+
componentData,
|
|
715
|
+
})
|
|
625
716
|
}
|
|
626
717
|
}
|
|
627
718
|
},
|
|
@@ -680,14 +771,20 @@ export function createAPI(
|
|
|
680
771
|
cause: chunks.chunks.join(''),
|
|
681
772
|
})
|
|
682
773
|
}
|
|
683
|
-
return endResponse(api, status, performance)
|
|
774
|
+
return endResponse({ api, apiStatus: status, componentData, performance })
|
|
684
775
|
}
|
|
685
776
|
|
|
686
|
-
function endResponse(
|
|
687
|
-
api
|
|
688
|
-
apiStatus
|
|
689
|
-
|
|
690
|
-
|
|
777
|
+
function endResponse({
|
|
778
|
+
api,
|
|
779
|
+
apiStatus,
|
|
780
|
+
componentData,
|
|
781
|
+
performance,
|
|
782
|
+
}: {
|
|
783
|
+
api: ApiRequest
|
|
784
|
+
apiStatus: ApiStatus
|
|
785
|
+
componentData: ComponentData
|
|
786
|
+
performance: ApiPerformance
|
|
787
|
+
}) {
|
|
691
788
|
performance.responseEnd = Date.now()
|
|
692
789
|
|
|
693
790
|
const data = {
|
|
@@ -708,7 +805,7 @@ export function createAPI(
|
|
|
708
805
|
status: data.status,
|
|
709
806
|
headers: data.headers,
|
|
710
807
|
},
|
|
711
|
-
formulaContext: getFormulaContext(api),
|
|
808
|
+
formulaContext: getFormulaContext(api, componentData),
|
|
712
809
|
errorFormula: api.isError,
|
|
713
810
|
performance,
|
|
714
811
|
})
|
|
@@ -718,11 +815,11 @@ export function createAPI(
|
|
|
718
815
|
data.body = apiStatus.error
|
|
719
816
|
}
|
|
720
817
|
|
|
721
|
-
apiError(api, data, performance)
|
|
722
|
-
triggerActions('failed', api, data)
|
|
818
|
+
apiError({ api, componentData, data, performance })
|
|
819
|
+
triggerActions({ eventName: 'failed', api, componentData, data })
|
|
723
820
|
} else {
|
|
724
|
-
apiSuccess(api, data, performance)
|
|
725
|
-
triggerActions('success', api, data)
|
|
821
|
+
apiSuccess({ api, componentData, data, performance })
|
|
822
|
+
triggerActions({ eventName: 'success', api, componentData, data })
|
|
726
823
|
}
|
|
727
824
|
}
|
|
728
825
|
|
|
@@ -757,9 +854,9 @@ export function createAPI(
|
|
|
757
854
|
|
|
758
855
|
// eslint-disable-next-line prefer-const
|
|
759
856
|
payloadSignal = ctx.dataSignal.map((_) => {
|
|
760
|
-
const payloadContext = getFormulaContext(api)
|
|
857
|
+
const payloadContext = getFormulaContext(api, initialComponentData)
|
|
761
858
|
return {
|
|
762
|
-
request: constructRequest(api),
|
|
859
|
+
request: constructRequest(api, initialComponentData),
|
|
763
860
|
api: getApiForComparison(api),
|
|
764
861
|
autoFetch: api.autoFetch
|
|
765
862
|
? applyFormula(api.autoFetch, payloadContext)
|
|
@@ -768,7 +865,7 @@ export function createAPI(
|
|
|
768
865
|
}
|
|
769
866
|
})
|
|
770
867
|
payloadSignal.subscribe(async (_) => {
|
|
771
|
-
const { url, requestSettings } = constructRequest(api)
|
|
868
|
+
const { url, requestSettings } = constructRequest(api, initialComponentData)
|
|
772
869
|
// Ensure we only use caching if the page is currently loading
|
|
773
870
|
const cacheMatch =
|
|
774
871
|
// We lookup the API from cache as long as autofetch is defined (and not statically falsy)
|
|
@@ -783,42 +880,54 @@ export function createAPI(
|
|
|
783
880
|
|
|
784
881
|
if (cacheMatch) {
|
|
785
882
|
if (cacheMatch.error) {
|
|
786
|
-
apiError(
|
|
883
|
+
apiError({
|
|
787
884
|
api,
|
|
788
|
-
{
|
|
885
|
+
data: {
|
|
789
886
|
body: cacheMatch.error,
|
|
790
887
|
status: cacheMatch.response?.status,
|
|
791
888
|
headers: cacheMatch.response?.headers ?? undefined,
|
|
792
889
|
},
|
|
793
|
-
{
|
|
890
|
+
performance: {
|
|
794
891
|
requestStart:
|
|
795
892
|
cacheMatch.response?.performance?.requestStart ?? null,
|
|
796
893
|
responseStart:
|
|
797
894
|
cacheMatch.response?.performance?.responseStart ?? null,
|
|
798
895
|
responseEnd: cacheMatch.response?.performance?.responseEnd ?? null,
|
|
799
896
|
},
|
|
800
|
-
|
|
897
|
+
componentData: initialComponentData,
|
|
898
|
+
})
|
|
801
899
|
} else {
|
|
802
|
-
apiSuccess(
|
|
900
|
+
apiSuccess({
|
|
803
901
|
api,
|
|
804
|
-
{
|
|
902
|
+
data: {
|
|
805
903
|
body: cacheMatch.data,
|
|
806
904
|
status: cacheMatch.response?.status,
|
|
807
905
|
headers: cacheMatch.response?.headers ?? undefined,
|
|
808
906
|
},
|
|
809
|
-
{
|
|
907
|
+
performance: {
|
|
810
908
|
requestStart:
|
|
811
909
|
cacheMatch.response?.performance?.requestStart ?? null,
|
|
812
910
|
responseStart:
|
|
813
911
|
cacheMatch.response?.performance?.responseStart ?? null,
|
|
814
912
|
responseEnd: cacheMatch.response?.performance?.responseEnd ?? null,
|
|
815
913
|
},
|
|
816
|
-
|
|
914
|
+
componentData: initialComponentData,
|
|
915
|
+
})
|
|
817
916
|
}
|
|
818
917
|
} else {
|
|
819
|
-
if (
|
|
918
|
+
if (
|
|
919
|
+
applyFormula(
|
|
920
|
+
api.autoFetch,
|
|
921
|
+
getFormulaContext(api, initialComponentData),
|
|
922
|
+
)
|
|
923
|
+
) {
|
|
820
924
|
// Execute will set the initial status of the api in the dataSignal
|
|
821
|
-
await execute(
|
|
925
|
+
await execute({
|
|
926
|
+
api,
|
|
927
|
+
url,
|
|
928
|
+
requestSettings,
|
|
929
|
+
componentData: initialComponentData,
|
|
930
|
+
})
|
|
822
931
|
} else {
|
|
823
932
|
ctx.dataSignal.update((data) => {
|
|
824
933
|
return {
|
|
@@ -838,24 +947,7 @@ export function createAPI(
|
|
|
838
947
|
})
|
|
839
948
|
|
|
840
949
|
return {
|
|
841
|
-
fetch: ({
|
|
842
|
-
actionInputs,
|
|
843
|
-
actionModels,
|
|
844
|
-
}: {
|
|
845
|
-
actionInputs?: Record<
|
|
846
|
-
string,
|
|
847
|
-
| ValueOperationValue
|
|
848
|
-
| {
|
|
849
|
-
name: string
|
|
850
|
-
formula?: Formula
|
|
851
|
-
}
|
|
852
|
-
>
|
|
853
|
-
actionModels?: {
|
|
854
|
-
onCompleted: ActionModel[]
|
|
855
|
-
onFailed: ActionModel[]
|
|
856
|
-
onMessage: ActionModel[]
|
|
857
|
-
}
|
|
858
|
-
}) => {
|
|
950
|
+
fetch: ({ actionInputs, actionModels, componentData }) => {
|
|
859
951
|
// Inputs might already be evaluated. If they are we add them as a value formula to be evaluated later.
|
|
860
952
|
const inputs = Object.entries(actionInputs ?? {}).reduce<
|
|
861
953
|
Record<
|
|
@@ -907,18 +999,26 @@ export function createAPI(
|
|
|
907
999
|
},
|
|
908
1000
|
}
|
|
909
1001
|
|
|
910
|
-
const { url, requestSettings } = constructRequest(
|
|
1002
|
+
const { url, requestSettings } = constructRequest(
|
|
1003
|
+
apiWithInputsAndActions,
|
|
1004
|
+
componentData,
|
|
1005
|
+
)
|
|
911
1006
|
|
|
912
|
-
return execute(
|
|
1007
|
+
return execute({
|
|
1008
|
+
api: apiWithInputsAndActions,
|
|
1009
|
+
url,
|
|
1010
|
+
requestSettings,
|
|
1011
|
+
componentData,
|
|
1012
|
+
})
|
|
913
1013
|
},
|
|
914
|
-
update: (newApi
|
|
1014
|
+
update: (newApi, componentData) => {
|
|
915
1015
|
api = newApi
|
|
916
|
-
const updateContext = getFormulaContext(api)
|
|
1016
|
+
const updateContext = getFormulaContext(api, componentData)
|
|
917
1017
|
const autoFetch =
|
|
918
1018
|
api.autoFetch && applyFormula(api.autoFetch, updateContext)
|
|
919
1019
|
if (autoFetch) {
|
|
920
1020
|
payloadSignal?.set({
|
|
921
|
-
request: constructRequest(newApi),
|
|
1021
|
+
request: constructRequest(newApi, componentData),
|
|
922
1022
|
api: getApiForComparison(newApi),
|
|
923
1023
|
autoFetch,
|
|
924
1024
|
proxy: applyFormula(
|
|
@@ -928,7 +1028,7 @@ export function createAPI(
|
|
|
928
1028
|
})
|
|
929
1029
|
}
|
|
930
1030
|
},
|
|
931
|
-
triggerActions: () => {
|
|
1031
|
+
triggerActions: (componentData) => {
|
|
932
1032
|
const apiData = ctx.dataSignal.get().Apis?.[api.name]
|
|
933
1033
|
if (
|
|
934
1034
|
apiData === undefined ||
|
|
@@ -937,13 +1037,23 @@ export function createAPI(
|
|
|
937
1037
|
return
|
|
938
1038
|
}
|
|
939
1039
|
if (apiData.error) {
|
|
940
|
-
triggerActions(
|
|
941
|
-
|
|
942
|
-
|
|
1040
|
+
triggerActions({
|
|
1041
|
+
eventName: 'failed',
|
|
1042
|
+
api,
|
|
1043
|
+
data: {
|
|
1044
|
+
body: apiData.error,
|
|
1045
|
+
status: apiData.response?.status,
|
|
1046
|
+
},
|
|
1047
|
+
componentData,
|
|
943
1048
|
})
|
|
944
1049
|
} else {
|
|
945
|
-
triggerActions(
|
|
946
|
-
|
|
1050
|
+
triggerActions({
|
|
1051
|
+
eventName: 'success',
|
|
1052
|
+
api,
|
|
1053
|
+
data: {
|
|
1054
|
+
body: apiData.data,
|
|
1055
|
+
},
|
|
1056
|
+
componentData,
|
|
947
1057
|
})
|
|
948
1058
|
}
|
|
949
1059
|
},
|