Closed marshall007 closed 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
}
}
@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?
I would say you should create your own CRUD
resource class with correct paths and methods.
@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.
Did you try to overwrite $requestInterceptor
?
export class Res extends Resource {
// This is default one
$requestInterceptor(req: Request, methodOptions?: ResourceActionBase): Request {
return req;
}
}
@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.
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.
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: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: