Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply Parameters specified in HttpModule registration to all requests #444

Closed
1 task done
bmeverett opened this issue Jun 9, 2022 · 2 comments
Closed
1 task done

Comments

@bmeverett
Copy link

Is there an existing issue that is already proposing this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe it

I would like parameters applied to all requests. When I setup my HttpModule I want to register with parameters

HttpModule.registerAsync({
      useFactory: () => ({
        params: {
          version: '1.3.5',
        },
      }),
    }),

I would like this parameters to be applied on every request. However, there are other parameters I need to apply specific to a request. For those requests I specify a new parameters object and the ones from the registration get overwritten.

this.httpService.get(`/url`,
      {
        baseURL: this.appConfigService.host,
        params:
          ids: ids.join(','),
        },
        

To get around this I can do spread the default parameters, but that feels like the equivalent of just adding the parameters manually to every request.

this.httpService.get(`/url`,
      {
        baseURL: this.appConfigService.host,
        params:
          ...this.httpService.axiosRef.defaults.params,
          ids: ids.join(','),
        },

Describe the solution you'd like

I would like HttpService to either merge the params object specified during module registration, or have some sort of config option to indicate that params need to be merged.

Teachability, documentation, adoption, migration strategy

No response

What is the motivation / use case for changing the behavior?

Have the ability to have parameters in one place that will apply to all requests in a particular service. This makes it easy so that if a parameter needs to be added for all requests, it can be done in one place instead of updating each request in a service.

Turns this:

this.httpService.get(`/url`,
      {
        baseURL: this.appConfigService.host,
        params:
          ids: ids.join(','),
          version: "1.2.3"
        },
        
this.httpService.get(`/url/123`,
      {
        baseURL: this.appConfigService.host,
        params:
          ids: ids.join(','),
          otherParam: 2,
        },
        

into:

this.httpService.get(`/url`,
      {
        baseURL: this.appConfigService.host,
        params:
          ids: ids.join(','),
        },
        
this.httpService.get(`/url/123`,
      {
        baseURL: this.appConfigService.host,
        params:
          ids: ids.join(','),
          otherParam: 2,
        },
        
@bmeverett
Copy link
Author

bmeverett commented Jun 12, 2022

Not sure if this feature would be best suited for this repo or potentially added to axios itself. Adding here, seems pretty straight forward:

get<T = any>(config: AxiosRequestConfig): Observable<AxiosResponse<T>> {
    return this.makeObservable<T>(this.instance.get, {
      ...this.instance.defaults,
      ...config,
    });
  }

Use the defaults setup and then allow the config to override them. This is one way to do it, but could lead to unintended behavior if that's not what people are expecting. The other approach would be to add a config option, which would either mean adding to AxiosRequestConfg or creating an interface for this project that extends AxiosRequestConfig and adds a mergeDefaultParameters option.

@bmeverett
Copy link
Author

I think this is actually already a feature of axios 😅 https://axios-http.com/docs/instance

The available instance methods are listed below. The specified config will be merged with the instance config.

I think it was my implementation that was at fault.

@nestjs nestjs locked and limited conversation to collaborators Jun 13, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant