@teleporthq/teleport-project-generator-next 0.45.20 → 0.45.21
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/__tests__/_helpers/data-exposure-fixture.ts +114 -44
- package/__tests__/component-bound-state.test.ts +817 -0
- package/__tests__/security-runtime-require-paths.test.ts +29 -1
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js +2 -0
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/state-data-source-plugin.d.ts +27 -0
- package/dist/cjs/state-data-source-plugin.d.ts.map +1 -1
- package/dist/cjs/state-data-source-plugin.js +278 -4
- package/dist/cjs/state-data-source-plugin.js.map +1 -1
- package/dist/cjs/tsconfig.tsbuildinfo +1 -1
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +3 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/state-data-source-plugin.d.ts +27 -0
- package/dist/esm/state-data-source-plugin.d.ts.map +1 -1
- package/dist/esm/state-data-source-plugin.js +278 -5
- package/dist/esm/state-data-source-plugin.js.map +1 -1
- package/dist/esm/tsconfig.tsbuildinfo +1 -1
- package/package.json +21 -21
- package/src/index.ts +6 -1
- package/src/state-data-source-plugin.ts +433 -9
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@teleporthq/teleport-project-generator-next",
|
|
3
|
-
"version": "0.45.
|
|
3
|
+
"version": "0.45.21",
|
|
4
4
|
"description": "Project generator for a standard Next.js project",
|
|
5
5
|
"author": "teleportHQ",
|
|
6
6
|
"license": "MIT",
|
|
@@ -29,25 +29,25 @@
|
|
|
29
29
|
"dependencies": {
|
|
30
30
|
"@babel/generator": "^7.5.5",
|
|
31
31
|
"@babel/types": "^7.5.5",
|
|
32
|
-
"@teleporthq/teleport-component-generator": "^0.45.
|
|
33
|
-
"@teleporthq/teleport-component-generator-react": "^0.45.
|
|
34
|
-
"@teleporthq/teleport-plugin-common": "^0.45.
|
|
35
|
-
"@teleporthq/teleport-plugin-css": "^0.45.
|
|
36
|
-
"@teleporthq/teleport-plugin-import-statements": "^0.45.
|
|
37
|
-
"@teleporthq/teleport-plugin-jsx-head-config": "^0.45.
|
|
38
|
-
"@teleporthq/teleport-plugin-jsx-next-image": "^0.45.
|
|
39
|
-
"@teleporthq/teleport-plugin-next-data-source": "^0.45.
|
|
40
|
-
"@teleporthq/teleport-plugin-next-inline-fetch": "^0.45.
|
|
41
|
-
"@teleporthq/teleport-plugin-next-static-paths": "^0.45.
|
|
42
|
-
"@teleporthq/teleport-plugin-next-static-props": "^0.45.
|
|
43
|
-
"@teleporthq/teleport-plugin-next-workflows": "^0.45.
|
|
44
|
-
"@teleporthq/teleport-postprocessor-prettier-js": "^0.45.
|
|
45
|
-
"@teleporthq/teleport-project-generator": "^0.45.
|
|
46
|
-
"@teleporthq/teleport-shared": "^0.45.
|
|
47
|
-
"@teleporthq/teleport-types": "^0.45.
|
|
48
|
-
"@teleporthq/teleport-uidl-builders": "^0.45.
|
|
49
|
-
"@teleporthq/teleport-uidl-resolver": "^0.45.
|
|
50
|
-
"@teleporthq/teleport-uidl-validator": "^0.45.
|
|
32
|
+
"@teleporthq/teleport-component-generator": "^0.45.21",
|
|
33
|
+
"@teleporthq/teleport-component-generator-react": "^0.45.21",
|
|
34
|
+
"@teleporthq/teleport-plugin-common": "^0.45.21",
|
|
35
|
+
"@teleporthq/teleport-plugin-css": "^0.45.21",
|
|
36
|
+
"@teleporthq/teleport-plugin-import-statements": "^0.45.21",
|
|
37
|
+
"@teleporthq/teleport-plugin-jsx-head-config": "^0.45.21",
|
|
38
|
+
"@teleporthq/teleport-plugin-jsx-next-image": "^0.45.21",
|
|
39
|
+
"@teleporthq/teleport-plugin-next-data-source": "^0.45.21",
|
|
40
|
+
"@teleporthq/teleport-plugin-next-inline-fetch": "^0.45.21",
|
|
41
|
+
"@teleporthq/teleport-plugin-next-static-paths": "^0.45.21",
|
|
42
|
+
"@teleporthq/teleport-plugin-next-static-props": "^0.45.21",
|
|
43
|
+
"@teleporthq/teleport-plugin-next-workflows": "^0.45.21",
|
|
44
|
+
"@teleporthq/teleport-postprocessor-prettier-js": "^0.45.21",
|
|
45
|
+
"@teleporthq/teleport-project-generator": "^0.45.21",
|
|
46
|
+
"@teleporthq/teleport-shared": "^0.45.21",
|
|
47
|
+
"@teleporthq/teleport-types": "^0.45.21",
|
|
48
|
+
"@teleporthq/teleport-uidl-builders": "^0.45.21",
|
|
49
|
+
"@teleporthq/teleport-uidl-resolver": "^0.45.21",
|
|
50
|
+
"@teleporthq/teleport-uidl-validator": "^0.45.21"
|
|
51
51
|
},
|
|
52
|
-
"gitHead": "
|
|
52
|
+
"gitHead": "47f0aec1e66252803ca417857204f17dc27ed1ce"
|
|
53
53
|
}
|
package/src/index.ts
CHANGED
|
@@ -18,7 +18,10 @@ import {
|
|
|
18
18
|
import { ReactStyleVariation, FileType, ProjectPlugin } from '@teleporthq/teleport-types'
|
|
19
19
|
import { createStyleSheetPlugin } from '@teleporthq/teleport-plugin-css'
|
|
20
20
|
import { createDocumentFileChunks, configContentGenerator } from './utils'
|
|
21
|
-
import {
|
|
21
|
+
import {
|
|
22
|
+
createComponentStateDataSourcePlugin,
|
|
23
|
+
createStateDataSourcePlugin,
|
|
24
|
+
} from './state-data-source-plugin'
|
|
22
25
|
import { NextProjectMapping } from './next-project-mapping'
|
|
23
26
|
import NextTemplate from './project-template'
|
|
24
27
|
import { createNextInternationalizationPlugin } from './internationalization/locale-mapper-component'
|
|
@@ -169,6 +172,7 @@ const createNextProjectGenerator = () => {
|
|
|
169
172
|
const nextComponentDataSourcePlugin = createNextComponentDataSourcePlugin()
|
|
170
173
|
const nextPagesDataSourcePlugin = createNextPagesDataSourcePlugin()
|
|
171
174
|
const stateDataSourcePlugin = createStateDataSourcePlugin()
|
|
175
|
+
const componentStateDataSourcePlugin = createComponentStateDataSourcePlugin()
|
|
172
176
|
const nextInternationalizationPlugin = createNextInternationalizationPlugin()
|
|
173
177
|
const aiChatSessionPersistencePlugin = createAIChatSessionPersistencePlugin({
|
|
174
178
|
basePath: ['components'],
|
|
@@ -207,6 +211,7 @@ const createNextProjectGenerator = () => {
|
|
|
207
211
|
nextImagePlugin,
|
|
208
212
|
nextComponentInlineFetchPlugin,
|
|
209
213
|
nextComponentDataSourcePlugin,
|
|
214
|
+
componentStateDataSourcePlugin,
|
|
210
215
|
nextInternationalizationPlugin,
|
|
211
216
|
// Chat-only: keeps the transcript alive across client-side
|
|
212
217
|
// navigation. MUST run before the welcome plugin, so a restored
|
|
@@ -10,6 +10,7 @@ import {
|
|
|
10
10
|
UIDLFilterConfigEntry,
|
|
11
11
|
} from '@teleporthq/teleport-types'
|
|
12
12
|
import {
|
|
13
|
+
DataExposure,
|
|
13
14
|
GenericUtils,
|
|
14
15
|
PageAccess,
|
|
15
16
|
PageArrival,
|
|
@@ -19,6 +20,8 @@ import {
|
|
|
19
20
|
import { RouteUtils } from '@teleporthq/teleport-plugin-common'
|
|
20
21
|
import * as types from '@babel/types'
|
|
21
22
|
import {
|
|
23
|
+
COMPONENT_STATE_ROUTE_FOLDER,
|
|
24
|
+
generateComponentStateRoute,
|
|
22
25
|
generateDataSourceFetcherWithCore,
|
|
23
26
|
generateRawQueryFetcher,
|
|
24
27
|
generateSafeFileName,
|
|
@@ -26,7 +29,12 @@ import {
|
|
|
26
29
|
validateDataSourceConfig,
|
|
27
30
|
buildProductTransformOptions,
|
|
28
31
|
buildServerLocaleParam,
|
|
32
|
+
buildClientLocaleExpression,
|
|
33
|
+
ensureDataSourceModule,
|
|
34
|
+
ensureDataSourceReadRoute,
|
|
35
|
+
ensureRouterDeclaration,
|
|
29
36
|
isLocalizedProject,
|
|
37
|
+
REQUEST_LOCALE_PARAM,
|
|
30
38
|
} from '@teleporthq/teleport-plugin-next-data-source'
|
|
31
39
|
import {
|
|
32
40
|
CsvColumnLabel,
|
|
@@ -73,14 +81,11 @@ const createSafeFetchExpressionWithFallback = (
|
|
|
73
81
|
}
|
|
74
82
|
|
|
75
83
|
/**
|
|
76
|
-
*
|
|
84
|
+
* `(__rows) => Array.isArray(__rows) ? __rows.map((__row) => ({ <label>: __row?.[<id>] })) : __rows`
|
|
77
85
|
* — a CSV source's rows re-keyed by column label, the keys the editor showed
|
|
78
86
|
* while the state was bound (see `getCsvColumnLabels`).
|
|
79
87
|
*/
|
|
80
|
-
const
|
|
81
|
-
fetchCallExpression: types.CallExpression,
|
|
82
|
-
columns: CsvColumnLabel[]
|
|
83
|
-
): types.CallExpression => {
|
|
88
|
+
const csvRowsRelabeller = (columns: CsvColumnLabel[]): types.ArrowFunctionExpression => {
|
|
84
89
|
const rows = types.identifier('__rows')
|
|
85
90
|
const row = types.identifier('__row')
|
|
86
91
|
const relabelledRow = types.objectExpression(
|
|
@@ -102,12 +107,18 @@ const relabelCsvRows = (
|
|
|
102
107
|
rows
|
|
103
108
|
)
|
|
104
109
|
|
|
105
|
-
return types.
|
|
106
|
-
types.memberExpression(fetchCallExpression, types.identifier('then')),
|
|
107
|
-
[types.arrowFunctionExpression([rows], relabelledRows)]
|
|
108
|
-
)
|
|
110
|
+
return types.arrowFunctionExpression([rows], relabelledRows)
|
|
109
111
|
}
|
|
110
112
|
|
|
113
|
+
/** `<fetch>.then(<csvRowsRelabeller>)` — the rows of a server-side fetch, re-keyed. */
|
|
114
|
+
const relabelCsvRows = (
|
|
115
|
+
fetchCallExpression: types.CallExpression,
|
|
116
|
+
columns: CsvColumnLabel[]
|
|
117
|
+
): types.CallExpression =>
|
|
118
|
+
types.callExpression(types.memberExpression(fetchCallExpression, types.identifier('then')), [
|
|
119
|
+
csvRowsRelabeller(columns),
|
|
120
|
+
])
|
|
121
|
+
|
|
111
122
|
/**
|
|
112
123
|
* Builds the AST for extracting a value from fetched data using a refPath.
|
|
113
124
|
* Uses optional chaining (?.) to safely navigate the data path.
|
|
@@ -1318,3 +1329,416 @@ export const createStateDataSourcePlugin: ComponentPluginFactory<{}> = () => {
|
|
|
1318
1329
|
|
|
1319
1330
|
return stateDataSourcePlugin
|
|
1320
1331
|
}
|
|
1332
|
+
|
|
1333
|
+
/** A state filled by its own SQL statement, not by the rows of its table. */
|
|
1334
|
+
const hasOwnQuery = (state: BoundState): boolean =>
|
|
1335
|
+
!!state.definition.query && state.definition.query.trim().length > 0
|
|
1336
|
+
|
|
1337
|
+
/**
|
|
1338
|
+
* The value a component's state takes from the rows its table's read route
|
|
1339
|
+
* answered (`__data`), held to the state's type: a list never takes anything
|
|
1340
|
+
* but a list, and a single value never takes one.
|
|
1341
|
+
*/
|
|
1342
|
+
const buildComponentStateValue = (data: types.Identifier, state: BoundState): types.Expression => {
|
|
1343
|
+
const { definition, binding } = state
|
|
1344
|
+
const extracted = buildRefPathExtraction(data, binding.refPath)
|
|
1345
|
+
const fallback = getTypeFallbackExpression(definition)
|
|
1346
|
+
const isList = types.callExpression(
|
|
1347
|
+
types.memberExpression(types.identifier('Array'), types.identifier('isArray')),
|
|
1348
|
+
[extracted]
|
|
1349
|
+
)
|
|
1350
|
+
|
|
1351
|
+
let value: types.Expression
|
|
1352
|
+
if (binding.refPath.length > 1) {
|
|
1353
|
+
value = types.logicalExpression('??', extracted, fallback)
|
|
1354
|
+
} else if (SINGLE_VALUE_STATE_TYPES.has(definition.type)) {
|
|
1355
|
+
value = types.conditionalExpression(isList, fallback, extracted)
|
|
1356
|
+
} else {
|
|
1357
|
+
value = types.conditionalExpression(isList, extracted, types.arrayExpression([]))
|
|
1358
|
+
}
|
|
1359
|
+
|
|
1360
|
+
return wrapWithMappingFunction(value, definition)
|
|
1361
|
+
}
|
|
1362
|
+
|
|
1363
|
+
/** Where a component reads one fetch group of its states from, and what the read depends on. */
|
|
1364
|
+
interface ComponentStateRead {
|
|
1365
|
+
/** What the effect prepares before it fetches. */
|
|
1366
|
+
setup: types.Statement[]
|
|
1367
|
+
/** The address it fetches. */
|
|
1368
|
+
url: types.Expression
|
|
1369
|
+
/** What it fetches again for. */
|
|
1370
|
+
deps: types.Expression[]
|
|
1371
|
+
/** A CSV source's rows, re-keyed by column label. */
|
|
1372
|
+
csvColumns: CsvColumnLabel[] | null
|
|
1373
|
+
}
|
|
1374
|
+
|
|
1375
|
+
/**
|
|
1376
|
+
* The table's own read route, asked for the rows in the state's order and
|
|
1377
|
+
* filter, and in the page's language:
|
|
1378
|
+
*
|
|
1379
|
+
* const __query = new URLSearchParams({ sorts: '…', filters: '…' })
|
|
1380
|
+
* if (router?.locale) { __query.set('locale', router?.locale) }
|
|
1381
|
+
* fetch(`/api/<route>?${__query}`) … again when router?.locale changes
|
|
1382
|
+
*/
|
|
1383
|
+
const tableReadOf = (group: FetchGroup, localized: boolean): ComponentStateRead => {
|
|
1384
|
+
const query = types.identifier('__query')
|
|
1385
|
+
const setup: types.Statement[] = [
|
|
1386
|
+
types.variableDeclaration('const', [
|
|
1387
|
+
types.variableDeclarator(
|
|
1388
|
+
query,
|
|
1389
|
+
types.newExpression(types.identifier('URLSearchParams'), [
|
|
1390
|
+
buildFetchParams(group.states[0].definition, false),
|
|
1391
|
+
])
|
|
1392
|
+
),
|
|
1393
|
+
]),
|
|
1394
|
+
]
|
|
1395
|
+
if (localized) {
|
|
1396
|
+
setup.push(
|
|
1397
|
+
types.ifStatement(
|
|
1398
|
+
buildClientLocaleExpression(),
|
|
1399
|
+
types.blockStatement([
|
|
1400
|
+
types.expressionStatement(
|
|
1401
|
+
types.callExpression(types.memberExpression(query, types.identifier('set')), [
|
|
1402
|
+
types.stringLiteral(REQUEST_LOCALE_PARAM),
|
|
1403
|
+
buildClientLocaleExpression(),
|
|
1404
|
+
])
|
|
1405
|
+
),
|
|
1406
|
+
])
|
|
1407
|
+
)
|
|
1408
|
+
)
|
|
1409
|
+
}
|
|
1410
|
+
|
|
1411
|
+
return {
|
|
1412
|
+
setup,
|
|
1413
|
+
url: types.templateLiteral(
|
|
1414
|
+
[
|
|
1415
|
+
types.templateElement({
|
|
1416
|
+
raw: `/api/${group.fileName}?`,
|
|
1417
|
+
cooked: `/api/${group.fileName}?`,
|
|
1418
|
+
}),
|
|
1419
|
+
types.templateElement({ raw: '', cooked: '' }, true),
|
|
1420
|
+
],
|
|
1421
|
+
[query]
|
|
1422
|
+
),
|
|
1423
|
+
deps: localized ? [buildClientLocaleExpression()] : [],
|
|
1424
|
+
csvColumns: getCsvColumnLabels(group.dataSource),
|
|
1425
|
+
}
|
|
1426
|
+
}
|
|
1427
|
+
|
|
1428
|
+
/**
|
|
1429
|
+
* The component's own statement route. The request carries nothing: the
|
|
1430
|
+
* statement is the route's, and runs verbatim, so it has no language either.
|
|
1431
|
+
*/
|
|
1432
|
+
const statementReadOf = (routeName: string): ComponentStateRead => ({
|
|
1433
|
+
setup: [],
|
|
1434
|
+
url: types.stringLiteral(`/api/${COMPONENT_STATE_ROUTE_FOLDER.slice(2).join('/')}/${routeName}`),
|
|
1435
|
+
deps: [],
|
|
1436
|
+
csvColumns: null,
|
|
1437
|
+
})
|
|
1438
|
+
|
|
1439
|
+
/**
|
|
1440
|
+
* The effect that fills the states of one fetch group once the component is on
|
|
1441
|
+
* screen:
|
|
1442
|
+
*
|
|
1443
|
+
* useEffect(() => {
|
|
1444
|
+
* let __active = true
|
|
1445
|
+
* <read.setup>
|
|
1446
|
+
* fetch(<read.url>)
|
|
1447
|
+
* .then((__res) => __res.json())
|
|
1448
|
+
* .then((__result) => {
|
|
1449
|
+
* if (!__active || !__result || __result.success !== true) return
|
|
1450
|
+
* const __data = __result.data
|
|
1451
|
+
* set<State>(…)
|
|
1452
|
+
* })
|
|
1453
|
+
* .catch((__err) => console.error(…))
|
|
1454
|
+
* return () => { __active = false }
|
|
1455
|
+
* }, [<read.deps>])
|
|
1456
|
+
*/
|
|
1457
|
+
const buildComponentStateFetchEffect = (
|
|
1458
|
+
group: FetchGroup,
|
|
1459
|
+
read: ComponentStateRead
|
|
1460
|
+
): types.ExpressionStatement => {
|
|
1461
|
+
const active = types.identifier('__active')
|
|
1462
|
+
const result = types.identifier('__result')
|
|
1463
|
+
const data = types.identifier('__data')
|
|
1464
|
+
const resultData = types.memberExpression(result, types.identifier('data'))
|
|
1465
|
+
|
|
1466
|
+
const received = types.arrowFunctionExpression(
|
|
1467
|
+
[result],
|
|
1468
|
+
types.blockStatement([
|
|
1469
|
+
types.ifStatement(
|
|
1470
|
+
types.logicalExpression(
|
|
1471
|
+
'||',
|
|
1472
|
+
types.logicalExpression(
|
|
1473
|
+
'||',
|
|
1474
|
+
types.unaryExpression('!', active),
|
|
1475
|
+
types.unaryExpression('!', result)
|
|
1476
|
+
),
|
|
1477
|
+
types.binaryExpression(
|
|
1478
|
+
'!==',
|
|
1479
|
+
types.memberExpression(result, types.identifier('success')),
|
|
1480
|
+
types.booleanLiteral(true)
|
|
1481
|
+
)
|
|
1482
|
+
),
|
|
1483
|
+
types.returnStatement()
|
|
1484
|
+
),
|
|
1485
|
+
types.variableDeclaration('const', [
|
|
1486
|
+
types.variableDeclarator(
|
|
1487
|
+
data,
|
|
1488
|
+
read.csvColumns
|
|
1489
|
+
? types.callExpression(csvRowsRelabeller(read.csvColumns), [resultData])
|
|
1490
|
+
: resultData
|
|
1491
|
+
),
|
|
1492
|
+
]),
|
|
1493
|
+
...group.states.map((state) =>
|
|
1494
|
+
types.expressionStatement(
|
|
1495
|
+
types.callExpression(
|
|
1496
|
+
types.identifier(
|
|
1497
|
+
`set${state.stateKey.charAt(0).toUpperCase()}${state.stateKey.slice(1)}`
|
|
1498
|
+
),
|
|
1499
|
+
[buildComponentStateValue(data, state)]
|
|
1500
|
+
)
|
|
1501
|
+
)
|
|
1502
|
+
),
|
|
1503
|
+
])
|
|
1504
|
+
)
|
|
1505
|
+
|
|
1506
|
+
const then = (chain: types.Expression, handler: types.Expression): types.CallExpression =>
|
|
1507
|
+
types.callExpression(types.memberExpression(chain, types.identifier('then')), [handler])
|
|
1508
|
+
const fetchChain = types.callExpression(
|
|
1509
|
+
types.memberExpression(
|
|
1510
|
+
then(
|
|
1511
|
+
then(
|
|
1512
|
+
types.callExpression(types.identifier('fetch'), [read.url]),
|
|
1513
|
+
types.arrowFunctionExpression(
|
|
1514
|
+
[types.identifier('__res')],
|
|
1515
|
+
types.callExpression(
|
|
1516
|
+
types.memberExpression(types.identifier('__res'), types.identifier('json')),
|
|
1517
|
+
[]
|
|
1518
|
+
)
|
|
1519
|
+
)
|
|
1520
|
+
),
|
|
1521
|
+
received
|
|
1522
|
+
),
|
|
1523
|
+
types.identifier('catch')
|
|
1524
|
+
),
|
|
1525
|
+
[
|
|
1526
|
+
types.arrowFunctionExpression(
|
|
1527
|
+
[types.identifier('__err')],
|
|
1528
|
+
types.callExpression(
|
|
1529
|
+
types.memberExpression(types.identifier('console'), types.identifier('error')),
|
|
1530
|
+
[
|
|
1531
|
+
types.stringLiteral(
|
|
1532
|
+
`Error fetching component state (${group.states
|
|
1533
|
+
.map((state) => state.stateKey)
|
|
1534
|
+
.join(', ')}):`
|
|
1535
|
+
),
|
|
1536
|
+
types.identifier('__err'),
|
|
1537
|
+
]
|
|
1538
|
+
)
|
|
1539
|
+
),
|
|
1540
|
+
]
|
|
1541
|
+
)
|
|
1542
|
+
|
|
1543
|
+
return types.expressionStatement(
|
|
1544
|
+
types.callExpression(types.identifier('useEffect'), [
|
|
1545
|
+
types.arrowFunctionExpression(
|
|
1546
|
+
[],
|
|
1547
|
+
types.blockStatement([
|
|
1548
|
+
types.variableDeclaration('let', [
|
|
1549
|
+
types.variableDeclarator(active, types.booleanLiteral(true)),
|
|
1550
|
+
]),
|
|
1551
|
+
...read.setup,
|
|
1552
|
+
types.expressionStatement(fetchChain),
|
|
1553
|
+
types.returnStatement(
|
|
1554
|
+
types.arrowFunctionExpression(
|
|
1555
|
+
[],
|
|
1556
|
+
types.blockStatement([
|
|
1557
|
+
types.expressionStatement(
|
|
1558
|
+
types.assignmentExpression('=', active, types.booleanLiteral(false))
|
|
1559
|
+
),
|
|
1560
|
+
])
|
|
1561
|
+
)
|
|
1562
|
+
),
|
|
1563
|
+
])
|
|
1564
|
+
),
|
|
1565
|
+
types.arrayExpression(read.deps),
|
|
1566
|
+
])
|
|
1567
|
+
)
|
|
1568
|
+
}
|
|
1569
|
+
|
|
1570
|
+
/** The only source whose module runs a statement: the TeleportHQ database. */
|
|
1571
|
+
const STATEMENT_SOURCE_TYPE = 'teleport'
|
|
1572
|
+
|
|
1573
|
+
/**
|
|
1574
|
+
* Writes the route a component reads one statement-filled fetch group from
|
|
1575
|
+
* (`generateComponentStateRoute`) and answers its name — or null when the
|
|
1576
|
+
* generated site has nothing to run the statement with.
|
|
1577
|
+
*/
|
|
1578
|
+
const writeComponentStatementRoute = (
|
|
1579
|
+
structure: Parameters<ComponentPlugin>[0],
|
|
1580
|
+
group: FetchGroup
|
|
1581
|
+
): string | null => {
|
|
1582
|
+
const { uidl, options } = structure
|
|
1583
|
+
const extractedResources = options.extractedResources
|
|
1584
|
+
const statement = group.states[0].definition.query
|
|
1585
|
+
if (
|
|
1586
|
+
!extractedResources ||
|
|
1587
|
+
group.dataSource.type !== STATEMENT_SOURCE_TYPE ||
|
|
1588
|
+
!isSelectOnlyQuery(statement)
|
|
1589
|
+
) {
|
|
1590
|
+
return null
|
|
1591
|
+
}
|
|
1592
|
+
if (
|
|
1593
|
+
!ensureDataSourceModule(
|
|
1594
|
+
group.fileName,
|
|
1595
|
+
group.dataSource,
|
|
1596
|
+
group.tableName,
|
|
1597
|
+
extractedResources,
|
|
1598
|
+
buildProductTransformOptions(options)
|
|
1599
|
+
)
|
|
1600
|
+
) {
|
|
1601
|
+
return null
|
|
1602
|
+
}
|
|
1603
|
+
|
|
1604
|
+
const componentSlug = StringUtils.camelCaseToDashCase(sanitizeFileName(uidl.name || 'component'))
|
|
1605
|
+
const routeName = `${componentSlug}-${sanitizeFileName(group.tableName)}-${
|
|
1606
|
+
group.fetchDiscriminator
|
|
1607
|
+
}`
|
|
1608
|
+
const resourceKey = `${COMPONENT_STATE_ROUTE_FOLDER.join('/')}/${routeName}`
|
|
1609
|
+
if (!extractedResources[resourceKey]) {
|
|
1610
|
+
extractedResources[resourceKey] = {
|
|
1611
|
+
fileName: routeName,
|
|
1612
|
+
fileType: FileType.JS,
|
|
1613
|
+
path: [...COMPONENT_STATE_ROUTE_FOLDER],
|
|
1614
|
+
content: generateComponentStateRoute({
|
|
1615
|
+
moduleFileName: group.fileName,
|
|
1616
|
+
statement,
|
|
1617
|
+
// A component has no protection of its own: the route admits whoever
|
|
1618
|
+
// may open a page that renders it.
|
|
1619
|
+
exposure: DataExposure.componentExposureFor(options.dataExposure, uidl.name),
|
|
1620
|
+
trustedReaderRoles: TableAccess.resolveTrustedReaderRoles(options.auth),
|
|
1621
|
+
withRevocation: options.auth?.enabled === true,
|
|
1622
|
+
}),
|
|
1623
|
+
}
|
|
1624
|
+
}
|
|
1625
|
+
return routeName
|
|
1626
|
+
}
|
|
1627
|
+
|
|
1628
|
+
/**
|
|
1629
|
+
* Fills a COMPONENT's states bound to a data source.
|
|
1630
|
+
*
|
|
1631
|
+
* A page's bound states arrive as props from its own server-side fetch (the
|
|
1632
|
+
* plugin above). A component has no such fetch, and the page that renders it
|
|
1633
|
+
* knows nothing of its states — so every state of a component declared as
|
|
1634
|
+
* `useState(props.<state> !== undefined ? props.<state> : <default>)` kept its
|
|
1635
|
+
* default on the generated site, and a list over it stayed empty.
|
|
1636
|
+
*
|
|
1637
|
+
* The component now reads for itself once it is on screen:
|
|
1638
|
+
*
|
|
1639
|
+
* - a state filled from a TABLE, through the table's own read route
|
|
1640
|
+
* (`/api/<source>-<table>-<id>`) — the route a list bound straight to the
|
|
1641
|
+
* table reads inside a component. That route already counts a state filled
|
|
1642
|
+
* from the table among the table's readers
|
|
1643
|
+
* (`DataExposure.resolveDataExposure`), answers the rows a page's state is
|
|
1644
|
+
* given, and is asked for them in the page's language;
|
|
1645
|
+
* - a state filled by an SQL STATEMENT of its own, through a route of the
|
|
1646
|
+
* component's (`/api/component-state/<route>`) that holds the statement — a
|
|
1647
|
+
* browser may hand a read route none — and admits whoever may open a page
|
|
1648
|
+
* that renders the component (`generateComponentStateRoute`).
|
|
1649
|
+
*
|
|
1650
|
+
* A statement that names the signed-in visitor or the page's row
|
|
1651
|
+
* (`{{Current User.…}}`, `{{Current Page Entity.id}}`) is left to a workflow:
|
|
1652
|
+
* it is resolved for a page's states only.
|
|
1653
|
+
*/
|
|
1654
|
+
export const createComponentStateDataSourcePlugin: ComponentPluginFactory<{}> = () => {
|
|
1655
|
+
const componentStateDataSourcePlugin: ComponentPlugin = async (structure) => {
|
|
1656
|
+
const { uidl, chunks, dependencies, options } = structure
|
|
1657
|
+
const { stateDefinitions = {} } = uidl
|
|
1658
|
+
|
|
1659
|
+
const dataSources = options.dataSources
|
|
1660
|
+
if (!dataSources || Object.keys(dataSources).length === 0) {
|
|
1661
|
+
return structure
|
|
1662
|
+
}
|
|
1663
|
+
if (!Object.values(stateDefinitions).some((definition) => !!definition.dataSourceBinding)) {
|
|
1664
|
+
return structure
|
|
1665
|
+
}
|
|
1666
|
+
|
|
1667
|
+
const componentBody = findComponentBody(chunks)
|
|
1668
|
+
const extractedResources = options.extractedResources
|
|
1669
|
+
if (!componentBody || !extractedResources) {
|
|
1670
|
+
return structure
|
|
1671
|
+
}
|
|
1672
|
+
|
|
1673
|
+
const { buildTime, runtimeUser } = collectBoundStates(stateDefinitions)
|
|
1674
|
+
const localized = isLocalizedProject(options)
|
|
1675
|
+
const transformOptions = buildProductTransformOptions(options)
|
|
1676
|
+
const effects: types.Statement[] = []
|
|
1677
|
+
let readsATable = false
|
|
1678
|
+
|
|
1679
|
+
groupByDataSourceAndTable(
|
|
1680
|
+
buildTime.filter((state) => !hasOwnQuery(state)),
|
|
1681
|
+
dataSources
|
|
1682
|
+
).forEach((group) => {
|
|
1683
|
+
if (
|
|
1684
|
+
ensureDataSourceReadRoute(
|
|
1685
|
+
group.fileName,
|
|
1686
|
+
group.dataSource,
|
|
1687
|
+
group.tableName,
|
|
1688
|
+
extractedResources,
|
|
1689
|
+
transformOptions
|
|
1690
|
+
)
|
|
1691
|
+
) {
|
|
1692
|
+
effects.push(buildComponentStateFetchEffect(group, tableReadOf(group, localized)))
|
|
1693
|
+
readsATable = true
|
|
1694
|
+
}
|
|
1695
|
+
})
|
|
1696
|
+
|
|
1697
|
+
const leftToAWorkflow: BoundState[] = [...runtimeUser]
|
|
1698
|
+
groupByDataSourceAndTable(buildTime.filter(hasOwnQuery), dataSources).forEach((group) => {
|
|
1699
|
+
const routeName = writeComponentStatementRoute(structure, group)
|
|
1700
|
+
if (routeName) {
|
|
1701
|
+
effects.push(buildComponentStateFetchEffect(group, statementReadOf(routeName)))
|
|
1702
|
+
} else {
|
|
1703
|
+
leftToAWorkflow.push(...group.states)
|
|
1704
|
+
}
|
|
1705
|
+
})
|
|
1706
|
+
if (leftToAWorkflow.length > 0) {
|
|
1707
|
+
// eslint-disable-next-line no-console
|
|
1708
|
+
console.warn(
|
|
1709
|
+
`[state-data-source-plugin] Component "${uidl.name}": ${leftToAWorkflow
|
|
1710
|
+
.map((state) => `"${state.stateKey}"`)
|
|
1711
|
+
.join(
|
|
1712
|
+
', '
|
|
1713
|
+
)} is filled by an SQL statement the generated site does not run for a component (one that names the signed-in visitor or the page's row, or one that is not a SELECT over the TeleportHQ database). Fill it from a workflow that runs when the component loads.`
|
|
1714
|
+
)
|
|
1715
|
+
}
|
|
1716
|
+
|
|
1717
|
+
if (effects.length === 0) {
|
|
1718
|
+
return structure
|
|
1719
|
+
}
|
|
1720
|
+
|
|
1721
|
+
const returnIndex = componentBody.body.findIndex((statement) =>
|
|
1722
|
+
types.isReturnStatement(statement)
|
|
1723
|
+
)
|
|
1724
|
+
componentBody.body.splice(
|
|
1725
|
+
returnIndex === -1 ? componentBody.body.length : returnIndex,
|
|
1726
|
+
0,
|
|
1727
|
+
...effects
|
|
1728
|
+
)
|
|
1729
|
+
|
|
1730
|
+
dependencies.useEffect = {
|
|
1731
|
+
type: 'library',
|
|
1732
|
+
path: 'react',
|
|
1733
|
+
version: '>=16.8.0',
|
|
1734
|
+
meta: { namedImport: true },
|
|
1735
|
+
}
|
|
1736
|
+
if (localized && readsATable) {
|
|
1737
|
+
ensureRouterDeclaration(componentBody.body, dependencies)
|
|
1738
|
+
}
|
|
1739
|
+
|
|
1740
|
+
return structure
|
|
1741
|
+
}
|
|
1742
|
+
|
|
1743
|
+
return componentStateDataSourcePlugin
|
|
1744
|
+
}
|