Skip to content

Commit

Permalink
chore: move weapp-fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
sonofmagic committed Sep 13, 2023
1 parent c19925b commit 41edd44
Show file tree
Hide file tree
Showing 11 changed files with 864 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"format": "prettier --write \"**/*.{ts,tsx,md}\"",
"release": "turbo run release",
"publish-packages": "turbo run build lint && npm run test && changeset version && changeset publish",
"sync": "cnpm sync @weapp-core/escape @weapp-core/regex"
"sync": "cnpm sync @weapp-core/escape @weapp-core/regex @weapp-core/http @weapp-core/runtime-injector weapp-websocket weapp-xmlhttprequest"
},
"devDependencies": {
"@changesets/cli": "^2.26.2",
Expand Down
26 changes: 26 additions & 0 deletions packages/fetch/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# weapp-fetch

> 这是一个在小程序中实现 fetch 标准 api 的 npm 包
>
## 为什么需要它?

我想在 `weapp` 环境下,使用现代的 `graphql` 客户端,主要是 `graphql-request``@apollo/client`

然而,它们都依赖于 `fetch` 这个 api,同时因为含义上传文件部分,它们也都需要 `FormData` 这样的对象

这显然在小程序这个环境下是没有的,所以,我就想着 `fork` 一下 `graphql-request` 做一个阉割版本的,在小程序环境里使用,就这样。

## Usage

```js
const { createFetch } = require('weapp-fetch')
// cjs or esm
import { createFetch } from 'weapp-fetch'
// maybe you need @ts-ignore
const weappFetch = createFetch(wx.request)
const uniFetch = createFetch(uni.request)
const taroFetch = createFetch(taro.request)

weappFetch(resource)
weappFetch(resource, options)
```
24 changes: 24 additions & 0 deletions packages/fetch/build.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import path from 'node:path'
import { defineBuildConfig } from 'unbuild'

export default defineBuildConfig({
// entries: ['./src/index', './src/cli'],
rollup: {
// 内联,相当于 nodeResolve
inlineDependencies: true,
// cjs
emitCJS: true,
// 添加 cjs 注入
cjsBridge: true,
dts: {
// https://github.com/unjs/unbuild/issues/135
respectExternal: false
}
},
alias: {
// 别名
'@': path.resolve(__dirname, './src')
},
// dts
declaration: true
})
38 changes: 38 additions & 0 deletions packages/fetch/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"name": "weapp-fetch",
"version": "0.0.0",
"description": "fetch in weapp",
"scripts": {
"build": "unbuild",
"test": "vitest run",
"test:dev": "vitest",
"release": "pnpm publish"
},
"keywords": [
"weapp",
"weapp-fetch",
"fetch"
],
"author": "SonOfMagic <qq1324318532@gmail.com>",
"license": "MIT",
"sideEffects": false,
"type": "commonjs",
"exports": {
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.mjs",
"require": "./dist/index.cjs"
}
},
"main": "./dist/index.cjs",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"files": [
"dist"
],
"publishConfig": {
"access": "public",
"registry": "https://registry.npmjs.org"
},
"dependencies": {}
}
Loading

0 comments on commit 41edd44

Please sign in to comment.