@terreno/api 0.7.2 → 0.8.1

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 (50) hide show
  1. package/dist/__tests__/{versionCheck.test.js → versionCheckPlugin.test.js} +2 -2
  2. package/dist/api.d.ts +4 -2
  3. package/dist/api.js +7 -2
  4. package/dist/consentApp.d.ts +33 -0
  5. package/dist/consentApp.js +484 -0
  6. package/dist/consentApp.test.d.ts +1 -0
  7. package/dist/consentApp.test.js +1132 -0
  8. package/dist/index.d.ts +6 -0
  9. package/dist/index.js +6 -0
  10. package/dist/models/consentForm.d.ts +2 -0
  11. package/dist/models/consentForm.js +115 -0
  12. package/dist/models/consentResponse.d.ts +2 -0
  13. package/dist/models/consentResponse.js +73 -0
  14. package/dist/models/versionConfig.d.ts +1 -1
  15. package/dist/openApiValidator.js +2 -0
  16. package/dist/populate.d.ts +1 -0
  17. package/dist/populate.js +53 -13
  18. package/dist/syncConsents.d.ts +67 -0
  19. package/dist/syncConsents.js +334 -0
  20. package/dist/syncConsents.test.d.ts +1 -0
  21. package/dist/syncConsents.test.js +249 -0
  22. package/dist/terrenoApp.js +6 -5
  23. package/dist/terrenoPlugin.d.ts +1 -1
  24. package/dist/types/consentForm.d.ts +32 -0
  25. package/dist/types/consentForm.js +2 -0
  26. package/dist/types/consentResponse.d.ts +23 -0
  27. package/dist/types/consentResponse.js +2 -0
  28. package/dist/vendor/wesleytodd-openapi/lib/generate-doc.js +9 -2
  29. package/dist/versionCheckPlugin.d.ts +2 -0
  30. package/dist/versionCheckPlugin.js +3 -6
  31. package/package.json +1 -1
  32. package/src/__tests__/{versionCheck.test.ts → versionCheckPlugin.test.ts} +2 -2
  33. package/src/api.ts +11 -4
  34. package/src/consentApp.test.ts +749 -0
  35. package/src/consentApp.ts +463 -0
  36. package/src/index.ts +6 -0
  37. package/src/models/consentForm.ts +123 -0
  38. package/src/models/consentResponse.ts +78 -0
  39. package/src/models/versionConfig.ts +1 -1
  40. package/src/openApiValidator.ts +2 -0
  41. package/src/populate.ts +33 -0
  42. package/src/syncConsents.test.ts +124 -0
  43. package/src/syncConsents.ts +263 -0
  44. package/src/terrenoApp.ts +6 -6
  45. package/src/terrenoPlugin.ts +1 -1
  46. package/src/types/consentForm.ts +41 -0
  47. package/src/types/consentResponse.ts +34 -0
  48. package/src/vendor/wesleytodd-openapi/lib/generate-doc.js +8 -2
  49. package/src/versionCheckPlugin.ts +5 -6
  50. /package/dist/__tests__/{versionCheck.test.d.ts → versionCheckPlugin.test.d.ts} +0 -0
@@ -0,0 +1,23 @@
1
+ import type mongoose from "mongoose";
2
+ import type { FindExactlyOnePlugin, FindOneOrNonePlugin } from "../plugins";
3
+ export type ConsentResponseMethods = {};
4
+ export type ConsentResponseStatics = FindExactlyOnePlugin<ConsentResponseDocument> & FindOneOrNonePlugin<ConsentResponseDocument>;
5
+ export type ConsentResponseModel = mongoose.Model<ConsentResponseDocument, object, ConsentResponseMethods> & ConsentResponseStatics;
6
+ export interface ConsentResponseDocument extends mongoose.Document {
7
+ _id: mongoose.Types.ObjectId;
8
+ userId: mongoose.Types.ObjectId;
9
+ consentFormId: mongoose.Types.ObjectId;
10
+ agreed: boolean;
11
+ agreedAt: Date;
12
+ checkboxValues?: Map<string, boolean>;
13
+ locale: string;
14
+ signature?: string;
15
+ signedAt?: Date;
16
+ ipAddress?: string;
17
+ userAgent?: string;
18
+ contentSnapshot?: string;
19
+ formVersionSnapshot?: number;
20
+ created: Date;
21
+ updated: Date;
22
+ deleted: boolean;
23
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -69,7 +69,14 @@ module.exports = function generateDocument(baseDocument, router, basePath) {
69
69
  });
70
70
  }
71
71
  operation.parameters = params_1;
72
- path = pathToRegexp.compile(path.replace(/\*|\(\*\)/g, '(.*)'))(keys_1, { encode: function (value) { return value; } });
72
+ try {
73
+ path = pathToRegexp.compile(path.replace(/\*|\(\*\)/g, '(.*)'))(keys_1, { encode: function (value) { return value; } });
74
+ }
75
+ catch (_e) {
76
+ // Express 5 stores path params in matchers instead of layer.keys, so keys
77
+ // may be incomplete. Fall back to simple regex replacement of :param → {param}.
78
+ path = path.replace(/:([a-zA-Z0-9_]+)/g, '{$1}');
79
+ }
73
80
  }
74
81
  doc.paths[path] = doc.paths[path] || {};
75
82
  doc.paths[path][layer.method] = operation;
