import { Contracts } from '../contracts' ; import { IContentType , IContentTypeSystemAttributes , IGenericElement } from '../models' ; export class TypeMapper { mapSingleType ( response ) { return this . mapType ( response ) ; } mapMultipleTypes ( response ) { const that = this ; return response . types . map ( function ( type ) { return that . mapType ( type ) ; } ) ; } private mapType ( type ) { if ( ! type ) { throw Error ( `Cannot map type` ) ; } if ( ! type . elements ) { throw Error ( `Cannot map type elements` ) ; } const system = { codename : type . system . codename , id : type . system . id , name : type . system . name , lastModified : type . system . last_modified } ; const elements = [ ] ; const elementNames = Object . getOwnPropertyNames ( type . elements ) ; elementNames . forEach ( ( elementName ) => { const typeElement = type . elements [ elementName ] as Contracts . IViewContentTypeElementContract ; if ( ! typeElement ) { throw Error ( `Cannot find element '${ elementName }' on type '${ type }'` ) ; } const elementCodename = elementName ; const taxonomyGroup = typeElement . taxonomy_group ; const options = [ ] ; const rawOptions = typeElement . options ; if ( rawOptions ) { if ( ! Array . isArray ( rawOptions ) ) { throw Error ( `Content type 'options' property has to be an array` ) ; } rawOptions . forEach ( ( rawOption ) => { options . push ( { codename : rawOption . codename , name : rawOption . name } ) ; } ) ; } elements . push ( { codename : elementCodename , taxonomyGroup : taxonomyGroup , options : options , name : typeElement . name , type : typeElement . type } ) ; } ) ; return { elements : elements , system : system } ; } }