@bale-wasm/http v0.1.3
wasm-http
use rust develop wasm http by Web API Request。
Webpack5
target: ['web', 'es2020'],
experiments: {
asyncWebAssembly: true
}Usage
It is necessary to determine whether the browser supports wasm:
if (typeof WebAssembly === 'object' && typeof WebAssembly.instantiate === 'function') {
// 浏览器支持WebAssembly
console.log('WebAssembly is supported')
} else {
// 浏览器不支持WebAssembly
console.log('WebAssembly is not supported')
}To use wasm, first import this to your file:
npm install @bale-wasm/httpimport { send } from '@bale-wasm/http/lib/wasm_http'Explanation
Request 文档地址: https://developer.mozilla.org/zh-CN/docs/Web/API/Request
opts 定义了
url、method、data、form、headers等属性。url
string类型, 全路径。method 可选
string类型,POST和GET, 默认为POST。form
FormData类型, 用于文件上传等。headers
object类型, 定义header头。timeout 可选
number类型, 定义超时时间,-1为不超时, 默认为30s。isFormSubmit 可选
bool类型, 是否通过form 表单提交。
request 定义了
cache、credentials、integrity、mode、redirect、referrer、referrer_policy、referrer_policy、signal等。cache 分为:
default、no-store、reload、no-cache、force-cache、only-if-cached, 默认为default。 文档地址: https://developer.mozilla.org/zh-CN/docs/Web/API/Request/cachecredentials 分为:
omit、same-origin、include, 默认为same-origin。 文档地址: https://developer.mozilla.org/zh-CN/docs/Web/API/Request/credentialsintegrity 可选
string类型。 文档地址: https://developer.mozilla.org/en-US/docs/Web/API/Request/integritymode 分为:
same-origin、cors、no-cors,navigate默认为no-cors。 文档地址: https://developer.mozilla.org/zh-CN/docs/Web/API/Request/moderedirect 分为:
follow、error、manual, 默认为follow。 文档地址: https://developer.mozilla.org/en-US/docs/Web/API/Request/redirectreferrer 可选
string类型。 文档地址: https://developer.mozilla.org/en-US/docs/Web/API/Request/referrerreferrerPolicy 分为:
none、no-referrer、no-referrer-when-downgrade、origin、origin-when-cross-origin、unsafe-url、same-origin、strict-origin、strict-origin-when-cross-origin, 默认为strict-origin-when-cross-origin。 文档地址: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Referrer-Policy
Examples
- 普通请求
let opts: { [K: string]: any } = {
url: 'https://api.github.com/repos/rustwasm/wasm-bindgen/branches/master',
method: 'get',
headers: {
Accept: 'application/vnd.github.v3+json'
}
}
let response = await send(opts, null)
console.log(response)FormData请求
let formData = new FormData()
formData.append('file', file) // file 为需要上传的文件
formData.append('version', '1.0')
formData.append('text', '测试')
let updateOpts: any = {
url: 'https://example.com/api/upload/',
method: 'post',
form: formData
}
let response = await send(opts, null)
console.log(response)