money-lover-app-client 1.0.5 → 1.0.6

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/dist/client.d.ts CHANGED
@@ -13,7 +13,7 @@ declare class MoneyLoverClient {
13
13
  getWallets(): Promise<MoneyLoverWallet[]>;
14
14
  getCategories(walletId: string): Promise<MoneyLoverCategory[]>;
15
15
  getTransactions(walletId: string, startDate: Date, endDate: Date): Promise<any>;
16
- addTransaction(transaction: MoneyLoverTransaction): Promise<any>;
16
+ addTransaction(transaction: MoneyLoverTransaction): Promise<MoneyLoverTransaction>;
17
17
  }
18
18
  export default MoneyLoverClient;
19
19
  export declare const NewMoneyLoverClient: () => MoneyLoverClient;
@@ -1,5 +1,5 @@
1
1
  import MoneyLoverClient from "../client";
2
2
  import { MoneyLoverTransaction } from "../interfaces/transaction";
3
3
  declare const getTransactions: (client: MoneyLoverClient, walletId: string, startDate: Date, endDate: Date) => Promise<any>;
4
- declare const addTransaction: (client: MoneyLoverClient, transaction: MoneyLoverTransaction) => Promise<any>;
4
+ declare const addTransaction: (client: MoneyLoverClient, transaction: MoneyLoverTransaction) => Promise<MoneyLoverTransaction>;
5
5
  export { getTransactions, addTransaction };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "money-lover-app-client",
3
- "version": "1.0.5",
3
+ "version": "1.0.6",
4
4
  "main": "dist/bundle.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "scripts": {
