@teleporthq/teleport-plugin-next-data-source 0.43.55 → 0.43.57

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 (101) hide show
  1. package/__tests__/array-mapper-cache-wiring.test.ts +425 -0
  2. package/__tests__/data-source-cache-wrapping.test.ts +152 -0
  3. package/__tests__/ecommerce-product-storefront-tax.test.ts +118 -0
  4. package/dist/cjs/cache/ast.d.ts +71 -0
  5. package/dist/cjs/cache/ast.d.ts.map +1 -0
  6. package/dist/cjs/cache/ast.js +229 -0
  7. package/dist/cjs/cache/ast.js.map +1 -0
  8. package/dist/cjs/cache/config.d.ts +22 -0
  9. package/dist/cjs/cache/config.d.ts.map +1 -0
  10. package/dist/cjs/cache/config.js +67 -0
  11. package/dist/cjs/cache/config.js.map +1 -0
  12. package/dist/cjs/cache/types.d.ts +31 -0
  13. package/dist/cjs/cache/types.d.ts.map +1 -0
  14. package/dist/cjs/cache/types.js +3 -0
  15. package/dist/cjs/cache/types.js.map +1 -0
  16. package/dist/cjs/data-source-fetchers.d.ts +5 -3
  17. package/dist/cjs/data-source-fetchers.d.ts.map +1 -1
  18. package/dist/cjs/data-source-fetchers.js +51 -8
  19. package/dist/cjs/data-source-fetchers.js.map +1 -1
  20. package/dist/cjs/fetchers/teleport.d.ts +2 -2
  21. package/dist/cjs/fetchers/teleport.d.ts.map +1 -1
  22. package/dist/cjs/fetchers/teleport.js +3 -2
  23. package/dist/cjs/fetchers/teleport.js.map +1 -1
  24. package/dist/cjs/index.d.ts +2 -0
  25. package/dist/cjs/index.d.ts.map +1 -1
  26. package/dist/cjs/index.js +14 -9
  27. package/dist/cjs/index.js.map +1 -1
  28. package/dist/cjs/loading-state.d.ts.map +1 -1
  29. package/dist/cjs/loading-state.js +74 -2
  30. package/dist/cjs/loading-state.js.map +1 -1
  31. package/dist/cjs/pagination-plugin.d.ts.map +1 -1
  32. package/dist/cjs/pagination-plugin.js +204 -64
  33. package/dist/cjs/pagination-plugin.js.map +1 -1
  34. package/dist/cjs/transformations/ecommerce-product.d.ts +15 -1
  35. package/dist/cjs/transformations/ecommerce-product.d.ts.map +1 -1
  36. package/dist/cjs/transformations/ecommerce-product.js +7 -3
  37. package/dist/cjs/transformations/ecommerce-product.js.map +1 -1
  38. package/dist/cjs/transformations/index.d.ts +13 -2
  39. package/dist/cjs/transformations/index.d.ts.map +1 -1
  40. package/dist/cjs/transformations/index.js +21 -3
  41. package/dist/cjs/transformations/index.js.map +1 -1
  42. package/dist/cjs/tsconfig.tsbuildinfo +1 -1
  43. package/dist/cjs/utils.d.ts +4 -3
  44. package/dist/cjs/utils.d.ts.map +1 -1
  45. package/dist/cjs/utils.js +6 -4
  46. package/dist/cjs/utils.js.map +1 -1
  47. package/dist/esm/cache/ast.d.ts +71 -0
  48. package/dist/esm/cache/ast.d.ts.map +1 -0
  49. package/dist/esm/cache/ast.js +196 -0
  50. package/dist/esm/cache/ast.js.map +1 -0
  51. package/dist/esm/cache/config.d.ts +22 -0
  52. package/dist/esm/cache/config.d.ts.map +1 -0
  53. package/dist/esm/cache/config.js +62 -0
  54. package/dist/esm/cache/config.js.map +1 -0
  55. package/dist/esm/cache/types.d.ts +31 -0
  56. package/dist/esm/cache/types.d.ts.map +1 -0
  57. package/dist/esm/cache/types.js +2 -0
  58. package/dist/esm/cache/types.js.map +1 -0
  59. package/dist/esm/data-source-fetchers.d.ts +5 -3
  60. package/dist/esm/data-source-fetchers.d.ts.map +1 -1
  61. package/dist/esm/data-source-fetchers.js +51 -8
  62. package/dist/esm/data-source-fetchers.js.map +1 -1
  63. package/dist/esm/fetchers/teleport.d.ts +2 -2
  64. package/dist/esm/fetchers/teleport.d.ts.map +1 -1
  65. package/dist/esm/fetchers/teleport.js +3 -2
  66. package/dist/esm/fetchers/teleport.js.map +1 -1
  67. package/dist/esm/index.d.ts +2 -0
  68. package/dist/esm/index.d.ts.map +1 -1
  69. package/dist/esm/index.js +12 -8
  70. package/dist/esm/index.js.map +1 -1
  71. package/dist/esm/loading-state.d.ts.map +1 -1
  72. package/dist/esm/loading-state.js +74 -2
  73. package/dist/esm/loading-state.js.map +1 -1
  74. package/dist/esm/pagination-plugin.d.ts.map +1 -1
  75. package/dist/esm/pagination-plugin.js +205 -65
  76. package/dist/esm/pagination-plugin.js.map +1 -1
  77. package/dist/esm/transformations/ecommerce-product.d.ts +15 -1
  78. package/dist/esm/transformations/ecommerce-product.d.ts.map +1 -1
  79. package/dist/esm/transformations/ecommerce-product.js +7 -3
  80. package/dist/esm/transformations/ecommerce-product.js.map +1 -1
  81. package/dist/esm/transformations/index.d.ts +13 -2
  82. package/dist/esm/transformations/index.d.ts.map +1 -1
  83. package/dist/esm/transformations/index.js +20 -3
  84. package/dist/esm/transformations/index.js.map +1 -1
  85. package/dist/esm/tsconfig.tsbuildinfo +1 -1
  86. package/dist/esm/utils.d.ts +4 -3
  87. package/dist/esm/utils.d.ts.map +1 -1
  88. package/dist/esm/utils.js +6 -4
  89. package/dist/esm/utils.js.map +1 -1
  90. package/package.json +5 -5
  91. package/src/cache/ast.ts +265 -0
  92. package/src/cache/config.ts +79 -0
  93. package/src/cache/types.ts +31 -0
  94. package/src/data-source-fetchers.ts +66 -12
  95. package/src/fetchers/teleport.ts +3 -3
  96. package/src/index.ts +10 -4
  97. package/src/loading-state.ts +65 -2
  98. package/src/pagination-plugin.ts +315 -116
  99. package/src/transformations/ecommerce-product.ts +53 -2
  100. package/src/transformations/index.ts +25 -4
  101. package/src/utils.ts +10 -5
