import { OpenYoloCredential , OpenYoloCredentialHintOptions , OpenYoloCredentialRequestOptions , OpenYoloProxyLoginResponse } from './data' ; import { OpenYoloError , OpenYoloExposedErrorData } from './errors' ; import { DataValidator , isBoolean , isUndefined , isValidCredential , isValidDisplayOptions , isValidError , isValidHintOptions , isValidProxyLoginResponse , isValidRequestOptions } from './validators' ; export const enum RpcMessageType { disableAutoSignIn = 'disableAutoSignIn' , disableAutoSignInResult = 'disableAutoSignInResult' , retrieve = 'retrieve' , hintAvailable = 'hintAvailable' , hintAvailableResult = 'hintAvailableResult' , hint = 'hint' , save = 'save' , saveResult = 'saveResult' , proxy = 'proxy' , proxyResult = 'proxyResult' , showProvider = 'showProvider' , credential = 'credential' , error = 'error' , cancelLastOperation = 'cancelLastOperation' , cancelLastOperationResult = 'cancelLastOperationResult' } export const RPC_MESSAGE_TYPES = [ RpcMessageType . disableAutoSignIn , RpcMessageType . disableAutoSignInResult , RpcMessageType . retrieve , RpcMessageType . hintAvailable , RpcMessageType . hintAvailableResult , RpcMessageType . hint , RpcMessageType . save , RpcMessageType . saveResult , RpcMessageType . proxy , RpcMessageType . proxyResult , RpcMessageType . showProvider , RpcMessageType . credential , RpcMessageType . error , RpcMessageType . cancelLastOperation , RpcMessageType . cancelLastOperationResult ] ; export type RpcMessageArgumentTypes = { 'disableAutoSignIn' : undefined ; 'disableAutoSignInResult' : undefined ; 'retrieve' : OpenYoloCredentialRequestOptions ; 'hintAvailable' : OpenYoloCredentialHintOptions ; 'hintAvailableResult' : boolean ; 'hint' : OpenYoloCredentialHintOptions ; 'save' : OpenYoloCredential ; 'saveResult' : boolean ; 'proxy' : OpenYoloCredential ; 'proxyResult' : OpenYoloProxyLoginResponse ; 'showProvider' : DisplayOptions ; 'credential' : OpenYoloCredential ; 'error' : OpenYoloExposedErrorData ; 'cancelLastOperation' : undefined ; 'cancelLastOperationResult' : undefined ; } ; export interface RpcMessageData < T extends RpcMessageType > { id : string ; ack : boolean ; args : RpcMessageArgumentTypes [ T ] ; } export interface RpcMessage < T extends RpcMessageType > { type : T ; data : RpcMessageData < T > ; } function rpcDataValidator ( dataValidator ) { return ( data ) : boolean => { return ! ! data && typeof data === 'object' && 'id' in data && typeof data [ 'id' ] === 'string' && dataValidator ( data [ 'args' ] ) ; } ; } export type RpcMessageDataValidators = { [ K in RpcMessageType ] : DataValidator ; } & { [ key ] : DataValidator ; } ; export const RPC_MESSAGE_DATA_VALIDATORS = { 'disableAutoSignIn' : rpcDataValidator ( isUndefined ) , 'disableAutoSignInResult' : rpcDataValidator ( isUndefined ) , 'retrieve' : rpcDataValidator ( isValidRequestOptions ) , 'hintAvailable' : rpcDataValidator ( isValidHintOptions ) , 'hintAvailableResult' : rpcDataValidator ( isBoolean ) , 'hint' : rpcDataValidator ( isValidHintOptions ) , 'save' : rpcDataValidator ( isValidCredential ) , 'saveResult' : rpcDataValidator ( isBoolean ) , 'proxy' : rpcDataValidator ( isValidCredential ) , 'proxyResult' : rpcDataValidator ( isValidProxyLoginResponse ) , 'showProvider' : rpcDataValidator ( isValidDisplayOptions ) , 'credential' : rpcDataValidator ( isValidCredential ) , 'error' : rpcDataValidator ( isValidError ) , 'cancelLastOperation' : rpcDataValidator ( isUndefined ) , 'cancelLastOperationResult' : rpcDataValidator ( isUndefined ) } ; export interface DisplayOptions { height ? : number ; width ? : number ; } export function rpcMessage < T extends RpcMessageType > ( type , id , args ) { return { type , data : { id , args , ack : false } } ; } export function disableAutoSignInMessage ( id ) { return rpcMessage ( RpcMessageType . disableAutoSignIn , id , undefined ) ; } export function disableAutoSignInResultMessage ( id ) { return rpcMessage ( RpcMessageType . disableAutoSignInResult , id , undefined ) ; } export function retrieveMessage ( id , options ) { return rpcMessage ( RpcMessageType . retrieve , id , options ) ; } export function hintAvailableMessage ( id , options ) { return rpcMessage ( RpcMessageType . hintAvailable , id , options ) ; } export function hintAvailableResponseMessage ( id , available ) { return rpcMessage ( RpcMessageType . hintAvailableResult , id , available ) ; } export function hintMessage ( id , options ) { return rpcMessage ( RpcMessageType . hint , id , options ) ; } export function proxyLoginMessage ( id , credential ) { return rpcMessage ( RpcMessageType . proxy , id , credential ) ; } export function proxyLoginResponseMessage ( id , response ) { return rpcMessage ( RpcMessageType . proxyResult , id , response ) ; } export function credentialResultMessage ( id , credential ) { return rpcMessage ( RpcMessageType . credential , id , credential ) ; } export function showProviderMessage ( id , options ) { return rpcMessage ( RpcMessageType . showProvider , id , options ) ; } export function saveMessage ( id , credential ) { return rpcMessage ( RpcMessageType . save , id , credential ) ; } export function saveResultMessage ( id , saved ) { return rpcMessage ( RpcMessageType . saveResult , id , saved ) ; } export function errorMessage ( id , error ) { return rpcMessage ( RpcMessageType . error , id , error . toData ( ) ) ; } export function cancelLastOperationMessage ( id ) { return rpcMessage ( RpcMessageType . cancelLastOperation , id , undefined ) ; } export function cancelLastOperationResultMessage ( id ) { return rpcMessage ( RpcMessageType . cancelLastOperationResult , id , undefined ) ; }