package/dist/client.js DELETED
@@ -1,95 +0,0 @@
1
- import jwt from "jsonwebtoken";
2
- import { formatDate } from "./utils";
3
- const moneyLoverUrl = "https://web.moneylover.me";
4
- const moneyLoverTokenUrl = "https://oauth.moneylover.me";
5
- class MoneyLoverClient {
6
- constructor(jwtToken = null) {
7
- this._jwtToken = null;
8
- this._jwtToken = jwtToken;
9
- }
10
- async _postRequest(path, body = null, headers = {}) {
11
- const res = await fetch(`${moneyLoverUrl}/api${path}`, {
12
- method: "POST",
13
- headers: {
14
- authorization: `AuthJWT ${this._jwtToken}`,
15
- "cache-Control": "no-cache, max-age=0, no-store, no-transform, must-revalidate",
16
- "Content-Type": "application/json",
17
- ...headers,
18
- },
19
- body: JSON.stringify(body),
20
- });
21
- const data = await res.json();
22
- if (data.error != null && data.error !== 0) {
23
- const error = new Error(`Error ${data.error}, ${data.msg}`);
24
- error.name = "MoneyLoverError";
25
- error.message = data.msg;
26
- throw error;
27
- }
28
- else if (data.e != null) {
29
- const error = new Error(`Error ${data.e}, ${data.message}`);
30
- error.name = "MoneyLoverError";
31
- error.message = data.message;
32
- throw error;
33
- }
34
- else {
35
- return data.data;
36
- }
37
- }
38
- static async getToken(email, password) {
39
- const loginUrlRes = await fetch(`${moneyLoverUrl}/api/user/login-url`, {
40
- method: "POST",
41
- });
42
- const loginUrlData = await loginUrlRes.json();
43
- const res = await fetch(`${moneyLoverTokenUrl}/token`, {
44
- method: "POST",
45
- headers: {
46
- authorization: `Bearer ${loginUrlData.data.request_token}`,
47
- client: loginUrlData.data.login_url.match("client=(.+?)&")[1],
48
- "Content-Type": "application/json",
49
- },
50
- body: JSON.stringify({ email, password }),
51
- });
52
- const tokenData = await res.json();
53
- return tokenData.access_token;
54
- }
55
- isTokenValid() {
56
- if (this._jwtToken != null) {
57
- // Check if token is expired
58
- const jwtToken = jwt.decode(this._jwtToken);
59
- if (!jwtToken.exp) {
60
- return false;
61
- }
62
- return jwtToken.exp * 1000 > Date.now();
63
- }
64
- return false;
65
- }
66
- getUserInfo() {
67
- return this._postRequest("/user/info");
68
- }
69
- getWallets() {
70
- return this._postRequest("/wallet/list");
71
- }
72
- getCategories(walletId) {
73
- return this._postRequest("/category/list-all", { walletId: walletId });
74
- }
75
- getTransactions(walletId, startDate, endDate) {
76
- return this._postRequest("/transaction/list", {
77
- startDate: startDate.toISOString().substr(0, 10),
78
- endDate: endDate.toISOString().substr(0, 10),
79
- walletId, // "all" to get the transactions of all wallets
80
- });
81
- }
82
- addTransaction(transaction) {
83
- return this._postRequest("/transaction/add", JSON.stringify({
84
- with: [],
85
- account: transaction.account,
86
- category: transaction.category,
87
- amount: transaction.amount,
88
- note: transaction.note,
89
- displayDate: formatDate(transaction.date),
90
- }), {
91
- "Content-Type": "application/json",
92
- });
93
- }
94
- }
95
- export default MoneyLoverClient;
@@ -1,13 +0,0 @@
1
- const getCategories = async (client, walletId) => {
2
- if (!client) {
3
- throw new Error("Not logged in");
4
- }
5
- if (!client.isTokenValid()) {
6
- throw new Error("Token has expired");
7
- }
8
- const allCategories = await client.getCategories(walletId);
9
- // Api may return all categories even when the walletId is specified,
10
- // so we filter them here to return only the categories that belong to the specified walletId.
11
- return allCategories.filter((category) => category.walletId === walletId);
12
- };
13
- export default getCategories;
@@ -1,24 +0,0 @@
1
- import MoneyLoverClient from "../client";
2
- import jwt from "jsonwebtoken";
3
- const debug = process.env.DEBUG === "true";
4
- const login = async (username, password) => {
5
- let token;
6
- token = await MoneyLoverClient.getToken(username, password);
7
- try {
8
- const jwtToken = jwt.decode(token);
9
- const ml = new MoneyLoverClient(token);
10
- const userInfo = await ml.getUserInfo();
11
- if (debug) {
12
- if (!jwtToken.exp) {
13
- throw new Error("Invalid JWT token: missing exp field");
14
- }
15
- console.log(`MoneyLoverClient: Logged in as ${userInfo.email} until ${new Date(jwtToken.exp * 1000)}`);
16
- }
17
- return userInfo;
18
- }
19
- catch (e) {
20
- console.error("MoneyLoverClient: Login failed", e);
21
- return null;
22
- }
23
- };
24
- export default login;
@@ -1,6 +0,0 @@
1
- const logout = async (deviceId = null) => {
2
- if (deviceId != null) {
3
- // Delete device from Money Lover servers
4
- }
5
- };
6
- export default logout;
@@ -1,26 +0,0 @@
1
- const getTransactions = async (client, walletId, startDate, endDate) => {
2
- if (!client) {
3
- throw new Error("MoneyLoverClient: Not logged in");
4
- }
5
- if (!client.isTokenValid()) {
6
- throw new Error("MoneyLoverClient: Token has expired");
7
- }
8
- if (!walletId) {
9
- // Default to all
10
- walletId = "all";
11
- }
12
- return await client.getTransactions(walletId, startDate, endDate);
13
- };
14
- const addTransaction = async (client, transaction) => {
15
- if (!client) {
16
- throw new Error("MoneyLoverClient: Not logged in");
17
- }
18
- if (!client.isTokenValid()) {
19
- throw new Error("MoneyLoverClient: Token has expired");
20
- }
21
- if (!transaction.account || !transaction.category) {
22
- throw new Error("MoneyLoverClient: Transaction must have account and category");
23
- }
24
- return await client.addTransaction(transaction);
25
- };
26
- export { getTransactions, addTransaction };
@@ -1,15 +0,0 @@
1
- const debug = process.env.DEBUG === "true";
2
- const getWallets = async (client) => {
3
- if (!client) {
4
- throw new Error("MoneyLoverClient: Not logged in");
5
- }
6
- if (!client.isTokenValid()) {
7
- throw new Error("MoneyLoverClient: Token has expired");
8
- }
9
- const wallets = await client.getWallets();
10
- if (debug) {
11
- console.log(`MoneyLoverClient: Retrieved ${wallets.length} wallets`);
12
- }
13
- return wallets;
14
- };
15
- export default getWallets;
package/dist/index.js DELETED
@@ -1 +0,0 @@
1
- "use strict";
@@ -1,6 +0,0 @@
1
- "use strict";
2
- var CategoryType;
3
- (function (CategoryType) {
4
- CategoryType[CategoryType["INCOME"] = 1] = "INCOME";
5
- CategoryType[CategoryType["EXPENSE"] = 2] = "EXPENSE";
6
- })(CategoryType || (CategoryType = {}));
@@ -1 +0,0 @@
1
- export {};
@@ -1,3 +0,0 @@
1
- export interface ClientState {
2
- jwtToken: string | null;
3
- }
@@ -1 +0,0 @@
1
- export {};
@@ -1 +0,0 @@
1
- export {};
@@ -1 +0,0 @@
1
- export {};
@@ -1 +0,0 @@
1
- export {};
package/dist/state.d.ts DELETED
@@ -1,3 +0,0 @@
1
- export declare function getState(key: string): any;
2
- export declare function setState(key: string, value: any): void;
3
- export declare function clearState(): void;
package/dist/state.js DELETED
@@ -1,19 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getState = getState;
4
- exports.setState = setState;
5
- exports.clearState = clearState;
6
- let state = {
7
- jwtToken: null,
8
- };
9
- function getState(key) {
10
- return state[key];
11
- }
12
- function setState(key, value) {
13
- state[key] = value;
14
- }
15
- function clearState() {
16
- state = {
17
- jwtToken: null,
18
- };
19
- }
package/dist/utils.js DELETED
@@ -1,12 +0,0 @@
1
- function formatDate(date) {
2
- const d = new Date(date);
3
- let month = "" + (d.getMonth() + 1);
4
- let day = "" + d.getDate();
5
- const year = d.getFullYear();
6
- if (month.length < 2)
7
- month = "0" + month;
8
- if (day.length < 2)
9
- day = "0" + day;
10
- return [year, month, day].join("-");
11
- }
12
- export { formatDate };