@livequery/rest 1.0.22 → 1.0.85

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.
@@ -1,48 +1,29 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
12
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
13
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
14
- return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
15
- };
16
- var _RestTransporter_instances, _RestTransporter_encode_query;
17
- Object.defineProperty(exports, "__esModule", { value: true });
18
- exports.RestTransporter = void 0;
19
- const rxjs_1 = require("rxjs");
20
- const operators_1 = require("rxjs/operators");
21
- const rxjs_2 = require("rxjs");
22
- const Socket_1 = require("./Socket");
23
- const query_string_1 = require("query-string");
24
- class RestTransporter {
1
+ import { of } from 'rxjs';
2
+ import { catchError, map, mergeMap } from 'rxjs/operators';
3
+ import { merge, Subject } from 'rxjs';
4
+ import { Socket } from './Socket';
5
+ import { stringify } from 'query-string';
6
+ export class RestTransporter {
7
+ config;
8
+ socket;
25
9
  constructor(config) {
26
10
  this.config = config;
27
- _RestTransporter_instances.add(this);
28
- config.websocket_url && (this.socket = new Socket_1.Socket(config.websocket_url));
11
+ config.websocket_url && (this.socket = new Socket(config.websocket_url));
29
12
  }
30
13
  query(ref, options) {
31
- var _a;
32
- const $on_reload = new rxjs_2.Subject();
33
- const http_request = (0, rxjs_2.merge)((0, rxjs_1.of)(1), ((_a = this.socket) === null || _a === void 0 ? void 0 : _a.$reconnect) || (0, rxjs_1.of)(), $on_reload)
34
- .pipe((0, operators_1.mergeMap)(() => this.call(ref, 'GET', options)), (0, operators_1.map)(({ data, error }) => {
35
- var _a;
14
+ const $on_reload = new Subject();
15
+ const http_request = merge(of(1), this.socket?.$reconnect || of(), $on_reload)
16
+ .pipe(mergeMap(() => this.call(ref, 'GET', options)), map(({ data, error }) => {
36
17
  if (error)
37
18
  throw error;
38
- data.realtime_token && ((_a = this.socket) === null || _a === void 0 ? void 0 : _a.subscribe(data.realtime_token));
19
+ data.realtime_token && this.socket?.subscribe(data.realtime_token);
39
20
  // If collection
40
21
  const collection = data;
41
22
  if (collection.items) {
42
23
  return {
43
24
  data: {
44
25
  changes: collection.items.map(data => ({ data, type: 'added', ref })),
45
- paging: Object.assign(Object.assign({}, collection.paging), { n: 0 })
26
+ paging: { ...collection.paging, n: 0 }
46
27
  }
47
28
  };
48
29
  }
@@ -54,62 +35,59 @@ class RestTransporter {
54
35
  paging: { n: 0 }
55
36
  }
56
37
  };
57
- }), (0, operators_1.catchError)(error => (0, rxjs_1.of)({ error })));
58
- const websocket_sync = (!this.socket || options._cursor) ? (0, rxjs_1.of)() : (this.socket.listen(ref).pipe((0, operators_1.map)((change) => ({ data: { changes: [change] } }))));
59
- return Object.assign((0, rxjs_2.merge)(http_request, websocket_sync), {
38
+ }), catchError(error => of({ error })));
39
+ const websocket_sync = (!this.socket || options._cursor) ? of() : (this.socket.listen(ref).pipe(map((change) => ({ data: { changes: [change] } }))));
40
+ return Object.assign(merge(http_request, websocket_sync), {
60
41
  reload: () => $on_reload.next()
61
42
  });
62
43
  }
63
- call(url, method, query = {}, payload) {
64
- var _a, _b;
65
- return __awaiter(this, void 0, void 0, function* () {
66
- const headers = Object.assign(Object.assign(Object.assign({}, (yield ((_b = (_a = this.config).headers) === null || _b === void 0 ? void 0 : _b.call(_a))) || {}), payload ? {
44
+ #encode_query(query) {
45
+ if (!query || Object.keys(query || {}).length == 0)
46
+ return '';
47
+ const encoded_query = Object.keys(query).reduce((o, key) => ({
48
+ ...o,
49
+ [key]: typeof query[key] == 'object' ? JSON.stringify(query[key]) : query[key]
50
+ }), {});
51
+ return `?${stringify(encoded_query)}`;
52
+ }
53
+ async call(url, method, query = {}, payload) {
54
+ const headers = {
55
+ ...await this.config.headers?.() || {},
56
+ ...payload ? {
67
57
  'Content-Type': 'application/json'
68
- } : {}), this.socket ? { socket_id: this.socket.socket_session } : {});
58
+ } : {},
59
+ ...this.socket ? { socket_id: this.socket.socket_session } : {}
60
+ };
61
+ try {
62
+ const result = await fetch(`${this.config.base_url()}/${url}${this.#encode_query(query)}`, {
63
+ method,
64
+ headers: headers,
65
+ ...payload ? { body: JSON.stringify(payload) } : {},
66
+ }).then(r => r.text());
69
67
  try {
70
- const result = yield fetch(`${this.config.base_url()}/${url}${__classPrivateFieldGet(this, _RestTransporter_instances, "m", _RestTransporter_encode_query).call(this, query)}`, Object.assign({ method, headers: headers }, payload ? { body: JSON.stringify(payload) } : {})).then(r => r.text());
71
- try {
72
- return JSON.parse(result);
73
- }
74
- catch (e) {
75
- return null;
76
- }
68
+ return JSON.parse(result);
77
69
  }
78
- catch (error) {
79
- throw error;
70
+ catch (e) {
71
+ return null;
80
72
  }
81
- });
73
+ }
74
+ catch (error) {
75
+ throw error;
76
+ }
82
77
  }
83
- get(ref, query = {}) {
84
- return __awaiter(this, void 0, void 0, function* () {
85
- return yield this.call(ref, 'GET', query, null);
86
- });
78
+ async get(ref, query = {}) {
79
+ return await this.call(ref, 'GET', query, null);
87
80
  }
88
- add(ref, data) {
89
- return __awaiter(this, void 0, void 0, function* () {
90
- return yield this.call(ref, 'POST', {}, data);
91
- });
81
+ async add(ref, data) {
82
+ return await this.call(ref, 'POST', {}, data);
92
83
  }
93
- update(ref, data, method = 'PATCH') {
94
- return __awaiter(this, void 0, void 0, function* () {
95
- return yield this.call(ref, method, {}, data);
96
- });
84
+ async update(ref, data, method = 'PATCH') {
85
+ return await this.call(ref, method, {}, data);
97
86
  }
98
- remove(ref) {
99
- return __awaiter(this, void 0, void 0, function* () {
100
- return yield this.call(ref, 'DELETE');
101
- });
87
+ async remove(ref) {
88
+ return await this.call(ref, 'DELETE');
102
89
  }
103
- trigger(ref, name, query, payload) {
104
- return __awaiter(this, void 0, void 0, function* () {
105
- return yield this.call(`${ref}/~${name}`, 'POST', query, payload);
106
- });
90
+ async trigger(ref, name, query, payload) {
91
+ return await this.call(`${ref}/~${name}`, 'POST', query, payload);
107
92
  }
108
93
  }
109
- exports.RestTransporter = RestTransporter;
110
- _RestTransporter_instances = new WeakSet(), _RestTransporter_encode_query = function _RestTransporter_encode_query(query) {
111
- if (!query || Object.keys(query || {}).length == 0)
112
- return '';
113
- const encoded_query = Object.keys(query).reduce((o, key) => (Object.assign(Object.assign({}, o), { [key]: typeof query[key] == 'object' ? JSON.stringify(query[key]) : query[key] })), {});
114
- return `?${(0, query_string_1.stringify)(encoded_query)}`;
115
- };
package/build/Socket.js CHANGED
@@ -1,42 +1,55 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
12
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
13
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
14
- return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
15
- };
16
- var _Socket_instances, _Socket_init;
17
- Object.defineProperty(exports, "__esModule", { value: true });
18
- exports.Socket = void 0;
19
- const rxjs_1 = require("rxjs");
20
- const operators_1 = require("rxjs/operators");
21
- const uuid_1 = require("uuid");
22
- class Socket {
1
+ import { firstValueFrom, fromEvent, Subject, merge, ReplaySubject } from "rxjs";
2
+ import { finalize } from "rxjs/operators";
3
+ import { v4 } from 'uuid';
4
+ export class Socket {
5
+ ws_url_fatory;
6
+ topics = new Map();
7
+ $input = new ReplaySubject(1000);
8
+ $reconnect = new Subject();
9
+ socket_session = v4();
10
+ async #init() {
11
+ if (typeof WebSocket == 'undefined')
12
+ return;
13
+ for (let n = 0; true; n++) {
14
+ console.log('Connecting websocket .... ');
15
+ const ws = new WebSocket(await this.ws_url_fatory());
16
+ const messages_handler = fromEvent(ws, 'message').subscribe((evt) => {
17
+ const { data, event } = JSON.parse(evt.data);
18
+ this[`$${event}`]?.(data);
19
+ });
20
+ const data_sender = fromEvent(ws, 'open').subscribe(() => {
21
+ console.log(n == 0 ? 'Websocket connected' : 'Websocket re-connected');
22
+ ws.send(JSON.stringify({ event: 'start', data: { id: this.socket_session } }));
23
+ const subscription = this.$input.subscribe(data => ws.send(JSON.stringify(data)));
24
+ n > 0 && this.$reconnect.next();
25
+ return () => {
26
+ subscription.unsubscribe();
27
+ this.$input.complete();
28
+ };
29
+ });
30
+ await firstValueFrom(merge(fromEvent(ws, 'error'), fromEvent(ws, 'close'), fromEvent(ws, 'closed')));
31
+ this.$input = new ReplaySubject(1000);
32
+ data_sender.unsubscribe();
33
+ messages_handler.unsubscribe();
34
+ ws.close();
35
+ console.log(`Websocket connection dropped, reconnect in 1s ...`);
36
+ await new Promise(s => setTimeout(s, 1000));
37
+ }
38
+ }
23
39
  constructor(ws_url_fatory) {
24
40
  this.ws_url_fatory = ws_url_fatory;
25
- _Socket_instances.add(this);
26
- this.topics = new Map();
27
- this.$input = new rxjs_1.ReplaySubject(1000);
28
- this.$reconnect = new rxjs_1.Subject();
29
- this.socket_session = (0, uuid_1.v4)();
30
- __classPrivateFieldGet(this, _Socket_instances, "m", _Socket_init).call(this);
41
+ this.#init();
31
42
  }
32
43
  $sync(data) {
33
- var _a, _b;
34
44
  for (const change of data.changes) {
35
45
  // Collection ref broadcast
36
- (_a = this.topics.get(change.ref)) === null || _a === void 0 ? void 0 : _a.stream.next(change);
46
+ this.topics.get(change.ref)?.stream.next(change);
37
47
  // Document ref broadcast
38
48
  const ref = `${change.ref}/${change.data.id}`;
39
- (_b = this.topics.get(ref)) === null || _b === void 0 ? void 0 : _b.stream.next(Object.assign(Object.assign({}, change), { ref }));
49
+ this.topics.get(ref)?.stream.next({
50
+ ...change,
51
+ ref
52
+ });
40
53
  }
41
54
  }
42
55
  subscribe(realtime_token) {
@@ -44,11 +57,11 @@ class Socket {
44
57
  }
45
58
  listen(ref) {
46
59
  if (!this.topics.has(ref)) {
47
- const stream = new rxjs_1.Subject();
60
+ const stream = new Subject();
48
61
  this.topics.set(ref, { stream, listen_count: 0 });
49
62
  }
50
63
  this.topics.get(ref).listen_count++;
51
- return this.topics.get(ref).stream.pipe((0, operators_1.finalize)(() => {
64
+ return this.topics.get(ref).stream.pipe(finalize(() => {
52
65
  this.topics.get(ref).listen_count--;
53
66
  setTimeout(() => {
54
67
  if (this.topics.get(ref).listen_count == 0) {
@@ -58,36 +71,3 @@ class Socket {
58
71
  }));
59
72
  }
60
73
  }
61
- exports.Socket = Socket;
62
- _Socket_instances = new WeakSet(), _Socket_init = function _Socket_init() {
63
- return __awaiter(this, void 0, void 0, function* () {
64
- if (typeof WebSocket == 'undefined')
65
- return;
66
- for (let n = 0; true; n++) {
67
- console.log('Connecting websocket .... ');
68
- const ws = new WebSocket(yield this.ws_url_fatory());
69
- const messages_handler = (0, rxjs_1.fromEvent)(ws, 'message').subscribe((evt) => {
70
- var _a;
71
- const { data, event } = JSON.parse(evt.data);
72
- (_a = this[`$${event}`]) === null || _a === void 0 ? void 0 : _a.call(this, data);
73
- });
74
- const data_sender = (0, rxjs_1.fromEvent)(ws, 'open').subscribe(() => {
75
- console.log(n == 0 ? 'Websocket connected' : 'Websocket re-connected');
76
- ws.send(JSON.stringify({ event: 'start', data: { id: this.socket_session } }));
77
- const subscription = this.$input.subscribe(data => ws.send(JSON.stringify(data)));
78
- n > 0 && this.$reconnect.next();
79
- return () => {
80
- subscription.unsubscribe();
81
- this.$input.complete();
82
- };
83
- });
84
- yield (0, rxjs_1.firstValueFrom)((0, rxjs_1.merge)((0, rxjs_1.fromEvent)(ws, 'error'), (0, rxjs_1.fromEvent)(ws, 'close'), (0, rxjs_1.fromEvent)(ws, 'closed')));
85
- this.$input = new rxjs_1.ReplaySubject(1000);
86
- data_sender.unsubscribe();
87
- messages_handler.unsubscribe();
88
- ws.close();
89
- console.log(`Websocket connection dropped, reconnect in 1s ...`);
90
- yield new Promise(s => setTimeout(s, 1000));
91
- }
92
- });
93
- };
package/build/index.js CHANGED
@@ -1,7 +1,2 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Socket = exports.RestTransporter = void 0;
4
- var RestTransporter_1 = require("./RestTransporter");
5
- Object.defineProperty(exports, "RestTransporter", { enumerable: true, get: function () { return RestTransporter_1.RestTransporter; } });
6
- var Socket_1 = require("./Socket");
7
- Object.defineProperty(exports, "Socket", { enumerable: true, get: function () { return Socket_1.Socket; } });
1
+ export { RestTransporter } from './RestTransporter';
2
+ export { Socket } from './Socket';
package/package.json CHANGED
@@ -3,7 +3,8 @@
3
3
  "repository": {
4
4
  "url": "https://github.com/livequery/rest"
5
5
  },
6
- "version": "1.0.22",
6
+ "version": "1.0.85",
7
+ "type": "module",
7
8
  "description": "",
8
9
  "main": "build/index.js",
9
10
  "types": "build/index.d.ts",
@@ -18,7 +19,7 @@
18
19
  "uuid": "^8.3.2"
19
20
  },
20
21
  "devDependencies": {
21
- "@livequery/types": "1.0.37"
22
+ "@livequery/types": "1.0.82"
22
23
  },
23
24
  "peerDependencies": {},
24
25
  "scripts": {