remult 0.14.30 → 0.14.33

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.
Files changed (46) hide show
  1. package/{readme.md → README.md} +2 -2
  2. package/esm/graphql.d.ts +2 -2
  3. package/esm/remult-express.d.ts +4 -9
  4. package/esm/remult-express.js +3 -2
  5. package/esm/remult-fastify.d.ts +2 -2
  6. package/esm/remult-fastify.js +5 -10
  7. package/esm/remult-fresh.d.ts +6 -7
  8. package/esm/remult-fresh.js +29 -32
  9. package/esm/server/expressBridge.d.ts +10 -60
  10. package/esm/server/expressBridge.js +254 -134
  11. package/esm/server/index.d.ts +1 -3
  12. package/esm/server/index.js +1 -3
  13. package/esm/src/backend-tests/test-http-server.backend-spec.js +1 -2
  14. package/esm/src/context.d.ts +1 -6
  15. package/graphql.d.ts +2 -2
  16. package/package.json +4 -4
  17. package/remult-express.d.ts +4 -9
  18. package/remult-express.js +3 -2
  19. package/remult-fastify.d.ts +2 -2
  20. package/remult-fastify.js +5 -10
  21. package/remult-fresh.d.ts +6 -7
  22. package/remult-fresh.js +10 -10
  23. package/server/expressBridge.d.ts +10 -60
  24. package/server/expressBridge.js +361 -215
  25. package/server/index.d.ts +1 -3
  26. package/server/index.js +2 -6
  27. package/src/backend-tests/test-http-server.backend-spec.js +1 -2
  28. package/src/context.d.ts +1 -6
  29. package/esm/remult-middleware.d.ts +0 -10
  30. package/esm/remult-middleware.js +0 -117
  31. package/esm/server/register-actions-on-server.d.ts +0 -3
  32. package/esm/server/register-actions-on-server.js +0 -24
  33. package/esm/server/register-entities-on-server.d.ts +0 -3
  34. package/esm/server/register-entities-on-server.js +0 -15
  35. package/esm/valueConverters.d.ts +0 -12
  36. package/esm/valueConverters.js +0 -169
  37. package/postgres/postgresColumnSyntax.d.ts +0 -2
  38. package/postgres/postgresColumnSyntax.js +0 -34
  39. package/remult-middleware.d.ts +0 -10
  40. package/remult-middleware.js +0 -131
  41. package/server/register-actions-on-server.d.ts +0 -3
  42. package/server/register-actions-on-server.js +0 -50
  43. package/server/register-entities-on-server.d.ts +0 -3
  44. package/server/register-entities-on-server.js +0 -19
  45. package/valueConverters.d.ts +0 -12
  46. package/valueConverters.js +0 -168
@@ -1,14 +1,12 @@
1
1
  import { __decorate, __metadata } from "tslib";
2
- import { registerActionsOnServer } from './register-actions-on-server';
3
- import { registerEntitiesOnServer } from './register-entities-on-server';
4
2
  import { JsonEntityFileStorage } from './JsonEntityFileStorage';
5
- import { Action, actionInfo } from '../src/server-action';
3
+ import { Action, actionInfo, classBackendMethodsArray, serverActionField } from '../src/server-action';
6
4
  import { DataApi, serializeError } from '../src/data-api';
7
5
  import { allEntities, Remult } from '../src/context';
8
6
  import { Entity, Fields, getEntityKey } from '../src/remult3';
9
7
  import { JsonDataProvider } from '../src/data-providers/json-data-provider';
10
8
  import { IdEntity } from '../src/id-entity';
11
- export function buildRemultServer(app, options) {
9
+ export function buildRemultServer(options) {
12
10
  if (!options) {
13
11
  options = {};
14
12
  }
@@ -37,33 +35,158 @@ export function buildRemultServer(app, options) {
37
35
  return dp;
38
36
  });
39
37
  }
