@datablock-dev/sdk 1.0.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 +78 -0
- package/dist/index.d.mts +41 -0
- package/dist/index.d.ts +41 -0
- package/dist/index.js +92 -0
- package/dist/index.mjs +66 -0
- package/package.json +38 -0
package/README.md
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
# @datablock/sdk
|
|
2
|
+
|
|
3
|
+
Official Datablock SDK for Node.js.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @datablock/sdk
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
import { Datablock } from "@datablock/sdk";
|
|
15
|
+
|
|
16
|
+
const client = new Datablock({
|
|
17
|
+
apiKey: "your-api-key",
|
|
18
|
+
projectId: "your-project-id",
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
// Send to a single recipient
|
|
22
|
+
const result = await client.email.send({
|
|
23
|
+
credentialId: "your-credential-id",
|
|
24
|
+
to: "recipient@example.com",
|
|
25
|
+
subject: "Hello from Datablock",
|
|
26
|
+
html: "<h1>Hello!</h1><p>This is a test email.</p>",
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
console.log(result.data.messageId);
|
|
30
|
+
|
|
31
|
+
// Send to multiple recipients (up to 50)
|
|
32
|
+
await client.email.send({
|
|
33
|
+
credentialId: "your-credential-id",
|
|
34
|
+
to: ["alice@example.com", "bob@example.com"],
|
|
35
|
+
subject: "Team Update",
|
|
36
|
+
text: "Plain text email body",
|
|
37
|
+
replyTo: "noreply@yourdomain.com",
|
|
38
|
+
});
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Configuration
|
|
42
|
+
|
|
43
|
+
| Option | Required | Default | Description |
|
|
44
|
+
| ----------- | -------- | ---------------------------- | -------------------------- |
|
|
45
|
+
| `apiKey` | Yes | — | Your Datablock API key |
|
|
46
|
+
| `projectId` | Yes | — | Your Datablock project ID |
|
|
47
|
+
| `baseUrl` | No | `https://api.datablock.dev` | API base URL override |
|
|
48
|
+
|
|
49
|
+
## Email
|
|
50
|
+
|
|
51
|
+
### `client.email.send(params)`
|
|
52
|
+
|
|
53
|
+
| Parameter | Type | Required | Description |
|
|
54
|
+
| -------------- | -------------------- | -------- | ------------------------------------ |
|
|
55
|
+
| `credentialId` | `string` | Yes | SMTP credential ID |
|
|
56
|
+
| `to` | `string \| string[]` | Yes | Recipient(s), max 50 |
|
|
57
|
+
| `subject` | `string` | Yes | Email subject line |
|
|
58
|
+
| `html` | `string` | No* | HTML body |
|
|
59
|
+
| `text` | `string` | No* | Plain text body |
|
|
60
|
+
| `replyTo` | `string \| string[]` | No | Reply-to address(es), max 5 |
|
|
61
|
+
|
|
62
|
+
\* At least one of `html` or `text` must be provided.
|
|
63
|
+
|
|
64
|
+
## Error Handling
|
|
65
|
+
|
|
66
|
+
```typescript
|
|
67
|
+
import { Datablock, DatablockError } from "@datablock/sdk";
|
|
68
|
+
|
|
69
|
+
try {
|
|
70
|
+
await client.email.send({ ... });
|
|
71
|
+
} catch (err) {
|
|
72
|
+
if (err instanceof DatablockError) {
|
|
73
|
+
console.error(err.code); // e.g. "INSUFFICIENT_SCOPE"
|
|
74
|
+
console.error(err.status); // e.g. 403
|
|
75
|
+
console.error(err.message); // Human-readable message
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
```
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
interface DatablockConfig {
|
|
2
|
+
apiKey: string;
|
|
3
|
+
projectId: string;
|
|
4
|
+
baseUrl?: string;
|
|
5
|
+
}
|
|
6
|
+
interface SendEmailParams {
|
|
7
|
+
credentialId: string;
|
|
8
|
+
to: string | string[];
|
|
9
|
+
subject: string;
|
|
10
|
+
html?: string;
|
|
11
|
+
text?: string;
|
|
12
|
+
replyTo?: string | string[];
|
|
13
|
+
}
|
|
14
|
+
interface SendEmailResponse {
|
|
15
|
+
success: true;
|
|
16
|
+
data: {
|
|
17
|
+
messageId: string;
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
declare class DatablockError extends Error {
|
|
21
|
+
readonly code: string;
|
|
22
|
+
readonly status: number;
|
|
23
|
+
constructor(status: number, code: string, message: string);
|
|
24
|
+
}
|
|
25
|
+
declare class EmailService {
|
|
26
|
+
private readonly client;
|
|
27
|
+
constructor(client: Datablock);
|
|
28
|
+
send(params: SendEmailParams): Promise<SendEmailResponse>;
|
|
29
|
+
}
|
|
30
|
+
declare class Datablock {
|
|
31
|
+
/** @internal */
|
|
32
|
+
readonly apiKey: string;
|
|
33
|
+
/** @internal */
|
|
34
|
+
readonly projectId: string;
|
|
35
|
+
/** @internal */
|
|
36
|
+
readonly baseUrl: string;
|
|
37
|
+
readonly email: EmailService;
|
|
38
|
+
constructor(config: DatablockConfig);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export { Datablock, type DatablockConfig, DatablockError, type SendEmailParams, type SendEmailResponse, Datablock as default };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
interface DatablockConfig {
|
|
2
|
+
apiKey: string;
|
|
3
|
+
projectId: string;
|
|
4
|
+
baseUrl?: string;
|
|
5
|
+
}
|
|
6
|
+
interface SendEmailParams {
|
|
7
|
+
credentialId: string;
|
|
8
|
+
to: string | string[];
|
|
9
|
+
subject: string;
|
|
10
|
+
html?: string;
|
|
11
|
+
text?: string;
|
|
12
|
+
replyTo?: string | string[];
|
|
13
|
+
}
|
|
14
|
+
interface SendEmailResponse {
|
|
15
|
+
success: true;
|
|
16
|
+
data: {
|
|
17
|
+
messageId: string;
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
declare class DatablockError extends Error {
|
|
21
|
+
readonly code: string;
|
|
22
|
+
readonly status: number;
|
|
23
|
+
constructor(status: number, code: string, message: string);
|
|
24
|
+
}
|
|
25
|
+
declare class EmailService {
|
|
26
|
+
private readonly client;
|
|
27
|
+
constructor(client: Datablock);
|
|
28
|
+
send(params: SendEmailParams): Promise<SendEmailResponse>;
|
|
29
|
+
}
|
|
30
|
+
declare class Datablock {
|
|
31
|
+
/** @internal */
|
|
32
|
+
readonly apiKey: string;
|
|
33
|
+
/** @internal */
|
|
34
|
+
readonly projectId: string;
|
|
35
|
+
/** @internal */
|
|
36
|
+
readonly baseUrl: string;
|
|
37
|
+
readonly email: EmailService;
|
|
38
|
+
constructor(config: DatablockConfig);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export { Datablock, type DatablockConfig, DatablockError, type SendEmailParams, type SendEmailResponse, Datablock as default };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
Datablock: () => Datablock,
|
|
24
|
+
DatablockError: () => DatablockError,
|
|
25
|
+
default: () => index_default
|
|
26
|
+
});
|
|
27
|
+
module.exports = __toCommonJS(index_exports);
|
|
28
|
+
var DatablockError = class extends Error {
|
|
29
|
+
constructor(status, code, message) {
|
|
30
|
+
super(message);
|
|
31
|
+
this.name = "DatablockError";
|
|
32
|
+
this.code = code;
|
|
33
|
+
this.status = status;
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
var EmailService = class {
|
|
37
|
+
constructor(client) {
|
|
38
|
+
this.client = client;
|
|
39
|
+
}
|
|
40
|
+
async send(params) {
|
|
41
|
+
if (!params.to) throw new Error("to is required");
|
|
42
|
+
if (!params.subject) throw new Error("subject is required");
|
|
43
|
+
if (!params.html && !params.text) {
|
|
44
|
+
throw new Error("Either html or text must be provided");
|
|
45
|
+
}
|
|
46
|
+
const url = `${this.client.baseUrl}/v1/projects/${this.client.projectId}/email/send`;
|
|
47
|
+
const res = await fetch(url, {
|
|
48
|
+
method: "POST",
|
|
49
|
+
headers: {
|
|
50
|
+
"Content-Type": "application/json",
|
|
51
|
+
Authorization: `Bearer ${this.client.apiKey}`
|
|
52
|
+
},
|
|
53
|
+
body: JSON.stringify({
|
|
54
|
+
credentialId: params.credentialId,
|
|
55
|
+
to: params.to,
|
|
56
|
+
subject: params.subject,
|
|
57
|
+
html: params.html,
|
|
58
|
+
text: params.text,
|
|
59
|
+
replyTo: params.replyTo
|
|
60
|
+
})
|
|
61
|
+
});
|
|
62
|
+
const body = await res.json();
|
|
63
|
+
if (!res.ok) {
|
|
64
|
+
const err = body;
|
|
65
|
+
throw new DatablockError(
|
|
66
|
+
res.status,
|
|
67
|
+
err.error?.code ?? "UNKNOWN",
|
|
68
|
+
err.error?.message ?? "Unknown error"
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
return body;
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
var Datablock = class {
|
|
75
|
+
constructor(config) {
|
|
76
|
+
if (!config.apiKey) throw new Error("apiKey is required");
|
|
77
|
+
if (!config.projectId) throw new Error("projectId is required");
|
|
78
|
+
this.apiKey = config.apiKey;
|
|
79
|
+
this.projectId = config.projectId;
|
|
80
|
+
this.baseUrl = (config.baseUrl ?? "https://api.datablock.dev").replace(
|
|
81
|
+
/\/$/,
|
|
82
|
+
""
|
|
83
|
+
);
|
|
84
|
+
this.email = new EmailService(this);
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
var index_default = Datablock;
|
|
88
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
89
|
+
0 && (module.exports = {
|
|
90
|
+
Datablock,
|
|
91
|
+
DatablockError
|
|
92
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
var DatablockError = class extends Error {
|
|
3
|
+
constructor(status, code, message) {
|
|
4
|
+
super(message);
|
|
5
|
+
this.name = "DatablockError";
|
|
6
|
+
this.code = code;
|
|
7
|
+
this.status = status;
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
var EmailService = class {
|
|
11
|
+
constructor(client) {
|
|
12
|
+
this.client = client;
|
|
13
|
+
}
|
|
14
|
+
async send(params) {
|
|
15
|
+
if (!params.to) throw new Error("to is required");
|
|
16
|
+
if (!params.subject) throw new Error("subject is required");
|
|
17
|
+
if (!params.html && !params.text) {
|
|
18
|
+
throw new Error("Either html or text must be provided");
|
|
19
|
+
}
|
|
20
|
+
const url = `${this.client.baseUrl}/v1/projects/${this.client.projectId}/email/send`;
|
|
21
|
+
const res = await fetch(url, {
|
|
22
|
+
method: "POST",
|
|
23
|
+
headers: {
|
|
24
|
+
"Content-Type": "application/json",
|
|
25
|
+
Authorization: `Bearer ${this.client.apiKey}`
|
|
26
|
+
},
|
|
27
|
+
body: JSON.stringify({
|
|
28
|
+
credentialId: params.credentialId,
|
|
29
|
+
to: params.to,
|
|
30
|
+
subject: params.subject,
|
|
31
|
+
html: params.html,
|
|
32
|
+
text: params.text,
|
|
33
|
+
replyTo: params.replyTo
|
|
34
|
+
})
|
|
35
|
+
});
|
|
36
|
+
const body = await res.json();
|
|
37
|
+
if (!res.ok) {
|
|
38
|
+
const err = body;
|
|
39
|
+
throw new DatablockError(
|
|
40
|
+
res.status,
|
|
41
|
+
err.error?.code ?? "UNKNOWN",
|
|
42
|
+
err.error?.message ?? "Unknown error"
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
return body;
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
var Datablock = class {
|
|
49
|
+
constructor(config) {
|
|
50
|
+
if (!config.apiKey) throw new Error("apiKey is required");
|
|
51
|
+
if (!config.projectId) throw new Error("projectId is required");
|
|
52
|
+
this.apiKey = config.apiKey;
|
|
53
|
+
this.projectId = config.projectId;
|
|
54
|
+
this.baseUrl = (config.baseUrl ?? "https://api.datablock.dev").replace(
|
|
55
|
+
/\/$/,
|
|
56
|
+
""
|
|
57
|
+
);
|
|
58
|
+
this.email = new EmailService(this);
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
var index_default = Datablock;
|
|
62
|
+
export {
|
|
63
|
+
Datablock,
|
|
64
|
+
DatablockError,
|
|
65
|
+
index_default as default
|
|
66
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@datablock-dev/sdk",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Datablock SDK for Node.js",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"module": "dist/index.mjs",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"publishConfig": {
|
|
9
|
+
"access": "public",
|
|
10
|
+
"registry": "https://registry.npmjs.org/"
|
|
11
|
+
},
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"import": "./dist/index.mjs",
|
|
15
|
+
"require": "./dist/index.js",
|
|
16
|
+
"types": "./dist/index.d.ts"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"dist"
|
|
21
|
+
],
|
|
22
|
+
"scripts": {
|
|
23
|
+
"build": "tsup src/index.ts --format cjs,esm --dts",
|
|
24
|
+
"prepublishOnly": "npm run build"
|
|
25
|
+
},
|
|
26
|
+
"keywords": [
|
|
27
|
+
"datablock",
|
|
28
|
+
"sdk"
|
|
29
|
+
],
|
|
30
|
+
"license": "MIT",
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"tsup": "^8.0.0",
|
|
33
|
+
"typescript": "^5.0.0"
|
|
34
|
+
},
|
|
35
|
+
"engines": {
|
|
36
|
+
"node": ">=18"
|
|
37
|
+
}
|
|
38
|
+
}
|