@teleporthq/teleport-plugin-next-data-source 0.40.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.
Files changed (240) hide show
  1. package/ARRAY_MAPPER_PAGINATION.md +1128 -0
  2. package/LICENSE +21 -0
  3. package/README.md +40 -0
  4. package/SEARCH_IMPLEMENTATION_SUMMARY.md +983 -0
  5. package/__tests__/fetchers.test.ts +545 -0
  6. package/__tests__/integration.test.ts +561 -0
  7. package/__tests__/mocks.ts +241 -0
  8. package/__tests__/pagination.test.ts +31 -0
  9. package/__tests__/plugin.test.ts +577 -0
  10. package/__tests__/utils.test.ts +430 -0
  11. package/__tests__/validation.test.ts +348 -0
  12. package/dist/cjs/array-mapper-pagination.d.ts +32 -0
  13. package/dist/cjs/array-mapper-pagination.d.ts.map +1 -0
  14. package/dist/cjs/array-mapper-pagination.js +77 -0
  15. package/dist/cjs/array-mapper-pagination.js.map +1 -0
  16. package/dist/cjs/count-fetchers.d.ts +12 -0
  17. package/dist/cjs/count-fetchers.d.ts.map +1 -0
  18. package/dist/cjs/count-fetchers.js +46 -0
  19. package/dist/cjs/count-fetchers.js.map +1 -0
  20. package/dist/cjs/data-source-fetchers.d.ts +14 -0
  21. package/dist/cjs/data-source-fetchers.d.ts.map +1 -0
  22. package/dist/cjs/data-source-fetchers.js +185 -0
  23. package/dist/cjs/data-source-fetchers.js.map +1 -0
  24. package/dist/cjs/fetchers/airtable.d.ts +6 -0
  25. package/dist/cjs/fetchers/airtable.d.ts.map +1 -0
  26. package/dist/cjs/fetchers/airtable.js +27 -0
  27. package/dist/cjs/fetchers/airtable.js.map +1 -0
  28. package/dist/cjs/fetchers/clickhouse.d.ts +6 -0
  29. package/dist/cjs/fetchers/clickhouse.d.ts.map +1 -0
  30. package/dist/cjs/fetchers/clickhouse.js +29 -0
  31. package/dist/cjs/fetchers/clickhouse.js.map +1 -0
  32. package/dist/cjs/fetchers/csv-file.d.ts +7 -0
  33. package/dist/cjs/fetchers/csv-file.d.ts.map +1 -0
  34. package/dist/cjs/fetchers/csv-file.js +36 -0
  35. package/dist/cjs/fetchers/csv-file.js.map +1 -0
  36. package/dist/cjs/fetchers/firestore.d.ts +6 -0
  37. package/dist/cjs/fetchers/firestore.d.ts.map +1 -0
  38. package/dist/cjs/fetchers/firestore.js +35 -0
  39. package/dist/cjs/fetchers/firestore.js.map +1 -0
  40. package/dist/cjs/fetchers/google-sheets.d.ts +6 -0
  41. package/dist/cjs/fetchers/google-sheets.d.ts.map +1 -0
  42. package/dist/cjs/fetchers/google-sheets.js +30 -0
  43. package/dist/cjs/fetchers/google-sheets.js.map +1 -0
  44. package/dist/cjs/fetchers/index.d.ts +17 -0
  45. package/dist/cjs/fetchers/index.d.ts.map +1 -0
  46. package/dist/cjs/fetchers/index.js +56 -0
  47. package/dist/cjs/fetchers/index.js.map +1 -0
  48. package/dist/cjs/fetchers/javascript.d.ts +7 -0
  49. package/dist/cjs/fetchers/javascript.d.ts.map +1 -0
  50. package/dist/cjs/fetchers/javascript.js +40 -0
  51. package/dist/cjs/fetchers/javascript.js.map +1 -0
  52. package/dist/cjs/fetchers/mariadb.d.ts +3 -0
  53. package/dist/cjs/fetchers/mariadb.d.ts.map +1 -0
  54. package/dist/cjs/fetchers/mariadb.js +23 -0
  55. package/dist/cjs/fetchers/mariadb.js.map +1 -0
  56. package/dist/cjs/fetchers/mongodb.d.ts +7 -0
  57. package/dist/cjs/fetchers/mongodb.d.ts.map +1 -0
  58. package/dist/cjs/fetchers/mongodb.js +52 -0
  59. package/dist/cjs/fetchers/mongodb.js.map +1 -0
  60. package/dist/cjs/fetchers/mysql.d.ts +3 -0
  61. package/dist/cjs/fetchers/mysql.d.ts.map +1 -0
  62. package/dist/cjs/fetchers/mysql.js +30 -0
  63. package/dist/cjs/fetchers/mysql.js.map +1 -0
  64. package/dist/cjs/fetchers/postgresql.d.ts +3 -0
  65. package/dist/cjs/fetchers/postgresql.d.ts.map +1 -0
  66. package/dist/cjs/fetchers/postgresql.js +25 -0
  67. package/dist/cjs/fetchers/postgresql.js.map +1 -0
  68. package/dist/cjs/fetchers/redis.d.ts +6 -0
  69. package/dist/cjs/fetchers/redis.d.ts.map +1 -0
  70. package/dist/cjs/fetchers/redis.js +46 -0
  71. package/dist/cjs/fetchers/redis.js.map +1 -0
  72. package/dist/cjs/fetchers/redshift.d.ts +2 -0
  73. package/dist/cjs/fetchers/redshift.d.ts.map +1 -0
  74. package/dist/cjs/fetchers/redshift.js +24 -0
  75. package/dist/cjs/fetchers/redshift.js.map +1 -0
  76. package/dist/cjs/fetchers/rest-api.d.ts +6 -0
  77. package/dist/cjs/fetchers/rest-api.d.ts.map +1 -0
  78. package/dist/cjs/fetchers/rest-api.js +58 -0
  79. package/dist/cjs/fetchers/rest-api.js.map +1 -0
  80. package/dist/cjs/fetchers/static-collection.d.ts +7 -0
  81. package/dist/cjs/fetchers/static-collection.d.ts.map +1 -0
  82. package/dist/cjs/fetchers/static-collection.js +24 -0
  83. package/dist/cjs/fetchers/static-collection.js.map +1 -0
  84. package/dist/cjs/fetchers/supabase.d.ts +7 -0
  85. package/dist/cjs/fetchers/supabase.d.ts.map +1 -0
  86. package/dist/cjs/fetchers/supabase.js +42 -0
  87. package/dist/cjs/fetchers/supabase.js.map +1 -0
  88. package/dist/cjs/fetchers/turso.d.ts +6 -0
  89. package/dist/cjs/fetchers/turso.d.ts.map +1 -0
  90. package/dist/cjs/fetchers/turso.js +25 -0
  91. package/dist/cjs/fetchers/turso.js.map +1 -0
  92. package/dist/cjs/index.d.ts +9 -0
  93. package/dist/cjs/index.d.ts.map +1 -0
  94. package/dist/cjs/index.js +325 -0
  95. package/dist/cjs/index.js.map +1 -0
  96. package/dist/cjs/pagination-plugin.d.ts +5 -0
  97. package/dist/cjs/pagination-plugin.d.ts.map +1 -0
  98. package/dist/cjs/pagination-plugin.js +1484 -0
  99. package/dist/cjs/pagination-plugin.js.map +1 -0
  100. package/dist/cjs/pagination-with-count.d.ts +6 -0
  101. package/dist/cjs/pagination-with-count.d.ts.map +1 -0
  102. package/dist/cjs/pagination-with-count.js +63 -0
  103. package/dist/cjs/pagination-with-count.js.map +1 -0
  104. package/dist/cjs/tsconfig.tsbuildinfo +1 -0
  105. package/dist/cjs/utils.d.ts +31 -0
  106. package/dist/cjs/utils.d.ts.map +1 -0
  107. package/dist/cjs/utils.js +763 -0
  108. package/dist/cjs/utils.js.map +1 -0
  109. package/dist/cjs/validation.d.ts +5 -0
  110. package/dist/cjs/validation.d.ts.map +1 -0
  111. package/dist/cjs/validation.js +29 -0
  112. package/dist/cjs/validation.js.map +1 -0
  113. package/dist/esm/array-mapper-pagination.d.ts +32 -0
  114. package/dist/esm/array-mapper-pagination.d.ts.map +1 -0
  115. package/dist/esm/array-mapper-pagination.js +72 -0
  116. package/dist/esm/array-mapper-pagination.js.map +1 -0
  117. package/dist/esm/count-fetchers.d.ts +12 -0
  118. package/dist/esm/count-fetchers.d.ts.map +1 -0
  119. package/dist/esm/count-fetchers.js +35 -0
  120. package/dist/esm/count-fetchers.js.map +1 -0
  121. package/dist/esm/data-source-fetchers.d.ts +14 -0
  122. package/dist/esm/data-source-fetchers.d.ts.map +1 -0
  123. package/dist/esm/data-source-fetchers.js +179 -0
  124. package/dist/esm/data-source-fetchers.js.map +1 -0
  125. package/dist/esm/fetchers/airtable.d.ts +6 -0
  126. package/dist/esm/fetchers/airtable.d.ts.map +1 -0
  127. package/dist/esm/fetchers/airtable.js +22 -0
  128. package/dist/esm/fetchers/airtable.js.map +1 -0
  129. package/dist/esm/fetchers/clickhouse.d.ts +6 -0
  130. package/dist/esm/fetchers/clickhouse.d.ts.map +1 -0
  131. package/dist/esm/fetchers/clickhouse.js +24 -0
  132. package/dist/esm/fetchers/clickhouse.js.map +1 -0
  133. package/dist/esm/fetchers/csv-file.d.ts +7 -0
  134. package/dist/esm/fetchers/csv-file.d.ts.map +1 -0
  135. package/dist/esm/fetchers/csv-file.js +30 -0
  136. package/dist/esm/fetchers/csv-file.js.map +1 -0
  137. package/dist/esm/fetchers/firestore.d.ts +6 -0
  138. package/dist/esm/fetchers/firestore.d.ts.map +1 -0
  139. package/dist/esm/fetchers/firestore.js +30 -0
  140. package/dist/esm/fetchers/firestore.js.map +1 -0
  141. package/dist/esm/fetchers/google-sheets.d.ts +6 -0
  142. package/dist/esm/fetchers/google-sheets.d.ts.map +1 -0
  143. package/dist/esm/fetchers/google-sheets.js +25 -0
  144. package/dist/esm/fetchers/google-sheets.js.map +1 -0
  145. package/dist/esm/fetchers/index.d.ts +17 -0
  146. package/dist/esm/fetchers/index.d.ts.map +1 -0
  147. package/dist/esm/fetchers/index.js +17 -0
  148. package/dist/esm/fetchers/index.js.map +1 -0
  149. package/dist/esm/fetchers/javascript.d.ts +7 -0
  150. package/dist/esm/fetchers/javascript.d.ts.map +1 -0
  151. package/dist/esm/fetchers/javascript.js +34 -0
  152. package/dist/esm/fetchers/javascript.js.map +1 -0
  153. package/dist/esm/fetchers/mariadb.d.ts +3 -0
  154. package/dist/esm/fetchers/mariadb.d.ts.map +1 -0
  155. package/dist/esm/fetchers/mariadb.js +18 -0
  156. package/dist/esm/fetchers/mariadb.js.map +1 -0
  157. package/dist/esm/fetchers/mongodb.d.ts +7 -0
  158. package/dist/esm/fetchers/mongodb.d.ts.map +1 -0
  159. package/dist/esm/fetchers/mongodb.js +46 -0
  160. package/dist/esm/fetchers/mongodb.js.map +1 -0
  161. package/dist/esm/fetchers/mysql.d.ts +3 -0
  162. package/dist/esm/fetchers/mysql.d.ts.map +1 -0
  163. package/dist/esm/fetchers/mysql.js +25 -0
  164. package/dist/esm/fetchers/mysql.js.map +1 -0
  165. package/dist/esm/fetchers/postgresql.d.ts +3 -0
  166. package/dist/esm/fetchers/postgresql.d.ts.map +1 -0
  167. package/dist/esm/fetchers/postgresql.js +20 -0
  168. package/dist/esm/fetchers/postgresql.js.map +1 -0
  169. package/dist/esm/fetchers/redis.d.ts +6 -0
  170. package/dist/esm/fetchers/redis.d.ts.map +1 -0
  171. package/dist/esm/fetchers/redis.js +41 -0
  172. package/dist/esm/fetchers/redis.js.map +1 -0
  173. package/dist/esm/fetchers/redshift.d.ts +2 -0
  174. package/dist/esm/fetchers/redshift.d.ts.map +1 -0
  175. package/dist/esm/fetchers/redshift.js +20 -0
  176. package/dist/esm/fetchers/redshift.js.map +1 -0
  177. package/dist/esm/fetchers/rest-api.d.ts +6 -0
  178. package/dist/esm/fetchers/rest-api.d.ts.map +1 -0
  179. package/dist/esm/fetchers/rest-api.js +53 -0
  180. package/dist/esm/fetchers/rest-api.js.map +1 -0
  181. package/dist/esm/fetchers/static-collection.d.ts +7 -0
  182. package/dist/esm/fetchers/static-collection.d.ts.map +1 -0
  183. package/dist/esm/fetchers/static-collection.js +18 -0
  184. package/dist/esm/fetchers/static-collection.js.map +1 -0
  185. package/dist/esm/fetchers/supabase.d.ts +7 -0
  186. package/dist/esm/fetchers/supabase.d.ts.map +1 -0
  187. package/dist/esm/fetchers/supabase.js +36 -0
  188. package/dist/esm/fetchers/supabase.js.map +1 -0
  189. package/dist/esm/fetchers/turso.d.ts +6 -0
  190. package/dist/esm/fetchers/turso.d.ts.map +1 -0
  191. package/dist/esm/fetchers/turso.js +20 -0
  192. package/dist/esm/fetchers/turso.js.map +1 -0
  193. package/dist/esm/index.d.ts +9 -0
  194. package/dist/esm/index.d.ts.map +1 -0
  195. package/dist/esm/index.js +306 -0
  196. package/dist/esm/index.js.map +1 -0
  197. package/dist/esm/pagination-plugin.d.ts +5 -0
  198. package/dist/esm/pagination-plugin.d.ts.map +1 -0
  199. package/dist/esm/pagination-plugin.js +1457 -0
  200. package/dist/esm/pagination-plugin.js.map +1 -0
  201. package/dist/esm/pagination-with-count.d.ts +6 -0
  202. package/dist/esm/pagination-with-count.d.ts.map +1 -0
  203. package/dist/esm/pagination-with-count.js +34 -0
  204. package/dist/esm/pagination-with-count.js.map +1 -0
  205. package/dist/esm/tsconfig.tsbuildinfo +1 -0
  206. package/dist/esm/utils.d.ts +31 -0
  207. package/dist/esm/utils.d.ts.map +1 -0
  208. package/dist/esm/utils.js +722 -0
  209. package/dist/esm/utils.js.map +1 -0
  210. package/dist/esm/validation.d.ts +5 -0
  211. package/dist/esm/validation.d.ts.map +1 -0
  212. package/dist/esm/validation.js +25 -0
  213. package/dist/esm/validation.js.map +1 -0
  214. package/package.json +33 -0
  215. package/src/array-mapper-pagination.ts +113 -0
  216. package/src/count-fetchers.ts +99 -0
  217. package/src/data-source-fetchers.ts +313 -0
  218. package/src/fetchers/airtable.ts +153 -0
  219. package/src/fetchers/clickhouse.ts +127 -0
  220. package/src/fetchers/csv-file.ts +163 -0
  221. package/src/fetchers/firestore.ts +138 -0
  222. package/src/fetchers/google-sheets.ts +189 -0
  223. package/src/fetchers/index.ts +32 -0
  224. package/src/fetchers/javascript.ts +150 -0
  225. package/src/fetchers/mariadb.ts +230 -0
  226. package/src/fetchers/mongodb.ts +239 -0
  227. package/src/fetchers/mysql.ts +237 -0
  228. package/src/fetchers/postgresql.ts +247 -0
  229. package/src/fetchers/redis.ts +152 -0
  230. package/src/fetchers/redshift.ts +138 -0
  231. package/src/fetchers/rest-api.ts +148 -0
  232. package/src/fetchers/static-collection.ts +149 -0
  233. package/src/fetchers/supabase.ts +246 -0
  234. package/src/fetchers/turso.ts +131 -0
  235. package/src/index.ts +352 -0
  236. package/src/pagination-plugin.ts +2335 -0
  237. package/src/pagination-with-count.ts +89 -0
  238. package/src/utils.ts +1013 -0
  239. package/src/validation.ts +32 -0
  240. package/tsconfig.json +9 -0