40
- let bridge = new ExpressBridge(app, new inProcessQueueHandler(options.queueStorage), options.initRequest, dataProvider);
41
- if (options.logApiEndPoints !== undefined)
42
- bridge.logApiEndPoints = options.logApiEndPoints;
43
- if (options.rootPath === undefined)
44
- options.rootPath = Remult.apiBaseUrl;
45
- let apiArea = bridge.addArea(options.rootPath);
46
- if (!options.disableAutoApi) {
47
- let actions = [];
38
+ {
39
+ let allControllers = [];
40
+ if (!options.entities)
41
+ options.entities = [...allEntities];
48
42
  if (options.entities)
49
- actions.push(...options.entities);
43
+ allControllers.push(...options.entities);
50
44
  if (options.controllers)
51
- actions.push(...options.controllers);
52
- registerActionsOnServer(apiArea, actions);
53
- registerEntitiesOnServer(apiArea, options.entities);
45
+ allControllers.push(...options.controllers);
46
+ options.controllers = allControllers;
54
47
  }
48
+ if (options.rootPath === undefined)
49
+ options.rootPath = Remult.apiBaseUrl;
50
+ actionInfo.runningOnServer = true;
51
+ let bridge = new RemultServerImplementation(new inProcessQueueHandler(options.queueStorage), options, dataProvider);
55
52
  return bridge;
56
53
  }
57
54
  ;
