@tinybirdco/sdk 0.0.4 → 0.0.7

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 (165) hide show
  1. package/README.md +52 -13
  2. package/dist/api/branches.d.ts.map +1 -1
  3. package/dist/api/branches.js +6 -5
  4. package/dist/api/branches.js.map +1 -1
  5. package/dist/api/branches.test.js +32 -6
  6. package/dist/api/branches.test.js.map +1 -1
  7. package/dist/api/build.d.ts.map +1 -1
  8. package/dist/api/build.js +2 -1
  9. package/dist/api/build.js.map +1 -1
  10. package/dist/api/deploy.d.ts +42 -3
  11. package/dist/api/deploy.d.ts.map +1 -1
  12. package/dist/api/deploy.js +162 -19
  13. package/dist/api/deploy.js.map +1 -1
  14. package/dist/api/deploy.test.js +83 -31
  15. package/dist/api/deploy.test.js.map +1 -1
  16. package/dist/api/fetcher.d.ts +6 -0
  17. package/dist/api/fetcher.d.ts.map +1 -0
  18. package/dist/api/fetcher.js +13 -0
  19. package/dist/api/fetcher.js.map +1 -0
  20. package/dist/api/local.d.ts.map +1 -1
  21. package/dist/api/local.js +5 -4
  22. package/dist/api/local.js.map +1 -1
  23. package/dist/api/local.test.js.map +1 -1
  24. package/dist/api/resources.d.ts +178 -0
  25. package/dist/api/resources.d.ts.map +1 -0
  26. package/dist/api/resources.js +245 -0
  27. package/dist/api/resources.js.map +1 -0
  28. package/dist/api/resources.test.d.ts +2 -0
  29. package/dist/api/resources.test.d.ts.map +1 -0
  30. package/dist/api/resources.test.js +255 -0
  31. package/dist/api/resources.test.js.map +1 -0
  32. package/dist/api/workspaces.d.ts.map +1 -1
  33. package/dist/api/workspaces.js +2 -1
  34. package/dist/api/workspaces.js.map +1 -1
  35. package/dist/api/workspaces.test.js +9 -1
  36. package/dist/api/workspaces.test.js.map +1 -1
  37. package/dist/cli/auth.d.ts.map +1 -1
  38. package/dist/cli/auth.js +2 -1
  39. package/dist/cli/auth.js.map +1 -1
  40. package/dist/cli/commands/build.d.ts +3 -4
  41. package/dist/cli/commands/build.d.ts.map +1 -1
  42. package/dist/cli/commands/build.js +23 -25
  43. package/dist/cli/commands/build.js.map +1 -1
  44. package/dist/cli/commands/deploy.d.ts +41 -0
  45. package/dist/cli/commands/deploy.d.ts.map +1 -0
  46. package/dist/cli/commands/deploy.js +92 -0
  47. package/dist/cli/commands/deploy.js.map +1 -0
  48. package/dist/cli/commands/dev.d.ts.map +1 -1
  49. package/dist/cli/commands/dev.js +7 -3
  50. package/dist/cli/commands/dev.js.map +1 -1
  51. package/dist/cli/commands/init.d.ts +38 -1
  52. package/dist/cli/commands/init.d.ts.map +1 -1
  53. package/dist/cli/commands/init.js +434 -23
  54. package/dist/cli/commands/init.js.map +1 -1
  55. package/dist/cli/commands/init.test.js +190 -30
  56. package/dist/cli/commands/init.test.js.map +1 -1
  57. package/dist/cli/index.js +80 -15
  58. package/dist/cli/index.js.map +1 -1
  59. package/dist/cli/utils/package-manager.d.ts +8 -0
  60. package/dist/cli/utils/package-manager.d.ts.map +1 -0
  61. package/dist/cli/utils/package-manager.js +45 -0
  62. package/dist/cli/utils/package-manager.js.map +1 -0
  63. package/dist/cli/utils/package-manager.test.d.ts +2 -0
  64. package/dist/cli/utils/package-manager.test.d.ts.map +1 -0
  65. package/dist/cli/utils/package-manager.test.js +85 -0
  66. package/dist/cli/utils/package-manager.test.js.map +1 -0
  67. package/dist/client/base.d.ts.map +1 -1
  68. package/dist/client/base.js +2 -1
  69. package/dist/client/base.js.map +1 -1
  70. package/dist/codegen/index.d.ts +39 -0
  71. package/dist/codegen/index.d.ts.map +1 -0
  72. package/dist/codegen/index.js +300 -0
  73. package/dist/codegen/index.js.map +1 -0
  74. package/dist/codegen/index.test.d.ts +2 -0
  75. package/dist/codegen/index.test.d.ts.map +1 -0
  76. package/dist/codegen/index.test.js +310 -0
  77. package/dist/codegen/index.test.js.map +1 -0
  78. package/dist/codegen/type-mapper.d.ts +20 -0
  79. package/dist/codegen/type-mapper.d.ts.map +1 -0
  80. package/dist/codegen/type-mapper.js +238 -0
  81. package/dist/codegen/type-mapper.js.map +1 -0
  82. package/dist/codegen/type-mapper.test.d.ts +2 -0
  83. package/dist/codegen/type-mapper.test.d.ts.map +1 -0
  84. package/dist/codegen/type-mapper.test.js +167 -0
  85. package/dist/codegen/type-mapper.test.js.map +1 -0
  86. package/dist/codegen/utils.d.ts +46 -0
  87. package/dist/codegen/utils.d.ts.map +1 -0
  88. package/dist/codegen/utils.js +141 -0
  89. package/dist/codegen/utils.js.map +1 -0
  90. package/dist/codegen/utils.test.d.ts +2 -0
  91. package/dist/codegen/utils.test.d.ts.map +1 -0
  92. package/dist/codegen/utils.test.js +178 -0
  93. package/dist/codegen/utils.test.js.map +1 -0
  94. package/dist/generator/index.d.ts +3 -0
  95. package/dist/generator/index.d.ts.map +1 -1
  96. package/dist/generator/index.js +17 -1
  97. package/dist/generator/index.js.map +1 -1
  98. package/dist/generator/index.test.js +104 -1
  99. package/dist/generator/index.test.js.map +1 -1
  100. package/dist/generator/loader.d.ts +15 -0
  101. package/dist/generator/loader.d.ts.map +1 -1
  102. package/dist/generator/loader.js +24 -0
  103. package/dist/generator/loader.js.map +1 -1
  104. package/dist/schema/connection.d.ts.map +1 -1
  105. package/dist/schema/connection.js +3 -2
  106. package/dist/schema/connection.js.map +1 -1
  107. package/dist/schema/datasource.d.ts.map +1 -1
  108. package/dist/schema/datasource.js +3 -2
  109. package/dist/schema/datasource.js.map +1 -1
  110. package/dist/schema/params.d.ts.map +1 -1
  111. package/dist/schema/params.js +3 -2
  112. package/dist/schema/params.js.map +1 -1
  113. package/dist/schema/pipe.d.ts +2 -2
  114. package/dist/schema/pipe.d.ts.map +1 -1
  115. package/dist/schema/pipe.js +4 -4
  116. package/dist/schema/pipe.js.map +1 -1
  117. package/dist/schema/project.d.ts.map +1 -1
  118. package/dist/schema/project.js +3 -2
  119. package/dist/schema/project.js.map +1 -1
  120. package/dist/schema/types.d.ts.map +1 -1
  121. package/dist/schema/types.js +3 -2
  122. package/dist/schema/types.js.map +1 -1
  123. package/dist/test/handlers.d.ts +49 -0
  124. package/dist/test/handlers.d.ts.map +1 -1
  125. package/dist/test/handlers.js +45 -0
  126. package/dist/test/handlers.js.map +1 -1
  127. package/package.json +4 -2
  128. package/src/api/branches.test.ts +65 -57
  129. package/src/api/branches.ts +7 -5
  130. package/src/api/build.ts +2 -1
  131. package/src/api/deploy.test.ts +141 -36
  132. package/src/api/deploy.ts +231 -23
  133. package/src/api/fetcher.ts +17 -0
  134. package/src/api/local.test.ts +43 -31
  135. package/src/api/local.ts +5 -4
  136. package/src/api/resources.test.ts +332 -0
  137. package/src/api/resources.ts +555 -0
  138. package/src/api/workspaces.test.ts +15 -9
  139. package/src/api/workspaces.ts +3 -1
  140. package/src/cli/auth.ts +2 -1
  141. package/src/cli/commands/build.ts +29 -33
  142. package/src/cli/commands/deploy.ts +131 -0
  143. package/src/cli/commands/dev.ts +10 -3
  144. package/src/cli/commands/init.test.ts +239 -30
  145. package/src/cli/commands/init.ts +548 -26
  146. package/src/cli/index.ts +117 -20
  147. package/src/cli/utils/package-manager.test.ts +118 -0
  148. package/src/cli/utils/package-manager.ts +44 -0
  149. package/src/client/base.ts +3 -2
  150. package/src/codegen/index.test.ts +367 -0
  151. package/src/codegen/index.ts +379 -0
  152. package/src/codegen/type-mapper.test.ts +224 -0
  153. package/src/codegen/type-mapper.ts +265 -0
  154. package/src/codegen/utils.test.ts +221 -0
  155. package/src/codegen/utils.ts +174 -0
  156. package/src/generator/index.test.ts +121 -1
  157. package/src/generator/index.ts +19 -1
  158. package/src/generator/loader.ts +43 -0
  159. package/src/schema/connection.ts +3 -2
  160. package/src/schema/datasource.ts +3 -2
  161. package/src/schema/params.ts +3 -2
  162. package/src/schema/pipe.ts +4 -4
  163. package/src/schema/project.ts +3 -2
  164. package/src/schema/types.ts +3 -2
  165. package/src/test/handlers.ts +58 -0
