@teleporthq/teleport-project-generator-next 0.45.19 → 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.
Files changed (41) hide show
  1. package/__tests__/_helpers/data-exposure-fixture.ts +114 -44
  2. package/__tests__/component-bound-state.test.ts +817 -0
  3. package/__tests__/nav-active-links-locale.test.ts +279 -0
  4. package/__tests__/page-transition-end2end.test.ts +3 -3
  5. package/__tests__/page-transition-language-switch.test.ts +273 -0
  6. package/__tests__/security-runtime-require-paths.test.ts +29 -1
  7. package/dist/cjs/index.d.ts.map +1 -1
  8. package/dist/cjs/index.js +2 -0
  9. package/dist/cjs/index.js.map +1 -1
  10. package/dist/cjs/nav-active-link/nav-active-link-component.d.ts +1 -1
  11. package/dist/cjs/nav-active-link/nav-active-link-component.d.ts.map +1 -1
  12. package/dist/cjs/nav-active-link/nav-active-link-component.js +12 -4
  13. package/dist/cjs/nav-active-link/nav-active-link-component.js.map +1 -1
  14. package/dist/cjs/page-transition/page-transition-component.d.ts.map +1 -1
  15. package/dist/cjs/page-transition/page-transition-component.js +1 -1
  16. package/dist/cjs/page-transition/page-transition-component.js.map +1 -1
  17. package/dist/cjs/state-data-source-plugin.d.ts +27 -0
  18. package/dist/cjs/state-data-source-plugin.d.ts.map +1 -1
  19. package/dist/cjs/state-data-source-plugin.js +278 -4
  20. package/dist/cjs/state-data-source-plugin.js.map +1 -1
  21. package/dist/cjs/tsconfig.tsbuildinfo +1 -1
  22. package/dist/esm/index.d.ts.map +1 -1
  23. package/dist/esm/index.js +3 -1
  24. package/dist/esm/index.js.map +1 -1
  25. package/dist/esm/nav-active-link/nav-active-link-component.d.ts +1 -1
  26. package/dist/esm/nav-active-link/nav-active-link-component.d.ts.map +1 -1
  27. package/dist/esm/nav-active-link/nav-active-link-component.js +12 -4
  28. package/dist/esm/nav-active-link/nav-active-link-component.js.map +1 -1
  29. package/dist/esm/page-transition/page-transition-component.d.ts.map +1 -1
  30. package/dist/esm/page-transition/page-transition-component.js +1 -1
  31. package/dist/esm/page-transition/page-transition-component.js.map +1 -1
  32. package/dist/esm/state-data-source-plugin.d.ts +27 -0
  33. package/dist/esm/state-data-source-plugin.d.ts.map +1 -1
  34. package/dist/esm/state-data-source-plugin.js +278 -5
  35. package/dist/esm/state-data-source-plugin.js.map +1 -1
  36. package/dist/esm/tsconfig.tsbuildinfo +1 -1
  37. package/package.json +21 -21
  38. package/src/index.ts +6 -1
  39. package/src/nav-active-link/nav-active-link-component.ts +68 -5
  40. package/src/page-transition/page-transition-component.ts +19 -5
  41. package/src/state-data-source-plugin.ts +433 -9
@@ -322,29 +322,9 @@ export interface RouteReply {
322
322
  params: unknown[][]
323
323
  }
324
324
 
