troyanskiy / ngx-resource

Resource (REST) Client for Angular 2
http://troyanskiy.github.io/ngx-resource/
200 stars 46 forks source link

Clarification on `$getUrl` and `$getPath` usage #128

Closed marshall007 closed 7 years ago

marshall007 commented 7 years ago

There are a few cases where I need to manually build URLs for resource methods, but I'm having trouble using the built in $getUrl and $getPath methods:

@ResourceParams({
  url: '/foos',
})
export class FooResource extends ResourceCRUD<{}, IFoo, IFoo> { }

// ...

resource.$getUrl({
  params: { id: '123', type: 'bar' },
  method: RequestMethod.Get,
  path: '/{!id}/{!type}'
})

// expected: '/foos/123/bar'
// actual: '/foos'

As an aside, it would be nice if we had the ability to build URLs for each resource action instead of manually passing in the relevant configuration to $getUrl/$getPath. For example:

resource.get.$getUrl({ id: '123' }) // -> '/foos/123'
resource.query.$getUrl({ type: 'bar' }) // -> '/foos?type=bar'
troyanskiy commented 7 years ago

Hi! $getUrl and $getPath are called for every request by the ResourceAction with methodOptions: ResourceActionBase. It can return a string or Promise which will be resolved as string.

Here is an example how you can use $getUrl and $getPath with separate actions.

@ResourceParams()
export class FooResource extends Resource {

  @ResourceAction({
    name: 'method1' // this is custom (users) param to use it in the $getUrl
  })
  someMethod1: ResourceMethod<any, any>;

  @ResourceAction({
    name: 'method2' // this is custom (users) param to use it in the $getUrl
  })
  someMethod2: ResourceMethod<any, any>;

  $getUrl(methodOptions: ResourceActionBase): string | Promise<string> {
    // here is custom user's param
    switch (methodOptions.name) {
      case 'method1':
        // put some logic here and return string or Promise<string>
        break;
      case 'method2':
        // put some logic here and return string or Promise<string>
        break;
    }
  }

  $getPath(methodOptions: ResourceActionBase): string | Promise<string> {
    // kind of same logic as $getUrl
  }

}
marshall007 commented 7 years ago

@troyanskiy oh, I see. I'm not looking to override the URL/path building behavior. I'm just looking for utility functions that allow me to pass in some params and get back the same URL a ResourceAction would use given those same params. Are such methods available?

troyanskiy commented 7 years ago

I would say you should create your own CRUD resource class with correct paths and methods.

marshall007 commented 7 years ago

@troyanskiy it would be nice if there were a way to get the raw Request object for a resource action without actually initiating the request. That would solve my use case because request.url will contain the full URL plus query params.

I can see how that would be difficult to implement given the current implementation of Resource.$_mainRequest, though.

troyanskiy commented 7 years ago

Did you try to overwrite $requestInterceptor?

export class Res extends Resource {

  // This is default one
  $requestInterceptor(req: Request, methodOptions?: ResourceActionBase): Request {
    return req;
  }

}
marshall007 commented 7 years ago

@troyanskiy to clarify, I'm not trying to alter the URL. I was just looking for utility methods that allow me to build URLs manually without sending any requests. I was initially incorrect in assuming that's what $getUrl and $getPath were for. Sorry if that wasn't clear.

troyanskiy commented 7 years ago

Hello all.

I've released some kind of beta of new library which is called rest-core + rest-ngx. Please check them rest-core and rest-ngx.

It works the way like ngx-resource but has a lot of breaking changes. Something was removed or simplified.

I've migrated all my projects to the new library. If you wish to switch to HttpClient or use some other http handlers like fetch try to migrate your projects to the lib.

Bugs and help requests are welcome in corresponding repos.

Thanks!

PS: Since ngx-resource is no longer supported by me, closing the issue.