@geekmidas/testkit 10.0.0-alpha.155 → 10.0.0-alpha.156

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -809,6 +809,29 @@ function memoryAdapter(config?: {
809
809
 
810
810
  We welcome contributions! Please see our [Contributing Guide](../../CONTRIBUTING.md) for details.
811
811
 
812
+ ## Optional peers
813
+
814
+ <!-- optional-peers:start -->
815
+
816
+ <!-- Generated by `node scripts/check-optional-peers.mjs --write-docs`; CI fails when it drifts. -->
817
+
818
+ Each of these entries imports an optional peer when it loads, so using it means installing that peer:
819
+
820
+ | Entry | Install |
821
+ | --- | --- |
822
+ | `@geekmidas/testkit/aws` | `vitest` |
823
+ | `@geekmidas/testkit/better-auth` | `better-auth` |
824
+ | `@geekmidas/testkit/kysely` | `kysely`, `pg` |
825
+ | `@geekmidas/testkit/logger` | `vitest` |
826
+ | `@geekmidas/testkit/objection` | `pg` |
827
+ | `@geekmidas/testkit/os` | `vitest` |
828
+ | `@geekmidas/testkit/postgres` | `pg` |
829
+ | `@geekmidas/testkit/transactions` | `kysely`, `pg` |
830
+
831
+ No other entry imports an optional peer when it loads. `@geekmidas/emailkit`, `@types/aws-lambda`, `db-errors`, `knex`, `objection` are loaded only when a feature that uses one runs (an `import()` inside a `try`, whose error names the package to install), or named only in types.
832
+
833
+ <!-- optional-peers:end -->
834
+
812
835
  ## License
813
836
 
814
837
  This project is licensed under the MIT License - see the [LICENSE](../../LICENSE) file for details.
package/dist/mailbox.cjs CHANGED
@@ -42,8 +42,13 @@ async function json(response, inbox) {
42
42
  }
43
43
  /** The message, through the one MIME parser everything that reads mail uses. */
44
44
  async function email(id, raw) {
45
- const { parseEmail } = await import("@geekmidas/emailkit/inbound");
46
- const message = await parseEmail(raw);
45
+ let inbound;
46
+ try {
47
+ inbound = await import("@geekmidas/emailkit/inbound");
48
+ } catch (cause) {
49
+ throw new MailboxNeedsEmailkit(cause);
50
+ }
51
+ const message = await inbound.parseEmail(raw);
47
52
  const html = message.html ?? "";
48
53
  const text = message.text ?? "";
49
54
  const links = [...new Set([...html.matchAll(/href="(https?:\/\/[^"]+)"/g), ...text.matchAll(/(https?:\/\/[^\s<>"]+)/g)].map(([, url]) => decodeEntities(url)))];
@@ -75,6 +80,13 @@ var NoMail = class extends Error {
75
80
  this.name = "NoMail";
76
81
  }
77
82
  };
83
+ /** Reading mail needs `@geekmidas/emailkit`, and it is not installed. */
84
+ var MailboxNeedsEmailkit = class extends Error {
85
+ constructor(cause) {
86
+ super("Reading mail from the mailbox parses it with @geekmidas/emailkit, which could not be loaded. Add it to the dev dependencies of the app whose mail the test reads.", { cause });
87
+ this.name = "MailboxNeedsEmailkit";
88
+ }
89
+ };
78
90
  /** Mailpit did not answer. */
