@oneblink/sdk-core 8.2.0-beta.1 → 8.2.0-beta.2

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.
@@ -2,6 +2,18 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.parseUserProfile = parseUserProfile;
4
4
  exports.getUserFriendlyName = getUserFriendlyName;
5
+ function parseBoolean(value) {
6
+ return typeof value === 'boolean' ? value : undefined;
7
+ }
8
+ function parseString(value) {
9
+ return typeof value === 'string' ? value : undefined;
10
+ }
11
+ function parseAddress(address) {
12
+ if (typeof address === 'object' && address && 'formatted' in address) {
13
+ return parseString(address.formatted);
14
+ }
15
+ return parseString(address);
16
+ }
5
17
  /**
6
18
  * Parse a User Profile based on a JWT payload. Will return `undefined` if not a
7
19
  * valid JWT payload
@@ -21,7 +33,7 @@ exports.getUserFriendlyName = getUserFriendlyName;
21
33
  * @param data An object containing all parameters to be passed to the function
22
34
  */
23
35
  function parseUserProfile(data) {
24
- var _a, _b, _c;
36
+ var _a, _b, _c, _d;
25
37
  if (typeof data !== 'object' || !data) {
26
38
  return;
27
39
  }
@@ -34,83 +46,39 @@ function parseUserProfile(data) {
34
46
  providerType: 'Cognito',
35
47
  providerUserId: jwtPayload.sub,
36
48
  userId: jwtPayload.sub,
37
- email: typeof jwtPayload.email === 'string' ? jwtPayload.email : undefined,
38
- emailVerified: typeof jwtPayload.email_verified === 'boolean'
39
- ? jwtPayload.email_verified
40
- : undefined,
41
- firstName: typeof jwtPayload.given_name === 'string'
42
- ? jwtPayload.given_name
43
- : undefined,
44
- middleName: typeof jwtPayload.middle_name === 'string'
45
- ? jwtPayload.middle_name
46
- : undefined,
47
- lastName: typeof jwtPayload.family_name === 'string'
48
- ? jwtPayload.family_name
49
- : undefined,
50
- fullName: typeof jwtPayload.name === 'string' ? jwtPayload.name : undefined,
51
- picture: typeof jwtPayload.picture === 'string' ? jwtPayload.picture : undefined,
52
- address: typeof jwtPayload.address === 'string' ? jwtPayload.address : undefined,
53
- role: typeof jwtPayload['custom:role'] === 'string'
54
- ? jwtPayload['custom:role']
55
- : undefined,
56
- username: typeof jwtPayload.email === 'string' ? jwtPayload.email : jwtPayload.sub,
49
+ email: parseString(jwtPayload.email),
50
+ emailVerified: parseBoolean(jwtPayload.email_verified),
51
+ firstName: parseString(jwtPayload.given_name),
52
+ middleName: parseString(jwtPayload.middle_name),
53
+ lastName: parseString(jwtPayload.family_name),
54
+ fullName: parseString(jwtPayload.name),
55
+ picture: parseString(jwtPayload.picture),
56
+ address: parseAddress(jwtPayload.address),
57
+ role: parseString(jwtPayload['custom:role']),
58
+ username: parseString(jwtPayload.email) || jwtPayload.sub,
57
59
  supervisor: {
58
- fullName: typeof jwtPayload['custom:supervisor_name'] === 'string'
59
- ? jwtPayload['custom:supervisor_name']
60
- : undefined,
61
- email: typeof jwtPayload['custom:supervisor_email'] === 'string'
62
- ? jwtPayload['custom:supervisor_email']
63
- : undefined,
64
- providerUserId: typeof jwtPayload['custom:supervisor_user_id'] === 'string'
65
- ? jwtPayload['custom:supervisor_user_id']
66
- : undefined,
60
+ fullName: parseString(jwtPayload['custom:supervisor_name']),
61
+ email: parseString(jwtPayload['custom:supervisor_email']),
62
+ providerUserId: parseString(jwtPayload['custom:supervisor_user_id']),
67
63
  },
68
- phoneNumber: typeof jwtPayload['custom:phone_number'] === 'string'
69
- ? jwtPayload['custom:phone_number']
70
- : undefined,
71
- phoneNumberVerified: typeof jwtPayload['custom:phone_number_verified'] === 'boolean'
72
- ? jwtPayload['custom:phone_number_verified']
73
- : undefined,
74
- groups: typeof jwtPayload['custom:groups'] === 'string'
75
- ? jwtPayload['custom:groups'].split(',').map((group) => group.trim())
76
- : undefined,
77
- areaCode: typeof jwtPayload['custom:area_code'] === 'string'
78
- ? jwtPayload['custom:area_code']
79
- : undefined,
80
- city: typeof jwtPayload['custom:city'] === 'string'
81
- ? jwtPayload['custom:city']
82
- : undefined,
83
- state: typeof jwtPayload['custom:state'] === 'string'
84
- ? jwtPayload['custom:state']
85
- : undefined,
86
- country: typeof jwtPayload['custom:country'] === 'string'
87
- ? jwtPayload['custom:country']
88
- : undefined,
89
- countryCallingCode: typeof jwtPayload['custom:country_calling_code'] === 'string'
90
- ? jwtPayload['custom:country_calling_code']
91
- : undefined,
92
- department: typeof jwtPayload['custom:department'] === 'string'
93
- ? jwtPayload['custom:department']
94
- : undefined,
95
- division: typeof jwtPayload['custom:division'] === 'string'
96
- ? jwtPayload['custom:division']
97
- : undefined,
98
- bargain: typeof jwtPayload['custom:bargain'] === 'string'
99
- ? jwtPayload['custom:bargain']
100
- : undefined,
101
- employeeNumber: typeof jwtPayload['custom:employee_number'] === 'string'
102
- ? jwtPayload['custom:employee_number']
103
- : undefined,
104
- departmentHeadFullName: typeof jwtPayload['custom:departmenthead_name'] === 'string'
105
- ? jwtPayload['custom:departmenthead_name']
106
- : undefined,
107
- departmentHeadEmail: typeof jwtPayload['custom:departmenthead_email'] === 'string'
108
- ? jwtPayload['custom:departmenthead_email']
109
- : undefined,
64
+ phoneNumber: parseString(jwtPayload['custom:phone_number']),
65
+ phoneNumberVerified: parseBoolean(jwtPayload['custom:phone_number_verified']),
66
+ groups: (_a = parseString(jwtPayload['custom:groups'])) === null || _a === void 0 ? void 0 : _a.split(',').map((group) => group.trim()),
67
+ areaCode: parseString(jwtPayload['custom:area_code']),
68
+ city: parseString(jwtPayload['custom:city']),
69
+ state: parseString(jwtPayload['custom:state']),
70
+ country: parseString(jwtPayload['custom:country']),
71
+ countryCallingCode: parseString(jwtPayload['custom:country_calling_code']),
72
+ department: parseString(jwtPayload['custom:department']),
73
+ division: parseString(jwtPayload['custom:division']),
74
+ bargain: parseString(jwtPayload['custom:bargain']),
75
+ employeeNumber: parseString(jwtPayload['custom:employee_number']),
76
+ departmentHeadFullName: parseString(jwtPayload['custom:departmenthead_name']),
77
+ departmentHeadEmail: parseString(jwtPayload['custom:departmenthead_email']),
110
78
  };
111
- if (!((_a = userProfile.supervisor) === null || _a === void 0 ? void 0 : _a.fullName) &&
112
- !((_b = userProfile.supervisor) === null || _b === void 0 ? void 0 : _b.email) &&
113
- !((_c = userProfile.supervisor) === null || _c === void 0 ? void 0 : _c.providerUserId)) {
79
+ if (!((_b = userProfile.supervisor) === null || _b === void 0 ? void 0 : _b.fullName) &&
80
+ !((_c = userProfile.supervisor) === null || _c === void 0 ? void 0 : _c.email) &&
81
+ !((_d = userProfile.supervisor) === null || _d === void 0 ? void 0 : _d.providerUserId)) {
114
82
  userProfile.supervisor = undefined;
115
83
  }
116
84
  if (jwtPayload.identities &&
@@ -1 +1 @@
1
- {"version":3,"file":"userService.js","sourceRoot":"","sources":["../src/userService.ts"],"names":[],"mappings":";;AAoBA,4CA0IC;AAmBD,kDAoBC;AAnMD;;;;;;;;;;;;;;;;;GAiBG;AACH,SAAgB,gBAAgB,CAC9B,IAAa;;IAEb,IAAI,OAAO,IAAI,KAAK,QAAQ,IAAI,CAAC,IAAI,EAAE,CAAC;QACtC,OAAM;IACR,CAAC;IACD,MAAM,UAAU,GAAG,IAA+B,CAAA;IAClD,IAAI,OAAO,UAAU,CAAC,GAAG,KAAK,QAAQ,EAAE,CAAC;QACvC,OAAM;IACR,CAAC;IACD,MAAM,WAAW,GAA0B;QACzC,UAAU,EAAE,KAAK;QACjB,YAAY,EAAE,SAAS;QACvB,cAAc,EAAE,UAAU,CAAC,GAAG;QAC9B,MAAM,EAAE,UAAU,CAAC,GAAG;QACtB,KAAK,EAAE,OAAO,UAAU,CAAC,KAAK,KAAK,QAAQ,CAAC,CAAC,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC,CAAC,SAAS;QAC1E,aAAa,EACX,OAAO,UAAU,CAAC,cAAc,KAAK,SAAS;YAC5C,CAAC,CAAC,UAAU,CAAC,cAAc;YAC3B,CAAC,CAAC,SAAS;QACf,SAAS,EACP,OAAO,UAAU,CAAC,UAAU,KAAK,QAAQ;YACvC,CAAC,CAAC,UAAU,CAAC,UAAU;YACvB,C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C,CAAC,CAAA;QACzD,WAAW,CAAC,YAAY,GAAG,YAAY,CAAA;QACvC,WAAW,CAAC,cAAc,GAAG,MAAM,CAAA;QACnC,WAAW,CAAC,UAAU,GAAG,YAAY,KAAK,MAAM,CAAA;QAChD,IAAI,WAAW,CAAC,UAAU,EAAE,CAAC;YAC3B,WAAW,CAAC,QAAQ,GAAG,UAAU,CAAC,kBAAkB,IAAI,MAAM,CAAA;QAChE,CAAC;IACH,CAAC;IAED,OAAO,WAAW,CAAA;AACpB,CAAC;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,SAAgB,mBAAmB,CACjC,WAAkC;IAElC,IAAI,WAAW,CAAC,QAAQ,EAAE,CAAC;QACzB,OAAO,WAAW,CAAC,QAAQ,CAAA;IAC7B,CAAC;IAED,IAAI,WAAW,CAAC,SAAS,IAAI,WAAW,CAAC,QAAQ,EAAE,CAAC;QAClD,OAAO,CAAC,WAAW,CAAC,SAAS,EAAE,WAAW,CAAC,QAAQ,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,CAAA;IACvE,CAAC;IAED,IAAI,WAAW,CAAC,KAAK,EAAE,CAAC;QACtB,OAAO,WAAW,CAAC,KAAK,CAAA;IAC1B,CAAC;IAED,IAAI,WAAW,CAAC,QAAQ,EAAE,CAAC;QACzB,OAAO,WAAW,CAAC,QAAQ,CAAA;IAC7B,CAAC;IAED,OAAO,WAAW,CAAC,MAAM,CAAA;AAC3B,CAAC","sourcesContent":["import { MiscTypes } from '@oneblink/types'\n\n/**\n * Parse a User Profile based on a JWT payload. Will return `undefined` if not a\n * valid JWT payload\n *\n * #### Example\n *\n * ```js\n * import jwtDecode from 'jwt-decode'\n *\n * const jwtPayload = jwtDecode('a valid token from a user')\n * const userProfile = userService.parseUserProfile(jwtPayload)\n * if (userProfile) {\n * // continue\n * }\n * ```\n *\n * @param data An object containing all parameters to be passed to the function\n */\nexport function parseUserProfile(\n data: unknown,\n): MiscTypes.UserProfile | undefined {\n if (typeof data !== 'object' || !data) {\n return\n }\n const jwtPayload = data as Record<string, unknown>\n if (typeof jwtPayload.sub !== 'string') {\n return\n }\n const userProfile: MiscTypes.UserProfile = {\n isSAMLUser: false,\n providerType: 'Cognito',\n providerUserId: jwtPayload.sub,\n userId: jwtPayload.sub,\n email: typeof jwtPayload.email === 'string' ? jwtPayload.email : undefined,\n emailVerified:\n typeof jwtPayload.email_verified === 'boolean'\n ? jwtPayload.email_verified\n : undefined,\n firstName:\n typeof jwtPayload.given_name === 'string'\n ? jwtPayload.given_name\n : undefined,\n middleName:\n typeof jwtPayload.middle_name === 'string'\n ? jwtPayload.middle_name\n : undefined,\n lastName:\n typeof jwtPayload.family_name === 'string'\n ? jwtPayload.family_name\n : undefined,\n fullName: typeof jwtPayload.name === 'string' ? jwtPayload.name : undefined,\n picture:\n typeof jwtPayload.picture === 'string' ? jwtPayload.picture : undefined,\n address:\n typeof jwtPayload.address === 'string' ? jwtPayload.address : undefined,\n role:\n typeof jwtPayload['custom:role'] === 'string'\n ? jwtPayload['custom:role']\n : undefined,\n username:\n typeof jwtPayload.email === 'string' ? jwtPayload.email : jwtPayload.sub,\n supervisor: {\n fullName:\n typeof jwtPayload['custom:supervisor_name'] === 'string'\n ? jwtPayload['custom:supervisor_name']\n : undefined,\n email:\n typeof jwtPayload['custom:supervisor_email'] === 'string'\n ? jwtPayload['custom:supervisor_email']\n : undefined,\n providerUserId:\n typeof jwtPayload['custom:supervisor_user_id'] === 'string'\n ? jwtPayload['custom:supervisor_user_id']\n : undefined,\n },\n phoneNumber:\n typeof jwtPayload['custom:phone_number'] === 'string'\n ? jwtPayload['custom:phone_number']\n : undefined,\n phoneNumberVerified:\n typeof jwtPayload['custom:phone_number_verified'] === 'boolean'\n ? jwtPayload['custom:phone_number_verified']\n : undefined,\n groups:\n typeof jwtPayload['custom:groups'] === 'string'\n ? jwtPayload['custom:groups'].split(',').map((group) => group.trim())\n : undefined,\n areaCode:\n typeof jwtPayload['custom:area_code'] === 'string'\n ? jwtPayload['custom:area_code']\n : undefined,\n city:\n typeof jwtPayload['custom:city'] === 'string'\n ? jwtPayload['custom:city']\n : undefined,\n state:\n typeof jwtPayload['custom:state'] === 'string'\n ? jwtPayload['custom:state']\n : undefined,\n country:\n typeof jwtPayload['custom:country'] === 'string'\n ? jwtPayload['custom:country']\n : undefined,\n countryCallingCode:\n typeof jwtPayload['custom:country_calling_code'] === 'string'\n ? jwtPayload['custom:country_calling_code']\n : undefined,\n department:\n typeof jwtPayload['custom:department'] === 'string'\n ? jwtPayload['custom:department']\n : undefined,\n division:\n typeof jwtPayload['custom:division'] === 'string'\n ? jwtPayload['custom:division']\n : undefined,\n bargain:\n typeof jwtPayload['custom:bargain'] === 'string'\n ? jwtPayload['custom:bargain']\n : undefined,\n employeeNumber:\n typeof jwtPayload['custom:employee_number'] === 'string'\n ? jwtPayload['custom:employee_number']\n : undefined,\n departmentHeadFullName:\n typeof jwtPayload['custom:departmenthead_name'] === 'string'\n ? jwtPayload['custom:departmenthead_name']\n : undefined,\n departmentHeadEmail:\n typeof jwtPayload['custom:departmenthead_email'] === 'string'\n ? jwtPayload['custom:departmenthead_email']\n : undefined,\n }\n\n if (\n !userProfile.supervisor?.fullName &&\n !userProfile.supervisor?.email &&\n !userProfile.supervisor?.providerUserId\n ) {\n userProfile.supervisor = undefined\n }\n\n if (\n jwtPayload.identities &&\n Array.isArray(jwtPayload.identities) &&\n jwtPayload.identities.length\n ) {\n const { providerType, userId } = jwtPayload.identities[0]\n userProfile.providerType = providerType\n userProfile.providerUserId = userId\n userProfile.isSAMLUser = providerType === 'SAML'\n if (userProfile.isSAMLUser) {\n userProfile.username = jwtPayload.preferred_username || userId\n }\n }\n\n return userProfile\n}\n\n/**\n * A friendly `string` that represents the a [User Profile](#user-profile). Uses\n * first name, last name, full name, email address or username.\n *\n * #### Example\n *\n * ```js\n * const userProfile = userService.parseUserProfile(jwtPayload)\n * const name = userService.getUserFriendlyName(userProfile)\n * if (name) {\n * // Display current user's name\n * }\n * ```\n *\n * @param userProfile\n * @returns\n */\nexport function getUserFriendlyName(\n userProfile: MiscTypes.UserProfile,\n): string {\n if (userProfile.fullName) {\n return userProfile.fullName\n }\n\n if (userProfile.firstName || userProfile.lastName) {\n return [userProfile.firstName, userProfile.lastName].join(' ').trim()\n }\n\n if (userProfile.email) {\n return userProfile.email\n }\n\n if (userProfile.username) {\n return userProfile.username\n }\n\n return userProfile.userId\n}\n"]}
1
+ {"version":3,"file":"userService.js","sourceRoot":"","sources":["../src/userService.ts"],"names":[],"mappings":";;AAoCA,4CA2EC;AAmBD,kDAoBC;AApJD,SAAS,YAAY,CAAC,KAAc;IAClC,OAAO,OAAO,KAAK,KAAK,SAAS,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,SAAS,CAAA;AACvD,CAAC;AAED,SAAS,WAAW,CAAC,KAAc;IACjC,OAAO,OAAO,KAAK,KAAK,QAAQ,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,SAAS,CAAA;AACtD,CAAC;AAED,SAAS,YAAY,CAAC,OAAgB;IACpC,IAAI,OAAO,OAAO,KAAK,QAAQ,IAAI,OAAO,IAAI,WAAW,IAAI,OAAO,EAAE,CAAC;QACrE,OAAO,WAAW,CAAC,OAAO,CAAC,SAAS,CAAC,CAAA;IACvC,CAAC;IAED,OAAO,WAAW,CAAC,OAAO,CAAC,CAAA;AAC7B,CAAC;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,SAAgB,gBAAgB,CAC9B,IAAa;;IAEb,IAAI,OAAO,IAAI,KAAK,QAAQ,IAAI,CAAC,IAAI,EAAE,CAAC;QACtC,OAAM;IACR,CAAC;IACD,MAAM,UAAU,GAAG,IAA+B,CAAA;IAClD,IAAI,OAAO,UAAU,CAAC,GAAG,KAAK,QAAQ,EAAE,CAAC;QACvC,OAAM;IACR,CAAC;IACD,MAAM,WAAW,GAA0B;QACzC,UAAU,EAAE,KAAK;QACjB,YAAY,EAAE,SAAS;QACvB,cAAc,E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AAM,CAAA;QAChD,IAAI,WAAW,CAAC,UAAU,EAAE,CAAC;YAC3B,WAAW,CAAC,QAAQ,GAAG,UAAU,CAAC,kBAAkB,IAAI,MAAM,CAAA;QAChE,CAAC;IACH,CAAC;IAED,OAAO,WAAW,CAAA;AACpB,CAAC;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,SAAgB,mBAAmB,CACjC,WAAkC;IAElC,IAAI,WAAW,CAAC,QAAQ,EAAE,CAAC;QACzB,OAAO,WAAW,CAAC,QAAQ,CAAA;IAC7B,CAAC;IAED,IAAI,WAAW,CAAC,SAAS,IAAI,WAAW,CAAC,QAAQ,EAAE,CAAC;QAClD,OAAO,CAAC,WAAW,CAAC,SAAS,EAAE,WAAW,CAAC,QAAQ,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,CAAA;IACvE,CAAC;IAED,IAAI,WAAW,CAAC,KAAK,EAAE,CAAC;QACtB,OAAO,WAAW,CAAC,KAAK,CAAA;IAC1B,CAAC;IAED,IAAI,WAAW,CAAC,QAAQ,EAAE,CAAC;QACzB,OAAO,WAAW,CAAC,QAAQ,CAAA;IAC7B,CAAC;IAED,OAAO,WAAW,CAAC,MAAM,CAAA;AAC3B,CAAC","sourcesContent":["import { MiscTypes } from '@oneblink/types'\n\nfunction parseBoolean(value: unknown): boolean | undefined {\n return typeof value === 'boolean' ? value : undefined\n}\n\nfunction parseString(value: unknown): string | undefined {\n return typeof value === 'string' ? value : undefined\n}\n\nfunction parseAddress(address: unknown): string | undefined {\n if (typeof address === 'object' && address && 'formatted' in address) {\n return parseString(address.formatted)\n }\n\n return parseString(address)\n}\n\n/**\n * Parse a User Profile based on a JWT payload. Will return `undefined` if not a\n * valid JWT payload\n *\n * #### Example\n *\n * ```js\n * import jwtDecode from 'jwt-decode'\n *\n * const jwtPayload = jwtDecode('a valid token from a user')\n * const userProfile = userService.parseUserProfile(jwtPayload)\n * if (userProfile) {\n * // continue\n * }\n * ```\n *\n * @param data An object containing all parameters to be passed to the function\n */\nexport function parseUserProfile(\n data: unknown,\n): MiscTypes.UserProfile | undefined {\n if (typeof data !== 'object' || !data) {\n return\n }\n const jwtPayload = data as Record<string, unknown>\n if (typeof jwtPayload.sub !== 'string') {\n return\n }\n const userProfile: MiscTypes.UserProfile = {\n isSAMLUser: false,\n providerType: 'Cognito',\n providerUserId: jwtPayload.sub,\n userId: jwtPayload.sub,\n email: parseString(jwtPayload.email),\n emailVerified: parseBoolean(jwtPayload.email_verified),\n firstName: parseString(jwtPayload.given_name),\n middleName: parseString(jwtPayload.middle_name),\n lastName: parseString(jwtPayload.family_name),\n fullName: parseString(jwtPayload.name),\n picture: parseString(jwtPayload.picture),\n address: parseAddress(jwtPayload.address),\n role: parseString(jwtPayload['custom:role']),\n username: parseString(jwtPayload.email) || jwtPayload.sub,\n supervisor: {\n fullName: parseString(jwtPayload['custom:supervisor_name']),\n email: parseString(jwtPayload['custom:supervisor_email']),\n providerUserId: parseString(jwtPayload['custom:supervisor_user_id']),\n },\n phoneNumber: parseString(jwtPayload['custom:phone_number']),\n phoneNumberVerified: parseBoolean(\n jwtPayload['custom:phone_number_verified'],\n ),\n groups: parseString(jwtPayload['custom:groups'])\n ?.split(',')\n .map((group) => group.trim()),\n areaCode: parseString(jwtPayload['custom:area_code']),\n city: parseString(jwtPayload['custom:city']),\n state: parseString(jwtPayload['custom:state']),\n country: parseString(jwtPayload['custom:country']),\n countryCallingCode: parseString(jwtPayload['custom:country_calling_code']),\n department: parseString(jwtPayload['custom:department']),\n division: parseString(jwtPayload['custom:division']),\n bargain: parseString(jwtPayload['custom:bargain']),\n employeeNumber: parseString(jwtPayload['custom:employee_number']),\n departmentHeadFullName: parseString(\n jwtPayload['custom:departmenthead_name'],\n ),\n departmentHeadEmail: parseString(jwtPayload['custom:departmenthead_email']),\n }\n\n if (\n !userProfile.supervisor?.fullName &&\n !userProfile.supervisor?.email &&\n !userProfile.supervisor?.providerUserId\n ) {\n userProfile.supervisor = undefined\n }\n\n if (\n jwtPayload.identities &&\n Array.isArray(jwtPayload.identities) &&\n jwtPayload.identities.length\n ) {\n const { providerType, userId } = jwtPayload.identities[0]\n userProfile.providerType = providerType\n userProfile.providerUserId = userId\n userProfile.isSAMLUser = providerType === 'SAML'\n if (userProfile.isSAMLUser) {\n userProfile.username = jwtPayload.preferred_username || userId\n }\n }\n\n return userProfile\n}\n\n/**\n * A friendly `string` that represents the a [User Profile](#user-profile). Uses\n * first name, last name, full name, email address or username.\n *\n * #### Example\n *\n * ```js\n * const userProfile = userService.parseUserProfile(jwtPayload)\n * const name = userService.getUserFriendlyName(userProfile)\n * if (name) {\n * // Display current user's name\n * }\n * ```\n *\n * @param userProfile\n * @returns\n */\nexport function getUserFriendlyName(\n userProfile: MiscTypes.UserProfile,\n): string {\n if (userProfile.fullName) {\n return userProfile.fullName\n }\n\n if (userProfile.firstName || userProfile.lastName) {\n return [userProfile.firstName, userProfile.lastName].join(' ').trim()\n }\n\n if (userProfile.email) {\n return userProfile.email\n }\n\n if (userProfile.username) {\n return userProfile.username\n }\n\n return userProfile.userId\n}\n"]}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@oneblink/sdk-core",
3
3
  "description": "OneBlink SDK for JavaScript (works in Browsers and NodeJS)",
4
- "version": "8.2.0-beta.1",
4
+ "version": "8.2.0-beta.2",
5
5
  "author": "OneBlink <developers@oneblink.io> (https://oneblink.io)",
6
6
  "bugs": {
7
7
  "url": "https://github.com/oneblink/sdk-core-js/issues"
@@ -11,7 +11,7 @@
11
11
  "@eslint/js": "^9.21.0",
12
12
  "@microsoft/eslint-plugin-sdl": "^1.1.0",
13
13
  "@microsoft/microsoft-graph-types": "^2.40.0",
14
- "@oneblink/release-cli": "^3.3.1-beta.3",
14
+ "@oneblink/release-cli": "^3.3.1-beta.4",
15
15
  "@oneblink/types": "github:oneblink/types",
16
16
  "@types/jest": "^29.5.14",
17
17
  "@types/lodash": "^4.17.16",