Skip to content
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
9 changes: 8 additions & 1 deletion packages/feathers/src/declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,16 @@ export interface ServiceOptions<MethodTypes = string> {
*/
events?: string[] | readonly string[]
/**
* A list of service methods that should be available __externally__ to clients
* A list of service methods that `all` hooks will apply to.
* Defaults to the standard service methods (find, get, create, update, patch, remove)
* that exist on the service.
*/
methods?: MethodTypes[] | readonly MethodTypes[]
/**
* A list of service methods that should be available __externally__ to clients
* via transports like HTTP or WebSockets. Defaults to `methods` if not specified.
*/
externalMethods?: MethodTypes[] | readonly MethodTypes[]
/**
* Provide a full list of events that this service should emit to clients.
* Unlike the `events` option, this will not be merged with the default events.
Expand Down
7 changes: 7 additions & 0 deletions packages/feathers/src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ export function getHookMethods(service: any, options: ServiceOptions) {
.concat(methods)
}

export function getExternalMethods(options: ServiceOptions): readonly string[] {
return options.externalMethods || options.methods || []
}

export function getServiceOptions(service: any): ServiceOptions {
return service[SERVICE]
}
Expand All @@ -44,12 +48,15 @@ export const normalizeServiceOptions = (service: any, options: ServiceOptions =
methods = defaultServiceMethods.filter((method) => typeof service[method] === 'function'),
events = service.events || []
} = options
// externalMethods defaults to methods for backwards compatibility
const externalMethods = options.externalMethods || methods
const serviceEvents = options.serviceEvents || defaultServiceEvents.concat(events)

return {
...options,
events,
methods,
externalMethods,
serviceEvents
}
}
Expand Down
12 changes: 9 additions & 3 deletions packages/transport-commons/src/socket/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { Application, getServiceOptions, Params, RealTimeConnection } from '@feathersjs/feathers'
import {
Application,
getServiceOptions,
getExternalMethods,
Params,
RealTimeConnection
} from '@feathersjs/feathers'
import { channels } from '../channels'
import { routing } from '../routing'
import { getDispatcher, runMethod } from './utils'
Expand Down Expand Up @@ -43,9 +49,9 @@ export function socket({ done, emit, socketMap, socketKey, getParams }: SocketOp
done.then((provider) =>
provider.on('connection', (connection: any) => {
const methodHandlers = Object.keys(app.services).reduce((result, name) => {
const { methods } = getServiceOptions(app.service(name))
const externalMethods = getExternalMethods(getServiceOptions(app.service(name)))

methods.forEach((method) => {
externalMethods.forEach((method) => {
if (!result[method]) {
result[method] = (...args: any[]) => {
const [path, ...rest] = args
Expand Down
9 changes: 5 additions & 4 deletions packages/transport-commons/src/socket/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import {
Application,
RealTimeConnection,
createContext,
getServiceOptions
getServiceOptions,
getExternalMethods
} from '@feathersjs/feathers'
import { NotFound, MethodNotAllowed, BadRequest } from '@feathersjs/errors'
import { createDebug } from '@feathersjs/commons'
Expand Down Expand Up @@ -97,10 +98,10 @@ export async function runMethod(
}

const { service, params: route = {} } = lookup
const { methods } = getServiceOptions(service)
const externalMethods = getExternalMethods(getServiceOptions(service))

// Only service methods are allowed
if (!methods.includes(method)) {
// Only externally exposed service methods are allowed
if (!externalMethods.includes(method)) {
throw new MethodNotAllowed(`Method '${method}' not allowed on service '${path}'`)
}

Expand Down