79
91
  var InboxUnreachable = class extends Error {
80
92
  inbox;
@@ -88,6 +100,7 @@ var InboxUnreachable = class extends Error {
88
100
  };
89
101
  //#endregion
90
102
  exports.InboxUnreachable = InboxUnreachable;
103
+ exports.MailboxNeedsEmailkit = MailboxNeedsEmailkit;
91
104
  exports.NoMail = NoMail;
92
105
  exports.createMailbox = createMailbox;
93
106
 
@@ -1 +1 @@
1
- {"version":3,"file":"mailbox.cjs","names":[],"sources":["../src/mailbox.ts"],"sourcesContent":["/**\n * The mail a test sent, read from Mailpit.\n *\n * Locally, mail goes over real SMTP to Mailpit — the same client, the same\n * message, only the host differs. A test reads what arrived through Mailpit's\n * HTTP API, so a magic link is signed in with by opening the email that was\n * actually sent rather than a token pulled from somewhere else.\n *\n * Mailpit has one inbox for everything, and transactions do not reach it, so a\n * test keeps to its own mail by address: a unique recipient per test, read by\n * `to:`, and cleared afterwards.\n */\n\nimport type { ReceivedAttachment } from '@geekmidas/emailkit/inbound';\n\n/** How long `last()` waits for mail that is still on its way. */\nconst DEFAULT_TIMEOUT = 5_000;\nconst POLL_INTERVAL = 50;\n\nexport interface MailboxOptions {\n\t/** Mailpit's HTTP address — what `gkm test` injects as `<ID>_INBOX_URL`. */\n\tinbox: string;\n\t/** The `fetch` to read it with. Defaults to the global one. */\n\tfetch?: typeof fetch;\n\t/** How long `last()` waits for mail to arrive. */\n\ttimeout?: number;\n}\n\n/** One email, as it arrived. */\nexport interface Email {\n\tid: string;\n\tfrom: string;\n\tto: string[];\n\tsubject: string;\n\ttext: string;\n\thtml: string;\n\t/** Every header, by lower-case name, as the parser an inbox uses reads them. */\n\theaders: Readonly<Record<string, readonly string[]>>;\n\t/** Every `http(s)` link in the email, in order. */\n\tlinks: string[];\n\t/** The first link — the one a sign-in or confirmation email is about. */\n\tlink: string | undefined;\n\t/**\n\t * Its files, read from the raw message by the parser an inbox's handler\n\t * uses — so what survives one survives the other.\n\t */\n\tattachments: ReceivedAttachment[];\n}\n\nexport interface Mailbox {\n\t/** The newest email to this address, waiting for one if none has arrived. */\n\tlast(): Promise<Email>;\n\t/** Every email to this address, newest first. */\n\tall(): Promise<Email[]>;\n\t/** Delete every email to this address. */\n\tclear(): Promise<void>;\n}\n\n/** A way to read the mail sent to one address. */\nexport function createMailbox(\n\toptions: MailboxOptions,\n): (address: string) => Mailbox {\n\tconst inbox = options.inbox.replace(/\\/$/, '');\n\tconst timeout = options.timeout ?? DEFAULT_TIMEOUT;\n\tconst get = (path: string, init?: RequestInit) =>\n\t\t(options.fetch ?? globalThis.fetch)(`${inbox}${path}`, init);\n\n\treturn (address) => {\n\t\tconst query = `?query=${encodeURIComponent(`to:\"${address}\"`)}`;\n\n\t\tconst all = async (): Promise<Email[]> => {\n\t\t\tconst found = await json<SearchResult>(\n\t\t\t\tawait get(`/api/v1/search${query}`),\n\t\t\t\tinbox,\n\t\t\t);\n\t\t\treturn Promise.all(\n\t\t\t\tfound.messages.map(async ({ ID }) => {\n\t\t\t\t\tconst response = await get(`/api/v1/message/${ID}/raw`);\n\t\t\t\t\tif (!response.ok) {\n\t\t\t\t\t\tthrow new InboxUnreachable(inbox, response.status);\n\t\t\t\t\t}\n\t\t\t\t\treturn email(ID, new Uint8Array(await response.arrayBuffer()));\n\t\t\t\t}),\n\t\t\t);\n\t\t};\n\n\t\treturn {\n\t\t\tall,\n\t\t\tasync last() {\n\t\t\t\tconst deadline = Date.now() + timeout;\n\t\t\t\tfor (;;) {\n\t\t\t\t\tconst [newest] = await all();\n\t\t\t\t\tif (newest) return newest;\n\t\t\t\t\tif (Date.now() >= deadline) throw new NoMail(address, timeout);\n\t\t\t\t\tawait new Promise((resolve) => setTimeout(resolve, POLL_INTERVAL));\n\t\t\t\t}\n\t\t\t},\n\t\t\tasync clear() {\n\t\t\t\tconst response = await get(`/api/v1/search${query}`, {\n\t\t\t\t\tmethod: 'DELETE',\n\t\t\t\t});\n\t\t\t\tif (!response.ok) {\n\t\t\t\t\tthrow new InboxUnreachable(inbox, response.status);\n\t\t\t\t}\n\t\t\t},\n\t\t};\n\t};\n}\n\ninterface SearchResult {\n\tmessages: { ID: string }[];\n}\n\nasync function json<T>(response: Response, inbox: string): Promise<T> {\n\tif (!response.ok) throw new InboxUnreachable(inbox, response.status);\n\treturn (await response.json()) as T;\n}\n\n/** The message, through the one MIME parser everything that reads mail uses. */\nasync function email(id: string, raw: Uint8Array): Promise<Email> {\n\tconst { parseEmail } = await import('@geekmidas/emailkit/inbound');\n\tconst message = await parseEmail(raw);\n\tconst html = message.html ?? '';\n\tconst text = message.text ?? '';\n\tconst links = [\n\t\t...new Set(\n\t\t\t[\n\t\t\t\t...html.matchAll(/href=\"(https?:\\/\\/[^\"]+)\"/g),\n\t\t\t\t...text.matchAll(/(https?:\\/\\/[^\\s<>\"]+)/g),\n\t\t\t].map(([, url]) => decodeEntities(url!)),\n\t\t),\n\t];\n\n\treturn {\n\t\tid,\n\t\tfrom: message.from?.address ?? '',\n\t\tto: message.to.map(({ address }) => address),\n\t\tsubject: message.subject ?? '',\n\t\ttext,\n\t\thtml,\n\t\theaders: message.headers,\n\t\tlinks,\n\t\tlink: links[0],\n\t\tattachments: message.attachments,\n\t};\n}\n\n/** `&amp;` in an HTML attribute is `&` in the URL it names. */\nfunction decodeEntities(url: string): string {\n\treturn url.replaceAll('&amp;', '&');\n}\n\n/** No email to an address arrived in time. */\nexport class NoMail extends Error {\n\tconstructor(\n\t\treadonly address: string,\n\t\treadonly timeout: number,\n\t) {\n\t\tsuper(\n\t\t\t`No email to ${address} arrived within ${timeout}ms. Check that the ` +\n\t\t\t\t`code under test sends it, and to that address.`,\n\t\t);\n\t\tthis.name = 'NoMail';\n\t}\n}\n\n/** Mailpit did not answer. */\nexport class InboxUnreachable extends Error {\n\tconstructor(\n\t\treadonly inbox: string,\n\t\treadonly status: number,\n\t) {\n\t\tsuper(\n\t\t\t`Mailpit at ${inbox} answered ${status}. Is it running? \\`gkm test\\` ` +\n\t\t\t\t`starts it when the app declares an Email construct.`,\n\t\t);\n\t\tthis.name = 'InboxUnreachable';\n\t}\n}\n"],"mappings":";;;AAgBA,MAAM,kBAAkB;AACxB,MAAM,gBAAgB;;AA0CtB,SAAgB,cACf,SAC+B;CAC/B,MAAM,QAAQ,QAAQ,MAAM,QAAQ,OAAO,EAAE;CAC7C,MAAM,UAAU,QAAQ,WAAW;CACnC,MAAM,OAAO,MAAc,UACzB,QAAQ,SAAS,WAAW,MAAA,CAAO,GAAG,QAAQ,QAAQ,IAAI;CAE5D,QAAQ,YAAY;EACnB,MAAM,QAAQ,UAAU,mBAAmB,OAAO,QAAQ,EAAE;EAE5D,MAAM,MAAM,YAA8B;GACzC,MAAM,QAAQ,MAAM,KACnB,MAAM,IAAI,iBAAiB,OAAO,GAClC,KACD;GACA,OAAO,QAAQ,IACd,MAAM,SAAS,IAAI,OAAO,EAAE,SAAS;IACpC,MAAM,WAAW,MAAM,IAAI,mBAAmB,GAAG,KAAK;IACtD,IAAI,CAAC,SAAS,IACb,MAAM,IAAI,iBAAiB,OAAO,SAAS,MAAM;IAElD,OAAO,MAAM,IAAI,IAAI,WAAW,MAAM,SAAS,YAAY,CAAC,CAAC;GAC9D,CAAC,CACF;EACD;EAEA,OAAO;GACN;GACA,M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;CAFV,YACC,SACA,SACC;EACD,MACC,eAAe,QAAQ,kBAAkB,QAAQ,kEAElD;EANS,KAAA,UAAA;EACA,KAAA,UAAA;EAMT,KAAK,OAAO;CACb;AACD;;AAGA,IAAa,mBAAb,cAAsC,MAAM;CAEjC;CACA;CAFV,YACC,OACA,QACC;EACD,MACC,cAAc,MAAM,YAAY,OAAO,kFAExC;EANS,KAAA,QAAA;EACA,KAAA,SAAA;EAMT,KAAK,OAAO;CACb;AACD"}
1
+ {"version":3,"file":"mailbox.cjs","names":[],"sources":["../src/mailbox.ts"],"sourcesContent":["/**\n * The mail a test sent, read from Mailpit.\n *\n * Locally, mail goes over real SMTP to Mailpit — the same client, the same\n * message, only the host differs. A test reads what arrived through Mailpit's\n * HTTP API, so a magic link is signed in with by opening the email that was\n * actually sent rather than a token pulled from somewhere else.\n *\n * Mailpit has one inbox for everything, and transactions do not reach it, so a\n * test keeps to its own mail by address: a unique recipient per test, read by\n * `to:`, and cleared afterwards.\n */\n\nimport type { ReceivedAttachment } from '@geekmidas/emailkit/inbound';\n\n/** How long `last()` waits for mail that is still on its way. */\nconst DEFAULT_TIMEOUT = 5_000;\nconst POLL_INTERVAL = 50;\n\nexport interface MailboxOptions {\n\t/** Mailpit's HTTP address — what `gkm test` injects as `<ID>_INBOX_URL`. */\n\tinbox: string;\n\t/** The `fetch` to read it with. Defaults to the global one. */\n\tfetch?: typeof fetch;\n\t/** How long `last()` waits for mail to arrive. */\n\ttimeout?: number;\n}\n\n/** One email, as it arrived. */\nexport interface Email {\n\tid: string;\n\tfrom: string;\n\tto: string[];\n\tsubject: string;\n\ttext: string;\n\thtml: string;\n\t/** Every header, by lower-case name, as the parser an inbox uses reads them. */\n\theaders: Readonly<Record<string, readonly string[]>>;\n\t/** Every `http(s)` link in the email, in order. */\n\tlinks: string[];\n\t/** The first link — the one a sign-in or confirmation email is about. */\n\tlink: string | undefined;\n\t/**\n\t * Its files, read from the raw message by the parser an inbox's handler\n\t * uses — so what survives one survives the other.\n\t */\n\tattachments: ReceivedAttachment[];\n}\n\nexport interface Mailbox {\n\t/** The newest email to this address, waiting for one if none has arrived. */\n\tlast(): Promise<Email>;\n\t/** Every email to this address, newest first. */\n\tall(): Promise<Email[]>;\n\t/** Delete every email to this address. */\n\tclear(): Promise<void>;\n}\n\n/** A way to read the mail sent to one address. */\nexport function createMailbox(\n\toptions: MailboxOptions,\n): (address: string) => Mailbox {\n\tconst inbox = options.inbox.replace(/\\/$/, '');\n\tconst timeout = options.timeout ?? DEFAULT_TIMEOUT;\n\tconst get = (path: string, init?: RequestInit) =>\n\t\t(options.fetch ?? globalThis.fetch)(`${inbox}${path}`, init);\n\n\treturn (address) => {\n\t\tconst query = `?query=${encodeURIComponent(`to:\"${address}\"`)}`;\n\n\t\tconst all = async (): Promise<Email[]> => {\n\t\t\tconst found = await json<SearchResult>(\n\t\t\t\tawait get(`/api/v1/search${query}`),\n\t\t\t\tinbox,\n\t\t\t);\n\t\t\treturn Promise.all(\n\t\t\t\tfound.messages.map(async ({ ID }) => {\n\t\t\t\t\tconst response = await get(`/api/v1/message/${ID}/raw`);\n\t\t\t\t\tif (!response.ok) {\n\t\t\t\t\t\tthrow new InboxUnreachable(inbox, response.status);\n\t\t\t\t\t}\n\t\t\t\t\treturn email(ID, new Uint8Array(await response.arrayBuffer()));\n\t\t\t\t}),\n\t\t\t);\n\t\t};\n\n\t\treturn {\n\t\t\tall,\n\t\t\tasync last() {\n\t\t\t\tconst deadline = Date.now() + timeout;\n\t\t\t\tfor (;;) {\n\t\t\t\t\tconst [newest] = await all();\n\t\t\t\t\tif (newest) return newest;\n\t\t\t\t\tif (Date.now() >= deadline) throw new NoMail(address, timeout);\n\t\t\t\t\tawait new Promise((resolve) => setTimeout(resolve, POLL_INTERVAL));\n\t\t\t\t}\n\t\t\t},\n\t\t\tasync clear() {\n\t\t\t\tconst response = await get(`/api/v1/search${query}`, {\n\t\t\t\t\tmethod: 'DELETE',\n\t\t\t\t});\n\t\t\t\tif (!response.ok) {\n\t\t\t\t\tthrow new InboxUnreachable(inbox, response.status);\n\t\t\t\t}\n\t\t\t},\n\t\t};\n\t};\n}\n\ninterface SearchResult {\n\tmessages: { ID: string }[];\n}\n\nasync function json<T>(response: Response, inbox: string): Promise<T> {\n\tif (!response.ok) throw new InboxUnreachable(inbox, response.status);\n\treturn (await response.json()) as T;\n}\n\n/** The message, through the one MIME parser everything that reads mail uses. */\nasync function email(id: string, raw: Uint8Array): Promise<Email> {\n\t// emailkit is an optional peer: loaded when mail is read, inside a `try`\n\t// so a bundled test harness for an app that sends none leaves it alone.\n\tlet inbound: typeof import('@geekmidas/emailkit/inbound');\n\ttry {\n\t\tinbound = await import('@geekmidas/emailkit/inbound');\n\t} catch (cause) {\n\t\tthrow new MailboxNeedsEmailkit(cause);\n\t}\n\tconst message = await inbound.parseEmail(raw);\n\tconst html = message.html ?? '';\n\tconst text = message.text ?? '';\n\tconst links = [\n\t\t...new Set(\n\t\t\t[\n\t\t\t\t...html.matchAll(/href=\"(https?:\\/\\/[^\"]+)\"/g),\n\t\t\t\t...text.matchAll(/(https?:\\/\\/[^\\s<>\"]+)/g),\n\t\t\t].map(([, url]) => decodeEntities(url!)),\n\t\t),\n\t];\n\n\treturn {\n\t\tid,\n\t\tfrom: message.from?.address ?? '',\n\t\tto: message.to.map(({ address }) => address),\n\t\tsubject: message.subject ?? '',\n\t\ttext,\n\t\thtml,\n\t\theaders: message.headers,\n\t\tlinks,\n\t\tlink: links[0],\n\t\tattachments: message.attachments,\n\t};\n}\n\n/** `&amp;` in an HTML attribute is `&` in the URL it names. */\nfunction decodeEntities(url: string): string {\n\treturn url.replaceAll('&amp;', '&');\n}\n\n/** No email to an address arrived in time. */\nexport class NoMail extends Error {\n\tconstructor(\n\t\treadonly address: string,\n\t\treadonly timeout: number,\n\t) {\n\t\tsuper(\n\t\t\t`No email to ${address} arrived within ${timeout}ms. Check that the ` +\n\t\t\t\t`code under test sends it, and to that address.`,\n\t\t);\n\t\tthis.name = 'NoMail';\n\t}\n}\n\n/** Reading mail needs `@geekmidas/emailkit`, and it is not installed. */\nexport class MailboxNeedsEmailkit extends Error {\n\tconstructor(cause: unknown) {\n\t\tsuper(\n\t\t\t'Reading mail from the mailbox parses it with @geekmidas/emailkit, ' +\n\t\t\t\t'which could not be loaded. Add it to the dev dependencies of the ' +\n\t\t\t\t'app whose mail the test reads.',\n\t\t\t{ cause },\n\t\t);\n\t\tthis.name = 'MailboxNeedsEmailkit';\n\t}\n}\n\n/** Mailpit did not answer. */\nexport class InboxUnreachable extends Error {\n\tconstructor(\n\t\treadonly inbox: string,\n\t\treadonly status: number,\n\t) {\n\t\tsuper(\n\t\t\t`Mailpit at ${inbox} answered ${status}. Is it running? \\`gkm test\\` ` +\n\t\t\t\t`starts it when the app declares an Email construct.`,\n\t\t);\n\t\tthis.name = 'InboxUnreachable';\n\t}\n}\n"],"mappings":";;;AAgBA,MAAM,kBAAkB;AACxB,MAAM,gBAAgB;;AA0CtB,SAAgB,cACf,SAC+B;CAC/B,MAAM,QAAQ,QAAQ,MAAM,QAAQ,OAAO,EAAE;CAC7C,MAAM,UAAU,QAAQ,WAAW;CACnC,MAAM,OAAO,MAAc,UACzB,QAAQ,SAAS,WAAW,MAAA,CAAO,GAAG,QAAQ,QAAQ,IAAI;CAE5D,QAAQ,YAAY;EACnB,MAAM,QAAQ,UAAU,mBAAmB,OAAO,QAAQ,EAAE;EAE5D,MAAM,MAAM,YAA8B;GACzC,MAAM,QAAQ,MAAM,KACnB,MAAM,IAAI,iBAAiB,OAAO,GAClC,KACD;GACA,OAAO,QAAQ,IACd,MAAM,SAAS,IAAI,OAAO,EAAE,SAAS;IACpC,MAAM,WAAW,MAAM,IAAI,mBAAmB,GAAG,KAAK;IACtD,IAAI,CAAC,SAAS,IACb,MAAM,IAAI,iBAAiB,OAAO,SAAS,MAAM;IAElD,OAAO,MAAM,IAAI,IAAI,WAAW,MAAM,SAAS,YAAY,CAAC,CAAC;GAC9D,CAAC,CACF;EACD;EAEA,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AqB;CAC5C,OAAO,IAAI,WAAW,SAAS,GAAG;AACnC;;AAGA,IAAa,SAAb,cAA4B,MAAM;CAEvB;CACA;CAFV,YACC,SACA,SACC;EACD,MACC,eAAe,QAAQ,kBAAkB,QAAQ,kEAElD;EANS,KAAA,UAAA;EACA,KAAA,UAAA;EAMT,KAAK,OAAO;CACb;AACD;;AAGA,IAAa,uBAAb,cAA0C,MAAM;CAC/C,YAAY,OAAgB;EAC3B,MACC,qKAGA,EAAE,MAAM,CACT;EACA,KAAK,OAAO;CACb;AACD;;AAGA,IAAa,mBAAb,cAAsC,MAAM;CAEjC;CACA;CAFV,YACC,OACA,QACC;EACD,MACC,cAAc,MAAM,YAAY,OAAO,kFAExC;EANS,KAAA,QAAA;EACA,KAAA,SAAA;EAMT,KAAK,OAAO;CACb;AACD"}
@@ -44,6 +44,10 @@ export declare class NoMail extends Error {
44
44
  readonly timeout: number;
45
45
  constructor(address: string, timeout: number);
46
46
  }
47
+ /** Reading mail needs `@geekmidas/emailkit`, and it is not installed. */
48
+ export declare class MailboxNeedsEmailkit extends Error {
49
+ constructor(cause: unknown);
50
+ }
47
51
  /** Mailpit did not answer. */
48
52
  export declare class InboxUnreachable extends Error {
49
53
  readonly inbox: string;
@@ -1 +1 @@
1
- {"version":3,"file":"mailbox.d.cts","names":[],"sources":["../src/mailbox.ts"],"mappings":";;iBAmBiB;;EAEhB;;EAEA,eAAe;;EAEf;;;iBAIgB;EAChB;EACA;EACA;EACA;EACA;EACA;;EAEA,SAAS,SAAS;;EAElB;;EAEA;;;;;EAKA,aAAa;;iBAGG;;EAEhB,QAAQ,QAAQ;;EAEhB,OAAO,QAAQ;;EAEf,SAAS;;;wBAIM,cACf,SAAS,kBACN,oBAAoB;;qBA4FX,eAAe;WAEjB;WACA;EAFV,YACU,iBACA;;;qBAWE,yBAAyB;WAE3B;WACA;EAFV,YACU,eACA"}
1
+ {"version":3,"file":"mailbox.d.cts","names":[],"sources":["../src/mailbox.ts"],"mappings":";;iBAmBiB;;EAEhB;;EAEA,eAAe;;EAEf;;;iBAIgB;EAChB;EACA;EACA;EACA;EACA;EACA;;EAEA,SAAS,SAAS;;EAElB;;EAEA;;;;;EAKA,aAAa;;iBAGG;;EAEhB,QAAQ,QAAQ;;EAEhB,OAAO,QAAQ;;EAEf,SAAS;;;wBAIM,cACf,SAAS,kBACN,oBAAoB;;qBAmGX,eAAe;WAEjB;WACA;EAFV,YACU,iBACA;;;qBAWE,6BAA6B;EACzC,YAAY;;;qBAYA,yBAAyB;WAE3B;WACA;EAFV,YACU,eACA"}
@@ -44,6 +44,10 @@ export declare class NoMail extends Error {
44
44
  readonly timeout: number;
45
45
  constructor(address: string, timeout: number);
46
46
  }
47
+ /** Reading mail needs `@geekmidas/emailkit`, and it is not installed. */
48
+ export declare class MailboxNeedsEmailkit extends Error {
49
+ constructor(cause: unknown);
50
+ }
47
51
  /** Mailpit did not answer. */
48
52
  export declare class InboxUnreachable extends Error {
49
53
  readonly inbox: string;
@@ -1 +1 @@
1
- {"version":3,"file":"mailbox.d.mts","names":[],"sources":["../src/mailbox.ts"],"mappings":";;iBAmBiB;;EAEhB;;EAEA,eAAe;;EAEf;;;iBAIgB;EAChB;EACA;EACA;EACA;EACA;EACA;;EAEA,SAAS,SAAS;;EAElB;;EAEA;;;;;EAKA,aAAa;;iBAGG;;EAEhB,QAAQ,QAAQ;;EAEhB,OAAO,QAAQ;;EAEf,SAAS;;;wBAIM,cACf,SAAS,kBACN,oBAAoB;;qBA4FX,eAAe;WAEjB;WACA;EAFV,YACU,iBACA;;;qBAWE,yBAAyB;WAE3B;WACA;EAFV,YACU,eACA"}
1
+ {"version":3,"file":"mailbox.d.mts","names":[],"sources":["../src/mailbox.ts"],"mappings":";;iBAmBiB;;EAEhB;;EAEA,eAAe;;EAEf;;;iBAIgB;EAChB;EACA;EACA;EACA;EACA;EACA;;EAEA,SAAS,SAAS;;EAElB;;EAEA;;;;;EAKA,aAAa;;iBAGG;;EAEhB,QAAQ,QAAQ;;EAEhB,OAAO,QAAQ;;EAEf,SAAS;;;wBAIM,cACf,SAAS,kBACN,oBAAoB;;qBAmGX,eAAe;WAEjB;WACA;EAFV,YACU,iBACA;;;qBAWE,6BAA6B;EACzC,YAAY;;;qBAYA,yBAAyB;WAE3B;WACA;EAFV,YACU,eACA"}
package/dist/mailbox.mjs CHANGED
@@ -41,8 +41,13 @@ async function json(response, inbox) {
41
41
  }
42
42
  /** The message, through the one MIME parser everything that reads mail uses. */
43
43
  async function email(id, raw) {
44
- const { parseEmail } = await import("@geekmidas/emailkit/inbound");
45
- const message = await parseEmail(raw);
44
+ let inbound;
45
+ try {
46
+ inbound = await import("@geekmidas/emailkit/inbound");
47
+ } catch (cause) {
48
+ throw new MailboxNeedsEmailkit(cause);
49
+ }
50
+ const message = await inbound.parseEmail(raw);
46
51
  const html = message.html ?? "";
47
52
  const text = message.text ?? "";
48
53
  const links = [...new Set([...html.matchAll(/href="(https?:\/\/[^"]+)"/g), ...text.matchAll(/(https?:\/\/[^\s<>"]+)/g)].map(([, url]) => decodeEntities(url)))];