@@ -108,7 +115,7 @@ function processComplexMatch(thing, keys) {
108
115
  // (i.e. /:id, /:name, etc...) with the name(s) of those parameter(s)
109
116
  // This could have been accomplished with replaceAll for Node version 15 and above
110
117
  // no-useless-escape is disabled since we need three backslashes
111
- .replace(/\(\?\:\(\[\^\\\/\]\+\?\)\)/g, function () { return "{".concat(keys[i++].name, "}"); }) // eslint-disable-line no-useless-escape
118
+ .replace(/\(\?\:\(\[\^\\\/\]\+\?\)\)/g, function () { return "{".concat(keys[i++].name, "}"); })
112
119
  .replace(/\\(.)/g, '$1')
113
120
  // The replace below removes the regex used at the start of the string and
114
121
  // the regex used to match the query parameters
@@ -3,8 +3,10 @@ import type { TerrenoPlugin } from "./terrenoPlugin";
3
3
  export type VersionCheckStatus = "ok" | "warning" | "required";
4
4
  export interface VersionCheckResponse {
5
5
  message?: string;
6
+ requiredVersion?: number;
6
7
  status: VersionCheckStatus;
7
8
  updateUrl?: string;
9
+ warningVersion?: number;
8
10
  }
9
11
  /**
10
12
  * TerrenoPlugin that adds a public GET /version-check endpoint for upgrade enforcement.
@@ -80,21 +80,18 @@ var VersionCheckPlugin = /** @class */ (function () {
80
80
  ? ((_c = config.webWarningVersion) !== null && _c !== void 0 ? _c : 0)
81
81
  : ((_d = config.mobileWarningVersion) !== null && _d !== void 0 ? _d : 0);
82
82
  response = {
83
+ requiredVersion: requiredVersion > 0 ? requiredVersion : undefined,
83
84
  status: "ok",
85
+ updateUrl: config.updateUrl || undefined,
86
+ warningVersion: warningVersion > 0 ? warningVersion : undefined,
84
87
  };
85
88
  if (requiredVersion > 0 && version < requiredVersion) {
86
89
  response.status = "required";
87
90
  response.message = (_e = config.requiredMessage) !== null && _e !== void 0 ? _e : DEFAULT_REQUIRED_MESSAGE;
88
- if (config.updateUrl) {
89
- response.updateUrl = config.updateUrl;
90
- }
91
91
  }
92
92
  else if (warningVersion > 0 && version < warningVersion) {
93
93
  response.status = "warning";
94
94
  response.message = (_f = config.warningMessage) !== null && _f !== void 0 ? _f : DEFAULT_WARNING_MESSAGE;
95
- if (config.updateUrl) {
96
- response.updateUrl = config.updateUrl;
97
- }
98
95
  }
99
96
  return [2 /*return*/, res.json(response)];
100
97
  }
package/package.json CHANGED
@@ -100,5 +100,5 @@
100
100
  "updateSnapshot": "bun test --update-snapshots"
101
101
  },
102
102
  "types": "dist/index.d.ts",
103
- "version": "0.7.2"
103
+ "version": "0.8.1"
104
104
  }
@@ -52,7 +52,7 @@ describe("VersionCheckPlugin", () => {
52
52
 
53
53
  const res = await app.get("/version-check").query({platform: "web", version: 150});
54
54
  expect(res.status).toBe(200);
55
- expect(res.body).toEqual({status: "ok"});
55
+ expect(res.body).toEqual({requiredVersion: 50, status: "ok", warningVersion: 100});
56
56
  });
57
57
 
58
58
  it("returns warning when client version < warning (web)", async () => {
@@ -116,7 +116,7 @@ describe("VersionCheckPlugin", () => {
116
116
 
117
117
  const res = await app.get("/version-check").query({platform: "web", version: 100});
118
118
  expect(res.status).toBe(200);
119
- expect(res.body).toEqual({status: "ok"});
119
+ expect(res.body).toEqual({requiredVersion: 50, status: "ok", warningVersion: 100});
120
120
  });
121
121
 
122
122
  it("version equal to required returns warning not required", async () => {
package/src/api.ts CHANGED
@@ -160,8 +160,8 @@ export interface ModelRouterOptions<T> {
160
160
  * query, max limit,
161
161
  * or 500. */
162
162
  maxLimit?: number; // defaults to 500
163
- /** */
164
- endpoints?: (router: any) => void;
163
+ /** Custom route setup function. Receives the router and optionally the full options (including openApi). */
164
+ endpoints?: (router: any, options?: Partial<ModelRouterOptions<T>>) => void;
165
165
  /**
166
166
  * Hook that runs after `transformer.transform` but before the object is created.
167
167
  * Can update the body fields based on the request or the user.
@@ -416,6 +416,8 @@ export interface ModelRouterRegistration {
416
416
  path: string;
417
417
  /** The Express router containing CRUD endpoints */
418
418
  router: express.Router;
419
+ /** @internal Rebuilds the router with the openApi instance injected into options */
420
+ _buildWithOpenApi: (openApi: any) => express.Router;
419
421
  }
420
422
 
421
423
  /**
@@ -459,7 +461,12 @@ export function modelRouter<T>(
459
461
  const router = _buildModelRouter(model, options);
460
462
 
461
463
  if (path !== undefined) {
462
- return {__type: "modelRouter", path, router};
464
+ return {
465
+ __type: "modelRouter",
466
+ _buildWithOpenApi: (openApi: any) => _buildModelRouter(model, {...options, openApi}),
467
+ path,
468
+ router,
469
+ };
463
470
  }
464
471
  return router;
465
472
  }
@@ -469,7 +476,7 @@ function _buildModelRouter<T>(model: Model<T>, options: ModelRouterOptions<T>):
469
476
 
470
477
  // Do before the other router options so endpoints take priority.
471
478
  if (options.endpoints) {
472
- options.endpoints(router);
479
+ options.endpoints(router, options);
473
480
  }
474
481
 
475
482
  const responseHandler = options.responseHandler ?? defaultResponseHandler;