@@ -1,11 +1,10 @@
1
1
  /**
2
- * Build command - generates and pushes resources to Tinybird
2
+ * Build command - generates and pushes resources to Tinybird branches
3
3
  */
4
4
 
5
5
  import { loadConfig, LOCAL_BASE_URL, type ResolvedConfig, type DevMode } from "../config.js";
6
6
  import { buildFromInclude, type BuildFromIncludeResult } from "../../generator/index.js";
7
7
  import { buildToTinybird, type BuildApiResult } from "../../api/build.js";
8
- import { deployToMain } from "../../api/deploy.js";
9
8
  import { getOrCreateBranch } from "../../api/branches.js";
10
9
  import {
11
10
  getLocalTokens,
@@ -24,8 +23,6 @@ export interface BuildCommandOptions {
24
23
  dryRun?: boolean;
25
24
  /** Override the token from config (used for branch tokens) */
26
25
  tokenOverride?: string;
27
- /** Use /v1/deploy instead of /v1/build (for main branch) */
28
- useDeployEndpoint?: boolean;
29
26
  /** Override the devMode from config */
30
27
  devModeOverride?: DevMode;
31
28
  }
@@ -49,7 +46,8 @@ export interface BuildCommandResult {
49
46
  /**
50
47
  * Run the build command
51
48
  *
52
- * Loads the schema, generates resources, and pushes to Tinybird API.
49
+ * Builds resources and pushes to Tinybird branches (not main).
50
+ * Use runDeploy for deploying to production.
53
51
  *
54
52
  * @param options - Build options
55
53
  * @returns Build command result
@@ -150,12 +148,19 @@ export async function runBuild(options: BuildCommandOptions = {}): Promise<Build
150
148
  };
151
149
  }
152
150
  } else {
153
- // Branch mode (default) - existing logic
154
- // Deploy to Tinybird
155
- // Determine token and endpoint based on git branch
156
- let effectiveToken = options.tokenOverride ?? config.token;
157
- // Use deploy endpoint if on main branch OR if no branch can be detected
158
- let useDeployEndpoint = options.useDeployEndpoint ?? (config.isMainBranch || !config.tinybirdBranch);
151
+ // Branch mode (default)
152
+ // Prevent building to main - must use deploy command
153
+ // Skip this check if tokenOverride is provided (dev command passes branch token)
154
+ const isMainBranch = config.isMainBranch || !config.tinybirdBranch;
155
+
156
+ if (isMainBranch && !options.tokenOverride) {
157
+ return {
158
+ success: false,
159
+ build: buildResult,
160
+ error: `Cannot deploy to main workspace with 'build' command. Use 'tinybird deploy' to deploy to production, or switch to a feature branch.`,
161
+ durationMs: Date.now() - startTime,
162
+ };
163
+ }
159
164
 
160
165
  if (debug) {
161
166
  console.log(`[debug] isMainBranch: ${config.isMainBranch}`);
@@ -163,8 +168,10 @@ export async function runBuild(options: BuildCommandOptions = {}): Promise<Build
163
168
  console.log(`[debug] tokenOverride: ${!!options.tokenOverride}`);
164
169
  }
165
170
 
166
- // For feature branches, get or create the Tinybird branch and use its token
167
- if (!config.isMainBranch && config.tinybirdBranch && !options.tokenOverride) {
171
+ let effectiveToken = options.tokenOverride ?? config.token;
172
+
173
+ // Get or create the Tinybird branch and use its token
174
+ if (!options.tokenOverride) {
168
175
  if (debug) {
169
176
  console.log(`[debug] Getting/creating Tinybird branch: ${config.tinybirdBranch}`);
170
177
  }
@@ -174,7 +181,7 @@ export async function runBuild(options: BuildCommandOptions = {}): Promise<Build
174
181
  baseUrl: config.baseUrl,
175
182
  token: config.token,
176
183
  },
177
- config.tinybirdBranch
184
+ config.tinybirdBranch!
178
185
  );
179
186
 
180
187
  if (!tinybirdBranch.token) {
@@ -187,7 +194,6 @@ export async function runBuild(options: BuildCommandOptions = {}): Promise<Build
187
194
  }
188
195
 
189
196
  effectiveToken = tinybirdBranch.token;
190
- useDeployEndpoint = false; // Always use /v1/build for branches
191
197
  if (debug) {
192
198
  console.log(`[debug] Using branch token for branch: ${config.tinybirdBranch}`);
193
199
  }
@@ -202,24 +208,14 @@ export async function runBuild(options: BuildCommandOptions = {}): Promise<Build
202
208
  }
203
209
 
204
210
  try {
205
- // Use /v1/deploy for main branch, /v1/build for feature branches
206
- if (useDeployEndpoint) {
207
- deployResult = await deployToMain(
208
- {
209
- baseUrl: config.baseUrl,
210
- token: effectiveToken,
211
- },
212
- buildResult.resources
213
- );
214
- } else {
215
- deployResult = await buildToTinybird(
216
- {
217
- baseUrl: config.baseUrl,
218
- token: effectiveToken,
219
- },
220
- buildResult.resources
221
- );
222
- }
211
+ // Always use /v1/build for branches
212
+ deployResult = await buildToTinybird(
213
+ {
214
+ baseUrl: config.baseUrl,
215
+ token: effectiveToken,
216
+ },
217
+ buildResult.resources
218
+ );
223
219
  } catch (error) {
224
220
  return {
225
221
  success: false,
@@ -0,0 +1,131 @@
1
+ /**
2
+ * Deploy command - deploys resources to main Tinybird workspace
3
+ */
4
+
5
+ import { loadConfig, type ResolvedConfig } from "../config.js";
6
+ import { buildFromInclude, type BuildFromIncludeResult } from "../../generator/index.js";
7
+ import { deployToMain } from "../../api/deploy.js";
8
+ import type { BuildApiResult } from "../../api/build.js";
9
+
10
+ /**
11
+ * Deploy command options
12
+ */
13
+ export interface DeployCommandOptions {
14
+ /** Working directory (defaults to cwd) */
15
+ cwd?: string;
16
+ /** Skip pushing to API (just generate) */
17
+ dryRun?: boolean;
18
+ /** Validate deploy with Tinybird API without applying */
19
+ check?: boolean;
20
+ }
21
+
22
+ /**
23
+ * Deploy command result
24
+ */
25
+ export interface DeployCommandResult {
26
+ /** Whether the deploy was successful */
27
+ success: boolean;
28
+ /** Build result with generated resources */
29
+ build?: BuildFromIncludeResult;
30
+ /** Deploy API result (if not dry run) */
31
+ deploy?: BuildApiResult;
32
+ /** Error message if failed */
33
+ error?: string;
34
+ /** Duration in milliseconds */
35
+ durationMs: number;
36
+ }
37
+
38
+ /**
39
+ * Run the deploy command
40
+ *
41
+ * Builds resources and deploys to main Tinybird workspace (production).
42
+ *
43
+ * @param options - Deploy options
44
+ * @returns Deploy command result
45
+ */
46
+ export async function runDeploy(options: DeployCommandOptions = {}): Promise<DeployCommandResult> {
47
+ const startTime = Date.now();
48
+ const cwd = options.cwd ?? process.cwd();
49
+
50
+ // Load config
51
+ let config: ResolvedConfig;
52
+ try {
53
+ config = loadConfig(cwd);
54
+ } catch (error) {
55
+ return {
56
+ success: false,
57
+ error: (error as Error).message,
58
+ durationMs: Date.now() - startTime,
59
+ };
60
+ }
61
+
62
+ // Build resources from include paths
63
+ let buildResult: BuildFromIncludeResult;
64
+ try {
65
+ buildResult = await buildFromInclude({
66
+ includePaths: config.include,
67
+ cwd: config.cwd,
68
+ });
69
+ } catch (error) {
70
+ return {
71
+ success: false,
72
+ error: `Build failed: ${(error as Error).message}`,
73
+ durationMs: Date.now() - startTime,
74
+ };
75
+ }
76
+
77
+ // If dry run, return without pushing
78
+ if (options.dryRun) {
79
+ return {
80
+ success: true,
81
+ build: buildResult,
82
+ durationMs: Date.now() - startTime,
83
+ };
84
+ }
85
+
86
+ const debug = !!process.env.TINYBIRD_DEBUG;
87
+
88
+ if (debug) {
89
+ console.log(`[debug] Deploying to main workspace`);
90
+ console.log(`[debug] baseUrl: ${config.baseUrl}`);
91
+ }
92
+
93
+ // Deploy to main workspace using /v1/deploy endpoint
94
+ let deployResult: BuildApiResult;
95
+ try {
96
+ deployResult = await deployToMain(
97
+ {
98
+ baseUrl: config.baseUrl,
99
+ token: config.token,
100
+ },
101
+ buildResult.resources,
102
+ {
103
+ check: options.check,
104
+ }
105
+ );
106
+ } catch (error) {
107
+ return {
108
+ success: false,
109
+ build: buildResult,
110
+ error: `Deploy failed: ${(error as Error).message}`,
111
+ durationMs: Date.now() - startTime,
112
+ };
113
+ }
114
+
115
+ if (!deployResult.success) {
116
+ return {
117
+ success: false,
118
+ build: buildResult,
119
+ deploy: deployResult,
120
+ error: deployResult.error,
121
+ durationMs: Date.now() - startTime,
122
+ };
123
+ }
124
+
125
+ return {
126
+ success: true,
127
+ build: buildResult,
128
+ deploy: deployResult,
129
+ durationMs: Date.now() - startTime,
130
+ };
131
+ }
@@ -187,9 +187,16 @@ export async function runDev(options: DevCommandOptions = {}): Promise<DevContro
187
187
  };
188
188
  } else {
189
189
  // Branch mode: use Tinybird cloud with branches
190
- // If we're on a feature branch, get or create the Tinybird branch
190
+ // Prevent dev mode on main branch - must use deploy command
191
+ if (config.isMainBranch || !config.tinybirdBranch) {
192
+ throw new Error(
193
+ `Cannot use 'dev' command on main branch. Use 'tinybird deploy' to deploy to production, or switch to a feature branch.`
194
+ );
195
+ }
196
+
197
+ // Get or create the Tinybird branch
191
198
  // Use tinybirdBranch (sanitized name) for Tinybird API, gitBranch for display
192
- if (!config.isMainBranch && config.tinybirdBranch) {
199
+ if (config.tinybirdBranch) {
193
200
  const branchName = config.tinybirdBranch; // Sanitized name for Tinybird
194
201
 
195
202
  // Always fetch fresh from API to avoid stale cache issues
@@ -246,10 +253,10 @@ export async function runDev(options: DevCommandOptions = {}): Promise<DevContro
246
253
  options.onBuildStart?.();
247
254
 
248
255
  try {
256
+ // Always use runBuild - main branch is blocked at startup
249
257
  const result = await runBuild({
250
258
  cwd: config.cwd,
251
259
  tokenOverride: effectiveToken,
252
- useDeployEndpoint: devMode !== "local" && config.isMainBranch,
253
260
  devModeOverride: devMode,
254
261
  });
255
262
  options.onBuildComplete?.(result);