import { Injectable , Inject , Optional } from '@angular/core' ; import { Http , Headers , Request , RequestOptions } from '@angular/http' ; import { NgModule , ModuleWithProviders } from '@angular/core' ; import { JSONSearchParams } from './search.params' ; import { ErrorHandler } from './error.service' ; import { LoopBackAuth } from './auth.service' ; import { LoopBackConfig } from '../../lb.config' ; import { LoopBackFilter , AccessToken } from '../../models/BaseModels' ; import { SDKModels } from '../custom/SDKModels' ; import { Observable } from 'rxjs/Observable' ; import { Subject } from 'rxjs/Subject' ; import { ErrorObservable } from 'rxjs/observable/ErrorObservable' ; import 'rxjs/add/operator/catch' ; import 'rxjs/add/operator/map' ; declare var EventSource ; module author author Nikolay Matiushenkov <https://github.com/mnvx>
*  license description @ Injectable ( ) export abstract class BaseLoopBackApi { protected path : string ; protected model : any ; constructor ( @ Inject ( Http ) protected http , @ Inject ( SDKModels ) protected models , @ Inject ( LoopBackAuth ) protected auth , @ Inject ( JSONSearchParams ) protected searchParams , @ Optional ( ) @ Inject ( ErrorHandler ) protected errorHandler ) { this . model = this . models . get ( this . getModelName ( ) ) ; } method param { string } method param { string } url param { any } routeParams param { any } urlParams param { any } postBody return { Observable < any > } description public request ( method , url , routeParams = { } , urlParams = { } , postBody = { } ) { let headers = new Headers ( ) ; headers . append ( 'Content-Type' , 'application/json' ) ; this . authenticate ( url , headers ) ; Object . keys ( routeParams ) . forEach ( ( key ) => { url = url . replace ( new RegExp ( ":" + key + "(\/|$)" , "g" ) , routeParams [ key ] + "$1" ) ; } ) ; let body ; let postBodyKeys = typeof postBody === 'object' ? Object . keys ( postBody ) : [ ] ; if ( postBodyKeys . length === 1 ) { body = postBody [ postBodyKeys . shift ( ) ] ; } else { body = postBody ; } if ( urlParams . filter ) { headers . append ( 'filter' , JSON . stringify ( urlParams . filter ) ) ; delete urlParams . filter ; } /**
        CODE BELOW WILL GENERATE THE FOLLOWING ISSUES:
        - https://github.com/mean-expert-official/loopback-sdk-builder/issues/356
        - https://github.com/mean-expert-official/loopback-sdk-builder/issues/328
        if (urlParams.where) {
          headers.append('where', JSON.stringify(urlParams.where));
          delete urlParams.where;
        }
        **/ this . searchParams . setJSON ( urlParams ) ; let request = new Request ( new RequestOptions ( { headers : headers , method : method , url : url , search : Object . keys ( urlParams ) . length > 0 ? this . searchParams . getURLSearchParams ( ) : null , body : body ? JSON . stringify ( body ) : undefined } ) ) ; return this . http . request ( request ) . map ( ( res ) => ( res . text ( ) != "" ? res . json ( ) : { } ) ) . catch ( ( e ) => this . errorHandler . handleError ( e ) ) ; } method author Jonathan Casarrubias <t: johncasarrubias, gh: mean-expert-official>
     *  license param { string } url param { Headers } headers return { void } description public authenticate < T > ( url , headers ) { if ( this . auth . getAccessTokenId ( ) ) { headers . append ( 'Authorization' , LoopBackConfig . getAuthPrefix ( ) + this . auth . getAccessTokenId ( ) ) ; } } method author Jonathan Casarrubias <t: johncasarrubias, gh: mean-expert-official>
     *  license param { T } data return { Observable < T > } description public create < T > ( data ) { return this . request ( 'POST' , [ LoopBackConfig . getPath ( ) , LoopBackConfig . getApiVersion ( ) , this . model . getModelDefinition ( ) . plural ] . join ( '/' ) , undefined , undefined , { data } ) . map ( ( data ) => this . model . factory ( data ) ) ; } method author Jonathan Casarrubias <t: johncasarrubias, gh: mean-expert-official>
     *  license param { T [ ] } data return { Observable < T [ ] > } description public createMany < T > ( data ) { return this . request ( 'POST' , [ LoopBackConfig . getPath ( ) , LoopBackConfig . getApiVersion ( ) , this . model . getModelDefinition ( ) . plural ] . join ( '/' ) , undefined , undefined , { data } ) . map ( ( datum ) => datum . map ( ( data ) => this . model . factory ( data ) ) ) ; } method author Jonathan Casarrubias <t: johncasarrubias, gh: mean-expert-official>
     *  license param { any } data return { Observable < T > } description public findById < T > ( id , filter = { } ) { let _urlParams = { } ; if ( filter ) _urlParams . filter = filter ; return this . request ( 'GET' , [ LoopBackConfig . getPath ( ) , LoopBackConfig . getApiVersion ( ) , this . model . getModelDefinition ( ) . plural , ':id' ] . join ( '/' ) , { id } , _urlParams , undefined ) . map ( ( data ) => this . model . factory ( data ) ) ; } method author Jonathan Casarrubias <t: johncasarrubias, gh: mean-expert-official>
     *  license return { Observable < T [ description public find < T > ( filter = { } ) { return this . request ( 'GET' , [ LoopBackConfig . getPath ( ) , LoopBackConfig . getApiVersion ( ) , this . model . getModelDefinition ( ) . plural ] . join ( '/' ) , undefined , { filter } , undefined ) . map ( ( datum ) => datum . map ( ( data ) => this . model . factory ( data ) ) ) ; } method author Jonathan Casarrubias <t: johncasarrubias, gh: mean-expert-official>
     *  license return { Observable < T [ ] > } description public exists < T > ( id ) { return this . request ( 'GET' , [ LoopBackConfig . getPath ( ) , LoopBackConfig . getApiVersion ( ) , this . model . getModelDefinition ( ) . plural , ':id/exists' ] . join ( '/' ) , { id } , undefined , undefined ) ; } method author Jonathan Casarrubias <t: johncasarrubias, gh: mean-expert-official>
     *  license return { Observable < T > } description public findOne < T > ( filter = { } ) { return this . request ( 'GET' , [ LoopBackConfig . getPath ( ) , LoopBackConfig . getApiVersion ( ) , this . model . getModelDefinition ( ) . plural , 'findOne' ] . join ( '/' ) , undefined , { filter } , undefined ) . map ( ( data ) => this . model . factory ( data ) ) ; } method author Jonathan Casarrubias <t: johncasarrubias, gh: mean-expert-official>
     *  license return { Observable < T [ ] > } description public updateAll < T > ( where = { } , data ) { let _urlParams = { } ; if ( where ) _urlParams . where = where ; return this . request ( 'POST' , [ LoopBackConfig . getPath ( ) , LoopBackConfig . getApiVersion ( ) , this . model . getModelDefinition ( ) . plural , 'update' ] . join ( '/' ) , undefined , _urlParams , { data } ) ; } method author Jonathan Casarrubias <t: johncasarrubias, gh: mean-expert-official>
     *  license return { Observable < T > } description public deleteById < T > ( id ) { return this . request ( 'DELETE' , [ LoopBackConfig . getPath ( ) , LoopBackConfig . getApiVersion ( ) , this . model . getModelDefinition ( ) . plural , ':id' ] . join ( '/' ) , { id } , undefined , undefined ) . map ( ( data ) => this . model . factory ( data ) ) ; } method author Jonathan Casarrubias <t: johncasarrubias, gh: mean-expert-official>
     *  license return { Observable < { count : number } > } description public count ( where = { } ) { let _urlParams = { } ; if ( where ) _urlParams . where = where ; return this . request ( 'GET' , [ LoopBackConfig . getPath ( ) , LoopBackConfig . getApiVersion ( ) , this . model . getModelDefinition ( ) . plural , 'count' ] . join ( '/' ) , undefined , _urlParams , undefined ) ; } method author Jonathan Casarrubias <t: johncasarrubias, gh: mean-expert-official>
     *  license return { Observable < T > } description public updateAttributes < T > ( id , data ) { return this . request ( 'PUT' , [ LoopBackConfig . getPath ( ) , LoopBackConfig . getApiVersion ( ) , this . model . getModelDefinition ( ) . plural , ':id' ] . join ( '/' ) , { id } , undefined , { data } ) . map ( ( data ) => this . model . factory ( data ) ) ; } method author Jonathan Casarrubias <t: johncasarrubias, gh: mean-expert-official>
     *  license return { Observable < T > } description public upsert < T > ( data = { } ) { return this . request ( 'PUT' , [ LoopBackConfig . getPath ( ) , LoopBackConfig . getApiVersion ( ) , this . model . getModelDefinition ( ) . plural , ] . join ( '/' ) , undefined , undefined , { data } ) . map ( ( data ) => this . model . factory ( data ) ) ; } method author Jonathan Casarrubias <t: johncasarrubias, gh: mean-expert-official>
     *  license return { Observable < T > } description public upsertPatch < T > ( data = { } ) { return this . request ( 'PATCH' , [ LoopBackConfig . getPath ( ) , LoopBackConfig . getApiVersion ( ) , this . model . getModelDefinition ( ) . plural , ] . join ( '/' ) , undefined , undefined , { data } ) . map ( ( data ) => this . model . factory ( data ) ) ; } method author Jonathan Casarrubias <t: johncasarrubias, gh: mean-expert-official>
     *  license return { Observable < T > } description public upsertWithWhere < T > ( where = { } , data = { } ) { let _urlParams = { } ; if ( where ) _urlParams . where = where ; return this . request ( 'POST' , [ LoopBackConfig . getPath ( ) , LoopBackConfig . getApiVersion ( ) , this . model . getModelDefinition ( ) . plural , 'upsertWithWhere' ] . join ( '/' ) , undefined , _urlParams , { data } ) . map ( ( data ) => this . model . factory ( data ) ) ; } method author Jonathan Casarrubias <t: johncasarrubias, gh: mean-expert-official>
     *  license return { Observable < T > } description public replaceOrCreate < T > ( data = { } ) { return this . request ( 'POST' , [ LoopBackConfig . getPath ( ) , LoopBackConfig . getApiVersion ( ) , this . model . getModelDefinition ( ) . plural , 'replaceOrCreate' ] . join ( '/' ) , undefined , undefined , { data } ) . map ( ( data ) => this . model . factory ( data ) ) ; } method author Jonathan Casarrubias <t: johncasarrubias, gh: mean-expert-official>
     *  license return { Observable < T > } description public replaceById < T > ( id , data = { } ) { return this . request ( 'POST' , [ LoopBackConfig . getPath ( ) , LoopBackConfig . getApiVersion ( ) , this . model . getModelDefinition ( ) . plural , ':id' , 'replace' ] . join ( '/' ) , { id } , undefined , { data } ) . map ( ( data ) => this . model . factory ( data ) ) ; } method author Jonathan Casarrubias <t: johncasarrubias, gh: mean-expert-official>
     *  license return { Observable < any > } description public createChangeStream ( ) { let subject = new Subject ( ) ; if ( typeof EventSource !== 'undefined' ) { let emit = ( msg ) => subject . next ( JSON . parse ( msg . data ) ) ; var source = new EventSource ( [ LoopBackConfig . getPath ( ) , LoopBackConfig . getApiVersion ( ) , this . model . getModelDefinition ( ) . plural , 'change-stream' ] . join ( '/' ) ) ; source . addEventListener ( 'data' , emit ) ; source . onerror = emit ; } else { console . warn ( 'SDK Builder: EventSource is not supported' ) ; } return subject . asObservable ( ) ; } method author Jonathan Casarrubias <t: johncasarrubias, gh: mean-expert-official>
     *  license return { string } description abstract getModelName ( ) ; }