Skip to content

Commit

Permalink
feat(rpc-adapter): adds option types and defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
rafamel committed Oct 25, 2019
1 parent e0fdf38 commit c61398b
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 2 deletions.
7 changes: 5 additions & 2 deletions packages/rpc-adapter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,19 @@
"@riseup/library": "^0.4.0",
"@riseup/tooling": "^0.4.0",
"@types/jest": "^24.0.13",
"@types/ws": "^6.0.3",
"@zerollup/ts-transform-paths": "^1.7.3",
"kpo": "^0.11.0",
"onchange": "^6.0.0",
"rxjs": "^6.5.3",
"typescript": "^3.6.4"
"typescript": "^3.6.4",
"ws": "^7.1.2"
},
"dependencies": {},
"peerDependencies": {
"@karmic/core": "0.0.0",
"rxjs": "6.x"
"rxjs": "6.x",
"ws": "^7.x"
},
"@pika/pack": {
"pipeline": [
Expand Down
17 changes: 17 additions & 0 deletions packages/rpc-adapter/src/defaults.ts
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' })
}
};
}
1 change: 1 addition & 0 deletions packages/rpc-adapter/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './options';
export * from './rpc';
57 changes: 57 additions & 0 deletions packages/rpc-adapter/src/types/options.ts
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;

0 comments on commit c61398b

Please sign in to comment.