1.0.1-alpha.4 • Published 1 year ago

@tabular-state/api-feathers v1.0.1-alpha.4

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

Feathers Client Adapter

This package provides a simple Feathers client adapter for @tabular-state/store.

Installation

npm install @tabular-state/api-feathers
pnpm install @tabular-state/api-feathers
yarn add @tabular-state/api-feathers

Usage

import { createFeathersClientAdapter, toFeathersQuery } from '@tabular-state/api-feathers';

const feathersClient = feathers();
// ... configure feathers client

// you do not need to handle response values from feathersClient!
// response values are handled by hooks inside createFeathersClientAdapter

const store = createStore<Tables>();

store.plugin({
  mount: () => {
    const disposeGetRow = store.hook('before', 'getRow', (ctx) => {
      const { table, rowId } = ctx.params;
      feathersClient
        .service(table)
        .get(rowId)
        .catch((error) => {
          // handle error
        });
    });

    const disposeQueryRows = store.hook('before', 'queryRows', (ctx) => {
      const { table, query } = ctx.params;
      const feathersQuery = toFeathersQuery(query);
      feathersClient
        .service(table)
        .find({
          query: feathersQuery,
        })
        .catch((error) => {
          // handle error
        });
    });

    return () => {
      disposeGetRow();
      disposeQueryRows();
    };
  },
});

feathers.configure(
  createFeathersClientAdapter({
    store,
    services: {
      users: {
        events: ['created', 'updated', 'patched', 'removed'], // listen to socket events if socket.io-client is used
        idField: 'id',
        tableName: 'users',
      },
    },
    dynamicServices(path) {
      const regex = /^(\/?)custom-service\/(?<uuid>[a-zA-Z0-9-]*)\/sub-custom-service(\/?)$/d;
      const isCustomService = regex.exec(path);
      if (isCustomService && isCustomService.groups) {
        const { uuid } = isCustomService.groups;
        return {
          idField: 'id',
          tableName: `custom-service/${uuid}/sub-custom-service`,
        };
      }
      return null;
    },
  }),
);

Explanation

This package provides a simple Feathers client adapter for @tabular-state/store by using hooks and socket events.

It uses the hooks after.find, after.get, after.create, after.update, after.patch and after.remove to update the store.

1.0.1-alpha.4

1 year ago

1.0.1-alpha.3

1 year ago

1.0.1-alpha.1

1 year ago

1.0.1-alpha.0

1 year ago

1.0.0-alpha.0

1 year ago

0.7.2-alpha.0

1 year ago

0.7.1-alpha.0

1 year ago

0.7.0-alpha.0

1 year ago

0.6.0-alpha.0

1 year ago