@raytio/core 11.0.0 → 11.1.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 (43) hide show
  1. package/CHANGELOG.md +4 -0
  2. package/README.md +98 -0
  3. package/dist/schema/expandSchema/__tests__/addLoadingTimes.test.d.ts +1 -0
  4. package/dist/schema/expandSchema/__tests__/addLoadingTimes.test.js +24 -0
  5. package/dist/schema/expandSchema/__tests__/expandSchema.test.d.ts +1 -0
  6. package/dist/schema/expandSchema/__tests__/expandSchema.test.js +95 -0
  7. package/dist/schema/expandSchema/__tests__/i18n.test.d.ts +1 -0
  8. package/dist/schema/expandSchema/__tests__/i18n.test.js +32 -0
  9. package/dist/schema/expandSchema/__tests__/maybeUseI18n.test.d.ts +1 -0
  10. package/dist/schema/expandSchema/__tests__/maybeUseI18n.test.js +98 -0
  11. package/dist/schema/expandSchema/__tests__/processSchema.test.d.ts +1 -0
  12. package/dist/schema/expandSchema/__tests__/processSchema.test.js +326 -0
  13. package/dist/schema/expandSchema/__tests__/sortSchemaProperties.test.d.ts +1 -0
  14. package/dist/schema/expandSchema/__tests__/sortSchemaProperties.test.js +182 -0
  15. package/dist/schema/expandSchema/__tests__/util.test.d.ts +1 -0
  16. package/dist/schema/expandSchema/__tests__/util.test.js +19 -0
  17. package/dist/schema/expandSchema/addLoadingTimes.d.ts +2 -0
  18. package/dist/schema/expandSchema/addLoadingTimes.js +12 -0
  19. package/dist/schema/expandSchema/constants.d.ts +2 -0
  20. package/dist/schema/expandSchema/constants.js +11 -0
  21. package/dist/schema/expandSchema/expandSchema.d.ts +7 -0
  22. package/dist/schema/expandSchema/expandSchema.js +19 -0
  23. package/dist/schema/expandSchema/i18n.d.ts +5 -0
  24. package/dist/schema/expandSchema/i18n.js +20 -0
  25. package/dist/schema/expandSchema/index.d.ts +3 -0
  26. package/dist/schema/expandSchema/index.js +21 -0
  27. package/dist/schema/expandSchema/maybeUseI18n.d.ts +2 -0
  28. package/dist/schema/expandSchema/maybeUseI18n.js +40 -0
  29. package/dist/schema/expandSchema/processSchema.d.ts +4 -0
  30. package/dist/schema/expandSchema/processSchema.js +94 -0
  31. package/dist/schema/expandSchema/removePrivateFields.d.ts +119 -0
  32. package/dist/schema/expandSchema/removePrivateFields.js +15 -0
  33. package/dist/schema/expandSchema/sortSchemaProperties.d.ts +21 -0
  34. package/dist/schema/expandSchema/sortSchemaProperties.js +40 -0
  35. package/dist/schema/expandSchema/unwrapSchema.d.ts +6 -0
  36. package/dist/schema/expandSchema/unwrapSchema.js +7 -0
  37. package/dist/schema/expandSchema/util.d.ts +6 -0
  38. package/dist/schema/expandSchema/util.js +15 -0
  39. package/dist/schema/index.d.ts +1 -0
  40. package/dist/schema/index.js +1 -0
  41. package/dist/verifications/safeHarbour.d.ts +1 -1
  42. package/dist/verifications/safeHarbour.js +4 -11
  43. package/package.json +2 -2
package/CHANGELOG.md CHANGED
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## 11.1.0 (2022-04-20)
11
+
12
+ - Move `expandSchema` into core
13
+
10
14
  ## 11.0.0 (2022-04-20)
11
15
 
12
16
  - 💥 BREAKING CHANGE: Changed the input arguments of `convertInstanceToRuleInput` to only require a list of `ProfileObject`s, instead of the whole `Instance` object.
