Skip to content

Commit

Permalink
feat: sync axios version
Browse files Browse the repository at this point in the history
  • Loading branch information
KeJunMao committed May 25, 2023
1 parent d8757fd commit 0359f99
Show file tree
Hide file tree
Showing 5 changed files with 331 additions and 540 deletions.
80 changes: 53 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,41 +69,67 @@ instance.request({
```

### 小程序
小程序没有 FormData 和 Blob 对象, 因此你需要额外的库来兼容,并通过自定义一个 vite 插件来兼容

小程序没有 FormData 和 Blob 对象, 需要自定义一个 vite 插件来兼容:

```ts
{
name: "vite-plugin-uni-axios",
transform(code, id) {
if (process.env.UNI_PLATFORM?.includes("mp")) {
if (id.includes("/form-data/lib/browser.js")) {
return {
code: code.replace("window", "globalThis"),
};
}
if (id.includes("/axios/lib/platform/browser/classes/FormData.js")) {
return {
code: `class FormData {};\nexport default FormData;`,
};
}
if (id.includes("/axios/lib/platform/browser/classes/Blob.js")) {
return {
code: `class Blob {};\nexport default Blob;`,
};
}
}
},
},
```

如果你需要 FormData 和 Blob 的话:

```bash
pnpm add miniprogram-formdata miniprogram-blob
```

```ts
// vite.config.ts
export default defineConfig({
plugins: [
{
transform(code, id) {
if (process.env.UNI_PLATFORM?.includes("mp")) {
if (id.includes("/form-data/lib/browser.js")) {
return {
code: code.replace("window", "globalThis"),
};
}
if (id.includes("/axios/lib/platform/browser/classes/FormData.js")) {
return {
code: `import FormData from 'miniprogram-formdata';\nexport default FormData;`,
};
}
if (id.includes("/axios/lib/platform/browser/index.js")) {
return {
code: `import Blob from 'miniprogram-blob'\n${code}`,
};
}
}
},
},
]
})
{
name: "vite-plugin-uni-axios",
transform(code, id) {
if (process.env.UNI_PLATFORM?.includes("mp")) {
if (id.includes("/form-data/lib/browser.js")) {
return {
code: code.replace("window", "globalThis"),
};
}
if (id.includes("/axios/lib/platform/browser/classes/FormData.js")) {
return {
code: `import FormData from 'miniprogram-formdata';\nexport default FormData;`,
};
}
if (id.includes("/axios/lib/platform/browser/classes/Blob.js")) {
return {
code: `import Blob from 'miniprogram-blob';\nexport default Blob;`,
};
}
}
},
},
```

如果你使用的是 vue cli,那么你需要编写一个类似的 webpack 插件

## 客户端类型

提供了 upload 和 download 方法的类型提示,及 AxiosRequestConfig 支持传递 uniapp 特有参数
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@uni-helper/axios-adapter",
"type": "module",
"version": "0.0.7",
"version": "1.4.0",
"license": "MIT",
"packageManager": "[email protected]",
"description": "The Axios adapter for uniapp",
Expand Down
17 changes: 10 additions & 7 deletions playground/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,22 @@
"@dcloudio/uni-mp-weixin": "3.0.0-alpha-3071220230324001",
"@dcloudio/uni-quickapp-webview": "3.0.0-alpha-3071220230324001",
"@uni-helper/axios-adapter": "workspace:*",
"@vueuse/integrations": "^9.12.0",
"axios": "^1.2.6",
"vue": "^3.2.45",
"@vueuse/core": "^9.13.0",
"@vueuse/integrations": "^9.13.0",
"@vueuse/shared": "^9.13.0",
"axios": "^1.4.0",
"vue": "^3.3.4",
"vue-demi": "^0.14.5",
"vue-i18n": "^9.2.2"
},
"devDependencies": {
"@dcloudio/types": "^3.2.8",
"@dcloudio/types": "^3.3.3",
"@dcloudio/uni-automator": "3.0.0-alpha-3071220230324001",
"@dcloudio/uni-cli-shared": "3.0.0-alpha-3071220230324001",
"@dcloudio/uni-stacktracey": "3.0.0-alpha-3071220230324001",
"@dcloudio/vite-plugin-uni": "3.0.0-alpha-3071220230324001",
"typescript": "^4.9.4",
"vite": "^4.0.4",
"vitest": "^0.30.0"
"typescript": "^5.0.4",
"vite": "^4.3.8",
"vitest": "^0.31.1"
}
}
26 changes: 25 additions & 1 deletion playground/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,29 @@ import uni from "@dcloudio/vite-plugin-uni";

// https://vitejs.dev/config/
export default defineConfig({
plugins: [uni()],
plugins: [
uni(),
{
name: "vite-plugin-uni-axios",
transform(code, id) {
if (process.env.UNI_PLATFORM?.includes("mp")) {
if (id.includes("/form-data/lib/browser.js")) {
return {
code: code.replace("window", "globalThis"),
};
}
if (id.includes("/axios/lib/platform/browser/classes/FormData.js")) {
return {
code: `class FormData {};\nexport default FormData;`,
};
}
if (id.includes("/axios/lib/platform/browser/classes/Blob.js")) {
return {
code: `class Blob {};\nexport default Blob;`,
};
}
}
},
},
],
});
Loading

0 comments on commit 0359f99

Please sign in to comment.