Giter Club home page Giter Club logo

axios-fetch-adapter's Introduction

Frontend Developer
  • Languages: JavaScript (ES2021), NodeJS, Python, Typescript, CSS, Sass, HTML
  • Frameworks: React.js/Redux/Redux-Saga/React-Router, VueJS, React-Spring, Django, Marterial UI, Tailwind CSS
  • Skills: Responsive Web Design, Site & Image Optimization, Web
  • Web Technologies: Node.js, RxJS, Express, REST, GraphQL, GitHub Actions
  • Tools: Git, Vim, Webpack, Rollup, ESLint, Webpack, Jest

You can also reach me on

My (incomplete) GitHub stats

axios-fetch-adapter's People

Contributors

chriscalo avatar evanb2 avatar evertt avatar mehimanshupatil avatar palmithor avatar vespaiach avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

axios-fetch-adapter's Issues

Error attempting to read response when empty.

If responseType is set, but the response is empty, this library throws an error.

SyntaxError: Unexpected end of JSON input
    at getResponse (index.js:72:1)
    at async fetchAdapter (index.js:29:1)

Incompatible with Service Workers and `FormData`

I recently came across an issue that is somewhat related to #16 where I was trying to upload a file to an AWS S3 bucket with a Presigned POST URL from inside a Manifest V3 Chrome Extension which requires the use of Service Workers.

axios.defaults.adapter = fetchAdapter;

const formData = new FormData();
Object.entries(fields).forEach(([key, value]) => {
  formData.append(key, value);
});
formData.append('file', file);
await axios.post(s3Url, formData);

Unfortunately this was erroring due to the Content-Type header being set to application/www-x-form-urlencoded instead of multipart/form-data and the WebKit boundary injected by the browser.

Looking into the code it seems the issue is caused by the use of isStandardBrowserEnv() inside the check which should automatically remove the Content-Type header, allowing the browser to fill in the correct one. This helper method is expecting window and document to be defined which is not possible in a Service Worker environment.

if (method !== 'GET' && method !== 'HEAD') {
    options.body = config.data;

    // In these cases the browser will automatically set the correct Content-Type,
    // but only if that header hasn't been set yet. So that's why we're deleting it.
    if (isFormData(options.body) && isStandardBrowserEnv()) {
        headers.delete('Content-Type');
    }
}

@vespaiach Removing the isStandardBrowserEnv() from the if statement solves my problem and allows the file upload to succeed, but I am unsure if it will have any knock on effect to other use cases of this package?

Handling of empty response body for error response codes

It is seen here, that we evaluate response.data identically for 'success' & error response codes.

In case of 404 error and when await response.text() === '', reading await response.json() throws an error

SyntaxError: Unexpected end of JSON input

The default axios XHR adapter behaves differently & won't throw in this case. With this adapter I have to look into caught error message text at this point.

corrections

Since, this adapter relies on fetch API so it won't work in Node environment

not true anymore.

fetch is built into all other env now, including deno, bun, nodejs,

Error on CloudFlare Workers

Error: The 'credentials' field on 'RequestInitializerDict' is not implemented.
    at createRequest (VM4 worker.js:28479)
    at fetchAdapter (VM4 worker.js:28354)
    at dispatchRequest (VM4 worker.js:27722)
    at async setup (VM4 worker.js:71161)

options.credentials = config.withCredentials ? 'include' : 'omit';
This has to be undefined in a CF worker as they don't implement options.credentials for some reason.

Headers are gone using this adapter with axios

Hi,

I'm using this adapter with axios in order to make a browser extension work with manifest v3.
However, after that change the headers are empty.

Here's the code:

import fetchAdapter from '@vespaiach/axios-fetch-adapter';
const axiosInstance = Axios.create({
            adapter: fetchAdapter,
            baseURL: `https://${hostname}`,
            withCredentials: true
        });

Then if I check the response the headers are empty. Can you help?

Project still active?

Is this repo still maintained/usable?

I tried using this with Quasar V2 to make my Axios calls compatible with service workers, but am having difficulty. All the rest of my frontend data is being saved, but not the Axios GET or POST requests. Any assistance would be much appreciated.

form-data content-type header changes and makes the payload invalid

When I use a normal axios request with a FormData payload, I get a header like this:

content-type: multipart/form-data; boundary=----WebKitFormBoundaryAtMBWBnzlZT7VwNE`

And the payload looks like this:

------WebKitFormBoundaryAtMBWBnzlZT7VwNE
Content-Disposition: form-data; name="client_id"

stuff
------WebKitFormBoundaryAtMBWBnzlZT7VwNE
Content-Disposition: form-data; name="grant_type"

password
------WebKitFormBoundaryAtMBWBnzlZT7VwNE
Content-Disposition: form-data; name="username"

manufacturer-moderator
------WebKitFormBoundaryAtMBWBnzlZT7VwNE
Content-Disposition: form-data; name="password"

blablatestpass
------WebKitFormBoundaryAtMBWBnzlZT7VwNE--

but when I use this adapter, the header changes to:

content-type: application/x-www-form-urlencoded

And because now the boundary is missing, the payload has become invalid.

Update to axios v1

Hey,
since Axios still does not support this by itself I would be willing to update to the latest in here to at least have latest version of Axios. Would you be willing to give me a CR and merge it if I will make a PR for that?

Support for axios 1.6.0 with impors from "./unsafe/*"

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch @vespaiach/[email protected] for the project I'm working on.

I needed to support axios version 1.6.0

Here is the diff that solved my problem:

diff --git a/node_modules/@vespaiach/axios-fetch-adapter/index.js b/node_modules/@vespaiach/axios-fetch-adapter/index.js
index 527e282..649926f 100644
--- a/node_modules/@vespaiach/axios-fetch-adapter/index.js
+++ b/node_modules/@vespaiach/axios-fetch-adapter/index.js
@@ -1,8 +1,9 @@
 import axios from 'axios';
-import settle from 'axios/lib/core/settle';
-import buildURL from 'axios/lib/helpers/buildURL';
-import buildFullPath from 'axios/lib/core/buildFullPath';
-import { isUndefined, isStandardBrowserEnv, isFormData } from 'axios/lib/utils';
+import settle from 'axios/unsafe/core/settle';
+import buildURL from 'axios/unsafe/helpers/buildURL';
+import buildFullPath from 'axios/unsafe/core/buildFullPath';
+import * as browserUtils from 'axios/unsafe/platform/browser';
+import * as utils from 'axios/unsafe/utils';
 
 /**
  * - Create a request object
@@ -106,7 +107,7 @@ function createRequest(config) {
 
         // In these cases the browser will automatically set the correct Content-Type,
         // but only if that header hasn't been set yet. So that's why we're deleting it.
-        if (isFormData(options.body) && isStandardBrowserEnv()) {
+        if (utils.default.isFormData(options.body) && browserUtils.default.isStandardBrowserEnv()) {
             headers.delete('Content-Type');
         }
     }
@@ -127,7 +128,7 @@ function createRequest(config) {
     }
     // This config is similar to XHR’s withCredentials flag, but with three available values instead of two.
     // So if withCredentials is not set, default value 'same-origin' will be used
-    if (!isUndefined(config.withCredentials)) {
+    if (!utils.default.isUndefined(config.withCredentials)) {
         options.credentials = config.withCredentials ? 'include' : 'omit';
     }
 

This issue body was partially generated by patch-package.

Error: unable to resolve dependency tree

$ npm i axios
$ npm i @vespaiach/axios-fetch-adapter

npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR! 
npm ERR! While resolving: [email protected]
npm ERR! Found: [email protected]
npm ERR! node_modules/axios
npm ERR!   axios@"^0.26.1" from the root project
npm ERR! 
npm ERR! Could not resolve dependency:
npm ERR! peer axios@"^0.21.1" from @vespaiach/[email protected]
npm ERR! node_modules/@vespaiach/axios-fetch-adapter
npm ERR!   @vespaiach/axios-fetch-adapter@"*" from the root project
npm ERR! 
npm ERR! Fix the upstream dependency conflict, or retry
npm ERR! this command with --force, or --legacy-peer-deps
npm ERR! to accept an incorrect (and potentially broken) dependency resolution.

Is this an issue on my end or does this package need to update its axios dependency?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.