@raytio/core 11.1.0 → 11.3.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.
Files changed (93) hide show
  1. package/CHANGELOG.md +17 -2
  2. package/README.md +123 -33
  3. package/dist/accessApplication/api/__tests__/fetchKeysForSubmission.test.d.ts +1 -0
  4. package/dist/accessApplication/api/__tests__/fetchKeysForSubmission.test.js +28 -0
  5. package/dist/accessApplication/api/__tests__/fetchPOsOrAOsForSubmission.test.d.ts +1 -0
  6. package/dist/accessApplication/api/__tests__/fetchPOsOrAOsForSubmission.test.js +23 -0
  7. package/dist/accessApplication/api/__tests__/fetchRelationshipsForSubmission.test.d.ts +1 -0
  8. package/dist/accessApplication/api/__tests__/fetchRelationshipsForSubmission.test.js +27 -0
  9. package/dist/accessApplication/api/__tests__/getMissingDataForInstance.test.d.ts +1 -0
  10. package/dist/accessApplication/api/__tests__/getMissingDataForInstance.test.js +30 -0
  11. package/dist/accessApplication/api/fetchKeysForSubmission.d.ts +12 -0
  12. package/dist/accessApplication/api/fetchKeysForSubmission.js +16 -0
  13. package/dist/accessApplication/api/fetchPOsOrAOsForSubmission.d.ts +6 -0
  14. package/dist/accessApplication/api/fetchPOsOrAOsForSubmission.js +25 -0
  15. package/dist/accessApplication/api/fetchRelationshipsForSubmission.d.ts +6 -0
  16. package/dist/accessApplication/api/fetchRelationshipsForSubmission.js +10 -0
  17. package/dist/accessApplication/api/getMissingDataForInstance.d.ts +10 -0
  18. package/dist/accessApplication/api/getMissingDataForInstance.js +28 -0
  19. package/dist/accessApplication/api/index.d.ts +2 -0
  20. package/dist/accessApplication/api/index.js +18 -0
  21. package/dist/accessApplication/api/legacy/__tests__/convertRelationships.test.d.ts +1 -0
  22. package/dist/accessApplication/api/legacy/__tests__/convertRelationships.test.js +37 -0
  23. package/dist/accessApplication/api/legacy/convertRelationships.d.ts +16 -0
  24. package/dist/accessApplication/api/legacy/convertRelationships.js +21 -0
  25. package/dist/accessApplication/api/legacy/index.d.ts +1 -0
  26. package/dist/accessApplication/api/legacy/index.js +17 -0
  27. package/dist/accessApplication/createAA.js +6 -5
  28. package/dist/accessApplication/index.d.ts +1 -0
  29. package/dist/accessApplication/index.js +1 -0
  30. package/dist/crypto/cognitoAttributes.d.ts +1 -1
  31. package/dist/crypto/decryptSharedData.d.ts +2 -2
  32. package/dist/crypto/decryptSharedData.js +2 -5
  33. package/dist/crypto/getAADecryptor.d.ts +18 -3
  34. package/dist/crypto/getAADecryptor.js +34 -6
  35. package/dist/crypto/helpers.d.ts +2 -2
  36. package/dist/general/types.d.ts +9 -0
  37. package/dist/general/types.js +10 -1
  38. package/dist/rules/calculateScore.d.ts +1 -1
  39. package/dist/rules/convertInstanceToRuleInput.d.ts +3 -3
  40. package/dist/rules/convertInstanceToRuleInput.js +5 -2
  41. package/dist/rules/evaluateScoreCondition.d.ts +1 -1
  42. package/dist/rules/helpers/__tests__/getValuesFromPath.test.js +1 -1
  43. package/dist/rules/helpers/addInfiniteThresholdBoundaries.d.ts +1 -1
  44. package/dist/rules/helpers/checkTypeofValue.d.ts +1 -1
  45. package/dist/rules/helpers/getValuesFromPath.d.ts +1 -1
  46. package/dist/rules/helpers/thresholds.d.ts +1 -1
  47. package/dist/rules/operators/bool.d.ts +1 -1
  48. package/dist/rules/operators/date.d.ts +1 -1
  49. package/dist/rules/operators/hfield.d.ts +1 -1
  50. package/dist/rules/operators/hschema.d.ts +1 -1
  51. package/dist/rules/operators/index.d.ts +1 -1
  52. package/dist/rules/operators/number.d.ts +1 -1
  53. package/dist/rules/operators/string.d.ts +1 -1
  54. package/dist/rules/types/config.d.ts +4 -3
  55. package/dist/rules/types/dataValueTypes.d.ts +2 -3
  56. package/dist/rules/types/internal.d.ts +4 -3
  57. package/dist/rules/validate.d.ts +2 -2
  58. package/dist/schema/expandSchema/__tests__/expandSchema.test.js +8 -7
  59. package/dist/schema/expandSchema/__tests__/i18n.test.js +1 -1
  60. package/dist/schema/expandSchema/__tests__/maybeUseI18n.test.js +2 -2
  61. package/dist/schema/expandSchema/__tests__/processSchema.test.js +6 -6
  62. package/dist/schema/expandSchema/addLoadingTimes.d.ts +1 -1
  63. package/dist/schema/expandSchema/constants.d.ts +1 -1
  64. package/dist/schema/expandSchema/expandSchema.d.ts +1 -1
  65. package/dist/schema/expandSchema/maybeUseI18n.d.ts +1 -1
  66. package/dist/schema/expandSchema/processSchema.d.ts +3 -4
  67. package/dist/schema/expandSchema/processSchema.js +2 -2
  68. package/dist/schema/expandSchema/removePrivateFields.d.ts +8 -6
  69. package/dist/schema/expandSchema/sortSchemaProperties.d.ts +3 -3
  70. package/dist/schema/expandSchema/unwrapSchema.d.ts +2 -6
  71. package/dist/schema/expandSchema/unwrapSchema.js +1 -1
  72. package/dist/schema/labels.d.ts +2 -1
  73. package/dist/testHelpers.d.ts +9 -0
  74. package/dist/testHelpers.js +9 -0
  75. package/dist/verifications/cleanInstance.d.ts +1 -1
  76. package/dist/verifications/getPOVerification.d.ts +1 -1
  77. package/dist/verifications/getPOVerification.js +6 -2
  78. package/dist/verifications/getVerifiedBy.js +6 -1
  79. package/dist/verifications/index.d.ts +0 -1
  80. package/dist/verifications/index.js +0 -1
  81. package/dist/verifications/safeHarbour.d.ts +2 -2
  82. package/dist/verifications/verifyCheck/__tests__/getOwnRealVerifications.test.js +74 -9
  83. package/dist/verifications/verifyCheck/__tests__/getSomeoneElsesRealVerifications.test.js +2 -2
  84. package/dist/verifications/verifyCheck/getOwnRealVerifications.d.ts +1 -1
  85. package/dist/verifications/verifyCheck/getOwnRealVerifications.js +3 -1
  86. package/dist/verifications/verifyCheck/getSomeoneElsesRealVerifications.d.ts +1 -1
  87. package/dist/verifications/verifyCheck/getSomeoneElsesRealVerifications.js +1 -1
  88. package/dist/verifications/verifyCheck/operations/__tests__/checkOwnVerification.test.js +45 -5
  89. package/dist/verifications/verifyCheck/operations/__tests__/checkSomeoneElsesVerifications.test.js +2 -2
  90. package/dist/verifications/verifyCheck/operations/__tests__/sampleBundle.json +1 -0
  91. package/dist/verifications/verifyCheck/operations/checkOwnVerification.d.ts +4 -3
  92. package/dist/verifications/verifyCheck/operations/checkOwnVerification.js +17 -8
  93. package/package.json +6 -42
