import { ITransport } from '../transport' ; import { IItems , Item , QueryOne , QueryMany , OneItem , ManyItems , PartialItem } from '../items' ; import { ID } from '../types' ; export class ItemsHandler < T extends Item > implements IItems < T > { protected transport : ITransport ; protected endpoint : string ; constructor ( collection , transport ) { this . transport = transport ; this . endpoint = collection . startsWith ( 'directus_' ) ? `/${ collection . substring ( 9 ) }` : `/items/${ collection }` ; } async readOne ( id , query ? ) { const response = await this . transport . get < T > ( `${ this . endpoint }/${ encodeURI ( id as string ) }` , { params : query , } ) ; return response . data as T ; } async readMany ( query ? ) { const { data , meta } = await this . transport . get < T [ ] > ( `${ this . endpoint }` , { params : query , } ) ; return { data , meta , } ; } async createOne ( item , query ? ) { return ( await this . transport . post < T > ( `${ this . endpoint }` , item , { params : query , } ) ) . data ; } async createMany ( items , query ? ) { return await this . transport . post < PartialItem < T > [ ] > ( `${ this . endpoint }` , items , { params : query , } ) ; } async updateOne ( id , item , query ? ) { return ( await this . transport . patch < PartialItem < T > > ( `${ this . endpoint }/${ encodeURI ( id as string ) }` , item , { params : query , } ) ) . data ; } async updateMany ( ids , data , query ? ) { return await this . transport . patch < PartialItem < T > [ ] > ( `${ this . endpoint }` , { keys : ids , data , } , { params : query , } ) ; } async updateByQuery ( updateQuery , data , query ? ) { return await this . transport . patch < PartialItem < T > [ ] > ( `${ this . endpoint }` , { query : updateQuery , data , } , { params : query , } ) ; } async deleteOne ( id ) { await this . transport . delete ( `${ this . endpoint }/${ encodeURI ( id as string ) }` ) ; } async deleteMany ( ids ) { await this . transport . delete ( `${ this . endpoint }` , ids ) ; } }