Apollo-link-rest: Allow specifying `headers` per endpoint

Created on 5 Feb 2019  ·  11Comments  ·  Source: apollographql/apollo-link-rest


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.

enhancement💡 feature help wanted 🛠

Most helpful comment

@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,
});

All 11 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.

Was this page helpful?
0 / 5 - 0 ratings