-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rpc-adapter): adds option types and defaults
- Loading branch information
Showing
4 changed files
with
80 additions
and
2 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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import WebSocket from 'ws'; | ||
import { error } from '@karmic/core'; | ||
import { RPCAdapterOptions } from './types'; | ||
|
||
export default function createDefaults(): Required<RPCAdapterOptions> { | ||
return { | ||
children: true, | ||
querySubscriptions: true, | ||
heartbeat: 60000, | ||
server: new WebSocket.Server({ noServer: true }), | ||
context: () => ({}), | ||
routeError: { | ||
name: 'RouteError', | ||
type: error({ code: 'ClientNotFound' }) | ||
} | ||
}; | ||
} |
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 +1,2 @@ | ||
export * from './options'; | ||
export * from './rpc'; |
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 |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import http from 'http'; | ||
import WebSocket from 'ws'; | ||
import { | ||
ErrorTypeImplementation, | ||
CollectionTreeDeclaration | ||
} from '@karmic/core'; | ||
|
||
/** | ||
* RPC adapter output. | ||
*/ | ||
export interface RPCAdapter { | ||
/** | ||
* Resulting collection declaration, including the necessary | ||
* modifications made by the adapter. | ||
*/ | ||
declaration: CollectionTreeDeclaration; | ||
} | ||
|
||
export interface RPCAdapterOptions { | ||
/** | ||
* Whether to create routes for type's children services. Default: `true`. | ||
*/ | ||
children?: boolean; | ||
/** | ||
* Whether to allow for subscription services to be queried as unary | ||
* for a single result. | ||
*/ | ||
querySubscriptions?: boolean; | ||
/** | ||
* Frequency at which broken connection checks are run at | ||
* to free up resources in milliseconds. Default: `60000`. | ||
*/ | ||
heartbeat?: number; | ||
/** | ||
* A *ws* `WebSocket.Server`. | ||
* Default: `new WebSocket.Server({ noServer: true })`. | ||
*/ | ||
server?: WebSocket.Server; | ||
/** | ||
* A function to be run for each connection to provide context. | ||
*/ | ||
context?: ContextFn; | ||
/** | ||
* An error definition for route errors. | ||
*/ | ||
routeError?: RPCAdapterRouteError; | ||
} | ||
|
||
export interface RPCAdapterRouteError { | ||
name: string; | ||
type: ErrorTypeImplementation; | ||
} | ||
|
||
export type ContextFn<T extends object = object> = ( | ||
req: http.IncomingMessage, | ||
ws: WebSocket | ||
) => Promise<T> | T; |