🎉 Initial commit

master
KaKi87 2021-10-21 18:31:28 +02:00
commit 4b156135cb
4 changed files with 121 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
.idea
node_modules

81
index.js Normal file
View File

@ -0,0 +1,81 @@
import { http } from '@tauri-apps/api';
import buildURL from 'axios/lib/helpers/buildURL';
import buildFullPath from 'axios/lib/core/buildFullPath';
import {
isFormData,
isArrayBuffer,
isString,
isPlainObject,
isBlob
} from 'axios/lib/utils';
import createError from 'axios/lib/core/createError';
import { transitional } from 'axios/lib/defaults';
import settle from 'axios/lib/core/settle';
import { getReasonPhrase } from 'http-status-codes';
export default config => new Promise((resolve, reject) => {
(async () => {
const timeout = Math.round(config.timeout / 1000);
if(config.timeout !== timeout)
console.warn(`Timeout of ${config.timeout}ms rounded to ${timeout}s`);
try {
const {
status,
headers,
data
} = await http.fetch(
buildURL(buildFullPath(config.baseURL, config.url), config.params, config.paramsSerializer),
{
method: config.method,
headers: {
...config.headers,
...config.auth && { 'Authorization': `Basic ${btoa(`${config.auth.username || ''}:${unescape(encodeURIComponent(config.auth.password || ''))}`)}` }
},
body: {
type: isFormData(config.data)
? 'form'
: isPlainObject(config.data)
? 'json'
: isString(config.data)
? 'text'
: (isArrayBuffer(config.data) || isBlob(config.data))
? 'bytes'
: undefined,
payload: isArrayBuffer(config.data)
? [...new Uint8Array(config.data)]
: isBlob(config.data)
? [...new Uint8Array(await config.data.arrayBuffer())]
: config.data
},
timeout,
responseType: {
'arraybuffer': http.ResponseType.Binary,
'document': http.ResponseType.Text,
'json': http.ResponseType.JSON,
'text': http.ResponseType.Text,
'blob': http.ResponseType.Binary
}[config.responseType]
}
);
settle(resolve, reject, {
data,
status,
statusText: getReasonPhrase(status),
headers,
config
});
}
catch(error){
if(error === 'failed to execute API: Network Error: Io Error: connection timed out'){
throw createError(
`timeout of ${timeout}ms exceeded`,
config,
(config.transitional || transitional).clarifyTimeoutError ? 'ETIMEDOUT' : 'ECONNABORTED'
);
}
else throw error;
}
})().catch(reject);
});

13
package.json Normal file
View File

@ -0,0 +1,13 @@
{
"name": "axios-tauri-adapter",
"version": "0.1.0",
"main": "index.js",
"author": "KaKi87 <KaKi87@pm.me>",
"license": "MIT",
"type": "module",
"dependencies": {
"@tauri-apps/api": "^1.0.0-beta.8",
"axios": "^0.23.0",
"http-status-codes": "^2.1.4"
}
}

25
yarn.lock Normal file
View File

@ -0,0 +1,25 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1
"@tauri-apps/api@^1.0.0-beta.8":
version "1.0.0-beta.8"
resolved "https://registry.yarnpkg.com/@tauri-apps/api/-/api-1.0.0-beta.8.tgz#a43dc4a4515148f29bfb616db85445b771e84f78"
integrity sha512-a56lXB7XvQ4+fKtT0pxpkjTSKhyrQ1Vmjyvt2ox3mT9xw3l7s8IOKHJ1WuqW6TA6xdoy3Cyja3Z3prw8hflS7g==
axios@^0.23.0:
version "0.23.0"
resolved "https://registry.yarnpkg.com/axios/-/axios-0.23.0.tgz#b0fa5d0948a8d1d75e3d5635238b6c4625b05149"
integrity sha512-NmvAE4i0YAv5cKq8zlDoPd1VLKAqX5oLuZKs8xkJa4qi6RGn0uhCYFjWtHHC9EM/MwOwYWOs53W+V0aqEXq1sg==
dependencies:
follow-redirects "^1.14.4"
follow-redirects@^1.14.4:
version "1.14.4"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.14.4.tgz#838fdf48a8bbdd79e52ee51fb1c94e3ed98b9379"
integrity sha512-zwGkiSXC1MUJG/qmeIFH2HBJx9u0V46QGUe3YR1fXG8bXQxq7fLj0RjLZQ5nubr9qNJUZrH+xUcwXEoXNpfS+g==
http-status-codes@^2.1.4:
version "2.1.4"
resolved "https://registry.yarnpkg.com/http-status-codes/-/http-status-codes-2.1.4.tgz#453d99b4bd9424254c4f6a9a3a03715923052798"
integrity sha512-MZVIsLKGVOVE1KEnldppe6Ij+vmemMuApDfjhVSLzyYP+td0bREEYyAoIw9yFePoBXManCuBqmiNP5FqJS5Xkg==