package/CHANGELOG.md CHANGED
@@ -5,13 +5,28 @@ All notable changes to this project will be documented in this file.
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
+ <!--
9
+ If you think there are missing entries, go to:
10
+ https://gitlab.com/raytio/raytio-client-v2/-/commits/master/packages/core
11
+ to see all commits that touched this package.
12
+ -->
13
+
8
14
  ## [Unreleased]
9
15
 
10
- ## 11.1.0 (2022-04-20)
16
+ ## 11.3.0 (2023-12-05)
17
+
18
+ - Migrate to the v3 API for instances
19
+
20
+ ## 11.2.0 (2023-07-03)
21
+
22
+ - Support key rotation
23
+ - Support the new Schema API format
24
+
25
+ ## 11.1.0 (2023-04-20)
11
26
 
12
27
  - Move `expandSchema` into core
13
28
 
14
- ## 11.0.0 (2022-04-20)
29
+ ## 11.0.0 (2023-04-20)
15
30
 
16
31
  - 💥 BREAKING CHANGE: Changed the input arguments of `convertInstanceToRuleInput` to only require a list of `ProfileObject`s, instead of the whole `Instance` object.
17
32
  - Any profile object with an expired field is now considered `Expired`, even if other fields are verified.
package/README.md CHANGED
@@ -20,6 +20,7 @@ If you wish to use `@raytio/core` directly, an example of configuring polyfills
20
20
 
