@owlmeans/server-mailer-mailgun 0.1.5 → 0.1.7
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/build/service.d.ts +13 -0
- package/build/service.d.ts.map +1 -1
- package/build/service.js +29 -5
- package/build/service.js.map +1 -1
- package/package.json +4 -4
- package/src/service.ts +46 -4
package/build/service.d.ts
CHANGED
|
@@ -1,3 +1,16 @@
|
|
|
1
1
|
import type { MailerService } from '@owlmeans/mailer';
|
|
2
|
+
import type { ServerConfig } from '@owlmeans/server-context';
|
|
3
|
+
export interface MailgunConfig extends ServerConfig {
|
|
4
|
+
mailgun: {
|
|
5
|
+
/** Mailgun API key (starts with key-...) */
|
|
6
|
+
apiKey: string;
|
|
7
|
+
/** Mailgun sending domain (e.g. mg.example.com) */
|
|
8
|
+
domain: string;
|
|
9
|
+
/** Sender address (e.g. "OwlMeans Platform <noreply@mg.example.com>") */
|
|
10
|
+
from: string;
|
|
11
|
+
/** Override the Mailgun API base URL. Defaults to https://api.mailgun.net/v3 */
|
|
12
|
+
baseUrl?: string;
|
|
13
|
+
};
|
|
14
|
+
}
|
|
2
15
|
export declare const makeMailgunMailerService: (alias?: string) => MailerService;
|
|
3
16
|
//# sourceMappingURL=service.d.ts.map
|
package/build/service.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"service.d.ts","sourceRoot":"","sources":["../src/service.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAA;
|
|
1
|
+
{"version":3,"file":"service.d.ts","sourceRoot":"","sources":["../src/service.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAA;AACrD,OAAO,KAAK,EAAE,YAAY,EAAiB,MAAM,0BAA0B,CAAA;AAG3E,MAAM,WAAW,aAAc,SAAQ,YAAY;IACjD,OAAO,EAAE;QACP,4CAA4C;QAC5C,MAAM,EAAE,MAAM,CAAA;QACd,mDAAmD;QACnD,MAAM,EAAE,MAAM,CAAA;QACd,yEAAyE;QACzE,IAAI,EAAE,MAAM,CAAA;QACZ,gFAAgF;QAChF,OAAO,CAAC,EAAE,MAAM,CAAA;KACjB,CAAA;CACF;AAID,eAAO,MAAM,wBAAwB,GAAI,cAAsB,KAAG,aA+BjE,CAAA"}
|
package/build/service.js
CHANGED
|
@@ -1,8 +1,32 @@
|
|
|
1
1
|
import { createService } from '@owlmeans/context';
|
|
2
2
|
import { MAILGUN_MAILER } from './consts.js';
|
|
3
|
-
export const makeMailgunMailerService = (alias = MAILGUN_MAILER) =>
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
}
|
|
3
|
+
export const makeMailgunMailerService = (alias = MAILGUN_MAILER) => {
|
|
4
|
+
const service = createService(alias, {
|
|
5
|
+
send: async (message) => {
|
|
6
|
+
const ctx = service.ctx;
|
|
7
|
+
const { apiKey, domain, from, baseUrl = 'https://api.mailgun.net/v3' } = ctx.cfg.mailgun;
|
|
8
|
+
const url = `${baseUrl}/${domain}/messages`;
|
|
9
|
+
const body = new URLSearchParams();
|
|
10
|
+
body.set('from', from);
|
|
11
|
+
body.set('to', message.to);
|
|
12
|
+
body.set('subject', message.subject);
|
|
13
|
+
if (message.text)
|
|
14
|
+
body.set('text', message.text);
|
|
15
|
+
if (message.html)
|
|
16
|
+
body.set('html', message.html);
|
|
17
|
+
const res = await fetch(url, {
|
|
18
|
+
method: 'POST',
|
|
19
|
+
headers: {
|
|
20
|
+
Authorization: `Basic ${btoa(`api:${apiKey}`)}`,
|
|
21
|
+
},
|
|
22
|
+
body,
|
|
23
|
+
});
|
|
24
|
+
if (!res.ok) {
|
|
25
|
+
const detail = await res.text().catch(() => '');
|
|
26
|
+
throw new Error(`Mailgun send failed [${res.status}]: ${detail}`);
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
});
|
|
30
|
+
return service;
|
|
31
|
+
};
|
|
8
32
|
//# sourceMappingURL=service.js.map
|
package/build/service.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"service.js","sourceRoot":"","sources":["../src/service.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAA;
|
|
1
|
+
{"version":3,"file":"service.js","sourceRoot":"","sources":["../src/service.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAA;AAGjD,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAA;AAiB5C,MAAM,CAAC,MAAM,wBAAwB,GAAG,CAAC,KAAK,GAAG,cAAc,EAAiB,EAAE;IAChF,MAAM,OAAO,GAAG,aAAa,CAAgB,KAAK,EAAE;QAClD,IAAI,EAAE,KAAK,EAAC,OAAO,EAAC,EAAE;YACpB,MAAM,GAAG,GAAI,OAAe,CAAC,GAAc,CAAA;YAC3C,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,GAAG,4BAA4B,EAAE,GAAG,GAAG,CAAC,GAAG,CAAC,OAAO,CAAA;YAExF,MAAM,GAAG,GAAG,GAAG,OAAO,IAAI,MAAM,WAAW,CAAA;YAE3C,MAAM,IAAI,GAAG,IAAI,eAAe,EAAE,CAAA;YAClC,IAAI,CAAC,GAAG,CAAC,MAAM,EAAE,IAAI,CAAC,CAAA;YACtB,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,OAAO,CAAC,EAAE,CAAC,CAAA;YAC1B,IAAI,CAAC,GAAG,CAAC,SAAS,EAAE,OAAO,CAAC,OAAO,CAAC,CAAA;YACpC,IAAI,OAAO,CAAC,IAAI;gBAAE,IAAI,CAAC,GAAG,CAAC,MAAM,EAAE,OAAO,CAAC,IAAI,CAAC,CAAA;YAChD,IAAI,OAAO,CAAC,IAAI;gBAAE,IAAI,CAAC,GAAG,CAAC,MAAM,EAAE,OAAO,CAAC,IAAI,CAAC,CAAA;YAEhD,MAAM,GAAG,GAAG,MAAM,KAAK,CAAC,GAAG,EAAE;gBAC3B,MAAM,EAAE,MAAM;gBACd,OAAO,EAAE;oBACP,aAAa,EAAE,SAAS,IAAI,CAAC,OAAO,MAAM,EAAE,CAAC,EAAE;iBAChD;gBACD,IAAI;aACL,CAAC,CAAA;YAEF,IAAI,CAAC,GAAG,CAAC,EAAE,EAAE,CAAC;gBACZ,MAAM,MAAM,GAAG,MAAM,GAAG,CAAC,IAAI,EAAE,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,EAAE,CAAC,CAAA;gBAC/C,MAAM,IAAI,KAAK,CAAC,wBAAwB,GAAG,CAAC,MAAM,MAAM,MAAM,EAAE,CAAC,CAAA;YACnE,CAAC;QACH,CAAC;KACF,CAAC,CAAA;IAEF,OAAO,OAAO,CAAA;AAChB,CAAC,CAAA"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@owlmeans/server-mailer-mailgun",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.7",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"scripts": {
|
|
@@ -22,9 +22,9 @@
|
|
|
22
22
|
}
|
|
23
23
|
},
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@owlmeans/context": "^0.1.
|
|
26
|
-
"@owlmeans/error": "^0.1.
|
|
27
|
-
"@owlmeans/mailer": "^0.1.
|
|
25
|
+
"@owlmeans/context": "^0.1.7",
|
|
26
|
+
"@owlmeans/error": "^0.1.7",
|
|
27
|
+
"@owlmeans/mailer": "^0.1.7"
|
|
28
28
|
},
|
|
29
29
|
"devDependencies": {
|
|
30
30
|
"@owlmeans/dep-config": "workspace:*",
|
package/src/service.ts
CHANGED
|
@@ -1,10 +1,52 @@
|
|
|
1
1
|
import { createService } from '@owlmeans/context'
|
|
2
2
|
import type { MailerService } from '@owlmeans/mailer'
|
|
3
|
+
import type { ServerConfig, ServerContext } from '@owlmeans/server-context'
|
|
3
4
|
import { MAILGUN_MAILER } from './consts.js'
|
|
4
5
|
|
|
5
|
-
export
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
6
|
+
export interface MailgunConfig extends ServerConfig {
|
|
7
|
+
mailgun: {
|
|
8
|
+
/** Mailgun API key (starts with key-...) */
|
|
9
|
+
apiKey: string
|
|
10
|
+
/** Mailgun sending domain (e.g. mg.example.com) */
|
|
11
|
+
domain: string
|
|
12
|
+
/** Sender address (e.g. "OwlMeans Platform <noreply@mg.example.com>") */
|
|
13
|
+
from: string
|
|
14
|
+
/** Override the Mailgun API base URL. Defaults to https://api.mailgun.net/v3 */
|
|
15
|
+
baseUrl?: string
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
type Context = ServerContext<MailgunConfig>
|
|
20
|
+
|
|
21
|
+
export const makeMailgunMailerService = (alias = MAILGUN_MAILER): MailerService => {
|
|
22
|
+
const service = createService<MailerService>(alias, {
|
|
23
|
+
send: async message => {
|
|
24
|
+
const ctx = (service as any).ctx as Context
|
|
25
|
+
const { apiKey, domain, from, baseUrl = 'https://api.mailgun.net/v3' } = ctx.cfg.mailgun
|
|
26
|
+
|
|
27
|
+
const url = `${baseUrl}/${domain}/messages`
|
|
28
|
+
|
|
29
|
+
const body = new URLSearchParams()
|
|
30
|
+
body.set('from', from)
|
|
31
|
+
body.set('to', message.to)
|
|
32
|
+
body.set('subject', message.subject)
|
|
33
|
+
if (message.text) body.set('text', message.text)
|
|
34
|
+
if (message.html) body.set('html', message.html)
|
|
35
|
+
|
|
36
|
+
const res = await fetch(url, {
|
|
37
|
+
method: 'POST',
|
|
38
|
+
headers: {
|
|
39
|
+
Authorization: `Basic ${btoa(`api:${apiKey}`)}`,
|
|
40
|
+
},
|
|
41
|
+
body,
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
if (!res.ok) {
|
|
45
|
+
const detail = await res.text().catch(() => '')
|
|
46
|
+
throw new Error(`Mailgun send failed [${res.status}]: ${detail}`)
|
|
47
|
+
}
|
|
9
48
|
},
|
|
10
49
|
})
|
|
50
|
+
|
|
51
|
+
return service
|
|
52
|
+
}
|