feathersjs / feathers

The API and real-time application framework
https://feathersjs.com
MIT License
15.03k stars 750 forks source link

feat(generators): Add service file for shared information #3008

Closed daffl closed 1 year ago

daffl commented 1 year ago

This pull request adds a <service>.shared file to a generated application which contains information that is shared between the client and the server:

A shared service file looks like this:

// For more information about this file see https://dove.feathersjs.com/guides/cli/service.shared.html
import type { Params } from '@feathersjs/feathers'
import type { ClientApplication } from '../../client'
import type { Message, MessageData, MessagePatch, MessageQuery, MessageService } from './messages.class'

export type { Message, MessageData, MessagePatch, MessageQuery }

export const messagePath = 'messages'

export const messageMethods = ['find', 'get', 'create', 'patch', 'remove'] as const

export type MessageClientService = Pick<MessageService<Params<MessageQuery>>, (typeof messageMethods)[number]>

export const messageClient = (client: ClientApplication) => {
  const connection = client.get('connection')

  client.use(messagePath, connection.service(messagePath), {
    methods: messageMethods
  })
}

// Add this service to the client service type index
declare module '../../client' {
  interface ServiceTypes {
    [messagePath]: MessageClientService
  }
}

This file can also be used to e.g. define common helpers and schemas that can be used on both sides to e.g. do client side validation.

Closes https://github.com/feathersjs/feathers/issues/2951

netlify[bot] commented 1 year ago

Deploy Preview for feathers-dove canceled.

Name Link
Latest commit 4943803224fbd85df4b3838b4391532b48a8c79f
Latest deploy log https://app.netlify.com/sites/feathers-dove/deploys/63d6b30d4fd4ea0008529189
marshallswain commented 1 year ago

I definitely prefer to group the types and variables, like this:

// For more information about this file see https://dove.feathersjs.com/guides/cli/service.shared.html
import type { Params } from '@feathersjs/feathers'
import type { ClientApplication } from '../../client'
import type { Message, MessageData, MessagePatch, MessageQuery, MessageService } from './messages.class'

export type { Message, MessageData, MessagePatch, MessageQuery }
export type MessageClientService = Pick<MessageService<Params<MessageQuery>>, (typeof messageMethods)[number]>

export const messagePath = 'messages'
export const messageMethods = ['find', 'get', 'create', 'patch', 'remove'] as const

export const messageClient = (client: ClientApplication) => {
  const connection = client.get('connection')

  client.use(messagePath, connection.service(messagePath), {
    methods: messageMethods
  })
}

// Add this service to the client service type index
declare module '../../client' {
  interface ServiceTypes {
    [messagePath]: MessageClientService
  }
}

Also, it's worth noting that the JS version will look like this:

export const messagePath = 'messages'
export const messageMethods = ['find', 'get', 'create', 'patch', 'remove']

export const messageClient = (client) => {
  const connection = client.get('connection')

  client.use(messagePath, connection.service(messagePath), {
    methods: messageMethods
  })
}

Filenames we talked about:

After reviewing it further, I think service.shared.ts makes the most sense.