-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding a well-typed server spec in prep for a typesafe client
- Loading branch information
Showing
5 changed files
with
109 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,51 @@ | ||
import * as t from 'io-ts'; | ||
import { Status, Request, ErrorBody } from 'ts-alias-wire'; | ||
|
||
/* | ||
Server configuration | ||
*/ | ||
export interface Config<Context> { | ||
export interface Config<Context, SpecT extends ServerBaseSpec> { | ||
channels: ServerSpec<Context, SpecT>, | ||
onContext: ContextFn<Context>, | ||
channels: { [k: string]: ConnectFn<Context> | undefined }, | ||
port: number, | ||
port?: number, | ||
} | ||
|
||
// Context | ||
export type ContextFn<Context> | ||
= (metadata: unknown) => Promise<Context> | Context; | ||
export type ServerSpec<Context, SpecT extends ServerBaseSpec> = { | ||
[Key in keyof SpecT]: SpecT[Key] extends ChannelSpec<Context, SpecT[Key]['argSpec'], infer _> ? SpecT[Key] : never; | ||
}; | ||
|
||
export type ServerBaseSpec = Record<string, { argSpec: t.Mixed, onConnect: unknown }>; | ||
|
||
export type ChannelSpec<Context, ArgSpecT extends t.Mixed, Event> = { | ||
argSpec: ArgSpecT, | ||
onConnect: ConnectFn<Context, t.TypeOf<ArgSpecT>, Event> | ||
} | ||
|
||
// New connection | ||
export type ConnectFn<Context> = ( | ||
controls: Channel, | ||
args: unknown, | ||
export type ConnectFn<Context, ArgT, Event> = ( | ||
controls: Channel<Event>, | ||
args: ArgT, | ||
context: Context, | ||
request: Request | ||
) => Promise<Destructor | void>; | ||
|
||
// Channel interface | ||
export interface Channel { | ||
export interface Channel<OkBody> { | ||
emit: <statusT extends Status>( | ||
status: statusT, | ||
response: ResponseT[statusT] | ||
response: ResponseT<OkBody>[statusT] | ||
) => void; | ||
} | ||
|
||
export type Destructor = () => void; | ||
|
||
// Context | ||
export type ContextFn<Context> | ||
= (metadata: unknown) => Promise<Context> | Context; | ||
|
||
// type util | ||
export type ResponseT = { | ||
ok: unknown, | ||
export type ResponseT<Event> = { | ||
ok: Event, | ||
error: ErrorBody, | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters