-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
67 lines (57 loc) · 1.75 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
const { applyPatch } = require("fast-json-patch");
const getSubDocument = (document, patch) => {
const keys = patch.map(x => x.path.split("/")[1]);
return keys.reduce((doc, key) => {
doc[key] = document[key];
return doc;
}, {});
};
const wrap = service => {
if (
typeof service.get !== "function" ||
typeof service.patch !== "function"
) {
throw new Error(
"feathers-json-patch expects services to implement get and patch methods"
);
}
const newService = {
get: (id, params) => service.get(id, params),
patch: (id, data, params) => {
if (id && Array.isArray(data)) {
return service
.get(id, params)
.then(document => getSubDocument(document, data))
.then(document => applyPatch(document, data).newDocument)
.then(document => service.patch(id, document, params));
} else {
return service.patch(id, data, params);
}
}
};
if (typeof service.find === "function") {
newService.find = params => service.find(params);
}
if (typeof service.create === "function") {
newService.create = (data, params) => service.create(data, params);
}
if (typeof service.update === "function") {
newService.update = (id, data, params) => service.update(id, data, params);
}
if (typeof service.remove === "function") {
newService.remove = (id, params) => service.remove(id, params);
}
if (typeof service.setup === "function") {
newService.setup = (app, path) => service.setup(app, path);
}
return newService;
};
module.exports = service =>
new Proxy(service, {
construct(target, args) {
return wrap(new target(...args));
},
apply(target, context, args) {
return wrap(target.apply(context, args));
}
});