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

How can I set beforepath in delete? #147

Closed a-radys closed 5 years ago

a-radys commented 5 years ago

Hello, How can I set beforepath when deleting a record? I tried to do it like in all method: this.setupService.delete(id, { beforepath: 'setup' }) but the path is not changed so it doesn't work.

maxi7587 commented 5 years ago

Hi @aradys , are you still experiencing this issue? I Have just checked the code and the beforepath should be correctly set...

This is Service class delete method:

public delete(id: string, params?: Object): Observable<void> {
    params = { ...{}, ...Base.ParamsResource, ...params };

    // http request
    let path = new PathBuilder();
    path.applyParams(this, params);
    path.appendPath(id);
    ...

And this in path-builder.ts:

public applyParams(service: Service, params: IParamsResource | IParamsCollection = {}) {
    this.appendPath(service.getPrePath());
    if (params.beforepath) {
        this.appendPath(params.beforepath);
    }
    this.appendPath(service.getPath());
    if (params.include) {
        this.setInclude(params.include);
    }
}
maxi7587 commented 5 years ago

I have just tested it and it's working fine... if the problem persists, maybe you can send a plunkr to test.

a-radys commented 5 years ago

It's still not working. I found out that my beforepath is set in params like this:

params: {
  beforepath: ""
  id: ""
  include: []
  params: {beforepath: "setup"}
  ttl: null
}

so it fails in PathBuilder.prototype.applyParams in line 157:

        if (params.beforepath) {
            this.appendPath(params.beforepath);
        }
a-radys commented 5 years ago

Is it possible to set default beforepath in the service for all operations? Not to set it each time for each delete. Maybe that would solve the issue.

pablorsk commented 5 years ago

Yes @aradys, you can set a method on your service:

export class AuthorsService extends Service<Author> {
    public resource = Author;
    public type = 'authors';

    public getPrePath() {
        return 'foo_path/bar_path';
    }
}

Also, remember you have global cofiguration:

    NgxJsonapiModule.forRoot({
        url: '//api.yourserver.com/v1/foo_path'
    }),