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

Allow specifying headers per endpoint #190

Closed
efoken opened this issue Feb 5, 2019 · 17 comments
Closed

Allow specifying headers per endpoint #190

efoken opened this issue Feb 5, 2019 · 17 comments
Labels
enhancement💡 feature Feature: new addition or enhancement to existing solutions help wanted 🛠

Comments

@efoken
Copy link

efoken commented Feb 5, 2019

I think it should be possible to specify exclusive headers per endpoint.

For example: I have WordPress REST API and the WooCommerce REST API (v2 & v3) configured as my different endpoints. So it should be possible to provide different Authorization headers for each endpoint.

@fbartho fbartho added enhancement💡 feature Feature: new addition or enhancement to existing solutions labels Feb 5, 2019
@fbartho
Copy link
Collaborator

fbartho commented Feb 5, 2019

Good point! Possibly we should allow our initial headers config to be a Headers/Hash or Function to execute each time? @efoken @paulpdaniels

@paulpdaniels
Copy link
Contributor

It makes sense, tbh I thought that’s how it worked now, but I guess I haven’t dealt with a problem having multiple resources with different header requirements.

@paulpdaniels
Copy link
Contributor

@efoken do you have an example of how you’d like to use it. Just to get a sense of what a typical use case would look like.

@efoken
Copy link
Author

efoken commented Feb 6, 2019

@paulpdaniels this would be an example where I'm using the WP REST API with authorization header as the default endpoint, and the WooCommerce REST APIs v2 and v3 as separate endpoints – v3 requires authorization and v2 does not:

import { camelCase, snakeCase } from 'lodash';
import { RestLink } from 'apollo-link-rest';

if (typeof window === 'undefined') {
  global.btoa = str => Buffer.from(str).toString('base64');
}

const restLink = new RestLink({
  uri: 'https://example.com/wp/v2',
  headers: {
    authorization: `Basic ${btoa('client_key:secret_key')}`,
  },
  endpoints: {
    'wc-v2': {
      uri: 'https://example.com/wp-json/wc/v2',
      headers: {
        authorization: undefined,
      },
    },
    'wc-v3': {
      uri: 'https://example.com/wp-json/wc/v3',
      headers: {
        authorization: `Basic ${btoa('another_client_key:secret')}`,
      },
    },
  },
  fieldNameNormalizer: camelCase,
  fieldNameDenormalizer: snakeCase,
});

@DennisBaekgaard
Copy link

Any news on this? I'm in a situation where I use rest-link, and have my own endpoint which requires authentication and another third-party endpoint which requires a key.

Is there any examples of how to go about this now?

@brad-sobie
Copy link

I also have a situation where I need an authorization header on some endpoints but not on others. I can't find a way to accomplish this.

@Pearce-Ropion
Copy link

Pearce-Ropion commented Oct 15, 2019

Has any work been done on this? I have a situation where one endpoint needs basic auth and another needs bearer auth

@kbudlla
Copy link

kbudlla commented Nov 14, 2019

Has anyone found a solution to this yet ?

@pixelfuture
Copy link

Any workaround for this? I am also in a situation where I need to customize headers for different endpoints.

@bmholmes
Copy link

bmholmes commented Dec 1, 2020

Has there been any update on this? Currently working on a project which requires different auth and headers for each endpoint. Is there a workaround for this?

@fbartho
Copy link
Collaborator

fbartho commented Dec 1, 2020

You can provide a customFetch implementation that analyzes the target URL and injects/appends headers based on a URL prefix, so this is doable today.

Please advise if that workaround doesn't work for you.

@fbartho
Copy link
Collaborator

fbartho commented Jan 5, 2022

Closing since a workaround is provided.

Happy to reopen this if people want to implement it. I'm not personally motivated to do so, as this would probably add complexity to the basic API and codebase.

@fbartho fbartho closed this as completed Jan 5, 2022
@asura-asp
Copy link

@fbartho
Can you please provide an example how to accomplish the analyzes the target URL part?

You can provide a customFetch implementation that analyzes the target URL

Thank you.

@asura-asp
Copy link

asura-asp commented Mar 24, 2022

Related #174

This is inconvenient but seems to work

const restLink = new RestLink({
 uri: "https://awesome-workatound-why-do.com",

 customFetch: (uri, options) => new Promise((resolve, reject) => {
    // console.log("customFetch uri was:", uri);
    const authHeaders = getAuthHeaders(uri);
     
    fetch(uri, { ...options, ...authHeaders })
      .then(response => resolve(response))
      .catch(err => reject(err));
  }),

...

@fbartho
Copy link
Collaborator

fbartho commented Mar 24, 2022

@asura-asp that’s a great implementation. You could alternatively skip the new Promise step, to simplify.

const restLink = new RestLink({
 uri: "https://awesome-workatound-why-do.com",

 customFetch: (uri, options) => {
    // console.log("customFetch uri was:", uri);
    const authHeaders = getAuthHeaders(uri);
     
    return fetch(uri, { ...options, ...authHeaders });
  }),
...

@flippidippi
Copy link

I feel like this workaround isn't friendly enough to close this issue. I def think things like headers and credentials should be set per endpoint and it would make it way more readable.

@fbartho
Copy link
Collaborator

fbartho commented Jul 11, 2022

@flippidippi — as I said before, I’m happy to reopen this if somebody feels motivated to work on a clean PR implementing this functionality w/ docs & tests.

I originally closed it, because I don’t feel the need to feature proposal tickets open if nobody is interested in implementing the feature in a clean fashion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement💡 feature Feature: new addition or enhancement to existing solutions help wanted 🛠
Projects
None yet
Development

No branches or pull requests