From 666aa721be857f2a029c35cc03fbb94910367b24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AD=99=E6=B5=A9=E6=9E=97?= Date: Fri, 28 Jun 2024 12:45:11 +0800 Subject: [PATCH] =?UTF-8?q?[Feature]:=20=E5=BE=AE=E6=9C=8D=E5=8A=A1?= =?UTF-8?q?=E6=A8=A1=E5=BC=8F=E4=B8=8Brest=20action=20=E5=8F=AF=E4=BB=A5?= =?UTF-8?q?=E4=BB=8Ectx.meta.requetHeaders=E4=B8=AD=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E8=AF=B7=E6=B1=82=E5=A4=B4=20#6954?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- services/service-api/index.js | 54 ++++++++++++++++++----------------- 1 file changed, 28 insertions(+), 26 deletions(-) diff --git a/services/service-api/index.js b/services/service-api/index.js index 57c7dac2aa..ccbf238c8f 100644 --- a/services/service-api/index.js +++ b/services/service-api/index.js @@ -106,7 +106,7 @@ module.exports = { ServiceObjectGraphql ], - projectStarted: false, + projectStarted: false, // More info about settings: https://moleculer.services/docs/0.14/moleculer-web.html settings: { @@ -120,7 +120,7 @@ module.exports = { // Global Express middlewares. More info: https://moleculer.services/docs/0.14/moleculer-web.html#Middlewares use: [ - function(req, res, next) { + function (req, res, next) { // 如果service-object-grapqhl未结算完成,则提示服务未就绪,刷新重试 if (!this.projectStarted) { const message = 'service is not ready, please refresh later.'; @@ -182,7 +182,7 @@ module.exports = { // Set request headers to context meta ctx.meta.userAgent = req.headers["user-agent"]; ctx.meta.clientIp = requestIp.getClientIp(req); - }, + }, /** * After call hook. You can modify the data. @@ -224,7 +224,7 @@ module.exports = { res.end(JSON.stringify({ error: err.message, detail: err })); } catch (error) { res.writeHead(500); - res.end(JSON.stringify({ error: err.message})); + res.end(JSON.stringify({ error: err.message })); } } }, @@ -308,7 +308,7 @@ module.exports = { let msg = err.message; - if(err.code === 422 && err.type === 'VALIDATION_ERROR'){ + if (err.code === 422 && err.type === 'VALIDATION_ERROR') { msg = err.data.map(item => item.message).join(''); } @@ -356,8 +356,8 @@ module.exports = { getObjectDataLoaderMapKey(objectName, referenceToField = '_id', spaceId) { if (objectName) { const key = `${spaceId}_object:${objectName}.${referenceToField}`; - if(!ObjectDataLoaderMapKeys[objectName]){ - ObjectDataLoaderMapKeys[objectName] = [] ; + if (!ObjectDataLoaderMapKeys[objectName]) { + ObjectDataLoaderMapKeys[objectName] = []; } ObjectDataLoaderMapKeys[objectName].push(key); return key; @@ -369,7 +369,7 @@ module.exports = { async objectDataLoaderHandler(actionName, staticParams, rootParams, graphqlCtx) { const rootKeys = Object.keys(rootParams); - const {root, args, context, resolveInfo} = graphqlCtx; + const { root, args, context, resolveInfo } = graphqlCtx; const userSession = context.ctx.meta.user; const spaceId = userSession.spaceId; const dataLoaderMapKey = this.getObjectDataLoaderMapKey( @@ -437,7 +437,7 @@ module.exports = { * @returns {Promise} */ async authenticate(ctx, route, req, res, alias) { - if(alias.authentication === false){ + if (alias.authentication === false) { return null; } let user = await steedosAuth.auth(req, res); @@ -457,8 +457,10 @@ module.exports = { * @returns {Promise} */ async authorize(ctx, route, req, res, alias) { - if(alias.authorization === false){ - return ; + ctx.meta.requestHeaders = req.headers; + + if (alias.authorization === false) { + return; } // Get the authenticated user. const user = ctx.meta.user; @@ -491,9 +493,9 @@ module.exports = { const rootKeys = Object.keys(rootParams); return async (root, args, context, resolveInfo) => { try { - if(useObjectDataLoader){ - return await this.objectDataLoaderHandler(actionName, staticParams, rootParams, {root, args, context, resolveInfo}); - }else if (useDataLoader) { + if (useObjectDataLoader) { + return await this.objectDataLoaderHandler(actionName, staticParams, rootParams, { root, args, context, resolveInfo }); + } else if (useDataLoader) { const userSession = context.ctx.meta.user; const spaceId = userSession.spaceId; const dataLoaderMapKey = this.getDataLoaderMapKey( @@ -688,7 +690,7 @@ module.exports = { for (const objectName in this.ObjectsUIResolvers) { - if(resolvers[objectName]){ + if (resolvers[objectName]) { resolvers[objectName]['_ui'] = this.ObjectsUIResolvers[objectName]; } } @@ -997,33 +999,33 @@ module.exports = { this.objectDataLoaders = new Map(); this.app = SteedosRouter.staticRouter(); }, - events:{ - '@objectRecordEvent.*.*': function(ctx){ + events: { + '@objectRecordEvent.*.*': function (ctx) { const { objectApiName, isUpdate, isDelete, id, doc } = ctx.params; - if(objectApiName && (isUpdate || isDelete)){ + if (objectApiName && (isUpdate || isDelete)) { const keys = ObjectDataLoaderMapKeys[objectApiName] || []; let dataLoaderKeys = [id]; - if(objectApiName === 'space_users'){ + if (objectApiName === 'space_users') { dataLoaderKeys.push(doc.user) } - _.each(keys, (key)=>{ + _.each(keys, (key) => { const loader = this.objectDataLoaders.get(key); - if(loader){ - for(const dataLoaderKey of dataLoaderKeys){ + if (loader) { + for (const dataLoaderKey of dataLoaderKeys) { loader.clear(dataLoaderKey); } } }) } }, - 'service-ui.started': function(){ + 'service-ui.started': function () { this.app.use("/", this.express()); }, - "$packages.changed": function(){ + "$packages.changed": function () { if (!this.projectStarted) { this.projectStarted = true // 开发环境, 显示耗时 - if(process.env.NODE_ENV == 'development' && global.__startDate){ + if (process.env.NODE_ENV == 'development' && global.__startDate) { console.log('耗时: ' + (new Date().getTime() - global.__startDate.getTime()) + ' ms'); } console.log(`Project is running at ${process.env.ROOT_URL}`); @@ -1037,7 +1039,7 @@ module.exports = { console.error('auto open browser failed.'); } }, 100) - + } } }