tako-sdk 0.1.4 → 0.1.5
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/dist/index.cjs +117 -1
- package/dist/index.d.cts +110 -0
- package/dist/index.d.ts +110 -0
- package/dist/index.js +117 -1
- package/package.json +7 -8
package/dist/index.cjs
CHANGED
|
@@ -28,7 +28,7 @@ __export(src_exports, {
|
|
|
28
28
|
});
|
|
29
29
|
module.exports = __toCommonJS(src_exports);
|
|
30
30
|
|
|
31
|
-
// src/types.ts
|
|
31
|
+
// src/types/types.ts
|
|
32
32
|
var SourceIndex = /* @__PURE__ */ ((SourceIndex2) => {
|
|
33
33
|
SourceIndex2["TAKO"] = "tako";
|
|
34
34
|
SourceIndex2["WEB"] = "web";
|
|
@@ -123,6 +123,122 @@ var TakoClient = class {
|
|
|
123
123
|
const response = await this.request("/knowledge_search", "POST", requestBody);
|
|
124
124
|
return response || { outputs: { knowledge_cards: [] } };
|
|
125
125
|
}
|
|
126
|
+
/**
|
|
127
|
+
* Get an image for a knowledge card
|
|
128
|
+
* @param cardId The ID of the knowledge card
|
|
129
|
+
* @returns bytes of the image
|
|
130
|
+
*/
|
|
131
|
+
async getImage(cardId) {
|
|
132
|
+
const response = await this.request(`/image/${cardId}`, "GET");
|
|
133
|
+
return response?.image ? Uint8Array.from(atob(response.image), (c) => c.charCodeAt(0)) : new Uint8Array();
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Beta visualize a file or a dataset (beta endpoint)
|
|
137
|
+
* @param takoFormattedDataset Tako formatted dataset to visualize
|
|
138
|
+
* @param fileId The ID of the file to visualize
|
|
139
|
+
* @param query The query to visualize the dataset
|
|
140
|
+
* @returns Knowledge search results
|
|
141
|
+
*/
|
|
142
|
+
async betaVisualize(takoFormattedDataset, fileId, query) {
|
|
143
|
+
if (takoFormattedDataset == null && fileId == null) {
|
|
144
|
+
throw new Error("Either takoFormattedDataset or fileId must be provided");
|
|
145
|
+
}
|
|
146
|
+
if (takoFormattedDataset != null && fileId != null) {
|
|
147
|
+
throw new Error("Only one of takoFormattedDataset or fileId must be provided");
|
|
148
|
+
}
|
|
149
|
+
const visualizeRequest = {
|
|
150
|
+
tako_formatted_dataset: takoFormattedDataset,
|
|
151
|
+
file_id: fileId,
|
|
152
|
+
query
|
|
153
|
+
};
|
|
154
|
+
const response = await this.request("/beta/visualize", "POST", visualizeRequest);
|
|
155
|
+
return response || { outputs: { knowledge_cards: [] } };
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Upload a file to Tako using presigned URL (supports .csv, .xls, .xlsx, .parquet up to 5MB)
|
|
159
|
+
* @param file The file to upload
|
|
160
|
+
* @returns The ID of the uploaded file
|
|
161
|
+
*/
|
|
162
|
+
async betaUploadFile(file) {
|
|
163
|
+
const maxSize = 5 * 1024 * 1024;
|
|
164
|
+
if (file.size > maxSize) {
|
|
165
|
+
throw new Error("File size exceeds 5MB limit");
|
|
166
|
+
}
|
|
167
|
+
const supportedFormats = [".csv", ".xls", ".xlsx", ".parquet"];
|
|
168
|
+
const fileExtension = file.name.toLowerCase().substring(file.name.lastIndexOf("."));
|
|
169
|
+
if (!supportedFormats.includes(fileExtension)) {
|
|
170
|
+
throw new Error(`Unsupported file format. Supported formats: ${supportedFormats.join(", ")}`);
|
|
171
|
+
}
|
|
172
|
+
const uploadUrlResponse = await this.getFileUploadUrl(file.name);
|
|
173
|
+
return await this.uploadFileToAws(file, uploadUrlResponse);
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Get a presigned URL for file upload (private method)
|
|
177
|
+
* @param fileName Name of the file to upload
|
|
178
|
+
* @returns Presigned URL response with upload details
|
|
179
|
+
*/
|
|
180
|
+
async getFileUploadUrl(fileName) {
|
|
181
|
+
const url = `${this.baseUrl}${this.pathPrefix}/beta/file_upload_url?file_name=${encodeURIComponent(fileName)}`;
|
|
182
|
+
const headers = {
|
|
183
|
+
"X-API-Key": this.apiKey
|
|
184
|
+
};
|
|
185
|
+
const response = await fetch(url, {
|
|
186
|
+
method: "GET",
|
|
187
|
+
headers
|
|
188
|
+
});
|
|
189
|
+
if (!response.ok) {
|
|
190
|
+
const errorData = await response.json();
|
|
191
|
+
const message = errorData.message || response.statusText;
|
|
192
|
+
switch (response.status) {
|
|
193
|
+
case 401:
|
|
194
|
+
throw new TakoUnauthorizedException(message, errorData);
|
|
195
|
+
case 429:
|
|
196
|
+
throw new TakoRateLimitException(message, errorData);
|
|
197
|
+
default:
|
|
198
|
+
throw new TakoException(response.status, message, errorData);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
return await response.json();
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Upload file to AWS S3 using presigned URL (private method)
|
|
205
|
+
* @param file The file to upload
|
|
206
|
+
* @param uploadUrlResponse The presigned URL response
|
|
207
|
+
* @returns The file ID from the presigned URL response
|
|
208
|
+
*/
|
|
209
|
+
async uploadFileToAws(file, uploadUrlResponse) {
|
|
210
|
+
const formData = new FormData();
|
|
211
|
+
Object.entries(uploadUrlResponse.fields).forEach(([key, value]) => {
|
|
212
|
+
formData.append(key, value);
|
|
213
|
+
});
|
|
214
|
+
formData.append("file", file);
|
|
215
|
+
const response = await fetch(uploadUrlResponse.url, {
|
|
216
|
+
method: "POST",
|
|
217
|
+
body: formData
|
|
218
|
+
});
|
|
219
|
+
if (!response.ok) {
|
|
220
|
+
const errorText = await response.text();
|
|
221
|
+
throw new Error(`Failed to upload file to AWS S3: ${response.status} ${response.statusText}. ${errorText}`);
|
|
222
|
+
}
|
|
223
|
+
return uploadUrlResponse.file_id;
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* Connect a file to Tako for visualization and analysis (beta endpoint)
|
|
227
|
+
* @param fileUrl URL of the file to connect to
|
|
228
|
+
* @param fileId Optional ID of the file to connect to. If not provided, a new file ID will be generated
|
|
229
|
+
* @returns Object containing 'message' and 'id' fields
|
|
230
|
+
*/
|
|
231
|
+
async betaFileConnector(fileUrl, fileId) {
|
|
232
|
+
if (!fileUrl?.trim()) {
|
|
233
|
+
throw new Error("File URL is required");
|
|
234
|
+
}
|
|
235
|
+
const requestBody = {
|
|
236
|
+
file_url: fileUrl,
|
|
237
|
+
...fileId ? { file_id: fileId } : {}
|
|
238
|
+
};
|
|
239
|
+
const response = await this.request("/beta/file_connector", "POST", requestBody);
|
|
240
|
+
return response || { message: "", id: "" };
|
|
241
|
+
}
|
|
126
242
|
};
|
|
127
243
|
|
|
128
244
|
// src/index.ts
|
package/dist/index.d.cts
CHANGED
|
@@ -59,6 +59,76 @@ declare class TakoRateLimitException extends TakoException {
|
|
|
59
59
|
constructor(message?: string, details?: any);
|
|
60
60
|
}
|
|
61
61
|
|
|
62
|
+
declare enum TakoDataFormatValueType {
|
|
63
|
+
STRING = "string",
|
|
64
|
+
NUMBER = "number",
|
|
65
|
+
BOOLEAN = "boolean",
|
|
66
|
+
DATE = "date",
|
|
67
|
+
FLOAT = "float",
|
|
68
|
+
NULL = "null",
|
|
69
|
+
ANY = "any"
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Each cell contains a single aspect (variable + value)
|
|
73
|
+
*/
|
|
74
|
+
interface TakoDataFormatCellValue {
|
|
75
|
+
/** The name of the variable */
|
|
76
|
+
variable_name: string;
|
|
77
|
+
/**
|
|
78
|
+
* The value of the variable.
|
|
79
|
+
* If the variable is a date, format it as an ISO 8601 string.
|
|
80
|
+
*/
|
|
81
|
+
value: string | number | boolean | null;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Each cell contains a single aspect (variable + value)
|
|
85
|
+
*/
|
|
86
|
+
interface TakoDataFormatRowValues {
|
|
87
|
+
cell_values: TakoDataFormatCellValue[];
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Variable contains rich metadata about the variables for each observation
|
|
91
|
+
*/
|
|
92
|
+
interface TakoDataFormatVariable {
|
|
93
|
+
/** The human friendly name of the column variable */
|
|
94
|
+
name: string;
|
|
95
|
+
/** The type of the column variable */
|
|
96
|
+
type: TakoDataFormatValueType;
|
|
97
|
+
/** The units of the variable in the data */
|
|
98
|
+
units?: string | null;
|
|
99
|
+
/** Whether the data is sortable by this variable */
|
|
100
|
+
is_sortable?: boolean | null;
|
|
101
|
+
/** Whether a higher value of this variable is better */
|
|
102
|
+
is_higher_better?: boolean | null;
|
|
103
|
+
}
|
|
104
|
+
interface TakoDataFormatQuantitativeVariable extends TakoDataFormatVariable {
|
|
105
|
+
}
|
|
106
|
+
interface TakoDataFormatTemporalVariable extends TakoDataFormatVariable {
|
|
107
|
+
}
|
|
108
|
+
interface TakoDataFormatCategoricalVariable extends TakoDataFormatVariable {
|
|
109
|
+
}
|
|
110
|
+
type ValidTakoDataFormatVariable = TakoDataFormatTemporalVariable | TakoDataFormatCategoricalVariable | TakoDataFormatVariable | TakoDataFormatQuantitativeVariable;
|
|
111
|
+
/**
|
|
112
|
+
* A single dataset contains all column variables and all the rows of data
|
|
113
|
+
*/
|
|
114
|
+
interface TakoDataFormatDataset {
|
|
115
|
+
/** The title of the dataset */
|
|
116
|
+
title: string;
|
|
117
|
+
/** The description of the dataset */
|
|
118
|
+
description?: string | null;
|
|
119
|
+
/** Details about all variables in the dataset */
|
|
120
|
+
variables: ValidTakoDataFormatVariable[];
|
|
121
|
+
/**
|
|
122
|
+
* Each row contains a single coherent set of values with each
|
|
123
|
+
* cell having different aspects (variable + value)
|
|
124
|
+
*/
|
|
125
|
+
rows: TakoDataFormatRowValues[];
|
|
126
|
+
}
|
|
127
|
+
interface FileConnectorResponse {
|
|
128
|
+
message: string;
|
|
129
|
+
id: string;
|
|
130
|
+
}
|
|
131
|
+
|
|
62
132
|
/**
|
|
63
133
|
* Tako API Client
|
|
64
134
|
*/
|
|
@@ -86,6 +156,46 @@ declare class TakoClient {
|
|
|
86
156
|
* @returns Knowledge search results
|
|
87
157
|
*/
|
|
88
158
|
knowledgeSearch(text: string, sourceIndexes?: SourceIndex[]): Promise<KnowledgeSearchResponse>;
|
|
159
|
+
/**
|
|
160
|
+
* Get an image for a knowledge card
|
|
161
|
+
* @param cardId The ID of the knowledge card
|
|
162
|
+
* @returns bytes of the image
|
|
163
|
+
*/
|
|
164
|
+
getImage(cardId: string): Promise<Uint8Array>;
|
|
165
|
+
/**
|
|
166
|
+
* Beta visualize a file or a dataset (beta endpoint)
|
|
167
|
+
* @param takoFormattedDataset Tako formatted dataset to visualize
|
|
168
|
+
* @param fileId The ID of the file to visualize
|
|
169
|
+
* @param query The query to visualize the dataset
|
|
170
|
+
* @returns Knowledge search results
|
|
171
|
+
*/
|
|
172
|
+
betaVisualize(takoFormattedDataset?: TakoDataFormatDataset | null, fileId?: string | null, query?: string | null): Promise<KnowledgeSearchResponse>;
|
|
173
|
+
/**
|
|
174
|
+
* Upload a file to Tako using presigned URL (supports .csv, .xls, .xlsx, .parquet up to 5MB)
|
|
175
|
+
* @param file The file to upload
|
|
176
|
+
* @returns The ID of the uploaded file
|
|
177
|
+
*/
|
|
178
|
+
betaUploadFile(file: File): Promise<string>;
|
|
179
|
+
/**
|
|
180
|
+
* Get a presigned URL for file upload (private method)
|
|
181
|
+
* @param fileName Name of the file to upload
|
|
182
|
+
* @returns Presigned URL response with upload details
|
|
183
|
+
*/
|
|
184
|
+
private getFileUploadUrl;
|
|
185
|
+
/**
|
|
186
|
+
* Upload file to AWS S3 using presigned URL (private method)
|
|
187
|
+
* @param file The file to upload
|
|
188
|
+
* @param uploadUrlResponse The presigned URL response
|
|
189
|
+
* @returns The file ID from the presigned URL response
|
|
190
|
+
*/
|
|
191
|
+
private uploadFileToAws;
|
|
192
|
+
/**
|
|
193
|
+
* Connect a file to Tako for visualization and analysis (beta endpoint)
|
|
194
|
+
* @param fileUrl URL of the file to connect to
|
|
195
|
+
* @param fileId Optional ID of the file to connect to. If not provided, a new file ID will be generated
|
|
196
|
+
* @returns Object containing 'message' and 'id' fields
|
|
197
|
+
*/
|
|
198
|
+
betaFileConnector(fileUrl: string, fileId?: string): Promise<FileConnectorResponse>;
|
|
89
199
|
}
|
|
90
200
|
|
|
91
201
|
/**
|
package/dist/index.d.ts
CHANGED
|
@@ -59,6 +59,76 @@ declare class TakoRateLimitException extends TakoException {
|
|
|
59
59
|
constructor(message?: string, details?: any);
|
|
60
60
|
}
|
|
61
61
|
|
|
62
|
+
declare enum TakoDataFormatValueType {
|
|
63
|
+
STRING = "string",
|
|
64
|
+
NUMBER = "number",
|
|
65
|
+
BOOLEAN = "boolean",
|
|
66
|
+
DATE = "date",
|
|
67
|
+
FLOAT = "float",
|
|
68
|
+
NULL = "null",
|
|
69
|
+
ANY = "any"
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Each cell contains a single aspect (variable + value)
|
|
73
|
+
*/
|
|
74
|
+
interface TakoDataFormatCellValue {
|
|
75
|
+
/** The name of the variable */
|
|
76
|
+
variable_name: string;
|
|
77
|
+
/**
|
|
78
|
+
* The value of the variable.
|
|
79
|
+
* If the variable is a date, format it as an ISO 8601 string.
|
|
80
|
+
*/
|
|
81
|
+
value: string | number | boolean | null;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Each cell contains a single aspect (variable + value)
|
|
85
|
+
*/
|
|
86
|
+
interface TakoDataFormatRowValues {
|
|
87
|
+
cell_values: TakoDataFormatCellValue[];
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Variable contains rich metadata about the variables for each observation
|
|
91
|
+
*/
|
|
92
|
+
interface TakoDataFormatVariable {
|
|
93
|
+
/** The human friendly name of the column variable */
|
|
94
|
+
name: string;
|
|
95
|
+
/** The type of the column variable */
|
|
96
|
+
type: TakoDataFormatValueType;
|
|
97
|
+
/** The units of the variable in the data */
|
|
98
|
+
units?: string | null;
|
|
99
|
+
/** Whether the data is sortable by this variable */
|
|
100
|
+
is_sortable?: boolean | null;
|
|
101
|
+
/** Whether a higher value of this variable is better */
|
|
102
|
+
is_higher_better?: boolean | null;
|
|
103
|
+
}
|
|
104
|
+
interface TakoDataFormatQuantitativeVariable extends TakoDataFormatVariable {
|
|
105
|
+
}
|
|
106
|
+
interface TakoDataFormatTemporalVariable extends TakoDataFormatVariable {
|
|
107
|
+
}
|
|
108
|
+
interface TakoDataFormatCategoricalVariable extends TakoDataFormatVariable {
|
|
109
|
+
}
|
|
110
|
+
type ValidTakoDataFormatVariable = TakoDataFormatTemporalVariable | TakoDataFormatCategoricalVariable | TakoDataFormatVariable | TakoDataFormatQuantitativeVariable;
|
|
111
|
+
/**
|
|
112
|
+
* A single dataset contains all column variables and all the rows of data
|
|
113
|
+
*/
|
|
114
|
+
interface TakoDataFormatDataset {
|
|
115
|
+
/** The title of the dataset */
|
|
116
|
+
title: string;
|
|
117
|
+
/** The description of the dataset */
|
|
118
|
+
description?: string | null;
|
|
119
|
+
/** Details about all variables in the dataset */
|
|
120
|
+
variables: ValidTakoDataFormatVariable[];
|
|
121
|
+
/**
|
|
122
|
+
* Each row contains a single coherent set of values with each
|
|
123
|
+
* cell having different aspects (variable + value)
|
|
124
|
+
*/
|
|
125
|
+
rows: TakoDataFormatRowValues[];
|
|
126
|
+
}
|
|
127
|
+
interface FileConnectorResponse {
|
|
128
|
+
message: string;
|
|
129
|
+
id: string;
|
|
130
|
+
}
|
|
131
|
+
|
|
62
132
|
/**
|
|
63
133
|
* Tako API Client
|
|
64
134
|
*/
|
|
@@ -86,6 +156,46 @@ declare class TakoClient {
|
|
|
86
156
|
* @returns Knowledge search results
|
|
87
157
|
*/
|
|
88
158
|
knowledgeSearch(text: string, sourceIndexes?: SourceIndex[]): Promise<KnowledgeSearchResponse>;
|
|
159
|
+
/**
|
|
160
|
+
* Get an image for a knowledge card
|
|
161
|
+
* @param cardId The ID of the knowledge card
|
|
162
|
+
* @returns bytes of the image
|
|
163
|
+
*/
|
|
164
|
+
getImage(cardId: string): Promise<Uint8Array>;
|
|
165
|
+
/**
|
|
166
|
+
* Beta visualize a file or a dataset (beta endpoint)
|
|
167
|
+
* @param takoFormattedDataset Tako formatted dataset to visualize
|
|
168
|
+
* @param fileId The ID of the file to visualize
|
|
169
|
+
* @param query The query to visualize the dataset
|
|
170
|
+
* @returns Knowledge search results
|
|
171
|
+
*/
|
|
172
|
+
betaVisualize(takoFormattedDataset?: TakoDataFormatDataset | null, fileId?: string | null, query?: string | null): Promise<KnowledgeSearchResponse>;
|
|
173
|
+
/**
|
|
174
|
+
* Upload a file to Tako using presigned URL (supports .csv, .xls, .xlsx, .parquet up to 5MB)
|
|
175
|
+
* @param file The file to upload
|
|
176
|
+
* @returns The ID of the uploaded file
|
|
177
|
+
*/
|
|
178
|
+
betaUploadFile(file: File): Promise<string>;
|
|
179
|
+
/**
|
|
180
|
+
* Get a presigned URL for file upload (private method)
|
|
181
|
+
* @param fileName Name of the file to upload
|
|
182
|
+
* @returns Presigned URL response with upload details
|
|
183
|
+
*/
|
|
184
|
+
private getFileUploadUrl;
|
|
185
|
+
/**
|
|
186
|
+
* Upload file to AWS S3 using presigned URL (private method)
|
|
187
|
+
* @param file The file to upload
|
|
188
|
+
* @param uploadUrlResponse The presigned URL response
|
|
189
|
+
* @returns The file ID from the presigned URL response
|
|
190
|
+
*/
|
|
191
|
+
private uploadFileToAws;
|
|
192
|
+
/**
|
|
193
|
+
* Connect a file to Tako for visualization and analysis (beta endpoint)
|
|
194
|
+
* @param fileUrl URL of the file to connect to
|
|
195
|
+
* @param fileId Optional ID of the file to connect to. If not provided, a new file ID will be generated
|
|
196
|
+
* @returns Object containing 'message' and 'id' fields
|
|
197
|
+
*/
|
|
198
|
+
betaFileConnector(fileUrl: string, fileId?: string): Promise<FileConnectorResponse>;
|
|
89
199
|
}
|
|
90
200
|
|
|
91
201
|
/**
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// src/types.ts
|
|
1
|
+
// src/types/types.ts
|
|
2
2
|
var SourceIndex = /* @__PURE__ */ ((SourceIndex2) => {
|
|
3
3
|
SourceIndex2["TAKO"] = "tako";
|
|
4
4
|
SourceIndex2["WEB"] = "web";
|
|
@@ -93,6 +93,122 @@ var TakoClient = class {
|
|
|
93
93
|
const response = await this.request("/knowledge_search", "POST", requestBody);
|
|
94
94
|
return response || { outputs: { knowledge_cards: [] } };
|
|
95
95
|
}
|
|
96
|
+
/**
|
|
97
|
+
* Get an image for a knowledge card
|
|
98
|
+
* @param cardId The ID of the knowledge card
|
|
99
|
+
* @returns bytes of the image
|
|
100
|
+
*/
|
|
101
|
+
async getImage(cardId) {
|
|
102
|
+
const response = await this.request(`/image/${cardId}`, "GET");
|
|
103
|
+
return response?.image ? Uint8Array.from(atob(response.image), (c) => c.charCodeAt(0)) : new Uint8Array();
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Beta visualize a file or a dataset (beta endpoint)
|
|
107
|
+
* @param takoFormattedDataset Tako formatted dataset to visualize
|
|
108
|
+
* @param fileId The ID of the file to visualize
|
|
109
|
+
* @param query The query to visualize the dataset
|
|
110
|
+
* @returns Knowledge search results
|
|
111
|
+
*/
|
|
112
|
+
async betaVisualize(takoFormattedDataset, fileId, query) {
|
|
113
|
+
if (takoFormattedDataset == null && fileId == null) {
|
|
114
|
+
throw new Error("Either takoFormattedDataset or fileId must be provided");
|
|
115
|
+
}
|
|
116
|
+
if (takoFormattedDataset != null && fileId != null) {
|
|
117
|
+
throw new Error("Only one of takoFormattedDataset or fileId must be provided");
|
|
118
|
+
}
|
|
119
|
+
const visualizeRequest = {
|
|
120
|
+
tako_formatted_dataset: takoFormattedDataset,
|
|
121
|
+
file_id: fileId,
|
|
122
|
+
query
|
|
123
|
+
};
|
|
124
|
+
const response = await this.request("/beta/visualize", "POST", visualizeRequest);
|
|
125
|
+
return response || { outputs: { knowledge_cards: [] } };
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Upload a file to Tako using presigned URL (supports .csv, .xls, .xlsx, .parquet up to 5MB)
|
|
129
|
+
* @param file The file to upload
|
|
130
|
+
* @returns The ID of the uploaded file
|
|
131
|
+
*/
|
|
132
|
+
async betaUploadFile(file) {
|
|
133
|
+
const maxSize = 5 * 1024 * 1024;
|
|
134
|
+
if (file.size > maxSize) {
|
|
135
|
+
throw new Error("File size exceeds 5MB limit");
|
|
136
|
+
}
|
|
137
|
+
const supportedFormats = [".csv", ".xls", ".xlsx", ".parquet"];
|
|
138
|
+
const fileExtension = file.name.toLowerCase().substring(file.name.lastIndexOf("."));
|
|
139
|
+
if (!supportedFormats.includes(fileExtension)) {
|
|
140
|
+
throw new Error(`Unsupported file format. Supported formats: ${supportedFormats.join(", ")}`);
|
|
141
|
+
}
|
|
142
|
+
const uploadUrlResponse = await this.getFileUploadUrl(file.name);
|
|
143
|
+
return await this.uploadFileToAws(file, uploadUrlResponse);
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Get a presigned URL for file upload (private method)
|
|
147
|
+
* @param fileName Name of the file to upload
|
|
148
|
+
* @returns Presigned URL response with upload details
|
|
149
|
+
*/
|
|
150
|
+
async getFileUploadUrl(fileName) {
|
|
151
|
+
const url = `${this.baseUrl}${this.pathPrefix}/beta/file_upload_url?file_name=${encodeURIComponent(fileName)}`;
|
|
152
|
+
const headers = {
|
|
153
|
+
"X-API-Key": this.apiKey
|
|
154
|
+
};
|
|
155
|
+
const response = await fetch(url, {
|
|
156
|
+
method: "GET",
|
|
157
|
+
headers
|
|
158
|
+
});
|
|
159
|
+
if (!response.ok) {
|
|
160
|
+
const errorData = await response.json();
|
|
161
|
+
const message = errorData.message || response.statusText;
|
|
162
|
+
switch (response.status) {
|
|
163
|
+
case 401:
|
|
164
|
+
throw new TakoUnauthorizedException(message, errorData);
|
|
165
|
+
case 429:
|
|
166
|
+
throw new TakoRateLimitException(message, errorData);
|
|
167
|
+
default:
|
|
168
|
+
throw new TakoException(response.status, message, errorData);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
return await response.json();
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Upload file to AWS S3 using presigned URL (private method)
|
|
175
|
+
* @param file The file to upload
|
|
176
|
+
* @param uploadUrlResponse The presigned URL response
|
|
177
|
+
* @returns The file ID from the presigned URL response
|
|
178
|
+
*/
|
|
179
|
+
async uploadFileToAws(file, uploadUrlResponse) {
|
|
180
|
+
const formData = new FormData();
|
|
181
|
+
Object.entries(uploadUrlResponse.fields).forEach(([key, value]) => {
|
|
182
|
+
formData.append(key, value);
|
|
183
|
+
});
|
|
184
|
+
formData.append("file", file);
|
|
185
|
+
const response = await fetch(uploadUrlResponse.url, {
|
|
186
|
+
method: "POST",
|
|
187
|
+
body: formData
|
|
188
|
+
});
|
|
189
|
+
if (!response.ok) {
|
|
190
|
+
const errorText = await response.text();
|
|
191
|
+
throw new Error(`Failed to upload file to AWS S3: ${response.status} ${response.statusText}. ${errorText}`);
|
|
192
|
+
}
|
|
193
|
+
return uploadUrlResponse.file_id;
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Connect a file to Tako for visualization and analysis (beta endpoint)
|
|
197
|
+
* @param fileUrl URL of the file to connect to
|
|
198
|
+
* @param fileId Optional ID of the file to connect to. If not provided, a new file ID will be generated
|
|
199
|
+
* @returns Object containing 'message' and 'id' fields
|
|
200
|
+
*/
|
|
201
|
+
async betaFileConnector(fileUrl, fileId) {
|
|
202
|
+
if (!fileUrl?.trim()) {
|
|
203
|
+
throw new Error("File URL is required");
|
|
204
|
+
}
|
|
205
|
+
const requestBody = {
|
|
206
|
+
file_url: fileUrl,
|
|
207
|
+
...fileId ? { file_id: fileId } : {}
|
|
208
|
+
};
|
|
209
|
+
const response = await this.request("/beta/file_connector", "POST", requestBody);
|
|
210
|
+
return response || { message: "", id: "" };
|
|
211
|
+
}
|
|
96
212
|
};
|
|
97
213
|
|
|
98
214
|
// src/index.ts
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tako-sdk",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.5",
|
|
4
4
|
"description": "JavaScript/TypeScript SDK for the Tako API",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -12,10 +12,8 @@
|
|
|
12
12
|
"scripts": {
|
|
13
13
|
"build": "tsup src/index.ts --format cjs,esm --dts",
|
|
14
14
|
"lint": "eslint src/**/*.ts",
|
|
15
|
-
"test": "
|
|
16
|
-
"prepublishOnly": "npm run build"
|
|
17
|
-
"test:js": "node test.js",
|
|
18
|
-
"test:ts": "node --loader ts-node/esm test.ts"
|
|
15
|
+
"test": "node --import tsx test/client.test.ts",
|
|
16
|
+
"prepublishOnly": "npm run build"
|
|
19
17
|
},
|
|
20
18
|
"keywords": [
|
|
21
19
|
"tako",
|
|
@@ -34,13 +32,14 @@
|
|
|
34
32
|
"ts-jest": "^29.1.1",
|
|
35
33
|
"ts-node": "^10.9.2",
|
|
36
34
|
"tsup": "^7.1.0",
|
|
35
|
+
"tsx": "^4.20.1",
|
|
37
36
|
"typescript": "^5.1.6"
|
|
38
37
|
},
|
|
39
38
|
"peerDependencies": {
|
|
40
|
-
"typescript": ">=4.7.0",
|
|
41
|
-
"eslint": "^7.0.0 || ^8.0.0",
|
|
42
39
|
"@typescript-eslint/eslint-plugin": "^5.0.0 || ^6.0.0",
|
|
43
|
-
"@typescript-eslint/parser": "^5.0.0 || ^6.0.0"
|
|
40
|
+
"@typescript-eslint/parser": "^5.0.0 || ^6.0.0",
|
|
41
|
+
"eslint": "^7.0.0 || ^8.0.0",
|
|
42
|
+
"typescript": ">=4.7.0"
|
|
44
43
|
},
|
|
45
44
|
"engines": {
|
|
46
45
|
"node": ">=14.0.0"
|