Skip to content

Commit

Permalink
Improve internal PropertyDescriptor types (#420)
Browse files Browse the repository at this point in the history
* extends ExecuteExtendedOptions with ParamsType

* update video room methods

* update chat methods

* fix BaseComponent type

* add changeset

* solve fixmes
  • Loading branch information
edolix authored Feb 4, 2022
1 parent e4b8970 commit 1bda627
Show file tree
Hide file tree
Showing 6 changed files with 191 additions and 96 deletions.
5 changes: 5 additions & 0 deletions .changeset/healthy-mugs-decide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@signalwire/core': patch
---

[internal] Improve internal PropertyDescriptor methods
10 changes: 7 additions & 3 deletions packages/core/src/BaseComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -547,13 +547,17 @@ export class BaseComponent<
}

/** @internal */
execute<InputType = unknown, OutputType = unknown>(
execute<
InputType = unknown,
OutputType = unknown,
ParamsType = Record<string, any>
>(
{ method, params }: ExecuteParams,
{
transformParams = identity,
transformResolve = identity,
transformReject = identity,
}: ExecuteExtendedOptions<InputType, OutputType> = {
}: ExecuteExtendedOptions<InputType, OutputType, ParamsType> = {
transformParams: identity,
transformResolve: identity,
transformReject: identity,
Expand All @@ -573,7 +577,7 @@ export class BaseComponent<
requestId,
componentId: this.__uuid,
method,
params: transformParams(params),
params: transformParams(params as ParamsType),
})
)
})
Expand Down
143 changes: 96 additions & 47 deletions packages/core/src/chat/methods.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,49 @@
import type { ChatPublishParams, ChatJSONRPCMethod } from '../types/chat'
import type {
ChatJSONRPCMethod,
ChatCursor,
InternalChatMessageEntity,
ChatMessageEntity,
InternalChatMemberEntity,
ChatMemberEntity,
} from '../types/chat'
import type { BaseChatConsumer } from './BaseChat'
import type { ExecuteExtendedOptions } from '../utils/interfaces'
import type { ExecuteExtendedOptions, BaseRPCResult } from '../utils/interfaces'
import { toExternalJSON } from '../utils'
import { toInternalChatChannels, isValidChannels } from './utils'

interface ChatMethodPropertyDescriptor<T, ParamsType>
type ChatMethodParams = Record<string, unknown>

interface ChatMethodPropertyDescriptor<OutputType, ParamsType>
extends PropertyDescriptor {
value: (params: ParamsType) => Promise<T>
value: (params: ParamsType) => Promise<OutputType>
}
type ChatMethodParams = Record<string, unknown>
type ChatMethodDescriptor<
T = unknown,
OutputType = unknown,
ParamsType = ChatMethodParams
> = ChatMethodPropertyDescriptor<T, ParamsType> & ThisType<BaseChatConsumer>
> = ChatMethodPropertyDescriptor<OutputType, ParamsType> &
ThisType<BaseChatConsumer>

