I want to send my environment configuration to my library module so i can have access to the api url, how do i do that?
This is my shared library module
export const CONFIG_TOKEN = new InjectionToken<any>('config');
@NgModule({
declarations: [
AscSharedLibsComponent,
BaseTableComponent,
ToolbarButtonsComponent,
],
imports: [
ReactiveFormsModule,
BrowserAnimationsModule,
TableModule,
InputTextModule,
ButtonModule,
RippleModule,
ToolbarModule,
FileUploadModule,
TooltipModule,
SpeedDialModule,
CheckboxModule,
StoreModule.forRoot({}),
StoreDevtoolsModule.instrument({ maxAge: 40 }),
],
exports: [AscSharedLibsComponent, BaseTableComponent],
providers: [
{ provide: CONFIG_TOKEN, useValue: {} },
],
})
export class AscSharedLibsModule {}
And the service which is also inside the library, where i would be using the config is
@Injectable({
providedIn: 'root',
})
export class TableConfigService {
constructor(
private restApi: RequestService,
@Inject(CONFIG_TOKEN) private config: any
) {
console.log('apiUrl======', this.config.apiUrl);
}
load(tableId: string) {
return this.restApi
.request(this.config['apiUrl'], 'GET', '/api/v5/app-table-designs', {
'name.equals': tableId,
})
.pipe(
map((res: any) => {
if (!res.body) return undefined;
return JSON.parse(res.body[0]['tableDefinition']);
})
);
}
}
This would be the consumer of my library, another application
@NgModule({
declarations: [AppComponent],
imports: [BrowserModule, AscSharedLibsModule, TableModule],
providers: [
Table,
TableService,
{
provide: CONFIG_TOKEN,
useValue: { apiUrl: 'your-api-url' },
},
],
bootstrap: [AppComponent],
})
export class AppModule {}
What am i doing wrong here? I am getting config api url as undefined when logged
This is my public.api.ts
/*
* Public API Surface of asc-shared-libs
*/
export * from './lib/asc-shared-libs.service';
export * from './lib/asc-shared-libs.component';
export * from './lib/asc-shared-libs.module';
export * from './lib/base-table/base-table.component';
export * from './lib/common/utils';
export * from './lib/model/base-table.model';
export * from './lib/services/request.service';
export * from './lib/services/table-config.service';
export * from './lib/services/text.service';