@ztimson/momentum 0.27.4 → 0.28.2

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/index.js DELETED
@@ -1,32 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./actions"), exports);
18
- __exportStar(require("./api"), exports);
19
- __exportStar(require("./auth"), exports);
20
- __exportStar(require("./core"), exports);
21
- __exportStar(require("./data"), exports);
22
- __exportStar(require("./email"), exports);
23
- __exportStar(require("./groups"), exports);
24
- __exportStar(require("./logger"), exports);
25
- __exportStar(require("./momentum"), exports);
26
- __exportStar(require("./payments"), exports);
27
- __exportStar(require("./pdf"), exports);
28
- __exportStar(require("./settings"), exports);
29
- __exportStar(require("./sockets"), exports);
30
- __exportStar(require("./static"), exports);
31
- __exportStar(require("./storage"), exports);
32
- __exportStar(require("./users"), exports);
package/dist/logger.js DELETED
@@ -1,59 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Logger = void 0;
4
- const api_1 = require("./api");
5
- const utils_1 = require("@ztimson/utils");
6
- class Logger {
7
- api;
8
- constructor(api, logLevel) {
9
- this.api = typeof api == 'string' ? new api_1.Api(api) : api;
10
- if (logLevel != null && logLevel != 'NONE') {
11
- // LOG_LEVEL[logLevel];
12
- window.addEventListener('error', (event) => this.error(event.error.stack));
13
- window.addEventListener('unhandledrejection', async (event) => this.error(event.reason.stack));
14
- }
15
- }
16
- buildLog(level, log) {
17
- return {
18
- time: new Date().getTime(),
19
- level,
20
- log,
21
- ctx: {
22
- cores: navigator.hardwareConcurrency,
23
- mem: navigator?.deviceMemory,
24
- res: [window.innerWidth, window.innerHeight],
25
- url: location.href,
26
- }
27
- };
28
- }
29
- clearClientLogs() {
30
- return this.api.request({ url: `/api/logs/client`, method: 'DELETE' });
31
- }
32
- clearServerLogs() {
33
- return this.api.request({ url: `/api/logs/server`, method: 'DELETE' });
34
- }
35
- clientLogs(length, page) {
36
- const query = [length ? `length=${length}` : undefined, page ? `page=${page}` : undefined,].filter(v => !!v).join('&');
37
- return this.api.request({ url: `/api/logs/client${query ? `?${query}` : ''}` }).then(resp => resp.data);
38
- }
39
- serverLogs(length, page) {
40
- const query = [length ? `length=${length}` : undefined, page ? `page=${page}` : undefined,].filter(v => !!v).join('&');
41
- return this.api.request({ url: `/api/logs/server${query ? `?${query}` : ''}` }).then(resp => resp.data);
42
- }
43
- debug(...logs) {
44
- return this.api.request({ url: `/api/logs/client`, body: this.buildLog(utils_1.LOG_LEVEL.DEBUG, logs) }).then(() => { }).catch(() => { });
45
- }
46
- log(...logs) {
47
- return this.api.request({ url: `/api/logs/client`, body: this.buildLog(utils_1.LOG_LEVEL.LOG, logs) }).then(() => { }).catch(() => { });
48
- }
49
- info(...logs) {
50
- return this.api.request({ url: `/api/logs/client`, body: this.buildLog(utils_1.LOG_LEVEL.INFO, logs) }).then(() => { }).catch(() => { });
51
- }
52
- warn(...logs) {
53
- return this.api.request({ url: `/api/logs/client`, body: this.buildLog(utils_1.LOG_LEVEL.WARN, logs) }).then(() => { }).catch(() => { });
54
- }
55
- error(...logs) {
56
- return this.api.request({ url: `/api/logs/client`, body: this.buildLog(utils_1.LOG_LEVEL.ERROR, logs) }).then(() => { }).catch(() => { });
57
- }
58
- }
59
- exports.Logger = Logger;
package/dist/momentum.js DELETED
@@ -1,80 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Momentum = void 0;
4
- const actions_1 = require("./actions");
5
- const api_1 = require("./api");
6
- const auth_1 = require("./auth");
7
- const data_1 = require("./data");
8
- const email_1 = require("./email");
9
- const groups_1 = require("./groups");
10
- const utils_1 = require("@ztimson/utils");
11
- const logger_1 = require("./logger");
12
- const payments_1 = require("./payments");
13
- const pdf_1 = require("./pdf");
14
- const sockets_1 = require("./sockets");
15
- const storage_1 = require("./storage");
16
- const users_1 = require("./users");
17
- const settings_1 = require("./settings");
18
- const static_1 = require("./static");
19
- class Momentum extends utils_1.TypedEmitter {
20
- api;
21
- actions;
22
- auth;
23
- data;
24
- email;
25
- groups;
26
- logger;
27
- payments;
28
- pdf;
29
- settings;
30
- socket;
31
- static;
32
- storage;
33
- users;
34
- constructor(url, opts) {
35
- super();
36
- this.api = new api_1.Api(url, opts?.api);
37
- this.actions = new actions_1.Actions(this.api);
38
- this.auth = new auth_1.Auth(this.api, {
39
- persist: opts?.persist ?? true,
40
- loginUi: opts?.loginUi
41
- });
42
- this.data = new data_1.Data(this.api);
43
- this.email = new email_1.Email(this.api);
44
- this.groups = new groups_1.Groups(this.api);
45
- this.logger = new logger_1.Logger(this.api, opts?.logLevel);
46
- if (opts?.stripeSecret)
47
- this.payments = new payments_1.Payments(this.api, opts.stripeSecret);
48
- this.pdf = new pdf_1.Pdf(this.api);
49
- this.settings = new settings_1.Settings(this.api);
50
- this.static = new static_1.Static(this.api);
51
- this.storage = new storage_1.Storage(this.api);
52
- this.users = new users_1.Users(this.api);
53
- if (opts?.socket)
54
- this.socket = new sockets_1.Socket(this.api);
55
- this.api.on('*', (event, ...args) => this.emit(`API::${event}`, ...args));
56
- this.actions.on('*', (event, ...args) => this.emit(`ACTIONS::${event}`, ...args));
57
- this.auth.on('*', (event, ...args) => this.emit(`AUTH::${event}`, ...args));
58
- this.data.on('*', (event, ...args) => this.emit(`DATA::${event}`, ...args));
59
- this.email.on('*', (event, ...args) => this.emit(`EMAIL::${event}`, ...args));
60
- this.groups.on('*', (event, ...args) => this.emit(`GROUPS::${event}`, ...args));
61
- if (this.payments)
62
- this.payments.on('*', (event, ...args) => this.emit(`PAYMENT::${event}`, ...args));
63
- this.pdf.on('*', (event, ...args) => this.emit(`PDF::${event}`, ...args));
64
- this.settings.on('*', (event, ...args) => this.emit(`SETTINGS::${event}`, ...args));
65
- this.static.on('*', (event, ...args) => this.emit(`STATIC::${event}`, ...args));
66
- this.storage.on('*', (event, ...args) => this.emit(`STORAGE::${event}`, ...args));
67
- this.users.on('*', (event, ...args) => this.emit(`USERS::${event}`, ...args));
68
- this.users.on('*', (event, ...args) => {
69
- if (Array.isArray(args[0])) {
70
- const u = args[0].find(u => u._id == this.auth.user?._id);
71
- if (u)
72
- this.auth.user = u;
73
- }
74
- else if (args[0]._id == this.auth.user?._id) {
75
- this.auth.user = args[0];
76
- }
77
- });
78
- }
79
- }
80
- exports.Momentum = Momentum;
package/dist/payments.js DELETED
@@ -1,51 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Payments = void 0;
4
- const utils_1 = require("@ztimson/utils");
5
- const api_1 = require("./api");
6
- class Payments extends utils_1.TypedEmitter {
7
- api;
8
- stripe;
9
- constructor(api, secret) {
10
- super();
11
- this.api = typeof api == 'string' ? new api_1.Api(api) : api;
12
- const setup = (retry = 1) => {
13
- try {
14
- if (!Stripe)
15
- throw new Error('Stripe not added');
16
- this.stripe = Stripe(secret);
17
- }
18
- catch (err) {
19
- if (retry > 0)
20
- setTimeout(() => setup(retry--), 250);
21
- else
22
- console.warn('Stripe failed, did you add the library & setup your API key?', err);
23
- }
24
- };
25
- setup();
26
- }
27
- async charge(amount, custom = {}) {
28
- this.emit('CHECKOUT', amount, custom);
29
- const request = await this.api.request({ url: '/api/payments', body: {
30
- amount,
31
- custom,
32
- } });
33
- return request.data.clientSecret;
34
- }
35
- async createForm(element, amount, custom) {
36
- const token = await this.charge(amount, custom);
37
- const form = this.stripe.elements({ clientSecret: token });
38
- form.create('payment').mount(element);
39
- return () => this.stripe.confirmPayment({
40
- elements: form,
41
- redirect: "if_required",
42
- confirmParams: { return_url: location.origin }
43
- });
44
- }
45
- async history(username) {
46
- const history = await this.api.request({ url: `/api/payments${username ? `/${username}` : ''}` });
47
- this.emit('LIST', username || null, history.data);
48
- return history.data;
49
- }
50
- }
51
- exports.Payments = Payments;
package/dist/pdf.js DELETED
@@ -1,34 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Pdf = void 0;
4
- const api_1 = require("./api");
5
- const utils_1 = require("@ztimson/utils");
6
- class Pdf extends utils_1.TypedEmitter {
7
- api;
8
- constructor(api) {
9
- super();
10
- this.api = typeof api == 'string' ? new api_1.Api(api) : api;
11
- }
12
- async handleResponse(resp, fileName) {
13
- const blob = await resp.blob();
14
- if (fileName) {
15
- const url = URL.createObjectURL(blob);
16
- (0, utils_1.download)(url, fileName.endsWith('.pdf') ? fileName : fileName + '.pdf');
17
- URL.revokeObjectURL(url);
18
- }
19
- this.emit('CREATE', blob);
20
- return blob;
21
- }
22
- fromHtml(content, opts = {}) {
23
- return this.api.request({ url: `/api/pdf`, body: { html: content } })
24
- .then((resp) => this.handleResponse(resp, opts.download ? (opts.fileName || new Date().toISOString()) : undefined));
25
- }
26
- fromTemplate(template, data, opts = {}) {
27
- return this.api.request({ url: `/api/pdf${template}`, body: data })
28
- .then((resp) => this.handleResponse(resp, opts.download ? (opts.fileName || new Date().toISOString()) : undefined));
29
- }
30
- fromUrl(url, opts = {}) {
31
- return this.api.request({ url: `/api/pdf?url=${url}` }).then((resp) => this.handleResponse(resp, opts.download ? (opts.fileName || new Date().toISOString()) : undefined));
32
- }
33
- }
34
- exports.Pdf = Pdf;
package/dist/settings.js DELETED
@@ -1,48 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Settings = void 0;
4
- const api_1 = require("./api");
5
- const utils_1 = require("@ztimson/utils");
6
- const rxjs_1 = require("rxjs");
7
- class Settings extends utils_1.TypedEmitter {
8
- api;
9
- $cache = new rxjs_1.BehaviorSubject({});
10
- get cache() { return this.$cache.value; }
11
- set cache(val) { this.$cache.next(val); }
12
- constructor(api) {
13
- super();
14
- this.api = typeof api == 'string' ? new api_1.Api(api) : api;
15
- }
16
- list(detailed = false) {
17
- return this.api.request({ url: `/api/settings` + (detailed ? '?detailed' : '') }).then(resp => {
18
- this.cache = !detailed ? resp.data : Object.values(resp.data || {}).reduce((acc, v) => ({ ...acc, [v.key]: v.value }), {});
19
- this.emit('LIST', resp.data || []);
20
- return resp.data;
21
- });
22
- }
23
- delete(key) {
24
- return this.api.request({ url: `/api/settings/${key}`, method: 'DELETE' }).then(() => {
25
- this.cache = { ...this.cache, [key]: undefined };
26
- this.emit('DELETE', key);
27
- });
28
- }
29
- read(key, reload = false) {
30
- if (!reload && this.cache[key])
31
- return Promise.resolve(this.cache[key]);
32
- return this.api.request({ url: `/api/settings/${key}` }).then(variable => {
33
- if (variable.data)
34
- this.cache = { ...this.cache, [variable.data.key]: variable };
35
- this.emit('READ', variable.data);
36
- return variable.data;
37
- });
38
- }
39
- update(variable) {
40
- return this.api.request({ url: `/api/settings/${variable.key}`, body: variable }).then(variable => {
41
- if (variable.data)
42
- this.cache = { ...this.cache, [variable.data.key]: variable.data.value };
43
- this.emit('UPDATE', variable.data);
44
- return variable.data;
45
- });
46
- }
47
- }
48
- exports.Settings = Settings;
package/dist/sockets.js DELETED
@@ -1,54 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Socket = void 0;
4
- const api_1 = require("./api");
5
- class Socket {
6
- static timeout = 10000;
7
- api;
8
- url;
9
- connection;
10
- open = false;
11
- constructor(api) {
12
- this.api = typeof api == 'string' ? new api_1.Api(api) : api;
13
- this.url = this.api.url.replace('http', 'ws');
14
- this.api.on('TOKEN', () => this.connect());
15
- if (!this.api.token)
16
- this.connect();
17
- }
18
- close() {
19
- console.debug('Disconnected from Momentum');
20
- this.open = false;
21
- this.connection.close();
22
- }
23
- connect(retry = 3) {
24
- if (this.connection?.readyState < 2)
25
- this.connection.close();
26
- this.connection = new WebSocket(this.url + (this.api.token ? `?token=${this.api.token}` : ''));
27
- const timeout = setTimeout(() => {
28
- if (this.open)
29
- return;
30
- this.connection.close();
31
- console.error(`Momentum connection timeout`);
32
- if (retry > 0)
33
- this.connect(retry - 1);
34
- }, Socket.timeout);
35
- this.connection.onclose = () => this.open = false;
36
- this.connection.onmessage = this.handle;
37
- this.connection.onopen = () => {
38
- this.open = true;
39
- clearTimeout(timeout);
40
- console.debug('Connected to Momentum');
41
- };
42
- }
43
- handle(...args) {
44
- console.log(args);
45
- }
46
- send(channel, payload) {
47
- this.connection.send(JSON.stringify({
48
- token: this.api.token,
49
- channel,
50
- payload
51
- }));
52
- }
53
- }
54
- exports.Socket = Socket;
package/dist/static.js DELETED
@@ -1,33 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Static = void 0;
4
- const api_1 = require("./api");
5
- const utils_1 = require("@ztimson/utils");
6
- class Static extends utils_1.TypedEmitter {
7
- api;
8
- constructor(api) {
9
- super();
10
- this.api = typeof api == 'string' ? new api_1.Api(api) : api;
11
- }
12
- delete(path) {
13
- return this.api.request({ url: `/api/static/${path}`, method: 'DELETE' }).then(() => {
14
- this.emit('DELETE', path);
15
- });
16
- }
17
- list(path) {
18
- const url = ('/api/static/' + path).replaceAll('//', '/');
19
- return this.api.request({ url }).then(resp => {
20
- this.emit('LIST', path, resp.data || []);
21
- return resp.data;
22
- });
23
- }
24
- upload(files, path = '/') {
25
- const data = new FormData();
26
- (Array.isArray(files) ? files : [files]).forEach(f => data.append('file', f));
27
- return this.api.request({ url: '/api/static' + path, body: data }).then(resp => {
28
- this.emit('UPLOAD', resp.data || []);
29
- return resp.data;
30
- });
31
- }
32
- }
33
- exports.Static = Static;
package/dist/storage.js DELETED
@@ -1,67 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Storage = void 0;
4
- const api_1 = require("./api");
5
- const utils_1 = require("@ztimson/utils");
6
- class Storage extends utils_1.TypedEmitter {
7
- api;
8
- constructor(api) {
9
- super();
10
- this.api = typeof api == 'string' ? new api_1.Api(api) : api;
11
- }
12
- delete(path) {
13
- const url = (path.startsWith('/api/storage/') ? path : '/api/storage/' + path).replaceAll('//', '/');
14
- return this.api.request({ url, method: 'DELETE' }).then(() => {
15
- this.emit('DELETE', url);
16
- });
17
- }
18
- list(path) {
19
- const url = (path.startsWith('/api/storage/') ? path : '/api/storage/' + path).replaceAll('//', '/');
20
- return this.api.request({ url: url + '?list' }).then(resp => {
21
- this.emit('LIST', path, resp.data);
22
- return resp.data;
23
- });
24
- }
25
- open(path, target = '_blank') {
26
- const p = (path.startsWith('/api/storage/') ? path : '/api/storage/' + path).replaceAll(/\/{2,}/g, '/');
27
- const link = `${this.api.url}${p}${this.api.token ? `?token=${this.api.token}` : ''}`;
28
- if (!target)
29
- return link;
30
- this.emit('OPEN', path);
31
- return window.open(link, target);
32
- }
33
- mkdir(path) {
34
- const p = (path.startsWith('/api/storage/') ? path : '/api/storage/' + path).replaceAll(/\/{2,}/g, '/');
35
- return this.api.request({ url: p + '?directory', method: 'POST' }).then(resp => resp.data);
36
- }
37
- download(path, opts = {}) {
38
- const p = ('/api/storage/' + path).replaceAll('//', '/');
39
- return this.api.request({ ...opts, url: p, decode: false }).then(async (response) => {
40
- const blob = await response.data.blob();
41
- const name = opts.downloadAs || new URL(response.url).pathname.split('/').pop();
42
- this.emit('DOWNLOAD', path, blob);
43
- (0, utils_1.downloadBlob)(blob, name);
44
- return response.data;
45
- });
46
- }
47
- upload(files, opts = '') {
48
- return new utils_1.PromiseProgress(async (res, rej, prog) => {
49
- if (!files)
50
- files = await (0, utils_1.fileBrowser)();
51
- if (!files || Array.isArray(files) && !files.length)
52
- return [];
53
- const url = this.api.url + ('/api/storage/' + (typeof opts == 'string' ? opts : opts?.path)).replaceAll('//', '/');
54
- return (0, utils_1.uploadWithProgress)({
55
- url,
56
- files: Array.isArray(files) ? files : [files],
57
- headers: this.api.headers
58
- }).onProgress(p => {
59
- prog(p);
60
- }).then(resp => {
61
- this.emit('UPLOAD', resp);
62
- res(resp);
63
- }).catch(err => rej(err));
64
- });
65
- }
66
- }
67
- exports.Storage = Storage;
package/dist/users.js DELETED
@@ -1,80 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Users = void 0;
4
- const api_1 = require("./api");
5
- const utils_1 = require("@ztimson/utils");
6
- const rxjs_1 = require("rxjs");
7
- class Users extends utils_1.TypedEmitter {
8
- api;
9
- listed = false;
10
- $cache = new rxjs_1.BehaviorSubject([]);
11
- get cache() { return this.$cache.value; }
12
- set cache(val) { this.$cache.next(val); }
13
- constructor(api) {
14
- super();
15
- this.api = typeof api == 'string' ? new api_1.Api(api) : api;
16
- }
17
- delete(username) {
18
- return this.api.request({
19
- url: `/api/users/${username}`,
20
- method: 'DELETE'
21
- }).then(() => {
22
- this.cache = this.cache.filter(u => u.username != username);
23
- this.emit('DELETE', username);
24
- });
25
- }
26
- list(reload = false) {
27
- if (!reload && this.listed)
28
- return Promise.resolve(this.cache);
29
- return this.api.request({ url: `/api/users` }).then(resp => {
30
- resp.data?.map(r => {
31
- r.image = this.api.url + r.image + `?token=${this.api.token}`;
32
- return r;
33
- });
34
- this.cache = resp.data || [];
35
- this.listed = true;
36
- this.emit('LIST', resp.data || []);
37
- return resp.data;
38
- });
39
- }
40
- read(username, reload = false) {
41
- if (!reload) {
42
- const cached = this.cache.find(u => u.username == username);
43
- if (cached)
44
- return Promise.resolve(cached);
45
- }
46
- return this.api.request({ url: `/api/users/${username}` }).then(resp => {
47
- if (resp.data) {
48
- resp.data.image = this.api.url + resp.data.image + `?token=${this.api.token}`;
49
- this.cache = [...this.cache.filter(u => u.username != username), resp.data];
50
- }
51
- this.emit('READ', resp.data);
52
- return resp.data;
53
- });
54
- }
55
- update(user) {
56
- return this.api.request({
57
- url: `/api/users/${user.username}`,
58
- method: 'PATCH',
59
- body: user
60
- }).then(resp => {
61
- if (resp.data) {
62
- resp.data.image = this.api.url + resp.data.image + `?token=${this.api.token}`;
63
- this.cache = this.cache.filter(u => u.username != user.username).concat([resp.data]);
64
- }
65
- this.emit(user._id ? 'UPDATE' : 'CREATE', resp.data);
66
- return resp.data;
67
- });
68
- }
69
- uploadImage(username, file) {
70
- return (0, utils_1.uploadWithProgress)({
71
- url: this.api.url + `/api/users/${username}/image`,
72
- files: [file],
73
- headers: this.api.headers,
74
- }).then(resp => {
75
- this.emit('UPLOAD_IMAGE', username, file);
76
- return resp;
77
- });
78
- }
79
- }
80
- exports.Users = Users;