Skip to content

Commit 6598c40

Browse files
committed
refactor: simplify in-page channel option types
1 parent 23b3ced commit 6598c40

3 files changed

Lines changed: 66 additions & 72 deletions

File tree

‎packages/devframe/src/in-page-channel/index.ts‎

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ export type {
1717
InPageChannelProtocol,
1818
InPageChannelStatus,
1919
InPageFunctionDefinition,
20-
InPageFunctionDefinitionFor,
2120
PageScriptChannel,
2221
PanelChannel,
2322
PanelPeer,
@@ -32,37 +31,14 @@ export type {
3231
* constant.
3332
*/
3433
export function defineChannelFunction<
35-
const NAME extends string,
34+
NAME extends string,
35+
TYPE extends InPageFunctionType,
3636
ARGS extends any[],
3737
RETURN = void,
38-
TYPE extends InPageFunctionType = 'query',
38+
const AS extends RpcArgsSchema | undefined = undefined,
39+
const RS extends RpcReturnSchema | undefined = undefined,
3940
>(
40-
definition: {
41-
name: NAME
42-
type?: TYPE
43-
args?: undefined
44-
returns?: undefined
45-
jsonSerializable?: boolean
46-
handler: (...args: ARGS) => RETURN
47-
},
48-
): InPageFunctionDefinition<NAME, TYPE, ARGS, RETURN>
49-
export function defineChannelFunction<
50-
const NAME extends string,
51-
const AS extends RpcArgsSchema,
52-
const RS extends RpcReturnSchema,
53-
TYPE extends InPageFunctionType = 'query',
54-
>(
55-
definition: {
56-
name: NAME
57-
type?: TYPE
58-
args: AS
59-
returns: RS
60-
jsonSerializable?: boolean
61-
handler: InPageFunctionDefinition<NAME, TYPE, never, never, AS, RS>['handler']
62-
},
63-
): InPageFunctionDefinition<NAME, TYPE, never, never, AS, RS>
64-
export function defineChannelFunction(
65-
definition: InPageFunctionDefinition<string, InPageFunctionType, any[], any, any, any>,
66-
): InPageFunctionDefinition<string, InPageFunctionType, any[], any, any, any> {
41+
definition: InPageFunctionDefinition<NAME, TYPE, ARGS, RETURN, AS, RS>,
42+
): InPageFunctionDefinition<NAME, TYPE, ARGS, RETURN, AS, RS> {
6743
return definition
6844
}

‎packages/devframe/src/in-page-channel/types.test-d.ts‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { InPageChannelProtocol } from './types'
2-
import { describe, it } from 'vitest'
2+
import { describe, expectTypeOf, it } from 'vitest'
33
import { createPageScriptChannel } from './page-script'
44
import { connectPanelChannel } from './panel'
55

@@ -20,13 +20,22 @@ describe('in-page channel function definitions', () => {
2020
name: 'devframes:test',
2121
functions: {
2222
echo: {
23-
handler: value => value.toUpperCase(),
23+
handler: (value) => {
24+
expectTypeOf(value).toEqualTypeOf<string>()
25+
return value.toUpperCase()
26+
},
2427
},
2528
sum: {
26-
handler: (a, b) => a + b,
29+
handler: (a, b) => {
30+
expectTypeOf(a).toEqualTypeOf<number>()
31+
expectTypeOf(b).toEqualTypeOf<number>()
32+
return a + b
33+
},
2734
},
2835
save: {
29-
handler: () => {},
36+
handler: (value) => {
37+
expectTypeOf(value).toEqualTypeOf<string>()
38+
},
3039
},
3140
},
3241
})
@@ -41,7 +50,7 @@ describe('in-page channel function definitions', () => {
4150
functions: {
4251
notify: {
4352
handler: (message) => {
44-
void message
53+
expectTypeOf(message).toEqualTypeOf<string>()
4554
},
4655
},
4756
},

‎packages/devframe/src/in-page-channel/types.ts‎

Lines changed: 46 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ type SharedStates<P extends InPageChannelProtocol>
3030

3131
type FnArgs<F> = F extends (...args: infer A) => any ? A : never
3232
type FnReturn<F> = F extends (...args: any[]) => infer R ? Awaited<R> : never
33+
34+
/**
35+
* Converts a protocol function to its accepted endpoint handler.
36+
*
37+
* @internal
38+
*/
3339
type ProtocolHandler<F> = F extends (...args: any[]) => any
3440
? (...args: FnArgs<F>) => Thenable<FnReturn<F>>
3541
: never
@@ -78,47 +84,45 @@ export type InPageFunctionDefinition<
7884
handler: (...args: InferArgsType<AS>) => Thenable<InferReturnType<RS>>
7985
}
8086

81-
/** Loosely-typed definition — the registration unit both endpoints accept. */
87+
/**
88+
* Loosely-typed definition used by the internal function registry.
89+
*
90+
* @internal
91+
*/
8292
export type InPageFunctionDefinitionAny = InPageFunctionDefinition<string, any, any, any, any, any>
8393

84-
type ProtocolSide = 'pageScript' | 'panel'
85-
type ProtocolSideFunctions<
86-
P extends InPageChannelProtocol,
87-
SIDE extends ProtocolSide,
88-
> = SideFunctions<NonNullable<P[SIDE]>>
94+
/**
95+
* Function metadata with its handler constrained by a protocol function.
96+
*
97+
* @internal
98+
*/
99+
interface InPageFunctionOption<F> {
100+
type?: InPageFunctionType
101+
/** Optional Standard Schema array validating the arguments. */
102+
args?: RpcArgsSchema
103+
/** Optional Standard Schema validating the resolved return value. */
104+
returns?: RpcReturnSchema
105+
jsonSerializable?: boolean
106+
handler: ProtocolHandler<F>
107+
}
89108

90109
/**
91-
* A function definition constrained by one side of an in-page channel
92-
* protocol. The `name` discriminant selects the matching protocol function,
93-
* which contextually types the handler's arguments and return value.
110+
* Functions implemented by {@link createPageScriptChannel}.
111+
*
112+
* @internal
94113
*/
95-
export type InPageFunctionDefinitionFor<
96-
P extends InPageChannelProtocol,
97-
SIDE extends ProtocolSide,
98-
> = {
99-
[NAME in keyof ProtocolSideFunctions<P, SIDE> & string]: {
100-
name: NAME
101-
type?: InPageFunctionType
102-
/** Optional Standard Schema array validating the arguments. */
103-
args?: RpcArgsSchema
104-
/** Optional Standard Schema validating the resolved return value. */
105-
returns?: RpcReturnSchema
106-
jsonSerializable?: boolean
107-
handler: ProtocolHandler<ProtocolSideFunctions<P, SIDE>[NAME]>
108-
}
109-
}[keyof ProtocolSideFunctions<P, SIDE> & string]
114+
type CreatePageScriptChannelOptionsFunctions<P extends InPageChannelProtocol> = {
115+
[NAME in keyof PageScriptFunctions<P> & string]: InPageFunctionOption<PageScriptFunctions<P>[NAME]>
116+
}
110117

111-
type InPageFunctionOptions<
112-
P extends InPageChannelProtocol,
113-
SIDE extends ProtocolSide,
114-
> = InPageChannelProtocol extends P
115-
? Record<string, Omit<InPageFunctionDefinitionAny, 'name'>>
116-
: {
117-
[NAME in keyof ProtocolSideFunctions<P, SIDE> & string]: Omit<
118-
Extract<InPageFunctionDefinitionFor<P, SIDE>, { name: NAME }>,
119-
'name'
120-
>
121-
}
118+
/**
119+
* Functions implemented by {@link connectPanelChannel}.
120+
*
121+
* @internal
122+
*/
123+
type ConnectPanelChannelOptionsFunctions<P extends InPageChannelProtocol> = {
124+
[NAME in keyof PanelFunctions<P> & string]: InPageFunctionOption<PanelFunctions<P>[NAME]>
125+
}
122126

123127
/**
124128
* Connection lifecycle of a panel endpoint: `connecting` (handshake retry
@@ -127,6 +131,11 @@ type InPageFunctionOptions<
127131
*/
128132
export type InPageChannelStatus = 'connecting' | 'connected' | 'closed'
129133

134+
/**
135+
* Options shared by both in-page channel endpoints.
136+
*
137+
* @internal
138+
*/
130139
interface InPageChannelCommonOptions {
131140
/**
132141
* Channel name, namespaced with the devframe id by convention
@@ -165,7 +174,7 @@ interface InPageChannelCommonOptions {
165174
/** Options for {@link createPageScriptChannel}. */
166175
export interface CreatePageScriptChannelOptions<Protocol extends InPageChannelProtocol = InPageChannelProtocol> extends InPageChannelCommonOptions {
167176
/** Implementations of the protocol's page-script functions. */
168-
functions?: InPageFunctionOptions<Protocol, 'pageScript'>
177+
functions?: CreatePageScriptChannelOptionsFunctions<Protocol>
169178
/**
170179
* Window whose `message` events carry panel hellos. Defaults to the
171180
* global `window`; pass `false` to skip the handshake listener entirely
@@ -177,7 +186,7 @@ export interface CreatePageScriptChannelOptions<Protocol extends InPageChannelPr
177186
/** Options for {@link connectPanelChannel}. */
178187
export interface ConnectPanelChannelOptions<Protocol extends InPageChannelProtocol = InPageChannelProtocol> extends InPageChannelCommonOptions {
179188
/** Implementations of the protocol's panel functions. */
180-
functions?: InPageFunctionOptions<Protocol, 'panel'>
189+
functions?: ConnectPanelChannelOptionsFunctions<Protocol>
181190
/**
182191
* The panel's own window (listens for the handshake grant). Defaults to
183192
* the global `window`; pass `false` with `transport` to skip the handshake.

0 commit comments

Comments
 (0)