@@ -74,6 +79,13 @@ var NoMail = class extends Error {
74
79
  this.name = "NoMail";
75
80
  }
76
81
  };
82
+ /** Reading mail needs `@geekmidas/emailkit`, and it is not installed. */
83
+ var MailboxNeedsEmailkit = class extends Error {
84
+ constructor(cause) {
85
+ super("Reading mail from the mailbox parses it with @geekmidas/emailkit, which could not be loaded. Add it to the dev dependencies of the app whose mail the test reads.", { cause });
86
+ this.name = "MailboxNeedsEmailkit";
87
+ }
88
+ };
77
89
  /** Mailpit did not answer. */
78
90
  var InboxUnreachable = class extends Error {
79
91
  inbox;
@@ -86,6 +98,6 @@ var InboxUnreachable = class extends Error {
86
98
  }
87
99
  };
88
100
  //#endregion
89
- export { InboxUnreachable, NoMail, createMailbox };
101
+ export { InboxUnreachable, MailboxNeedsEmailkit, NoMail, createMailbox };
90
102
 
91
103
  //# sourceMappingURL=mailbox.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"mailbox.mjs","names":[],"sources":["../src/mailbox.ts"],"sourcesContent":["/**\n * The mail a test sent, read from Mailpit.\n *\n * Locally, mail goes over real SMTP to Mailpit — the same client, the same\n * message, only the host differs. A test reads what arrived through Mailpit's\n * HTTP API, so a magic link is signed in with by opening the email that was\n * actually sent rather than a token pulled from somewhere else.\n *\n * Mailpit has one inbox for everything, and transactions do not reach it, so a\n * test keeps to its own mail by address: a unique recipient per test, read by\n * `to:`, and cleared afterwards.\n */\n\nimport type { ReceivedAttachment } from '@geekmidas/emailkit/inbound';\n\n/** How long `last()` waits for mail that is still on its way. */\nconst DEFAULT_TIMEOUT = 5_000;\nconst POLL_INTERVAL = 50;\n\nexport interface MailboxOptions {\n\t/** Mailpit's HTTP address — what `gkm test` injects as `<ID>_INBOX_URL`. */\n\tinbox: string;\n\t/** The `fetch` to read it with. Defaults to the global one. */\n\tfetch?: typeof fetch;\n\t/** How long `last()` waits for mail to arrive. */\n\ttimeout?: number;\n}\n\n/** One email, as it arrived. */\nexport interface Email {\n\tid: string;\n\tfrom: string;\n\tto: string[];\n\tsubject: string;\n\ttext: string;\n\thtml: string;\n\t/** Every header, by lower-case name, as the parser an inbox uses reads them. */\n\theaders: Readonly<Record<string, readonly string[]>>;\n\t/** Every `http(s)` link in the email, in order. */\n\tlinks: string[];\n\t/** The first link — the one a sign-in or confirmation email is about. */\n\tlink: string | undefined;\n\t/**\n\t * Its files, read from the raw message by the parser an inbox's handler\n\t * uses — so what survives one survives the other.\n\t */\n\tattachments: ReceivedAttachment[];\n}\n\nexport interface Mailbox {\n\t/** The newest email to this address, waiting for one if none has arrived. */\n\tlast(): Promise<Email>;\n\t/** Every email to this address, newest first. */\n\tall(): Promise<Email[]>;\n\t/** Delete every email to this address. */\n\tclear(): Promise<void>;\n}\n\n/** A way to read the mail sent to one address. */\nexport function createMailbox(\n\toptions: MailboxOptions,\n): (address: string) => Mailbox {\n\tconst inbox = options.inbox.replace(/\\/$/, '');\n\tconst timeout = options.timeout ?? DEFAULT_TIMEOUT;\n\tconst get = (path: string, init?: RequestInit) =>\n\t\t(options.fetch ?? globalThis.fetch)(`${inbox}${path}`, init);\n\n\treturn (address) => {\n\t\tconst query = `?query=${encodeURIComponent(`to:\"${address}\"`)}`;\n\n\t\tconst all = async (): Promise<Email[]> => {\n\t\t\tconst found = await json<SearchResult>(\n\t\t\t\tawait get(`/api/v1/search${query}`),\n\t\t\t\tinbox,\n\t\t\t);\n\t\t\treturn Promise.all(\n\t\t\t\tfound.messages.map(async ({ ID }) => {\n\t\t\t\t\tconst response = await get(`/api/v1/message/${ID}/raw`);\n\t\t\t\t\tif (!response.ok) {\n\t\t\t\t\t\tthrow new InboxUnreachable(inbox, response.status);\n\t\t\t\t\t}\n\t\t\t\t\treturn email(ID, new Uint8Array(await response.arrayBuffer()));\n\t\t\t\t}),\n\t\t\t);\n\t\t};\n\n\t\treturn {\n\t\t\tall,\n\t\t\tasync last() {\n\t\t\t\tconst deadline = Date.now() + timeout;\n\t\t\t\tfor (;;) {\n\t\t\t\t\tconst [newest] = await all();\n\t\t\t\t\tif (newest) return newest;\n\t\t\t\t\tif (Date.now() >= deadline) throw new NoMail(address, timeout);\n\t\t\t\t\tawait new Promise((resolve) => setTimeout(resolve, POLL_INTERVAL));\n\t\t\t\t}\n\t\t\t},\n\t\t\tasync clear() {\n\t\t\t\tconst response = await get(`/api/v1/search${query}`, {\n\t\t\t\t\tmethod: 'DELETE',\n\t\t\t\t});\n\t\t\t\tif (!response.ok) {\n\t\t\t\t\tthrow new InboxUnreachable(inbox, response.status);\n\t\t\t\t}\n\t\t\t},\n\t\t};\n\t};\n}\n\ninterface SearchResult {\n\tmessages: { ID: string }[];\n}\n\nasync function json<T>(response: Response, inbox: string): Promise<T> {\n\tif (!response.ok) throw new InboxUnreachable(inbox, response.status);\n\treturn (await response.json()) as T;\n}\n\n/** The message, through the one MIME parser everything that reads mail uses. */\nasync function email(id: string, raw: Uint8Array): Promise<Email> {\n\tconst { parseEmail } = await import('@geekmidas/emailkit/inbound');\n\tconst message = await parseEmail(raw);\n\tconst html = message.html ?? '';\n\tconst text = message.text ?? '';\n\tconst links = [\n\t\t...new Set(\n\t\t\t[\n\t\t\t\t...html.matchAll(/href=\"(https?:\\/\\/[^\"]+)\"/g),\n\t\t\t\t...text.matchAll(/(https?:\\/\\/[^\\s<>\"]+)/g),\n\t\t\t].map(([, url]) => decodeEntities(url!)),\n\t\t),\n\t];\n\n\treturn {\n\t\tid,\n\t\tfrom: message.from?.address ?? '',\n\t\tto: message.to.map(({ address }) => address),\n\t\tsubject: message.subject ?? '',\n\t\ttext,\n\t\thtml,\n\t\theaders: message.headers,\n\t\tlinks,\n\t\tlink: links[0],\n\t\tattachments: message.attachments,\n\t};\n}\n\n/** `&amp;` in an HTML attribute is `&` in the URL it names. */\nfunction decodeEntities(url: string): string {\n\treturn url.replaceAll('&amp;', '&');\n}\n\n/** No email to an address arrived in time. */\nexport class NoMail extends Error {\n\tconstructor(\n\t\treadonly address: string,\n\t\treadonly timeout: number,\n\t) {\n\t\tsuper(\n\t\t\t`No email to ${address} arrived within ${timeout}ms. Check that the ` +\n\t\t\t\t`code under test sends it, and to that address.`,\n\t\t);\n\t\tthis.name = 'NoMail';\n\t}\n}\n\n/** Mailpit did not answer. */\nexport class InboxUnreachable extends Error {\n\tconstructor(\n\t\treadonly inbox: string,\n\t\treadonly status: number,\n\t) {\n\t\tsuper(\n\t\t\t`Mailpit at ${inbox} answered ${status}. Is it running? \\`gkm test\\` ` +\n\t\t\t\t`starts it when the app declares an Email construct.`,\n\t\t);\n\t\tthis.name = 'InboxUnreachable';\n\t}\n}\n"],"mappings":";;AAgBA,MAAM,kBAAkB;AACxB,MAAM,gBAAgB;;AA0CtB,SAAgB,cACf,SAC+B;CAC/B,MAAM,QAAQ,QAAQ,MAAM,QAAQ,OAAO,EAAE;CAC7C,MAAM,UAAU,QAAQ,WAAW;CACnC,MAAM,OAAO,MAAc,UACzB,QAAQ,SAAS,WAAW,MAAA,CAAO,GAAG,QA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,CACb,GAAG,IAAI,IACN,CACC,GAAG,KAAK,SAAS,4BAA4B,GAC7C,GAAG,KAAK,SAAS,yBAAyB,CAC3C,CAAC,CAAC,KAAK,GAAG,SAAS,eAAe,GAAI,CAAC,CACxC,CACD;CAEA,OAAO;EACN;EACA,MAAM,QAAQ,MAAM,WAAW;EAC/B,IAAI,QAAQ,GAAG,KAAK,EAAE,cAAc,OAAO;EAC3C,SAAS,QAAQ,WAAW;EAC5B;EACA;EACA,SAAS,QAAQ;EACjB;EACA,MAAM,MAAM;EACZ,aAAa,QAAQ;CACtB;AACD;;AAGA,SAAS,eAAe,KAAqB;CAC5C,OAAO,IAAI,WAAW,SAAS,GAAG;AACnC;;AAGA,IAAa,SAAb,cAA4B,MAAM;CAEvB;CACA;CAFV,YACC,SACA,SACC;EACD,MACC,eAAe,QAAQ,kBAAkB,QAAQ,kEAElD;EANS,KAAA,UAAA;EACA,KAAA,UAAA;EAMT,KAAK,OAAO;CACb;AACD;;AAGA,IAAa,mBAAb,cAAsC,MAAM;CAEjC;CACA;CAFV,YACC,OACA,QACC;EACD,MACC,cAAc,MAAM,YAAY,OAAO,kFAExC;EANS,KAAA,QAAA;EACA,KAAA,SAAA;EAMT,KAAK,OAAO;CACb;AACD"}
