Skip to content

Commit 1492fee

Browse files
committed
add tests for async and simplified return type for async functions
1 parent bafb6bc commit 1492fee

File tree

2 files changed

+56
-4
lines changed

2 files changed

+56
-4
lines changed

types/mokapi/index.d.ts

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ export interface EventHandler {
122122
* })
123123
* }
124124
*/
125-
export type HttpEventHandler = (request: HttpRequest, response: HttpResponse) => void | Promise<void>;
125+
export type HttpEventHandler = (request: HttpRequest, response: HttpResponse) => void;
126126

127127
/**
128128
* HttpRequest is an object used by HttpEventHandler that contains request-specific
@@ -204,7 +204,7 @@ export interface Url {
204204
* })
205205
* }
206206
*/
207-
export type KafkaEventHandler = (message: KafkaEventMessage) => void | Promise<void>;
207+
export type KafkaEventHandler = (message: KafkaEventMessage) => void;
208208

209209
/**
210210
* KafkaEventMessage is an object used by KafkaEventHandler that contains Kafka-specific message data.
@@ -243,7 +243,7 @@ export interface KafkaEventMessage {
243243
* })
244244
* }
245245
*/
246-
export type LdapEventHandler = (request: LdapSearchRequest, response: LdapSearchResponse) => void | Promise<void>;
246+
export type LdapEventHandler = (request: LdapSearchRequest, response: LdapSearchResponse) => void;
247247

248248
/**
249249
* LdapSearchRequest is an object used by LdapEventHandler that contains request-specific data.
@@ -343,7 +343,7 @@ export enum LdapResultStatus {
343343
SizeLimitExceeded = 4,
344344
}
345345

346-
export type SmtpEventHandler = (record: SmtpEventMessage) => void | Promise<void>;
346+
export type SmtpEventHandler = (record: SmtpEventMessage) => void;
347347

348348
export interface SmtpEventMessage {
349349
server: string;
@@ -456,8 +456,57 @@ export interface JSONObject {
456456
[key: string]: JSONValue;
457457
}
458458

459+
/**
460+
* Specifies the date-time format defined in [RFC 3339](https://datatracker.ietf.org/doc/html/rfc3339).
461+
* This constant can be used when defining or validating datetime strings.
462+
*
463+
* @example
464+
* const date = new Date().toISOString()
465+
* if (isValidDate(date, RFC3339)) {
466+
* // do something
467+
* }
468+
*/
459469
export const RFC3339 = "RFC3339";
460470

471+
/**
472+
* Applies a patch object to a target object. Only properties that are explicitly defined in the patch
473+
* are applied. This includes nested objects. Properties marked with `Delete` will be removed.
474+
*
475+
* This function is especially useful when working with generated mock data in Mokapi that you want to override
476+
* or refine with specific values.
477+
*
478+
* https://mokapi.io/docs/javascript-api/mokapi/patch
479+
*
480+
* @param target The original object or value to be patched.
481+
* @param patch The patch object or value. Only defined values are applied; undefined values are ignored. Use `Delete` to remove fields.
482+
* @returns A new object or value with the patch applied.
483+
*
484+
* @example
485+
* const result = patch({ name: "foo", age: 42 }, { name: "bar" })
486+
* // result: { name: "bar", age: 42 }
487+
*
488+
* @example
489+
* const result = patch({ name: "foo", meta: { version: 1 } }, { meta: { version: 2 } })
490+
* // result: { name: "foo", meta: { version: 2 } }
491+
*
492+
* @example
493+
* const result = patch({ name: "foo", age: 42 }, { age: Delete })
494+
* // result: { name: "foo" }
495+
*/
461496
export function patch(target: any, patch: any): any;
462497

498+
/**
499+
* Special marker used with the `patch` function to indicate a property should be removed.
500+
*
501+
* When used as a value inside a patch object, the corresponding property will be deleted
502+
* from the result.
503+
*
504+
* This is useful when refining or overriding mock data in a script while keeping validation logic intact.
505+
*
506+
* https://mokapi.io/docs/javascript-api/mokapi/patch#delete
507+
*
508+
* @example
509+
* const result = patch({ name: "foo", age: 42 }, { age: Delete })
510+
* // result: { name: "foo" }
511+
*/
463512
export const Delete: unique symbol;

types/mokapi/test/mokapi-tests.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ on("ldap", (req: LdapSearchRequest, res: LdapSearchResponse) => {});
4444
on("http", handler, "");
4545
on("http", handler, {});
4646
on("http", handler, { tags: { foo: "bar" } });
47+
on("http", async () => {});
4748

4849
// @ts-expect-error
4950
every(12, () => {});
@@ -58,6 +59,7 @@ every("12m", () => {}, { times: 3 });
5859
// @ts-expect-error
5960
every("12m", () => {}, { runFirstTimeImmediately: 1 });
6061
every("12m", () => {}, { runFirstTimeImmediately: true });
62+
every('', async () => {})
6163

6264
// @ts-expect-error
6365
cron(12, () => {});
@@ -72,6 +74,7 @@ cron("12m", () => {}, { times: 3 });
7274
// @ts-expect-error
7375
cron("12m", () => {}, { runFirstTimeImmediately: 1 });
7476
cron("12m", () => {}, { runFirstTimeImmediately: true });
77+
cron('', async () => {});
7578

7679
// @ts-expect-error
7780
env(12);

0 commit comments

Comments
 (0)