apollo-link-webextensions-messaging
Advanced tools
Comparing version 1.0.0-rc.2 to 1.0.0-rc.3
module.exports = { | ||
preset: 'ts-jest', | ||
testEnvironment: 'node', | ||
testRegex: './src/__tests__/unit/.*\\.ts' | ||
}; |
import { Operation } from 'apollo-link'; | ||
import { ExecutionResult, GraphQLError } from 'graphql'; | ||
import { Message } from './types'; | ||
export declare type RPCNotificationMessage<TParam> = { | ||
@@ -8,3 +9,3 @@ jsonrpc: '2.0'; | ||
}; | ||
export declare const isRPCNotificationMessage: <T>(message: Record<string, unknown>) => message is RPCNotificationMessage<T>; | ||
export declare const isRPCNotificationMessage: <T>(message: Message) => message is RPCNotificationMessage<T>; | ||
export declare type OperationRequestRPC = RPCNotificationMessage<{ | ||
@@ -25,3 +26,3 @@ operationId: string; | ||
}>; | ||
export declare const isOperationRequestRPC: (message: Record<string, unknown>) => message is RPCNotificationMessage<{ | ||
export declare const isOperationRequestRPC: (message: Message) => message is RPCNotificationMessage<{ | ||
operationId: string; | ||
@@ -46,3 +47,3 @@ operationName: string; | ||
}>; | ||
export declare const isOperationResultRPC: (message: Record<string, unknown>, operationId: string) => message is RPCNotificationMessage<{ | ||
export declare const isOperationResultRPC: (message: Message, operationId: string) => message is RPCNotificationMessage<{ | ||
operationId: string; | ||
@@ -60,3 +61,3 @@ result: SerializedExecutionResult; | ||
}>; | ||
export declare const isOperationErrorRPC: (message: Record<string, unknown>, operationId: string) => message is RPCNotificationMessage<{ | ||
export declare const isOperationErrorRPC: (message: Message, operationId: string) => message is RPCNotificationMessage<{ | ||
operationId: string; | ||
@@ -72,3 +73,3 @@ errorMessage: string; | ||
}>; | ||
export declare const isOperationCompleteRPC: (message: Record<string, unknown>, operationId: string) => message is RPCNotificationMessage<{ | ||
export declare const isOperationCompleteRPC: (message: Message, operationId: string) => message is RPCNotificationMessage<{ | ||
operationId: string; | ||
@@ -83,4 +84,4 @@ }>; | ||
}>; | ||
export declare const isOperationUnsubscribeRPC: (message: Record<string, unknown>, operationId: string) => message is RPCNotificationMessage<{ | ||
export declare const isOperationUnsubscribeRPC: (message: Message, operationId: string) => message is RPCNotificationMessage<{ | ||
operationId: string; | ||
}>; |
@@ -5,2 +5,3 @@ "use strict"; | ||
exports.isRPCNotificationMessage = function (message) { | ||
//@ts-ignore object type does not allow me to check properties | ||
return message.jsonrpc === '2.0'; | ||
@@ -7,0 +8,0 @@ }; |
/// <reference types="node" /> | ||
import { EventEmitter } from 'events'; | ||
import { MessagingPort, Event, PortDisconnectEvent, PortMessageEvent } from '../types'; | ||
import { MessagingPort, Event, PortDisconnectEvent, PortMessageEvent, Message } from '../types'; | ||
export declare class EventEmitterBasedEvent<T extends Function> implements Event<T> { | ||
@@ -16,3 +16,3 @@ private ee; | ||
constructor(); | ||
postMessage(message: Record<string, unknown>): void; | ||
postMessage(message: Message): void; | ||
disconnect(): void; | ||
@@ -19,0 +19,0 @@ } |
@@ -5,8 +5,9 @@ export interface Event<T extends Function> { | ||
} | ||
export declare type Message = Record<string, unknown> | object; | ||
export declare type PortDisconnectEvent = Event<(port: MessagingPort) => void>; | ||
export declare type PortMessageEvent = Event<(message: Record<string, unknown>, port: MessagingPort) => void>; | ||
export declare type PortMessageEvent = Event<(message: Message) => void>; | ||
export interface MessagingPort { | ||
postMessage: (message: Record<string, unknown>) => void; | ||
postMessage: (message: Message) => void; | ||
onDisconnect: PortDisconnectEvent; | ||
onMessage: PortMessageEvent; | ||
} |
{ | ||
"name": "apollo-link-webextensions-messaging", | ||
"version": "1.0.0-rc.2", | ||
"version": "1.0.0-rc.3", | ||
"description": "", | ||
@@ -18,3 +18,5 @@ "main": "lib/index.js", | ||
"devDependencies": { | ||
"@types/chrome": "0.0.96", | ||
"@types/delay": "^3.1.0", | ||
"@types/firefox-webext-browser": "^70.0.1", | ||
"@types/jest": "^25.1.2", | ||
@@ -21,0 +23,0 @@ "@types/uniqid": "^4.1.3", |
@@ -17,3 +17,3 @@ import { ApolloLink, GraphQLRequest, execute, Observable } from 'apollo-link'; | ||
} from './rpcs'; | ||
import { MessagingPort } from './types'; | ||
import { MessagingPort, Message } from './types'; | ||
@@ -56,3 +56,3 @@ type CreateWebExtensionMessagingExecutorListenerOptions = { | ||
const operationOnMessageListener = (message: Record<string, unknown>): void => { | ||
const operationOnMessageListener = (message: Message): void => { | ||
if (isOperationUnsubscribeRPC(message, operationId)) { | ||
@@ -106,3 +106,3 @@ close(); | ||
const onMessageListener = (message: Record<string, unknown>): void => { | ||
const onMessageListener = (message: Message): void => { | ||
if (isOperationResultRPC(message, operationId)) { | ||
@@ -109,0 +109,0 @@ observer.next(message.params.result); |
@@ -0,3 +1,5 @@ | ||
/* eslint-disable @typescript-eslint/ban-ts-ignore */ | ||
import { Operation } from 'apollo-link'; | ||
import { print, ExecutionResult, GraphQLError } from 'graphql'; | ||
import { Message } from './types'; | ||
@@ -9,3 +11,4 @@ export type RPCNotificationMessage<TParam> = { | ||
}; | ||
export const isRPCNotificationMessage = <T>(message: Record<string, unknown>): message is RPCNotificationMessage<T> => | ||
export const isRPCNotificationMessage = <T>(message: Message): message is RPCNotificationMessage<T> => | ||
//@ts-ignore object type does not allow me to check properties | ||
message.jsonrpc === '2.0'; | ||
@@ -35,3 +38,3 @@ | ||
}); | ||
export const isOperationRequestRPC = (message: Record<string, unknown>): message is OperationRequestRPC => | ||
export const isOperationRequestRPC = (message: Message): message is OperationRequestRPC => | ||
isRPCNotificationMessage(message) && (message.method === OPERATION_REQUEST_METHOD); | ||
@@ -57,3 +60,3 @@ | ||
}); | ||
export const isOperationResultRPC = (message: Record<string, unknown>, operationId: string): message is OperationResultRPC => | ||
export const isOperationResultRPC = (message: Message, operationId: string): message is OperationResultRPC => | ||
isRPCNotificationMessage(message) && | ||
@@ -83,3 +86,3 @@ (message.method === OPERATION_RESULT_METHOD) && | ||
}; | ||
export const isOperationErrorRPC = (message: Record<string, unknown>, operationId: string): message is OperationErrorRPC => | ||
export const isOperationErrorRPC = (message: Message, operationId: string): message is OperationErrorRPC => | ||
isRPCNotificationMessage(message) && | ||
@@ -100,3 +103,3 @@ (message.method === OPERATION_ERROR_METHOD) && | ||
}); | ||
export const isOperationCompleteRPC = (message: Record<string, unknown>, operationId: string): message is OperationCompleteRPC => | ||
export const isOperationCompleteRPC = (message: Message, operationId: string): message is OperationCompleteRPC => | ||
isRPCNotificationMessage(message) && | ||
@@ -118,5 +121,5 @@ (message.method === OPERATION_COMPLETE_METHOD) && | ||
}); | ||
export const isOperationUnsubscribeRPC = (message: Record<string, unknown>, operationId: string): message is OperationUnsubscribeRPC => | ||
export const isOperationUnsubscribeRPC = (message: Message, operationId: string): message is OperationUnsubscribeRPC => | ||
isRPCNotificationMessage(message) && | ||
(message.method === OPERATION_UNSUBSCRIBE_METHOD) && | ||
(isRecord(message.params) && message.params.operationId === operationId); |
/* eslint-disable @typescript-eslint/ban-ts-ignore */ | ||
import { EventEmitter } from 'events'; | ||
import { MessagingPort, Event, PortDisconnectEvent, PortMessageEvent } from '../types'; | ||
import { MessagingPort, Event, PortDisconnectEvent, PortMessageEvent, Message } from '../types'; | ||
@@ -40,3 +40,3 @@ let ID = 0; | ||
postMessage(message: Record<string, unknown>): void { | ||
postMessage(message: Message): void { | ||
this.emit('post-message', message); | ||
@@ -43,0 +43,0 @@ } |
@@ -0,1 +1,7 @@ | ||
/* | ||
Set of minimal types that should be compatible with | ||
corresponding types in: | ||
- @types/chrome | ||
- @types/firefox-webext-browser | ||
*/ | ||
export interface Event<T extends Function> { | ||
@@ -6,9 +12,12 @@ addListener(callback: T): void; | ||
export type Message = Record<string, unknown> | object; | ||
export type PortDisconnectEvent = Event<(port: MessagingPort) => void> | ||
export type PortMessageEvent = Event<(message: Record<string, unknown>, port: MessagingPort) => void> | ||
export type PortMessageEvent = Event<(message: Message) => void> | ||
// should be compatible with chrome.runtime.Port and browser.runtime.Port | ||
export interface MessagingPort { | ||
postMessage: (message: Record<string, unknown>) => void; | ||
postMessage: (message: Message) => void; | ||
onDisconnect: PortDisconnectEvent; | ||
onMessage: PortMessageEvent; | ||
} |
43802
25
1033
17