Преглед изворни кода

Add `temp-email` driver implementation to the SDK

Nariman Jelveh пре 1 година
родитељ
комит
233a2719c6
2 измењених фајлова са 66 додато и 1 уклоњено
  1. 4 1
      packages/puter-js/src/index.js
  2. 62 0
      packages/puter-js/src/modules/Email.js

+ 4 - 1
packages/puter-js/src/index.js

@@ -1,6 +1,7 @@
 import OS from './modules/OS.js';
 import FileSystem from './modules/FileSystem/index.js';
 import Hosting from './modules/Hosting.js';
+import Email from './modules/Email.js';
 import Apps from './modules/Apps.js';
 import UI from './modules/UI.js';
 import KV from './modules/KV.js';
@@ -196,6 +197,8 @@ window.puter = (function() {
             this.ui = new UI(this.appInstanceID, this.parentInstanceID, this.appID, this.env, this.util);
             // Hosting
             this.hosting = new Hosting(this.authToken, this.APIOrigin, this.appID, this.env);
+            // Email
+            this.email = new Email(this.authToken, this.APIOrigin, this.appID);
             // Apps
             this.apps = new Apps(this.authToken, this.APIOrigin, this.appID, this.env);
             // AI
@@ -208,7 +211,7 @@ window.puter = (function() {
 
         updateSubmodules() {
             // Update submodules with new auth token and API origin
-            [this.os, this.fs, this.hosting, this.apps, this.ai, this.kv].forEach(module => {
+            [this.os, this.fs, this.hosting, this.email, this.apps, this.ai, this.kv].forEach(module => {
                 if(!module) return;
                 module.setAuthToken(this.authToken);
                 module.setAPIOrigin(this.APIOrigin);

+ 62 - 0
packages/puter-js/src/modules/Email.js

@@ -0,0 +1,62 @@
+import * as utils from '../lib/utils.js'
+
+class Email{
+    /**
+     * Creates a new instance with the given authentication token, API origin, and app ID,
+     *
+     * @class
+     * @param {string} authToken - Token used to authenticate the user.
+     * @param {string} APIOrigin - Origin of the API server. Used to build the API endpoint URLs.
+     * @param {string} appID - ID of the app to use.
+     */
+    constructor (authToken, APIOrigin, appID) {
+        this.authToken = authToken;
+        this.APIOrigin = APIOrigin;
+        this.appID = appID;
+    }
+
+    /**
+     * Sets a new authentication token.
+     *
+     * @param {string} authToken - The new authentication token.
+     * @memberof [Email]
+     * @returns {void}
+     */
+    setAuthToken (authToken) {
+        this.authToken = authToken;
+    }
+
+    /**
+     * Sets the API origin.
+     * 
+     * @param {string} APIOrigin - The new API origin.
+     * @memberof [Email]
+     * @returns {void}
+     */
+    setAPIOrigin (APIOrigin) {
+        this.APIOrigin = APIOrigin;
+    }
+
+    send = async(...args) => {
+        let options = {};
+
+        // arguments are required
+        if(!args || args.length === 0){
+            throw ({message: 'Arguments are required', code: 'arguments_required'});
+        }
+
+        if(typeof args[0] === 'object'){
+            options = args[0];
+        }else{
+            options = {
+                to: args[0],
+                subject: args[1],
+                html: args[2]
+            }
+        }
+
+        return utils.make_driver_method(['to', 'subject', 'html'], 'temp-email', 'send').call(this, options);
+    }
+}
+
+export default Email