dymo-api 1.0.15 → 1.0.17
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/package.json +3 -1
- package/src/dymo-api.ts +19 -3
- package/src/lib/interfaces.ts +23 -0
- package/src/private-api.ts +27 -26
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dymo-api",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.17",
|
|
4
4
|
"description": "Flow system for Dymo API.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -25,6 +25,8 @@
|
|
|
25
25
|
},
|
|
26
26
|
"homepage": "https://dymo.tpeoficial.com",
|
|
27
27
|
"dependencies": {
|
|
28
|
+
"@react-email/render": "1.0.1",
|
|
29
|
+
"@types/react": "^18.3.5",
|
|
28
30
|
"axios": "^1.6.8"
|
|
29
31
|
},
|
|
30
32
|
"contributors": [
|
package/src/dymo-api.ts
CHANGED
|
@@ -4,8 +4,7 @@ import * as PublicAPI from "./public-api";
|
|
|
4
4
|
import config from "./config";
|
|
5
5
|
|
|
6
6
|
const customError = (code: number, message: string): Error => {
|
|
7
|
-
|
|
8
|
-
return Object.assign(error, { code, message: `[${config.lib.name}] ${message}` });
|
|
7
|
+
return Object.assign(new Error(), { code, message: `[${config.lib.name}] ${message}` });
|
|
9
8
|
};
|
|
10
9
|
|
|
11
10
|
interface TokensResponse {
|
|
@@ -18,17 +17,29 @@ interface Tokens {
|
|
|
18
17
|
api?: string;
|
|
19
18
|
};
|
|
20
19
|
|
|
20
|
+
interface ServerEmailConfig {
|
|
21
|
+
host: string;
|
|
22
|
+
port: number;
|
|
23
|
+
secure: boolean;
|
|
24
|
+
auth: {
|
|
25
|
+
user: string;
|
|
26
|
+
pass: string;
|
|
27
|
+
};
|
|
28
|
+
};
|
|
29
|
+
|
|
21
30
|
class DymoAPI {
|
|
22
31
|
private rootApiKey: string | null;
|
|
23
32
|
private apiKey: string | null;
|
|
24
33
|
private tokensResponse: TokensResponse | null;
|
|
25
34
|
private lastFetchTime: Date | null;
|
|
35
|
+
private serverEmailConfig?: ServerEmailConfig;
|
|
26
36
|
|
|
27
|
-
constructor({ rootApiKey = null, apiKey = null }: { rootApiKey?: string | null; apiKey?: string | null }) {
|
|
37
|
+
constructor({ rootApiKey = null, apiKey = null, serverEmailConfig = undefined }: { rootApiKey?: string | null; apiKey?: string | null; serverEmailConfig?: ServerEmailConfig; }) {
|
|
28
38
|
this.rootApiKey = rootApiKey;
|
|
29
39
|
this.apiKey = apiKey;
|
|
30
40
|
this.tokensResponse = null;
|
|
31
41
|
this.lastFetchTime = null;
|
|
42
|
+
this.serverEmailConfig = serverEmailConfig;
|
|
32
43
|
this.initializeTokens(); // Calls the function to obtain tokens when creating the object.
|
|
33
44
|
}
|
|
34
45
|
|
|
@@ -70,6 +81,11 @@ class DymoAPI {
|
|
|
70
81
|
return await PrivateAPI.isValidData(this.apiKey, data);
|
|
71
82
|
}
|
|
72
83
|
|
|
84
|
+
async sendEmail(data: any): Promise<any> {
|
|
85
|
+
if (this.serverEmailConfig) throw customError(5000, `You must configure the email client settings.`);
|
|
86
|
+
return await PrivateAPI.sendEmail(this.apiKey, { serverEmailConfig: this.serverEmailConfig, ...data });
|
|
87
|
+
}
|
|
88
|
+
|
|
73
89
|
// FUNCTIONS / Public.
|
|
74
90
|
async getPrayerTimes(data: any): Promise<any> {
|
|
75
91
|
return await PublicAPI.getPrayerTimes(data);
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type * as React from "react";
|
|
2
|
+
|
|
3
|
+
interface PhoneData {
|
|
4
|
+
iso: any;
|
|
5
|
+
phone: string;
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
interface CreditCardData {
|
|
9
|
+
pan: string | number;
|
|
10
|
+
expirationDate?: string;
|
|
11
|
+
cvc?: string | number;
|
|
12
|
+
cvv?: string | number;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export interface Validator {
|
|
16
|
+
email?: string;
|
|
17
|
+
phone?: PhoneData;
|
|
18
|
+
domain?: string;
|
|
19
|
+
creditCard?: string | CreditCardData;
|
|
20
|
+
ip?: string;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export type SendEmail = | { from: string; to: string; subject: string; html: string; react?: never } | { from: string; to: string; subject: string; html?: never; react: React.ReactNode };
|
package/src/private-api.ts
CHANGED
|
@@ -1,34 +1,14 @@
|
|
|
1
1
|
import axios from "axios";
|
|
2
|
-
import config from "./config";
|
|
2
|
+
import config from "./config";
|
|
3
|
+
import * as Interfaces from "./lib/interfaces";
|
|
4
|
+
import { renderAsync } from "@react-email/render";
|
|
3
5
|
|
|
4
6
|
const customError = (code: number, message: string): Error => {
|
|
5
|
-
|
|
6
|
-
return Object.assign(error, { code, message: `[${config.lib.name}] ${message}` });
|
|
7
|
+
return Object.assign(new Error(), { code, message: `[${config.lib.name}] ${message}` });
|
|
7
8
|
};
|
|
8
9
|
|
|
9
|
-
|
|
10
|
-
iso: any;
|
|
11
|
-
phone: string;
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
interface CreditCardData {
|
|
15
|
-
pan: string | number;
|
|
16
|
-
expirationDate?: string;
|
|
17
|
-
cvc?: string | number;
|
|
18
|
-
cvv?: string | number;
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
interface Data {
|
|
22
|
-
email?: string;
|
|
23
|
-
phone?: PhoneData;
|
|
24
|
-
domain?: string;
|
|
25
|
-
creditCard?: string | CreditCardData;
|
|
26
|
-
ip?: string;
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
export const isValidData = async (token: string | null, data: Data): Promise<any> => {
|
|
10
|
+
export const isValidData = async (token: string | null, data: Interfaces.Validator): Promise<any> => {
|
|
30
11
|
if (token === null) throw customError(3000, "Invalid private token.");
|
|
31
|
-
|
|
32
12
|
let i = false;
|
|
33
13
|
for (const key in data) {
|
|
34
14
|
if (data.hasOwnProperty(key) && (key === "email" || key === "phone" || key === "domain" || key === "creditCard" || key === "ip")) {
|
|
@@ -37,11 +17,32 @@ export const isValidData = async (token: string | null, data: Data): Promise<any
|
|
|
37
17
|
}
|
|
38
18
|
}
|
|
39
19
|
if (!i) throw customError(1500, "You must provide at least one parameter.");
|
|
40
|
-
|
|
41
20
|
try {
|
|
42
21
|
const response = await axios.post("https://api.tpeoficial.com/v1/private/secure/verify", data, { headers: { "Authorization": token } });
|
|
43
22
|
return response.data;
|
|
44
23
|
} catch (error: any) {
|
|
45
24
|
throw customError(5000, error.message);
|
|
46
25
|
}
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export const sendEmail = async (token: string | null, data: Interfaces.SendEmail): Promise<any> => {
|
|
29
|
+
if (token === null) throw customError(3000, "Invalid private token.");
|
|
30
|
+
|
|
31
|
+
if (!data.from) throw customError(1500, "You must provide an email address from which the following will be sent.");
|
|
32
|
+
if (!data.to) throw customError(1500, "You must provide an email to be sent to.");
|
|
33
|
+
if (!data.subject) throw customError(1500, "You must provide a subject for the email to be sent.");
|
|
34
|
+
if (!data.html && !data.react) throw customError(1500, "You must provide HTML or a React component.");
|
|
35
|
+
if (data.html && data.react) throw customError(1500, "You must provide only HTML or a React component, not both.");
|
|
36
|
+
try {
|
|
37
|
+
data.html = await renderAsync(data.react as React.ReactElement);
|
|
38
|
+
delete data.react;
|
|
39
|
+
} catch (error) {
|
|
40
|
+
throw customError(1500, "An error occurred while rendering your React component.");
|
|
41
|
+
}
|
|
42
|
+
try {
|
|
43
|
+
const response = await axios.post("https://api.tpeoficial.com/v1/private/sender/sendEmail", data, { headers: { "Authorization": token } });
|
|
44
|
+
return response.data;
|
|
45
|
+
} catch (error: any) {
|
|
46
|
+
throw customError(5000, error.message);
|
|
47
|
+
}
|
|
47
48
|
};
|