@@ -0,0 +1,763 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
26
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
27
+ if (ar || !(i in from)) {
28
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
29
+ ar[i] = from[i];
30
+ }
31
+ }
32
+ return to.concat(ar || Array.prototype.slice.call(from));
33
+ };
34
+ Object.defineProperty(exports, "__esModule", { value: true });
35
+ exports.extractDataSourceIntoGetStaticProps = exports.sanitizeIdentifier = exports.isValidUrl = exports.sanitizePort = exports.sanitizeNumericParam = exports.replaceSecretReference = exports.isEmbeddedDataSource = exports.extractDataSourceIntoNextAPIFolder = exports.generateSafeFileName = exports.validateDataSourceConfig = exports.validateNodeContent = exports.validateResourceDefinition = exports.hoistLoadingFromRepeater = exports.isNonEmptyString = exports.sanitizeFileName = void 0;
36
+ var teleport_types_1 = require("@teleporthq/teleport-types");
37
+ var types = __importStar(require("@babel/types"));
38
+ var teleport_plugin_common_1 = require("@teleporthq/teleport-plugin-common");
39
+ var teleport_shared_1 = require("@teleporthq/teleport-shared");
40
+ var data_source_fetchers_1 = require("./data-source-fetchers");
41
+ var VALID_DATA_SOURCE_TYPES = [
42
+ 'rest-api',
43
+ 'postgresql',
44
+ 'mysql',
45
+ 'mariadb',
46
+ 'amazon-redshift',
47
+ 'mongodb',
48
+ 'cockroachdb',
49
+ 'tidb',
50
+ 'redis',
51
+ 'firestore',
52
+ 'clickhouse',
53
+ 'airtable',
54
+ 'supabase',
55
+ 'turso',
56
+ 'javascript',
57
+ 'google-sheets',
58
+ 'csv-file',
59
+ 'static-collection',
60
+ ];
61
+ var sanitizeFileName = function (input) {
62
+ if (!input || typeof input !== 'string') {
63
+ return 'unknown';
64
+ }
65
+ return (input
66
+ // Remove path traversal attempts
67
+ .replace(/\.\./g, '')
68
+ .replace(/[\/\\]/g, '-')
69
+ // Remove invalid filename characters
70
+ .replace(/[<>:"|?*\x00-\x1F]/g, '')
71
+ // Replace spaces with dashes
72
+ .replace(/\s+/g, '-')
73
+ // Remove leading/trailing dashes and dots
74
+ .replace(/^[-_.]+|[-_.]+$/g, '')
75
+ // Limit length to prevent filesystem issues
76
+ .substring(0, 200) || 'unknown');
77
+ };
78
+ exports.sanitizeFileName = sanitizeFileName;
79
+ var isNonEmptyString = function (value) {
80
+ return typeof value === 'string' && value.trim().length > 0;
81
+ };
82
+ exports.isNonEmptyString = isNonEmptyString;
83
+ var hoistLoadingFromRepeater = function (dataSourceNode) {
84
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
85
+ if (!dataSourceNode || !dataSourceNode.content) {
86
+ return;
87
+ }
88
+ var children = dataSourceNode.children || [];
89
+ for (var _i = 0, children_1 = children; _i < children_1.length; _i++) {
90
+ var child = children_1[_i];
91
+ if (child.type === 'cms-list-repeater' && ((_b = (_a = child.content) === null || _a === void 0 ? void 0 : _a.nodes) === null || _b === void 0 ? void 0 : _b.loading)) {
92
+ if (!dataSourceNode.content.nodes) {
93
+ dataSourceNode.content.nodes = {};
94
+ }
95
+ if (!dataSourceNode.content.nodes.loading &&
96
+ ((_d = (_c = child.content.nodes.loading.content) === null || _c === void 0 ? void 0 : _c.children) === null || _d === void 0 ? void 0 : _d.length) > 0) {
97
+ dataSourceNode.content.nodes.loading = child.content.nodes.loading;
98
+ }
99
+ break;
100
+ }
101
+ }
102
+ if ((_g = (_f = (_e = dataSourceNode.content.nodes) === null || _e === void 0 ? void 0 : _e.success) === null || _f === void 0 ? void 0 : _f.content) === null || _g === void 0 ? void 0 : _g.children) {
103
+ for (var _m = 0, _o = dataSourceNode.content.nodes.success.content.children; _m < _o.length; _m++) {
104
+ var child = _o[_m];
105
+ if (child.type === 'cms-list-repeater' && ((_j = (_h = child.content) === null || _h === void 0 ? void 0 : _h.nodes) === null || _j === void 0 ? void 0 : _j.loading)) {
106
+ if (!dataSourceNode.content.nodes.loading &&
107
+ ((_l = (_k = child.content.nodes.loading.content) === null || _k === void 0 ? void 0 : _k.children) === null || _l === void 0 ? void 0 : _l.length) > 0) {
108
+ dataSourceNode.content.nodes.loading = child.content.nodes.loading;
109
+ }
110
+ break;
111
+ }
112
+ }
113
+ }
114
+ };
115
+ exports.hoistLoadingFromRepeater = hoistLoadingFromRepeater;
116
+ // tslint:disable-next-line:no-any
117
+ var validateResourceDefinition = function (resourceDefinition) {
118
+ if (!resourceDefinition || typeof resourceDefinition !== 'object') {
119
+ return { isValid: false, error: 'Resource definition is missing or invalid' };
120
+ }
121
+ var dataSourceId = resourceDefinition.dataSourceId, tableName = resourceDefinition.tableName, dataSourceType = resourceDefinition.dataSourceType;
122
+ if (!(0, exports.isNonEmptyString)(dataSourceId)) {
123
+ return { isValid: false, error: 'Data source ID is missing or invalid' };
124
+ }
125
+ if (!(0, exports.isNonEmptyString)(dataSourceType)) {
126
+ return { isValid: false, error: 'Data source type is missing or invalid' };
127
+ }
128
+ if (!VALID_DATA_SOURCE_TYPES.includes(dataSourceType)) {
129
+ return { isValid: false, error: "Invalid data source type: ".concat(dataSourceType) };
130
+ }
131
+ // tableName is optional for some data sources (like REST API, JavaScript, etc.)
132
+ if (tableName !== undefined && !(0, exports.isNonEmptyString)(tableName)) {
133
+ return { isValid: false, error: 'Table name must be a non-empty string when provided' };
134
+ }
135
+ return { isValid: true };
136
+ };
137
+ exports.validateResourceDefinition = validateResourceDefinition;
138
+ // tslint:disable-next-line:no-any
139
+ var validateNodeContent = function (content) {
140
+ if (!content || typeof content !== 'object') {
141
+ return { isValid: false, error: 'Node content is missing or invalid' };
142
+ }
143
+ if (!content.resourceDefinition) {
144
+ return { isValid: false, error: 'Resource definition is missing from node content' };
145
+ }
146
+ // key is optional - we can generate one from the resource definition if missing
147
+ return { isValid: true };
148
+ };
149
+ exports.validateNodeContent = validateNodeContent;
150
+ var validateDataSourceConfig = function (dataSource) {
151
+ if (!dataSource || typeof dataSource !== 'object') {
152
+ return { isValid: false, error: 'Data source is missing or invalid' };
153
+ }
154
+ if (!(0, exports.isNonEmptyString)(dataSource.id)) {
155
+ return { isValid: false, error: 'Data source ID is missing' };
156
+ }
157
+ if (!(0, exports.isNonEmptyString)(dataSource.type)) {
158
+ return { isValid: false, error: 'Data source type is missing' };
159
+ }
160
+ if (!dataSource.config || typeof dataSource.config !== 'object') {
161
+ return { isValid: false, error: 'Data source config is missing or invalid' };
162
+ }
163
+ return { isValid: true };
164
+ };
165
+ exports.validateDataSourceConfig = validateDataSourceConfig;
166
+ var generateSafeFileName = function (dataSourceType, tableName, dataSourceId) {
167
+ var sanitizedType = (0, exports.sanitizeFileName)(dataSourceType);
168
+ var sanitizedTable = (0, exports.sanitizeFileName)(tableName || 'data');
169
+ var sanitizedId = (0, exports.sanitizeFileName)(dataSourceId);
170
+ // Create a unique identifier using parts of the dataSourceId
171
+ var shortId = sanitizedId.substring(0, 8);
172
+ var baseName = "".concat(sanitizedType, "-").concat(sanitizedTable, "-").concat(shortId);
173
+ return teleport_shared_1.StringUtils.camelCaseToDashCase(baseName);
174
+ };
175
+ exports.generateSafeFileName = generateSafeFileName;
176
+ var extractDataSourceIntoNextAPIFolder = function (node, dataSources, componentChunk, extractedResources) {
177
+ var _a, _b, _c;
178
+ try {
179
+ // Validate node content structure
180
+ var contentValidation = (0, exports.validateNodeContent)(node.content);
181
+ if (!contentValidation.isValid) {
182
+ return;
183
+ }
184
+ var resourceDefinition = node.content.resourceDefinition;
185
+ // Validate resource definition
186
+ var resourceValidation = (0, exports.validateResourceDefinition)(resourceDefinition);
187
+ if (!resourceValidation.isValid) {
188
+ return;
189
+ }
190
+ var dataSourceId = resourceDefinition.dataSourceId, tableName = resourceDefinition.tableName, dataSourceType = resourceDefinition.dataSourceType;
191
+ // Check if dataSources object exists
192
+ if (!dataSources || typeof dataSources !== 'object') {
193
+ return;
194
+ }
195
+ // Check if data source exists
196
+ var dataSource = dataSources[dataSourceId];
197
+ if (!dataSource) {
198
+ return;
199
+ }
200
+ // Validate data source configuration
201
+ var configValidation = (0, exports.validateDataSourceConfig)(dataSource);
202
+ if (!configValidation.isValid) {
203
+ return;
204
+ }
205
+ // Check if component chunk has meta and nodesLookup
206
+ if (!componentChunk.meta || !componentChunk.meta.nodesLookup) {
207
+ return;
208
+ }
209
+ // Generate safe file name
210
+ var fileName = (0, exports.generateSafeFileName)(dataSourceType, tableName || 'data', dataSourceId);
211
+ // Check if file name is valid
212
+ if (!fileName || fileName === 'unknown') {
213
+ return;
214
+ }
215
+ // Find JSX node by searching through nodesLookup
216
+ // The JSX generator creates keys like: ds-{dataSourceId}-{timestamp}
217
+ // We need to find the node that matches our resourceDefinition
218
+ // IMPORTANT: Match by both dataSourceId AND renderPropIdentifier to handle multiple DataProviders with same data source
219
+ var jsxNode = null;
220
+ var targetRenderProp = (_a = node.content) === null || _a === void 0 ? void 0 : _a.renderPropIdentifier;
221
+ // tslint:disable-next-line:no-any
222
+ for (var _i = 0, _d = Object.values(componentChunk.meta.nodesLookup); _i < _d.length; _i++) {
223
+ var jsxElement = _d[_i];
224
+ // tslint:disable-next-line:no-any
225
+ if (jsxElement.type === 'JSXElement') {
226
+ var attrs = jsxElement.openingElement.attributes;
227
+ // Look for resourceDefinition attribute
228
+ // tslint:disable-next-line:no-any
229
+ var resourceDefAttr = attrs.find(function (attr) {
230
+ return attr.type === 'JSXAttribute' &&
231
+ attr.name.name === 'resourceDefinition';
232
+ });
233
+ // Look for name attribute to match with renderPropIdentifier
234
+ // tslint:disable-next-line:no-any
235
+ var nameAttr = attrs.find(function (attr) {
236
+ return attr.type === 'JSXAttribute' &&
237
+ attr.name.name === 'name';
238
+ });
239
+ if (resourceDefAttr &&
240
+ resourceDefAttr.value &&
241
+ resourceDefAttr.value.type === 'JSXExpressionContainer') {
242
+ var expr = resourceDefAttr.value.expression;
243
+ if (expr.type === 'ObjectExpression') {
244
+ // Check if this matches our resourceDefinition
245
+ var props = expr.properties;
246
+ // tslint:disable-next-line:no-any
247
+ var idProp = props.find(function (p) { var _a; return ((_a = p.key) === null || _a === void 0 ? void 0 : _a.value) === 'dataSourceId'; });
248
+ // tslint:disable-next-line:no-any
249
+ var idValue = (_b = idProp === null || idProp === void 0 ? void 0 : idProp.value) === null || _b === void 0 ? void 0 : _b.value;
250
+ // Match by dataSourceId
251
+ var dataSourceMatches = idValue === dataSourceId;
252
+ // Also check name prop matches renderPropIdentifier
253
+ var renderPropMatches = !targetRenderProp; // If no targetRenderProp, don't check it
254
+ if (targetRenderProp && nameAttr && nameAttr.value) {
255
+ // The name attribute value is usually a StringLiteral or JSXExpressionContainer
256
+ if (nameAttr.value.type === 'StringLiteral') {
257
+ if (nameAttr.value.value === targetRenderProp) {
258
+ renderPropMatches = true;
259
+ }
260
+ }
261
+ else if (nameAttr.value.type === 'JSXExpressionContainer') {
262
+ var nameExpr = nameAttr.value.expression;
263
+ if (nameExpr.type === 'StringLiteral' && nameExpr.value === targetRenderProp) {
264
+ renderPropMatches = true;
265
+ }
266
+ }
267
+ }
268
+ // Before matching, check if this node already has fetchData - if so, skip it
269
+ // This is crucial for handling multiple DataProviders with same dataSourceId and name
270
+ var alreadyHasFetchData = attrs.some(function (attr) { var _a; return ((_a = attr.name) === null || _a === void 0 ? void 0 : _a.name) === 'fetchData'; });
271
+ if (dataSourceMatches && renderPropMatches && !alreadyHasFetchData) {
272
+ jsxNode = jsxElement;
273
+ break;
274
+ }
275
+ else if (dataSourceMatches && renderPropMatches && alreadyHasFetchData) {
276
+ // Continue searching for the next matching node without fetchData
277
+ }
278
+ }
279
+ }
280
+ }
281
+ }
282
+ if (!jsxNode || jsxNode.type !== 'JSXElement') {
283
+ return;
284
+ }
285
+ // Ensure opening element and attributes exist
286
+ if (!jsxNode.openingElement || !Array.isArray(jsxNode.openingElement.attributes)) {
287
+ return;
288
+ }
289
+ // Check if this node has already been processed (has fetchData attribute)
290
+ var existingFetchData = jsxNode.openingElement.attributes.find(function (attr) { var _a; return ((_a = attr.name) === null || _a === void 0 ? void 0 : _a.name) === 'fetchData'; });
291
+ if (existingFetchData) {
292
+ return;
293
+ }
294
+ // Check if there are resource params
295
+ var resourceParams = ((_c = node.content.resource) === null || _c === void 0 ? void 0 : _c.params) || {};
296
+ var hasParams = Object.keys(resourceParams).length > 0;
297
+ // Build resource path - use template literal if params exist
298
+ var resourcePath = void 0;
299
+ if (hasParams) {
300
+ resourcePath = types.templateLiteral([
301
+ types.templateElement({ raw: "/api/".concat(fileName, "?"), cooked: "/api/".concat(fileName, "?") }),
302
+ types.templateElement({ raw: '', cooked: '' }),
303
+ ], [types.newExpression(types.identifier('URLSearchParams'), [types.identifier('params')])]);
304
+ }
305
+ else {
306
+ resourcePath = types.stringLiteral("/api/".concat(fileName));
307
+ }
308
+ var fetchAST = types.callExpression(types.memberExpression(types.callExpression(types.identifier('fetch'), [
309
+ resourcePath,
310
+ types.objectExpression([
311
+ types.objectProperty(types.identifier('headers'), types.objectExpression([
312
+ types.objectProperty(types.stringLiteral('Content-Type'), types.stringLiteral('application/json')),
313
+ ])),
314
+ ]),
315
+ ]), types.identifier('then')), [
316
+ types.arrowFunctionExpression([types.identifier('res')], types.callExpression(types.memberExpression(types.identifier('res'), types.identifier('json')), [])),
317
+ ]);
318
+ var dataExpression = teleport_plugin_common_1.ASTUtils.generateMemberExpressionASTFromPath([
319
+ 'response',
320
+ 'data',
321
+ ]);
322
+ // If there are params, the arrow function should accept params
323
+ var resourceAST = types.arrowFunctionExpression(hasParams ? [types.identifier('params')] : [], types.callExpression(types.memberExpression(fetchAST, types.identifier('then'), false), [
324
+ types.arrowFunctionExpression([types.identifier('response')], dataExpression),
325
+ ]));
326
+ jsxNode.openingElement.attributes.unshift(types.jSXAttribute(types.jsxIdentifier('fetchData'), types.jsxExpressionContainer(resourceAST)));
327
+ var hasPersistDataAttr = jsxNode.openingElement.attributes.some(function (attr) {
328
+ var _a;
329
+ return attr.type === 'JSXAttribute' &&
330
+ ((_a = attr.name) === null || _a === void 0 ? void 0 : _a.name) === 'persistDataDuringLoading';
331
+ });
332
+ if (!hasPersistDataAttr) {
333
+ jsxNode.openingElement.attributes.push(types.jsxAttribute(types.jsxIdentifier('persistDataDuringLoading'), types.jsxExpressionContainer(types.booleanLiteral(true))));
334
+ }
335
+ // Ensure extracted resources object exists
336
+ if (!extractedResources || typeof extractedResources !== 'object') {
337
+ return;
338
+ }
339
+ // Check if a utils file already exists for this data source (from getStaticProps)
340
+ // If so, create an API route that re-exports handler from it to avoid code duplication
341
+ if (extractedResources["utils/".concat(fileName)]) {
342
+ var apiRouteCode = "import dataSourceModule from '../../utils/data-sources/".concat(fileName, "'\n\nexport default dataSourceModule.handler\n");
343
+ extractedResources["api/".concat(fileName)] = {
344
+ fileName: fileName,
345
+ fileType: teleport_types_1.FileType.JS,
346
+ path: ['pages', 'api'],
347
+ content: apiRouteCode,
348
+ };
349
+ return;
350
+ }
351
+ // Generate fetcher code with BOTH fetchData and handler
352
+ var fetcherCode = void 0;
353
+ try {
354
+ fetcherCode = (0, data_source_fetchers_1.generateDataSourceFetcherWithCore)(dataSource, tableName || '');
355
+ }
356
+ catch (error) {
357
+ return;
358
+ }
359
+ extractedResources["api/".concat(fileName)] = {
360
+ fileName: fileName,
361
+ fileType: teleport_types_1.FileType.JS,
362
+ path: ['pages', 'api'],
363
+ content: fetcherCode,
364
+ };
365
+ }
366
+ catch (error) {
367
+ // Catch any unexpected errors to prevent plugin from crashing
368
+ }
369
+ };
370
+ exports.extractDataSourceIntoNextAPIFolder = extractDataSourceIntoNextAPIFolder;
371
+ var isEmbeddedDataSource = function (dataSourceType) {
372
+ return ['javascript', 'csv-file', 'static-collection'].includes(dataSourceType);
373
+ };
374
+ exports.isEmbeddedDataSource = isEmbeddedDataSource;
375
+ var replaceSecretReference = function (value, options) {
376
+ if (options === void 0) { options = {}; }
377
+ var _a = options.templateLiteral, templateLiteral = _a === void 0 ? false : _a;
378
+ // Handle null and undefined
379
+ if (value === null) {
380
+ return 'null';
381
+ }
382
+ if (value === undefined) {
383
+ return 'undefined';
384
+ }
385
+ // Handle non-string values
386
+ if (typeof value !== 'string') {
387
+ try {
388
+ return JSON.stringify(value);
389
+ }
390
+ catch (error) {
391
+ // Handle circular references or non-serializable values
392
+ return 'null';
393
+ }
394
+ }
395
+ // Check if it's a secret reference
396
+ if (value.startsWith('teleporthq.secrets.')) {
397
+ var envVar = value.replace('teleporthq.secrets.', '');
398
+ // Validate environment variable name
399
+ // Must be alphanumeric, underscores, and start with letter or underscore
400
+ if (/^[A-Z_][A-Z0-9_]*$/i.test(envVar)) {
401
+ return templateLiteral ? "${process.env.".concat(envVar, "}") : "process.env.".concat(envVar);
402
+ }
403
+ else {
404
+ // Invalid env var name, use as regular string
405
+ return JSON.stringify(value);
406
+ }
407
+ }
408
+ // Regular string value
409
+ try {
410
+ return JSON.stringify(value);
411
+ }
412
+ catch (error) {
413
+ // Fallback for any serialization issues
414
+ return '""';
415
+ }
416
+ };
417
+ exports.replaceSecretReference = replaceSecretReference;
418
+ var sanitizeNumericParam = function (value, defaultValue) {
419
+ if (defaultValue === void 0) { defaultValue = 0; }
420
+ if (typeof value === 'number' && !isNaN(value) && isFinite(value)) {
421
+ return Math.max(0, Math.floor(value));
422
+ }
423
+ if (typeof value === 'string') {
424
+ var parsed = parseInt(value, 10);
425
+ if (!isNaN(parsed) && isFinite(parsed)) {
426
+ return Math.max(0, parsed);
427
+ }
428
+ }
429
+ return defaultValue;
430
+ };
431
+ exports.sanitizeNumericParam = sanitizeNumericParam;
432
+ var sanitizePort = function (port, defaultPort) {
433
+ var sanitized = (0, exports.sanitizeNumericParam)(port, defaultPort);
434
+ // Ensure port is in valid range (1-65535)
435
+ if (sanitized < 1 || sanitized > 65535) {
436
+ return defaultPort;
437
+ }
438
+ return sanitized;
439
+ };
440
+ exports.sanitizePort = sanitizePort;
441
+ var isValidUrl = function (url) {
442
+ if (typeof url !== 'string' || !url) {
443
+ return false;
444
+ }
445
+ try {
446
+ var parsed = new URL(url);
447
+ // Check for valid protocols
448
+ return ['http:', 'https:', 'mongodb:', 'redis:', 'postgresql:', 'mysql:'].includes(parsed.protocol);
449
+ }
450
+ catch (_a) {
451
+ return false;
452
+ }
453
+ };
454
+ exports.isValidUrl = isValidUrl;
455
+ var sanitizeIdentifier = function (identifier) {
456
+ if (typeof identifier !== 'string' || !identifier) {
457
+ return '';
458
+ }
459
+ // Remove dangerous characters, only allow safe ones
460
+ return identifier.replace(/[^a-zA-Z0-9_-]/g, '').substring(0, 64);
461
+ };
462
+ exports.sanitizeIdentifier = sanitizeIdentifier;
463
+ // tslint:disable-next-line:no-any
464
+ var extractDataSourceIntoGetStaticProps = function (node, dataSources, componentChunk, getStaticPropsChunk, chunks, extractedResources, dependencies) {
465
+ var _a, _b;
466
+ try {
467
+ // Validate node content
468
+ var contentValidation = (0, exports.validateNodeContent)(node.content);
469
+ if (!contentValidation.isValid) {
470
+ return { success: false };
471
+ }
472
+ var resourceDefinition = node.content.resourceDefinition;
473
+ var resourceValidation = (0, exports.validateResourceDefinition)(resourceDefinition);
474
+ if (!resourceValidation.isValid) {
475
+ return { success: false };
476
+ }
477
+ var dataSourceId_1 = resourceDefinition.dataSourceId, tableName_1 = resourceDefinition.tableName, dataSourceType = resourceDefinition.dataSourceType;
478
+ if (!dataSources || typeof dataSources !== 'object') {
479
+ return { success: false };
480
+ }
481
+ var dataSource = dataSources[dataSourceId_1];
482
+ if (!dataSource) {
483
+ return { success: false };
484
+ }
485
+ var configValidation = (0, exports.validateDataSourceConfig)(dataSource);
486
+ if (!configValidation.isValid) {
487
+ return { success: false };
488
+ }
489
+ if (!componentChunk.meta || !componentChunk.meta.nodesLookup) {
490
+ return { success: false };
491
+ }
492
+ // Generate prop key first
493
+ var sanitizedDsName = teleport_shared_1.StringUtils.dashCaseToCamelCase((0, exports.sanitizeFileName)(dataSource.name || dataSourceId_1));
494
+ var sanitizedTableName = teleport_shared_1.StringUtils.dashCaseToCamelCase((0, exports.sanitizeFileName)(tableName_1 || 'data'));
495
+ var propKey_1 = "".concat(sanitizedDsName, "_").concat(sanitizedTableName, "_data");
496
+ // Find ALL JSX nodes matching this dataSourceId AND tableName and add initialData to ALL of them
497
+ var matchingJsxNodes_2 = [];
498
+ // Helper function to recursively traverse the AST and find all matching JSXElements
499
+ var traverseAST_1 = function (astNode) {
500
+ var _a, _b;
501
+ if (!astNode || typeof astNode !== 'object') {
502
+ return;
503
+ }
504
+ if (astNode.type === 'JSXElement') {
505
+ var jsxElement = astNode;
506
+ var attrs = jsxElement.openingElement.attributes;
507
+ var resourceDefAttr = attrs.find(function (attr) {
508
+ return attr.type === 'JSXAttribute' &&
509
+ attr.name.name === 'resourceDefinition';
510
+ });
511
+ if (resourceDefAttr &&
512
+ resourceDefAttr.value &&
513
+ resourceDefAttr.value.type === 'JSXExpressionContainer') {
514
+ var expr = resourceDefAttr.value.expression;
515
+ if (expr.type === 'ObjectExpression') {
516
+ var props = expr.properties;
517
+ // tslint:disable-next-line:no-any
518
+ var idProp = props.find(function (p) { var _a; return ((_a = p.key) === null || _a === void 0 ? void 0 : _a.value) === 'dataSourceId'; });
519
+ // tslint:disable-next-line:no-any
520
+ var idValue = (_a = idProp === null || idProp === void 0 ? void 0 : idProp.value) === null || _a === void 0 ? void 0 : _a.value;
521
+ // Also check tableName to ensure we're matching the right data source
522
+ // tslint:disable-next-line:no-any
523
+ var tableNameProp = props.find(function (p) { var _a; return ((_a = p.key) === null || _a === void 0 ? void 0 : _a.value) === 'tableName'; });
524
+ // tslint:disable-next-line:no-any
525
+ var tableNameValue = (_b = tableNameProp === null || tableNameProp === void 0 ? void 0 : tableNameProp.value) === null || _b === void 0 ? void 0 : _b.value;
526
+ if (idValue === dataSourceId_1 && tableNameValue === tableName_1) {
527
+ matchingJsxNodes_2.push(jsxElement);
528
+ }
529
+ }
530
+ }
531
+ }
532
+ // Recursively traverse all properties
533
+ for (var key in astNode) {
534
+ if (astNode.hasOwnProperty(key)) {
535
+ var value = astNode[key];
536
+ if (Array.isArray(value)) {
537
+ value.forEach(function (item) { return traverseAST_1(item); });
538
+ }
539
+ else if (typeof value === 'object') {
540
+ traverseAST_1(value);
541
+ }
542
+ }
543
+ }
544
+ };
545
+ // Traverse the entire component AST content
546
+ traverseAST_1(componentChunk.content);
547
+ if (matchingJsxNodes_2.length === 0) {
548
+ return { success: false };
549
+ }
550
+ // Update ALL matching JSX nodes with initialData
551
+ for (var _i = 0, matchingJsxNodes_1 = matchingJsxNodes_2; _i < matchingJsxNodes_1.length; _i++) {
552
+ var jsxNode = matchingJsxNodes_1[_i];
553
+ // For SSR/SSG with initialData, rename 'children' to 'renderSuccess'
554
+ var childrenAttrIndex = jsxNode.openingElement.attributes.findIndex(function (attr) { var _a; return ((_a = attr.name) === null || _a === void 0 ? void 0 : _a.name) === 'children'; });
555
+ if (childrenAttrIndex !== -1) {
556
+ var childrenAttr = jsxNode.openingElement.attributes[childrenAttrIndex];
557
+ var renderSuccessAttr = types.jsxAttribute(types.jsxIdentifier('renderSuccess'), childrenAttr.value);
558
+ jsxNode.openingElement.attributes[childrenAttrIndex] = renderSuccessAttr;
559
+ }
560
+ // Remove existing initialData and persistDataDuringLoading attributes to avoid duplicates
561
+ jsxNode.openingElement.attributes = jsxNode.openingElement.attributes.filter(function (attr) {
562
+ var _a, _b;
563
+ return ((_a = attr.name) === null || _a === void 0 ? void 0 : _a.name) !== 'initialData' &&
564
+ ((_b = attr.name) === null || _b === void 0 ? void 0 : _b.name) !== 'persistDataDuringLoading';
565
+ });
566
+ // Add initialData attribute
567
+ var initialDataAttr = types.jsxAttribute(types.jsxIdentifier('initialData'), types.jsxExpressionContainer(types.memberExpression(types.identifier('props'), types.identifier(propKey_1))));
568
+ jsxNode.openingElement.attributes.push(initialDataAttr);
569
+ // Add persistDataDuringLoading={true}
570
+ var persistDataAttr = types.jsxAttribute(types.jsxIdentifier('persistDataDuringLoading'), types.jsxExpressionContainer(types.booleanLiteral(true)));
571
+ jsxNode.openingElement.attributes.push(persistDataAttr);
572
+ }
573
+ // Generate safe file name for the fetcher
574
+ var fileName = (0, exports.generateSafeFileName)(dataSourceType, tableName_1 || 'data', dataSourceId_1);
575
+ if (!fileName || fileName === 'unknown') {
576
+ return { success: false };
577
+ }
578
+ // Generate fetcher code with core function
579
+ var fetcherCode = void 0;
580
+ try {
581
+ fetcherCode = (0, data_source_fetchers_1.generateDataSourceFetcherWithCore)(dataSource, tableName_1 || '');
582
+ }
583
+ catch (error) {
584
+ // tslint:disable-next-line:no-console
585
+ console.error("Failed to generate fetcher for ".concat(dataSourceType, " (").concat(dataSourceId_1, "):"), error);
586
+ return { success: false };
587
+ }
588
+ // Ensure extracted resources object exists
589
+ if (!extractedResources || typeof extractedResources !== 'object') {
590
+ return { success: false };
591
+ }
592
+ // Add the fetcher to utils folder (for server-side use)
593
+ // Use a unique key to avoid conflicts with API routes
594
+ extractedResources["utils/".concat(fileName)] = {
595
+ fileName: fileName,
596
+ fileType: teleport_types_1.FileType.JS,
597
+ path: ['utils', 'data-sources'],
598
+ content: fetcherCode,
599
+ };
600
+ // Add dependency for the fetcher
601
+ var fetcherImportName = teleport_shared_1.StringUtils.dashCaseToCamelCase(fileName);
602
+ dependencies[fetcherImportName] = {
603
+ type: 'local',
604
+ path: "../utils/data-sources/".concat(fileName),
605
+ };
606
+ // Build params object from resource params
607
+ // tslint:disable-next-line:no-any
608
+ var resourceParams = ((_a = node.content.resource) === null || _a === void 0 ? void 0 : _a.params) || {};
609
+ var paramsProperties_1 = [];
610
+ // tslint:disable-next-line:no-any
611
+ Object.entries(resourceParams).forEach(function (_a) {
612
+ var key = _a[0], value = _a[1];
613
+ if (value.type === 'static') {
614
+ var astValue = void 0;
615
+ if (value.content === null || value.content === undefined) {
616
+ astValue = types.nullLiteral();
617
+ }
618
+ else if (Array.isArray(value.content)) {
619
+ // Handle array values (like queryColumns)
620
+ astValue = types.arrayExpression(value.content.map(function (item) {
621
+ if (typeof item === 'string') {
622
+ return types.stringLiteral(item);
623
+ }
624
+ if (typeof item === 'number') {
625
+ return types.numericLiteral(item);
626
+ }
627
+ if (typeof item === 'boolean') {
628
+ return types.booleanLiteral(item);
629
+ }
630
+ return types.nullLiteral();
631
+ }));
632
+ }
633
+ else if (typeof value.content === 'string') {
634
+ astValue = types.stringLiteral(value.content);
635
+ }
636
+ else if (typeof value.content === 'number') {
637
+ astValue = types.numericLiteral(value.content);
638
+ }
639
+ else if (typeof value.content === 'boolean') {
640
+ astValue = types.booleanLiteral(value.content);
641
+ }
642
+ else {
643
+ astValue = types.nullLiteral();
644
+ }
645
+ paramsProperties_1.push(types.objectProperty(types.stringLiteral(key), astValue));
646
+ }
647
+ // Note: We don't handle 'expr' or 'dynamic' params in getStaticProps
648
+ // as those should be detected by hasResourceDynamicParams check earlier
649
+ });
650
+ var fetchCallExpression = types.callExpression(types.memberExpression(types.identifier(fetcherImportName), types.identifier('fetchData')), [types.objectExpression(paramsProperties_1)]);
651
+ var safeFetchExpression = createSafeFetchExpression(fetchCallExpression, propKey_1);
652
+ // Get response value path (if specified)
653
+ var responseMemberAST = node.content.valuePath
654
+ ? teleport_plugin_common_1.ASTUtils.generateMemberExpressionASTFromPath(__spreadArray([
655
+ propKey_1
656
+ ], teleport_plugin_common_1.ASTUtils.parseValuePath(node.content.valuePath), true))
657
+ : types.identifier(propKey_1);
658
+ // Create or update getStaticProps chunk
659
+ var tryBlock = null;
660
+ if (!getStaticPropsChunk) {
661
+ tryBlock = types.tryStatement(types.blockStatement([
662
+ types.returnStatement(types.objectExpression([
663
+ types.objectProperty(types.identifier('props'), types.objectExpression([
664
+ types.objectProperty(types.identifier(propKey_1), responseMemberAST, false, false),
665
+ ])),
666
+ types.objectProperty(types.identifier('revalidate'), types.numericLiteral(1)),
667
+ ])),
668
+ ]), types.catchClause(types.identifier('error'), types.blockStatement([
669
+ types.expressionStatement(types.callExpression(types.memberExpression(types.identifier('console'), types.identifier('error')), [types.stringLiteral('Error in getStaticProps:'), types.identifier('error')])),
670
+ types.returnStatement(types.objectExpression([
671
+ types.objectProperty(types.identifier('props'), types.objectExpression([])),
672
+ ])),
673
+ ])));
674
+ getStaticPropsChunk = {
675
+ name: 'getStaticProps',
676
+ type: teleport_types_1.ChunkType.AST,
677
+ fileType: teleport_types_1.FileType.JS,
678
+ content: types.exportNamedDeclaration(types.functionDeclaration(types.identifier('getStaticProps'), [types.identifier('context')], types.blockStatement([tryBlock]), false, true)),
679
+ linkAfter: ['jsx-component'],
680
+ };
681
+ chunks.push(getStaticPropsChunk);
682
+ }
683
+ else {
684
+ // Update existing getStaticProps
685
+ var functionDeclaration = getStaticPropsChunk.content
686
+ .declaration;
687
+ var functionBody = functionDeclaration.body.body;
688
+ tryBlock = functionBody.find(function (subNode) { return subNode.type === 'TryStatement'; });
689
+ if (!tryBlock) {
690
+ return { success: false };
691
+ }
692
+ }
693
+ if (!tryBlock) {
694
+ return { success: false };
695
+ }
696
+ // Ensure props include current data source
697
+ var returnStatement = tryBlock.block.body.find(function (subNode) { return subNode.type === 'ReturnStatement'; });
698
+ if (!returnStatement) {
699
+ return { success: false };
700
+ }
701
+ var propsObject = returnStatement.argument.properties.find(function (property) { return property.key.name === 'props'; });
702
+ var propsValue = propsObject.value;
703
+ // Check if propKey already exists in the parallel fetch metadata
704
+ var parallelFetchMeta = (_b = getStaticPropsChunk.meta) === null || _b === void 0 ? void 0 : _b.parallelFetch;
705
+ var existingInFetchMeta = parallelFetchMeta === null || parallelFetchMeta === void 0 ? void 0 : parallelFetchMeta.names.includes(propKey_1);
706
+ // Always ensure both the fetch and the prop are in sync
707
+ if (!existingInFetchMeta) {
708
+ // Add to parallel fetch metadata
709
+ registerParallelFetch(getStaticPropsChunk, tryBlock, propKey_1, safeFetchExpression);
710
+ // Check if prop exists in return object
711
+ var existingProp = propsValue.properties.find(function (prop) {
712
+ return prop.type === 'ObjectProperty' &&
713
+ prop.key.type === 'Identifier' &&
714
+ prop.key.name === propKey_1;
715
+ });
716
+ // Add prop if it doesn't exist
717
+ if (!existingProp) {
718
+ propsValue.properties.unshift(types.objectProperty(types.identifier(propKey_1), responseMemberAST, false, false));
719
+ }
720
+ }
721
+ return { success: true, chunk: getStaticPropsChunk };
722
+ }
723
+ catch (error) {
724
+ return { success: false };
725
+ }
726
+ };
727
+ exports.extractDataSourceIntoGetStaticProps = extractDataSourceIntoGetStaticProps;
728
+ var createSafeFetchExpression = function (fetchCallExpression, label) {
729
+ var errorIdentifier = types.identifier('error');
730
+ var catchHandler = types.arrowFunctionExpression([errorIdentifier], types.blockStatement([
731
+ types.expressionStatement(types.callExpression(types.memberExpression(types.identifier('console'), types.identifier('error')), [types.stringLiteral("Error fetching ".concat(label, ":")), errorIdentifier])),
732
+ types.returnStatement(types.arrayExpression([])),
733
+ ]));
734
+ return types.callExpression(types.memberExpression(fetchCallExpression, types.identifier('catch')), [catchHandler]);
735
+ };
736
+ var registerParallelFetch = function (getStaticPropsChunk, tryBlock, propKey, expression) {
737
+ var _a;
738
+ if (!getStaticPropsChunk.meta) {
739
+ getStaticPropsChunk.meta = {};
740
+ }
741
+ var meta = (_a = getStaticPropsChunk.meta.parallelFetchData) !== null && _a !== void 0 ? _a : (getStaticPropsChunk.meta.parallelFetchData = {
742
+ names: [],
743
+ expressions: [],
744
+ });
745
+ meta.names.push(propKey);
746
+ meta.expressions.push(expression);
747
+ updateParallelFetchStatement(tryBlock, meta);
748
+ };
749
+ var updateParallelFetchStatement = function (tryBlock, meta) {
750
+ if (meta.declaration) {
751
+ var existingIndex = tryBlock.block.body.indexOf(meta.declaration);
752
+ if (existingIndex !== -1) {
753
+ tryBlock.block.body.splice(existingIndex, 1);
754
+ }
755
+ }
756
+ var promiseAllCall = types.awaitExpression(types.callExpression(types.memberExpression(types.identifier('Promise'), types.identifier('all')), [types.arrayExpression(meta.expressions.map(function (expression) { return expression; }))]));
757
+ var arrayPattern = types.arrayPattern(meta.names.map(function (name) { return types.identifier(name); }));
758
+ meta.declaration = types.variableDeclaration('const', [
759
+ types.variableDeclarator(arrayPattern, promiseAllCall),
760
+ ]);
761
+ tryBlock.block.body.unshift(meta.declaration);
762
+ };
763
+ //# sourceMappingURL=utils.js.map