Skip to content

Commit

Permalink
feat: Add support work over remote config
Browse files Browse the repository at this point in the history
  • Loading branch information
EndyKaufman committed Apr 23, 2019
1 parent 7266606 commit 9d4c133
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 4 deletions.
6 changes: 6 additions & 0 deletions apps/demo/src/app/app.module.ts
Expand Up @@ -19,12 +19,18 @@ import { APP_ROUTES } from './app.routes';
import { config } from './config/config';
import { SharedModule } from './shared/shared.module';
import { metaFactory } from './utils/meta-factory';
import { NgxRemoteConfigModule } from 'ngx-remote-config';
@NgModule({
declarations: [AppComponent],
entryComponents: [],
imports: [
CommonModule,
HttpClientModule,
NgxRemoteConfigModule.forRoot({
withoutIterceptor: !environment.production,
url: !environment.production ? undefined : environment.remoteConfig.url,
default: !environment.production ? undefined : environment.remoteConfig.default
}),
SharedModule,
IonicStorageModule.forRoot(),
TranslateModule.forRoot(),
Expand Down
3 changes: 3 additions & 0 deletions apps/demo/src/environments/environment.interface.ts
@@ -1,6 +1,9 @@
import { INgxRemoteConfig } from 'ngx-remote-config';

export interface IEnvironment {
server: boolean;
type: 'prod' | 'development';
production: boolean;
apiUrl: string;
remoteConfig?: INgxRemoteConfig;
}
8 changes: 7 additions & 1 deletion apps/demo/src/environments/environment.prod.ts
Expand Up @@ -4,5 +4,11 @@ export const environment: IEnvironment = {
server: false,
type: 'prod',
production: true,
apiUrl: 'https://core-nestjs.rucken.io/api'
apiUrl: '/api',
remoteConfig: {
url: 'https://testapi.io/api/EndyKaufman/rucken-ionic.json',
default: {
'/api/(.*)': 'https://core-nestjs.rucken.io/api/'
}
}
};
4 changes: 3 additions & 1 deletion libs/rucken/ionic/package.json
Expand Up @@ -77,6 +77,8 @@
"ngx-permissions": "^6.0.4",
"ngx-repository": "^0.6.3",
"bind-observable": "^1.0.2",
"@ionic/storage": "^2.2.0"
"@ionic/storage": "^2.2.0",
"@ionic-native/http": "^5.5.0",
"ngx-remote-config": "0.0.5"
}
}
1 change: 1 addition & 0 deletions libs/rucken/ionic/src/index.ts
Expand Up @@ -44,3 +44,4 @@ export * from './lib/modules/storage/sqlite.storage';
export * from './lib/services/groups.service';
export * from './lib/services/permissions.service';
export * from './lib/services/providers';
export * from './lib/utils/ngx-remote-config.service';
8 changes: 8 additions & 0 deletions libs/rucken/ionic/src/lib/ionic.module.ts
Expand Up @@ -7,6 +7,9 @@ import { ENTITIES_PROVIDERS } from './config/providers';
import { IonicAuthModalModule } from './modules/auth-modal/auth-modal.module';
import { IonicModalsModule } from './modules/modals/modals.module';
import { SERVICES_PROVIDERS } from './services/providers';
import { HTTP } from '@ionic-native/http/ngx';
import { NgxRemoteConfigService } from 'ngx-remote-config';
import { NgxRemoteConfigIonicService } from './utils/ngx-remote-config.service';


@NgModule({
Expand All @@ -22,6 +25,11 @@ import { SERVICES_PROVIDERS } from './services/providers';
IonicModalsModule
],
providers: [
HTTP,
{
provide: NgxRemoteConfigService,
useClass: NgxRemoteConfigIonicService
},
{
provide: LANGUAGES_ITEM_CONFIG_TOKEN,
useValue: {
Expand Down
46 changes: 46 additions & 0 deletions libs/rucken/ionic/src/lib/utils/ngx-remote-config.service.ts
@@ -0,0 +1,46 @@
import { HttpClient } from '@angular/common/http';
import { Inject, Injectable } from '@angular/core';
import { HTTP } from '@ionic-native/http/ngx';
import { Platform } from '@ionic/angular';
import { INgxRemoteConfig, NgxRemoteConfigService, NGX_REMOTE_CONFIG } from 'ngx-remote-config';

@Injectable()
export class NgxRemoteConfigIonicService<T = any> extends NgxRemoteConfigService {
constructor(
private http: HTTP,
private platform: Platform,
@Inject(NGX_REMOTE_CONFIG) private options: INgxRemoteConfig,
private httpClient: HttpClient
) {
super(
options,
httpClient
);
}
initConfigAsync() {
if (this.platform.is('cordova')) {
return new Promise(resolve =>
this.http.disableRedirect(false).then(result =>
this.http.get(this.options.url, {}, {}).then(
response => {
let config: T;
try {
config = JSON.parse(response.data);
} catch (error) {
config = response.data;
}
this.config$.next(config || {});
resolve(config);
},
response => {
const config: T = this.options.default || {};
this.config$.next(config);
resolve(config);
}
)
)
);
}
return super.initConfigAsync();
}
}
37 changes: 35 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Expand Up @@ -114,6 +114,7 @@
"@capacitor/android": "^1.0.0-beta.19",
"@capacitor/core": "1.0.0-beta.19",
"@ionic-native/core": "5.4.0",
"@ionic-native/http": "^5.5.0",
"@ionic-native/splash-screen": "5.4.0",
"@ionic-native/status-bar": "5.4.0",
"@ionic/angular": "4.2.0",
Expand All @@ -136,6 +137,7 @@
"ngx-bind-io": "^0.6.6",
"ngx-cookie-service": "^2.1.0",
"ngx-dynamic-form-builder": "^0.9.0",
"ngx-remote-config": "0.0.5",
"ngx-repository": "^0.6.3",
"object-path": "^0.11.4",
"rxjs": "6.4.0",
Expand Down

0 comments on commit 9d4c133

Please sign in to comment.