@tinybirdco/sdk 0.0.2 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +38 -4
- package/dist/api/build.d.ts +2 -0
- package/dist/api/build.d.ts.map +1 -1
- package/dist/api/build.js +13 -0
- package/dist/api/build.js.map +1 -1
- package/dist/api/build.test.js +1 -0
- package/dist/api/build.test.js.map +1 -1
- package/dist/api/deploy.d.ts.map +1 -1
- package/dist/api/deploy.js +3 -0
- package/dist/api/deploy.js.map +1 -1
- package/dist/api/deploy.test.js +1 -0
- package/dist/api/deploy.test.js.map +1 -1
- package/dist/api/local.d.ts +92 -0
- package/dist/api/local.d.ts.map +1 -0
- package/dist/api/local.js +176 -0
- package/dist/api/local.js.map +1 -0
- package/dist/api/local.test.d.ts +2 -0
- package/dist/api/local.test.d.ts.map +1 -0
- package/dist/api/local.test.js +182 -0
- package/dist/api/local.test.js.map +1 -0
- package/dist/cli/commands/build.d.ts +3 -0
- package/dist/cli/commands/build.d.ts.map +1 -1
- package/dist/cli/commands/build.js +97 -47
- package/dist/cli/commands/build.js.map +1 -1
- package/dist/cli/commands/dev.d.ts +9 -2
- package/dist/cli/commands/dev.d.ts.map +1 -1
- package/dist/cli/commands/dev.js +56 -31
- package/dist/cli/commands/dev.js.map +1 -1
- package/dist/cli/config.d.ts +14 -0
- package/dist/cli/config.d.ts.map +1 -1
- package/dist/cli/config.js +7 -0
- package/dist/cli/config.js.map +1 -1
- package/dist/cli/config.test.js +29 -0
- package/dist/cli/config.test.js.map +1 -1
- package/dist/cli/index.js +39 -3
- package/dist/cli/index.js.map +1 -1
- package/dist/generator/connection.d.ts +49 -0
- package/dist/generator/connection.d.ts.map +1 -0
- package/dist/generator/connection.js +78 -0
- package/dist/generator/connection.js.map +1 -0
- package/dist/generator/connection.test.d.ts +2 -0
- package/dist/generator/connection.test.d.ts.map +1 -0
- package/dist/generator/connection.test.js +106 -0
- package/dist/generator/connection.test.js.map +1 -0
- package/dist/generator/datasource.d.ts.map +1 -1
- package/dist/generator/datasource.js +20 -0
- package/dist/generator/datasource.js.map +1 -1
- package/dist/generator/datasource.test.js +92 -0
- package/dist/generator/datasource.test.js.map +1 -1
- package/dist/generator/index.d.ts +8 -2
- package/dist/generator/index.d.ts.map +1 -1
- package/dist/generator/index.js +10 -3
- package/dist/generator/index.js.map +1 -1
- package/dist/generator/loader.d.ts +8 -1
- package/dist/generator/loader.d.ts.map +1 -1
- package/dist/generator/loader.js +17 -2
- package/dist/generator/loader.js.map +1 -1
- package/dist/index.d.ts +4 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/schema/connection.d.ts +83 -0
- package/dist/schema/connection.d.ts.map +1 -0
- package/dist/schema/connection.js +61 -0
- package/dist/schema/connection.js.map +1 -0
- package/dist/schema/connection.test.d.ts +2 -0
- package/dist/schema/connection.test.d.ts.map +1 -0
- package/dist/schema/connection.test.js +117 -0
- package/dist/schema/connection.test.js.map +1 -0
- package/dist/schema/datasource.d.ts +16 -0
- package/dist/schema/datasource.d.ts.map +1 -1
- package/dist/schema/datasource.js.map +1 -1
- package/dist/schema/project.d.ts +12 -3
- package/dist/schema/project.d.ts.map +1 -1
- package/dist/schema/project.js +2 -0
- package/dist/schema/project.js.map +1 -1
- package/package.json +1 -1
- package/src/api/build.test.ts +1 -0
- package/src/api/build.ts +20 -0
- package/src/api/deploy.test.ts +1 -0
- package/src/api/deploy.ts +3 -0
- package/src/api/local.test.ts +250 -0
- package/src/api/local.ts +270 -0
- package/src/cli/commands/build.ts +120 -54
- package/src/cli/commands/dev.ts +76 -38
- package/src/cli/config.test.ts +47 -0
- package/src/cli/config.ts +20 -0
- package/src/cli/index.ts +39 -3
- package/src/generator/connection.test.ts +135 -0
- package/src/generator/connection.ts +104 -0
- package/src/generator/datasource.test.ts +108 -0
- package/src/generator/datasource.ts +27 -1
- package/src/generator/index.ts +16 -4
- package/src/generator/loader.ts +21 -3
- package/src/index.ts +12 -0
- package/src/schema/connection.test.ts +149 -0
- package/src/schema/connection.ts +123 -0
- package/src/schema/datasource.ts +17 -0
- package/src/schema/project.ts +20 -5
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
import { describe, it, expect, beforeAll, afterEach, afterAll } from "vitest";
|
|
2
|
+
import { setupServer } from "msw/node";
|
|
3
|
+
import { http, HttpResponse } from "msw";
|
|
4
|
+
import {
|
|
5
|
+
getLocalTokens,
|
|
6
|
+
listLocalWorkspaces,
|
|
7
|
+
createLocalWorkspace,
|
|
8
|
+
getOrCreateLocalWorkspace,
|
|
9
|
+
isLocalRunning,
|
|
10
|
+
getLocalWorkspaceName,
|
|
11
|
+
LocalNotRunningError,
|
|
12
|
+
LocalApiError,
|
|
13
|
+
} from "./local.js";
|
|
14
|
+
import { LOCAL_BASE_URL } from "../cli/config.js";
|
|
15
|
+
|
|
16
|
+
const server = setupServer();
|
|
17
|
+
|
|
18
|
+
beforeAll(() => server.listen({ onUnhandledRequest: "error" }));
|
|
19
|
+
afterEach(() => server.resetHandlers());
|
|
20
|
+
afterAll(() => server.close());
|
|
21
|
+
|
|
22
|
+
describe("Local API", () => {
|
|
23
|
+
describe("isLocalRunning", () => {
|
|
24
|
+
it("returns true when local container is running", async () => {
|
|
25
|
+
server.use(
|
|
26
|
+
http.get(`${LOCAL_BASE_URL}/tokens`, () => {
|
|
27
|
+
return HttpResponse.json({
|
|
28
|
+
user_token: "user-token",
|
|
29
|
+
admin_token: "admin-token",
|
|
30
|
+
workspace_admin_token: "workspace-token",
|
|
31
|
+
});
|
|
32
|
+
})
|
|
33
|
+
);
|
|
34
|
+
|
|
35
|
+
const result = await isLocalRunning();
|
|
36
|
+
expect(result).toBe(true);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("returns false when local container is not running", async () => {
|
|
40
|
+
server.use(
|
|
41
|
+
http.get(`${LOCAL_BASE_URL}/tokens`, () => {
|
|
42
|
+
return HttpResponse.error();
|
|
43
|
+
})
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
const result = await isLocalRunning();
|
|
47
|
+
expect(result).toBe(false);
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
describe("getLocalTokens", () => {
|
|
52
|
+
it("returns tokens from local container", async () => {
|
|
53
|
+
server.use(
|
|
54
|
+
http.get(`${LOCAL_BASE_URL}/tokens`, () => {
|
|
55
|
+
return HttpResponse.json({
|
|
56
|
+
user_token: "user-token-123",
|
|
57
|
+
admin_token: "admin-token-456",
|
|
58
|
+
workspace_admin_token: "workspace-token-789",
|
|
59
|
+
});
|
|
60
|
+
})
|
|
61
|
+
);
|
|
62
|
+
|
|
63
|
+
const tokens = await getLocalTokens();
|
|
64
|
+
|
|
65
|
+
expect(tokens.user_token).toBe("user-token-123");
|
|
66
|
+
expect(tokens.admin_token).toBe("admin-token-456");
|
|
67
|
+
expect(tokens.workspace_admin_token).toBe("workspace-token-789");
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it("throws LocalNotRunningError when container is not running", async () => {
|
|
71
|
+
server.use(
|
|
72
|
+
http.get(`${LOCAL_BASE_URL}/tokens`, () => {
|
|
73
|
+
return HttpResponse.error();
|
|
74
|
+
})
|
|
75
|
+
);
|
|
76
|
+
|
|
77
|
+
await expect(getLocalTokens()).rejects.toThrow(LocalNotRunningError);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it("throws LocalApiError when response is invalid", async () => {
|
|
81
|
+
server.use(
|
|
82
|
+
http.get(`${LOCAL_BASE_URL}/tokens`, () => {
|
|
83
|
+
return HttpResponse.json({
|
|
84
|
+
// Missing required fields
|
|
85
|
+
user_token: "user-token",
|
|
86
|
+
});
|
|
87
|
+
})
|
|
88
|
+
);
|
|
89
|
+
|
|
90
|
+
await expect(getLocalTokens()).rejects.toThrow(LocalApiError);
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
describe("listLocalWorkspaces", () => {
|
|
95
|
+
it("returns list of workspaces", async () => {
|
|
96
|
+
server.use(
|
|
97
|
+
http.get(`${LOCAL_BASE_URL}/v1/user/workspaces`, () => {
|
|
98
|
+
return HttpResponse.json({
|
|
99
|
+
organization_id: "org-123",
|
|
100
|
+
workspaces: [
|
|
101
|
+
{ id: "ws-1", name: "Workspace1", token: "token-1" },
|
|
102
|
+
{ id: "ws-2", name: "Workspace2", token: "token-2" },
|
|
103
|
+
],
|
|
104
|
+
});
|
|
105
|
+
})
|
|
106
|
+
);
|
|
107
|
+
|
|
108
|
+
const result = await listLocalWorkspaces("admin-token");
|
|
109
|
+
|
|
110
|
+
expect(result.organizationId).toBe("org-123");
|
|
111
|
+
expect(result.workspaces).toHaveLength(2);
|
|
112
|
+
expect(result.workspaces[0]).toEqual({
|
|
113
|
+
id: "ws-1",
|
|
114
|
+
name: "Workspace1",
|
|
115
|
+
token: "token-1",
|
|
116
|
+
});
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it("throws LocalApiError on failure", async () => {
|
|
120
|
+
server.use(
|
|
121
|
+
http.get(`${LOCAL_BASE_URL}/v1/user/workspaces`, () => {
|
|
122
|
+
return new HttpResponse("Not found", { status: 404 });
|
|
123
|
+
})
|
|
124
|
+
);
|
|
125
|
+
|
|
126
|
+
await expect(listLocalWorkspaces("admin-token")).rejects.toThrow(LocalApiError);
|
|
127
|
+
});
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
describe("createLocalWorkspace", () => {
|
|
131
|
+
it("creates a new workspace", async () => {
|
|
132
|
+
server.use(
|
|
133
|
+
http.post(`${LOCAL_BASE_URL}/v1/workspaces`, async ({ request }) => {
|
|
134
|
+
const formData = await request.text();
|
|
135
|
+
const params = new URLSearchParams(formData);
|
|
136
|
+
return HttpResponse.json({
|
|
137
|
+
id: "new-ws-id",
|
|
138
|
+
name: params.get("name"),
|
|
139
|
+
token: "new-ws-token",
|
|
140
|
+
});
|
|
141
|
+
})
|
|
142
|
+
);
|
|
143
|
+
|
|
144
|
+
const result = await createLocalWorkspace("user-token", "TestWorkspace");
|
|
145
|
+
|
|
146
|
+
expect(result.id).toBe("new-ws-id");
|
|
147
|
+
expect(result.name).toBe("TestWorkspace");
|
|
148
|
+
expect(result.token).toBe("new-ws-token");
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
it("throws LocalApiError on failure", async () => {
|
|
152
|
+
server.use(
|
|
153
|
+
http.post(`${LOCAL_BASE_URL}/v1/workspaces`, () => {
|
|
154
|
+
return new HttpResponse("Server error", { status: 500 });
|
|
155
|
+
})
|
|
156
|
+
);
|
|
157
|
+
|
|
158
|
+
await expect(createLocalWorkspace("user-token", "TestWorkspace")).rejects.toThrow(
|
|
159
|
+
LocalApiError
|
|
160
|
+
);
|
|
161
|
+
});
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
describe("getOrCreateLocalWorkspace", () => {
|
|
165
|
+
const tokens = {
|
|
166
|
+
user_token: "user-token",
|
|
167
|
+
admin_token: "admin-token",
|
|
168
|
+
workspace_admin_token: "default-token",
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
it("returns existing workspace if found", async () => {
|
|
172
|
+
server.use(
|
|
173
|
+
http.get(`${LOCAL_BASE_URL}/v1/user/workspaces`, () => {
|
|
174
|
+
return HttpResponse.json({
|
|
175
|
+
organization_id: "org-123",
|
|
176
|
+
workspaces: [
|
|
177
|
+
{ id: "existing-ws", name: "MyWorkspace", token: "existing-token" },
|
|
178
|
+
],
|
|
179
|
+
});
|
|
180
|
+
})
|
|
181
|
+
);
|
|
182
|
+
|
|
183
|
+
const result = await getOrCreateLocalWorkspace(tokens, "MyWorkspace");
|
|
184
|
+
|
|
185
|
+
expect(result.wasCreated).toBe(false);
|
|
186
|
+
expect(result.workspace.name).toBe("MyWorkspace");
|
|
187
|
+
expect(result.workspace.token).toBe("existing-token");
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
it("creates new workspace if not found", async () => {
|
|
191
|
+
let createCalled = false;
|
|
192
|
+
|
|
193
|
+
server.use(
|
|
194
|
+
http.get(`${LOCAL_BASE_URL}/v1/user/workspaces`, () => {
|
|
195
|
+
// Return different response based on whether create was called
|
|
196
|
+
if (createCalled) {
|
|
197
|
+
return HttpResponse.json({
|
|
198
|
+
organization_id: "org-123",
|
|
199
|
+
workspaces: [
|
|
200
|
+
{ id: "new-ws", name: "NewWorkspace", token: "new-token" },
|
|
201
|
+
],
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
return HttpResponse.json({
|
|
205
|
+
organization_id: "org-123",
|
|
206
|
+
workspaces: [], // Empty initially
|
|
207
|
+
});
|
|
208
|
+
}),
|
|
209
|
+
http.post(`${LOCAL_BASE_URL}/v1/workspaces`, () => {
|
|
210
|
+
createCalled = true;
|
|
211
|
+
return HttpResponse.json({
|
|
212
|
+
id: "new-ws",
|
|
213
|
+
name: "NewWorkspace",
|
|
214
|
+
token: "new-token",
|
|
215
|
+
});
|
|
216
|
+
})
|
|
217
|
+
);
|
|
218
|
+
|
|
219
|
+
const result = await getOrCreateLocalWorkspace(tokens, "NewWorkspace");
|
|
220
|
+
|
|
221
|
+
expect(result.wasCreated).toBe(true);
|
|
222
|
+
expect(result.workspace.name).toBe("NewWorkspace");
|
|
223
|
+
expect(result.workspace.token).toBe("new-token");
|
|
224
|
+
});
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
describe("getLocalWorkspaceName", () => {
|
|
228
|
+
it("uses branch name when available", () => {
|
|
229
|
+
const name = getLocalWorkspaceName("feature_branch", "/some/path");
|
|
230
|
+
expect(name).toBe("feature_branch");
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
it("uses hash-based name when no branch", () => {
|
|
234
|
+
const name = getLocalWorkspaceName(null, "/some/path");
|
|
235
|
+
expect(name).toMatch(/^Build_[a-f0-9]{16}$/);
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
it("generates consistent hash for same path", () => {
|
|
239
|
+
const name1 = getLocalWorkspaceName(null, "/same/path");
|
|
240
|
+
const name2 = getLocalWorkspaceName(null, "/same/path");
|
|
241
|
+
expect(name1).toBe(name2);
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
it("generates different hash for different paths", () => {
|
|
245
|
+
const name1 = getLocalWorkspaceName(null, "/path/one");
|
|
246
|
+
const name2 = getLocalWorkspaceName(null, "/path/two");
|
|
247
|
+
expect(name1).not.toBe(name2);
|
|
248
|
+
});
|
|
249
|
+
});
|
|
250
|
+
});
|
package/src/api/local.ts
ADDED
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Local Tinybird container API client
|
|
3
|
+
* For use with tinybird-local Docker image
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import * as crypto from "crypto";
|
|
7
|
+
import { LOCAL_BASE_URL } from "../cli/config.js";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Tokens returned by the local /tokens endpoint
|
|
11
|
+
*/
|
|
12
|
+
export interface LocalTokens {
|
|
13
|
+
/** User token for user-level operations */
|
|
14
|
+
user_token: string;
|
|
15
|
+
/** Admin token for admin operations like listing workspaces */
|
|
16
|
+
admin_token: string;
|
|
17
|
+
/** Default workspace admin token */
|
|
18
|
+
workspace_admin_token: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Workspace info from local Tinybird
|
|
23
|
+
*/
|
|
24
|
+
export interface LocalWorkspace {
|
|
25
|
+
/** Workspace ID */
|
|
26
|
+
id: string;
|
|
27
|
+
/** Workspace name */
|
|
28
|
+
name: string;
|
|
29
|
+
/** Workspace token */
|
|
30
|
+
token: string;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Response from /v1/user/workspaces endpoint
|
|
35
|
+
*/
|
|
36
|
+
interface UserWorkspacesResponse {
|
|
37
|
+
organization_id?: string;
|
|
38
|
+
workspaces: Array<{
|
|
39
|
+
id: string;
|
|
40
|
+
name: string;
|
|
41
|
+
token: string;
|
|
42
|
+
}>;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Error thrown when local container is not running
|
|
47
|
+
*/
|
|
48
|
+
export class LocalNotRunningError extends Error {
|
|
49
|
+
constructor() {
|
|
50
|
+
super(
|
|
51
|
+
`Tinybird local is not running. Start it with:\n` +
|
|
52
|
+
`docker run -d -p 7181:7181 --name tinybird-local tinybirdco/tinybird-local:latest`
|
|
53
|
+
);
|
|
54
|
+
this.name = "LocalNotRunningError";
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Error thrown by local API operations
|
|
60
|
+
*/
|
|
61
|
+
export class LocalApiError extends Error {
|
|
62
|
+
constructor(
|
|
63
|
+
message: string,
|
|
64
|
+
public readonly status?: number,
|
|
65
|
+
public readonly body?: unknown
|
|
66
|
+
) {
|
|
67
|
+
super(message);
|
|
68
|
+
this.name = "LocalApiError";
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Check if local Tinybird container is running
|
|
74
|
+
*
|
|
75
|
+
* @returns true if container is running and healthy
|
|
76
|
+
*/
|
|
77
|
+
export async function isLocalRunning(): Promise<boolean> {
|
|
78
|
+
try {
|
|
79
|
+
const response = await fetch(`${LOCAL_BASE_URL}/tokens`, {
|
|
80
|
+
method: "GET",
|
|
81
|
+
signal: AbortSignal.timeout(5000),
|
|
82
|
+
});
|
|
83
|
+
return response.ok;
|
|
84
|
+
} catch {
|
|
85
|
+
return false;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Get tokens from local Tinybird container
|
|
91
|
+
*
|
|
92
|
+
* @returns Local tokens
|
|
93
|
+
* @throws LocalNotRunningError if container is not running
|
|
94
|
+
*/
|
|
95
|
+
export async function getLocalTokens(): Promise<LocalTokens> {
|
|
96
|
+
try {
|
|
97
|
+
const response = await fetch(`${LOCAL_BASE_URL}/tokens`, {
|
|
98
|
+
method: "GET",
|
|
99
|
+
signal: AbortSignal.timeout(5000),
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
if (!response.ok) {
|
|
103
|
+
throw new LocalApiError(
|
|
104
|
+
`Failed to get local tokens: ${response.status} ${response.statusText}`,
|
|
105
|
+
response.status
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const tokens = (await response.json()) as LocalTokens;
|
|
110
|
+
|
|
111
|
+
// Validate response structure
|
|
112
|
+
if (!tokens.user_token || !tokens.admin_token || !tokens.workspace_admin_token) {
|
|
113
|
+
throw new LocalApiError(
|
|
114
|
+
"Invalid tokens response from local Tinybird - missing required fields"
|
|
115
|
+
);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
return tokens;
|
|
119
|
+
} catch (error) {
|
|
120
|
+
if (error instanceof LocalApiError) {
|
|
121
|
+
throw error;
|
|
122
|
+
}
|
|
123
|
+
// Connection error - container not running
|
|
124
|
+
throw new LocalNotRunningError();
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* List workspaces in local Tinybird
|
|
130
|
+
*
|
|
131
|
+
* @param adminToken - Admin token from getLocalTokens()
|
|
132
|
+
* @returns List of workspaces with their info
|
|
133
|
+
*/
|
|
134
|
+
export async function listLocalWorkspaces(
|
|
135
|
+
adminToken: string
|
|
136
|
+
): Promise<{ workspaces: LocalWorkspace[]; organizationId?: string }> {
|
|
137
|
+
const url = `${LOCAL_BASE_URL}/v1/user/workspaces?with_organization=true&token=${adminToken}`;
|
|
138
|
+
|
|
139
|
+
const response = await fetch(url, {
|
|
140
|
+
method: "GET",
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
if (!response.ok) {
|
|
144
|
+
const body = await response.text();
|
|
145
|
+
throw new LocalApiError(
|
|
146
|
+
`Failed to list local workspaces: ${response.status} ${response.statusText}`,
|
|
147
|
+
response.status,
|
|
148
|
+
body
|
|
149
|
+
);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
const data = (await response.json()) as UserWorkspacesResponse;
|
|
153
|
+
|
|
154
|
+
return {
|
|
155
|
+
workspaces: data.workspaces.map((ws) => ({
|
|
156
|
+
id: ws.id,
|
|
157
|
+
name: ws.name,
|
|
158
|
+
token: ws.token,
|
|
159
|
+
})),
|
|
160
|
+
organizationId: data.organization_id,
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Create a workspace in local Tinybird
|
|
166
|
+
*
|
|
167
|
+
* @param userToken - User token from getLocalTokens()
|
|
168
|
+
* @param workspaceName - Name for the new workspace
|
|
169
|
+
* @param organizationId - Organization ID to assign workspace to
|
|
170
|
+
* @returns Created workspace info
|
|
171
|
+
*/
|
|
172
|
+
export async function createLocalWorkspace(
|
|
173
|
+
userToken: string,
|
|
174
|
+
workspaceName: string,
|
|
175
|
+
organizationId?: string
|
|
176
|
+
): Promise<LocalWorkspace> {
|
|
177
|
+
const url = `${LOCAL_BASE_URL}/v1/workspaces`;
|
|
178
|
+
|
|
179
|
+
const formData = new URLSearchParams();
|
|
180
|
+
formData.append("name", workspaceName);
|
|
181
|
+
if (organizationId) {
|
|
182
|
+
formData.append("assign_to_organization_id", organizationId);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
const response = await fetch(url, {
|
|
186
|
+
method: "POST",
|
|
187
|
+
headers: {
|
|
188
|
+
Authorization: `Bearer ${userToken}`,
|
|
189
|
+
"Content-Type": "application/x-www-form-urlencoded",
|
|
190
|
+
},
|
|
191
|
+
body: formData.toString(),
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
if (!response.ok) {
|
|
195
|
+
const responseBody = await response.text();
|
|
196
|
+
throw new LocalApiError(
|
|
197
|
+
`Failed to create local workspace: ${response.status} ${response.statusText}`,
|
|
198
|
+
response.status,
|
|
199
|
+
responseBody
|
|
200
|
+
);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
const data = (await response.json()) as { id: string; name: string; token: string };
|
|
204
|
+
|
|
205
|
+
return {
|
|
206
|
+
id: data.id,
|
|
207
|
+
name: data.name,
|
|
208
|
+
token: data.token,
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Get or create a workspace in local Tinybird
|
|
214
|
+
*
|
|
215
|
+
* @param tokens - Tokens from getLocalTokens()
|
|
216
|
+
* @param workspaceName - Name of the workspace to get or create
|
|
217
|
+
* @returns Workspace info and whether it was newly created
|
|
218
|
+
*/
|
|
219
|
+
export async function getOrCreateLocalWorkspace(
|
|
220
|
+
tokens: LocalTokens,
|
|
221
|
+
workspaceName: string
|
|
222
|
+
): Promise<{ workspace: LocalWorkspace; wasCreated: boolean }> {
|
|
223
|
+
// List existing workspaces
|
|
224
|
+
const { workspaces, organizationId } = await listLocalWorkspaces(tokens.admin_token);
|
|
225
|
+
|
|
226
|
+
// Check if workspace already exists
|
|
227
|
+
const existing = workspaces.find((ws) => ws.name === workspaceName);
|
|
228
|
+
if (existing) {
|
|
229
|
+
return { workspace: existing, wasCreated: false };
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
// Create new workspace
|
|
233
|
+
await createLocalWorkspace(
|
|
234
|
+
tokens.user_token,
|
|
235
|
+
workspaceName,
|
|
236
|
+
organizationId
|
|
237
|
+
);
|
|
238
|
+
|
|
239
|
+
// Fetch the workspace again to get the token (create response may not include it)
|
|
240
|
+
const { workspaces: updatedWorkspaces } = await listLocalWorkspaces(tokens.admin_token);
|
|
241
|
+
const newWorkspace = updatedWorkspaces.find((ws) => ws.name === workspaceName);
|
|
242
|
+
|
|
243
|
+
if (!newWorkspace) {
|
|
244
|
+
throw new LocalApiError(
|
|
245
|
+
`Created workspace '${workspaceName}' but could not find it in workspace list`
|
|
246
|
+
);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
return { workspace: newWorkspace, wasCreated: true };
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* Get workspace name for local mode based on git branch or path
|
|
254
|
+
*
|
|
255
|
+
* @param tinybirdBranch - Sanitized git branch name (or null if not in git)
|
|
256
|
+
* @param cwd - Current working directory (used for hash if no branch)
|
|
257
|
+
* @returns Workspace name to use
|
|
258
|
+
*/
|
|
259
|
+
export function getLocalWorkspaceName(
|
|
260
|
+
tinybirdBranch: string | null,
|
|
261
|
+
cwd: string
|
|
262
|
+
): string {
|
|
263
|
+
if (tinybirdBranch) {
|
|
264
|
+
return tinybirdBranch;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
// No branch detected - use hash of path like Python implementation
|
|
268
|
+
const hash = crypto.createHash("sha256").update(cwd).digest("hex");
|
|
269
|
+
return `Build_${hash.substring(0, 16)}`;
|
|
270
|
+
}
|