mirror of
https://github.com/DSpace/dspace-angular.git
synced 2025-10-07 01:54:15 +00:00
Providing global config as an opaque token.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import {
|
||||
Component,
|
||||
ChangeDetectionStrategy,
|
||||
Inject,
|
||||
ViewEncapsulation,
|
||||
OnDestroy,
|
||||
OnInit, HostListener
|
||||
@@ -9,7 +10,8 @@ import { TranslateService } from "ng2-translate";
|
||||
import { HostWindowState } from "./shared/host-window.reducer";
|
||||
import { Store } from "@ngrx/store";
|
||||
import { HostWindowResizeAction } from "./shared/host-window.actions";
|
||||
import { GlobalConfig } from "../config";
|
||||
|
||||
import { GLOBAL_CONFIG, GlobalConfig, globalConfig } from '../config';
|
||||
|
||||
@Component({
|
||||
changeDetection: ChangeDetectionStrategy.Default,
|
||||
@@ -28,13 +30,14 @@ export class AppComponent implements OnDestroy, OnInit {
|
||||
recipient: 'World'
|
||||
};
|
||||
|
||||
env: string = GlobalConfig.production;
|
||||
env: string = this.config.production;
|
||||
|
||||
styles = {
|
||||
color: 'red'
|
||||
};
|
||||
|
||||
constructor(
|
||||
@Inject(GLOBAL_CONFIG) private config: GlobalConfig,
|
||||
private translate: TranslateService,
|
||||
private store: Store<HostWindowState>
|
||||
) {
|
||||
|
@@ -1,4 +1,4 @@
|
||||
import { Injectable } from "@angular/core";
|
||||
import { Inject, Injectable } from "@angular/core";
|
||||
import { DataEffects } from "./data.effects";
|
||||
import { Serializer } from "../serializer";
|
||||
import { Collection } from "../shared/collection.model";
|
||||
@@ -9,15 +9,18 @@ import { Actions, Effect } from "@ngrx/effects";
|
||||
import { RequestCacheFindAllAction, RequestCacheFindByIDAction } from "../cache/request-cache.actions";
|
||||
import { CollectionDataService } from "./collection-data.service";
|
||||
|
||||
import { GLOBAL_CONFIG, GlobalConfig, globalConfig } from '../../../config';
|
||||
|
||||
@Injectable()
|
||||
export class CollectionDataEffects extends DataEffects<Collection> {
|
||||
constructor(
|
||||
@Inject(GLOBAL_CONFIG) config: GlobalConfig,
|
||||
actions$: Actions,
|
||||
restApi: DSpaceRESTv2Service,
|
||||
cache: ObjectCacheService,
|
||||
dataService: CollectionDataService
|
||||
) {
|
||||
super(actions$, restApi, cache, dataService);
|
||||
super(config, actions$, restApi, cache, dataService);
|
||||
}
|
||||
|
||||
protected getFindAllEndpoint(action: RequestCacheFindAllAction): string {
|
||||
|
@@ -1,9 +1,9 @@
|
||||
import { Inject } from "@angular/core";
|
||||
import { Actions } from "@ngrx/effects";
|
||||
import { Observable } from "rxjs";
|
||||
import { DSpaceRESTV2Response } from "../dspace-rest-v2/dspace-rest-v2-response.model";
|
||||
import { DSpaceRESTv2Service } from "../dspace-rest-v2/dspace-rest-v2.service";
|
||||
import { ObjectCacheService } from "../cache/object-cache.service";
|
||||
import { GlobalConfig } from "../../../config";
|
||||
import { CacheableObject } from "../cache/object-cache.reducer";
|
||||
import { Serializer } from "../serializer";
|
||||
import {
|
||||
@@ -13,12 +13,15 @@ import {
|
||||
import { DataService } from "./data.service";
|
||||
import { hasNoValue } from "../../shared/empty.util";
|
||||
|
||||
import { GlobalConfig } from '../../../config';
|
||||
|
||||
export abstract class DataEffects<T extends CacheableObject> {
|
||||
protected abstract getFindAllEndpoint(action: RequestCacheFindAllAction): string;
|
||||
protected abstract getFindByIdEndpoint(action: RequestCacheFindByIDAction): string;
|
||||
protected abstract getSerializer(): Serializer<T>;
|
||||
|
||||
constructor(
|
||||
private config: GlobalConfig,
|
||||
private actions$: Actions,
|
||||
private restApi: DSpaceRESTv2Service,
|
||||
private objectCache: ObjectCacheService,
|
||||
@@ -38,11 +41,11 @@ export abstract class DataEffects<T extends CacheableObject> {
|
||||
if (hasNoValue(t) || hasNoValue(t.uuid)) {
|
||||
throw new Error('The server returned an invalid object');
|
||||
}
|
||||
this.objectCache.add(t, GlobalConfig.cache.msToLive);
|
||||
this.objectCache.add(t, this.config.cache.msToLive);
|
||||
});
|
||||
})
|
||||
.map((ts: Array<T>) => ts.map(t => t.uuid))
|
||||
.map((ids: Array<string>) => new RequestCacheSuccessAction(action.payload.key, ids, new Date().getTime(), GlobalConfig.cache.msToLive))
|
||||
.map((ids: Array<string>) => new RequestCacheSuccessAction(action.payload.key, ids, new Date().getTime(), this.config.cache.msToLive))
|
||||
.catch((error: Error) => Observable.of(new RequestCacheErrorAction(action.payload.key, error.message)));
|
||||
});
|
||||
|
||||
@@ -56,9 +59,9 @@ export abstract class DataEffects<T extends CacheableObject> {
|
||||
if (hasNoValue(t) || hasNoValue(t.uuid)) {
|
||||
throw new Error('The server returned an invalid object');
|
||||
}
|
||||
this.objectCache.add(t, GlobalConfig.cache.msToLive);
|
||||
this.objectCache.add(t, this.config.cache.msToLive);
|
||||
})
|
||||
.map((t: T) => new RequestCacheSuccessAction(action.payload.key, [t.uuid], new Date().getTime(), GlobalConfig.cache.msToLive))
|
||||
.map((t: T) => new RequestCacheSuccessAction(action.payload.key, [t.uuid], new Date().getTime(), this.config.cache.msToLive))
|
||||
.catch((error: Error) => Observable.of(new RequestCacheErrorAction(action.payload.key, error.message)));
|
||||
});
|
||||
|
||||
|
@@ -1,4 +1,4 @@
|
||||
import { Injectable } from "@angular/core";
|
||||
import { Inject, Injectable } from "@angular/core";
|
||||
import { DataEffects } from "./data.effects";
|
||||
import { Serializer } from "../serializer";
|
||||
import { Item } from "../shared/item.model";
|
||||
@@ -9,15 +9,18 @@ import { Actions, Effect } from "@ngrx/effects";
|
||||
import { RequestCacheFindAllAction, RequestCacheFindByIDAction } from "../cache/request-cache.actions";
|
||||
import { ItemDataService } from "./item-data.service";
|
||||
|
||||
import { GLOBAL_CONFIG, GlobalConfig, globalConfig } from '../../../config';
|
||||
|
||||
@Injectable()
|
||||
export class ItemDataEffects extends DataEffects<Item> {
|
||||
constructor(
|
||||
@Inject(GLOBAL_CONFIG) config: GlobalConfig,
|
||||
actions$: Actions,
|
||||
restApi: DSpaceRESTv2Service,
|
||||
cache: ObjectCacheService,
|
||||
dataService: ItemDataService
|
||||
) {
|
||||
super(actions$, restApi, cache, dataService);
|
||||
super(config, actions$, restApi, cache, dataService);
|
||||
}
|
||||
|
||||
protected getFindAllEndpoint(action: RequestCacheFindAllAction): string {
|
||||
|
@@ -1,14 +1,16 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { Inject, Injectable } from '@angular/core';
|
||||
import { Http, RequestOptionsArgs } from '@angular/http';
|
||||
import { Observable } from 'rxjs/Observable';
|
||||
import { RESTURLCombiner } from "../url-combiner/rest-url-combiner";
|
||||
|
||||
import { GLOBAL_CONFIG, GlobalConfig, globalConfig } from '../../../config';
|
||||
|
||||
/**
|
||||
* Service to access DSpace's REST API
|
||||
*/
|
||||
@Injectable()
|
||||
export class DSpaceRESTv2Service {
|
||||
constructor(public _http: Http) {
|
||||
constructor(private http: Http, @Inject(GLOBAL_CONFIG) private config: GlobalConfig) {
|
||||
|
||||
}
|
||||
|
||||
@@ -23,7 +25,7 @@ export class DSpaceRESTv2Service {
|
||||
* An Observablse<string> containing the response from the server
|
||||
*/
|
||||
get(relativeURL: string, options?: RequestOptionsArgs): Observable<string> {
|
||||
return this._http.get(new RESTURLCombiner(relativeURL).toString(), options)
|
||||
return this.http.get(new RESTURLCombiner(this.config, relativeURL).toString(), options)
|
||||
.map(res => res.json())
|
||||
.catch(err => {
|
||||
console.log('Error: ', err);
|
||||
|
@@ -1,5 +1,6 @@
|
||||
import { URLCombiner } from "./url-combiner";
|
||||
import { GlobalConfig } from "../../../config";
|
||||
|
||||
import { GlobalConfig } from '../../../config';
|
||||
|
||||
/**
|
||||
* Combines a variable number of strings representing parts
|
||||
@@ -8,7 +9,7 @@ import { GlobalConfig } from "../../../config";
|
||||
* TODO write tests once GlobalConfig becomes injectable
|
||||
*/
|
||||
export class RESTURLCombiner extends URLCombiner {
|
||||
constructor(...parts:Array<string>) {
|
||||
super(GlobalConfig.rest.baseURL, GlobalConfig.rest.nameSpace, ...parts);
|
||||
constructor(config: GlobalConfig, ...parts: Array<string>) {
|
||||
super(config.rest.baseURL, config.rest.nameSpace, ...parts);
|
||||
}
|
||||
}
|
||||
|
@@ -1,8 +1,10 @@
|
||||
// Look in ./config folder for config
|
||||
import { OpaqueToken } from '@angular/core';
|
||||
|
||||
const path = require('path');
|
||||
import path from 'path';
|
||||
|
||||
let configContext = require.context("../config", false, /js$/);
|
||||
|
||||
let EnvConfig: any = {};
|
||||
let EnvConfigFile: string;
|
||||
let DefaultConfig: any = {};
|
||||
@@ -29,6 +31,29 @@ try {
|
||||
EnvConfig = {};
|
||||
}
|
||||
|
||||
const GlobalConfig = Object.assign(DefaultConfig, EnvConfig);
|
||||
const GLOBAL_CONFIG = new OpaqueToken('config');
|
||||
|
||||
export {GlobalConfig}
|
||||
interface GlobalConfig {
|
||||
"production": string,
|
||||
"rest": {
|
||||
"nameSpace": string,
|
||||
"baseURL": string
|
||||
},
|
||||
"ui": {
|
||||
"nameSpace": string,
|
||||
"baseURL": string
|
||||
},
|
||||
"cache": {
|
||||
"msToLive": number,
|
||||
},
|
||||
"universal": {
|
||||
"shouldRehydrate": boolean
|
||||
}
|
||||
}
|
||||
|
||||
const globalConfig = {
|
||||
provide: GLOBAL_CONFIG,
|
||||
useValue: <GlobalConfig>Object.assign(DefaultConfig, EnvConfig)
|
||||
};
|
||||
|
||||
export { GLOBAL_CONFIG, GlobalConfig, globalConfig}
|
||||
|
@@ -1,4 +1,4 @@
|
||||
import { NgModule } from '@angular/core';
|
||||
import { Inject, NgModule } from '@angular/core';
|
||||
import { Http } from '@angular/http';
|
||||
import { FormsModule } from '@angular/forms';
|
||||
import { RouterModule } from '@angular/router';
|
||||
@@ -22,7 +22,8 @@ import { effects } from '../../app/app.effects';
|
||||
// see https://github.com/angular/angular/pull/12322
|
||||
import { Meta } from '../angular2-meta';
|
||||
import { RehydrateStoreAction } from "../../app/store.actions";
|
||||
import { GlobalConfig } from "../../config";
|
||||
|
||||
import { GLOBAL_CONFIG, GlobalConfig, globalConfig } from '../../config';
|
||||
|
||||
// import * as LRU from 'modern-lru';
|
||||
|
||||
@@ -82,17 +83,19 @@ export const UNIVERSAL_KEY = 'UNIVERSAL_CACHE';
|
||||
|
||||
Meta,
|
||||
|
||||
globalConfig
|
||||
|
||||
// { provide: AUTO_PREBOOT, useValue: false } // turn off auto preboot complete
|
||||
]
|
||||
})
|
||||
export class MainModule {
|
||||
constructor(public store: Store<AppState>) {
|
||||
constructor(public store: Store<AppState>, @Inject(GLOBAL_CONFIG) private config: GlobalConfig, ) {
|
||||
// TODO(gdi2290): refactor into a lifecycle hook
|
||||
this.doRehydrate();
|
||||
}
|
||||
|
||||
doRehydrate() {
|
||||
if (GlobalConfig.universal.shouldRehydrate) {
|
||||
if (this.config.universal.shouldRehydrate) {
|
||||
let defaultValue = {};
|
||||
let serverCache = this._getCacheValue(NGRX_CACHE_KEY, defaultValue);
|
||||
this.store.dispatch(new RehydrateStoreAction(serverCache));
|
||||
|
@@ -21,6 +21,8 @@ import { effects } from '../../app/app.effects';
|
||||
// see https://github.com/angular/angular/pull/12322
|
||||
import { Meta } from '../angular2-meta';
|
||||
|
||||
import { globalConfig } from '../../config';
|
||||
|
||||
export function createTranslateLoader(http: Http) {
|
||||
return new TranslateStaticLoader(http, './assets/i18n', '.json');
|
||||
}
|
||||
@@ -70,6 +72,8 @@ export const UNIVERSAL_KEY = 'UNIVERSAL_CACHE';
|
||||
{ provide: 'LRU', useFactory: getLRU, deps: [] },
|
||||
|
||||
Meta,
|
||||
|
||||
globalConfig
|
||||
]
|
||||
})
|
||||
export class MainModule {
|
||||
|
Reference in New Issue
Block a user