Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try to guess context data that's required down stream #199

Open
wants to merge 2 commits into
base: release
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ const feathers = require('@feathersjs/feathers');
const { _ } = require('@feathersjs/commons');
const SYNC = Symbol('feathers-sync/enabled');

const defaultEvents = ['created', 'updated', 'removed', 'patched'];
const eventMap = {
created: 'create',
updated: 'update',
patched: 'patch',
removed: 'remove'
};
const defaultEvents = Object.keys(eventMap);
const getServiceOptions = service => {
if (typeof feathers.getServiceOptions === 'function') {
return feathers.getServiceOptions(service);
Expand All @@ -12,6 +18,20 @@ const getServiceOptions = service => {
return {};
};

const constructContext = (app, path, event, data, context = {}) => {
const id = (typeof data === 'object' && !Array.isArray(data))
? (data._id || data.id)
: undefined;
return Object.assign({
path,
id,
method: eventMap[event],
params: {},
result: data,
event
}, context);
};

module.exports = app => {
if (app[SYNC]) {
return;
Expand All @@ -25,6 +45,7 @@ module.exports = app => {

app.on('sync-in', (rawData) => {
const { event, path, data, context } = app.sync.deserialize(rawData);
if (!path) return;
const service = app.service(path);
const hook = context
? Object.assign({ app, service }, context)
Expand Down Expand Up @@ -53,9 +74,10 @@ module.exports = app => {
}

const serializedContext = ctx && typeof ctx.toJSON === 'function' ? ctx.toJSON() : ctx;
const constructedContext = constructContext(app, path, event, data, serializedContext);
const context = ctx && (ctx.app === app || ctx.service === service)
? _.omit(serializedContext, 'app', 'service', 'self')
: serializedContext;
? _.omit(constructedContext, 'app', 'service', 'self')
: constructedContext;

debug(`Sending sync-out event '${path} ${event}'`);

Expand Down
37 changes: 36 additions & 1 deletion test/core.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ describe('feathers-sync core tests', () => {
path: 'todo',
data: message,
context: {
id: undefined,
arguments: [message, {}],
data: message,
params: {},
Expand Down Expand Up @@ -80,13 +81,47 @@ describe('feathers-sync core tests', () => {
app.service('todo').create({ message });
});

it('sends sync-out for manual emits', done => {
const message = { message: 'This is a test', id: 1 };

app.once('sync-out', data => {
try {
assert.deepStrictEqual(data, {
event: 'created',
path: 'todo',
data: message,
context: {
id: 1,
params: {},
method: 'create',
event: 'created',
path: 'todo',
result: message
}
});
done();
} catch (error) {
done(error);
}
});

app.service('todo').emit('created', message);
});

it('sends sync-out for custom events', done => {
app.once('sync-out', data => {
assert.deepStrictEqual(data, {
event: 'custom',
path: 'todo',
data: 'testing',
context: undefined
context: {
path: 'todo',
id: undefined,
method: undefined,
params: {},
result: 'testing',
event: 'custom'
}
});
done();
});
Expand Down
Loading