@@ -1,4 +1,20 @@
1
1
  import type { UIDLEcommerceCategory } from '@teleporthq/teleport-types'
2
+ import { StorefrontTax } from '@teleporthq/teleport-shared'
3
+
4
+ /**
5
+ * Everything the product transform needs baked in at export time, beyond the
6
+ * row itself. An options OBJECT rather than positional parameters: the list has
7
+ * already grown twice and every caller in the chain has to forward it verbatim.
8
+ */
9
+ export interface EcommerceProductTransformOptions {
10
+ /** Category taxonomy — lives only in the UIDL, there is no DB table for it. */
11
+ categories?: UIDLEcommerceCategory[]
12
+ /**
13
+ * Percentage to ADD on top of every stored (NET) price for DISPLAY, or 0.
14
+ * Resolved by the caller through `StorefrontTax.resolveStorefrontTaxRate`.
15
+ */
16
+ storefrontTaxRate?: number
17
+ }
2
18
 
3
19
  /**
4
20
  * Flattens the nested category tree (`ecommerceSettings.categories`, baked at
@@ -47,11 +63,16 @@ const flattenCategoriesById = (
47
63
  * that UIDL components expect.
48
64
  */
49
65
  export const generateEcommerceProductTransformationCode = (
50
- categories?: UIDLEcommerceCategory[]
66
+ options: EcommerceProductTransformOptions = {}
51
67
  ): string => {
52
- const categoriesByIdJson = JSON.stringify(flattenCategoriesById(categories))
68
+ const categoriesByIdJson = JSON.stringify(flattenCategoriesById(options.categories))
69
+ const taxHelperCode = StorefrontTax.generateStorefrontTaxHelperCode(
70
+ options.storefrontTaxRate ?? 0
71
+ )
53
72
 
54
73
  return `
74
+ ${taxHelperCode}
75
+
55
76
  // Category taxonomy (id -> {name, slug, translations}), baked in at export
56
77
  // time — see flattenCategoriesById in ecommerce-product.ts. Empty when the
57
78
  // store has no category taxonomy, or products.category_ids simply resolves
@@ -349,6 +370,32 @@ function buildEcommerceProduct(record, options) {
349
370
  var hasInStockCombination = !!(firstVariant && isVariantInStock(firstVariant))
350
371
  var defaultVariantId = hasInStockCombination ? firstVariant.id : ''
351
372
  var defaultVariantPrice = firstVariant ? formatVariantPrice(firstVariant.price, price) : ''
373
+ // Gross (tax-inclusive) counterparts of the three price fields the STOREFRONT
374
+ // renders. \`price\` / \`defaultVariantPrice\` / \`variantsJson\` deliberately stay
375
+ // NET: the same transform feeds the merchant's admin panel, and every write
376
+ // path (add-to-cart, checkout, the invoice route) re-reads the raw row and
377
+ // must keep seeing the price the merchant typed. Only these three are safe to
378
+ // gross, because nothing writes them back.
379
+ //
380
+ // MUST mirror packages/renderer/src/utils/ecommerce-products.ts.
381
+ var displayPrice = grossMoney(price)
382
+ var defaultVariantDisplayPrice = firstVariant
383
+ ? grossMoney(formatVariantPrice(firstVariant.price, price))
384
+ : ''
385
+ var variantsDisplay = []
386
+ for (var dv = 0; dv < variants.length; dv++) {
387
+ var dvRow = variants[dv]
388
+ variantsDisplay.push({
389
+ id: dvRow.id,
390
+ options: dvRow.options,
391
+ // A null variant price INHERITS the product price, so gross the RESOLVED
392
+ // value — otherwise an inheriting combination would fall back to the net
393
+ // base price in the picker while an overriding one showed gross.
394
+ price: applyStorefrontTax(dvRow.price == null ? price : dvRow.price),
395
+ quantity: dvRow.quantity,
396
+ image_url: dvRow.image_url,
397
+ })
398
+ }
352
399
  // Can this product be added to the cart at all, before the shopper touches the
353
400
  // picker? A STRING, same convention as outOfStock/requiresVariantSelection.
354
401
  //
@@ -373,6 +420,7 @@ function buildEcommerceProduct(record, options) {
373
420
  // combinations + axes from these JSON strings via getAttribute.
374
421
  var variantOptionsJson = safeStringifyJson(variantOptions)
375
422
  var variantsJson = safeStringifyJson(variants)
423
+ var variantsDisplayJson = safeStringifyJson(variantsDisplay)
376
424
 
377
425
  return {
378
426
  id: id,
@@ -418,8 +466,11 @@ function buildEcommerceProduct(record, options) {
418
466
  variants: variants,
419
467
  variantOptionsJson: variantOptionsJson,
420
468
  variantsJson: variantsJson,
469
+ variantsDisplayJson: variantsDisplayJson,
421
470
  defaultVariantId: defaultVariantId,
422
471
  defaultVariantPrice: defaultVariantPrice,
472
+ displayPrice: displayPrice,
473
+ defaultVariantDisplayPrice: defaultVariantDisplayPrice,
423
474
  hasPurchasableVariant: hasPurchasableVariant,
424
475
  }
425
476
  }
@@ -1,7 +1,28 @@
1
- import type { UIDLEcommerceCategory } from '@teleporthq/teleport-types'
1
+ import type { GeneratorOptions } from '@teleporthq/teleport-types'
2
+ import { StorefrontTax } from '@teleporthq/teleport-shared'
2
3
  import { generateSharedTransformationCode } from './shared-utils'
3
4
  import { generateBlogPostTransformationCode } from './blog-post'
4
- import { generateEcommerceProductTransformationCode } from './ecommerce-product'
5
+ import {
6
+ generateEcommerceProductTransformationCode,
7
+ type EcommerceProductTransformOptions,
8
+ } from './ecommerce-product'
9
+
10
+ export type { EcommerceProductTransformOptions }
11
+
12
+ /**
13
+ * Collects everything the product transform bakes in from the generator
14
+ * options, in ONE place so every fetcher-emitting call site stays in step.
15
+ *
16
+ * `storefrontTaxRate` is what turns a NET catalogue price into the price the
17
+ * shopper is quoted; `resolveStorefrontTaxRate` returns 0 (a no-op) for
18
+ * tax-inclusive or untaxed stores.
19
+ */
20
+ export const buildProductTransformOptions = (
21
+ options: Pick<GeneratorOptions, 'ecommerceSettings' | 'invoiceSettings'>
22
+ ): EcommerceProductTransformOptions => ({
23
+ categories: options.ecommerceSettings?.categories,
24
+ storefrontTaxRate: StorefrontTax.resolveStorefrontTaxRate(options.invoiceSettings),
25
+ })
5
26
 
6
27
  export type TransformationType = 'blog-post' | 'ecommerce-product' | null
7
28
 
@@ -51,7 +72,7 @@ export const detectTransformationType = (tableName: string): TransformationType
51
72
  */
52
73
  export const getTransformationCode = (
53
74
  tableName: string,
54
- categories?: UIDLEcommerceCategory[]
75
+ options: EcommerceProductTransformOptions = {}
55
76
  ): string => {
56
77
  const type = detectTransformationType(tableName)
57
78
  if (!type) {
@@ -64,7 +85,7 @@ export const getTransformationCode = (
64
85
  case 'blog-post':
65
86
  return shared + generateBlogPostTransformationCode()
66
87
  case 'ecommerce-product':
67
- return shared + generateEcommerceProductTransformationCode(categories)
88
+ return shared + generateEcommerceProductTransformationCode(options)
68
89
  default:
69
90
  return ''
70
91
  }
package/src/utils.ts CHANGED
@@ -7,12 +7,12 @@ import {
7
7
  UIDLDataSource,
8
8
  DataSourceType,
9
9
  ChunkType,
10
- UIDLEcommerceCategory,
11
10
  } from '@teleporthq/teleport-types'
12
11
  import * as types from '@babel/types'
13
12
  import { ASTUtils } from '@teleporthq/teleport-plugin-common'
14
13
  import { StringUtils } from '@teleporthq/teleport-shared'
15
14
  import { generateDataSourceFetcherWithCore } from './data-source-fetchers'
15
+ import type { EcommerceProductTransformOptions } from './transformations'
16
16
  import { DATA_SOURCE_ISR_REVALIDATE_SECONDS } from './isr'
17
17
 
18
18
  const VALID_DATA_SOURCE_TYPES: DataSourceType[] = [
@@ -192,7 +192,7 @@ export const extractDataSourceIntoNextAPIFolder = (
192
192
  dataSources: Record<string, UIDLDataSource>,
193
193
  componentChunk: ChunkDefinition,
194
194
  extractedResources: GeneratorOptions['extractedResources'],
195
- categories?: UIDLEcommerceCategory[]
195
+ transformOptions: EcommerceProductTransformOptions = {}
196
196
  ) => {
197
197
  try {
198
198
  // Validate node content structure
@@ -446,7 +446,12 @@ export default dataSourceModule.handler
446
446
  // Generate fetcher code for API route (exports just the handler)
447
447
  let fetcherCode: string
448
448
  try {
449
- fetcherCode = generateDataSourceFetcherWithCore(dataSource, tableName || '', true, categories)
449
+ fetcherCode = generateDataSourceFetcherWithCore(
450
+ dataSource,
451
+ tableName || '',
452
+ true,
453
+ transformOptions
454
+ )
450
455
  } catch (error) {
451
456
  return
452
457
  }
@@ -896,7 +901,7 @@ export const extractDataSourceIntoGetStaticProps = (
896
901
  dependencies: Record<string, any>,
897
902
  dynamicRouteAttr?: string,
898
903
  folderPath?: string[],
899
- categories?: UIDLEcommerceCategory[]
904
+ transformOptions: EcommerceProductTransformOptions = {}
900
905
  ): { success: boolean; chunk?: any } => {
901
906
  try {
902
907
  // Validate node content
@@ -1224,7 +1229,7 @@ export const extractDataSourceIntoGetStaticProps = (
1224
1229
  dataSource,
1225
1230
  tableName || '',
1226
1231
  false,
1227
- categories
1232
+ transformOptions
1228
1233
  )
1229
1234
  } catch (error) {
1230
1235
  // tslint:disable-next-line:no-console