Search code examples
aureliaaurelia-auth

How do I configure FetchClient to use a non-default api when using Aurelia Auth


I'm setting up aurelia-auth and configured endpoints for my authorization server and a protected api:

  aurelia.use.plugin('aurelia-api', configure => {
configure
  .registerEndpoint('auth', 'http://localhost:5000/')
  .registerEndpoint('api', 'http://localhost:5006')}

When I want to fetch data I inject the AuthService into my module and then call

this.authService.config.client.client.fetch('StaticData/offices')

but this calls against the auth endpoint not the api one, how do I tell the fetch client to use the non-default endpoint?


Solution

  • I was heading down the wrong path, you use the configuration object off aurelia-api to get an endpoint you can then call:

    import { inject } from 'aurelia-framework';
    import { Config } from 'aurelia-api'
    
    
    @inject (Config)
    export class Locations {
        constructor (private apiEndpointConfig: Config)
        {}
        dataItems;
        hasItems: boolean;
    
       created(){
    
        var api =  this.apiEndpointConfig.getEndpoint('api');
        api.client.fetch('StaticData/offices')
        .then(response=>response.json())
        .then(response=> 
        {
            this.dataItems=response;
            this.hasItems=true;
        });
     }
    

    }