@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
@@ -0,0 +1,178 @@
1
+ /**
2
+ * Tinybird Resources API client
3
+ * Functions to list and fetch datasources and pipes from a workspace
4
+ */
5
+ import type { WorkspaceApiConfig } from "./workspaces.js";
6
+ /**
7
+ * Error thrown by resource API operations
8
+ */
9
+ export declare class ResourceApiError extends Error {
10
+ readonly status: number;
11
+ readonly endpoint: string;
12
+ readonly body?: unknown | undefined;
13
+ constructor(message: string, status: number, endpoint: string, body?: unknown | undefined);
14
+ }
15
+ /**
16
+ * Column information from Tinybird API
17
+ */
18
+ export interface DatasourceColumn {
19
+ /** Column name */
20
+ name: string;
21
+ /** ClickHouse type (e.g., "String", "DateTime", "Nullable(String)") */
22
+ type: string;
23
+ /** JSON path for JSON extraction */
24
+ jsonpath?: string;
25
+ /** Default value expression */
26
+ default?: string;
27
+ /** Codec for compression */
28
+ codec?: string;
29
+ }
30
+ /**
31
+ * Engine information from Tinybird API
32
+ */
33
+ export interface DatasourceEngine {
34
+ /** Engine type (e.g., "MergeTree", "ReplacingMergeTree") */
35
+ type: string;
36
+ /** Sorting key columns */
37
+ sorting_key?: string;
38
+ /** Partition key expression */
39
+ partition_key?: string;
40
+ /** Primary key columns */
41
+ primary_key?: string;
42
+ /** TTL expression */
43
+ ttl?: string;
44
+ /** Version column (ReplacingMergeTree) */
45
+ ver?: string;
46
+ /** Sign column (CollapsingMergeTree) */
47
+ sign?: string;
48
+ /** Version column (VersionedCollapsingMergeTree) */
49
+ version?: string;
50
+ /** Summing columns (SummingMergeTree) */
51
+ summing_columns?: string;
52
+ }
53
+ /**
54
+ * Full datasource information from Tinybird API
55
+ */
56
+ export interface DatasourceInfo {
57
+ /** Datasource name */
58
+ name: string;
59
+ /** Human-readable description */
60
+ description?: string;
61
+ /** Column definitions */
62
+ columns: DatasourceColumn[];
63
+ /** Engine configuration */
64
+ engine: DatasourceEngine;
65
+ }
66
+ /**
67
+ * Node information from a pipe
68
+ */
69
+ export interface PipeNode {
70
+ /** Node name */
71
+ name: string;
72
+ /** SQL query */
73
+ sql: string;
74
+ /** Node description */
75
+ description?: string;
76
+ }
77
+ /**
78
+ * Parameter information from a pipe
79
+ */
80
+ export interface PipeParam {
81
+ /** Parameter name */
82
+ name: string;
83
+ /** ClickHouse type */
84
+ type: string;
85
+ /** Default value */
86
+ default?: string | number;
87
+ /** Whether the parameter is required */
88
+ required: boolean;
89
+ /** Parameter description */
90
+ description?: string;
91
+ }
92
+ /**
93
+ * Pipe type classification
94
+ */
95
+ export type PipeType = "endpoint" | "materialized" | "copy" | "pipe";
96
+ /**
97
+ * Full pipe information from Tinybird API
98
+ */
99
+ export interface PipeInfo {
100
+ /** Pipe name */
101
+ name: string;
102
+ /** Human-readable description */
103
+ description?: string;
104
+ /** Nodes in the pipe */
105
+ nodes: PipeNode[];
106
+ /** Query parameters */
107
+ params: PipeParam[];
108
+ /** Pipe type */
109
+ type: PipeType;
110
+ /** Endpoint configuration (if type is endpoint) */
111
+ endpoint?: {
112
+ enabled: boolean;
113
+ cache?: {
114
+ enabled: boolean;
115
+ ttl?: number;
116
+ };
117
+ };
118
+ /** Materialized view configuration (if type is materialized) */
119
+ materialized?: {
120
+ datasource: string;
121
+ };
122
+ /** Copy pipe configuration (if type is copy) */
123
+ copy?: {
124
+ target_datasource: string;
125
+ copy_schedule?: string;
126
+ copy_mode?: "append" | "replace";
127
+ };
128
+ /** Output column schema (for endpoints) */
129
+ output_columns: DatasourceColumn[];
130
+ }
131
+ /**
132
+ * List all datasources in the workspace
133
+ *
134
+ * @param config - API configuration
135
+ * @returns Array of datasource names
136
+ */
137
+ export declare function listDatasources(config: WorkspaceApiConfig): Promise<string[]>;
138
+ /**
139
+ * Get detailed information about a specific datasource
140
+ *
141
+ * @param config - API configuration
142
+ * @param name - Datasource name
143
+ * @returns Datasource information including schema and engine
144
+ */
145
+ export declare function getDatasource(config: WorkspaceApiConfig, name: string): Promise<DatasourceInfo>;
146
+ /**
147
+ * List all pipes in the workspace
148
+ *
149
+ * @param config - API configuration
150
+ * @returns Array of pipe names
151
+ */
152
+ export declare function listPipes(config: WorkspaceApiConfig): Promise<string[]>;
153
+ /**
154
+ * Get detailed information about a specific pipe
155
+ *
156
+ * @param config - API configuration
157
+ * @param name - Pipe name
158
+ * @returns Pipe information including nodes, params, and output schema
159
+ */
160
+ export declare function getPipe(config: WorkspaceApiConfig, name: string): Promise<PipeInfo>;
161
+ /**
162
+ * Fetch all resources from a workspace
163
+ *
164
+ * @param config - API configuration
165
+ * @returns All datasources and pipes with full details
166
+ */
167
+ export declare function fetchAllResources(config: WorkspaceApiConfig): Promise<{
168
+ datasources: DatasourceInfo[];
169
+ pipes: PipeInfo[];
170
+ }>;
171
+ /**
172
+ * Check if a workspace has any resources
173
+ *
174
+ * @param config - API configuration
175
+ * @returns True if the workspace has at least one datasource or pipe
176
+ */
177
+ export declare function hasResources(config: WorkspaceApiConfig): Promise<boolean>;
178
+ //# sourceMappingURL=resources.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"resources.d.ts","sourceRoot":"","sources":["../../src/api/resources.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;AAG1D;;GAEG;AACH,qBAAa,gBAAiB,SAAQ,KAAK;aAGvB,MAAM,EAAE,MAAM;aACd,QAAQ,EAAE,MAAM;aAChB,IAAI,CAAC,EAAE,OAAO;gBAH9B,OAAO,EAAE,MAAM,EACC,MAAM,EAAE,MAAM,EACd,QAAQ,EAAE,MAAM,EAChB,IAAI,CAAC,EAAE,OAAO,YAAA;CAKjC;AAID;;GAEG;AACH,MAAM,WAAW,gBAAgB;IAC/B,kBAAkB;IAClB,IAAI,EAAE,MAAM,CAAC;IACb,uEAAuE;IACvE,IAAI,EAAE,MAAM,CAAC;IACb,oCAAoC;IACpC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,+BAA+B;IAC/B,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,4BAA4B;IAC5B,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAED;;GAEG;AACH,MAAM,WAAW,gBAAgB;IAC/B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AC,QAAQ,CAAC,CA4EnB;AAID;;;;;GAKG;AACH,wBAAsB,iBAAiB,CACrC,MAAM,EAAE,kBAAkB,GACzB,OAAO,CAAC;IACT,WAAW,EAAE,cAAc,EAAE,CAAC;IAC9B,KAAK,EAAE,QAAQ,EAAE,CAAC;CACnB,CAAC,CAcD;AAED;;;;;GAKG;AACH,wBAAsB,YAAY,CAChC,MAAM,EAAE,kBAAkB,GACzB,OAAO,CAAC,OAAO,CAAC,CAOlB"}
@@ -0,0 +1,245 @@
1
+ /**
2
+ * Tinybird Resources API client
3
+ * Functions to list and fetch datasources and pipes from a workspace
4
+ */
5
+ import { tinybirdFetch } from "./fetcher.js";
6
+ /**
7
+ * Error thrown by resource API operations
8
+ */
9
+ export class ResourceApiError extends Error {
10
+ status;
11
+ endpoint;
12
+ body;
13
+ constructor(message, status, endpoint, body) {
14
+ super(message);
15
+ this.status = status;
16
+ this.endpoint = endpoint;
17
+ this.body = body;
18
+ this.name = "ResourceApiError";
19
+ }
20
+ }
21
+ // ============ API Helper ============
22
+ /**
23
+ * Handle API response and throw appropriate errors
24
+ */
25
+ async function handleApiResponse(response, endpoint) {
26
+ if (response.status === 401) {
27
+ throw new ResourceApiError("Invalid or expired token", 401, endpoint);
28
+ }
29
+ if (response.status === 403) {
30
+ throw new ResourceApiError("Insufficient permissions to access resources", 403, endpoint);
31
+ }
32
+ if (response.status === 404) {
33
+ throw new ResourceApiError("Resource not found", 404, endpoint);
34
+ }
35
+ if (!response.ok) {
36
+ const body = await response.text();
37
+ throw new ResourceApiError(`API request failed: ${response.status} ${response.statusText}`, response.status, endpoint, body);
38
+ }
39
+ return response.json();
40
+ }
41
+ // ============ Datasource API ============
42
+ /**
43
+ * List all datasources in the workspace
44
+ *
45
+ * @param config - API configuration
46
+ * @returns Array of datasource names
47
+ */
48
+ export async function listDatasources(config) {
49
+ const url = new URL("/v0/datasources", config.baseUrl);
50
+ const response = await tinybirdFetch(url.toString(), {
51
+ method: "GET",
52
+ headers: {
53
+ Authorization: `Bearer ${config.token}`,
54
+ },
55
+ });
56
+ const data = await handleApiResponse(response, "/v0/datasources");
57
+ return data.datasources.map((ds) => ds.name);
58
+ }
59
+ /**
60
+ * Get detailed information about a specific datasource
61
+ *
62
+ * @param config - API configuration
63
+ * @param name - Datasource name
64
+ * @returns Datasource information including schema and engine
65
+ */
66
+ export async function getDatasource(config, name) {
67
+ const url = new URL(`/v0/datasources/${encodeURIComponent(name)}`, config.baseUrl);
68
+ const response = await tinybirdFetch(url.toString(), {
69
+ method: "GET",
70
+ headers: {
71
+ Authorization: `Bearer ${config.token}`,
72
+ },
73
+ });
74
+ const data = await handleApiResponse(response, `/v0/datasources/${name}`);
75
+ // Extract columns from either schema.columns (detail) or columns (list)
76
+ const rawColumns = data.schema?.columns ?? data.columns ?? [];
77
+ // Extract engine info from the engine object
78
+ const engineObj = data.engine;
79
+ const engineType = parseEngineType(engineObj?.engine);
80
+ // Engine properties can be in different places depending on the endpoint
81
+ const sortingKey = engineObj?.sorting_key ?? engineObj?.engine_sorting_key ?? data.sorting_key;
82
+ const partitionKey = engineObj?.partition_key ?? engineObj?.engine_partition_key ?? data.partition_key;
83
+ const primaryKey = engineObj?.primary_key ?? engineObj?.engine_primary_key ?? data.primary_key;
84
+ return {
85
+ name: data.name,
86
+ description: data.description,
87
+ columns: rawColumns.map((col) => ({
88
+ name: col.name,
89
+ type: col.type,
90
+ jsonpath: col.jsonpath,
91
+ default: col.default_value,
92
+ codec: col.codec,
93
+ })),
94
+ engine: {
95
+ type: engineType,
96
+ sorting_key: sortingKey,
97
+ partition_key: partitionKey,
98
+ primary_key: primaryKey,
99
+ ttl: data.ttl,
100
+ ver: engineObj?.engine_ver,
101
+ sign: engineObj?.engine_sign,
102
+ version: engineObj?.engine_version,
103
+ summing_columns: engineObj?.engine_summing_columns,
104
+ },
105
+ };
106
+ }
107
+ /**
108
+ * Parse engine type from engine string
109
+ */
110
+ function parseEngineType(engineString) {
111
+ if (!engineString) {
112
+ return "MergeTree";
113
+ }
114
+ // Engine string might be like "MergeTree" or "ReplacingMergeTree(version_column)"
115
+ const match = engineString.match(/^(\w+)/);
116
+ return match ? match[1] : "MergeTree";
117
+ }
118
+ // ============ Pipe API ============
119
+ /**
120
+ * List all pipes in the workspace
121
+ *
122
+ * @param config - API configuration
123
+ * @returns Array of pipe names
124
+ */
125
+ export async function listPipes(config) {
126
+ const url = new URL("/v0/pipes", config.baseUrl);
127
+ const response = await tinybirdFetch(url.toString(), {
128
+ method: "GET",
129
+ headers: {
130
+ Authorization: `Bearer ${config.token}`,
131
+ },
132
+ });
133
+ const data = await handleApiResponse(response, "/v0/pipes");
134
+ return data.pipes.map((p) => p.name);
135
+ }
136
+ /**
137
+ * Get detailed information about a specific pipe
138
+ *
139
+ * @param config - API configuration
140
+ * @param name - Pipe name
141
+ * @returns Pipe information including nodes, params, and output schema
142
+ */
143
+ export async function getPipe(config, name) {
144
+ const url = new URL(`/v0/pipes/${encodeURIComponent(name)}`, config.baseUrl);
145
+ const response = await tinybirdFetch(url.toString(), {
146
+ method: "GET",
147
+ headers: {
148
+ Authorization: `Bearer ${config.token}`,
149
+ },
150
+ });
151
+ const data = await handleApiResponse(response, `/v0/pipes/${name}`);
152
+ // Determine pipe type
153
+ let pipeType = "pipe";
154
+ if (data.endpoint) {
155
+ pipeType = "endpoint";
156
+ }
157
+ else if (data.materialized_datasource) {
158
+ pipeType = "materialized";
159
+ }
160
+ else if (data.copy_target_datasource) {
161
+ pipeType = "copy";
162
+ }
163
+ // Extract nodes
164
+ const nodes = (data.nodes ?? []).map((node) => ({
165
+ name: node.name,
166
+ sql: node.sql,
167
+ description: node.description,
168
+ }));
169
+ // Extract params from all nodes (they're typically on the first node)
170
+ const params = [];
171
+ const seenParams = new Set();
172
+ for (const node of data.nodes ?? []) {
173
+ for (const param of node.params ?? []) {
174
+ if (!seenParams.has(param.name)) {
175
+ seenParams.add(param.name);
176
+ params.push({
177
+ name: param.name,
178
+ type: param.type,
179
+ default: param.default,
180
+ required: param.required ?? true,
181
+ description: param.description,
182
+ });
183
+ }
184
+ }
185
+ }
186
+ // Extract output columns from the last node
187
+ const lastNode = data.nodes?.[data.nodes.length - 1];
188
+ const outputColumns = (lastNode?.columns ?? []).map((col) => ({
189
+ name: col.name,
190
+ type: col.type,
191
+ }));
192
+ return {
193
+ name: data.name,
194
+ description: data.description,
195
+ nodes,
196
+ params,
197
+ type: pipeType,
198
+ endpoint: pipeType === "endpoint" ? { enabled: true } : undefined,
199
+ materialized: data.materialized_datasource
200
+ ? { datasource: data.materialized_datasource }
201
+ : undefined,
202
+ copy: data.copy_target_datasource
203
+ ? {
204
+ target_datasource: data.copy_target_datasource,
205
+ copy_schedule: data.copy_schedule,
206
+ copy_mode: data.copy_mode,
207
+ }
208
+ : undefined,
209
+ output_columns: outputColumns,
210
+ };
211
+ }
212
+ // ============ Convenience Functions ============
213
+ /**
214
+ * Fetch all resources from a workspace
215
+ *
216
+ * @param config - API configuration
217
+ * @returns All datasources and pipes with full details
218
+ */
219
+ export async function fetchAllResources(config) {
220
+ // List all resources first
221
+ const [datasourceNames, pipeNames] = await Promise.all([
222
+ listDatasources(config),
223
+ listPipes(config),
224
+ ]);
225
+ // Fetch details in parallel
226
+ const [datasources, pipes] = await Promise.all([
227
+ Promise.all(datasourceNames.map((name) => getDatasource(config, name))),
228
+ Promise.all(pipeNames.map((name) => getPipe(config, name))),
229
+ ]);
230
+ return { datasources, pipes };
231
+ }
232
+ /**
233
+ * Check if a workspace has any resources
234
+ *
235
+ * @param config - API configuration
236
+ * @returns True if the workspace has at least one datasource or pipe
237
+ */
238
+ export async function hasResources(config) {
239
+ const [datasourceNames, pipeNames] = await Promise.all([
240
+ listDatasources(config),
241
+ listPipes(config),
242
+ ]);
243
+ return datasourceNames.length > 0 || pipeNames.length > 0;
244
+ }
245
+ //# sourceMappingURL=resources.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"resources.js","sourceRoot":"","sources":["../../src/api/resources.ts"],"names":[],"mappings":"AA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G,CAAC,eAAe,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,aAAa,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC,CAAC;QACvE,OAAO,CAAC,GAAG,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,OAAO,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC,CAAC;KAC5D,CAAC,CAAC;IAEH,OAAO,EAAE,WAAW,EAAE,KAAK,EAAE,CAAC;AAChC,CAAC;AAED;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,YAAY,CAChC,MAA0B;IAE1B,MAAM,CAAC,eAAe,EAAE,SAAS,CAAC,GAAG,MAAM,OAAO,CAAC,GAAG,CAAC;QACrD,eAAe,CAAC,MAAM,CAAC;QACvB,SAAS,CAAC,MAAM,CAAC;KAClB,CAAC,CAAC;IAEH,OAAO,eAAe,CAAC,MAAM,GAAG,CAAC,IAAI,SAAS,CAAC,MAAM,GAAG,CAAC,CAAC;AAC5D,CAAC"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=resources.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"resources.test.d.ts","sourceRoot":"","sources":["../../src/api/resources.test.ts"],"names":[],"mappings":""}