Skip to content

Commit

Permalink
feat: EndpointApi renamed to WidgetHooks, simplified test of order ca…
Browse files Browse the repository at this point in the history
…rd context
  • Loading branch information
cmath10 committed Oct 30, 2024
1 parent 9043dc7 commit da02cb0
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 18 deletions.
10 changes: 5 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type {
} from '@remote-ui/rpc'

import type { ContextAccessor } from '~types/context/schema'
import type { ExtensionApi } from '~types/endpoint'
import type { WidgetHooks } from '~types/endpoint'

import { SchemaMap } from '~types/context'

Expand All @@ -22,7 +22,7 @@ export {
} from './context/store'

export const createWidgetEndpoint = <M extends Record<string, AnyFunction>>(
api: ExtensionApi<M>,
hooks: WidgetHooks<M>,
messenger: MessageEndpoint
): Endpoint<ContextAccessor<SchemaMap>> => {
const endpoint = createEndpoint<ContextAccessor<SchemaMap>>(messenger)
Expand All @@ -39,16 +39,16 @@ export const createWidgetEndpoint = <M extends Record<string, AnyFunction>>(
release(methods)
}

await api.run(channel, target, methods)
await hooks.run(channel, target, methods)
},

release () {
api.release()
hooks.release()

onRelease()
onRelease = () => {}
},
} as ExtensionApi<M> as unknown as Record<string, AnyFunction | undefined>)
} as WidgetHooks<M> as unknown as Record<string, AnyFunction | undefined>)

return endpoint
}
21 changes: 9 additions & 12 deletions tests/order/card.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
import type { Channel } from '@omnicajs/vue-remote/host'
import type { ExtensionApi } from '~types/endpoint'
import type { None } from '~types/scaffolding'

import type { ContextAccessor, TypeOf } from '~types/context/schema'

import type { Schema } from '~types/context/order/card'
Expand All @@ -28,7 +24,6 @@ import {
} from 'vue'

import { createPinia } from 'pinia'
import { createWidgetEndpoint } from '@/index'
import {
createEndpoint,
fromMessagePort,
Expand Down Expand Up @@ -95,7 +90,7 @@ const createHostContext = () => {
}
},

on (context: string, event: string, handler: (payload: unknown) => void) {
on (context, event: string, handler: (payload: unknown) => void) {
if (context !== 'order/card') {
throw new Error(`[crm:embed:host] Context ${context} is not supported`)
}
Expand Down Expand Up @@ -133,23 +128,25 @@ describe('order/card', () => {

const { order, accessor } = createHostContext()

const host = createEndpoint<ExtensionApi<None>>(fromMessagePort(port1))
const host = createEndpoint<{ run (): void }>(fromMessagePort(port1))

host.expose(accessor)

const pinia = createPinia()

pinia.use(injectAccessor<SchemaMap>(createWidgetEndpoint<None>({
async run () {
const remote = createEndpoint<ContextAccessor<SchemaMap>>(fromMessagePort(port2))

remote.expose({
run () {
const app = createApp({ template: '<div />' })

app.use(pinia)
},
})

release () {},
}, fromMessagePort(port2))))
pinia.use(injectAccessor<SchemaMap>(remote))

await host.call.run(null as unknown as Channel, 'order/card:customer', {})
await host.call.run()

const wrapper = mount({
setup () {
Expand Down
2 changes: 1 addition & 1 deletion types/endpoint.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export type Target =
| 'order/card:customer.phone'
| 'order/card:delivery.address'

export interface ExtensionApi<M extends Record<string, AnyFunction>> {
export interface WidgetHooks<M extends Record<string, AnyFunction>> {
run (
channel: Channel,
target: Target,
Expand Down

0 comments on commit da02cb0

Please sign in to comment.