Skip to content

Commit 4ba5691

Browse files
committed
Update tests, add labels for better editor hints
1 parent 7bf99ca commit 4ba5691

File tree

6 files changed

+40
-23
lines changed

6 files changed

+40
-23
lines changed

docs/content/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ All Packages
6060
#title
6161
`@webext-core/messaging`
6262
#description
63-
A simpler, type-safe API for sending and recieving messages.
63+
A simpler, type-safe API for sending and receiving messages.
6464
<br />
6565
<br />
6666
[Go to docs →](/messaging/installation)

docs/content/messaging/api.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ interface BaseMessagingConfig {
1919

2020
Shared configuration between all the different messengers.
2121

22-
### Properties
22+
### Properties
2323

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

@@ -35,7 +35,7 @@ interface CustomEventMessage {
3535

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

38-
### Properties
38+
### Properties
3939

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

@@ -153,7 +153,7 @@ interface ExtensionMessage {
153153

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

156-
### Properties
156+
### Properties
157157

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

259-
Interface used to log text to the console when sending and recieving messages.
259+
Interface used to log text to the console when sending and receiving messages.
260260

261261
## `MaybePromise`
262262

@@ -281,9 +281,9 @@ interface Message<
281281
}
282282
```
283283

284-
Contains information about the message recieved.
284+
Contains information about the message received.
285285

286-
### Properties
286+
### Properties
287287

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

@@ -306,7 +306,7 @@ interface MessageSender {
306306

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

309-
### Properties
309+
### Properties
310310

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

335-
### Properties
335+
### Properties
336336

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

355355
> Internally, this is just an object with random keys for the data and return types.
356356
357-
### Properties
357+
### Properties
358358

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

@@ -392,7 +392,7 @@ interface SendMessageOptions {
392392

393393
Options for sending a message to a specific tab/frame
394394

395-
### Properties
395+
### Properties
396396

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

@@ -429,4 +429,4 @@ details.
429429

430430
---
431431

432-
_API reference generated by [`docs/generate-api-references.ts`](https://github.com/aklinker1/webext-core/blob/main/docs/generate-api-references.ts)_
432+
_API reference generated by [`docs/generate-api-references.ts`](https://github.com/aklinker1/webext-core/blob/main/docs/generate-api-references.ts)_

packages/messaging/src/extension.test-d.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ describe('Messenger Typing', () => {
5858
getStringLength: (data: string) => number;
5959
}>();
6060

61+
// @ts-expect-error: Requires one parameter
62+
sendMessage('getStringLength');
63+
sendMessage('getStringLength', 'test');
64+
sendMessage('getStringLength', 'test', 123);
65+
6166
expectTypeOf(sendMessage).parameter(0).toMatchTypeOf<'getStringLength'>();
6267
expectTypeOf(sendMessage).parameter(1).toBeString();
6368
expectTypeOf(sendMessage).returns.resolves.toBeNumber();
@@ -66,21 +71,33 @@ describe('Messenger Typing', () => {
6671
expectTypeOf(onMessage).parameter(1).returns.resolves.toBeNumber();
6772
});
6873

69-
it('should require passing undefined to sendMessage when there is no data', () => {
74+
it('should accept passing undefined to sendMessage when there is no data', () => {
7075
const { sendMessage } = defineExtensionMessaging<{
7176
ping: ProtocolWithReturn<undefined, 'pong'>;
7277
}>();
7378

79+
sendMessage('ping');
80+
sendMessage('ping', undefined);
81+
// @ts-expect-error: It will still throw an error if you try to pass a target without sending `undefined` for the data.
82+
sendMessage('ping', 123);
83+
sendMessage('ping', undefined, 123);
84+
7485
expectTypeOf(sendMessage).parameter(0).toMatchTypeOf<'ping'>();
7586
expectTypeOf(sendMessage).parameter(1).toBeUndefined();
7687
expectTypeOf(sendMessage).parameter(2).toEqualTypeOf<number | undefined | SendMessageOptions>();
7788
});
7889

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

95+
sendMessage('ping');
96+
sendMessage('ping', undefined);
97+
// @ts-expect-error: It will still throw an error if you try to pass a target without sending `undefined` for the data.
98+
sendMessage('ping', 123);
99+
sendMessage('ping', undefined, 123);
100+
84101
expectTypeOf(sendMessage).parameter(0).toMatchTypeOf<'ping'>();
85102
expectTypeOf(sendMessage).parameter(1).toBeUndefined();
86103
expectTypeOf(sendMessage).parameter(2).toEqualTypeOf<number | undefined | SendMessageOptions>();

packages/messaging/src/extension.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ describe('Messaging Wrapper', () => {
2323
vi.restoreAllMocks();
2424
});
2525

26-
it('should send and recieve messages', async () => {
26+
it('should send and receive messages', async () => {
2727
const { onMessage, sendMessage } = defineExtensionMessaging<ProtocolMap>();
2828
const input = 'test';
2929
const expected = 4;

packages/messaging/src/generic.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export interface GenericMessenger<
5555
sendMessage<TType extends keyof TProtocolMap>(
5656
type: TType,
5757
...args: GetDataType<TProtocolMap[TType]> extends undefined
58-
? [undefined?, ...TSendMessageArgs]
58+
? [data?: undefined, ...args: TSendMessageArgs]
5959
: never
6060
): Promise<GetReturnType<TProtocolMap[TType]>>;
6161
sendMessage<TType extends keyof TProtocolMap>(
@@ -65,13 +65,13 @@ export interface GenericMessenger<
6565
): Promise<GetReturnType<TProtocolMap[TType]>>;
6666

6767
/**
68-
* Trigger a callback when a message of the requested type is recieved. You cannot setup multiple
68+
* Trigger a callback when a message of the requested type is received. You cannot setup multiple
6969
* listeners for the same message type in the same JS context.
7070
*
7171
* To remove the listener, call the returned message.
7272
*
73-
* @param type The message type to listen for. Call `sendMessage` with the same type to triggern this listener.
74-
* @param onReceived The callback executed when a message is recieved.
73+
* @param type The message type to listen for. Call `sendMessage` with the same type to trigger this listener.
74+
* @param onReceived The callback executed when a message is received.
7575
*/
7676
onMessage<TType extends keyof TProtocolMap>(
7777
type: TType,
@@ -113,13 +113,13 @@ export function defineGenericMessanging<
113113
return {
114114
async sendMessage<TType extends keyof TProtocolMap>(
115115
type: TType,
116-
data: TProtocolMap[TType],
116+
data?: TProtocolMap[TType],
117117
...args: TSendMessageArgs
118118
): Promise<any> {
119119
const _message: Message<TProtocolMap, TType> = {
120120
id: getNextId(),
121121
type: type,
122-
data,
122+
data: data as any,
123123
timestamp: Date.now(),
124124
};
125125
const message = (await config.verifyMessageData?.(_message)) ?? _message;

packages/messaging/src/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Interface used to log text to the console when sending and recieving messages.
2+
* Interface used to log text to the console when sending and receiving messages.
33
*/
44
export interface Logger {
55
debug(...args: any[]): void;
@@ -95,7 +95,7 @@ export interface NamespaceMessagingConfig extends BaseMessagingConfig {
9595
}
9696

9797
/**
98-
* Contains information about the message recieved.
98+
* Contains information about the message received.
9999
*/
100100
export interface Message<
101101
TProtocolMap extends Record<string, any>,

0 commit comments

Comments
 (0)