1
+ {"version":3,"file":"mailbox.mjs","names":[],"sources":["../src/mailbox.ts"],"sourcesContent":["/**\n * The mail a test sent, read from Mailpit.\n *\n * Locally, mail goes over real SMTP to Mailpit — the same client, the same\n * message, only the host differs. A test reads what arrived through Mailpit's\n * HTTP API, so a magic link is signed in with by opening the email that was\n * actually sent rather than a token pulled from somewhere else.\n *\n * Mailpit has one inbox for everything, and transactions do not reach it, so a\n * test keeps to its own mail by address: a unique recipient per test, read by\n * `to:`, and cleared afterwards.\n */\n\nimport type { ReceivedAttachment } from '@geekmidas/emailkit/inbound';\n\n/** How long `last()` waits for mail that is still on its way. */\nconst DEFAULT_TIMEOUT = 5_000;\nconst POLL_INTERVAL = 50;\n\nexport interface MailboxOptions {\n\t/** Mailpit's HTTP address — what `gkm test` injects as `<ID>_INBOX_URL`. */\n\tinbox: string;\n\t/** The `fetch` to read it with. Defaults to the global one. */\n\tfetch?: typeof fetch;\n\t/** How long `last()` waits for mail to arrive. */\n\ttimeout?: number;\n}\n\n/** One email, as it arrived. */\nexport interface Email {\n\tid: string;\n\tfrom: string;\n\tto: string[];\n\tsubject: string;\n\ttext: string;\n\thtml: string;\n\t/** Every header, by lower-case name, as the parser an inbox uses reads them. */\n\theaders: Readonly<Record<string, readonly string[]>>;\n\t/** Every `http(s)` link in the email, in order. */\n\tlinks: string[];\n\t/** The first link — the one a sign-in or confirmation email is about. */\n\tlink: string | undefined;\n\t/**\n\t * Its files, read from the raw message by the parser an inbox's handler\n\t * uses — so what survives one survives the other.\n\t */\n\tattachments: ReceivedAttachment[];\n}\n\nexport interface Mailbox {\n\t/** The newest email to this address, waiting for one if none has arrived. */\n\tlast(): Promise<Email>;\n\t/** Every email to this address, newest first. */\n\tall(): Promise<Email[]>;\n\t/** Delete every email to this address. */\n\tclear(): Promise<void>;\n}\n\n/** A way to read the mail sent to one address. */\nexport function createMailbox(\n\toptions: MailboxOptions,\n): (address: string) => Mailbox {\n\tconst inbox = options.inbox.replace(/\\/$/, '');\n\tconst timeout = options.timeout ?? DEFAULT_TIMEOUT;\n\tconst get = (path: string, init?: RequestInit) =>\n\t\t(options.fetch ?? globalThis.fetch)(`${inbox}${path}`, init);\n\n\treturn (address) => {\n\t\tconst query = `?query=${encodeURIComponent(`to:\"${address}\"`)}`;\n\n\t\tconst all = async (): Promise<Email[]> => {\n\t\t\tconst found = await json<SearchResult>(\n\t\t\t\tawait get(`/api/v1/search${query}`),\n\t\t\t\tinbox,\n\t\t\t);\n\t\t\treturn Promise.all(\n\t\t\t\tfound.messages.map(async ({ ID }) => {\n\t\t\t\t\tconst response = await get(`/api/v1/message/${ID}/raw`);\n\t\t\t\t\tif (!response.ok) {\n\t\t\t\t\t\tthrow new InboxUnreachable(inbox, response.status);\n\t\t\t\t\t}\n\t\t\t\t\treturn email(ID, new Uint8Array(await response.arrayBuffer()));\n\t\t\t\t}),\n\t\t\t);\n\t\t};\n\n\t\treturn {\n\t\t\tall,\n\t\t\tasync last() {\n\t\t\t\tconst deadline = Date.now() + timeout;\n\t\t\t\tfor (;;) {\n\t\t\t\t\tconst [newest] = await all();\n\t\t\t\t\tif (newest) return newest;\n\t\t\t\t\tif (Date.now() >= deadline) throw new NoMail(address, timeout);\n\t\t\t\t\tawait new Promise((resolve) => setTimeout(resolve, POLL_INTERVAL));\n\t\t\t\t}\n\t\t\t},\n\t\t\tasync clear() {\n\t\t\t\tconst response = await get(`/api/v1/search${query}`, {\n\t\t\t\t\tmethod: 'DELETE',\n\t\t\t\t});\n\t\t\t\tif (!response.ok) {\n\t\t\t\t\tthrow new InboxUnreachable(inbox, response.status);\n\t\t\t\t}\n\t\t\t},\n\t\t};\n\t};\n}\n\ninterface SearchResult {\n\tmessages: { ID: string }[];\n}\n\nasync function json<T>(response: Response, inbox: string): Promise<T> {\n\tif (!response.ok) throw new InboxUnreachable(inbox, response.status);\n\treturn (await response.json()) as T;\n}\n\n/** The message, through the one MIME parser everything that reads mail uses. */\nasync function email(id: string, raw: Uint8Array): Promise<Email> {\n\t// emailkit is an optional peer: loaded when mail is read, inside a `try`\n\t// so a bundled test harness for an app that sends none leaves it alone.\n\tlet inbound: typeof import('@geekmidas/emailkit/inbound');\n\ttry {\n\t\tinbound = await import('@geekmidas/emailkit/inbound');\n\t} catch (cause) {\n\t\tthrow new MailboxNeedsEmailkit(cause);\n\t}\n\tconst message = await inbound.parseEmail(raw);\n\tconst html = message.html ?? '';\n\tconst text = message.text ?? '';\n\tconst links = [\n\t\t...new Set(\n\t\t\t[\n\t\t\t\t...html.matchAll(/href=\"(https?:\\/\\/[^\"]+)\"/g),\n\t\t\t\t...text.matchAll(/(https?:\\/\\/[^\\s<>\"]+)/g),\n\t\t\t].map(([, url]) => decodeEntities(url!)),\n\t\t),\n\t];\n\n\treturn {\n\t\tid,\n\t\tfrom: message.from?.address ?? '',\n\t\tto: message.to.map(({ address }) => address),\n\t\tsubject: message.subject ?? '',\n\t\ttext,\n\t\thtml,\n\t\theaders: message.headers,\n\t\tlinks,\n\t\tlink: links[0],\n\t\tattachments: message.attachments,\n\t};\n}\n\n/** `&amp;` in an HTML attribute is `&` in the URL it names. */\nfunction decodeEntities(url: string): string {\n\treturn url.replaceAll('&amp;', '&');\n}\n\n/** No email to an address arrived in time. */\nexport class NoMail extends Error {\n\tconstructor(\n\t\treadonly address: string,\n\t\treadonly timeout: number,\n\t) {\n\t\tsuper(\n\t\t\t`No email to ${address} arrived within ${timeout}ms. Check that the ` +\n\t\t\t\t`code under test sends it, and to that address.`,\n\t\t);\n\t\tthis.name = 'NoMail';\n\t}\n}\n\n/** Reading mail needs `@geekmidas/emailkit`, and it is not installed. */\nexport class MailboxNeedsEmailkit extends Error {\n\tconstructor(cause: unknown) {\n\t\tsuper(\n\t\t\t'Reading mail from the mailbox parses it with @geekmidas/emailkit, ' +\n\t\t\t\t'which could not be loaded. Add it to the dev dependencies of the ' +\n\t\t\t\t'app whose mail the test reads.',\n\t\t\t{ cause },\n\t\t);\n\t\tthis.name = 'MailboxNeedsEmailkit';\n\t}\n}\n\n/** Mailpit did not answer. */\nexport class InboxUnreachable extends Error {\n\tconstructor(\n\t\treadonly inbox: string,\n\t\treadonly status: number,\n\t) {\n\t\tsuper(\n\t\t\t`Mailpit at ${inbox} answered ${status}. Is it running? \\`gkm test\\` ` +\n\t\t\t\t`starts it when the app declares an Email construct.`,\n\t\t);\n\t\tthis.name = 'InboxUnreachable';\n\t}\n}\n"],"mappings":";;AAgBA,MAAM,kBAAkB;AACxB,MAAM,gBAAgB;;AA0CtB,SAAgB,cACf,SAC+B;CAC/B,MAAM,QAAQ,QAAQ,MAAM,QAAQ,OAAO,EAAE;CAC7C,MAAM,UAAU,QAAQ,WAAW;CACnC,MAAM,OAAO,MAAc,UACzB,QAAQ,SAAS,WAAW,MAAA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AAG;CAC5C,MAAM,OAAO,QAAQ,QAAQ;CAC7B,MAAM,OAAO,QAAQ,QAAQ;CAC7B,MAAM,QAAQ,CACb,GAAG,IAAI,IACN,CACC,GAAG,KAAK,SAAS,4BAA4B,GAC7C,GAAG,KAAK,SAAS,yBAAyB,CAC3C,CAAC,CAAC,KAAK,GAAG,SAAS,eAAe,GAAI,CAAC,CACxC,CACD;CAEA,OAAO;EACN;EACA,MAAM,QAAQ,MAAM,WAAW;EAC/B,IAAI,QAAQ,GAAG,KAAK,EAAE,cAAc,OAAO;EAC3C,SAAS,QAAQ,WAAW;EAC5B;EACA;EACA,SAAS,QAAQ;EACjB;EACA,MAAM,MAAM;EACZ,aAAa,QAAQ;CACtB;AACD;;AAGA,SAAS,eAAe,KAAqB;CAC5C,OAAO,IAAI,WAAW,SAAS,GAAG;AACnC;;AAGA,IAAa,SAAb,cAA4B,MAAM;CAEvB;CACA;CAFV,YACC,SACA,SACC;EACD,MACC,eAAe,QAAQ,kBAAkB,QAAQ,kEAElD;EANS,KAAA,UAAA;EACA,KAAA,UAAA;EAMT,KAAK,OAAO;CACb;AACD;;AAGA,IAAa,uBAAb,cAA0C,MAAM;CAC/C,YAAY,OAAgB;EAC3B,MACC,qKAGA,EAAE,MAAM,CACT;EACA,KAAK,OAAO;CACb;AACD;;AAGA,IAAa,mBAAb,cAAsC,MAAM;CAEjC;CACA;CAFV,YACC,OACA,QACC;EACD,MACC,cAAc,MAAM,YAAY,OAAO,kFAExC;EANS,KAAA,QAAA;EACA,KAAA,SAAA;EAMT,KAAK,OAAO;CACb;AACD"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@geekmidas/testkit",
3
- "version": "10.0.0-alpha.155",
3
+ "version": "10.0.0-alpha.156",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "exports": {
@@ -161,18 +161,18 @@
161
161
  "dependencies": {
162
162
  "@faker-js/faker": "~10.6.0",
163
163
  "tough-cookie": "~6.0.2",
164
- "@geekmidas/envkit": "^10.0.0-alpha.155",
165
- "@geekmidas/logger": "^10.0.0-alpha.155",
166
- "@geekmidas/services": "^10.0.0-alpha.155"
164
+ "@geekmidas/envkit": ">=10.0.0-alpha.156 <10.0.0",
165
+ "@geekmidas/logger": ">=10.0.0-alpha.156 <10.0.0",
166
+ "@geekmidas/services": ">=10.0.0-alpha.156 <10.0.0"
167
167
  },
168
168
  "devDependencies": {
169
169
  "@better-auth/test-utils": "~1.7.6",
170
170
  "@types/pg": "~8.23.1",
171
171
  "better-auth": "~1.7.6",
172
- "@geekmidas/envkit": "^10.0.0-alpha.155",
173
- "@geekmidas/logger": "^10.0.0-alpha.155",
174
- "@geekmidas/services": "^10.0.0-alpha.155",
175
- "@geekmidas/emailkit": "^10.0.0-alpha.155"
172
+ "@geekmidas/envkit": ">=10.0.0-alpha.156 <10.0.0",
173
+ "@geekmidas/logger": ">=10.0.0-alpha.156 <10.0.0",
174
+ "@geekmidas/services": ">=10.0.0-alpha.156 <10.0.0",
175
+ "@geekmidas/emailkit": ">=10.0.0-alpha.156 <10.0.0"
176
176
  },
177
177
  "repository": {
178
178
  "type": "git",
@@ -191,7 +191,7 @@
191
191
  "vitest": "~5.0.2",
192
192
  "@types/aws-lambda": ">=8.10.163",
193
193
  "better-auth": ">=1.7.5",
194
- "@geekmidas/emailkit": "^10.0.0-alpha.155"
194
+ "@geekmidas/emailkit": ">=10.0.0-alpha.156 <10.0.0"
195
195
  },
196
196
  "peerDependenciesMeta": {
197
197
  "kysely": {