remult 3.3.16 → 3.3.17-next.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.
@@ -1,8 +1,9 @@
1
1
  import type { GenericResponse } from './server/index.js';
2
+ import type { SubscriptionServerRouteApi, SubscriptionServerWithRoutes } from './server/remult-api-server.js';
2
3
  import type { Remult } from './src/context.js';
3
- import type { SubscriptionServer } from './src/live-query/SubscriptionServer.js';
4
- export declare class SseSubscriptionServer implements SubscriptionServer {
4
+ export declare class SseSubscriptionServer implements SubscriptionServerWithRoutes {
5
5
  private canUserConnectToChannel?;
6
+ initApiServer(api: SubscriptionServerRouteApi): void;
6
7
  constructor(canUserConnectToChannel?: ((channel: string, remult: Remult) => boolean) | undefined);
7
8
  publishMessage<T>(channel: string, message: any): Promise<void>;
8
9
  }
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.clientConnection = exports.SseSubscriptionServer = void 0;
4
4
  var tslib_1 = require("tslib");
5
5
  var SseSubscriptionClient_js_1 = require("./src/live-query/SseSubscriptionClient.js");
6
+ var SubscriptionChannel_js_1 = require("./src/live-query/SubscriptionChannel.js");
6
7
  var SseSubscriptionServer = /** @class */ (function () {
7
8
  function SseSubscriptionServer(canUserConnectToChannel) {
8
9
  this.canUserConnectToChannel = canUserConnectToChannel;
@@ -16,6 +17,45 @@ var SseSubscriptionServer = /** @class */ (function () {
16
17
  this.canUserConnectToChannel = function () { return true; };
17
18
  }
18
19
  }
20
+ SseSubscriptionServer.prototype.initApiServer = function (api) {
21
+ var _this = this;
22
+ var streamPath = '/' + SubscriptionChannel_js_1.streamUrl;
23
+ api.addRoute(streamPath, 'get', function (_a) { return tslib_1.__awaiter(_this, [_a], void 0, function (_b) {
24
+ var req = _b.req, origRes = _b.origRes;
25
+ return tslib_1.__generator(this, function (_c) {
26
+ this.openHttpServerStream(req, origRes);
27
+ return [2 /*return*/];
28
+ });
29
+ }); });
30
+ api.addRoute(streamPath + '/subscribe', 'post', function (_a) { return tslib_1.__awaiter(_this, [_a], void 0, function (_b) {
31
+ var _c;
32
+ var remult = _b.remult, res = _b.res, getBody = _b.getBody;
33
+ return tslib_1.__generator(this, function (_d) {
34
+ switch (_d.label) {
35
+ case 0:
36
+ _c = this.subscribeToChannel;
37
+ return [4 /*yield*/, getBody()];
38
+ case 1:
39
+ _c.apply(this, [_d.sent(), res, remult]);
40
+ return [2 /*return*/];
41
+ }
42
+ });
43
+ }); });
44
+ api.addRoute(streamPath + '/unsubscribe', 'post', function (_a) { return tslib_1.__awaiter(_this, [_a], void 0, function (_b) {
45
+ var _c;
46
+ var remult = _b.remult, res = _b.res, getBody = _b.getBody;
47
+ return tslib_1.__generator(this, function (_d) {
48
+ switch (_d.label) {
49
+ case 0:
50
+ _c = this.subscribeToChannel;
51
+ return [4 /*yield*/, getBody()];
52
+ case 1:
53
+ _c.apply(this, [_d.sent(), res, remult, true]);
54
+ return [2 /*return*/];
55
+ }
56
+ });
57
+ }); });
58
+ };
19
59
  //@internal
20
60
  SseSubscriptionServer.prototype.subscribeToChannel = function (_a, res, remult, remove) {
21
61
  var e_1, _b;
@@ -1,6 +1,19 @@
1
1
  import { ConnectionNotFoundError } from './src/live-query/SseSubscriptionClient.js';
2
+ import { streamUrl } from './src/live-query/SubscriptionChannel.js';
2
3
  export class SseSubscriptionServer {
3
4
  canUserConnectToChannel;
5
+ initApiServer(api) {
6
+ const streamPath = '/' + streamUrl;
7
+ api.addRoute(streamPath, 'get', async ({ req, origRes }) => {
8
+ this.openHttpServerStream(req, origRes);
9
+ });
10
+ api.addRoute(streamPath + '/subscribe', 'post', async ({ remult, res, getBody }) => {
11
+ this.subscribeToChannel(await getBody(), res, remult);
12
+ });
13
+ api.addRoute(streamPath + '/unsubscribe', 'post', async ({ remult, res, getBody }) => {
14
+ this.subscribeToChannel(await getBody(), res, remult, true);
15
+ });
16
+ }
4
17
  //@internal
5
18
  subscribeToChannel({ channel, clientId }, res, remult, remove = false) {
6
19
  for (const c of this.connections) {
@@ -2,7 +2,7 @@ import { __decorate, __metadata } from "tslib";
2
2
  import { SseSubscriptionServer } from '../SseSubscriptionServer.js';
3
3
  import { Remult, RemultAsyncLocalStorage, withRemult } from '../src/context.js';
4
4
  import { DataApi, serializeError } from '../src/data-api.js';
5
- import { liveQueryKeepAliveRoute, streamUrl, } from '../src/live-query/SubscriptionChannel.js';
5
+ import { liveQueryKeepAliveRoute } from '../src/live-query/SubscriptionChannel.js';
6
6
  import { InMemoryLiveQueryStorage, LiveQueryPublisher, } from '../src/live-query/SubscriptionServer.js';
7
7
  import { IdEntity } from '../src/remult3/IdEntity.js';
8
8
  import { Fields } from '../src/remult3/Fields.js';
@@ -266,19 +266,20 @@ export class RemultServerImplementation {
266
266
  r.route(this.options.rootPath + '/admin').get(admin());
267
267
  }
268
268
  r.route(this.options.rootPath + '/me').get(this.process(async (remult, req, res) => res.success(remult.user ?? null)));
269
- if (this.options.subscriptionServer instanceof SseSubscriptionServer) {
270
- const streamPath = this.options.rootPath + '/' + streamUrl;
271
- r.route(streamPath).get(this.process(async (remult, req, res, origReq, origRes) => {
272
- ;
273
- remult.subscriptionServer.openHttpServerStream(origReq, origRes);
274
- }));
275
- r.route(streamPath + '/subscribe').post(this.process(async (remult, _2, res, _, _1, origReq) => {
276
- const body = remult.subscriptionServer.subscribeToChannel(await this.coreOptions.getRequestBody(origReq), res, remult);
277
- }));
278
- r.route(streamPath + '/unsubscribe').post(this.process(async (remult, req, res, reqInfo, origRes, origReq) => {
279
- ;
280
- remult.subscriptionServer.subscribeToChannel(await this.coreOptions.getRequestBody(origReq), res, remult, true);
281
- }));
269
+ const subscriptionServer = this.options.subscriptionServer;
270
+ if (isOfType(subscriptionServer, 'initApiServer')) {
271
+ subscriptionServer.initApiServer({
272
+ rootPath: this.options.rootPath,
273
+ addRoute: (relativePath, method, handler) => {
274
+ r.route(this.options.rootPath + relativePath)[method](this.process(async (remult, _, res, req, origRes, origReq) => handler({
275
+ remult,
276
+ res,
277
+ req,
278
+ origRes,
279
+ getBody: () => this.coreOptions.getRequestBody(origReq),
280
+ })));
281
+ },
282
+ });
282
283
  }
283
284
  r.route(this.options.rootPath + '/' + liveQueryKeepAliveRoute).post(this.process(async (remult, req, res, reqInfo, origRes, origReq) => {
284
285
  res.success(await remult.liveQueryStorage.keepAliveAndReturnUnknownQueryIds(await this.coreOptions.getRequestBody(origReq)));
package/internals.d.ts CHANGED
@@ -14,6 +14,8 @@ export declare const actionInfo: {
14
14
  };
15
15
  export { isOfType } from './src/isOfType.js';
16
16
  export type { ClassType } from './classType.js';
17
+ export type { SubscriptionServerWithRoutes, SubscriptionServerRouteApi, SubscriptionServerRouteHandlerArgs, } from './server/remult-api-server.js';
18
+ export type { DataApiResponse } from './src/data-api.js';
17
19
  export { flags } from './src/remult3/remult3.js';
18
20
  export { DataProviderPromiseWrapper } from './src/data-interfaces.js';
19
21
  export { pagedQueryResult } from './src/remult3/pagedQueryResult.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "remult",
3
- "version": "3.3.16",
3
+ "version": "3.3.17-next.0",
4
4
  "description": "A CRUD framework for full-stack TypeScript",
5
5
  "homepage": "https://remult.dev",
6
6
  "repository": {
@@ -1,6 +1,7 @@
1
1
  import type { ClassType } from '../classType.js';
2
2
  import type { Allowed, UserInfo } from '../src/context.js';
3
3
  import { Remult } from '../src/context.js';
4
+ import type { DataApiResponse } from '../src/data-api.js';
4
5
  import type { DataProvider } from '../src/data-interfaces.js';
5
6
  import type { LiveQueryStorage, SubscriptionServer } from '../src/live-query/SubscriptionServer.js';
6
7
  import { IdEntity } from '../src/remult3/IdEntity.js';
@@ -167,6 +168,22 @@ export interface GenericResponse {
167
168
  status(statusCode: number): GenericResponse;
168
169
  end(): void;
169
170
  }
171
+ /** A `SubscriptionServer` that also needs to serve http routes (for example SSE),
172
+ * can implement this interface to register them on the api server */
173
+ export interface SubscriptionServerWithRoutes extends SubscriptionServer {
174
+ initApiServer(api: SubscriptionServerRouteApi): void;
175
+ }
176
+ export interface SubscriptionServerRouteApi {
177
+ rootPath: string;
178
+ addRoute(relativePath: string, method: 'get' | 'post', handler: (args: SubscriptionServerRouteHandlerArgs) => Promise<void>): void;
179
+ }
180
+ export interface SubscriptionServerRouteHandlerArgs {
181
+ remult: Remult;
182
+ res: DataApiResponse;
183
+ req: GenericRequestInfo;
184
+ origRes: GenericResponse;
185
+ getBody(): Promise<any>;
186
+ }
170
187
  export interface queuedJobInfo {
171
188
  info: queuedJobInfoResponse;
172
189
  userId: string;
@@ -441,45 +441,25 @@ var RemultServerImplementation = /** @class */ (function () {
441
441
  r.route(this.options.rootPath + '/me').get(this.process(function (remult, req, res) { return tslib_1.__awaiter(_this, void 0, void 0, function () { var _a; return tslib_1.__generator(this, function (_b) {
442
442
  return [2 /*return*/, res.success((_a = remult.user) !== null && _a !== void 0 ? _a : null)];
443
443
  }); }); }));
444
- if (this.options.subscriptionServer instanceof SseSubscriptionServer_js_1.SseSubscriptionServer) {
445
- var streamPath = this.options.rootPath + '/' + SubscriptionChannel_js_1.streamUrl;
446
- r.route(streamPath).get(this.process(function (remult, req, res, origReq, origRes) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
447
- return tslib_1.__generator(this, function (_a) {
448
- ;
449
- remult.subscriptionServer.openHttpServerStream(origReq, origRes);
450
- return [2 /*return*/];
451
- });
452
- }); }));
453
- r.route(streamPath + '/subscribe').post(this.process(function (remult, _2, res, _, _1, origReq) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
454
- var body, _a, _b;
455
- return tslib_1.__generator(this, function (_c) {
456
- switch (_c.label) {
457
- case 0:
458
- _b = (_a = remult.subscriptionServer).subscribeToChannel;
459
- return [4 /*yield*/, this.coreOptions.getRequestBody(origReq)];
460
- case 1:
461
- body = _b.apply(_a, [_c.sent(), res,
462
- remult]);
463
- return [2 /*return*/];
464
- }
465
- });
466
- }); }));
467
- r.route(streamPath + '/unsubscribe').post(this.process(function (remult, req, res, reqInfo, origRes, origReq) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
468
- var _a, _b;
469
- return tslib_1.__generator(this, function (_c) {
470
- switch (_c.label) {
471
- case 0:
472
- ;
473
- _b = (_a = remult.subscriptionServer).subscribeToChannel;
474
- return [4 /*yield*/, this.coreOptions.getRequestBody(origReq)];
475
- case 1:
476
- _b.apply(_a, [_c.sent(), res,
477
- remult,
478
- true]);
479
- return [2 /*return*/];
480
- }
481
- });
482
- }); }));
444
+ var subscriptionServer = this.options.subscriptionServer;
445
+ if ((0, isOfType_js_1.isOfType)(subscriptionServer, 'initApiServer')) {
446
+ subscriptionServer.initApiServer({
447
+ rootPath: this.options.rootPath,
448
+ addRoute: function (relativePath, method, handler) {
449
+ r.route(_this.options.rootPath + relativePath)[method](_this.process(function (remult, _, res, req, origRes, origReq) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
450
+ var _this = this;
451
+ return tslib_1.__generator(this, function (_a) {
452
+ return [2 /*return*/, handler({
453
+ remult: remult,
454
+ res: res,
455
+ req: req,
456
+ origRes: origRes,
457
+ getBody: function () { return _this.coreOptions.getRequestBody(origReq); },
458
+ })];
459
+ });
460
+ }); }));
461
+ },
462
+ });
483
463
  }
484
464
  r.route(this.options.rootPath + '/' + SubscriptionChannel_js_1.liveQueryKeepAliveRoute).post(this.process(function (remult, req, res, reqInfo, origRes, origReq) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
485
465
  var _a, _b, _c, _d;