import { API , DEFAULT_API_OPTIONS } from '../core/api' ; import { ApiOptions , Node , FilesList , Files as FilesType , Keys , DirectoryList , Block } from '../models' ; import SchemaMiller , { MillOpts } from '../helpers/schema-miller' ; import Mills from './mills' ; import Threads from './threads' ; extends API export default class Files extends API { mills : Mills ; threads : Threads ; constructor ( opts = DEFAULT_API_OPTIONS ) { super ( opts ) ; this . mills = new Mills ( opts ) ; this . threads = new Threads ( opts ) ; } param thread param offset param limit returns async list ( thread ? , offset ? , limit ? ) { const response = await this . sendGet ( 'files' , undefined , { thread : thread || '' , offset : offset || '' , limit : limit || 5 } ) ; return response . json ( ) as Promise < FilesList > ; } param target returns async keys ( target ) { const response = await this . sendGet ( `keys/${ target }` ) ; return response . json ( ) as Promise < Keys > ; } param id returns async ignore ( id ) { const response = await this . sendDelete ( `blocks/${ id }` ) ; return response . json ( ) as Promise < Block > ; } param thread param file param caption returns async add ( file , caption , thread ? ) { if ( ! thread ) { thread = 'default' ; } const schemaNode = ( await this . threads . get ( thread ) ) . schema_node ; if ( ! schemaNode ) { throw new Error ( 'A thread schema is required before adding files to a thread.' ) ; } const dir = await SchemaMiller . mill ( file , schemaNode , async ( mill , opts , form , headers ) => { const file = await this . mills . run ( mill , opts , form , headers ) ; return file ; } ) ; const dirs = { items : [ dir ] } ; const response = await this . sendPost ( `threads/${ thread }/files` , undefined , { caption } , dirs ) ; return response . json ( ) ; } }