58
- class ExpressBridge {
59
- constructor(app, queue, initRequest, dataProvider) {
60
- this.app = app;
55
+ class RemultServerImplementation {
56
+ constructor(queue, options, dataProvider) {
61
57
  this.queue = queue;
62
- this.initRequest = initRequest;
58
+ this.options = options;
63
59
  this.dataProvider = dataProvider;
60
+ this.registeredRouter = false;
61
+ this.hasQueue = false;
64
62
  /* internal */
65
63
  this.backendMethodsOpenApi = [];
66
- this.logApiEndPoints = true;
64
+ }
65
+ handle(req, gRes) {
66
+ if (!this.routeImpl) {
67
+ this.routeImpl = new RouteImplementation();
68
+ this.registerRouter(this.routeImpl);
69
+ }
70
+ return this.routeImpl.handle(req, gRes);
71
+ }
72
+ registerRouter(r) {
73
+ if (this.registeredRouter)
74
+ throw "Router already registered";
75
+ this.registeredRouter = true;
76
+ {
77
+ for (const c of this.options.controllers) {
78
+ let z = c[classBackendMethodsArray];
79
+ if (z)
80
+ for (const a of z) {
81
+ let x = a[serverActionField];
82
+ if (!x) {
83
+ throw 'failed to set server action, did you forget the BackendMethod Decorator?';
84
+ }
85
+ this.addAction(x, r);
86
+ }
87
+ }
88
+ if (this.hasQueue)
89
+ this.addAction({
90
+ __register: x => {
91
+ x(Action.apiUrlForJobStatus, false, () => true, async (data, req, res) => {
92
+ let job = await this.queue.getJobInfo(data.queuedJobId);
93
+ let userId = undefined;
94
+ if (req?.user)
95
+ userId = req.user.id;
96
+ if (job.userId == '')
97
+ job.userId = undefined;
98
+ if (userId != job.userId)
99
+ res.forbidden();
100
+ else
101
+ res.success(job.info);
102
+ });
103
+ }
104
+ }, r);
105
+ }
106
+ this.options.entities.forEach(e => {
107
+ let key = getEntityKey(e);
108
+ if (key != undefined)
109
+ this.add(key, c => {
110
+ return new DataApi(c.repo(e), c);
111
+ }, r);
112
+ });
113
+ }
114
+ add(key, dataApiFactory, r) {
115
+ let myRoute = this.options.rootPath + '/' + key;
116
+ if (this.options.logApiEndPoints)
117
+ console.log(myRoute);
118
+ r.route(myRoute)
119
+ .get(this.process((c, req, res) => dataApiFactory(c).httpGet(res, req))).put(this.process(async (c, req, res, orig) => dataApiFactory(c).put(res, '', orig.body)))
120
+ .delete(this.process(async (c, req, res, orig) => dataApiFactory(c).delete(res, '')))
121
+ .post(this.process(async (c, req, res, orig) => dataApiFactory(c).httpPost(res, req, orig.body)));
122
+ r.route(myRoute + '/:id')
123
+ //@ts-ignore
124
+ .get(this.process(async (c, req, res, orig) => dataApiFactory(c).get(res, orig.params.id)))
125
+ //@ts-ignore
126
+ .put(this.process(async (c, req, res, orig) => dataApiFactory(c).put(res, orig.params.id, orig.body)))
127
+ //@ts-ignore
128
+ .delete(this.process(async (c, req, res, orig) => dataApiFactory(c).delete(res, orig.params.id)));
129
+ }
130
+ process(what) {
131
+ return async (req, res) => {
132
+ let myReq = new ExpressRequestBridgeToDataApiRequest(req);
133
+ let myRes = new ExpressResponseBridgeToDataApiResponse(res, req);
134
+ let remult = new Remult();
135
+ remult.setDataProvider(await this.dataProvider);
136
+ if (req) {
137
+ let user;
138
+ if (this.options.getUser)
139
+ user = await this.options.getUser(req);
140
+ else {
141
+ user = req['user'];
142
+ if (!user)
143
+ user = req['auth'];
144
+ }
145
+ if (user)
146
+ remult.setUser(user);
147
+ }
148
+ if (this.options.initRequest) {
149
+ await this.options.initRequest(remult, req);
150
+ }
151
+ what(remult, myReq, myRes, req);
152
+ };
153
+ }
154
+ ;
155
+ async getRemult(req) {
156
+ let remult;
157
+ await this.process(async (c) => {
158
+ remult = c;
159
+ })(req, undefined);
160
+ return remult;
161
+ }
162
+ addAction(action, r) {
163
+ action.__register((url, queue, allowed, what) => {
164
+ let myUrl = this.options.rootPath + '/' + url;
165
+ let tag = (() => {
166
+ let split = url.split('/');
167
+ if (split.length == 1)
168
+ return 'Static Backend Methods';
169
+ else
170
+ return split[0];
171
+ })();
172
+ this.backendMethodsOpenApi.push({ path: myUrl, allowed, tag });
173
+ if (this.options.logApiEndPoints)
174
+ console.log(myUrl);
175
+ if (queue) {
176
+ this.hasQueue = true;
177
+ this.queue.mapQueuedAction(myUrl, what);
178
+ }
179
+ r.route(myUrl).post(this.process(async (remult, req, res, orig) => {
180
+ if (queue) {
181
+ let r = {
182
+ queuedJobId: await this.queue.submitJob(myUrl, remult, orig.body)
183
+ };
184
+ res.success(r);
185
+ }
186
+ else
187
+ return what(orig.body, remult, res);
188
+ }));
189
+ });
67
190
  }
68
191
  openApiDoc(options) {
69
192
  let r = new Remult();
@@ -115,7 +238,7 @@ class ExpressBridge {
115
238
  return item;
116
239
  }
117
240
  };
118
- for (const e of allEntities) {
241
+ for (const e of this.options.entities) {
119
242
  let meta = r.repo(e).metadata;
120
243
  let key = getEntityKey(e);
121
244
  let parameters = [];
@@ -352,117 +475,8 @@ class ExpressBridge {
352
475
  };
353
476
  return spec;
354
477
  }
355
- addArea(rootUrl) {
356
- var r = new SiteArea(this, this.app, rootUrl, this.logApiEndPoints);
357
- if (!this.firstArea) {
358
- this.firstArea = r;
359
- }
360
- return r;
361
- }
362
- async getRemult(req) {
363
- return this.firstArea.getRemult(req);
364
- }
365
- }
366
- export class SiteArea {
367
- constructor(bridge, app, rootUrl, logApiEndpoints) {
368
- this.bridge = bridge;
369
- this.app = app;
370
- this.rootUrl = rootUrl;
371
- this.logApiEndpoints = logApiEndpoints;
372
- }
373
- add(key, dataApiFactory) {
374
- let myRoute = this.rootUrl + '/' + key;
375
- if (this.logApiEndpoints)
376
- console.log(myRoute);
377
- this.app.route(myRoute)
378
- .get(this.process((c, req, res) => dataApiFactory(c).httpGet(res, req))).put(this.process(async (c, req, res, orig) => dataApiFactory(c).put(res, '', orig.body)))
379
- .delete(this.process(async (c, req, res, orig) => dataApiFactory(c).delete(res, '')))
380
- .post(this.process(async (c, req, res, orig) => dataApiFactory(c).httpPost(res, req, orig.body)));
381
- this.app.route(myRoute + '/:id')
382
- //@ts-ignore
383
- .get(this.process(async (c, req, res, orig) => dataApiFactory(c).get(res, orig.params.id)))
384
- //@ts-ignore
385
- .put(this.process(async (c, req, res, orig) => dataApiFactory(c).put(res, orig.params.id, orig.body)))
386
- //@ts-ignore
387
- .delete(this.process(async (c, req, res, orig) => dataApiFactory(c).delete(res, orig.params.id)));
388
- }
389
- process(what) {
390
- return async (req, res) => {
391
- let myReq = new ExpressRequestBridgeToDataApiRequest(req);
392
- let myRes = new ExpressResponseBridgeToDataApiResponse(res, req);
393
- let remult = new Remult();
394
- remult.setDataProvider(await this.bridge.dataProvider);
395
- if (req) {
396
- let user = req['user'];
397
- if (!user)
398
- user = req['auth'];
399
- if (user)
400
- remult.setUser(user);
401
- }
402
- if (this.bridge.initRequest) {
403
- await this.bridge.initRequest(remult, req);
404
- }
405
- what(remult, myReq, myRes, req);
406
- };
407
- }
408
- ;
409
- async getRemult(req) {
410
- let remult;
411
- await this.process(async (c) => {
412
- remult = c;
413
- })(req, undefined);
414
- return remult;
415
- }
416
- initQueue() {
417
- this.addAction({
418
- __register: x => {
419
- x(Action.apiUrlForJobStatus, false, () => true, async (data, req, res) => {
420
- let job = await this.bridge.queue.getJobInfo(data.queuedJobId);
421
- let userId = undefined;
422
- if (req?.user)
423
- userId = req.user.id;
424
- if (job.userId == '')
425
- job.userId = undefined;
426
- if (userId != job.userId)
427
- res.forbidden();
428
- else
429
- res.success(job.info);
430
- });
431
- }
432
- });
433
- this.initQueue = () => { };
434
- }
435
- addAction(action) {
436
- action.__register((url, queue, allowed, what) => {
437
- let myUrl = this.rootUrl + '/' + url;
438
- let tag = (() => {
439
- let split = url.split('/');
440
- if (split.length == 1)
441
- return 'Static Backend Methods';
442
- else
443
- return split[0];
444
- })();
445
- this.bridge.backendMethodsOpenApi.push({ path: myUrl, allowed, tag });
446
- if (this.logApiEndpoints)
447
- console.log(myUrl);
448
- if (queue) {
449
- this.initQueue();
450
- this.bridge.queue.mapQueuedAction(myUrl, what);
451
- }
452
- this.app.route(myUrl).post(this.process(async (remult, req, res, orig) => {
453
- if (queue) {
454
- let r = {
455
- queuedJobId: await this.bridge.queue.submitJob(myUrl, remult, orig.body)
456
- };
457
- res.success(r);
458
- }
459
- else
460
- return what(orig.body, remult, res);
461
- }));
462
- });
463
- }
464
478
  }
465
- export class ExpressRequestBridgeToDataApiRequest {
479
+ class ExpressRequestBridgeToDataApiRequest {
466
480
  constructor(r) {
467
481
  this.r = r;
468
482
  }
@@ -479,8 +493,6 @@ class ExpressResponseBridgeToDataApiResponse {
479
493
  this.setStatus(403).end();
480
494
  }
481
495
  setStatus(status) {
482
- if (this.r.setStatus)
483
- return this.r.setStatus(status);
484
496
  return this.r.status(status);
485
497
  }
486
498
  progress(progress) {
@@ -624,6 +636,114 @@ export class EntityQueueStorage {
624
636
  return q.id;
625
637
  }
626
638
  }
639
+ class RouteImplementation {
640
+ constructor() {
641
+ this.map = new Map();
642
+ }
643
+ route(path) {
644
+ //consider using:
645
+ //* https://raw.githubusercontent.com/cmorten/opine/main/src/utils/pathToRegex.ts
646
+ //* https://github.com/pillarjs/path-to-regexp
647
+ let r = path.toLowerCase();
648
+ let m = new Map();
649
+ this.map.set(r, m);
650
+ const route = {
651
+ get: (h) => {
652
+ m.set("get", h);
653
+ return route;
654
+ },
655
+ put: (h) => {
656
+ m.set("put", h);
657
+ return route;
658
+ },
659
+ post: (h) => {
660
+ m.set("post", h);
661
+ return route;
662
+ },
663
+ delete: (h) => {
664
+ m.set("delete", h);
665
+ return route;
666
+ }
667
+ };
668
+ return route;
669
+ }
670
+ async handle(req, gRes) {
671
+ return new Promise(res => {
672
+ const response = new class {
673
+ constructor() {
674
+ this.statusCode = 200;
675
+ }
676
+ json(data) {
677
+ if (gRes !== undefined)
678
+ gRes.json(data);
679
+ res({ statusCode: this.statusCode, data });
680
+ }
681
+ status(statusCode) {
682
+ if (gRes !== undefined)
683
+ gRes.status(statusCode);
684
+ this.statusCode = statusCode;
685
+ return this;
686
+ }
687
+ end() {
688
+ if (gRes !== undefined)
689
+ gRes.end();
690
+ res({
691
+ statusCode: this.statusCode
692
+ });
693
+ }
694
+ };
695
+ this.middleware(req, response, () => res(undefined));
696
+ });
697
+ }
698
+ middleware(req, res, next) {
699
+ let theUrl = req.url;
700
+ if (theUrl.startsWith('/')) //next sends a partial url '/api/tasks' and not the full url
701
+ theUrl = 'http://stam' + theUrl;
702
+ const url = new URL(theUrl);
703
+ const path = url.pathname;
704
+ if (!req.query) {
705
+ let query = {};
706
+ url.searchParams.forEach((val, key) => {
707
+ let current = query[key];
708
+ if (!current) {
709
+ query[key] = val;
710
+ return;
711
+ }
712
+ if (Array.isArray(current)) {
713
+ current.push(val);
714
+ return;
715
+ }
716
+ query[key] = [current, val];
717
+ });
718
+ req.query = query;
719
+ }
720
+ let lowerPath = path.toLowerCase();
721
+ let m = this.map.get(lowerPath);
722
+ if (m) {
723
+ let h = m.get(req.method.toLowerCase());
724
+ if (h) {
725
+ h(req, res, next);
726
+ return;
727
+ }
728
+ }
729
+ let idPosition = path.lastIndexOf('/');
730
+ if (idPosition >= 0) {
731
+ lowerPath = path.substring(0, idPosition) + '/:id';
732
+ m = this.map.get(lowerPath);
733
+ if (m) {
734
+ let h = m.get(req.method.toLowerCase());
735
+ if (h) {
736
+ if (!req.params)
737
+ req.params = {};
738
+ req.params.id = path.substring(idPosition + 1);
739
+ h(req, res, next);
740
+ return;
741
+ }
742
+ }
743
+ }
744
+ next();
745
+ }
746
+ }
627
747
  let JobsInQueueEntity = class JobsInQueueEntity extends IdEntity {
628
748
  };
629
749
  __decorate([
@@ -1,4 +1,2 @@
1
1
  export { JsonEntityFileStorage } from './JsonEntityFileStorage';
2
- export { SiteArea, queuedJobInfo, QueueStorage } from './expressBridge';
3
- export { registerEntitiesOnServer } from './register-entities-on-server';
4
- export { registerActionsOnServer } from './register-actions-on-server';
2
+ export { queuedJobInfo, QueueStorage, GenericRequest, GenericRequestHandler, GenericResponse, GenericRouter, buildRemultServer, RemultServerOptions, SpecificRoute, RemultServer } from './expressBridge';
@@ -1,4 +1,2 @@
1
1
  export { JsonEntityFileStorage } from './JsonEntityFileStorage';
2
- export { SiteArea } from './expressBridge';
3
- export { registerEntitiesOnServer } from './register-entities-on-server';
4
- export { registerActionsOnServer } from './register-actions-on-server';
2
+ export { buildRemultServer } from './expressBridge';
@@ -11,9 +11,8 @@ const environments = [
11
11
  ["koa", 3002],
12
12
  ["fastify", 3003],
13
13
  ["express", 3004],
14
- ["middleware", 3005],
14
+ ["generic server", 3005],
15
15
  ["optine", 3006],
16
- ["fresh", 8000],
17
16
  ];
18
17
  function test(name, test) {
19
18
  for (const [env, port] of environments) {
@@ -40,12 +40,7 @@ export declare class Remult {
40
40
  /** Returns the current user's info */
41
41
  get user(): UserInfo;
42
42
  /** Set's the current user info */
43
- setUser(info: UserInfo | {
44
- sub?: string;
45
- name?: string;
46
- permissions?: string[];
47
- username?: string;
48
- }): Promise<void>;
43
+ setUser(info: UserInfo): Promise<void>;
49
44
  private _user;
50
45
  private _userChangeEvent;
51
46
  /** Checks if a user was authenticated */
package/graphql.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { RemultExpressBridge } from "./server/expressBridge";
2
- export declare function remultGraphql(api: RemultExpressBridge): {
1
+ import { RemultServer } from "./server/expressBridge";
2
+ export declare function remultGraphql(api: RemultServer): {
3
3
  rootValue: {};
4
4
  schema: string;
5
5
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "remult",
3
- "version": "0.14.30",
3
+ "version": "0.14.33",
4
4
  "description": "A CRUD framework for full-stack TypeScript",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -28,9 +28,9 @@
28
28
  "require": "./remult-express.js",
29
29
  "import": "./esm/remult-express.js"
30
30
  },
31
- "./remult-middleware": {
32
- "require": "./remult-middleware.js",
33
- "import": "./esm/remult-middleware.js"
31
+ "./server": {
32
+ "require": "./server/index.js",
33
+ "import": "./esm/server/index.js"
34
34
  },
35
35
  "./remult-fastify": {
36
36
  "require": "./remult-fastify.js",
@@ -1,11 +1,6 @@
1
- import { RemultMiddlewareOptions } from './server/expressBridge';
2
- export declare function remultExpress(options?: RemultMiddlewareOptions & {
1
+ import * as express from 'express';
2
+ import { RemultServer, RemultServerOptions } from './server/expressBridge';
3
+ export declare function remultExpress(options?: RemultServerOptions & {
3
4
  bodyParser?: boolean;
4
5
  bodySizeLimit?: string;
5
- }): import("express-serve-static-core").Router & {
6
- getRemult: (req: any) => Promise<import(".").Remult>;
7
- openApiDoc: (options: {
8
- title: string;
9
- }) => any;
10
- addArea: (x: any) => void;
11
- };
6
+ }): express.RequestHandler & RemultServer;
package/remult-express.js CHANGED
@@ -15,11 +15,12 @@ function remultExpress(options) {
15
15
  app.use(express.json({ limit: options.bodySizeLimit }));
16
16
  app.use(express.urlencoded({ extended: true, limit: options.bodySizeLimit }));
17
17
  }
18
- var server = expressBridge_1.buildRemultServer(app, options);
18
+ var server = expressBridge_1.buildRemultServer(options);
19
+ server.registerRouter(app);
19
20
  return Object.assign(app, {
20
21
  getRemult: function (req) { return server.getRemult(req); },
21
22
  openApiDoc: function (options) { return server.openApiDoc(options); },
22
- addArea: function (x) { return server.addArea(x); }
23
+ registerRouter: function (x) { return server.registerRouter(x); }
23
24
  });
24
25
  }
25
26
  exports.remultExpress = remultExpress;
@@ -1,3 +1,3 @@
1
1
  import type { FastifyPluginCallback } from 'fastify';
2
- import { RemultServer, RemultMiddlewareOptions } from './server/expressBridge';
3
- export declare function remultFastify(options: RemultMiddlewareOptions): FastifyPluginCallback & RemultServer;
2
+ import { RemultServer, RemultServerOptions } from './server/expressBridge';
3
+ export declare function remultFastify(options: RemultServerOptions): FastifyPluginCallback & RemultServer;
package/remult-fastify.js CHANGED
@@ -23,7 +23,7 @@ function remultFastify(options) {
23
23
  };
24
24
  return response;
25
25
  }
26
- var api;
26
+ var api = expressBridge_1.buildRemultServer(options);
27
27
  var pluginFunction = function (instance, op) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
28
28
  var fastifyRouter;
29
29
  return tslib_1.__generator(this, function (_a) {
@@ -48,19 +48,14 @@ function remultFastify(options) {
48
48
  return r;
49
49
  },
50
50
  };
51
- api = expressBridge_1.buildRemultServer(fastifyRouter, options);
51
+ api.registerRouter(fastifyRouter);
52
52
  return [2 /*return*/];
53
53
  });
54
54
  }); };
55
- var getApi = function () {
56
- if (!api)
57
- throw "Please call fastify's register before using this method";
58
- return api;
59
- };
60
55
  return Object.assign(pluginFunction, {
61
- addArea: function (x) { return getApi().addArea(x); },
62
- getRemult: function (x) { return getApi().getRemult(x); },
63
- openApiDoc: function (x) { return getApi().openApiDoc(x); }
56
+ getRemult: function (x) { return api.getRemult(x); },
57
+ openApiDoc: function (x) { return api.openApiDoc(x); },
58
+ handle: function (req, res) { return api.handle(req, res); }
64
59
  });
65
60
  }
66
61
  exports.remultFastify = remultFastify;
package/remult-fresh.d.ts CHANGED
@@ -1,18 +1,17 @@
1
- import { RemultMiddlewareOptions, RemultServer } from "./server/expressBridge";
2
- export declare function remultFresh(options: RemultMiddlewareOptions, response: FreshResponse): RemultFresh;
1
+ import { RemultServerOptions, RemultServer } from "./server/expressBridge";
2
+ export declare function remultFresh(options: RemultServerOptions, response: FreshResponse): RemultFresh;
3
3
  export interface RemultFresh extends RemultServer {
4
- handle(req: FreshRequest, ctx: FreshContext): Promise<FreshResponse>;
4
+ freshHandler(req: FreshRequest, ctx: FreshContext): Promise<FreshResponse>;
5
5
  }
6
- interface FreshRequest {
6
+ export interface FreshRequest {
7
7
  url: string;
8
8
  method: string;
9
9
  json: () => Promise<any>;
10
10
  }
11
- interface FreshContext {
11
+ export interface FreshContext {
12
12
  next: () => Promise<any>;
13
13
  }
14
- interface FreshResponse {
14
+ export interface FreshResponse {
15
15
  new (body?: any | undefined, init?: ResponseInit): any;
16
16
  json(data: unknown, init?: ResponseInit): any;
17
17
  }
18
- export {};
package/remult-fresh.js CHANGED
@@ -2,13 +2,13 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.remultFresh = void 0;
4
4
  var tslib_1 = require("tslib");
5
- var remult_middleware_1 = require("./remult-middleware");
5
+ var expressBridge_1 = require("./server/expressBridge");
6
6
  function remultFresh(options, response) {
7
7
  var _this = this;
8
- var mw = remult_middleware_1.remultMiddleware(options);
9
- var orig = mw.handle;
10
- return tslib_1.__assign(tslib_1.__assign({}, mw), {
11
- handle: function (req, ctx) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
8
+ var server = expressBridge_1.buildRemultServer(options);
9
+ var orig = server.handle;
10
+ return Object.assign(server, {
11
+ freshHandler: function (req, ctx) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
12
12
  var theReq, _a, _b, init, res;
13
13
  return tslib_1.__generator(this, function (_c) {
14
14
  switch (_c.label) {
@@ -25,7 +25,6 @@ function remultFresh(options, response) {
25
25
  }
26
26
  return [3 /*break*/, 3];
27
27
  case 1:
28
- console.log(req);
29
28
  _b = theReq;
30
29
  return [4 /*yield*/, req.json()];
31
30
  case 2:
@@ -37,17 +36,17 @@ function remultFresh(options, response) {
37
36
  case 4:
38
37
  res = _c.sent();
39
38
  if (res) {
40
- if (res.statusCode)
41
- init.status = res.statusCode;
39
+ init.status = res.statusCode;
42
40
  if (res.data) {
43
- console.log({ init: init, res: res });
44
41
  return [2 /*return*/, response.json(res.data, init)];
45
42
  }
46
43
  else
47
44
  return [2 /*return*/, new response(undefined, init)];
48
45
  }
49
- else
46
+ else {
50
47
  return [2 /*return*/, ctx.next()];
48
+ }
49
+ ;
51
50
  return [2 /*return*/];
52
51
  }
53
52
  });
@@ -56,3 +55,4 @@ function remultFresh(options, response) {
56
55
  }
57
56
  exports.remultFresh = remultFresh;
58
57
  ;
58
+ ;