user 6 rokov pred
rodič
commit
1b55faeaa1

+ 2 - 0
lib/index.d.ts

@@ -1,5 +1,6 @@
 import { Storage } from './storage';
 import { IWebClient } from './webclient';
+import { UploadItemParameters } from './upload-item-parameters';
 export declare class BankClient {
     private urlBase;
     private storage;
@@ -12,6 +13,7 @@ export declare class BankClient {
     bootstrap(): any;
     getNonce(): Promise<number>;
     getBalance(): Promise<number>;
+    upload(recip: string, params: UploadItemParameters): Promise<any>;
     private getPriv;
     private makePlaintextPayload;
 }

+ 34 - 0
lib/index.js

@@ -101,6 +101,40 @@ class BankClient {
             return postResponse.balance;
         });
     }
+    upload(recip, params) {
+        return __awaiter(this, void 0, void 0, function* () {
+            const url = this.urlBase + '/bank/upload';
+            const formData = {};
+            if (params.fileData) {
+                formData.file = {
+                    value: params.fileData,
+                    options: {
+                        filename: params.fileName
+                    }
+                };
+            }
+            if (params.thumbFileData) {
+                formData.thumb = {
+                    value: params.thumbFileData,
+                    options: {
+                        filename: params.thumbFileName
+                    }
+                };
+            }
+            for (const attr of ['title', 'text', 'type']) {
+                if (params[attr] != null) {
+                    formData[attr] = params[attr];
+                }
+            }
+            const uploadResponse = yield this.webClient.requestJSON({
+                formData,
+                method: 'POST',
+                url
+            });
+            console.log('uploadResponse', uploadResponse);
+            return uploadResponse.hash;
+        });
+    }
     getPriv() {
         if (!this.privateKey) {
             throw new Error('missing private key');

Rozdielové dáta súboru neboli zobrazené, pretože súbor je príliš veľký
+ 1 - 1
lib/index.js.map


+ 9 - 0
lib/upload-item-parameters.d.ts

@@ -0,0 +1,9 @@
+export interface UploadItemParameters {
+    type?: string;
+    title: string;
+    text?: string;
+    fileName?: string;
+    fileData?: File;
+    thumbFileName?: string;
+    thumbFileData?: string;
+}

+ 3 - 0
lib/upload-item-parameters.js

@@ -0,0 +1,3 @@
+"use strict";
+Object.defineProperty(exports, "__esModule", { value: true });
+//# sourceMappingURL=upload-item-parameters.js.map

+ 1 - 0
lib/upload-item-parameters.js.map

@@ -0,0 +1 @@
+{"version":3,"file":"upload-item-parameters.js","sourceRoot":"","sources":["../src/upload-item-parameters.ts"],"names":[],"mappings":""}

+ 10 - 2
lib/webclient-node.js

@@ -13,9 +13,9 @@ class WebClientNode {
     request(options) {
         return __awaiter(this, void 0, void 0, function* () {
             const rpOptions = {
+                body: options.body,
                 method: options.method,
                 uri: options.url,
-                body: options.body,
                 headers: options.headers
             };
             const result = yield request(rpOptions);
@@ -34,7 +34,15 @@ class WebClientNode {
                 }
             }
             options.headers.accept = 'application/json';
-            return JSON.parse(yield this.request(options));
+            const rpOptions = {
+                body: options.body,
+                formData: options.formData || {},
+                headers: options.headers,
+                method: options.method,
+                uri: options.url,
+            };
+            const result = yield request(rpOptions);
+            return JSON.parse(result);
         });
     }
 }

Rozdielové dáta súboru neboli zobrazené, pretože súbor je príliš veľký
+ 1 - 1
lib/webclient-node.js.map


+ 1 - 0
lib/webclient-options.d.ts

@@ -3,4 +3,5 @@ export interface IWebClientOptions {
     url: string;
     headers?: any;
     body?: any;
+    formData?: any;
 }

+ 36 - 3
src/index.ts

@@ -4,7 +4,7 @@ import RsaPrivateKey = crypto.keys;
 import { Storage } from './storage';
 import { IWebClient } from './webclient';
 const TextEncoder = util.TextEncoder;
-
+import { UploadItemParameters } from './upload-item-parameters';
 import { encodeHex } from './util';
 import WebClientNode from './webclient-node';
 
@@ -95,8 +95,39 @@ export class BankClient {
       });
       return postResponse.balance;
     }
-
-
+    
+    public async upload(recip: string, params: UploadItemParameters) {
+      const url = this.urlBase + '/bank/upload';
+      const formData: any = {};
+      if (params.fileData) {
+        formData.file = {
+          value: params.fileData,
+          options: {
+            filename: params.fileName
+          }
+        };
+      }
+      if (params.thumbFileData) {
+        formData.thumb = {
+          value: params.thumbFileData,
+          options: {
+            filename: params.thumbFileName
+          }
+        };
+      }
+      for (const attr of ['title', 'text', 'type']) {
+        if (params[attr] != null) {
+          formData[attr] = params[attr];
+        }
+      }
+      const uploadResponse: any = await this.webClient.requestJSON({
+        formData,
+        method: 'POST',
+        url
+      });
+      console.log('uploadResponse', uploadResponse);
+      return uploadResponse.hash;
+    }
 
     private getPriv(): RsaPrivateKey {
       if (!this.privateKey) {
@@ -134,4 +165,6 @@ export class BankClient {
         });
       });
     }
+
+    
 }

+ 9 - 0
src/upload-item-parameters.ts

@@ -0,0 +1,9 @@
+export interface UploadItemParameters {
+    type?: string;
+    title: string;
+    text?: string;
+    fileName?: string;
+    fileData?: File;
+    thumbFileName?: string;
+    thumbFileData?: string;
+}

+ 10 - 2
src/webclient-node.ts

@@ -6,9 +6,9 @@ export default class WebClientNode implements IWebClient {
 
     public async request(options: IWebClientOptions): Promise<string> {
         const rpOptions = {
+            body: options.body,
             method: options.method,
             uri: options.url,
-            body: options.body,
             headers: options.headers
         };
         const result = await request(rpOptions);
@@ -26,6 +26,14 @@ export default class WebClientNode implements IWebClient {
             }
         }
         options.headers.accept = 'application/json';
-        return JSON.parse(await this.request(options));
+        const rpOptions = {
+            body: options.body,
+            formData: options.formData || {},
+            headers: options.headers,
+            method: options.method,
+            uri: options.url,
+        };
+        const result = await request(rpOptions);
+        return JSON.parse(result);
     }
 }

+ 1 - 0
src/webclient-options.ts

@@ -5,4 +5,5 @@ export interface IWebClientOptions {
     url: string;
     headers?: any;
     body?: any;
+    formData?: any;
 }