Closed Capevace closed 3 years ago
New API in place and works pretty well.
Needed now:
Most API features implemented, the few missing get their own issues.
Documentation also still needed, however since the old plugin API has been completely removed, we can close this issue.
Some experimentation:
```js /** * A file to experiment with different API designs. */ // Server Side Sync module class ServiceController { constructor(name, initialState = {}) { this.name = name; // Service State this.state = initialState; this.handlers = { 'action': { permissions: [], handler } }; autoBind(this); } action(name, options = {}) { options = { handler: () => { throw new Error(`Action '${name}' in service '${this.name}' has no handler`); }, ...options, permissions: [ ['update', 'service', 'any'], ...(options.permissions || []) ] }; if (name in Object.keys(this.handlers)) { throw new Error(`Action handler for '${name}' already exists on service ${this.name}`); } this.handlers[name] = { permissions: options.permissions, handler: options.handler }; // We return the action builder, an easier to use api with lots of helper functions. // let builder = { handler(handler) { if (typeof handler !== 'function') { throw new Error(`Action handler for '${this.name}' has to be a function`); } this.handlers[name].handler = handler; return builder; }, requirePermission(crud, resource, scope = 'any') { if (scope !== 'any' || scope !== 'own') { throw new Error(`Permission scope has to be either 'any' or 'own'.`); } this.handlers[name].permissions.push([crud, resource, scope]); return builder; }, requirePermissions(perms) { if (!Array.isArray(perms)) { throw new Error(`You need to pass permissions as an array like this (e.g. [['read', 'user', 'any'], ['update', 'user', 'own']]`) } perms.forEach(permissionArray => builder.permission(...permissionArray)); return builder; } }; return builder; } } //// register action const service = sync.createService('test'); permissions .grant('guest').readAny('video'); permissions .grant('user').createAny('video'); service.action('action:') .requirePermission('create', 'video', 'any') .handler(async (data, context) => { // Custom data filtration const { filter } = permissions.can(context.user.role).createAny('video'); // return some data // filter based on permissions passed to action return context.filter({ test: 1 }); }) //// class ServiceClient { constructor(name, controller) { } subscribe(nameOrCb, cb) { } dispatch(name, data) {} } class CoreService extends { } const RESERVED_SERVICES = ['core']; /** * Sync is the main thing */ class Sync { constructor() { /** * The services that sync manages * @type {Object