21
21
  - [SafeHarbourObj](#safeharbourobj)
22
22
  - [SafeHarbourResult](#safeharbourresult)
23
+ - [ServerRelationship](#serverrelationship)
23
24
  - [VerDetails](#verdetails)
24
25
 
25
26
  ### Functions
@@ -27,9 +28,10 @@ If you wish to use `@raytio/core` directly, an example of configuring polyfills
27
28
  - [calcSafeHarbourScore](#calcsafeharbourscore)
28
29
  - [calculateScore](#calculatescore)
29
30
  - [checkJsonSignature](#checkjsonsignature)
30
- - [cleanInstance](#cleaninstance)
31
31
  - [convertInstanceToRuleInput](#convertinstancetoruleinput)
32
+ - [convertServerRelationship](#convertserverrelationship)
32
33
  - [createAA](#createaa)
34
+ - [createHashedNId](#createhashednid)
33
35
  - [decryptSharedData](#decryptshareddata)
34
36
  - [evaluateRule](#evaluaterule)
35
37
  - [expandSchema](#expandschema)
@@ -37,6 +39,8 @@ If you wish to use `@raytio/core` directly, an example of configuring polyfills
37
39
  - [findSuitableLocale](#findsuitablelocale)
38
40
  - [fromCognitoAttributes](#fromcognitoattributes)
39
41
  - [getAADecryptor](#getaadecryptor)
42
+ - [getAAPublicKey](#getaapublickey)
43
+ - [getMissingDataForInstance](#getmissingdataforinstance)
40
44
  - [getNidFromUrn](#getnidfromurn)
41
45
  - [getOwnRealVerifications](#getownrealverifications)
42
46
  - [getPOVerification](#getpoverification)
@@ -77,6 +81,14 @@ the response from [calcSafeHarbourScore](#calcsafeharbourscore)
77
81
 
78
82
  ___
79
83
 
84
+ ### ServerRelationship
85
+
86
+ Ƭ **ServerRelationship**: `Omit`<`Relationship`, ``"p_id"`` \| ``"start"`` \| ``"end"``\> & { `from_id`: `NId` ; `id`: `PId` } & { `to_id`: `NId` } \| { `to_i_id`: `IId` }
87
+
88
+ This type represents the relationship format returned by the Postgres API
89
+
90
+ ___
91
+
80
92
  ### VerDetails
81
93
 
82
94
  Ƭ **VerDetails**: `Object`
@@ -108,7 +120,7 @@ from different sources. For information, refer to the
108
120
  | `data.person` | `ProfileObject` |
109
121
  | `data.profileObjects` | `ProfileObject`[] |
110
122
  | `data.realVers` | `RealVer`[] |
111
- | `data.getSchema` | (`schemaName`: `string`) => `Promise`<`Schema`\> |
123
+ | `data.getSchema` | (`schemaName`: `SchemaName`) => `Promise`<`Schema`\> |
112
124
 
113
125
  #### Returns
114
126
 
@@ -138,7 +150,7 @@ ___
138
150
 
139
151
  ### checkJsonSignature
140
152
 
141
- ▸ **checkJsonSignature**(`data`, `signature`): `Promise`<`boolean`\>
153
+ ▸ **checkJsonSignature**(`data`, `signature`, `keyId`): `Promise`<`boolean`\>
142
154
 
143
155
  checks that a json object was signed by the provided signature. Unless you're
144
156
  dealing with bundled verifications, you should use `getOwnRealVerifications`
@@ -150,6 +162,7 @@ or `getSomeoneElsesRealVerifications` instead.
150
162
  | :------ | :------ |
151
163
  | `data` | `unknown` |
152
164
  | `signature` | `string` |
165
+ | `keyId` | `undefined` \| `string` |
153
166
 
154
167
  #### Returns
155
168
 
@@ -157,43 +170,41 @@ or `getSomeoneElsesRealVerifications` instead.
157
170
 
158
171
  ___
159
172
 
160
- ### cleanInstance
161
-
162
- ▸ **cleanInstance**(`instance`): `Instance`
163
-
164
- The API response from share/v2/access_application/instance/:iId
165
- returns a complicated hashed_n_id format, so you need to clean up
166
- the API response using this function as soon as possible.
173
+ ### convertInstanceToRuleInput
167
174
 
168
- We relace `hashed_n_id`s with a string `HASHED::{NId}::{AId}`
175
+ **convertInstanceToRuleInput**(`POs`, `realVers`, `getSchema`): `Promise`<`RuleData`\>
169
176
 
170
177
  #### Parameters
171
178
 
172
179
  | Name | Type |
173
180
  | :------ | :------ |
174
- | `instance` | `Instance` |
181
+ | `POs` | `ProfileObject`[] |
182
+ | `realVers` | `RealVer`[] |
183
+ | `getSchema` | (`schemaName`: `SchemaName`) => `Promise`<`Schema`\> |
175
184
 
176
185
  #### Returns
177
186
 
178
- `Instance`
187
+ `Promise`<`RuleData`\>
179
188
 
180
189
  ___
181
190
 
182
- ### convertInstanceToRuleInput
191
+ ### convertServerRelationship
183
192
 
184
- ▸ **convertInstanceToRuleInput**(`POs`, `realVers`, `getSchema`): `Promise`<`RuleData`\>
193
+ ▸ **convertServerRelationship**(`serverRelationship`): `Relationship`
194
+
195
+ Converts relationship type used by the server into the (Urn format) type used by the client
185
196
 
186
197
  #### Parameters
187
198
 
188
- | Name | Type |
189
- | :------ | :------ |
190
- | `POs` | `ProfileObject`[] |
191
- | `realVers` | `RealVer`[] |
192
- | `getSchema` | (`schemaName`: `string`) => `Promise`<`Schema`\> |
199
+ | Name | Type | Description |
200
+ | :------ | :------ | :------ |
201
+ | `serverRelationship` | [`ServerRelationship`](#serverrelationship) | relationship type used in the server |
193
202
 
194
203
  #### Returns
195
204
 
196
- `Promise`<`RuleData`\>
205
+ `Relationship`
206
+
207
+ a Relationship (client-side type) that is equivalent to the inputted serverRelationship value
197
208
 
198
209
  ___
199
210
 
@@ -225,6 +236,29 @@ as well as the `userDoc` data which is stored in the user's cognito attributes.
225
236
 
226
237
  ___
227
238
 
239
+ ### createHashedNId
240
+
241
+ ▸ **createHashedNId**(`nId`, `aId`): `NId`
242
+
243
+ the API returns hashed NIds in a separate field,
244
+ which breaks the whole app. So, we move the hashed ID into
245
+ the normal `n_id` field, and use a different format.
246
+
247
+ This was first introduced in #1048
248
+
249
+ #### Parameters
250
+
251
+ | Name | Type |
252
+ | :------ | :------ |
253
+ | `nId` | `NId` |
254
+ | `aId` | `AId` |
255
+
256
+ #### Returns
257
+
258
+ `NId`
259
+
260
+ ___
261
+
228
262
  ### decryptSharedData
229
263
 
230
264
  ▸ **decryptSharedData**(`«destructured»`): `Promise`<{ `applicationDecryptor`: `ApplicationDataEncryptorI` ; `instance`: `Instance` }\>
@@ -303,7 +337,7 @@ ___
303
337
 
304
338
  ### findSchemaLabel
305
339
 
306
- ▸ **findSchemaLabel**(`labels`): `undefined` \| `string`
340
+ ▸ **findSchemaLabel**(`labels`): `undefined` \| `SchemaName`
307
341
 
308
342
  Finds the label (on a profile object) which is the schema name
309
343
 
@@ -315,7 +349,7 @@ Finds the label (on a profile object) which is the schema name
315
349
 
316
350
  #### Returns
317
351
 
318
- `undefined` \| `string`
352
+ `undefined` \| `SchemaName`
319
353
 
320
354
  ___
321
355
 
@@ -360,7 +394,7 @@ ___
360
394
 
361
395
  ### getAADecryptor
362
396
 
363
- ▸ **getAADecryptor**(`«destructured»`): `Promise`<{ `decryptor`: `ApplicationEncryptor` ; `publicKeyNId`: `NId` }\>
397
+ ▸ **getAADecryptor**(`«destructured»`): `Promise`<{ `decryptor`: `ApplicationEncryptor` ; `publicKeyId`: `KId` }\>
364
398
 
365
399
  Fetchs the public and private keys for an Access Application, then initializes
366
400
  the [https://npm.im/@raytio/maxcryptor|Maxcryptor](https://npm.im/@raytio/maxcryptor|Maxcryptor)'s `ApplicationEncryptor`.
@@ -377,41 +411,98 @@ the [https://npm.im/@raytio/maxcryptor|Maxcryptor](https://npm.im/@raytio/maxcry
377
411
 
378
412
  #### Returns
379
413
 
380
- `Promise`<{ `decryptor`: `ApplicationEncryptor` ; `publicKeyNId`: `NId` }\>
414
+ `Promise`<{ `decryptor`: `ApplicationEncryptor` ; `publicKeyId`: `KId` }\>
381
415
 
382
416
  an `ApplicationEncryptor` and the public key of the Access Application
383
417
 
384
418
  ___
385
419
 
420
+ ### getAAPublicKey
421
+
422
+ ▸ **getAAPublicKey**(`«destructured»`): `Promise`<`PublicKeyNode`\>
423
+
424
+ Fetches the Public Key Information for an Access Application
425
+
426
+ #### Parameters
427
+
428
+ | Name | Type |
429
+ | :------ | :------ |
430
+ | `«destructured»` | `Object` |
431
+ | › `aId` | `AId` |
432
+ | › `apiToken` | `string` |
433
+ | › `apiUrl` | `string` |
434
+
435
+ #### Returns
436
+
437
+ `Promise`<`PublicKeyNode`\>
438
+
439
+ the id and Key information of the Applications Public Key
440
+
441
+ ___
442
+
443
+ ### getMissingDataForInstance
444
+
445
+ ▸ **getMissingDataForInstance**(`«destructured»`): `Promise`<`Instance`\>
446
+
447
+ the new API doesn't return the whole instance at once,
448
+ so we have to make several additional API requests.
449
+
450
+ #### Parameters
451
+
452
+ | Name | Type |
453
+ | :------ | :------ |
454
+ | `«destructured»` | `Object` |
455
+ | › `apiToken` | `string` |
456
+ | › `apiUrl` | `string` |
457
+ | › `instanceWithoutData` | `InstanceWithoutData` |
458
+
459
+ #### Returns
460
+
461
+ `Promise`<`Instance`\>
462
+
463
+ ___
464
+
386
465
  ### getNidFromUrn
387
466
 
388
- ▸ **getNidFromUrn**(`urn`): `NId`
467
+ ▸ **getNidFromUrn**<`IDType`\>(`urn`): `IDType`
389
468
 
390
469
  two overloads - if you provide undefined, you might get undefined back
391
470
 
471
+ #### Type parameters
472
+
473
+ | Name | Type |
474
+ | :------ | :------ |
475
+ | `IDType` | `NId` |
476
+
392
477
  #### Parameters
393
478
 
394
479
  | Name | Type |
395
480
  | :------ | :------ |
396
- | `urn` | \`urn:user:${string}\` \| \`urn:profile\_object:${string}\` \| \`urn:instance:${string}\` \| \`urn:schema:${string}\` \| \`urn:temp\_object:${string}\` \| \`urn:document:${string}\` |
481
+ | `urn` | `Urn` |
397
482
 
398
483
  #### Returns
399
484
 
400
- `NId`
485
+ `IDType`
401
486
 
402
- ▸ **getNidFromUrn**(`urn`): `undefined` \| `NId`
487
+ ▸ **getNidFromUrn**<`IDType`\>(`urn`): `undefined` \| `IDType`
403
488
 
404
489
  two overloads - if you provide undefined, you might get undefined back
405
490
 
491
+ #### Type parameters
492
+
493
+ | Name | Type |
494
+ | :------ | :------ |
495
+ | `IDType` | `NId` |
496
+
406
497
  #### Parameters
407
498
 
408
499
  | Name | Type |
409
500
  | :------ | :------ |
410
- | `urn` | `undefined` \| \`urn:user:${string}\` \| \`urn:profile\_object:${string}\` \| \`urn:instance:${string}\` \| \`urn:schema:${string}\` \| \`urn:temp\_object:${string}\` \| \`urn:document:${string}\` |
501
+ | `urn` | `undefined` \| `Urn` |
411
502
 
412
503
  #### Returns
413
504
 
414
- `undefined` \| `NId`
505
+ `undefined` \| `IDType`
415
506
 
416
507
  ___
417
508
 
@@ -645,7 +736,7 @@ ___
645
736
 
646
737
  ### someEncrypted
647
738
 
648
- ▸ **someEncrypted**<`T`, `K`\>(`...args`): `number`
739
+ ▸ **someEncrypted**<`T`\>(`...args`): `number`
649
740
 
650
741
  Given a profile object's properties, returns the number
651
742
  of properties that are encryted.
@@ -655,7 +746,6 @@ of properties that are encryted.
655
746
  | Name | Type |
656
747
  | :------ | :------ |
657
748
  | `T` | extends `object` |
658
- | `K` | extends `string` \| `number` \| `symbol` |
659
749
 
660
750
  #### Parameters
661
751
 
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const fetchKeysForSubmission_1 = require("../fetchKeysForSubmission");
4
+ vi.spyOn(global, "fetch").mockResolvedValueOnce({
5
+ json: async () => [
6
+ { aain_id: "n1", field_name: "f1", key_data: 1 },
7
+ { aain_id: "n1", field_name: "f2", key_data: 1 },
8
+ { aain_id: "n2", field_name: "f2", key_data: 3 },
9
+ { aain_id: "n3", field_name: "f1", key_data: 4 },
10
+ { aain_id: "n3", field_name: "f2", key_data: 5 },
11
+ { aain_id: "n3", field_name: "f3", key_data: 6 },
12
+ ],
13
+ });
14
+ describe("fetchKeysForSubmission", () => {
15
+ it("converts the hashed_n_id format into n_id", async () => {
16
+ expect(await (0, fetchKeysForSubmission_1.fetchKeysForSubmission)({
17
+ iId: "i1",
18
+ apiToken: "apiToken",
19
+ apiUrl: "apiUrl",
20
+ })).toStrictEqual({
21
+ n1: { f1: 1, f2: 1 },
22
+ n2: { f2: 3 },
23
+ n3: { f1: 4, f2: 5, f3: 6 },
24
+ });
25
+ expect(fetch).toHaveBeenCalledTimes(1);
26
+ expect(fetch).toHaveBeenNthCalledWith(1, "apiUrl/db/v1/dsm_access_application_instance_node_keys?aai_id=eq.i1", { headers: { Authorization: "Bearer apiToken" } });
27
+ });
28
+ });
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const fetchPOsOrAOsForSubmission_1 = require("../fetchPOsOrAOsForSubmission");
4
+ vi.spyOn(global, "fetch").mockResolvedValueOnce({
5
+ json: async () => [
6
+ { n_id: "n1", properties: { a: 1 }, aa_id: "a1", aai_id: "i1" },
7
+ { hashed_n_id: "n2", properties: { b: 2 }, aa_id: "a2", aai_id: "i2" },
8
+ ],
9
+ });
10
+ describe("fetchPOsOrAOsForSubmission", () => {
11
+ it("converts the hashed_n_id format into n_id", async () => {
12
+ expect(await (0, fetchPOsOrAOsForSubmission_1.fetchPOsOrAOsForSubmission)({ iId: "i1", apiToken: "apiToken", apiUrl: "apiUrl" }, "AOs")).toStrictEqual([
13
+ { n_id: "n1", properties: { a: 1 }, isFromSubmission: ["a1", "i1"] },
14
+ {
15
+ n_id: "HASHED::n2::a2",
16
+ properties: { b: 2 },
17
+ isFromSubmission: ["a2", "i2"],
18
+ },
19
+ ]);
20
+ expect(fetch).toHaveBeenCalledTimes(1);
21
+ expect(fetch).toHaveBeenNthCalledWith(1, "apiUrl/db/v1/dsm_access_application_instance_application_objects?aai_id=eq.i1", { headers: { Authorization: "Bearer apiToken" } });
22
+ });
23
+ });
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const fetchRelationshipsForSubmission_1 = require("../fetchRelationshipsForSubmission");
4
+ vi.spyOn(global, "fetch").mockResolvedValueOnce({
5
+ json: async () => [
6
+ { id: "p1", from_id: "n1", to_id: "n2" },
7
+ { id: "p1", from_id: "n1", to_i_id: "n2" },
8
+ ],
9
+ });
10
+ describe("fetchRelationshipsForSubmission", () => {
11
+ it("converts the hashed_n_id format into n_id", async () => {
12
+ expect(await (0, fetchRelationshipsForSubmission_1.fetchRelationshipsForSubmission)({
13
+ iId: "i1",
14
+ apiToken: "apiToken",
15
+ apiUrl: "apiUrl",
16
+ })).toStrictEqual([
17
+ {
18
+ p_id: "p1",
19
+ start: "urn:profile_object:n1",
20
+ end: "urn:profile_object:n2",
21
+ },
22
+ { p_id: "p1", start: "urn:profile_object:n1", end: "urn:instance:n2" },
23
+ ]);
24
+ expect(fetch).toHaveBeenCalledTimes(1);
25
+ expect(fetch).toHaveBeenNthCalledWith(1, "apiUrl/db/v1/dsm_access_application_instance_node_relationships?aai_id=eq.i1", { headers: { Authorization: "Bearer apiToken" } });
26
+ });
27
+ });
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const getMissingDataForInstance_1 = require("../getMissingDataForInstance");
4
+ vi.mock("../fetchKeysForSubmission", () => ({
5
+ fetchKeysForSubmission: async () => [3, 4],
6
+ }));
7
+ vi.mock("../fetchPOsOrAOsForSubmission", () => ({
8
+ fetchPOsOrAOsForSubmission: async () => [1, 2],
9
+ }));
10
+ vi.mock("../fetchRelationshipsForSubmission", () => ({
11
+ fetchRelationshipsForSubmission: async () => [5, 6],
12
+ }));
13
+ describe("getMissingDataForInstance", () => {
14
+ it("converts the hashed_n_id format into n_id", async () => {
15
+ expect(await (0, getMissingDataForInstance_1.getMissingDataForInstance)({
16
+ instanceWithoutData: {
17
+ id: "i1",
18
+ confirmation_code: "ABC123",
19
+ },
20
+ apiToken: "apiToken",
21
+ apiUrl: "apiUrl",
22
+ })).toStrictEqual({
23
+ confirmation_code: "ABC123",
24
+ id: "i1",
25
+ keys: [3, 4],
26
+ profile_objects: [1, 2, 1, 2],
27
+ relationships: [5, 6],
28
+ });
29
+ });
30
+ });
@@ -0,0 +1,12 @@
1
+ import type { IId, NId2 } from "@raytio/types";
2
+ export declare function fetchKeysForSubmission({ apiToken, apiUrl, iId, }: {
3
+ apiUrl: string;
4
+ apiToken: string;
5
+ iId: IId;
6
+ }): Promise<{
7
+ [nId2: NId2]: {
8
+ [fieldName: string]: {
9
+ data: string;
10
+ };
11
+ };
12
+ }>;
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.fetchKeysForSubmission = void 0;
4
+ const util_1 = require("../../util");
5
+ async function fetchKeysForSubmission({ apiToken, apiUrl, iId, }) {
6
+ var _a;
7
+ const keys = await fetch(`${apiUrl}/db/v1/dsm_access_application_instance_node_keys?aai_id=eq.${iId}`, { headers: { Authorization: `Bearer ${apiToken}` } }).then(util_1.handleResponse);
8
+ const keysObject = {};
9
+ for (const key of keys) {
10
+ /* eslint-disable fp/no-mutation -- much more logical written like this */
11
+ keysObject[_a = key.aain_id] || (keysObject[_a] = {});
12
+ keysObject[key.aain_id][key.field_name] = key.key_data;
13
+ }
14
+ return keysObject;
15
+ }
16
+ exports.fetchKeysForSubmission = fetchKeysForSubmission;
@@ -0,0 +1,6 @@
1
+ import type { IId, ProfileObject } from "@raytio/types";
2
+ export declare function fetchPOsOrAOsForSubmission({ iId, apiUrl, apiToken }: {
3
+ iId: IId;
4
+ apiUrl: string;
5
+ apiToken: string;
6
+ }, type: "POs" | "AOs"): Promise<ProfileObject[]>;
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.fetchPOsOrAOsForSubmission = void 0;
4
+ const ramda_1 = require("ramda");
5
+ const util_1 = require("../../util");
6
+ const general_1 = require("../../general");
7
+ const API_URLS = {
8
+ POs: "dsm_access_application_instance_nodes",
9
+ AOs: "dsm_access_application_instance_application_objects",
10
+ };
11
+ async function fetchPOsOrAOsForSubmission({ iId, apiUrl, apiToken }, type) {
12
+ const POsforAA = await fetch(`${apiUrl}/db/v1/${API_URLS[type]}?aai_id=eq.${iId}`, { headers: { Authorization: `Bearer ${apiToken}` } }).then(util_1.handleResponse);
13
+ /**
14
+ * the backend finally includes the aId and iId properties,
15
+ * but we already have our own system for accessing this data
16
+ * since we've needed it for the past few years.
17
+ *
18
+ * additionally, we relace the weird `hashed_n_id` format with
19
+ * the result from {@link createHashedNId}, because that's what we've
20
+ * always done.
21
+ */
22
+ const cleanedPOs = POsforAA.map((PO) => (Object.assign(Object.assign({}, (0, ramda_1.omit)(["aa_id", "aai_id", "hashed_n_id"], PO)), { n_id: PO.n_id || (0, general_1.createHashedNId)(PO.hashed_n_id, PO.aa_id), isFromSubmission: [PO.aa_id, PO.aai_id] }))).filter(PO => PO.properties !== null && PO.properties !== undefined);
23
+ return cleanedPOs;
24
+ }
25
+ exports.fetchPOsOrAOsForSubmission = fetchPOsOrAOsForSubmission;
@@ -0,0 +1,6 @@
1
+ import type { IId, Relationship } from "@raytio/types";
2
+ export declare function fetchRelationshipsForSubmission({ iId, apiUrl, apiToken, }: {
3
+ iId: IId;
4
+ apiUrl: string;
5
+ apiToken: string;
6
+ }): Promise<Relationship[]>;
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.fetchRelationshipsForSubmission = void 0;
4
+ const util_1 = require("../../util");
5
+ const legacy_1 = require("./legacy");
6
+ async function fetchRelationshipsForSubmission({ iId, apiUrl, apiToken, }) {
7
+ const relationships = await fetch(`${apiUrl}/db/v1/dsm_access_application_instance_node_relationships?aai_id=eq.${iId}`, { headers: { Authorization: `Bearer ${apiToken}` } }).then(util_1.handleResponse);
8
+ return relationships.map(legacy_1.convertServerRelationship);
9
+ }
10
+ exports.fetchRelationshipsForSubmission = fetchRelationshipsForSubmission;
@@ -0,0 +1,10 @@
1
+ import type { Instance, InstanceWithoutData } from "@raytio/types";
2
+ /**
3
+ * the new API doesn't return the whole instance at once,
4
+ * so we have to make several additional API requests.
5
+ */
6
+ export declare const getMissingDataForInstance: ({ apiToken, apiUrl, instanceWithoutData, }: {
7
+ apiToken: string;
8
+ apiUrl: string;
9
+ instanceWithoutData: InstanceWithoutData;
10
+ }) => Promise<Instance>;
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getMissingDataForInstance = void 0;
4
+ const fetchKeysForSubmission_1 = require("./fetchKeysForSubmission");
5
+ const fetchPOsOrAOsForSubmission_1 = require("./fetchPOsOrAOsForSubmission");
6
+ const fetchRelationshipsForSubmission_1 = require("./fetchRelationshipsForSubmission");
7
+ /**
8
+ * the new API doesn't return the whole instance at once,
9
+ * so we have to make several additional API requests.
10
+ */
11
+ const getMissingDataForInstance = async ({ apiToken, apiUrl, instanceWithoutData, }) => {
12
+ const options = {
13
+ apiToken,
14
+ apiUrl,
15
+ iId: instanceWithoutData.id,
16
+ };
17
+ // these 4 can be requested in parallel. Ideally the backend
18
+ // would do this work for us like the old API used to.
19
+ const [POs, AOs, relationships, keys] = await Promise.all([
20
+ (0, fetchPOsOrAOsForSubmission_1.fetchPOsOrAOsForSubmission)(options, "POs"),
21
+ (0, fetchPOsOrAOsForSubmission_1.fetchPOsOrAOsForSubmission)(options, "AOs"),
22
+ (0, fetchRelationshipsForSubmission_1.fetchRelationshipsForSubmission)(options),
23
+ (0, fetchKeysForSubmission_1.fetchKeysForSubmission)(options),
24
+ ]);
25
+ return Object.assign(Object.assign({}, instanceWithoutData), { profile_objects: [...POs, ...AOs], relationships,
26
+ keys });
27
+ };
28
+ exports.getMissingDataForInstance = getMissingDataForInstance;
@@ -0,0 +1,2 @@
1
+ export * from "./legacy";
2
+ export * from "./getMissingDataForInstance";
@@ -0,0 +1,18 @@
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("./legacy"), exports);
18
+ __exportStar(require("./getMissingDataForInstance"), exports);
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const ramda_1 = require("ramda");
4
+ const convertRelationships_1 = require("../convertRelationships");
5
+ const relationship = {
6
+ p_id: "PId",
7
+ start_date: "",
8
+ end_date: "",
9
+ start: "urn:profile_object:1",
10
+ end: "urn:profile_object:2",
11
+ type: "IS_RELATED_TO",
12
+ active: true,
13
+ properties: undefined,
14
+ };
15
+ const serverRelationship = {
16
+ id: "PId",
17
+ start_date: "",
18
+ end_date: "",
19
+ active: true,
20
+ from_id: `1`,
21
+ to_id: `2`,
22
+ to_i_id: null,
23
+ type: "IS_RELATED_TO",
24
+ properties: undefined,
25
+ };
26
+ describe("convertServerRelationship", () => {
27
+ it("Converts fields in PO relationship", () => {
28
+ expect((0, convertRelationships_1.convertServerRelationship)(serverRelationship)).toStrictEqual(relationship);
29
+ });
30
+ it("Converts fields in instance/crust relationship", () => {
31
+ // Same Ids with Instance and PO to_ids swapped
32
+ const instanceServerRelationship = Object.assign(Object.assign({}, (0, ramda_1.omit)(["to_id", "to_i_id"], serverRelationship)), { to_i_id: "2" });
33
+ // Replacing Urn prepend text
34
+ const instanceRelationship = Object.assign(Object.assign({}, (0, ramda_1.omit)(["end"], relationship)), { end: "urn:instance:2" });
35
+ expect((0, convertRelationships_1.convertServerRelationship)(instanceServerRelationship)).toStrictEqual(instanceRelationship);
36
+ });
37
+ });
@@ -0,0 +1,16 @@
1
+ import type { IId, NId, PId, Relationship } from "@raytio/types";
2
+ /** This type represents the relationship format returned by the Postgres API */
3
+ export type ServerRelationship = Omit<Relationship, "p_id" | "start" | "end"> & {
4
+ id: PId;
5
+ from_id: NId;
6
+ } & ({
7
+ to_id: NId;
8
+ } | {
9
+ to_i_id: IId;
10
+ });
11
+ /**
12
+ * Converts relationship type used by the server into the (Urn format) type used by the client
13
+ * @param serverRelationship relationship type used in the server
14
+ * @returns a Relationship (client-side type) that is equivalent to the inputted serverRelationship value
15
+ */
16
+ export declare function convertServerRelationship(serverRelationship: ServerRelationship): Relationship;