feat: make Context platform specific, CoreContext as Core

This commit is contained in:
Jacob Nguyen
2023-04-27 13:23:16 -05:00
parent e6f9207a47
commit 7f59f81b35
3 changed files with 27 additions and 17 deletions

View File

@@ -1,18 +1,31 @@
import { Result as Either } from 'ts-results-es';
import { Result as Either, Ok, Err } from 'ts-results-es';
export interface Context<Left, Right> {
get message(): Left;
get interaction(): Right;
/**
*
* @since 3.0.0
*
*/
export interface CoreContext<M, I> {
get message(): M;
get interaction(): I;
}
function safeUnwrap<T>(res: Either<T, T>) {
export function safeUnwrap<T>(res: Either<T, T>) {
return res.val;
}
export function wrap<Left, Right>(
val: Left|Right,
fa: (val: Left|Right) => Either<Left, Right>
) {
return fa(val);
export function wrap<A extends unknown, B extends unknown>(
val: B|A,
): Either<A,B> {
if(typeof val !== 'object' || Array.isArray(val) || val == null) {
throw Error("Could not form correct Context." + "Recieved " + val)
}
const msgInteractionObject = (val as unknown as { interaction: unknown }).interaction;
//falsy comparison: ==. Checks if its null OR undefined
if(msgInteractionObject == null) {
return Ok(val as A)
}
return Err(val as B);
}

View File

@@ -1,6 +1,3 @@
import { Context } from './context';
import type Wrapper from './wrapper';
import { ModuleStore } from './moduleStore';
export * from './errors';
export * from './enums';
export { Context, Wrapper, ModuleStore };
export * from './moduleStore';

View File

@@ -1,5 +1,5 @@
import type { ServerlessDependencies, WebsocketDependencies } from '../../types/handler';
import { DispatchType, ServerlessStrategy, WebsocketStrategy } from '../platform/strategy';
import { DispatchType, ServerlessStrategy, WebsocketStrategy } from '../platform';
export interface WebsocketWrapper {
@@ -19,7 +19,7 @@ export interface WebsocketWrapper {
* @deprecated
* Type alias for WebsocketWrapper
*/
export type Wrapper = WebsocketWrapper
export type Wrapper = WebsocketWrapper | ServerlessWrapper
export interface ServerlessWrapper {
readonly platform: ServerlessStrategy