troyanskiy / ngx-resource-core

RESTful Core Library
40 stars 10 forks source link

Possibility to set url or params with variables #44

Closed daniloff200 closed 5 years ago

daniloff200 commented 5 years ago

hey @troyanskiy Probably, I missed that in docs, but, is there any way to set url or params with variables?

For example, here's my code:

  constructor(handler: ResourceHandler, public appConfig: Config) {
    super(handler);
  }

  @ResourceAction({
    // IResourceAction
    method: ResourceRequestMethod.Get,
    url: this.appConfig.url' + 'myservice/{!id}',
    params: {id: 'id'}
  })

  @ResourceAction({
    // IResourceAction
    method: ResourceRequestMethod.Get,
    url: this.appConfig.url' + 'myservice2/{!id}/{!userId}',
    params: {id: 'id', userId: this.appConfig.userId}
  })

I can replace this.appConfig.url with some hardcode url, but, this url can be changed in any moment, so, it's not a good idea.

Thanks for any help and suggestions!

troyanskiy commented 5 years ago

Hi @daniloff200

You can use ResourceGlobalConfig.url to set the URL globally

And params is better to specify with the call:

constructor(handler: ResourceHandler, public appConfig: Config) {
    super(handler);
  }

  @ResourceAction({
    // IResourceAction
    method: ResourceRequestMethod.Get,
    path: 'myservice/{!id}'
  })
  req1: IResourceMethod<{id: string}, IRetDataInterface>;

  @ResourceAction({
    // IResourceAction
    method: ResourceRequestMethod.Get,
    path:  '/myservice2/{!id}/{!userId}'
  })
  req2: IResourceMethod<{id: string, userId: string | number}, IRetDataInterface2>;

Then in your code

const data = await this.<the resource>.req1({id: '12312'});
const data2 = await this.<the resource>.req2({id: 'asdasa', userId: 23423});
daniloff200 commented 5 years ago

Thanks, that helped :) Forgot to close that!