Skip to content

Enhance account creation and configuration options#27

Open
hesham-fouda wants to merge 4 commits into
cemalgnlts:mainfrom
hesham-fouda:main
Open

Enhance account creation and configuration options#27
hesham-fouda wants to merge 4 commits into
cemalgnlts:mainfrom
hesham-fouda:main

Conversation

@hesham-fouda
Copy link
Copy Markdown

Summary

Two additive enhancements that make the SDK reusable against any mail.tm-compatible API (e.g. self-hosted instances)
and give callers more control over how disposable accounts are generated.

Both changes are fully backwards-compatible — every existing call site keeps working unchanged.

Changes

1. Configurable baseUrl, baseMercure, and custom headers

The constructor now accepts three optional fields:

Option Purpose
baseUrl API base. Defaults to https://api.mail.tm.
baseMercure Mercure SSE hub. Defaults to https://mercure.mail.tm/.well-known/mercure.
headers Record<string, string> merged into every request. Useful for x-api-key, custom auth, tracing IDs,
etc.
const mailjs = new Mailjs({
  baseUrl:     "https://api.example.com",
  baseMercure: "https://mercure.example.com/.well-known/mercure",
  headers: { "x-api-key": "xxxx" },
});

baseUrl / baseMercure were previously hard-coded private fields, so this is the first way to point the SDK at a
non-mail.tm host without forking.

2. Richer options on createOneAccount

createOneAccount() now accepts an options object alongside the legacy useUUID: boolean form:

┌──────────┬───────────────────────────────────────────────────────────────────────┐
  Option                                  Effect                                 
├──────────┼───────────────────────────────────────────────────────────────────────┤
 domain    Use this domain directly  skips the getDomains() round-trip.         
├──────────┼───────────────────────────────────────────────────────────────────────┤
 name      Exact local-part. Overrides prefix / hashSize / useUUID.              
├──────────┼───────────────────────────────────────────────────────────────────────┤
 prefix    Local-part prefix; a random hex suffix of hashSize bytes is appended. 
├──────────┼───────────────────────────────────────────────────────────────────────┤
 hashSize  Random suffix size in bytes (= 2× hex chars). Default 8.              
├──────────┼───────────────────────────────────────────────────────────────────────┤
 password  Override the auto-generated password.                                 
├──────────┼───────────────────────────────────────────────────────────────────────┤
 useUUID   Same as before; ignored when name is set.                             
└──────────┴───────────────────────────────────────────────────────────────────────┘

Local-part precedence: name > prefix+hashSize > useUUID > random hex.

// Legacy form — still works
await mailjs.createOneAccount();
await mailjs.createOneAccount(true);

// New options
await mailjs.createOneAccount({ domain: "yourdomain.com" });
await mailjs.createOneAccount({ domain: "yourdomain.com", prefix: "test-", hashSize: 4 });
await mailjs.createOneAccount({ domain: "yourdomain.com", name: "support", password: "s3cret-pw" });

The domain option is the practical motivator  server-to-server flows that already know the target domain no longer
need to hit /domains on every account creation.

Compatibility

- No breaking changes. All existing call sites behave exactly as before when none of the new options are passed.
- IRequestObject.headers typing is unchanged at the public surface; extra headers are merged via Object.assign inside
_send so no public type widens.
- Tests in tests.js continue to pass against the live mail.tm service.

Test plan

- Existing tests pass (npm run build && node tests.js)
- new Mailjs() with no args still hits api.mail.tm
- headers: { "x-api-key": "..." } is sent on every request
- createOneAccount({ domain }) skips getDomains() (verified by network logs)
- createOneAccount({ name }) produces the exact local-part
- createOneAccount(true) still produces a UUID local-part

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant