box-cat
Version:
对暴露get,post之类接口的实例(例如axios、fly.io)二度封装
17 lines (14 loc) • 3.42 kB
JavaScript
// * Released under the MIT License.
function e(e){var n=function(e,n){return void 0===n&&(n="param"),e.split(new RegExp(n)).slice(0,2)}(e);n[0]=function(e){return["$","(",")","*","+",".","[","]","?","\\","^","{","}","|"].some((function(n){return n===e}))?"\\"+e:e}(n[0]);var t,r,o=(t=n[0],r=n[1],new RegExp("\\/"+t+"([^"+r+"]+)"+r));return n[1]=function(e){return""===e?e:"[^"+e+"]"}(n[1]),{paramsRegExp:o,sliceRegExp:function(e,n){return new RegExp("\\/"+e+n+"[^/]+","g")}(n[0],n[1])}}function n(n){void 0===n&&(n={});var t=function(e){var n={methods:{get:["get"],post:["post"],put:["put"],delete:["delete"],options:["options"],head:["head"],trace:["trace"],connect:["connect"],patch:["patch"]},rule:":param",methodsRule:"startsWith"};if(e.mergeMethods){var t=e.mergeMethods;delete e.mergeMethods,Object.assign(n.methods,t)}return Object.assign({},n,e)}(n),r=e(t.rule);return{processOptions:t,paramsRegExp:r.paramsRegExp,sliceRegExp:r.sliceRegExp}}
/*! *****************************************************************************
Copyright (c) Microsoft Corporation. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
this file except in compliance with the License. You may obtain a copy of the
License at http://www.apache.org/licenses/LICENSE-2.0
THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
MERCHANTABLITY OR NON-INFRINGEMENT.
See the Apache Version 2.0 License for specific language governing permissions
and limitations under the License.
***************************************************************************** */function t(){for(var e=0,n=0,t=arguments.length;n<t;n++)e+=arguments[n].length;var r=Array(e),o=0;for(n=0;n<t;n++)for(var i=arguments[n],c=0,u=i.length;c<u;c++,o++)r[o]=i[c];return r}function r(e,n,t,r,o,i){if(1===e.length)return[e[0],t,Object.assign({},i,r)];var c=void 0;if("object"==typeof t){var u=[],a=void 0,s=function(e){-1!==(a=n.findIndex((function(n){return n===e})))&&(u[a]="/"+t[e])};for(var f in t)s(f);c=u.reduce((function(n,t,r){return""+n+t+e[r+1]}),e[0])}else c=e[0]+"/"+t+e[1];return[c,r,Object.assign({},i,o)]}function o(e,o,i){void 0===i&&(i={});var c,u=n(i),a=u.processOptions,s=u.paramsRegExp,f=u.sliceRegExp;return function(n){var i,u,p;if(i=n,u=a.methodsRule,p=a.methods,function(e,n,t){return!(!n||!t[n])||(t[n]?console.warn("BoxCat:engine\u6ca1\u6709"+n+"\u65b9\u6cd5"):console.warn("BoxCat:\u6ca1\u6709\u5339\u914d\u5230"+e+"\u6240\u9700\u7684\u8bf7\u6c42\u65b9\u5f0f"),!1)}(n,c=Object.keys(p).find((function(e){return!!p[e].find((function(e){return i.toLocaleLowerCase()[u](e.toLocaleLowerCase())}))})),o))return function(e,n,o,i,c,u){var a=n.split(i),s=n.match(i),f=[];return s&&(f=s.map((function(e){return e.replace(c,"$1")}))),function(n,i,c){for(var s=[],p=3;p<arguments.length;p++)s[p-3]=arguments[p];return o[e].apply(o,t(r(a,f,n,i,c,u),s))}}(c,e[n],o,f,s,a.config)}}function i(e,n,t){void 0===t&&(t={});var r=o(e,n,t),i={};return Object.keys(e).forEach((function(e){i[e]=r(e)})),i}function c(e,n,t){return void 0===t&&(t={}),"undefined"!=typeof Proxy?(r=o(e,n,t),new Proxy({},{get:function(e,n,t){return e[n]||(e[n]=r(n)),Reflect.get(e,n,t)}})):i(e,n,t);var r}var u=i;export{u as createAll,i as createApis,c as createProxy};