Nx: How to handle API urls.

Created on 24 Mar 2018  路  1Comment  路  Source: nrwl/nx

How to handle API path for Web and Ionic. I have my all api requests in libs. And Web part doesn't need IP address where as Ionic App needs IP address. How do I differ in between them?

Most helpful comment

You could have an APIModule that takes an argument for the address, and initialize it in both your Web app and Ionic app.

libs/api/src/api.module.ts

export const API_HOST = new InjectionToken('API_HOST');

@NgModule()
export class ApiModule {
  static forRoot(host: string) {
    return {
      ngModule: ApiModule,
      providers: [{
        provide: API_HOST,
        useValue: host
      }]
    }
  }
}

apps/mobile/src/app.module.ts

imports: [
  ApiModule.forRoot(SOME_IP_ADDRESS)
]

apps/web/src/app.module.ts

imports: [
  ApiModule.forRoot('')
]

>All comments

You could have an APIModule that takes an argument for the address, and initialize it in both your Web app and Ionic app.

libs/api/src/api.module.ts

export const API_HOST = new InjectionToken('API_HOST');

@NgModule()
export class ApiModule {
  static forRoot(host: string) {
    return {
      ngModule: ApiModule,
      providers: [{
        provide: API_HOST,
        useValue: host
      }]
    }
  }
}

apps/mobile/src/app.module.ts

imports: [
  ApiModule.forRoot(SOME_IP_ADDRESS)
]

apps/web/src/app.module.ts

imports: [
  ApiModule.forRoot('')
]
Was this page helpful?
0 / 5 - 0 ratings

Related issues

zachnewburgh picture zachnewburgh  路  3Comments

zpydee picture zpydee  路  3Comments

about-code picture about-code  路  3Comments

ZempTime picture ZempTime  路  3Comments

Koslun picture Koslun  路  3Comments