@tblabs/storage 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/IRepo.d.ts ADDED
@@ -0,0 +1,11 @@
1
+ export interface IRepo<T> {
2
+ Ping(): Promise<boolean>;
3
+ Delete(folder: string, id: string): Promise<void>;
4
+ Update(folder: string, id: string, item: T): Promise<void>;
5
+ GetOne(folder: string, id: string): Promise<T | null>;
6
+ FindOne(id: string): Promise<T | null>;
7
+ GetAll(folder: string): Promise<T[]>;
8
+ GetAllIds(folder: string): Promise<string[]>;
9
+ Move(id: string, sourceFolder: string, targetFolder: string): Promise<void>;
10
+ Drop(): Promise<void>;
11
+ }
package/IRepo.js ADDED
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,4 @@
1
+ export interface HttpResponse {
2
+ status: number;
3
+ data: any;
4
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,4 @@
1
+ export interface IMessage {
2
+ Name: string;
3
+ Body?: any;
4
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,4 @@
1
+ export interface ISender {
2
+ Id: string;
3
+ AuthToken?: string;
4
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,5 @@
1
+ export declare class Message {
2
+ Name: string;
3
+ Body?: any | undefined;
4
+ constructor(Name: string, Body?: any | undefined);
5
+ }
@@ -0,0 +1,8 @@
1
+ export class Message {
2
+ Name;
3
+ Body;
4
+ constructor(Name, Body) {
5
+ this.Name = Name;
6
+ this.Body = Body;
7
+ }
8
+ }
@@ -0,0 +1,12 @@
1
+ import { IMessage } from './IMessage';
2
+ import { ISender } from './ISender';
3
+ import { MessageBusResponse } from './MessageBusResponse';
4
+ export declare class MessageBus {
5
+ private serverUrl;
6
+ private user?;
7
+ constructor(serverUrl: string, user?: ISender | undefined);
8
+ SendMessage(message: IMessage): Promise<MessageBusResponse>;
9
+ private PostJSON;
10
+ Ping(): Promise<boolean>;
11
+ private SendMessagePackage;
12
+ }
@@ -0,0 +1,80 @@
1
+ import { MessageBusResponse } from './MessageBusResponse';
2
+ import { MessagePackage } from './MessagePackage';
3
+ /*
4
+ SAMPLE POST:
5
+
6
+ {
7
+ "Sender": {
8
+ "Id": "guid",
9
+ "AuthToken": "token"
10
+ },
11
+ "Message": {
12
+ "Name": "Update",
13
+ "Body": {
14
+ "Id": "number"
15
+ "Content": { "foo": "bar" },
16
+ "CreateIfNotExist": false
17
+ }
18
+ }
19
+ }
20
+ */
21
+ export class MessageBus {
22
+ serverUrl;
23
+ user;
24
+ constructor(serverUrl, user) {
25
+ this.serverUrl = serverUrl;
26
+ this.user = user;
27
+ }
28
+ async SendMessage(message) {
29
+ let messagePackage = new MessagePackage();
30
+ messagePackage.Message = message;
31
+ messagePackage.Sender = this.user || { Id: "Anonymous" };
32
+ return await this.SendMessagePackage(messagePackage);
33
+ }
34
+ async PostJSON(data) {
35
+ let httpResponse = { status: 0, data: undefined };
36
+ const dataAsString = JSON.stringify(data);
37
+ const response = await fetch(this.serverUrl, {
38
+ method: "POST",
39
+ headers: {
40
+ "Content-Type": "application/json",
41
+ },
42
+ body: dataAsString,
43
+ });
44
+ httpResponse.status = response.status;
45
+ const json = await response.json();
46
+ httpResponse.data = json;
47
+ return httpResponse;
48
+ }
49
+ async Ping() {
50
+ try {
51
+ const response = await fetch(this.serverUrl + "/ping", {
52
+ method: "GET",
53
+ });
54
+ if (response.status != 200) {
55
+ return false;
56
+ }
57
+ const body = await response.text();
58
+ return (body == "pong");
59
+ }
60
+ catch (ex) {
61
+ return false;
62
+ }
63
+ }
64
+ async SendMessagePackage(messagePackage) {
65
+ try {
66
+ const response = await this.PostJSON(messagePackage);
67
+ if (response.status == 200 && response.data.IsSuccess == true) {
68
+ const result = response.data;
69
+ return MessageBusResponse.Success(result.Result);
70
+ }
71
+ else if (response.status == 200 && response.data.IsSuccess == false) {
72
+ return MessageBusResponse.Fault(response.status, response.data.ErrorMessage);
73
+ }
74
+ return MessageBusResponse.Fault(response.status, "Non-200 status");
75
+ }
76
+ catch (ex) {
77
+ return MessageBusResponse.Fault(ex.response?.status, ex.message);
78
+ }
79
+ }
80
+ }
@@ -0,0 +1,8 @@
1
+ export declare class MessageBusResponse {
2
+ IsSuccess: boolean;
3
+ ErrorMessage: string;
4
+ ErrorCode: number;
5
+ Result?: any;
6
+ static Success(result?: any): MessageBusResponse;
7
+ static Fault(errorCode: number, errorMessage: string): MessageBusResponse;
8
+ }
@@ -0,0 +1,20 @@
1
+ export class MessageBusResponse {
2
+ IsSuccess = false;
3
+ ErrorMessage = "";
4
+ ErrorCode = (-1);
5
+ Result = undefined;
6
+ static Success(result) {
7
+ const response = new MessageBusResponse();
8
+ response.IsSuccess = true;
9
+ response.Result = result;
10
+ return response;
11
+ }
12
+ static Fault(errorCode, errorMessage) {
13
+ const response = new MessageBusResponse();
14
+ response.IsSuccess = false;
15
+ response.Result = undefined;
16
+ response.ErrorCode = errorCode || (-1);
17
+ response.ErrorMessage = errorMessage;
18
+ return response;
19
+ }
20
+ }
@@ -0,0 +1,6 @@
1
+ import { IMessage } from "./IMessage";
2
+ import { ISender } from "./ISender";
3
+ export declare class MessagePackage {
4
+ Sender?: ISender;
5
+ Message: IMessage;
6
+ }
@@ -0,0 +1,4 @@
1
+ export class MessagePackage {
2
+ Sender;
3
+ Message;
4
+ }
@@ -0,0 +1,4 @@
1
+ import { Message } from '../MessageBus/Message';
2
+ export declare class DeleteCommand extends Message {
3
+ constructor(Id: string, Folder: string);
4
+ }
@@ -0,0 +1,6 @@
1
+ import { Message } from '../MessageBus/Message';
2
+ export class DeleteCommand extends Message {
3
+ constructor(Id, Folder) {
4
+ super("Delete", { Id, Folder });
5
+ }
6
+ }
@@ -0,0 +1,4 @@
1
+ import { Message } from '../MessageBus/Message';
2
+ export declare class DropCommand extends Message {
3
+ constructor(Folder: string);
4
+ }
@@ -0,0 +1,6 @@
1
+ import { Message } from '../MessageBus/Message';
2
+ export class DropCommand extends Message {
3
+ constructor(Folder) {
4
+ super("Drop", { Folder });
5
+ }
6
+ }
@@ -0,0 +1,4 @@
1
+ import { Message } from '../MessageBus/Message';
2
+ export declare class FindQuery extends Message {
3
+ constructor(Id: string, Folder: string);
4
+ }
@@ -0,0 +1,6 @@
1
+ import { Message } from '../MessageBus/Message';
2
+ export class FindQuery extends Message {
3
+ constructor(Id, Folder) {
4
+ super("Find", { Id, Folder });
5
+ }
6
+ }
@@ -0,0 +1,4 @@
1
+ import { Message } from '../MessageBus/Message';
2
+ export declare class ListQuery extends Message {
3
+ constructor(Folder: string, AttachFilesContent: boolean);
4
+ }
@@ -0,0 +1,6 @@
1
+ import { Message } from '../MessageBus/Message';
2
+ export class ListQuery extends Message {
3
+ constructor(Folder, AttachFilesContent) {
4
+ super("List", { Folder, AttachFilesContent });
5
+ }
6
+ }
@@ -0,0 +1,4 @@
1
+ import { Message } from '../MessageBus/Message';
2
+ export declare class MoveCommand extends Message {
3
+ constructor(Id: string, From: string, To: string);
4
+ }
@@ -0,0 +1,6 @@
1
+ import { Message } from '../MessageBus/Message';
2
+ export class MoveCommand extends Message {
3
+ constructor(Id, From, To) {
4
+ super("Move", { Id, From, To });
5
+ }
6
+ }
@@ -0,0 +1,4 @@
1
+ import { Message } from '../MessageBus/Message';
2
+ export declare class ReadQuery extends Message {
3
+ constructor(Id: string, Folder: string);
4
+ }
@@ -0,0 +1,6 @@
1
+ import { Message } from '../MessageBus/Message';
2
+ export class ReadQuery extends Message {
3
+ constructor(Id, Folder) {
4
+ super("Read", { Id, Folder });
5
+ }
6
+ }
@@ -0,0 +1,4 @@
1
+ import { Message } from '../MessageBus/Message';
2
+ export declare class UpdateCommand extends Message {
3
+ constructor(Id: string, Folder: string, Content: object);
4
+ }
@@ -0,0 +1,6 @@
1
+ import { Message } from '../MessageBus/Message';
2
+ export class UpdateCommand extends Message {
3
+ constructor(Id, Folder, Content) {
4
+ super("Update", { Id, Folder, Content });
5
+ }
6
+ }
@@ -0,0 +1,18 @@
1
+ import { IRepo } from "./IRepo";
2
+ import { MessageBus } from "./MessageBus/MessageBus";
3
+ export declare class OnlineRepo<T extends object> implements IRepo<T> {
4
+ private _bus;
5
+ private storageFolder;
6
+ LOG: boolean;
7
+ constructor(_bus: MessageBus, storageFolder: string);
8
+ private Log;
9
+ Ping(): Promise<boolean>;
10
+ GetOne(folder: string, id: string): Promise<T | null>;
11
+ FindOne(id: string): Promise<T | null>;
12
+ GetAll(folder: string): Promise<T[]>;
13
+ GetAllIds(folder: string): Promise<string[]>;
14
+ Update(folder: string, id: string, item: T): Promise<void>;
15
+ Delete(folder: string, id: string): Promise<void>;
16
+ Drop(): Promise<void>;
17
+ Move(id: string, sourceFolder: string, targetFolder: string): Promise<void>;
18
+ }
package/OnlineRepo.js ADDED
@@ -0,0 +1,103 @@
1
+ import { DeleteCommand } from "./Messages/DeleteCommand";
2
+ import { DropCommand } from "./Messages/DropCommand";
3
+ import { FindQuery } from "./Messages/FindQuery";
4
+ import { ListQuery } from "./Messages/ListQuery";
5
+ import { MoveCommand } from "./Messages/MoveCommand";
6
+ import { ReadQuery } from "./Messages/ReadQuery";
7
+ import { UpdateCommand } from "./Messages/UpdateCommand";
8
+ export class OnlineRepo {
9
+ _bus;
10
+ storageFolder;
11
+ LOG = false;
12
+ constructor(_bus, storageFolder) {
13
+ this._bus = _bus;
14
+ this.storageFolder = storageFolder;
15
+ }
16
+ Log(...message) {
17
+ if (this.LOG)
18
+ console.log('[OnlineRepo]', ...message);
19
+ }
20
+ async Ping() {
21
+ return await this._bus.Ping();
22
+ }
23
+ async GetOne(folder, id) {
24
+ this.Log(`Fetching "${id}"...`);
25
+ const folderPath = this.storageFolder + "/" + folder;
26
+ const queryResult = await this._bus.SendMessage(new ReadQuery(id, folderPath));
27
+ if (queryResult.IsSuccess) {
28
+ const item = queryResult.Result;
29
+ this.Log(`Found "${id}" in "${folderPath}".`);
30
+ return item;
31
+ }
32
+ this.Log(`Not found "${id}" in "${folderPath}".`);
33
+ return null;
34
+ }
35
+ async FindOne(id) {
36
+ this.Log(`Fetching "${id}"...`);
37
+ const queryResult = await this._bus.SendMessage(new FindQuery(id, this.storageFolder));
38
+ if (queryResult.IsSuccess) {
39
+ const item = queryResult.Result;
40
+ this.Log(`Found "${id}".`);
41
+ return item;
42
+ }
43
+ this.Log(`Not found "${id}".`);
44
+ return null;
45
+ }
46
+ async GetAll(folder) {
47
+ this.Log(`Fetching all from "${this.storageFolder}"...`);
48
+ const folderPath = this.storageFolder + "/" + folder;
49
+ const queryResult = await this._bus.SendMessage(new ListQuery(folderPath, true));
50
+ if (queryResult.IsSuccess) {
51
+ const items = queryResult.Result;
52
+ this.Log(`Fetched ${items.length} items from "${folderPath}".`);
53
+ return items;
54
+ }
55
+ else
56
+ throw new Error(`Could not fetch all from "${folder}": ${queryResult.ErrorMessage}`);
57
+ }
58
+ async GetAllIds(folder) {
59
+ this.Log(`Fetching all from "${this.storageFolder}"...`);
60
+ const folderPath = this.storageFolder + "/" + folder;
61
+ const queryResult = await this._bus.SendMessage(new ListQuery(folderPath, false));
62
+ if (queryResult.IsSuccess) {
63
+ const itemsIds = Object.values(queryResult.Result);
64
+ this.Log(`Fetched ${itemsIds.length} items ids from "${folderPath}".`);
65
+ return itemsIds;
66
+ }
67
+ else
68
+ throw new Error(`Could not fetch all ids from "${folder}": ${queryResult.ErrorMessage}`);
69
+ }
70
+ async Update(folder, id, item) {
71
+ this.Log(`Updating "${id}"...`);
72
+ const folderPath = this.storageFolder + "/" + folder;
73
+ const result = await this._bus.SendMessage(new UpdateCommand(id, folderPath, item));
74
+ if (!result.IsSuccess)
75
+ throw new Error(`Could not update "${id}" in "${folderPath}": ${result.ErrorMessage}`);
76
+ this.Log(`Updated "${id}" in "${folderPath}".`);
77
+ }
78
+ ;
79
+ async Delete(folder, id) {
80
+ const folderPath = this.storageFolder + "/" + folder;
81
+ this.Log(`Deleting "${id}" from "${folderPath}"...`);
82
+ const result = await this._bus.SendMessage(new DeleteCommand(id, folderPath));
83
+ if (!result.IsSuccess)
84
+ throw new Error(`Could not delete "${id}" from "${folderPath}": ${result.ErrorMessage}`);
85
+ this.Log(`Deleted "${id}" from "${folderPath}".`);
86
+ }
87
+ async Drop() {
88
+ this.Log(`Dropping "${this.storageFolder}"...`);
89
+ const result = await this._bus.SendMessage(new DropCommand(this.storageFolder));
90
+ if (!result.IsSuccess)
91
+ throw new Error(`Could not drop "${this.storageFolder}": ${result.ErrorMessage}`);
92
+ this.Log(`Dropped "${this.storageFolder}".`);
93
+ }
94
+ async Move(id, sourceFolder, targetFolder) {
95
+ const source = this.storageFolder + "/" + sourceFolder;
96
+ const target = this.storageFolder + "/" + targetFolder;
97
+ this.Log(`Moving "${id}" from "${source}" to "${target}"...`);
98
+ const result = await this._bus.SendMessage(new MoveCommand(id, source, target));
99
+ if (!result.IsSuccess)
100
+ throw new Error(`Could not move "${id}" from "${source}" to "${target}": ${result.ErrorMessage}`);
101
+ this.Log(`Moved "${id}" from "${source}" to "${target}".`);
102
+ }
103
+ }
package/index.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from "./IRepo";
2
+ export * from "./OnlineRepo";
package/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export * from "./IRepo";
2
+ export * from "./OnlineRepo";
package/package.json ADDED
@@ -0,0 +1,17 @@
1
+ {
2
+ "name": "@tblabs/storage",
3
+ "version": "1.0.0",
4
+ "description": "online storage module",
5
+ "license": "beerware",
6
+ "main": "index.js",
7
+ "types": "index.d.ts",
8
+ "scripts": {
9
+ "pack": "npm pack",
10
+ "clean": "rm ./bin -rf",
11
+ "build": "npm run clean && tsc ./src/index.ts --declaration --outDir ./bin && cp ./package.json ./bin/package.json",
12
+ "publish": "npm run build && npm run pack && cd ./bin && npm publish --access public"
13
+ },
14
+ "devDependencies": {
15
+ "typescript": "^6.0.2"
16
+ }
17
+ }