325
- /**
326
- * The read route a generated page fetches its table from, booted from the
327
- * generated files: `pages/api/<file>.js` (inline, or re-exporting the
328
- * `utils/data-sources/<file>.js` module), run against a fake `pg` that answers
329
- * every SELECT with `rows`, and a fake session.
330
- */
331
- export const bootReadRoute = (
332
- root: GeneratedFolder,
333
- fileName: string,
334
- rows: any[] = [{ id: 'r1', name: 'Row', status: 'active', password: 'hash-x' }]
335
- ): ((call?: RouteCall) => Promise<RouteReply>) => {
336
- const apiSource = fileText(root, ['pages', 'api'], fileName)
337
- if (apiSource === null) {
338
- throw new Error('no route pages/api/' + fileName)
339
- }
340
- const reExport = /import dataSourceModule from '\.\.\/\.\.\/utils\/data-sources\/([^']+)'/.exec(
341
- apiSource
342
- )
343
- const moduleSource = reExport ? fileText(root, ['utils', 'data-sources'], reExport[1]) : apiSource
344
- if (moduleSource === null) {
345
- throw new Error('no module for ' + fileName)
346
- }
347
- const code = moduleSource
325
+ /** A generated data-source module, as CommonJS that exports what it defines. */
326
+ const moduleAsCommonJs = (moduleSource: string): string =>
327
+ moduleSource
348
328
  .replace(
349
329
  "import { Client, types as pgTypes } from 'pg'",
350
330
  "const { Client, types: pgTypes } = require('pg')"
@@ -357,13 +337,16 @@ export const bootReadRoute = (
357
337
  .concat(
358
338
  '\nmodule.exports = {' +
359
339
  ' handler: typeof cachedHandler !== "undefined" ? cachedHandler : handler,' +
360
- ' getCount: typeof cachedGetCount !== "undefined" ? cachedGetCount : (typeof getCount !== "undefined" ? getCount : null)' +
340
+ ' getCount: typeof cachedGetCount !== "undefined" ? cachedGetCount : (typeof getCount !== "undefined" ? getCount : null),' +
341
+ ' fetchData: typeof fetchData !== "undefined" ? fetchData : null' +
361
342
  ' }'
362
343
  )
363
- const exported = /export default dataSourceModule\.getCount/.test(apiSource)
364
- ? 'getCount'
365
- : 'handler'
366
344
 
345
+ /**
346
+ * What a booted route runs against: a fake `pg` that answers every SELECT with
347
+ * `rows`, and a fake session.
348
+ */
349
+ const createFakeRuntime = (rows: any[]) => {
367
350
  const queries: string[] = []
368
351
  const params: unknown[][] = []
369
352
  let session: RouteCall['session'] = null
@@ -408,24 +391,35 @@ export const bootReadRoute = (
408
391
  }
409
392
  throw new Error('unexpected require: ' + name)
410
393
  }
411
- const moduleObj: { exports: any } = { exports: {} }
412
- new Function('require', 'module', 'exports', 'process', 'console', code)(
413
- fakeRequire,
414
- moduleObj,
415
- moduleObj.exports,
416
- {
417
- env: {
418
- NEXTAUTH_SECRET: 'server-secret',
419
- TELEPORT_DB_HOST: 'h',
420
- TELEPORT_DB_USER: 'u',
421
- TELEPORT_DB_PASSWORD: 'p',
422
- TELEPORT_DB_NAME: 'd',
394
+
395
+ /** Runs CommonJS `code` and answers what it exports. `globals` are in its scope. */
396
+ const load = (code: string, globals: Record<string, unknown> = {}): any => {
397
+ const moduleObj: { exports: any } = { exports: {} }
398
+ const names = Object.keys(globals)
399
+ new Function('require', 'module', 'exports', 'process', 'console', ...names, code)(
400
+ fakeRequire,
401
+ moduleObj,
402
+ moduleObj.exports,
403
+ {
404
+ env: {
405
+ NEXTAUTH_SECRET: 'server-secret',
406
+ TELEPORT_DB_HOST: 'h',
407
+ TELEPORT_DB_USER: 'u',
408
+ TELEPORT_DB_PASSWORD: 'p',
409
+ TELEPORT_DB_NAME: 'd',
410
+ },
423
411
  },
424
- },
425
- { log: (): void => undefined, warn: (): void => undefined, error: (): void => undefined }
426
- )
412
+ { log: (): void => undefined, warn: (): void => undefined, error: (): void => undefined },
413
+ ...names.map((name) => globals[name])
414
+ )
415
+ return moduleObj.exports
416
+ }
427
417
 
428
- return async (call: RouteCall = {}) => {
418
+ /** Sends `call` to `handler` and answers what it replied, and what it asked the database. */
419
+ const send = async (
420
+ handler: (req: any, res: any) => Promise<unknown>,
421
+ call: RouteCall = {}
422
+ ): Promise<RouteReply> => {
429
423
  session = call.session || null
430
424
  entitled = call.entitled === true
431
425
  queries.length = 0
@@ -448,7 +442,83 @@ export const bootReadRoute = (
448
442
  ...(session ? { cookie: 'next-auth.session-token=signed' } : {}),
449
443
  ...(call.headers || {}),
450
444
  }
451
- await moduleObj.exports[exported]({ method: 'GET', query: call.query || {}, headers }, res)
445
+ await handler({ method: 'GET', query: call.query || {}, headers }, res)
452
446
  return { ...reply, queries: [...queries], params: [...params] }
453
447
  }
448
+
449
+ return { load, send }
450
+ }
451
+
452
+ const DEFAULT_ROWS = [{ id: 'r1', name: 'Row', status: 'active', password: 'hash-x' }]
453
+
454
+ /**
455
+ * The read route a generated page fetches its table from, booted from the
456
+ * generated files: `pages/api/<file>.js` (inline, or re-exporting the
457
+ * `utils/data-sources/<file>.js` module), run against a fake `pg` that answers
458
+ * every SELECT with `rows`, and a fake session.
459
+ */
460
+ export const bootReadRoute = (
461
+ root: GeneratedFolder,
462
+ fileName: string,
463
+ rows: any[] = DEFAULT_ROWS
464
+ ): ((call?: RouteCall) => Promise<RouteReply>) => {
465
+ const apiSource = fileText(root, ['pages', 'api'], fileName)
466
+ if (apiSource === null) {
467
+ throw new Error('no route pages/api/' + fileName)
468
+ }
469
+ const reExport = /import dataSourceModule from '\.\.\/\.\.\/utils\/data-sources\/([^']+)'/.exec(
470
+ apiSource
471
+ )
472
+ const moduleSource = reExport ? fileText(root, ['utils', 'data-sources'], reExport[1]) : apiSource
473
+ if (moduleSource === null) {
474
+ throw new Error('no module for ' + fileName)
475
+ }
476
+ const exported = /export default dataSourceModule\.getCount/.test(apiSource)
477
+ ? 'getCount'
478
+ : 'handler'
479
+
480
+ const runtime = createFakeRuntime(rows)
481
+ const booted = runtime.load(moduleAsCommonJs(moduleSource))
482
+ return (call: RouteCall = {}) => runtime.send(booted[exported], call)
483
+ }
484
+
485
+ export const COMPONENT_STATE_FOLDER = ['pages', 'api', 'component-state']
486
+
487
+ /** The routes a project's components read their own statements from, by file name. */
488
+ export const componentStateRoutesOf = (root: GeneratedFolder): string[] =>
489
+ (folderAt(root, COMPONENT_STATE_FOLDER)?.files || []).map((file) => file.name)
490
+
491
+ /**
492
+ * The route a component reads a statement-filled state from
493
+ * (`pages/api/component-state/<file>.js`), booted with the data-source module
494
+ * it imports, against the same fake `pg` and session.
495
+ */
496
+ export const bootComponentStateRoute = (
497
+ root: GeneratedFolder,
498
+ fileName: string,
499
+ rows: any[] = DEFAULT_ROWS
500
+ ): ((call?: RouteCall) => Promise<RouteReply>) => {
501
+ const routeSource = fileText(root, COMPONENT_STATE_FOLDER, fileName)
502
+ if (routeSource === null) {
503
+ throw new Error('no route pages/api/component-state/' + fileName)
504
+ }
505
+ const imported =
506
+ /import dataSourceModule from '\.\.\/\.\.\/\.\.\/utils\/data-sources\/([^']+)'/.exec(
507
+ routeSource
508
+ )
509
+ const moduleSource = imported ? fileText(root, ['utils', 'data-sources'], imported[1]) : null
510
+ if (moduleSource === null) {
511
+ throw new Error('no data-source module for ' + fileName)
512
+ }
513
+
514
+ const runtime = createFakeRuntime(rows)
515
+ const dataSourceModule = runtime.load(moduleAsCommonJs(moduleSource))
516
+ const booted = runtime.load(
517
+ routeSource
518
+ .replace(/^import .*$/gm, '')
519
+ .replace(/^export default async function handler/m, 'async function handler')
520
+ .concat('\nmodule.exports = { handler }'),
521
+ { dataSourceModule }
522
+ )
523
+ return (call: RouteCall = {}) => runtime.send(booted.handler, call)
454
524
  }