Skip to content

Commit

Permalink
fix: prevent client from emitting the events back to server
Browse files Browse the repository at this point in the history
  • Loading branch information
jd1378 committed Mar 25, 2024
1 parent 20dc129 commit 5737712
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/create-pinia-service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Params as FeathersParams, FeathersService, Id, Paginated, PaginationOptions } from '@feathersjs/feathers'
import { Params as FeathersParams, FeathersService, Id, Paginated, PaginationOptions, SERVICE } from '@feathersjs/feathers'
import type { MaybeRef } from '@vueuse/core'
import type { ComputedRef } from 'vue-demi'
import { computed, isRef, reactive, ref, unref } from 'vue-demi'
Expand All @@ -12,6 +12,8 @@ import type { ServiceInstance } from './modeling/index.js'
interface PiniaServiceOptions {
servicePath: string
store: any
methods?: string[]
events?: string[]
}

// FIXME: Those are very hacky, there should be a simpler way of recovering service types
Expand All @@ -25,19 +27,27 @@ type SvcModel<S extends FeathersService> = ServiceInstance<SvcResult<S>>
export class PiniaService<Svc extends FeathersService> {
store
servicePath = ''

constructor(public service: Svc, public options: PiniaServiceOptions) {
this.store = options.store
this.servicePath = options.servicePath
this.options = options
this.options.methods = ['create', 'patch', 'remove']
this.options.events = ['created', 'patched', 'removed']

// copy custom methods from service onto this instance, exclude existing methods
const keysToIgnore = Object.getOwnPropertyNames(Object.getPrototypeOf(this)).concat(existingServiceMethods)
for (const key in service) {
if (typeof service[key] === 'function' && !keysToIgnore.includes(key)) {
const instance = this as any
instance[key] = (service[key] as any).bind(service)
this.options.methods.push(key)
}
}

Object.defineProperty(this, SERVICE, {
value: this.options
})
}

/**
Expand Down

0 comments on commit 5737712

Please sign in to comment.