-
Notifications
You must be signed in to change notification settings - Fork 122
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
Comments
Good point! Possibly we should allow our initial headers config to be a Headers/Hash or Function to execute each time? @efoken @paulpdaniels |
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. |
@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. |
@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,
}); |
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? |
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. |
Has any work been done on this? I have a situation where one endpoint needs basic auth and another needs bearer auth |
Has anyone found a solution to this yet ? |
Any workaround for this? I am also in a situation where I need to customize headers for different endpoints. |
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? |
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. |
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
Thank you. |
Related #174 This is inconvenient but seems to work
|
@asura-asp that’s a great implementation. You could alternatively skip the 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 });
}),
... |
I feel like this workaround isn't friendly enough to close this issue. I def think things like |
@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. |
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.
The text was updated successfully, but these errors were encountered: