reyesoft / ngx-jsonapi

JSON API client library for Angular 5+ 👌 :: Production Ready 🚀
https://ngx-jsonapi.reyesoft.com/
MIT License
101 stars 52 forks source link

Change api url after app loading #35

Closed ZeevKatz closed 6 years ago

ZeevKatz commented 6 years ago

There is a way to change the api path after loading? I'm trying to load the api path from configuration JSON file for dynamic config options in production and set the url only after the config file loading.

pablorsk commented 6 years ago

hi @zeevkatz,

yeap! you can do with any service. For example:

@Injectable()
export class AuthorsService extends Service<Author> {
    public resource = Author;
    public type = 'authors';
    public schema: ISchema = {
        relationships: {
            photos: {
                hasMany: true
            }
        }
    };

    public getPath(): string {
        return 'my_particular_path_for_this_resource_type';
    }
}
ZeevKatz commented 6 years ago

Maybe I was uncleared, I'd like to change the api host and not a specific service path.

pablorsk commented 6 years ago

Oh! ok....

I'm thinking on a feature... We can atach new method on service for specials URIs, o just a particular service base url... :thinking:

ZeevKatz commented 6 years ago

Created new pull request with solution for this issue. Waiting for your merge...

ZeevKatz commented 6 years ago

Hi @pablorsk, Please let me know if there are issues with my last pull request that related to this issue. Jan 30 was really far from now and this is a really important change, Thanks.

valentasm1 commented 6 years ago

I am not sure if i am not too late for possible solution. But maybe it will help for someone one day. Use case is to have one api url for deployment and other for prod. Also not 100% clear that it gonna work for all but you'll get idea.

Create folder under root "environments" with two files environment.ts

export const environment = {
  production: false,
  someEndpoint : 'http://localhost:55180/',
  anotherEndPoint : "http://localhost:55009/x-files/"
};

environment.prod.ts

export const environment = {
  production: true,
  someEndpoint : 'http://api.superdomain.com/',
  anotherEndPoint : "http://api.superdomain.com/no-files/"
};

And then in you service you could use

private rootUrl = environment.someEndpoint + "x-files-documents/";
public getPath(): string {
    return this.rootUrl;
}

So when you build with flag prod it gonna take variables from prod file. Easy peasy.

pablorsk commented 6 years ago

In Multinexo, @valentasm1, we have a very similar code.