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

Include withCredentials config option to enable credentials in cross… #489

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

theigwe
Copy link

@theigwe theigwe commented Oct 21, 2021

In reference to Issue 487,
this is to enable withCredentials config option available in axios for cross-site Access-Control with credentials in requests.

I used this in my project by creating custom driver to enable cookie authentication.

…site Access-Control requests for graphql driver
Copy link
Owner

@klis87 klis87 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you very much for this PR, it will make graphql driver much more flexible!

@@ -2,6 +2,7 @@ import { Driver, Subscriber } from '@redux-requests/core';

interface GraphqlDriverConfig {
url: string;
withCredentials?: boolean;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because you added ...rest in the actual implementation, could we also support here more axios options? Maybe axios has some ready to use interface so it would not be too time-consuming?

const axiosInstance = axios.create({
baseURL: url,
...rest
});

return (requestConfig, requestAction, driverActions) => {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we also here support more axios attrs (like requestConfig.headers for instance)? Then it would be possible to add things like withCredentials also per request, not only globally. I think the most safe for now would be just to support requestConfig.axiosOptions and spread it like that:

    const responsePromise = axiosInstance({
      cancelToken: abortSource.token,
      method: 'post',
      data,
      headers: requestConfig.headers,
      onDownloadProgress:
        driverActions.setDownloadProgress &&
        (progressEvent => {
          if (progressEvent.lengthComputable) {
            driverActions.setDownloadProgress(calculateProgress(progressEvent));
          }
        }),
      onUploadProgress:
        driverActions.setUploadProgress &&
        (progressEvent => {
          if (progressEvent.lengthComputable) {
            driverActions.setUploadProgress(calculateProgress(progressEvent));
          }
        }),
     ...requestConfig.axiosOptions,
    })

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will update as soon as possible. I actually implemented more options in my custom driver.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at the axios API AxiosRequestConfig interface, I doubt if throwing all the options might be great. I considered doing this for the GraphqlDriverConfig interface:

/*
url,
transformRequest,
transformResponse,
headers,
timeout,
withCredentials,
adapter,
auth,
xsrfCookieName,
xsrfHeaderName,
maxContentLength,
validateStatus,
maxRedirects,
socketPath,
httpAgent,
httpsAgent,
proxy,
*/
type GraphqlDriverConfig = Omit<
  AxiosRequestConfig,
  | 'baseURL'
  | 'method'
  | 'cancelToken'
  | 'onDownloadProgress'
  | 'onUploadProgress'
  | 'data'
  | 'params'
  | 'responseType'
  | 'paramsSerializer'
> & {
  url: string;
};

Then any of the options can still be configured per request with axiosOptions

Copy link
Owner

@klis87 klis87 Nov 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your research and apologies for late reply! Could we please use https://www.typescriptlang.org/docs/handbook/utility-types.html#picktype-keys instead of omit? So we will be strict and new axios types won't be added without our knowledge.

Rgarding attrs I would actually support are:

  • headers
  • timeout
  • withCredentials
  • auth
  • xsrfCookieName
  • xsrfHeaderName
  • proxy

I think only those would be handy for graphql driver, what do you think?

Then any of the options can still be configured per request with axiosOptions

That would be excellent!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants