feathers-plus / generator-feathers-plus

A Yeoman generator to (re)generate a FeathersJS application supporting both REST and GraphQL architectural concepts and their query languages.
https://generator.feathers-plus.com/
Other
44 stars 30 forks source link

TypeScript: Extract Application interface for reuse #270

Closed deskoh closed 4 years ago

deskoh commented 5 years ago

Currently the generated app.interface.ts looks something like the following:

export type App = Application<{
  'messages': Message,
  // !code moduleExports // !end
}>;

Propose to extract out the generic type for reuse:

export interface IApplication {
  'messages': Message,
  // !code moduleExports // !end
}

export type App = Application<IApplication>;

For example, using a custom react hooks for feathers-reactive can be written something like:

import { useState, useEffect } from 'react';
import App from 'Feathers';
import { IApplication } from 'app.interface';

// Other service imports

function useFeathersSubscription<T extends keyof IApplication>(serviceName: T, params?: Params) {
  // Assume non-paginated data.
  const [list, setList] = useState([] as IApplication[T][]);

  const service = App.service(serviceName);

  useEffect(() => {
    const subscription = service.watch()
      .find(params)
      .subscribe((results) => {
        // Assume non-paginated data.
        setList(results as IApplication[T][]);
      });

      return () => { subscription.unsubscribe(); };
    }, [service, params]);

  return [list, service];
}
bitflower commented 5 years ago

That'd be great. I've build a little hack around that but yeah - this abstraction would help. Might give it a shot as PR