+
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/content/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ All Packages
#title
`@webext-core/messaging`
#description
A simpler, type-safe API for sending and recieving messages.
A simpler, type-safe API for sending and receiving messages.
<br />
<br />
[Go to docs →](/messaging/installation)
Expand Down
22 changes: 11 additions & 11 deletions docs/content/messaging/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ interface BaseMessagingConfig {

Shared configuration between all the different messengers.

### Properties
### Properties

- ***`logger?: Logger`*** (default: `console`)<br/>The logger to use when logging messages. Set to `null` to disable logging.

Expand All @@ -35,7 +35,7 @@ interface CustomEventMessage {

Additional fields available on the `Message` from a `CustomEventMessenger`.

### Properties
### Properties

- ***`event: CustomEvent`***<br/>The event that was fired, resulting in the message being passed.

Expand Down Expand Up @@ -153,7 +153,7 @@ interface ExtensionMessage {

Additional fields available on the `Message` from an `ExtensionMessenger`.

### Properties
### Properties

- ***`sender: Runtime.MessageSender`***<br/>Information about where the message came from. See
[`Runtime.MessageSender`](https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/runtime/MessageSender).
Expand Down Expand Up @@ -256,7 +256,7 @@ interface Logger {
}
```

Interface used to log text to the console when sending and recieving messages.
Interface used to log text to the console when sending and receiving messages.

## `MaybePromise`

Expand All @@ -281,9 +281,9 @@ interface Message<
}
```

Contains information about the message recieved.
Contains information about the message received.

### Properties
### Properties

- ***`id: number`***<br/>A semi-unique, auto-incrementing number used to trace messages being sent.

Expand All @@ -306,7 +306,7 @@ interface MessageSender {

An object containing information about the script context that sent a message or request.

### Properties
### Properties

- ***`tab?: Tabs.Tab`***<br/>The $(ref:tabs.Tab) which opened the connection, if any. This property will <strong>only</strong>
be present when the connection was opened from a tab (including content scripts), and <strong>only</strong>
Expand All @@ -332,7 +332,7 @@ interface NamespaceMessagingConfig extends BaseMessagingConfig {
}
```

### Properties
### Properties

- ***`namespace: string`***<br/>A string used to ensure the messenger only sends messages to and listens for messages from
other messengers of the same type, with the same namespace.
Expand All @@ -354,7 +354,7 @@ Used to add a return type to a message in the protocol map.

> Internally, this is just an object with random keys for the data and return types.

### Properties
### Properties

- ***`BtVgCTPYZu: TData`***<br/>Stores the data type. Randomly named so that it isn't accidentally implemented.

Expand Down Expand Up @@ -392,7 +392,7 @@ interface SendMessageOptions {

Options for sending a message to a specific tab/frame

### Properties
### Properties

- ***`tabId: number`***<br/>The tab to send a message to

Expand Down Expand Up @@ -429,4 +429,4 @@ details.

---

_API reference generated by [`docs/generate-api-references.ts`](https://github.com/aklinker1/webext-core/blob/main/docs/generate-api-references.ts)_
_API reference generated by [`docs/generate-api-references.ts`](https://github.com/aklinker1/webext-core/blob/main/docs/generate-api-references.ts)_
21 changes: 19 additions & 2 deletions packages/messaging/src/extension.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ describe('Messenger Typing', () => {
getStringLength: (data: string) => number;
}>();

// @ts-expect-error: Requires one parameter
sendMessage('getStringLength');
sendMessage('getStringLength', 'test');
sendMessage('getStringLength', 'test', 123);

expectTypeOf(sendMessage).parameter(0).toMatchTypeOf<'getStringLength'>();
expectTypeOf(sendMessage).parameter(1).toBeString();
expectTypeOf(sendMessage).returns.resolves.toBeNumber();
Expand All @@ -66,21 +71,33 @@ describe('Messenger Typing', () => {
expectTypeOf(onMessage).parameter(1).returns.resolves.toBeNumber();
});

it('should require passing undefined to sendMessage when there is no data', () => {
it('should accept passing undefined to sendMessage when there is no data', () => {
const { sendMessage } = defineExtensionMessaging<{
ping: ProtocolWithReturn<undefined, 'pong'>;
}>();

sendMessage('ping');
sendMessage('ping', undefined);
// @ts-expect-error: It will still throw an error if you try to pass a target without sending `undefined` for the data.
sendMessage('ping', 123);
sendMessage('ping', undefined, 123);

expectTypeOf(sendMessage).parameter(0).toMatchTypeOf<'ping'>();
expectTypeOf(sendMessage).parameter(1).toBeUndefined();
expectTypeOf(sendMessage).parameter(2).toEqualTypeOf<number | undefined | SendMessageOptions>();
});

it('should require passing undefined to sendMessage when there is no arguments in a function definition', () => {
it('should accept passing undefined to sendMessage when there is no arguments in a function definition', () => {
const { sendMessage } = defineExtensionMessaging<{
ping(): 'pong';
}>();

sendMessage('ping');
sendMessage('ping', undefined);
// @ts-expect-error: It will still throw an error if you try to pass a target without sending `undefined` for the data.
sendMessage('ping', 123);
sendMessage('ping', undefined, 123);

expectTypeOf(sendMessage).parameter(0).toMatchTypeOf<'ping'>();
expectTypeOf(sendMessage).parameter(1).toBeUndefined();
expectTypeOf(sendMessage).parameter(2).toEqualTypeOf<number | undefined | SendMessageOptions>();
Expand Down
2 changes: 1 addition & 1 deletion packages/messaging/src/extension.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe('Messaging Wrapper', () => {
vi.restoreAllMocks();
});

it('should send and recieve messages', async () => {
it('should send and receive messages', async () => {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yeah, I also fixed a ton of typos lol. I could not spell "receive" correctly 😆

const { onMessage, sendMessage } = defineExtensionMessaging<ProtocolMap>();
const input = 'test';
const expected = 4;
Expand Down
16 changes: 11 additions & 5 deletions packages/messaging/src/generic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,26 @@ export interface GenericMessenger<
* @param data The data to send with the message.
* @param args Different messengers will have additional arguments to configure how a message gets sent.
*/
sendMessage<TType extends keyof TProtocolMap>(
type: TType,
...args: GetDataType<TProtocolMap[TType]> extends undefined
? [data?: undefined, ...args: TSendMessageArgs]
: never
): Promise<GetReturnType<TProtocolMap[TType]>>;
sendMessage<TType extends keyof TProtocolMap>(
type: TType,
data: GetDataType<TProtocolMap[TType]>,
...args: TSendMessageArgs
): Promise<GetReturnType<TProtocolMap[TType]>>;

/**
* Trigger a callback when a message of the requested type is recieved. You cannot setup multiple
* Trigger a callback when a message of the requested type is received. You cannot setup multiple
* listeners for the same message type in the same JS context.
*
* To remove the listener, call the returned message.
*
* @param type The message type to listen for. Call `sendMessage` with the same type to triggern this listener.
* @param onReceived The callback executed when a message is recieved.
* @param type The message type to listen for. Call `sendMessage` with the same type to trigger this listener.
* @param onReceived The callback executed when a message is received.
*/
onMessage<TType extends keyof TProtocolMap>(
type: TType,
Expand Down Expand Up @@ -107,13 +113,13 @@ export function defineGenericMessanging<
return {
async sendMessage<TType extends keyof TProtocolMap>(
type: TType,
data: TProtocolMap[TType],
data?: TProtocolMap[TType],
...args: TSendMessageArgs
): Promise<any> {
const _message: Message<TProtocolMap, TType> = {
id: getNextId(),
type: type,
data,
data: data as any,
timestamp: Date.now(),
};
const message = (await config.verifyMessageData?.(_message)) ?? _message;
Expand Down
4 changes: 2 additions & 2 deletions packages/messaging/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Interface used to log text to the console when sending and recieving messages.
* Interface used to log text to the console when sending and receiving messages.
*/
export interface Logger {
debug(...args: any[]): void;
Expand Down Expand Up @@ -95,7 +95,7 @@ export interface NamespaceMessagingConfig extends BaseMessagingConfig {
}

/**
* Contains information about the message recieved.
* Contains information about the message received.
*/
export interface Message<
TProtocolMap extends Record<string, any>,
Expand Down
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载