From 0006761a5814bc095a101c614d230ba4154f9868 Mon Sep 17 00:00:00 2001 From: Bugzy Date: Fri, 6 Oct 2023 21:37:48 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20BaseContext=20=E6=94=AF=E6=8C=81=20dbJQ?= =?UTF-8?q?L=20=3D=20uniCloud.databaseForJQL(ctx)=E5=8F=AF=E4=BB=A5?= =?UTF-8?q?=E6=96=B9=E4=BE=BF=E5=9C=A8=E4=BA=91=E5=87=BD=E6=95=B0=E4=B8=AD?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=20this.dbJQL?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dist/index.js | 2 +- src/BaseContext.ts | 4 ++++ types/uniCloud.d.ts | 2 ++ 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/dist/index.js b/dist/index.js index b28b9e8..ab4cb21 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1 +1 @@ -"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("fs"),t=require("path"),n=require("events"),r=require("querystring");function i(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var o=i(e),s=i(t);function a(e,t){return e(t={exports:{}},t.exports),t.exports}var c=a((function(e,t){!function(n){const r=Function.prototype.toString;function i(e){if("function"!=typeof e)return!1;if(/^class[\s{]/.test(r.call(e)))return!0;const t=function(e){return r.call(e).replace(/^[^{]*{\s*/,"").replace(/\s*}[^}]*$/,"")}(e);return/classCallCheck\(/.test(t)||/TypeError\("Cannot call a class as a function"\)/.test(t)}e.exports&&(t=e.exports=i),t.isClass=i}()})),u=(c.isClass,a((function(e){const t={};e.exports=function(n="./",...r){const i=new Map,a={};return i.set(a,{path:s.default.isAbsolute(n)?s.default.join(n,"./"):s.default.join(s.default.dirname(e.parent.filename),n,"./"),is_class:!1}),function e(n){return new Proxy(n,{get:(n,s)=>{if(s in n||"symbol"==typeof s||"inspect"==s)return n[s];const a=i.get(n);if(a.is_class)return a.instance||(a.instance=new n(...r)),a.instance[s]?a.instance[s]:"$map"==s?a:a.instance[s];if("$map"==s)return a;let u={};const f=a.path+s+"/",l=a.path+s+".js";if(t[f]||((e=>o.default.existsSync(e)&&o.default.statSync(e).isFile())(l)?t[f]="file":(e=>o.default.existsSync(e)&&o.default.statSync(e).isDirectory())(f)?t[f]="dir":t[f]="none"),"file"==t[f])u=require(l);else if("dir"!=t[f])return;return i.set(u,{path:f,is_class:c(u)}),n[s]=e(u),n[s]},set:(e,t,n)=>{if(t in e)return e[t]=n,!0;const o=i.get(e);return o.is_class?(o.instance||(o.instance=new e(...r)),o.instance[t]=n,!0):(e[t]=n,!0)}})}(a)}})));function f(e,t){void 0===t&&(t={});for(var n=function(e){for(var t=[],n=0;n=48&&c<=57||c>=65&&c<=90||c>=97&&c<=122||95===c))break;s+=e[a++]}if(!s)throw new TypeError("Missing parameter name at "+n);t.push({type:"NAME",index:n,value:s}),n=a}else t.push({type:"CLOSE",index:n,value:e[n++]});else t.push({type:"OPEN",index:n,value:e[n++]});else t.push({type:"ESCAPED_CHAR",index:n++,value:e[n++]});else t.push({type:"MODIFIER",index:n,value:e[n++]})}return t.push({type:"END",index:n,value:""}),t}(e),r=t.prefixes,i=void 0===r?"./":r,o="[^"+l(t.delimiter||"/#?")+"]+?",s=[],a=0,c=0,u="",f=function(e){if(c-1:void 0===b;i||(y+="(?:"+h+"(?="+p+"))?"),C||(y+="(?="+h+"|"+p+")")}return new RegExp(y,d(n))}(f(e,n),t,n)}function h(e,t,n){return e instanceof RegExp?function(e,t){if(!t)return e;var n=e.source.match(/\((?!\?)/g);if(n)for(var r=0;r!1,g=()=>!0;function w(e){if("string"==typeof e){const t=h(e,[],{end:!1});return t.global&&(t.lastIndex=0),e=>t.test(e.event.action)}if(e instanceof RegExp)return t=>(e.global&&(e.lastIndex=0),e.test(t.event.action));if("function"==typeof e)return e;if(Array.isArray(e)){const t=e.map(e=>w(e));return e=>t.some(t=>t(e))}throw new Error("match/ignore pattern must be RegExp, Array or String, but got "+e)}class x{constructor(e){this.ctx=e,this.config=e.config,this.service=e.service,this.controller=e.controller,this.throw=e.throw,this.db=e.db,this.curl=e.curl,this.httpclient=e.httpclient}pick(e,t){return e=e||{},"string"==typeof t&&(t=t.split(/ +/)),t.reduce((function(t,n){return null==e[n]||(t[n]=e[n]),t}),{})}}var E=function(e){if(!Array.isArray(e))throw new TypeError("Middleware stack must be an array!");for(const t of e)if("function"!=typeof t)throw new TypeError("Middleware must be composed of functions!");return function(t,n){let r=-1;return function i(o){if(o<=r)return Promise.reject(new Error("next() called multiple times"));r=o;let s=e[o];o===e.length&&(s=n);if(!s)return Promise.resolve();try{return Promise.resolve(s(t,i.bind(null,o+1)))}catch(e){return Promise.reject(e)}}(0)}};const b=e=>"string"!=typeof e,C="application/json";function A(e,t){if(t){const{headers:t,httpMethod:n,body:i,queryStringParameters:o}=e.event;if(function(e){const t=Object.keys(e).find(e=>"content-type"===e.toLowerCase());t?(e["content-type"]=e[t].toLowerCase(),"content-type"!==t&&delete e[t]):e["content-type"]=C}(t),e.query=o,"GET"===n)e.data=e.query;else if(e.data=Object.create(null),i){const n=t["content-type"];if(n===C)try{e.data=JSON.parse(i)}catch(e){}else"application/x-www-form-urlencoded"===n&&(e.data=r.parse(i))}}e.set=function(t,n){if(2===arguments.length)Array.isArray(n)?n=n.map(e=>"string"==typeof e?e:String(e)):"string"!=typeof n&&(n=String(n)),e.headers[t]=n;else if(b(t))for(const n in t)e.set(n,t[n])}}async function R(e,t){const n=function(e,t){const n=t.env;return!(!n||"http"!==n.MP_SOURCE)||!(!e.httpMethod||!e.headers)}(e.event,e.context);if(A(e,n),n){const n={"content-type":C};try{await t()}catch(t){const r={code:t.code||y,message:t.message};return!0===e.config.debug&&(r.stack=t.stack||""),e.body={mpserverlessComposedResponse:!0,statusCode:400,headers:n,body:JSON.stringify(r)}}const r=e.headers["content-type"]||C;e.body={mpserverlessComposedResponse:!0,isBase64Encoded:!!e.isBase64Encoded,statusCode:e.status,headers:Object.assign(e.headers,{"content-type":r}),body:r===C?JSON.stringify(e.body):e.body}}else await t()}function O(e){const t=async function(t){t.throw(e)};return t._name="error",t}class S extends n.EventEmitter{constructor(e){super(),this.middleware=[],this.config=e||{};const{baseDir:t=process.cwd(),middleware:n}=this.config;this.serviceDir=s.default.resolve(t,"service"),this.controllerDir=s.default.resolve(t,"controller"),this.initMiddleware(n)}use(e,t){if("function"!=typeof e)throw new TypeError("middleware must be a function");return this.middleware.push(this.wrapMiddleware(e,t)),this}async serve(e,t){const n=function(e,t,n,r,i){const o={state:{},event:t,context:n};return o.config=e,o.service=u(r,o),o.controller=u(i,o),o.query=Object.create(null),o.data=t.data||Object.create(null),o.status=200,o.headers=Object.create(null),o.throw=(e,t)=>{if(t)throw{code:e,message:t};throw{code:y,message:e}},m&&(o.db=uniCloud.database(),o.curl=uniCloud.httpclient.request.bind(uniCloud.httpclient),o.httpclient=uniCloud.httpclient),o}(this.config,e||m&&uniCloud.$args,t||m&&uniCloud.$ctx,this.serviceDir,this.controllerDir),r=this.controller(n);let i;return i="error"===r._name?E([R,r]):E(this.middleware.concat(r)),new Promise(e=>{i(n).then(()=>{e(this.respond(n))}).catch(t=>{e(this.failed(t))})})}initMiddleware(e){this.use(R,{name:"http"}),Array.isArray(e)&&e.forEach(([e,t])=>{this.use(e,t)})}wrapMiddleware(e,t){const n=function(e){if(!e)return g;const{enable:t,match:n,ignore:r}=e;if(!1===t)return v;if(!n&&!r)return g;if(n&&r)throw new Error("options.match and options.ignore can not both present");const i=w(n||r);return function(e){const t=i(e);return n?t:!t}}(t),r=(t,r)=>n(t)?e(t,r):r();return t&&t.name&&(r._name=t.name),r._name||(r._name=e._name||e.name),r}controller(e){const t=function(e){!e.action&&e.path&&(e.action=e.path.substr(1));let t=String(e.action||"");return t.startsWith("/")&&(e.action=t=t.substr(1)),t}(e.event);if(!t)return O("action is required");const n=t.split("/").filter(Boolean),r=n.length;if(1===r)return O('action must contain "/"');const i=n[r-1];let o=e.controller;for(let e=0;e{if(s in n||"symbol"==typeof s||"inspect"==s)return n[s];const a=i.get(n);if(a.is_class)return a.instance||(a.instance=new n(...r)),a.instance[s]?a.instance[s]:"$map"==s?a:a.instance[s];if("$map"==s)return a;let u={};const f=a.path+s+"/",l=a.path+s+".js";if(t[f]||((e=>o.default.existsSync(e)&&o.default.statSync(e).isFile())(l)?t[f]="file":(e=>o.default.existsSync(e)&&o.default.statSync(e).isDirectory())(f)?t[f]="dir":t[f]="none"),"file"==t[f])u=require(l);else if("dir"!=t[f])return;return i.set(u,{path:f,is_class:c(u)}),n[s]=e(u),n[s]},set:(e,t,n)=>{if(t in e)return e[t]=n,!0;const o=i.get(e);return o.is_class?(o.instance||(o.instance=new e(...r)),o.instance[t]=n,!0):(e[t]=n,!0)}})}(a)}})));function f(e,t){void 0===t&&(t={});for(var n=function(e){for(var t=[],n=0;n=48&&c<=57||c>=65&&c<=90||c>=97&&c<=122||95===c))break;s+=e[a++]}if(!s)throw new TypeError("Missing parameter name at "+n);t.push({type:"NAME",index:n,value:s}),n=a}else t.push({type:"CLOSE",index:n,value:e[n++]});else t.push({type:"OPEN",index:n,value:e[n++]});else t.push({type:"ESCAPED_CHAR",index:n++,value:e[n++]});else t.push({type:"MODIFIER",index:n,value:e[n++]})}return t.push({type:"END",index:n,value:""}),t}(e),r=t.prefixes,i=void 0===r?"./":r,o="[^"+l(t.delimiter||"/#?")+"]+?",s=[],a=0,c=0,u="",f=function(e){if(c-1:void 0===b;i||(y+="(?:"+h+"(?="+p+"))?"),C||(y+="(?="+h+"|"+p+")")}return new RegExp(y,d(n))}(f(e,n),t,n)}function h(e,t,n){return e instanceof RegExp?function(e,t){if(!t)return e;var n=e.source.match(/\((?!\?)/g);if(n)for(var r=0;r!1,g=()=>!0;function w(e){if("string"==typeof e){const t=h(e,[],{end:!1});return t.global&&(t.lastIndex=0),e=>t.test(e.event.action)}if(e instanceof RegExp)return t=>(e.global&&(e.lastIndex=0),e.test(t.event.action));if("function"==typeof e)return e;if(Array.isArray(e)){const t=e.map(e=>w(e));return e=>t.some(t=>t(e))}throw new Error("match/ignore pattern must be RegExp, Array or String, but got "+e)}class x{constructor(e){this.ctx=e,this.config=e.config,this.service=e.service,this.controller=e.controller,this.throw=e.throw,this.db=e.db,this.dbJQL=e.dbJQL,this.curl=e.curl,this.httpclient=e.httpclient}pick(e,t){return e=e||{},"string"==typeof t&&(t=t.split(/ +/)),t.reduce((function(t,n){return null==e[n]||(t[n]=e[n]),t}),{})}}var E=function(e){if(!Array.isArray(e))throw new TypeError("Middleware stack must be an array!");for(const t of e)if("function"!=typeof t)throw new TypeError("Middleware must be composed of functions!");return function(t,n){let r=-1;return function i(o){if(o<=r)return Promise.reject(new Error("next() called multiple times"));r=o;let s=e[o];o===e.length&&(s=n);if(!s)return Promise.resolve();try{return Promise.resolve(s(t,i.bind(null,o+1)))}catch(e){return Promise.reject(e)}}(0)}};const b=e=>"string"!=typeof e,C="application/json";function A(e,t){if(t){const{headers:t,httpMethod:n,body:i,queryStringParameters:o}=e.event;if(function(e){const t=Object.keys(e).find(e=>"content-type"===e.toLowerCase());t?(e["content-type"]=e[t].toLowerCase(),"content-type"!==t&&delete e[t]):e["content-type"]=C}(t),e.query=o,"GET"===n)e.data=e.query;else if(e.data=Object.create(null),i){const n=t["content-type"];if(n===C)try{e.data=JSON.parse(i)}catch(e){}else"application/x-www-form-urlencoded"===n&&(e.data=r.parse(i))}}e.set=function(t,n){if(2===arguments.length)Array.isArray(n)?n=n.map(e=>"string"==typeof e?e:String(e)):"string"!=typeof n&&(n=String(n)),e.headers[t]=n;else if(b(t))for(const n in t)e.set(n,t[n])}}async function R(e,t){const n=function(e,t){const n=t.env;return!(!n||"http"!==n.MP_SOURCE)||!(!e.httpMethod||!e.headers)}(e.event,e.context);if(A(e,n),n){const n={"content-type":C};try{await t()}catch(t){const r={code:t.code||y,message:t.message};return!0===e.config.debug&&(r.stack=t.stack||""),e.body={mpserverlessComposedResponse:!0,statusCode:400,headers:n,body:JSON.stringify(r)}}const r=e.headers["content-type"]||C;e.body={mpserverlessComposedResponse:!0,isBase64Encoded:!!e.isBase64Encoded,statusCode:e.status,headers:Object.assign(e.headers,{"content-type":r}),body:r===C?JSON.stringify(e.body):e.body}}else await t()}function O(e){const t=async function(t){t.throw(e)};return t._name="error",t}class S extends n.EventEmitter{constructor(e){super(),this.middleware=[],this.config=e||{};const{baseDir:t=process.cwd(),middleware:n}=this.config;this.serviceDir=s.default.resolve(t,"service"),this.controllerDir=s.default.resolve(t,"controller"),this.initMiddleware(n)}use(e,t){if("function"!=typeof e)throw new TypeError("middleware must be a function");return this.middleware.push(this.wrapMiddleware(e,t)),this}async serve(e,t){const n=function(e,t,n,r,i){const o={state:{},event:t,context:n};return o.config=e,o.service=u(r,o),o.controller=u(i,o),o.query=Object.create(null),o.data=t.data||Object.create(null),o.status=200,o.headers=Object.create(null),o.throw=(e,t)=>{if(t)throw{code:e,message:t};throw{code:y,message:e}},m&&(o.db=uniCloud.database(),o.dbJQL=uniCloud.databaseForJQL(o),o.curl=uniCloud.httpclient.request.bind(uniCloud.httpclient),o.httpclient=uniCloud.httpclient),o}(this.config,e||m&&uniCloud.$args,t||m&&uniCloud.$ctx,this.serviceDir,this.controllerDir),r=this.controller(n);let i;return i="error"===r._name?E([R,r]):E(this.middleware.concat(r)),new Promise(e=>{i(n).then(()=>{e(this.respond(n))}).catch(t=>{e(this.failed(t))})})}initMiddleware(e){this.use(R,{name:"http"}),Array.isArray(e)&&e.forEach(([e,t])=>{this.use(e,t)})}wrapMiddleware(e,t){const n=function(e){if(!e)return g;const{enable:t,match:n,ignore:r}=e;if(!1===t)return v;if(!n&&!r)return g;if(n&&r)throw new Error("options.match and options.ignore can not both present");const i=w(n||r);return function(e){const t=i(e);return n?t:!t}}(t),r=(t,r)=>n(t)?e(t,r):r();return t&&t.name&&(r._name=t.name),r._name||(r._name=e._name||e.name),r}controller(e){const t=function(e){!e.action&&e.path&&(e.action=e.path.substr(1));let t=String(e.action||"");return t.startsWith("/")&&(e.action=t=t.substr(1)),t}(e.event);if(!t)return O("action is required");const n=t.split("/").filter(Boolean),r=n.length;if(1===r)return O('action must contain "/"');const i=n[r-1];let o=e.controller;for(let e=0;e service: Record controller: Record @@ -51,6 +53,7 @@ export class BaseContext { this.throw = ctx.throw // uniCloud this.db = ctx.db + this.dbJQL = ctx.dbJQL this.curl = ctx.curl this.httpclient = ctx.httpclient } @@ -100,6 +103,7 @@ export function createContext( // uniCloud if (runInUniCloud) { ctx.db = uniCloud.database() + ctx.dbJQL = uniCloud.databaseForJQL(ctx) ctx.curl = uniCloud.httpclient.request.bind(uniCloud.httpclient) ctx.httpclient = uniCloud.httpclient } diff --git a/types/uniCloud.d.ts b/types/uniCloud.d.ts index d881003..a40ee59 100644 --- a/types/uniCloud.d.ts +++ b/types/uniCloud.d.ts @@ -9,4 +9,6 @@ declare var uniCloud: { $ctx: any httpclient: any database: () => any + // eslint-disable-next-line no-unused-vars + databaseForJQL: (ctx: any) => any } -- Gitee