@tarojs/plugin-http
Version:
Taro 小程序端支持使用 web 请求 的插件
41 lines (34 loc) • 1.05 kB
text/typescript
import { document, window } from '@tarojs/runtime'
import { Cookie, createCookieInstance } from './Cookie'
import { type XMLHttpRequestEvent, XMLHttpRequest } from './XMLHttpRequest'
declare const ENABLE_COOKIE: boolean
if (process.env.TARO_PLATFORM !== 'web') {
if (ENABLE_COOKIE) {
const _cookie = createCookieInstance()
Object.defineProperties(document, {
URL: {
get () {
if (this.defaultView) return this.defaultView.location.href
return ''
},
},
cookie: {
get () {
return _cookie.getCookie(this.URL)
},
set (value: string) {
if (!value || typeof value !== 'string') return
_cookie.setCookie(value, this.URL)
},
},
/** 获取完整的 cookie,包括 httpOnly 也能获取到 */
$$cookie: {
get () {
return _cookie.getCookie(this.URL, true)
},
}
})
}
window.XMLHttpRequest = XMLHttpRequest
}
export { Cookie, document, XMLHttpRequest, XMLHttpRequestEvent }