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

custom axios api override example has a typo #320

Open
mattrodak-fpwd opened this issue Jul 27, 2023 · 2 comments
Open

custom axios api override example has a typo #320

mattrodak-fpwd opened this issue Jul 27, 2023 · 2 comments
Labels
documentation Improvements or additions to documentation

Comments

@mattrodak-fpwd
Copy link

mattrodak-fpwd commented Jul 27, 2023

Hey there, many thanks for this wonderful library! I'm trying to use axios for the interceptors and the example code provided has an issue with the headers property.

import { contract } from './some-contract';
import axios, { Method, AxiosError, AxiosResponse, isAxiosError } from 'axios';

const client = initClient(contract, {
  baseUrl: 'http://localhost:3333/api',
  baseHeaders: {
    'Content-Type': 'application/json',
  },
  api: async ({ path, method, headers, body }) => {
    const baseUrl = 'http://localhost:3333/api'; //baseUrl is not available as a param, yet
    try {
      const result = await axios.request({
        method: method as Method,
        url: `${this.baseUrl}/${path}`,
        headers,
        data: body,
      });
      return { status: result.status, body: result.data, headers: response.headers };
    } catch (e: Error | AxiosError | any) {
      if (isAxiosError(e)) {
        const error = e as AxiosError;
        const response = error.response as AxiosResponse;
        return { status: response.status, body: response.data, headers: response.headers };
      }
      throw e;
    }
  },
});

In the try block return object headers property uses nonexistent response.headers variable. Changing it to result.headers makes the whole block incompatible with api signature because axios headers are incompatible with the ones from the ApiFetcherArgs type.

@oliverbutler
Copy link
Collaborator

oliverbutler commented Jul 27, 2023

I just looked how I did this on a project

 try {
      const result = await axios.request({
        method: method as Method,
        url: newPath,
        headers: {
          ...headers,
          ...extraHeaders,
        },
        data: body,
      });

      return {
        status: result.status,
        body: result.data,
        // @ts-expect-error toJSON is not typed
        headers: new Headers(result.headers.toJSON()),
      };
    } catch (e: Error | AxiosError | unknown) {
      if (isAxiosError(e)) {
        const error = e as AxiosError;
        const response = error.response as AxiosResponse;
        return {
          status: response.status,
          body: response.data,
          // @ts-expect-error toJSON is not typed
          headers: new Headers(response?.headers?.toJSON() || ''),
        };
      }
      throw e;
    }
    ```

looks like we needed to use `new Headers(response?.headers?.toJSON() || '')` which isn't great.

@oliverbutler oliverbutler added the documentation Improvements or additions to documentation label Jul 27, 2023
@goldo
Copy link

goldo commented Dec 18, 2023

also interested by a cleaner fix here 👀

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

No branches or pull requests

3 participants