Comparing version 0.1.7 to 0.2.0-beta
/// <reference types="types" /> | ||
/// <reference types="types" /> | ||
/** | ||
@@ -32,1 +33,32 @@ * origin request options type | ||
}; | ||
declare type WeioResponseDefaultData = string | ArrayBuffer | WechatMiniprogram.IAnyObject; | ||
/** | ||
* Weio response | ||
* | ||
* @author yoneyy (y.tianyuan) | ||
*/ | ||
export declare type WeioResponse<T = WeioResponseDefaultData> = { | ||
cookies: string[]; | ||
data: T; | ||
header: Record<string, any>; | ||
profile: WechatMiniprogram.RequestProfile; | ||
statusCode: number; | ||
errMsg: string; | ||
}; | ||
/**************************************************************************/ | ||
export declare type WeioRejectedHandle = (err: any) => any; | ||
export declare type WeioFulfilledHandle<T = any> = (val: T) => T | Promise<T> | void; | ||
/** | ||
* weio interceptor handles | ||
* | ||
* @author yoneyy (y.tianyuan) | ||
*/ | ||
export declare type WeioInterceptorHandles<T = any> = { | ||
fulfilled: WeioFulfilledHandle<T> | ((val: WeioRequestComposeCustomOptions) => Promise<WeioResponse>); | ||
rejected?: WeioRejectedHandle; | ||
}; | ||
export declare type WeioInterceptorManager<T> = { | ||
use(fulfilled: WeioFulfilledHandle<T>, rejected?: WeioRejectedHandle): number; | ||
eject(id: number): boolean; | ||
}; | ||
export {}; |
@@ -1,2 +0,3 @@ | ||
import { WeioRequestOptions, WeioInstanceRequestOptions, WeioRequestComposeCustomOptions, WeioRequestOmitDataAndURLOptions, WeioRequestComposeCustomOmitDataAndURLOptions } from './typings'; | ||
/// <reference types="types" /> | ||
import { WeioResponse, WeioRequestOptions, WeioInterceptorManager, WeioInstanceRequestOptions, WeioRequestComposeCustomOptions, WeioRequestOmitDataAndURLOptions, WeioRequestComposeCustomOmitDataAndURLOptions } from './typings'; | ||
declare class Weio { | ||
@@ -9,4 +10,16 @@ /** | ||
private readonly baseOptions; | ||
interceptors: { | ||
request: WeioInterceptorManager<WeioRequestComposeCustomOptions>; | ||
response: WeioInterceptorManager<WeioResponse<string | ArrayBuffer | WechatMiniprogram.IAnyObject>>; | ||
}; | ||
constructor(options?: WeioInstanceRequestOptions); | ||
/** | ||
* core request method | ||
* | ||
* @param option request option | ||
* @returns | ||
* @author yoneyy (y.tianyuan) | ||
*/ | ||
private dispatch; | ||
/** | ||
* base request | ||
@@ -19,3 +32,3 @@ * | ||
*/ | ||
request<T>(option: WeioRequestComposeCustomOptions): Promise<T>; | ||
request<R>(option: WeioRequestComposeCustomOptions): Promise<R | void>; | ||
/** | ||
@@ -57,3 +70,3 @@ * Create a new weio instance | ||
params: WeioRequestOptions['data']; | ||
} & WeioRequestOmitDataAndURLOptions): Promise<T>; | ||
} & WeioRequestOmitDataAndURLOptions): Promise<void | WeioResponse<T>>; | ||
/** | ||
@@ -69,3 +82,3 @@ * send a post request | ||
*/ | ||
post<T>(url: WeioRequestOptions['url'], data?: WeioRequestComposeCustomOptions['data'], options?: WeioRequestComposeCustomOmitDataAndURLOptions): Promise<T>; | ||
post<T>(url: WeioRequestOptions['url'], data?: WeioRequestComposeCustomOptions['data'], options?: WeioRequestComposeCustomOmitDataAndURLOptions): Promise<void | WeioResponse<T>>; | ||
/** | ||
@@ -81,3 +94,3 @@ * send a put request | ||
*/ | ||
put<T>(url: WeioRequestOptions['url'], data?: WeioRequestComposeCustomOptions['data'], options?: WeioRequestComposeCustomOmitDataAndURLOptions): Promise<T>; | ||
put<T>(url: WeioRequestOptions['url'], data?: WeioRequestComposeCustomOptions['data'], options?: WeioRequestComposeCustomOmitDataAndURLOptions): Promise<void | WeioResponse<T>>; | ||
/** | ||
@@ -93,3 +106,3 @@ * send a delete request | ||
params: WeioRequestOptions['data']; | ||
} & WeioRequestOmitDataAndURLOptions): Promise<T>; | ||
} & WeioRequestOmitDataAndURLOptions): Promise<void | WeioResponse<T>>; | ||
/** | ||
@@ -106,3 +119,3 @@ * send a options request | ||
params: WeioRequestOptions['data']; | ||
} & WeioRequestOmitDataAndURLOptions): Promise<T>; | ||
} & WeioRequestOmitDataAndURLOptions): Promise<void | WeioResponse<T>>; | ||
/** | ||
@@ -119,4 +132,4 @@ * send a head request | ||
params: WeioRequestOptions['data']; | ||
} & WeioRequestOmitDataAndURLOptions): Promise<T>; | ||
} & WeioRequestOmitDataAndURLOptions): Promise<void | WeioResponse<T>>; | ||
} | ||
export default Weio; |
@@ -1,1 +0,1 @@ | ||
var t=function(){return t=Object.assign||function(t){for(var o,r=1,n=arguments.length;r<n;r++)for(var e in o=arguments[r])Object.prototype.hasOwnProperty.call(o,e)&&(t[e]=o[e]);return t},t.apply(this,arguments)};var o=function(){function o(t){this.baseOptions=null!=t?t:{}}return o.prototype.request=function(o){var r,n,e,a=this;return o.url="".concat(null!==(n=null===(r=this.baseOptions)||void 0===r?void 0:r.baseURL)&&void 0!==n?n:"").concat(null==o?void 0:o.url),!0!==o.qs||"object"!==(e=o.data,Object.prototype.toString.call(e).match(/[A-Z]\w+/)[0].toLocaleLowerCase())||"POST"!==o.method&&"PUT"!==o.method||(o.url+=this.qs(o.data)),new Promise((function(r,n){wx.request(t(t(t({},a.baseOptions),o),{success:function(t){return r(t)},fail:function(t){return n(t)}}))}))},o.create=function(t){return new o(t)},o.prototype.weioFactory=function(t){var o=this;return function(r){return r.method=t,o.request(r)}},o.prototype.qs=function(t){return"?".concat(Object.keys(t).map((function(o){return"".concat(o,"=").concat(t[o])})).join("&"))},o.prototype.get=function(o,r){return this.weioFactory("GET")(t({url:o,data:null==r?void 0:r.params},r))},o.prototype.post=function(o,r,n){return this.weioFactory("POST")(t({url:o,data:r},n))},o.prototype.put=function(o,r,n){return this.weioFactory("PUT")(t({url:o,data:r},n))},o.prototype.delete=function(o,r){return this.weioFactory("DELETE")(t({url:o,data:null==r?void 0:r.params},r))},o.prototype.options=function(o,r){return this.weioFactory("OPTIONS")(t({url:o,data:null==r?void 0:r.params},r))},o.prototype.head=function(o,r){return this.weioFactory("HEAD")(t({url:o,data:null==r?void 0:r.params},r))},o}();export{o as default}; | ||
var t=function(){return t=Object.assign||function(t){for(var r,e=1,o=arguments.length;e<o;e++)for(var n in r=arguments[e])Object.prototype.hasOwnProperty.call(r,n)&&(t[n]=r[n]);return t},t.apply(this,arguments)};var r=function(){function t(){this.interceptors=[]}return t.prototype.use=function(t,r){return this.interceptors.push({fulfilled:t,rejected:r}),this.interceptors.length-1},t.prototype.eject=function(t){return!!this.interceptors[t]&&(this.interceptors[t]=null,!0)},t.prototype.clear=function(){this.interceptors=[]},t.prototype.length=function(){return this.interceptors.filter(Boolean).length},t.prototype.forEach=function(t){this.interceptors.forEach((function(r){null!==r&&t(r)}))},t}(),e=function(){function e(t){this.interceptors={request:new r,response:new r},this.baseOptions=null!=t?t:{}}return e.prototype.dispatch=function(r){var e=this;return"string"!=typeof(null==r?void 0:r.url)?Promise.resolve():new Promise((function(o,n){wx.request(t(t(t({},e.baseOptions),r),{success:function(t){return o(t)},fail:function(t){return n(t)}}))}))},e.prototype.request=function(t){var r,e,o;if(t.url="".concat(null!==(e=null===(r=this.baseOptions)||void 0===r?void 0:r.baseURL)&&void 0!==e?e:"").concat(null==t?void 0:t.url),!0===t.qs&&"object"===(o=t.data,Object.prototype.toString.call(o).match(/[A-Z]\w+/)[0].toLocaleLowerCase())&&("POST"===t.method||"PUT"===t.method)){var n=/\?/g.test(t.url),i=/(.*?)=(.*?)/g.test(t.url);t.url+=this.qs(t.data,n?i?"&":"":"?")}var u,c=[],s=[];this.interceptors.request.forEach((function(t){c.unshift(t)})),this.interceptors.response.forEach((function(t){s.push(t)}));for(var a=t;c.length;){var l=c.shift(),p=l.fulfilled,f=l.rejected;try{a=p(t)}catch(t){null==f||f(t);break}}if(null==a)return Promise.resolve();try{u=this.dispatch.call(this,a)}catch(t){return Promise.reject(t)}for(;s.length;){var h=s.shift();p=h.fulfilled,f=h.rejected;u=u.then(p,f)}return u},e.create=function(t){return new e(t)},e.prototype.weioFactory=function(t){var r=this;return function(e){return e.method=t,r.request(e)}},e.prototype.qs=function(t,r){return void 0===r&&(r=""),"".concat(r).concat(Object.keys(t).map((function(r){return"".concat(r,"=").concat(t[r])})).join("&"))},e.prototype.get=function(r,e){return this.weioFactory("GET")(t({url:r,data:null==e?void 0:e.params},e))},e.prototype.post=function(r,e,o){return this.weioFactory("POST")(t({url:r,data:e},o))},e.prototype.put=function(r,e,o){return this.weioFactory("PUT")(t({url:r,data:e},o))},e.prototype.delete=function(r,e){return this.weioFactory("DELETE")(t({url:r,data:null==e?void 0:e.params},e))},e.prototype.options=function(r,e){return this.weioFactory("OPTIONS")(t({url:r,data:null==e?void 0:e.params},e))},e.prototype.head=function(r,e){return this.weioFactory("HEAD")(t({url:r,data:null==e?void 0:e.params},e))},e}();export{e as default}; |
@@ -1,1 +0,1 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=function(){return t=Object.assign||function(t){for(var o,r=1,e=arguments.length;r<e;r++)for(var n in o=arguments[r])Object.prototype.hasOwnProperty.call(o,n)&&(t[n]=o[n]);return t},t.apply(this,arguments)};var o=function(){function o(t){this.baseOptions=null!=t?t:{}}return o.prototype.request=function(o){var r,e,n,u=this;return o.url="".concat(null!==(e=null===(r=this.baseOptions)||void 0===r?void 0:r.baseURL)&&void 0!==e?e:"").concat(null==o?void 0:o.url),!0!==o.qs||"object"!==(n=o.data,Object.prototype.toString.call(n).match(/[A-Z]\w+/)[0].toLocaleLowerCase())||"POST"!==o.method&&"PUT"!==o.method||(o.url+=this.qs(o.data)),new Promise((function(r,e){wx.request(t(t(t({},u.baseOptions),o),{success:function(t){return r(t)},fail:function(t){return e(t)}}))}))},o.create=function(t){return new o(t)},o.prototype.weioFactory=function(t){var o=this;return function(r){return r.method=t,o.request(r)}},o.prototype.qs=function(t){return"?".concat(Object.keys(t).map((function(o){return"".concat(o,"=").concat(t[o])})).join("&"))},o.prototype.get=function(o,r){return this.weioFactory("GET")(t({url:o,data:null==r?void 0:r.params},r))},o.prototype.post=function(o,r,e){return this.weioFactory("POST")(t({url:o,data:r},e))},o.prototype.put=function(o,r,e){return this.weioFactory("PUT")(t({url:o,data:r},e))},o.prototype.delete=function(o,r){return this.weioFactory("DELETE")(t({url:o,data:null==r?void 0:r.params},r))},o.prototype.options=function(o,r){return this.weioFactory("OPTIONS")(t({url:o,data:null==r?void 0:r.params},r))},o.prototype.head=function(o,r){return this.weioFactory("HEAD")(t({url:o,data:null==r?void 0:r.params},r))},o}();exports.default=o; | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=function(){return t=Object.assign||function(t){for(var e,r=1,o=arguments.length;r<o;r++)for(var n in e=arguments[r])Object.prototype.hasOwnProperty.call(e,n)&&(t[n]=e[n]);return t},t.apply(this,arguments)};var e=function(){function t(){this.interceptors=[]}return t.prototype.use=function(t,e){return this.interceptors.push({fulfilled:t,rejected:e}),this.interceptors.length-1},t.prototype.eject=function(t){return!!this.interceptors[t]&&(this.interceptors[t]=null,!0)},t.prototype.clear=function(){this.interceptors=[]},t.prototype.length=function(){return this.interceptors.filter(Boolean).length},t.prototype.forEach=function(t){this.interceptors.forEach((function(e){null!==e&&t(e)}))},t}(),r=function(){function r(t){this.interceptors={request:new e,response:new e},this.baseOptions=null!=t?t:{}}return r.prototype.dispatch=function(e){var r=this;return"string"!=typeof(null==e?void 0:e.url)?Promise.resolve():new Promise((function(o,n){wx.request(t(t(t({},r.baseOptions),e),{success:function(t){return o(t)},fail:function(t){return n(t)}}))}))},r.prototype.request=function(t){var e,r,o;if(t.url="".concat(null!==(r=null===(e=this.baseOptions)||void 0===e?void 0:e.baseURL)&&void 0!==r?r:"").concat(null==t?void 0:t.url),!0===t.qs&&"object"===(o=t.data,Object.prototype.toString.call(o).match(/[A-Z]\w+/)[0].toLocaleLowerCase())&&("POST"===t.method||"PUT"===t.method)){var n=/\?/g.test(t.url),i=/(.*?)=(.*?)/g.test(t.url);t.url+=this.qs(t.data,n?i?"&":"":"?")}var u,c=[],s=[];this.interceptors.request.forEach((function(t){c.unshift(t)})),this.interceptors.response.forEach((function(t){s.push(t)}));for(var a=t;c.length;){var l=c.shift(),p=l.fulfilled,f=l.rejected;try{a=p(t)}catch(t){null==f||f(t);break}}if(null==a)return Promise.resolve();try{u=this.dispatch.call(this,a)}catch(t){return Promise.reject(t)}for(;s.length;){var h=s.shift();p=h.fulfilled,f=h.rejected;u=u.then(p,f)}return u},r.create=function(t){return new r(t)},r.prototype.weioFactory=function(t){var e=this;return function(r){return r.method=t,e.request(r)}},r.prototype.qs=function(t,e){return void 0===e&&(e=""),"".concat(e).concat(Object.keys(t).map((function(e){return"".concat(e,"=").concat(t[e])})).join("&"))},r.prototype.get=function(e,r){return this.weioFactory("GET")(t({url:e,data:null==r?void 0:r.params},r))},r.prototype.post=function(e,r,o){return this.weioFactory("POST")(t({url:e,data:r},o))},r.prototype.put=function(e,r,o){return this.weioFactory("PUT")(t({url:e,data:r},o))},r.prototype.delete=function(e,r){return this.weioFactory("DELETE")(t({url:e,data:null==r?void 0:r.params},r))},r.prototype.options=function(e,r){return this.weioFactory("OPTIONS")(t({url:e,data:null==r?void 0:r.params},r))},r.prototype.head=function(e,r){return this.weioFactory("HEAD")(t({url:e,data:null==r?void 0:r.params},r))},r}();exports.default=r; |
{ | ||
"name": "weio", | ||
"version": "0.1.7", | ||
"version": "0.2.0-beta", | ||
"description": "Promise based HTTP client for for wechat miniprogram", | ||
@@ -5,0 +5,0 @@ "main": "./lib/weio.js", |
102
README.md
@@ -1,6 +0,16 @@ | ||
# Weio | ||
<div align="center"> | ||
<img src="https://yoneyy.gonghuolianmeng.com/2022-10-10-021650.png" alt="weio" style="zoom:30%;" /> | ||
</div> | ||
Promise based HTTP client for for wechat miniprogram | ||
<div align="center"> | ||
<img src="https://img.shields.io/github/stars/yoneyy/weio?color=yellow" /> | ||
<img src="https://img.shields.io/github/forks/yoneyy/weio" /> | ||
<img src="https://img.shields.io/github/issues/yoneyy/weio" /> | ||
<img src="https://img.shields.io/github/license/yoneyy/weio?logo=MIT" /> | ||
</div> | ||
## How to install it ? | ||
**Promise based HTTP client for for wechat miniprogram** | ||
## How to install `weio` ? | ||
```sh | ||
@@ -14,18 +24,78 @@ # yarn | ||
## How to use it ? | ||
## How to use `weio` ? | ||
安装完之后需在`微信开发者工具`在菜单栏中找到 `工具` --> `构建npm` | ||
After installation, you need to find the build npm option under Tools in the `WeChat Developer Tools` menu bar | ||
<div align="center"> | ||
<img src="/Users/yutianyuan/Library/Application%20Support/typora-user-images/image-20221010105126334.png" alt="image-20221010105126334" style="zoom: 33%;" /> | ||
</div> | ||
```ts | ||
// esm | ||
import weio from 'weio'; | ||
// esm | ||
import weio from 'weio'; | ||
const request = weio.create({ | ||
baseURL: 'https://example.com' | ||
}); | ||
const request = weio.create({ | ||
baseURL: 'https://example.com' | ||
}); | ||
const {data: ret} = await request.get(url [,options]); | ||
const {data: ret} = await request.head(url [,options]); | ||
const {data: ret} = await request.delete(url [,options]); | ||
const {data: ret} = await request.options(url [,options]); | ||
// 请求拦截器 | ||
// request interceptors | ||
request.interceptors.request.use(request => { | ||
return request; | ||
}, error => { | ||
return error; | ||
}); | ||
const {data: ret} = await request.put(url [,data] [,options]); | ||
const {data: ret} = await request.post(url [,data] [,options]); | ||
``` | ||
// 响应拦截器 | ||
// response interceptors | ||
request.interceptors.response.use(response => { | ||
return request; | ||
}, error => { | ||
return error; | ||
}); | ||
const {data: ret} = await request.get(url [,options]); | ||
const {data: ret} = await request.head(url [,options]); | ||
const {data: ret} = await request.delete(url [,options]); | ||
const {data: ret} = await request.options(url [,options]); | ||
const {data: ret} = await request.put(url [,data [,options]]); | ||
const {data: ret} = await request.post(url [,data [,options]]); | ||
``` | ||
```ts | ||
// cjs | ||
const weio = require('weio'); | ||
const request = weio.create({ | ||
baseURL: 'https://example.com' | ||
}); | ||
// 请求拦截器 | ||
// request interceptors | ||
request.interceptors.request.use(request => { | ||
return request; | ||
}, error => { | ||
return error; | ||
}); | ||
// 响应拦截器 | ||
// response interceptors | ||
request.interceptors.response.use(response => { | ||
return request; | ||
}, error => { | ||
return error; | ||
}); | ||
const {data: ret} = await request.get(url [,options]); | ||
const {data: ret} = await request.head(url [,options]); | ||
const {data: ret} = await request.delete(url [,options]); | ||
const {data: ret} = await request.options(url [,options]); | ||
const {data: ret} = await request.put(url [,data [,options]]); | ||
const {data: ret} = await request.post(url [,data [,options]]); | ||
``` | ||
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
19331
13
262
101