import { Options } from "got" ; import { ApiRequest } from "../http_client/base" ; import { StandartParams } from "../interfaces/standart_params" ; import { ApiError } from "../models/api_error" ; import { PaginatedResult } from "../models/paginated_result" ; import { Keyable } from "../interfaces/keyable" ; import { ClientData } from "../interfaces/client_data" ; export class BaseCollection { clientData : ClientData ; protected static rootElementName : string = "" ; protected static rootElementNameSingular : string | null = null ; protected static endpoint : string | null = null ; protected static prefixURI : string | null = null ; protected static elementClass : any = null ; protected static secondaryElementNameSingular : string | null = null ; protected static secondaryElementClass : any = null ; constructor ( clientData ) { this . clientData = clientData ; } get ( id , params = { } ) { params [ "id" ] = id ; return this . createPromise ( "GET" , params , this . populateObjectFromJsonRoot , this . handleReject , null ) ; } list ( params = { } ) { return this . createPromise ( "GET" , params , this . populateArrayFromJson , this . handleReject , null ) ; } create ( body , params = { } ) { return this . createPromise ( "POST" , params , this . populateObjectFromJson , this . handleReject , body ) ; } update ( id , body , params = { } ) { params [ "id" ] = id ; return this . createPromise ( "PUT" , params , this . populateObjectFromJson , this . handleReject , body ) ; } delete ( id , params = { } ) { params [ "id" ] = id ; return this . createPromise ( "DELETE" , params , this . returnBareJSON , this . handleReject , null ) ; } protected populateObjectFromJsonRoot ( json , headers ) { const childClass = < typeof BaseCollection > this . constructor ; if ( childClass . rootElementNameSingular ) { json = Object ( json ) [ childClass . rootElementNameSingular ] ; } return this . populateObjectFromJson ( json , headers ) ; } protected populateSecondaryObjectFromJsonRoot ( json , headers ) { const childClass = < typeof BaseCollection > this . constructor ; json = Object ( json ) [ < string > childClass . secondaryElementNameSingular ] ; return this . populateObjectFromJson ( json , headers , true ) ; } protected populateObjectFromJson ( json , _headers , secondary = false ) { const childClass = < typeof BaseCollection > this . constructor ; if ( secondary ) { return new childClass . secondaryElementClass ( json ) ; } else { return new childClass . elementClass ( json ) ; } } protected populateArrayFromJson ( json , headers ) { const childClass = < typeof BaseCollection > this . constructor ; const arr = [ ] ; const jsonArray = json [ ( < any > childClass ) . rootElementName ] ; for ( const obj of jsonArray ) { arr . push ( < this > this . populateObjectFromJson ( obj , headers ) ) ; } if ( Object ( headers ) [ "x-pagination-total-count" ] && Object ( headers ) [ "x-pagination-page" ] ) { const result = new PaginatedResult ( arr , headers ) ; return result ; } else { if ( json [ "errors" ] ) { const result = { errors : json [ "errors" ] , items : arr , } ; return result ; } else { return arr ; } } } protected populateApiErrorFromJson ( json ) { return < ApiError > json ; } protected returnBareJSON ( json ) { return json ; } protected handleReject ( data ) { return this . populateApiErrorFromJson ( data ) ; } protected createPromise ( method , params , resolveFn , rejectFn , body , uri = null ) { const childClass = < typeof BaseCollection > this . constructor ; if ( ! uri ) { uri = childClass . prefixURI ; } return new Promise ( ( resolve , reject ) => { const response = new ApiRequest ( < string > uri , method , body , params , this . clientData ) ; response . promise . then ( ( data ) => { resolve ( resolveFn . call ( this , data [ "json" ] , data [ "headers" ] ) ) ; } ) . catch ( ( data ) => { reject ( rejectFn . call ( this , data ) ) ; } ) ; } ) ; } protected objToArray ( raw_body ) { if ( ! Array . isArray ( raw_body ) ) { return Array < Object > ( raw_body ) ; } else { return raw_body ; } } }