package/README.md CHANGED
@@ -32,9 +32,12 @@ If you wish to use `@raytio/core` directly, an example of configuring polyfills
32
32
  - [createAA](#createaa)
33
33
  - [decryptSharedData](#decryptshareddata)
34
34
  - [evaluateRule](#evaluaterule)
35
+ - [expandSchema](#expandschema)
35
36
  - [findSchemaLabel](#findschemalabel)
37
+ - [findSuitableLocale](#findsuitablelocale)
36
38
  - [fromCognitoAttributes](#fromcognitoattributes)
37
39
  - [getAADecryptor](#getaadecryptor)
40
+ - [getNidFromUrn](#getnidfromurn)
38
41
  - [getOwnRealVerifications](#getownrealverifications)
39
42
  - [getPOVerification](#getpoverification)
40
43
  - [getSomeoneElsesRealVerifications](#getsomeoneelsesrealverifications)
@@ -46,6 +49,7 @@ If you wish to use `@raytio/core` directly, an example of configuring polyfills
46
49
  - [isScoreResultValid](#isscoreresultvalid)
47
50
  - [repairDate](#repairdate)
48
51
  - [someEncrypted](#someencrypted)
52
+ - [sortSchemaProperties](#sortschemaproperties)
49
53
  - [toCognitoAttributes](#tocognitoattributes)
50
54
 
51
55
  ## Type Aliases
@@ -275,6 +279,28 @@ evaluates an individual rule, normally you should use [calculateScore](#calculat
275
279
 
276
280
  ___
277
281
 
282
+ ### expandSchema
283
+
284
+ ▸ **expandSchema**(`wrappedSchema`, `allUnexpandedSchemas`, `userLocales`): `Schema`
285
+
286
+ ❣️ This is the main function to transform a schema from
287
+ the JSON that the API returns, into a `Schema` object that's useful
288
+ to the client.
289
+
290
+ #### Parameters
291
+
292
+ | Name | Type |
293
+ | :------ | :------ |
294
+ | `wrappedSchema` | `WrappedSchema` |
295
+ | `allUnexpandedSchemas` | `WrappedSchema`[] |
296
+ | `userLocales` | readonly `string`[] |
297
+
298
+ #### Returns
299
+
300
+ `Schema`
301
+
302
+ ___
303
+
278
304
  ### findSchemaLabel
279
305
 
280
306
  ▸ **findSchemaLabel**(`labels`): `undefined` \| `string`
@@ -293,6 +319,26 @@ Finds the label (on a profile object) which is the schema name
293
319
 
294
320
  ___
295
321
 
322
+ ### findSuitableLocale
323
+
324
+ ▸ **findSuitableLocale**(`options`, `langs`): `undefined` \| `string`
325
+
326
+ Selects the most suitable locale to use from a list of options.
327
+ Returns undefined if there is no language that the user speaks
328
+
329
+ #### Parameters
330
+
331
+ | Name | Type |
332
+ | :------ | :------ |
333
+ | `options` | `string`[] |
334
+ | `langs` | readonly `string`[] |
335
+
336
+ #### Returns
337
+
338
+ `undefined` \| `string`
339
+
340
+ ___
341
+
296
342
  ### fromCognitoAttributes
297
343
 
298
344
  ▸ **fromCognitoAttributes**(`attributes`): `UserDoc`
@@ -337,6 +383,38 @@ an `ApplicationEncryptor` and the public key of the Access Application
337
383
 
338
384
  ___
339
385
 
386
+ ### getNidFromUrn
387
+
388
+ ▸ **getNidFromUrn**(`urn`): `NId`
389
+
390
+ two overloads - if you provide undefined, you might get undefined back
391
+
392
+ #### Parameters
393
+
394
+ | Name | Type |
395
+ | :------ | :------ |
396
+ | `urn` | \`urn:user:${string}\` \| \`urn:profile\_object:${string}\` \| \`urn:instance:${string}\` \| \`urn:schema:${string}\` \| \`urn:temp\_object:${string}\` \| \`urn:document:${string}\` |
397
+
398
+ #### Returns
399
+
400
+ `NId`
401
+
402
+ ▸ **getNidFromUrn**(`urn`): `undefined` \| `NId`
403
+
404
+ two overloads - if you provide undefined, you might get undefined back
405
+
406
+ #### Parameters
407
+
408
+ | Name | Type |
409
+ | :------ | :------ |
410
+ | `urn` | `undefined` \| \`urn:user:${string}\` \| \`urn:profile\_object:${string}\` \| \`urn:instance:${string}\` \| \`urn:schema:${string}\` \| \`urn:temp\_object:${string}\` \| \`urn:document:${string}\` |
411
+
412
+ #### Returns
413
+
414
+ `undefined` \| `NId`
415
+
416
+ ___
417
+
340
418
  ### getOwnRealVerifications
341
419
 
342
420
  ▸ **getOwnRealVerifications**(`«destructured»`): `Promise`<`RealVer`[]\>
@@ -591,6 +669,26 @@ of properties that are encryted.
591
669
 
592
670
  ___
593
671
 
672
+ ### sortSchemaProperties
673
+
674
+ ▸ **sortSchemaProperties**(`properties`): `Section`[]
675
+
676
+ Schema properties are an object, so they need to be converted into an
677
+ array, grouped by the group tag, and then sorted based on the `priority`
678
+ attribute within their group.
679
+
680
+ #### Parameters
681
+
682
+ | Name | Type |
683
+ | :------ | :------ |
684
+ | `properties` | `Record`<`string`, `SchemaField`\> |
685
+
686
+ #### Returns
687
+
688
+ `Section`[]
689
+
690
+ ___
691
+
594
692
  ### toCognitoAttributes
595
693
 
596
694
  ▸ **toCognitoAttributes**(`userDoc`): `Object`
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const addLoadingTimes_1 = require("../addLoadingTimes");
4
+ describe("addLoadingTimes", () => {
5
+ it("parses loading times from the schema tags", () => {
6
+ const schema = {
7
+ name: "hi",
8
+ tags: [
9
+ "a:b",
10
+ "c",
11
+ "time:action1:20",
12
+ "time:action2:5",
13
+ "time:action3:0",
14
+ "time:action4:not a number",
15
+ ],
16
+ };
17
+ expect((0, addLoadingTimes_1.addLoadingTimes)(schema)).toStrictEqual(Object.assign(Object.assign({}, schema), { timing: {
18
+ action1: 20,
19
+ action2: 5,
20
+ action3: 0,
21
+ action4: NaN,
22
+ } }));
23
+ });
24
+ });
@@ -0,0 +1,95 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const expandSchema_1 = require("../expandSchema");
4
+ const fromAPI = [
5
+ {
6
+ name: "ps_Vessel",
7
+ start_date: "1970-01-01T00:00:00+00:00",
8
+ end_date: "2222-12-31T23:59:59+00:00",
9
+ active: true,
10
+ version_current: true,
11
+ version: "0.0.7",
12
+ type: "ps",
13
+ schema: {
14
+ $id: "https://api.rayt.io/graph/v1/schema/ps/ps_Vessel",
15
+ $schema: "http://json-schema.org/draft-07/schema#",
16
+ title: "Vessel/Boat",
17
+ description: "A schema that describes a maritime vessel",
18
+ i18n: {
19
+ "mi-NZ": {
20
+ $schema: { title: "Waka" },
21
+ vesselName: { title: "nga waka ingoa" },
22
+ },
23
+ },
24
+ tags: ["time:action1:20"],
25
+ required: ["vesselName"],
26
+ properties: {
27
+ manufacture_year: {
28
+ allOf: [
29
+ { $ref: "#/definitions/ss_birth_year_integer" },
30
+ { tags: ["aaa"], priority: 700 },
31
+ ],
32
+ },
33
+ vesselName: { type: "string", title: "Vessel Name", priority: 1000 },
34
+ },
35
+ definitions: {
36
+ ss_birth_year_integer: { $ref: "urn:schema:ss_birth_year_integer" },
37
+ },
38
+ },
39
+ },
40
+ {
41
+ name: "ss_birth_year_integer",
42
+ version: "0.0.1",
43
+ schema: {
44
+ $id: "this field often has typos. in this case its blatantly wrong",
45
+ $schema: "https://whatever",
46
+ maximum: 2022,
47
+ minimum: 1900,
48
+ tags: ["type:globally_unique_field"],
49
+ title: "Year of birth",
50
+ },
51
+ },
52
+ ];
53
+ describe("expandSchema", () => {
54
+ it("can do the entire expansion process", () => {
55
+ expect((0, expandSchema_1.expandSchema)(fromAPI[0], fromAPI, ["lb-LU"])).toStrictEqual({
56
+ wasExpandedByClient: true,
57
+ // $schema was removed
58
+ name: "ps_Vessel",
59
+ title: "Vessel/Boat",
60
+ description: "A schema that describes a maritime vessel",
61
+ start_date: "1970-01-01T00:00:00+00:00",
62
+ end_date: "2222-12-31T23:59:59+00:00",
63
+ version: "0.0.7",
64
+ i18n: fromAPI[0].schema.i18n,
65
+ isProfileSchema: true,
66
+ isSpSchema: false,
67
+ properties: {
68
+ manufacture_year: {
69
+ // $id was removed
70
+ $prop: "manufacture_year",
71
+ $ref: "ss_birth_year_integer",
72
+ maximum: 2022,
73
+ minimum: 1900,
74
+ title: "Year of birth",
75
+ // it merged the allOf into a single object
76
+ priority: 700,
77
+ tags: ["aaa", "type:globally_unique_field"],
78
+ },
79
+ vesselName: {
80
+ $prop: "vesselName",
81
+ // no $ref attribute
82
+ priority: 1000,
83
+ title: "Vessel Name",
84
+ type: "string",
85
+ },
86
+ },
87
+ required: ["vesselName"],
88
+ tags: ["time:action1:20"],
89
+ timing: {
90
+ action1: 20,
91
+ },
92
+ verified_fields: undefined,
93
+ });
94
+ });
95
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const i18n_1 = require("../i18n");
4
+ const DEFAULT_LOCALES = ["de-DE"]; // match what the client does in jest.setup.ts
5
+ describe("getAllLocales", () => {
6
+ it("correctly expands languages with a country-componentand lowercases them", () => {
7
+ const expanded = (0, i18n_1.getAllLocales)([
8
+ "en-NZ",
9
+ "de-CH",
10
+ "be",
11
+ "ar-001",
12
+ "az-Cyrl-AZ",
13
+ ]);
14
+ expect(expanded).toStrictEqual([
15
+ "en-nz",
16
+ "en",
17
+ "de-ch",
18
+ "de",
19
+ "be",
20
+ "ar-001",
21
+ "ar",
22
+ "az-cyrl-az",
23
+ "az",
24
+ ]);
25
+ });
26
+ });
27
+ describe("findSuitableLocale", () => {
28
+ it("picks the most appropriate option", () => {
29
+ // the users's locale is `de-DE`, so it picked the most appropriate one from the options: `de-AT`
30
+ expect((0, i18n_1.findSuitableLocale)(["en-029", "de-AT", "fr-FR"], DEFAULT_LOCALES)).toBe("de-AT");
31
+ });
32
+ });
@@ -0,0 +1,98 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const maybeUseI18n_1 = require("../maybeUseI18n");
4
+ const DEFAULT_LOCALES = ["de-DE"]; // match what the client does in jest.setup.ts
5
+ describe("maybeUseI18n", () => {
6
+ it("correctly applies schema-wise overrides", () => {
7
+ const originalSchema = {
8
+ schema_group: "old_schemas",
9
+ title: "Default title",
10
+ description: "default desc",
11
+ i18n: {
12
+ "de-de": {
13
+ $schema: {
14
+ title: "der Name dieses Schema",
15
+ description: "eine Beschreibung in Deutsch",
16
+ },
17
+ old_schemas: {
18
+ title_plural: "All the old sChema",
19
+ },
20
+ },
21
+ },
22
+ };
23
+ const translatedSchema = (0, maybeUseI18n_1.maybeUseI18n)(originalSchema, DEFAULT_LOCALES);
24
+ expect(translatedSchema).toStrictEqual({
25
+ schema_group: "old_schemas",
26
+ group_title: "All the old sChema",
27
+ title: "der Name dieses Schema",
28
+ description: "eine Beschreibung in Deutsch",
29
+ clientLocale: "de-de",
30
+ groupNames: {},
31
+ properties: {},
32
+ i18n: originalSchema.i18n,
33
+ });
34
+ });
35
+ it("corretly creates a groupNames property with the correct locale", () => {
36
+ const originalSchema = {
37
+ i18n: {
38
+ "en-nz": {
39
+ "group:DoB": { title: "Your date of birth" },
40
+ },
41
+ "fr-fr": {
42
+ "group:DoB": { title: "anniversaire" },
43
+ },
44
+ "de-de": {
45
+ "group:DoB": { title: "dein Geburtstag" },
46
+ },
47
+ },
48
+ properties: {
49
+ year_of_birth: {
50
+ tags: ["group:DoB"],
51
+ },
52
+ },
53
+ };
54
+ const translatedSchema = (0, maybeUseI18n_1.maybeUseI18n)(originalSchema, DEFAULT_LOCALES);
55
+ expect(translatedSchema).toStrictEqual({
56
+ clientLocale: "de-de",
57
+ groupNames: {
58
+ DoB: "dein Geburtstag",
59
+ },
60
+ group_title: undefined,
61
+ properties: originalSchema.properties,
62
+ i18n: originalSchema.i18n,
63
+ });
64
+ });
65
+ it("correctly applies overrides to certain schema fields", () => {
66
+ const originalSchema = {
67
+ i18n: {
68
+ "en-029": {
69
+ year_of_birth: { title: "Your year of birth" },
70
+ },
71
+ de: {
72
+ // also testing whether it utilizes "de" even tho locale is "de-DE"
73
+ year_of_birth: { title: "dein Geburtsjahr", randomProp: 1 },
74
+ },
75
+ },
76
+ properties: {
77
+ year_of_birth: {
78
+ title: "Default field title",
79
+ tags: ["group:DoB"],
80
+ },
81
+ },
82
+ };
83
+ const translatedSchema = (0, maybeUseI18n_1.maybeUseI18n)(originalSchema, DEFAULT_LOCALES);
84
+ expect(translatedSchema).toStrictEqual({
85
+ clientLocale: "de",
86
+ groupNames: {},
87
+ group_title: undefined,
88
+ properties: {
89
+ year_of_birth: {
90
+ title: "dein Geburtsjahr",
91
+ randomProp: 1,
92
+ tags: ["group:DoB"],
93
+ },
94
+ },
95
+ i18n: originalSchema.i18n,
96
+ });
97
+ });
98
+ });