Skip to content

Commit

Permalink
refactor: middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
zhennann committed Dec 15, 2023
1 parent 262898f commit 89c1f7d
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 66 deletions.
Original file line number Diff line number Diff line change
@@ -1,31 +1,28 @@
module.exports = ctx => {
const moduleInfo = module.info;
class Middleware {
async execute(options, next) {
// must exists
const scene = options.scene;
const scenes = options.scenes;
if (!scene && !scenes) ctx.throw.module(moduleInfo.relativeName, 1001);
const moduleInfo = module.info;
module.exports = class Middleware {
async execute(options, next) {
// must exists
const scene = options.scene;
const scenes = options.scenes;
if (!scene && !scenes) this.ctx.throw.module(moduleInfo.relativeName, 1001);

// local.disabled
if (ctx.app.meta.isLocal && ctx.config.module(moduleInfo.relativeName).configFront.local.disabled) {
// next
return await next();
}
// local.disabled
if (this.ctx.app.meta.isLocal && this.ctx.config.module(moduleInfo.relativeName).configFront.local.disabled) {
// next
return await next();
}

// scene
if (scene) {
await sceneVerify({ ctx, scene });
} else if (scenes) {
for (const scene of scenes) {
await sceneVerify({ ctx, scene });
}
// scene
if (scene) {
await sceneVerify({ ctx: this.ctx, scene });
} else if (scenes) {
for (const scene of scenes) {
await sceneVerify({ ctx: this.ctx, scene });
}
// next
await next();
}
// next
await next();
}
return Middleware;
};

async function sceneVerify({ ctx, scene }) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
module.exports = ctx => {
class Middleware {
async execute(options, next) {
// check appReady
if (!ctx.innerAccess) {
await ctx.bean.instance.checkAppReady();
}
// next
await next();
module.exports = class Middleware {
async execute(options, next) {
// check appReady
if (!this.ctx.innerAccess) {
await this.ctx.bean.instance.checkAppReady();
}
// next
await next();
}
return Middleware;
};
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
module.exports = ctx => {
// const moduleInfo = module.info;
class Middleware {
async execute(options, next) {
// init instance
await ctx.bean.instance.initInstance();
// next
await next();
}
module.exports = class Middleware {
async execute(options, next) {
// init instance
await this.ctx.bean.instance.initInstance();
// next
await next();
}
return Middleware;
};
Original file line number Diff line number Diff line change
@@ -1,28 +1,25 @@
// request.body
// validate: module(optional), validator, schema(optional)
// data:
module.exports = ctx => {
const moduleInfo = module.info;
class Middleware {
async execute(options, next) {
// must exists
const validator = options.validator;
if (!validator) ctx.throw.module(moduleInfo.relativeName, 1001);
// params
const module = options.module || ctx.module.info.relativeName;
const schema = options.schema || (ctx.meta._validator && ctx.meta._validator.schema);
const data = ctx.request.body[options.data || 'data'];
// if error throw 422
await ctx.bean.validation.validate({
module,
validator,
schema,
data,
filterOptions: true,
});
// next
await next();
}
const moduleInfo = module.info;
module.exports = class Middleware {
async execute(options, next) {
// must exists
const validator = options.validator;
if (!validator) this.ctx.throw.module(moduleInfo.relativeName, 1001);
// params
const module = options.module || this.ctx.module.info.relativeName;
const schema = options.schema || (this.ctx.meta._validator && this.ctx.meta._validator.schema);
const data = this.ctx.request.body[options.data || 'data'];
// if error throw 422
await this.ctx.bean.validation.validate({
module,
validator,
schema,
data,
filterOptions: true,
});
// next
await next();
}
return Middleware;
};

0 comments on commit 89c1f7d

Please sign in to comment.