import { Observable } from 'rxjs/Observable' ; import { Database } from './db' ; import { Net } from './Net' ; import { forEach , isNonNullable } from './utils' ; import { SDKFetch } from './SDKFetch' ; import * as socket from './sockets' ; import * as socketInterceptor from './sockets/interceptor' ; import { schemaColl } from './schemas' ; import { SchemaColl , Variables , GraphQLRequest , GraphQLResponse , GraphQLClientOption , GraphQLQuery , GraphQLVariables , GraphQLResult , } from './utils/internalTypes' ; import { SDKAsyncJob } from './SDKAsyncJob' ; export const schemas = [ ] ; export { schemaColl } ; export { CacheStrategy } from './Net' ; export class SDK { private schemas = schemaColl . toArray ( ) ; private graphQLClientOption : GraphQLClientOption | null = null ; net = new Net ( this . schemas ) ; fetch = new SDKFetch ( ) ; asyncJob = new SDKAsyncJob ( this . fetch ) ; socketClient : socket . Client ; database : Database | undefined ; socketProxy : socket . Proxy ; lift : typeof Net . prototype . lift = ( ApiResult ) : any => { return this . net . lift ( ApiResult ) ; } ; constructor ( ) { this . socketClient = new socket . Client ( this . fetch , this . net , socket . mapMsgTypeToTable ) ; this . socketProxy = this . socketClient . proxy ; this . socketClient . interceptors . append ( socketInterceptor . redirectLike ) ; } initReactiveDB ( db ) { this . database = db ; forEach ( this . schemas , d => { this . database ! . defineSchema ( d . name , d . schema ) ; } ) ; this . database . connect ( ) ; this . socketClient . initReactiveDB ( this . database ) ; return this . net . persist ( this . database ) ; } setGraphQLEndpoint ( endpoint , requestOptions = { } ) { this . graphQLClientOption = { host : endpoint , headers : { ... requestOptions , [ 'Content-Type' ] : 'application/json' } } ; } graph < R extends object , V extends Variables = Variables , Q extends GraphQLQuery = string > ( query , variables , withHeaders ) ; graph < R extends object , V extends Variables = Variables , Q extends GraphQLQuery = string > ( query , variables , withHeaders ) ; graph < R extends object , V extends Variables = Variables , Q extends GraphQLQuery = string > ( query , variables ) ; graph < R extends object , Q extends GraphQLQuery = string > ( query ) ; graph < R extends object , V extends Variables = Variables , Q extends GraphQLQuery = string > ( query , variables ? , withHeaders = false ) { if ( ! isNonNullable ( this . graphQLClientOption ) ) { throw Error ( 'GraphQL server should be specified.' ) ; } const requestBody = JSON . stringify ( { query , variables : variables ? variables : undefined , } ) ; return this . fetch . post < GraphQLResponse < R > > ( this . graphQLClientOption . host , requestBody , { ... this . graphQLClientOption , includeHeaders : true } ) . map ( ( { headers , body } ) => { const { errors , data } = body ; if ( errors ) { const errmsg = errors . map ( ( { message } ) => `${ message }` ) ; throw new Error ( errmsg . join ( '\n' ) ) ; } return withHeaders ? { ... ( data ! as any ) , headers } : data ! ; } ) ; } }