@cloudstrife21/email-sdk 0.1.0
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 +115 -0
- package/dist/client.d.ts +10 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +111 -0
- package/dist/client.js.map +1 -0
- package/dist/env.d.ts +3 -0
- package/dist/env.d.ts.map +1 -0
- package/dist/env.js +20 -0
- package/dist/env.js.map +1 -0
- package/dist/errors.d.ts +12 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +17 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -0
- package/dist/types.d.ts +58 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/package.json +36 -0
package/README.md
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
# @cloudstrife21/email-sdk
|
|
2
|
+
|
|
3
|
+
Server-side TypeScript client for the StrongStart email send API.
|
|
4
|
+
|
|
5
|
+
## Scope
|
|
6
|
+
|
|
7
|
+
- Node.js server environments only
|
|
8
|
+
- `POST /api/esp/v1/send-template`
|
|
9
|
+
- Optional HMAC signing via `x-signature`
|
|
10
|
+
- Typed errors for non-2xx API responses
|
|
11
|
+
|
|
12
|
+
## Install
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
npm install @cloudstrife21/email-sdk
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
Until it is published, you can consume the sibling package directly during local development:
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
npm install ../email-sdk
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Environment variables
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
EMAIL_API_BASE_URL=https://email.strongstart.digital
|
|
28
|
+
EMAIL_API_KEY=k_live_xxx
|
|
29
|
+
EMAIL_CLIENT_ID=app_ytexas
|
|
30
|
+
EMAIL_HMAC_SECRET=sh_hmac_xxx
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Usage
|
|
34
|
+
|
|
35
|
+
```ts
|
|
36
|
+
import { createEmailClient, emailClientConfigFromEnv, EmailApiError } from '@cloudstrife21/email-sdk';
|
|
37
|
+
|
|
38
|
+
const email = createEmailClient(emailClientConfigFromEnv(process.env));
|
|
39
|
+
|
|
40
|
+
try {
|
|
41
|
+
const result = await email.sendTemplate({
|
|
42
|
+
templateKey: 'welcome_email',
|
|
43
|
+
to: 'user@example.com',
|
|
44
|
+
data: {
|
|
45
|
+
name: 'Ada',
|
|
46
|
+
ctaUrl: 'https://ytexas.org/welcome'
|
|
47
|
+
},
|
|
48
|
+
category: 'transactional'
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
console.log(result.id, result.status, result.requestId);
|
|
52
|
+
} catch (error) {
|
|
53
|
+
if (error instanceof EmailApiError) {
|
|
54
|
+
console.error(error.status, error.code, error.requestId, error.payload);
|
|
55
|
+
}
|
|
56
|
+
throw error;
|
|
57
|
+
}
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## API
|
|
61
|
+
|
|
62
|
+
### `createEmailClient(config)`
|
|
63
|
+
|
|
64
|
+
Creates a client for the StrongStart send endpoint.
|
|
65
|
+
|
|
66
|
+
Config fields:
|
|
67
|
+
|
|
68
|
+
- `baseUrl`: API origin such as `https://email.strongstart.digital`
|
|
69
|
+
- `apiKey`: tenant-scoped API key sent as `x-api-key`
|
|
70
|
+
- `clientId`: application identifier expected by the API
|
|
71
|
+
- `hmacSecret`: optional request-signing secret
|
|
72
|
+
- `fetch`: optional fetch implementation override
|
|
73
|
+
- `defaultHeaders`: optional headers applied to every request
|
|
74
|
+
- `generateIdempotencyKey`: optional idempotency key factory
|
|
75
|
+
|
|
76
|
+
### `email.sendTemplate(request, options?)`
|
|
77
|
+
|
|
78
|
+
Supported request fields:
|
|
79
|
+
|
|
80
|
+
- `templateId` or `templateKey`
|
|
81
|
+
- optional `version` when using `templateKey`
|
|
82
|
+
- `to`
|
|
83
|
+
- optional `data`
|
|
84
|
+
- optional `subject`
|
|
85
|
+
- optional `senderDomain`
|
|
86
|
+
- optional `category`
|
|
87
|
+
- optional `cc` and `bcc` for forward compatibility with the API contract
|
|
88
|
+
|
|
89
|
+
Options:
|
|
90
|
+
|
|
91
|
+
- `idempotencyKey`
|
|
92
|
+
- `requestId`
|
|
93
|
+
- `headers`
|
|
94
|
+
- `signal`
|
|
95
|
+
|
|
96
|
+
## Errors
|
|
97
|
+
|
|
98
|
+
Non-2xx responses throw `EmailApiError` with:
|
|
99
|
+
|
|
100
|
+
- `status`
|
|
101
|
+
- `code`
|
|
102
|
+
- `requestId`
|
|
103
|
+
- `payload`
|
|
104
|
+
|
|
105
|
+
## Build
|
|
106
|
+
|
|
107
|
+
```bash
|
|
108
|
+
npm run build
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
## Test
|
|
112
|
+
|
|
113
|
+
```bash
|
|
114
|
+
npm test
|
|
115
|
+
```
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { EmailClientConfig, SendTemplateOptions, SendTemplateRequest, SendTemplateResponse } from './types';
|
|
2
|
+
export declare class EmailClient {
|
|
3
|
+
private readonly config;
|
|
4
|
+
private readonly fetchImpl;
|
|
5
|
+
constructor(config: EmailClientConfig);
|
|
6
|
+
sendTemplate(request: SendTemplateRequest, options?: SendTemplateOptions): Promise<SendTemplateResponse>;
|
|
7
|
+
}
|
|
8
|
+
export declare function createEmailClient(config: EmailClientConfig): EmailClient;
|
|
9
|
+
export declare function createRequestSignature(body: string, hmacSecret: string): string;
|
|
10
|
+
//# sourceMappingURL=client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAEV,iBAAiB,EAEjB,mBAAmB,EACnB,mBAAmB,EACnB,oBAAoB,EACrB,MAAM,SAAS,CAAC;AA6EjB,qBAAa,WAAW;IACtB,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAoB;IAC3C,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAY;gBAE1B,MAAM,EAAE,iBAAiB;IAc/B,YAAY,CAAC,OAAO,EAAE,mBAAmB,EAAE,OAAO,CAAC,EAAE,mBAAmB,GAAG,OAAO,CAAC,oBAAoB,CAAC;CAe/G;AAED,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,iBAAiB,GAAG,WAAW,CAExE;AAED,wBAAgB,sBAAsB,CAAC,IAAI,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,MAAM,CAE/E"}
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { createHmac, randomUUID } from 'crypto';
|
|
2
|
+
import { EmailApiError, EmailClientConfigError } from './errors';
|
|
3
|
+
const SEND_TEMPLATE_PATH = '/api/esp/v1/send-template';
|
|
4
|
+
function getFetch(override) {
|
|
5
|
+
if (override)
|
|
6
|
+
return override;
|
|
7
|
+
if (typeof fetch === 'function')
|
|
8
|
+
return fetch;
|
|
9
|
+
throw new EmailClientConfigError('A fetch implementation is required');
|
|
10
|
+
}
|
|
11
|
+
function normalizeBaseUrl(baseUrl) {
|
|
12
|
+
const value = baseUrl.trim();
|
|
13
|
+
if (!value)
|
|
14
|
+
throw new EmailClientConfigError('baseUrl is required');
|
|
15
|
+
return value.replace(/\/+$/, '');
|
|
16
|
+
}
|
|
17
|
+
function toHeadersList(headers) {
|
|
18
|
+
if (!headers)
|
|
19
|
+
return [];
|
|
20
|
+
if (headers instanceof Headers)
|
|
21
|
+
return Array.from(headers.entries());
|
|
22
|
+
if (Array.isArray(headers))
|
|
23
|
+
return headers.map(([key, value]) => [key, String(value)]);
|
|
24
|
+
return Object.entries(headers).map(([key, value]) => [key, String(value)]);
|
|
25
|
+
}
|
|
26
|
+
function createBody(config, request) {
|
|
27
|
+
const body = {
|
|
28
|
+
clientId: config.clientId,
|
|
29
|
+
to: request.to,
|
|
30
|
+
data: request.data,
|
|
31
|
+
subject: request.subject,
|
|
32
|
+
sender_domain: request.senderDomain,
|
|
33
|
+
category: request.category,
|
|
34
|
+
cc: request.cc,
|
|
35
|
+
bcc: request.bcc,
|
|
36
|
+
};
|
|
37
|
+
if ('templateId' in request) {
|
|
38
|
+
body.templateId = request.templateId;
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
body.templateKey = request.templateKey;
|
|
42
|
+
body.version = request.version;
|
|
43
|
+
}
|
|
44
|
+
return JSON.stringify(body);
|
|
45
|
+
}
|
|
46
|
+
async function parseError(response) {
|
|
47
|
+
const contentType = response.headers.get('content-type') || '';
|
|
48
|
+
if (contentType.includes('application/json')) {
|
|
49
|
+
const payload = (await response.json().catch(() => null));
|
|
50
|
+
if (payload?.message)
|
|
51
|
+
return payload;
|
|
52
|
+
}
|
|
53
|
+
const text = await response.text().catch(() => '');
|
|
54
|
+
return {
|
|
55
|
+
message: text || `http_${response.status}`,
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
function buildHeaders(config, body, options) {
|
|
59
|
+
const headers = new Headers();
|
|
60
|
+
headers.set('content-type', 'application/json');
|
|
61
|
+
headers.set('x-api-key', config.apiKey);
|
|
62
|
+
for (const [key, value] of toHeadersList(config.defaultHeaders))
|
|
63
|
+
headers.set(key, value);
|
|
64
|
+
for (const [key, value] of toHeadersList(options?.headers))
|
|
65
|
+
headers.set(key, value);
|
|
66
|
+
const idempotencyKey = options?.idempotencyKey ?? config.generateIdempotencyKey?.() ?? randomUUID();
|
|
67
|
+
if (idempotencyKey)
|
|
68
|
+
headers.set('idempotency-key', idempotencyKey);
|
|
69
|
+
if (options?.requestId)
|
|
70
|
+
headers.set('x-request-id', options.requestId);
|
|
71
|
+
if (config.hmacSecret) {
|
|
72
|
+
headers.set('x-signature', createRequestSignature(body, config.hmacSecret));
|
|
73
|
+
}
|
|
74
|
+
return headers;
|
|
75
|
+
}
|
|
76
|
+
export class EmailClient {
|
|
77
|
+
constructor(config) {
|
|
78
|
+
if (!config.apiKey?.trim())
|
|
79
|
+
throw new EmailClientConfigError('apiKey is required');
|
|
80
|
+
if (!config.clientId?.trim())
|
|
81
|
+
throw new EmailClientConfigError('clientId is required');
|
|
82
|
+
this.config = {
|
|
83
|
+
...config,
|
|
84
|
+
baseUrl: normalizeBaseUrl(config.baseUrl),
|
|
85
|
+
apiKey: config.apiKey.trim(),
|
|
86
|
+
clientId: config.clientId.trim(),
|
|
87
|
+
hmacSecret: config.hmacSecret?.trim() || undefined,
|
|
88
|
+
};
|
|
89
|
+
this.fetchImpl = getFetch(config.fetch);
|
|
90
|
+
}
|
|
91
|
+
async sendTemplate(request, options) {
|
|
92
|
+
const body = createBody(this.config, request);
|
|
93
|
+
const response = await this.fetchImpl(`${this.config.baseUrl}${SEND_TEMPLATE_PATH}`, {
|
|
94
|
+
method: 'POST',
|
|
95
|
+
headers: buildHeaders(this.config, body, options),
|
|
96
|
+
body,
|
|
97
|
+
signal: options?.signal,
|
|
98
|
+
});
|
|
99
|
+
if (!response.ok) {
|
|
100
|
+
throw new EmailApiError(response.status, await parseError(response));
|
|
101
|
+
}
|
|
102
|
+
return (await response.json());
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
export function createEmailClient(config) {
|
|
106
|
+
return new EmailClient(config);
|
|
107
|
+
}
|
|
108
|
+
export function createRequestSignature(body, hmacSecret) {
|
|
109
|
+
return `sha256=${createHmac('sha256', hmacSecret).update(body).digest('hex')}`;
|
|
110
|
+
}
|
|
111
|
+
//# sourceMappingURL=client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.js","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,QAAQ,CAAC;AAEhD,OAAO,EAAE,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C,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC;AACjF,CAAC"}
|
package/dist/env.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"env.d.ts","sourceRoot":"","sources":["../src/env.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,iBAAiB,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AAEjE,wBAAgB,wBAAwB,CAAC,GAAG,EAAE,cAAc,GAAG,IAAI,CAAC,iBAAiB,EAAE,OAAO,GAAG,gBAAgB,GAAG,wBAAwB,CAAC,CAgB5I"}
|
package/dist/env.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { EmailClientConfigError } from './errors';
|
|
2
|
+
export function emailClientConfigFromEnv(env) {
|
|
3
|
+
const baseUrl = String(env.EMAIL_API_BASE_URL || '').trim();
|
|
4
|
+
const apiKey = String(env.EMAIL_API_KEY || '').trim();
|
|
5
|
+
const clientId = String(env.EMAIL_CLIENT_ID || '').trim();
|
|
6
|
+
const hmacSecret = String(env.EMAIL_HMAC_SECRET || '').trim();
|
|
7
|
+
if (!baseUrl)
|
|
8
|
+
throw new EmailClientConfigError('EMAIL_API_BASE_URL is required');
|
|
9
|
+
if (!apiKey)
|
|
10
|
+
throw new EmailClientConfigError('EMAIL_API_KEY is required');
|
|
11
|
+
if (!clientId)
|
|
12
|
+
throw new EmailClientConfigError('EMAIL_CLIENT_ID is required');
|
|
13
|
+
return {
|
|
14
|
+
baseUrl,
|
|
15
|
+
apiKey,
|
|
16
|
+
clientId,
|
|
17
|
+
hmacSecret: hmacSecret || undefined,
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
//# sourceMappingURL=env.js.map
|
package/dist/env.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"env.js","sourceRoot":"","sources":["../src/env.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,sBAAsB,EAAE,MAAM,UAAU,CAAC;AAGlD,MAAM,UAAU,wBAAwB,CAAC,GAAmB;IAC1D,MAAM,OAAO,GAAG,MAAM,CAAC,GAAG,CAAC,kBAAkB,IAAI,EAAE,CAAC,CAAC,IAAI,EAAE,CAAC;IAC5D,MAAM,MAAM,GAAG,MAAM,CAAC,GAAG,CAAC,aAAa,IAAI,EAAE,CAAC,CAAC,IAAI,EAAE,CAAC;IACtD,MAAM,QAAQ,GAAG,MAAM,CAAC,GAAG,CAAC,eAAe,IAAI,EAAE,CAAC,CAAC,IAAI,EAAE,CAAC;IAC1D,MAAM,UAAU,GAAG,MAAM,CAAC,GAAG,CAAC,iBAAiB,IAAI,EAAE,CAAC,CAAC,IAAI,EAAE,CAAC;IAE9D,IAAI,CAAC,OAAO;QAAE,MAAM,IAAI,sBAAsB,CAAC,gCAAgC,CAAC,CAAC;IACjF,IAAI,CAAC,MAAM;QAAE,MAAM,IAAI,sBAAsB,CAAC,2BAA2B,CAAC,CAAC;IAC3E,IAAI,CAAC,QAAQ;QAAE,MAAM,IAAI,sBAAsB,CAAC,6BAA6B,CAAC,CAAC;IAE/E,OAAO;QACL,OAAO;QACP,MAAM;QACN,QAAQ;QACR,UAAU,EAAE,UAAU,IAAI,SAAS;KACpC,CAAC;AACJ,CAAC"}
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { EmailApiErrorPayload } from './types';
|
|
2
|
+
export declare class EmailApiError extends Error {
|
|
3
|
+
readonly status: number;
|
|
4
|
+
readonly code: string;
|
|
5
|
+
readonly requestId?: string;
|
|
6
|
+
readonly payload: EmailApiErrorPayload;
|
|
7
|
+
constructor(status: number, payload: EmailApiErrorPayload);
|
|
8
|
+
}
|
|
9
|
+
export declare class EmailClientConfigError extends Error {
|
|
10
|
+
constructor(message: string);
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=errors.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,SAAS,CAAC;AAEpD,qBAAa,aAAc,SAAQ,KAAK;IACtC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAC;IAC5B,QAAQ,CAAC,OAAO,EAAE,oBAAoB,CAAC;gBAE3B,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,oBAAoB;CAQ1D;AAED,qBAAa,sBAAuB,SAAQ,KAAK;gBACnC,OAAO,EAAE,MAAM;CAI5B"}
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export class EmailApiError extends Error {
|
|
2
|
+
constructor(status, payload) {
|
|
3
|
+
super(payload.message);
|
|
4
|
+
this.name = 'EmailApiError';
|
|
5
|
+
this.status = status;
|
|
6
|
+
this.code = payload.message;
|
|
7
|
+
this.requestId = payload.requestId;
|
|
8
|
+
this.payload = payload;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
export class EmailClientConfigError extends Error {
|
|
12
|
+
constructor(message) {
|
|
13
|
+
super(message);
|
|
14
|
+
this.name = 'EmailClientConfigError';
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAEA,MAAM,OAAO,aAAc,SAAQ,KAAK;IAMtC,YAAY,MAAc,EAAE,OAA6B;QACvD,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;QACvB,IAAI,CAAC,IAAI,GAAG,eAAe,CAAC;QAC5B,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,IAAI,GAAG,OAAO,CAAC,OAAO,CAAC;QAC5B,IAAI,CAAC,SAAS,GAAG,OAAO,CAAC,SAAS,CAAC;QACnC,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;IACzB,CAAC;CACF;AAED,MAAM,OAAO,sBAAuB,SAAQ,KAAK;IAC/C,YAAY,OAAe;QACzB,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,wBAAwB,CAAC;IACvC,CAAC;CACF"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { EmailClient, createEmailClient, createRequestSignature } from './client';
|
|
2
|
+
export { emailClientConfigFromEnv } from './env';
|
|
3
|
+
export { EmailApiError, EmailClientConfigError } from './errors';
|
|
4
|
+
export type { EmailApiErrorPayload, EmailClientConfig, EmailClientEnv, EmailRecipient, EmailTemplateData, SendTemplateOptions, SendTemplateRequest, SendTemplateResponse, } from './types';
|
|
5
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,iBAAiB,EAAE,sBAAsB,EAAE,MAAM,UAAU,CAAC;AAClF,OAAO,EAAE,wBAAwB,EAAE,MAAM,OAAO,CAAC;AACjD,OAAO,EAAE,aAAa,EAAE,sBAAsB,EAAE,MAAM,UAAU,CAAC;AACjE,YAAY,EACV,oBAAoB,EACpB,iBAAiB,EACjB,cAAc,EACd,cAAc,EACd,iBAAiB,EACjB,mBAAmB,EACnB,mBAAmB,EACnB,oBAAoB,GACrB,MAAM,SAAS,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,iBAAiB,EAAE,sBAAsB,EAAE,MAAM,UAAU,CAAC;AAClF,OAAO,EAAE,wBAAwB,EAAE,MAAM,OAAO,CAAC;AACjD,OAAO,EAAE,aAAa,EAAE,sBAAsB,EAAE,MAAM,UAAU,CAAC"}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
export type EmailRecipient = string | string[];
|
|
2
|
+
export type EmailTemplateData = Record<string, unknown>;
|
|
3
|
+
export type SendTemplateRequest = {
|
|
4
|
+
to: EmailRecipient;
|
|
5
|
+
data?: EmailTemplateData;
|
|
6
|
+
subject?: string;
|
|
7
|
+
senderDomain?: string;
|
|
8
|
+
category?: string;
|
|
9
|
+
cc?: EmailRecipient;
|
|
10
|
+
bcc?: EmailRecipient;
|
|
11
|
+
} & ({
|
|
12
|
+
templateId: string;
|
|
13
|
+
templateKey?: never;
|
|
14
|
+
version?: never;
|
|
15
|
+
} | {
|
|
16
|
+
templateId?: never;
|
|
17
|
+
templateKey: string;
|
|
18
|
+
version?: number;
|
|
19
|
+
});
|
|
20
|
+
export type SendTemplateResponse = {
|
|
21
|
+
id: string;
|
|
22
|
+
status: string;
|
|
23
|
+
provider: string;
|
|
24
|
+
providerMessageId: string | null;
|
|
25
|
+
idempotent?: boolean;
|
|
26
|
+
requestId?: string;
|
|
27
|
+
};
|
|
28
|
+
export type EmailApiErrorPayload = {
|
|
29
|
+
message: string;
|
|
30
|
+
requestId?: string;
|
|
31
|
+
hint?: string;
|
|
32
|
+
emails?: string[];
|
|
33
|
+
cap?: number;
|
|
34
|
+
[key: string]: unknown;
|
|
35
|
+
};
|
|
36
|
+
export type FetchLike = (input: RequestInfo | URL, init?: RequestInit) => Promise<Response>;
|
|
37
|
+
export type SendTemplateOptions = {
|
|
38
|
+
idempotencyKey?: string;
|
|
39
|
+
requestId?: string;
|
|
40
|
+
headers?: HeadersInit;
|
|
41
|
+
signal?: AbortSignal;
|
|
42
|
+
};
|
|
43
|
+
export type EmailClientConfig = {
|
|
44
|
+
baseUrl: string;
|
|
45
|
+
apiKey: string;
|
|
46
|
+
clientId: string;
|
|
47
|
+
hmacSecret?: string;
|
|
48
|
+
fetch?: FetchLike;
|
|
49
|
+
defaultHeaders?: HeadersInit;
|
|
50
|
+
generateIdempotencyKey?: () => string | undefined;
|
|
51
|
+
};
|
|
52
|
+
export type EmailClientEnv = {
|
|
53
|
+
EMAIL_API_BASE_URL?: string;
|
|
54
|
+
EMAIL_API_KEY?: string;
|
|
55
|
+
EMAIL_CLIENT_ID?: string;
|
|
56
|
+
EMAIL_HMAC_SECRET?: string;
|
|
57
|
+
};
|
|
58
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,cAAc,GAAG,MAAM,GAAG,MAAM,EAAE,CAAC;AAE/C,MAAM,MAAM,iBAAiB,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AAExD,MAAM,MAAM,mBAAmB,GAAG;IAChC,EAAE,EAAE,cAAc,CAAC;IACnB,IAAI,CAAC,EAAE,iBAAiB,CAAC;IACzB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,EAAE,CAAC,EAAE,cAAc,CAAC;IACpB,GAAG,CAAC,EAAE,cAAc,CAAC;CACtB,GAAG,CACA;IACE,UAAU,EAAE,MAAM,CAAC;IACnB,WAAW,CAAC,EAAE,KAAK,CAAC;IACpB,OAAO,CAAC,EAAE,KAAK,CAAC;CACjB,GACD;IACE,UAAU,CAAC,EAAE,KAAK,CAAC;IACnB,WAAW,EAAE,MAAM,CAAC;IACpB,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB,CACJ,CAAC;AAEF,MAAM,MAAM,oBAAoB,GAAG;IACjC,EAAE,EAAE,MAAM,CAAC;IACX,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,EAAE,MAAM,CAAC;IACjB,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DED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
|
package/package.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@cloudstrife21/email-sdk",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Server-side TypeScript client for the StrongStart email send API.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"sideEffects": false,
|
|
7
|
+
"main": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"files": [
|
|
10
|
+
"dist",
|
|
11
|
+
"README.md"
|
|
12
|
+
],
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"import": "./dist/index.js"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"engines": {
|
|
20
|
+
"node": ">=18"
|
|
21
|
+
},
|
|
22
|
+
"publishConfig": {
|
|
23
|
+
"access": "public"
|
|
24
|
+
},
|
|
25
|
+
"scripts": {
|
|
26
|
+
"build": "tsc -p tsconfig.build.json",
|
|
27
|
+
"prepublishOnly": "npm run build && npm test",
|
|
28
|
+
"test": "vitest run",
|
|
29
|
+
"test:watch": "vitest"
|
|
30
|
+
},
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"@types/node": "^24.5.2",
|
|
33
|
+
"typescript": "^5.8.3",
|
|
34
|
+
"vitest": "^3.2.4"
|
|
35
|
+
}
|
|
36
|
+
}
|