@@ -2,20 +2,18 @@ import { setTimeout as sleep } from 'node:timers/promises'
22import chalk from 'chalk'
33import { type Command , Option } from 'commander'
44import { clientFrom } from '../../context.js'
5+ import type {
6+ CompleteTableImportResponse ,
7+ CreateTableImportResponse ,
8+ GetTableImportResponse ,
9+ } from '../../generated/v2-api.js'
510import { SimApiError , type SimClient } from '../../http/client.js'
6- import { coerce } from '../../runtime/request.js'
11+ import { coerce , type FieldSpec } from '../../runtime/request.js'
712import { contentTypeFor , localFile } from '../../transfer/local-file.js'
8- import { finishTransfer } from '../../transfer/multipart .js'
13+ import { finishUploadSession } from '../../transfer/upload-session .js'
914import { printProtocolResult } from './result.js'
1015
11- interface TableImport {
12- id : string
13- status : 'uploading' | 'queued' | 'processing' | 'completed' | 'failed' | 'canceled' | 'expired'
14- tableId : string | null
15- rowsProcessed : number
16- error : string | null
17- upload : { uploadToken : string ; partSize : number ; partCount : number } | null
18- }
16+ type TableImport = GetTableImportResponse [ 'data' ]
1917
2018interface ImportOptions {
2119 name ?: string
@@ -39,8 +37,8 @@ function tableNameFrom(fileName: string): string {
3937 return ( / ^ [ 0 - 9 ] / . test ( cleaned ) ? `_${ cleaned } ` : cleaned ) . slice ( 0 , 128 )
4038}
4139
42- function jsonFlag ( raw : string , flagName : string ) : unknown {
43- return coerce ( raw , { kind : 'object' } , { json : true } , flagName )
40+ function jsonFlag ( raw : string , flagName : string , kind : FieldSpec [ 'kind' ] ) : unknown {
41+ return coerce ( raw , { kind } , { json : true } , flagName )
4442}
4543
4644async function watchImport (
@@ -144,31 +142,33 @@ export function attachTableImport(tables: Command): void {
144142 target = { type : 'new' , name, ...( options . folderId ? { folderId : options . folderId } : { } ) }
145143 }
146144
147- const started = await client . request < { data : TableImport } > ( '/api/v2/tables/imports' , {
145+ const started = await client . request < CreateTableImportResponse > ( '/api/v2/tables/imports' , {
148146 method : 'POST' ,
149147 body : {
150148 workspaceId,
151149 source,
152150 target,
153- ...( options . mapping ? { mapping : jsonFlag ( options . mapping , 'mapping' ) } : { } ) ,
151+ ...( options . mapping ? { mapping : jsonFlag ( options . mapping , 'mapping' , 'object' ) } : { } ) ,
154152 ...( options . createColumns
155- ? { createColumns : jsonFlag ( options . createColumns , 'create-columns' ) }
153+ ? { createColumns : jsonFlag ( options . createColumns , 'create-columns' , 'array' ) }
156154 : { } ) ,
157155 ...( options . timezone ? { timezone : options . timezone } : { } ) ,
158156 } ,
159157 } )
160158
161- let job = started . data
162- if ( path && job . upload ) {
163- job = await finishTransfer < TableImport > (
159+ let job : TableImport = started . data . session
160+ if ( path ) {
161+ if ( ! local || ! started . data . uploadToken || ! started . data . transfer ) {
162+ throw new Error ( 'Local table import did not return an upload transfer' )
163+ }
164+ job = await finishUploadSession < CompleteTableImportResponse [ 'data' ] > (
164165 client ,
165166 workspaceId ,
166167 {
167168 basePath : `/api/v2/tables/imports/${ encodeURIComponent ( job . id ) } ` ,
168- uploadToken : job . upload . uploadToken ,
169- partSize : job . upload . partSize ,
170- partCount : job . upload . partCount ,
171- size : local ?. size ?? 0 ,
169+ uploadToken : started . data . uploadToken ,
170+ transfer : started . data . transfer ,
171+ size : local . size ,
172172 } ,
173173 path
174174 )
0 commit comments