@tarojs/plugin-http 3.6.0-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2018
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.MD ADDED
@@ -0,0 +1,59 @@
1
+ # @tarojs/plugin-http
2
+
3
+ `Taro` 运行时扩展插件, 扩展了 `web` 开发中网络请求相关的能力,让 `taro` 可以使用 [axios](https://github.com/axios/axios) 等网络请求封装库。
4
+
5
+ > 本插件需搭配 taro 3.6.0 及其以上版本使用
6
+
7
+ ### XMLHttpRequest
8
+
9
+ 在小程序端模仿浏览器的 `XMLHttpRequest` 实现的对象,在浏览器环境中返回浏览器本身的 `XMLHttpRequest`。此对象通过 Webpack 的 [ProvidePlugin](https://webpack.js.org/plugins/provide-plugin/) 注入到全局对象以供第三方库调用。
10
+
11
+ ### document.cookie
12
+
13
+ 在小程序端通过本地存储模仿浏览器的 `document.cookie` 实现的对象,在浏览器环境中返回浏览器本身的 `document.cookie`。
14
+
15
+ ### 其他影响
16
+
17
+ 1.为了能正常使用 [axios](https://github.com/axios/axios) 库, 本插件会通过 Webpack 的 [ProvidePlugin](https://webpack.js.org/plugins/provide-plugin/)功能,在编译小程序时将全局的 `FormData`、 `Blob` 对象替换成 `undefined`,这样使用 `axios` 时不会有 `FormData is not defined` 这类异常,相应的代价是 `axios` 的上传文件的功能也将不可用, 考虑到小程序有自己的独特上传API,这是可以接受的。
18
+
19
+ 如果需要改变上述行为,可以通过传入选项 `disabledFormData` 、 `disabledBlob`:
20
+ ```js
21
+ // config/index.js
22
+ config = {
23
+ // ...
24
+ plugins: [
25
+ ['@tarojs/plugin-http', {
26
+ disabledFormData: false,
27
+ disabledBlob: false
28
+ }]
29
+ ],
30
+ }
31
+ ```
32
+
33
+ 2.[axios](https://github.com/axios/axios) 中使用了`document.createElement("a")` 动态创建 `a` 标签设置 `href` 属性,然后读取 `protocol`、`host` 等属性来判断是否同源跨域;@tarojs/runtime 3.6.0 版本中支持了动态创建 `a` 标签, 并导出了一些公共函数,因此本插件需要配合 `taro 3.6.0及其以上版本` 使用
34
+
35
+ ### 安装
36
+
37
+ ```
38
+ npm i @tarojs/plugin-http
39
+ ```
40
+
41
+ ### 使用
42
+
43
+ ```js
44
+ // config/index.js
45
+ config = {
46
+ // ...
47
+ plugins: ['@tarojs/plugin-http'],
48
+ }
49
+ ```
50
+
51
+ 插件提供了3个参数:
52
+
53
+
54
+ | 参数名 | 默认值 | 说明 |
55
+ | :--- | :--- | :--- |
56
+ | enableCookie | false | (是否)注入相关代码,支持 `document.cookie` 、 通过后端返回 `Set-Cookie` 响应头来设置 `cookie` |
57
+ | disabledFormData | true | (是否)禁用掉 FormData 全局对象 |
58
+ | disabledBlob | true | (是否)禁用掉 Blob 全局对象 |
59
+
@@ -0,0 +1,11 @@
1
+ import { IPluginContext } from '@tarojs/service';
2
+ interface IOptions {
3
+ /** 支持 document.cookie 和 http 设置 cookie (默认false) */
4
+ enableCookie?: boolean;
5
+ /** 禁用掉 FormData 全局对象 (默认true禁用) */
6
+ disabledFormData?: boolean;
7
+ /** 禁用掉 Blob 全局对象 (默认true禁用) */
8
+ disabledBlob?: boolean;
9
+ }
10
+ declare const _default: (ctx: IPluginContext, options: IOptions) => void;
11
+ export { _default as default };
package/dist/index.js ADDED
@@ -0,0 +1,45 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var shared = require('@tarojs/shared');
6
+ var path = require('path');
7
+
8
+ var name = "@tarojs/plugin-http";
9
+
10
+ var index = (ctx, options) => {
11
+ if (!['h5', 'rn'].includes(ctx.runOpts.options.platform)) {
12
+ ctx.modifyWebpackChain(({ chain }) => {
13
+ chain.plugin('definePlugin').tap((args) => {
14
+ var _a;
15
+ args[0].ENABLE_COOKIE = (_a = options.enableCookie) !== null && _a !== void 0 ? _a : false;
16
+ return args;
17
+ });
18
+ const runtimeAlia = `${name}/dist/runtime`;
19
+ chain.resolve.alias.set(runtimeAlia, path.join(__dirname, 'runtime.js'));
20
+ // 注入相关全局BOM对象
21
+ chain.plugin('providerPlugin').tap((args) => {
22
+ var _a, _b;
23
+ var _c, _d;
24
+ args[0].XMLHttpRequest = [runtimeAlia, 'XMLHttpRequest'];
25
+ ((_a = options.disabledFormData) !== null && _a !== void 0 ? _a : true) && ((_c = args[0]).FormData || (_c.FormData = [runtimeAlia, 'FormData']));
26
+ ((_b = options.disabledBlob) !== null && _b !== void 0 ? _b : true) && ((_d = args[0]).Blob || (_d.Blob = [runtimeAlia, 'Blob']));
27
+ return args;
28
+ });
29
+ });
30
+ ctx.registerMethod({
31
+ name: 'onSetupClose',
32
+ fn(platform) {
33
+ const injectedPath = `post:${name}/dist/runtime`;
34
+ if (shared.isArray(platform.runtimePath)) {
35
+ platform.runtimePath.push(injectedPath);
36
+ }
37
+ else if (shared.isString(platform.runtimePath)) {
38
+ platform.runtimePath = [platform.runtimePath, injectedPath];
39
+ }
40
+ },
41
+ });
42
+ }
43
+ };
44
+
45
+ exports.default = index;
@@ -0,0 +1,94 @@
1
+ import { document, Events } from '@tarojs/runtime';
2
+ declare class Cookie {
3
+ #private;
4
+ constructor();
5
+ static parse(cookieStr: string): {
6
+ key: string;
7
+ value: string;
8
+ path: string | null;
9
+ domain: string | null;
10
+ expires: number | null;
11
+ maxAge: number | null;
12
+ secure: boolean;
13
+ httpOnly: boolean;
14
+ } | null;
15
+ /**
16
+ * 判断 domain
17
+ */
18
+ $_checkDomain(host: any, cookieDomain: any): boolean;
19
+ /**
20
+ * 判断 path
21
+ */
22
+ $_checkPath(path: any, cookiePath: any): boolean;
23
+ /**
24
+ * 判断过期
25
+ */
26
+ $_checkExpires(cookie: any): boolean;
27
+ /**
28
+ * 设置 cookie
29
+ */
30
+ setCookie(cookie: any, url: any): void;
31
+ /**
32
+ * 拉取 cookie
33
+ */
34
+ getCookie(url: string, includeHttpOnly?: boolean): string;
35
+ /**
36
+ * 序列化
37
+ */
38
+ serialize(): string;
39
+ /**
40
+ * 反序列化
41
+ */
42
+ deserialize(str: any): void;
43
+ }
44
+ // https://developer.mozilla.org/zh-CN/docs/Web/API/XMLHttpRequest
45
+ declare class XMLHttpRequest extends Events {
46
+ #private;
47
+ static readonly UNSENT = 0;
48
+ static readonly OPENED = 1;
49
+ static readonly HEADERS_RECEIVED = 2;
50
+ static readonly LOADING = 3;
51
+ static readonly DONE = 4;
52
+ // 欺骗一些库让其认为是原生的xhr
53
+ static toString(): string;
54
+ toString(): string;
55
+ // 事件
56
+ /** 当 request 被停止时触发,例如当程序调用 XMLHttpRequest.abort() 时 */
57
+ onabort: (() => void) | null;
58
+ /** 当 request 遭遇错误时触发 */
59
+ onerror: ((err: any) => void) | null;
60
+ /** 接收到响应数据时触发 */
61
+ onloadstart: (() => void) | null;
62
+ /** 请求成功完成时触发 */
63
+ onload: (() => void) | null;
64
+ /** 当请求结束时触发,无论请求成功 ( load) 还是失败 (abort 或 error)。 */
65
+ onloadend: (() => void) | null;
66
+ /** 在预设时间内没有接收到响应时触发 */
67
+ ontimeout: (() => void) | null;
68
+ /** 当 readyState 属性发生变化时,调用的事件处理器 */
69
+ onreadystatechange: (() => void) | null;
70
+ constructor();
71
+ addEventListener(event: string, callback: (arg: any) => void): void;
72
+ removeEventListener(event: string, callback: (arg: any) => void): void;
73
+ /**
74
+ * 对外属性和方法
75
+ */
76
+ get timeout(): number;
77
+ set timeout(timeout: number);
78
+ get status(): number;
79
+ get statusText(): any;
80
+ get readyState(): number;
81
+ get responseType(): string;
82
+ set responseType(value: string);
83
+ get responseText(): null;
84
+ get response(): null;
85
+ get withCredentials(): boolean;
86
+ set withCredentials(value: boolean);
87
+ abort(): void;
88
+ getAllResponseHeaders(): string;
89
+ getResponseHeader(name: any): string | null;
90
+ open(method: any, url: any): void;
91
+ setRequestHeader(header: any, value: any): void;
92
+ send(data: any): void;
93
+ }
94
+ export { Cookie, document, XMLHttpRequest };