import { Injectable } from '@angular/core' ; import { HttpClient } from '@angular/common/http' ; import { Observable } from 'rxjs' ; import { Globals } from '../shared/globals' ; import { ElectronService } from './electron.service' ; import { FileInfos } from '../models/file-infos' ; import { api } from 'cc-map-editor-common' ; @ Injectable ( ) export class HttpClientService { private readonly fileName = 'config.json' ; constructor ( private http , private electron ) { } getAllFiles ( ) { return this . request ( 'api/allFiles' , api . getAllFiles ) ; } getAllTilesets ( ) { return this . request ( 'api/allTilesets' , api . getAllTilesets ) ; } getMaps ( ) { return this . request ( 'api/allMaps' , api . getAllMaps ) ; } getMods ( ) { return this . request ( 'api/allMods' , api . getAllMods ) ; } getAssetsFile < T > ( path ) { if ( ! Globals . isElectron ) { return this . http . post < T > ( Globals . URL + 'api/get' , { path } ) ; } const cc = this . electron . getAssetsPath ( ) ; return this . toObservable ( api . get < T > ( cc , path ) ) ; } resolveFile ( path ) { if ( ! Globals . isElectron ) { return this . http . post < string > ( Globals . URL + 'api/resolve' , { path } ) ; } const cc = this . electron . getAssetsPath ( ) ; return this . toObservable ( api . resolve ( cc , path ) ) ; } saveFile ( path , content ) { const file = { path : path , content : content } ; if ( ! Globals . isElectron ) { return this . http . post < string > ( Globals . URL + 'api/saveFile' , file ) ; } return this . toObservable ( api . saveFile ( this . electron . getAssetsPath ( ) , file ) ) ; } param url param common private request < T > ( url , common ) { if ( ! Globals . isElectron ) { return this . http . get < T > ( Globals . URL + url ) ; } const path = this . electron . getAssetsPath ( ) ; return this . toObservable ( common ( path ) ) ; } private toObservable < T > ( promise ) { return new Observable < T > ( subsriber => { promise . then ( value => subsriber . next ( value ) ) . catch ( err => subsriber . error ( err ) ) . finally ( ( ) => subsriber . complete ( ) ) ; } ) ; } }