@@ -22,6 +22,7 @@
|
|||||||
"@babel/plugin-transform-runtime": "^7.7.6",
|
"@babel/plugin-transform-runtime": "^7.7.6",
|
||||||
"@babel/preset-env": "^7.7.5",
|
"@babel/preset-env": "^7.7.5",
|
||||||
"cross-env": "^5.2.1",
|
"cross-env": "^5.2.1",
|
||||||
|
"jest": "^25.5.2",
|
||||||
"eslint": "^6.8.0"
|
"eslint": "^6.8.0"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -18,6 +18,7 @@
|
|||||||
"@babel/preset-env": "^7.7.5",
|
"@babel/preset-env": "^7.7.5",
|
||||||
"client-sessions": "^0.8.0",
|
"client-sessions": "^0.8.0",
|
||||||
"cross-env": "^5.2.1",
|
"cross-env": "^5.2.1",
|
||||||
|
"jest": "^25.5.2",
|
||||||
"eslint": "^6.8.0"
|
"eslint": "^6.8.0"
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|||||||
9
packages/medusa-interfaces/.eslintrc
Normal file
9
packages/medusa-interfaces/.eslintrc
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
{
|
||||||
|
"plugins": ["prettier"],
|
||||||
|
"extends": ["prettier"],
|
||||||
|
"rules": {
|
||||||
|
"prettier/prettier": "error",
|
||||||
|
"semi": "error",
|
||||||
|
"no-unused-expressions": "true"
|
||||||
|
}
|
||||||
|
}
|
||||||
7
packages/medusa-interfaces/.prettierrc
Normal file
7
packages/medusa-interfaces/.prettierrc
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
{
|
||||||
|
"endOfLine": "lf",
|
||||||
|
"semi": false,
|
||||||
|
"singleQuote": false,
|
||||||
|
"tabWidth": 2,
|
||||||
|
"trailingComma": "es5"
|
||||||
|
}
|
||||||
@@ -11,7 +11,8 @@
|
|||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "babel src --out-dir dist/ --ignore **/__tests__",
|
"build": "babel src --out-dir dist/ --ignore **/__tests__",
|
||||||
"prepare": "cross-env NODE_ENV=production npm run build",
|
"prepare": "cross-env NODE_ENV=production npm run build",
|
||||||
"watch": "babel -w src --out-dir dist/ --ignore **/__tests__"
|
"watch": "babel -w src --out-dir dist/ --ignore **/__tests__",
|
||||||
|
"test": "jest"
|
||||||
},
|
},
|
||||||
"author": "Sebastian Rindom",
|
"author": "Sebastian Rindom",
|
||||||
"license": "AGPL-3.0-or-later",
|
"license": "AGPL-3.0-or-later",
|
||||||
@@ -22,7 +23,9 @@
|
|||||||
"@babel/plugin-transform-runtime": "^7.7.6",
|
"@babel/plugin-transform-runtime": "^7.7.6",
|
||||||
"@babel/preset-env": "^7.7.5",
|
"@babel/preset-env": "^7.7.5",
|
||||||
"cross-env": "^5.2.1",
|
"cross-env": "^5.2.1",
|
||||||
"eslint": "^6.8.0"
|
"eslint": "^6.8.0",
|
||||||
|
"jest": "^25.5.2",
|
||||||
|
"prettier": "^1.19.1"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"mongoose": "^5.8.0"
|
"mongoose": "^5.8.0"
|
||||||
|
|||||||
66
packages/medusa-interfaces/src/__tests__/base-service.js
Normal file
66
packages/medusa-interfaces/src/__tests__/base-service.js
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
import BaseService from "../base-service"
|
||||||
|
|
||||||
|
describe("BaseService", () => {
|
||||||
|
describe("addDecorator", () => {
|
||||||
|
const baseService = new BaseService()
|
||||||
|
|
||||||
|
it("successfully adds decorator", () => {
|
||||||
|
baseService.addDecorator(obj => {
|
||||||
|
return (obj.decorator1 = true)
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(baseService.decorators_.length).toEqual(1)
|
||||||
|
})
|
||||||
|
|
||||||
|
it("throws if decorator is not a function", () => {
|
||||||
|
expect(() => baseService.addDecorator("not a function")).toThrow(
|
||||||
|
"Decorators must be of type function"
|
||||||
|
)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe("runDecorators_", () => {
|
||||||
|
it("returns success when passwords match", async () => {
|
||||||
|
const baseService = new BaseService()
|
||||||
|
|
||||||
|
baseService.addDecorator(obj => {
|
||||||
|
obj.decorator1 = true
|
||||||
|
return obj
|
||||||
|
})
|
||||||
|
baseService.addDecorator(obj => {
|
||||||
|
obj.decorator2 = true
|
||||||
|
return obj
|
||||||
|
})
|
||||||
|
|
||||||
|
const result = await baseService.runDecorators_({ data: "initial" })
|
||||||
|
expect(result).toEqual({
|
||||||
|
data: "initial",
|
||||||
|
decorator1: true,
|
||||||
|
decorator2: true,
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
it("skips failing decorator", async () => {
|
||||||
|
const baseService = new BaseService()
|
||||||
|
|
||||||
|
baseService.addDecorator(obj => {
|
||||||
|
obj.decorator1 = true
|
||||||
|
return obj
|
||||||
|
})
|
||||||
|
baseService.addDecorator(obj => {
|
||||||
|
return Promise.reject("fail")
|
||||||
|
})
|
||||||
|
baseService.addDecorator(obj => {
|
||||||
|
obj.decorator3 = true
|
||||||
|
return Promise.resolve(obj)
|
||||||
|
})
|
||||||
|
|
||||||
|
const result = await baseService.runDecorators_({ data: "initial" })
|
||||||
|
expect(result).toEqual({
|
||||||
|
data: "initial",
|
||||||
|
decorator1: true,
|
||||||
|
decorator3: true,
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -2,5 +2,35 @@
|
|||||||
* Common functionality for Services
|
* Common functionality for Services
|
||||||
* @interface
|
* @interface
|
||||||
*/
|
*/
|
||||||
class BaseService {}
|
class BaseService {
|
||||||
|
constructor() {
|
||||||
|
this.decorators_ = []
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds a decorator to a service. The decorator must be a function and should
|
||||||
|
* return a decorated object.
|
||||||
|
* @param {function} fn - the decorator to add to the service
|
||||||
|
*/
|
||||||
|
addDecorator(fn) {
|
||||||
|
if (typeof fn !== "function") {
|
||||||
|
throw Error("Decorators must be of type function")
|
||||||
|
}
|
||||||
|
|
||||||
|
this.decorators_.push(fn)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Runs the decorators registered on the service. The decorators are run in
|
||||||
|
* the order they have been registered in. Failing decorators will be skipped
|
||||||
|
* in order to ensure deliverability in spite of breaking code.
|
||||||
|
* @param {object} obj - the object to decorate.
|
||||||
|
* @return {object} the decorated object.
|
||||||
|
*/
|
||||||
|
runDecorators_(obj) {
|
||||||
|
return this.decorators_.reduce(async (acc, next) => {
|
||||||
|
return acc.then(res => next(res)).catch(() => acc)
|
||||||
|
}, Promise.resolve(obj))
|
||||||
|
}
|
||||||
|
}
|
||||||
export default BaseService
|
export default BaseService
|
||||||
|
|||||||
@@ -18,6 +18,7 @@
|
|||||||
"@babel/preset-env": "^7.7.5",
|
"@babel/preset-env": "^7.7.5",
|
||||||
"client-sessions": "^0.8.0",
|
"client-sessions": "^0.8.0",
|
||||||
"cross-env": "^5.2.1",
|
"cross-env": "^5.2.1",
|
||||||
|
"jest": "^25.5.2",
|
||||||
"eslint": "^6.8.0"
|
"eslint": "^6.8.0"
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|||||||
@@ -24,12 +24,12 @@
|
|||||||
"@babel/preset-env": "^7.7.5",
|
"@babel/preset-env": "^7.7.5",
|
||||||
"@babel/runtime": "^7.7.6",
|
"@babel/runtime": "^7.7.6",
|
||||||
"cross-env": "^5.2.1",
|
"cross-env": "^5.2.1",
|
||||||
"jest": "^24.9.0"
|
"jest": "^25.5.2"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"mongoose": "^5.8.0",
|
|
||||||
"medusa-test-utils": "^1.0.0",
|
|
||||||
"medusa-core-utils": "^1.0.0",
|
"medusa-core-utils": "^1.0.0",
|
||||||
"medusa-interfaces": "^1.0.0"
|
"medusa-interfaces": "^1.0.0",
|
||||||
|
"medusa-test-utils": "^1.0.0",
|
||||||
|
"mongoose": "^5.8.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,6 +22,7 @@
|
|||||||
"@babel/plugin-transform-runtime": "^7.7.6",
|
"@babel/plugin-transform-runtime": "^7.7.6",
|
||||||
"@babel/preset-env": "^7.7.5",
|
"@babel/preset-env": "^7.7.5",
|
||||||
"cross-env": "^5.2.1",
|
"cross-env": "^5.2.1",
|
||||||
|
"jest": "^25.5.2",
|
||||||
"eslint": "^6.8.0"
|
"eslint": "^6.8.0"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
|||||||
@@ -24,7 +24,7 @@
|
|||||||
"@babel/runtime": "^7.7.6",
|
"@babel/runtime": "^7.7.6",
|
||||||
"cross-env": "^5.2.1",
|
"cross-env": "^5.2.1",
|
||||||
"eslint": "^6.8.0",
|
"eslint": "^6.8.0",
|
||||||
"jest": "^24.9.0",
|
"jest": "^25.5.2",
|
||||||
"nodemon": "^2.0.1",
|
"nodemon": "^2.0.1",
|
||||||
"prettier": "^1.19.1",
|
"prettier": "^1.19.1",
|
||||||
"supertest": "^4.0.2"
|
"supertest": "^4.0.2"
|
||||||
|
|||||||
Reference in New Issue
Block a user