const createChatMethod = <InputType, OutputType = InputType>(
/**
* Transform for returning `undefined` for `execute`s that were
* successully resolved. If the `execute` failed for some reason, then
* the promise will be rejected and this transform will never be
* executed.
*/
const baseCodeTransform = () => {}

const createChatMethod = <
InputType,
OutputType = InputType,
ParamsType extends ChatMethodParams = ChatMethodParams
>(
method: ChatJSONRPCMethod,
options: ExecuteExtendedOptions<InputType, OutputType> = {}
options: ExecuteExtendedOptions<InputType, OutputType, ParamsType> = {}
): ChatMethodDescriptor<OutputType> => ({
value: function (params: ChatMethodParams = {}): Promise<OutputType> {
const channels = isValidChannels(params?.channels)
? toInternalChatChannels(params.channels)
: undefined

value: function (params = {}): Promise<OutputType> {
return this.execute(
{
method,
params: {
...params,
channels,
},
params,
},
options
)
Expand All @@ -40,27 +54,25 @@ const createChatMethod = <InputType, OutputType = InputType>(
* Type the params for each chat method that requires a memberId.
* Additional params can be passed.
*/
interface ChatMemberMethodParams {
interface ChatMemberMethodParams extends Record<string, unknown> {
memberId?: string
[key: string]: unknown
}

const createChatMemberMethod = <InputType, OutputType = InputType>(
const createChatMemberMethod = <
InputType,
OutputType = InputType,
ParamsType extends ChatMemberMethodParams = ChatMemberMethodParams
>(
method: ChatJSONRPCMethod,
options: ExecuteExtendedOptions<InputType, OutputType> = {}
options: ExecuteExtendedOptions<InputType, OutputType, ParamsType> = {}
): ChatMethodDescriptor<OutputType> => ({
value: function ({ memberId, ...rest }: ChatMemberMethodParams = {}) {
const channels = isValidChannels(rest?.channels)
? toInternalChatChannels(rest.channels)
: undefined

value: function ({ memberId, ...rest } = {}) {
return this.execute(
{
method,
params: {
member_id: memberId,
...rest,
channels,
},
},
options
Expand All @@ -71,17 +83,35 @@ const createChatMemberMethod = <InputType, OutputType = InputType>(
/**
* Chat Methods
*/
export const publish = createChatMethod<ChatPublishParams>('chat.publish')
export const getMessages = createChatMethod<{ messages: any[]; cursor: any }>(
'chat.messages.get',
{
transformResolve: (payload) => ({
messages: payload.messages.map((message) => toExternalJSON(message)),
cursor: payload.cursor,
}),
}
)
export const getMembers = createChatMethod<{ members: any[] }>(
export const publish = createChatMethod<BaseRPCResult, void>('chat.publish', {
transformResolve: baseCodeTransform,
})

interface GetMessagesInput extends BaseRPCResult {
messages: InternalChatMessageEntity[]
cursor: ChatCursor
}
interface GetMessagesOutput {
messages: ChatMessageEntity[]
cursor: ChatCursor
}
export const getMessages = createChatMethod<
GetMessagesInput,
GetMessagesOutput
>('chat.messages.get', {
transformResolve: (payload) => ({
messages: payload.messages.map((message) => toExternalJSON(message)),
cursor: payload.cursor,
}),
})

interface GetMembersInput extends BaseRPCResult {
members: InternalChatMemberEntity[]
}
interface GetMembersOutput {
members: ChatMemberEntity[]
}
export const getMembers = createChatMethod<GetMembersInput, GetMembersOutput>(
'chat.members.get',
{
transformResolve: (payload) => ({
Expand All @@ -90,18 +120,37 @@ export const getMembers = createChatMethod<{ members: any[] }>(
}
)

const transformParamChannels = (params: ChatMemberMethodParams) => {
const channels = isValidChannels(params?.channels)
? toInternalChatChannels(params.channels)
: undefined

return {
...params,
channels,
}
}
/**
* Chat Member Methods
*/
export const setMemberState = createChatMemberMethod<any, void>(
export const setMemberState = createChatMemberMethod<BaseRPCResult, void>(
'chat.member.set_state',
{
transformResolve: () => {},
}
)
export const getMemberState = createChatMemberMethod<{ channels: any }>(
'chat.member.get_state',
{
transformResolve: (payload) => ({ channels: payload.channels }),
transformResolve: baseCodeTransform,
transformParams: transformParamChannels,
}
)

interface GetMemberStateInput extends BaseRPCResult {
channels: any
}
interface GetMemberStateOutput {
channels: any
}
export const getMemberState = createChatMemberMethod<
GetMemberStateInput,
GetMemberStateOutput
>('chat.member.get_state', {
transformResolve: (payload) => ({ channels: payload.channels }),
transformParams: transformParamChannels,
})
Loading

0 comments on commit 1bda627

Please sign in to comment.