@cloudbase/adapter-wx_mp
Advanced tools
Comparing version 0.4.1 to 1.0.0
@@ -30,4 +30,6 @@ module.exports = { | ||
"no-undefined": 0, | ||
"@typescript-eslint/no-this-alias": 0, | ||
"@typescript-eslint/prefer-optional-chain": 0, | ||
"@typescript-eslint/explicit-member-accessibility": 0 | ||
} | ||
}; |
@@ -1,2 +0,2 @@ | ||
import { AbstractSDKRequest, IRequestOptions, IUploadRequestOptions, StorageInterface, SDKAdapterInterface, IRequestConfig } from '@cloudbase/adapter-interface'; | ||
import { AbstractSDKRequest, IRequestOptions, IUploadRequestOptions, StorageInterface, SDKAdapterInterface, IRequestConfig, IFetchOptions } from "@cloudbase/adapter-interface"; | ||
declare function isMatch(): boolean; | ||
@@ -11,4 +11,3 @@ export declare class WxRequest extends AbstractSDKRequest { | ||
download(options: IRequestOptions): Promise<unknown>; | ||
private _clearTimeout; | ||
private _setTimeout; | ||
fetch(options: IFetchOptions): Promise<unknown>; | ||
} | ||
@@ -15,0 +14,0 @@ export declare const wxMpStorage: StorageInterface; |
@@ -61,3 +61,4 @@ var __extends = (this && this.__extends) || (function () { | ||
}; | ||
import { AbstractSDKRequest, StorageType, formatUrl } from '@cloudbase/adapter-interface'; | ||
import { AbstractSDKRequest, StorageType, formatUrl, } from "@cloudbase/adapter-interface"; | ||
import { ReadableStream } from "web-streams-polyfill"; | ||
function isMatch() { | ||
@@ -67,23 +68,5 @@ if (typeof wx === 'undefined') { | ||
} | ||
if (typeof App === 'undefined') { | ||
return false; | ||
} | ||
if (typeof Page === 'undefined') { | ||
return false; | ||
} | ||
if (typeof getApp !== 'function') { | ||
return false; | ||
} | ||
if (!wx.onAppHide) { | ||
return false; | ||
} | ||
if (!wx.offAppHide) { | ||
return false; | ||
} | ||
if (!wx.onAppShow) { | ||
return false; | ||
} | ||
if (!wx.offAppShow) { | ||
return false; | ||
} | ||
if (!wx.getSystemInfoSync) { | ||
@@ -117,2 +100,5 @@ return false; | ||
} | ||
function isPlugin() { | ||
return typeof App === 'undefined' && typeof getApp === 'undefined' && !wx.onAppHide && !wx.offAppHide && !wx.onAppShow && !wx.offAppShow; | ||
} | ||
var WxRequest = (function (_super) { | ||
@@ -132,19 +118,33 @@ __extends(WxRequest, _super); | ||
return new Promise(function (resolve, reject) { | ||
var timer = null; | ||
var url = options.url, data = options.data, headers = options.headers; | ||
var task = wx.request({ | ||
url: formatUrl('https:', url), | ||
url: formatUrl("https:", url), | ||
data: data, | ||
method: 'POST', | ||
timeout: self._timeout, | ||
method: "POST", | ||
header: headers, | ||
success: function (res) { | ||
self._clearTimeout(timer); | ||
resolve(res); | ||
}, | ||
fail: function (err) { | ||
self._clearTimeout(timer); | ||
reject(err); | ||
} | ||
}, | ||
complete: function (err) { | ||
if (!err || !err.errMsg) { | ||
return; | ||
} | ||
if (!self._timeout || | ||
self._restrictedMethods.indexOf("post") === -1) { | ||
return; | ||
} | ||
var errMsg = err.errMsg; | ||
if (errMsg === "request:fail timeout") { | ||
console.warn(self._timeoutMsg); | ||
try { | ||
task.abort(); | ||
} | ||
catch (e) { } | ||
} | ||
}, | ||
}); | ||
timer = self._setTimeout('post', task); | ||
}); | ||
@@ -156,14 +156,13 @@ }; | ||
return new Promise(function (resolve) { return __awaiter(_this, void 0, void 0, function () { | ||
var timer, url, file, data, headers, task; | ||
var url, file, data, headers, onUploadProgress, task; | ||
return __generator(this, function (_a) { | ||
timer = null; | ||
url = options.url, file = options.file, data = options.data, headers = options.headers; | ||
url = options.url, file = options.file, data = options.data, headers = options.headers, onUploadProgress = options.onUploadProgress; | ||
task = wx.uploadFile({ | ||
url: formatUrl('https:', url), | ||
url: url, | ||
filePath: file, | ||
name: 'file', | ||
formData: __assign(__assign({}, data), { file: file }), | ||
formData: __assign({}, data), | ||
header: headers, | ||
timeout: this._timeout, | ||
success: function (res) { | ||
self._clearTimeout(timer); | ||
var result = { | ||
@@ -179,7 +178,26 @@ statusCode: res.statusCode, | ||
fail: function (err) { | ||
self._clearTimeout(timer); | ||
resolve(err); | ||
}, | ||
complete: function (err) { | ||
if (!err || !err.errMsg) { | ||
return; | ||
} | ||
if (!self._timeout || self._restrictedMethods.indexOf('upload') === -1) { | ||
return; | ||
} | ||
var errMsg = err.errMsg; | ||
if (errMsg === 'request:fail timeout') { | ||
console.warn(self._timeoutMsg); | ||
try { | ||
task.abort(); | ||
} | ||
catch (e) { } | ||
} | ||
} | ||
}); | ||
timer = self._setTimeout('upload', task); | ||
if (onUploadProgress) { | ||
task.onProgressUpdate(function (res) { | ||
onUploadProgress(res); | ||
}); | ||
} | ||
return [2]; | ||
@@ -193,3 +211,2 @@ }); | ||
return new Promise(function (resolve, reject) { | ||
var timer = null; | ||
var url = options.url, headers = options.headers; | ||
@@ -199,4 +216,4 @@ var task = wx.downloadFile({ | ||
header: headers, | ||
timeout: _this._timeout, | ||
success: function (res) { | ||
self._clearTimeout(timer); | ||
if (res.statusCode === 200 && res.tempFilePath) { | ||
@@ -213,26 +230,82 @@ resolve({ | ||
fail: function (err) { | ||
self._clearTimeout(timer); | ||
reject(err); | ||
}, | ||
complete: function (err) { | ||
if (!err || !err.errMsg) { | ||
return; | ||
} | ||
if (!self._timeout || self._restrictedMethods.indexOf('download') === -1) { | ||
return; | ||
} | ||
var errMsg = err.errMsg; | ||
if (errMsg === 'request:fail timeout') { | ||
console.warn(self._timeoutMsg); | ||
try { | ||
task.abort(); | ||
} | ||
catch (e) { } | ||
} | ||
} | ||
}); | ||
timer = _this._setTimeout('download', task); | ||
}); | ||
}; | ||
WxRequest.prototype._clearTimeout = function (timer) { | ||
if (timer) { | ||
clearTimeout(timer); | ||
timer = null; | ||
} | ||
WxRequest.prototype.fetch = function (options) { | ||
var url = options.url, body = options.body, enableAbort = options.enableAbort, headers = options.headers, method = options.method, _a = options.stream, shouldStream = _a === void 0 ? false : _a; | ||
var self = this; | ||
var controller = null; | ||
var stream = new ReadableStream({ | ||
start: function (c) { | ||
controller = c; | ||
}, | ||
cancel: function () { | ||
controller = null; | ||
}, | ||
}); | ||
return new Promise(function (resolve, reject) { | ||
shouldStream && resolve({ data: stream }); | ||
var task = wx.request({ | ||
url: formatUrl("https:", url), | ||
data: body, | ||
timeout: self._timeout, | ||
method: method.toUpperCase(), | ||
header: headers, | ||
success: function (res) { | ||
var _a; | ||
(_a = controller) === null || _a === void 0 ? void 0 : _a.close(); | ||
!shouldStream && resolve(res); | ||
}, | ||
fail: function (err) { | ||
var _a; | ||
(_a = controller) === null || _a === void 0 ? void 0 : _a.close(); | ||
reject(err); | ||
if (shouldStream) { | ||
throw err; | ||
} | ||
}, | ||
complete: function (err) { | ||
if (!err || !err.errMsg) { | ||
return; | ||
} | ||
if (!self._timeout || | ||
self._restrictedMethods.indexOf("post") === -1 || | ||
!enableAbort) { | ||
return; | ||
} | ||
var errMsg = err.errMsg; | ||
if (errMsg === "request:fail timeout") { | ||
console.warn(self._timeoutMsg); | ||
try { | ||
task.abort(); | ||
} | ||
catch (e) { } | ||
} | ||
}, | ||
enableChunked: shouldStream, | ||
}); | ||
task.onChunkReceived(function (buffer) { | ||
var _a; | ||
(_a = controller) === null || _a === void 0 ? void 0 : _a.enqueue(buffer.data); | ||
}); | ||
}); | ||
}; | ||
WxRequest.prototype._setTimeout = function (method, task) { | ||
var _this = this; | ||
if (!this._timeout || this._restrictedMethods.indexOf(method) === -1) { | ||
return null; | ||
} | ||
var timer = setTimeout(function () { | ||
console.warn(_this._timeoutMsg); | ||
task.abort(); | ||
}, this._timeout); | ||
return timer; | ||
}; | ||
return WxRequest; | ||
@@ -299,3 +372,8 @@ }(AbstractSDKRequest)); | ||
var info = wx.getAccountInfoSync(); | ||
return info && info.miniProgram ? info.miniProgram.appId : ''; | ||
if (isPlugin()) { | ||
return info && info.plugin ? info.plugin.appId : ''; | ||
} | ||
else { | ||
return info && info.miniProgram ? info.miniProgram.appId : ''; | ||
} | ||
} | ||
@@ -302,0 +380,0 @@ }; |
{ | ||
"name": "@cloudbase/adapter-wx_mp", | ||
"version": "0.4.1", | ||
"version": "1.0.0", | ||
"description": "wechat miniprogram adapter of cloudbase javascript SDK", | ||
@@ -10,3 +10,4 @@ "main": "dist/index.js", | ||
"build:esm": "tsc -p tsconfig.esm.json", | ||
"build": "webpack && npm run build:esm" | ||
"build": "webpack && npm run build:esm", | ||
"prepublish": "npm run eslint&&npm run build" | ||
}, | ||
@@ -29,4 +30,5 @@ "author": "", | ||
"dependencies": { | ||
"@cloudbase/adapter-interface": "^0.4.0" | ||
"@cloudbase/adapter-interface": "0.5", | ||
"web-streams-polyfill": "^4.0.0" | ||
} | ||
} | ||
} |
@@ -1,5 +0,5 @@ | ||
## @cloudbase/adapter-wx_mp | ||
## cloudbase-adapter-wx_mp | ||
[![NPM Version](https://img.shields.io/npm/v/@cloudbase/adapter-wx_mp.svg?style=flat)](https://www.npmjs.com/package/@cloudbase/adapter-wx_mp) | ||
[![](https://img.shields.io/npm/dt/@cloudbase/adapter-wx_mp.svg)](https://www.npmjs.com/package/@cloudbase/adapter-wx_mp) | ||
[![NPM Version](https://img.shields.io/npm/v/cloudbase-adapter-wx_mp.svg?style=flat)](https://www.npmjs.com/package/cloudbase-adapter-wx_mp) | ||
[![](https://img.shields.io/npm/dt/cloudbase-adapter-wx_mp.svg)](https://www.npmjs.com/package/cloudbase-adapter-wx_mp) | ||
@@ -10,9 +10,13 @@ tcb-js-sdk适配器接口,各平台适配器按照此接口规范进行开发。 | ||
```bash | ||
npm i @cloudbase/adapter-wx_mp -S | ||
npm i cloudbase-adapter-wx_mp -S | ||
``` | ||
### Unpkg(推荐) | ||
可以使用unpkg托管的js文件,地址如下: | ||
https://unpkg.com/cloudbase-adapter-wx_mp/dist/index.js | ||
## 使用 | ||
```javascript | ||
import tcb from 'tsb-js-sdk'; | ||
import adapterForWxMp from '@cloudbase/adapter-wx_mp'; | ||
import adapterForWxMp from 'cloudbase-adapter-wx_mp'; | ||
@@ -19,0 +23,0 @@ // 以下两种方式二选一 |
@@ -17,3 +17,4 @@ const path = require('path'); | ||
libraryTarget: 'umd', | ||
umdNamedDefine: true | ||
umdNamedDefine: true, | ||
globalObject: 'typeof window !== "undefined"?window:this' | ||
}, | ||
@@ -20,0 +21,0 @@ resolve: { |
Sorry, the diff of this file is too big to display
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
89528
584
1
29
2
1
+ Addedweb-streams-polyfill@^4.0.0
+ Added@cloudbase/adapter-interface@0.5.0(transitive)
+ Addedweb-streams-polyfill@4.1.0(transitive)
- Removed@cloudbase/adapter-interface@0.4.1(transitive)