@azure/communication-common
Advanced tools
Comparing version 2.0.0 to 2.1.0-alpha.20220504.1
# Release History | ||
## 2.1.0 (Unreleased) | ||
### Features Added | ||
- Added `getIdentifierRawId` and `createIdentifierFromRawId` to translate between a `CommunicationIdentifier` and its underlying canonical rawId representation. Developers can now use the rawId as an encoded format for identifiers to store in their databases or as stable keys in general. | ||
- Always include `rawId` when serializing identifiers to wire format. | ||
## 2.0.0 (2022-03-08) | ||
@@ -4,0 +11,0 @@ |
@@ -52,2 +52,86 @@ // Copyright (c) Microsoft Corporation. | ||
}; | ||
/** | ||
* Returns the rawId for a given CommunicationIdentifier. You can use the rawId for encoding the identifier and then use it as a key in a database. | ||
* | ||
* @param identifier - The identifier to be translated to its rawId. | ||
*/ | ||
export const getIdentifierRawId = (identifier) => { | ||
const identifierKind = getIdentifierKind(identifier); | ||
switch (identifierKind.kind) { | ||
case "communicationUser": | ||
return identifierKind.communicationUserId; | ||
case "microsoftTeamsUser": { | ||
const { microsoftTeamsUserId, rawId, cloud, isAnonymous } = identifierKind; | ||
if (rawId) | ||
return rawId; | ||
if (isAnonymous) | ||
return `8:teamsvisitor:${microsoftTeamsUserId}`; | ||
switch (cloud) { | ||
case "dod": | ||
return `8:dod:${microsoftTeamsUserId}`; | ||
case "gcch": | ||
return `8:gcch:${microsoftTeamsUserId}`; | ||
case "public": | ||
return `8:orgid:${microsoftTeamsUserId}`; | ||
} | ||
return `8:orgid:${microsoftTeamsUserId}`; | ||
} | ||
case "phoneNumber": { | ||
const { phoneNumber, rawId } = identifierKind; | ||
if (rawId) | ||
return rawId; | ||
// strip the leading +. We just assume correct E.164 format here because validation should only happen server-side, not client-side. | ||
return `4:${phoneNumber.replace(/^\+/, "")}`; | ||
} | ||
case "unknown": { | ||
return identifierKind.id; | ||
} | ||
} | ||
}; | ||
/** | ||
* Creates a CommunicationIdentifierKind from a given rawId. When storing rawIds use this function to restore the identifier that was encoded in the rawId. | ||
* | ||
* @param rawId - The rawId to be translated to its identifier representation. | ||
*/ | ||
export const createIdentifierFromRawId = (rawId) => { | ||
if (rawId.startsWith("4:")) { | ||
return { kind: "phoneNumber", phoneNumber: `+${rawId.substring("4:".length)}` }; | ||
} | ||
const segments = rawId.split(":"); | ||
if (segments.length < 3) | ||
return { kind: "unknown", id: rawId }; | ||
const prefix = `${segments[0]}:${segments[1]}:`; | ||
const suffix = rawId.substring(prefix.length); | ||
switch (prefix) { | ||
case "8:teamsvisitor:": | ||
return { kind: "microsoftTeamsUser", microsoftTeamsUserId: suffix, isAnonymous: true }; | ||
case "8:orgid:": | ||
return { | ||
kind: "microsoftTeamsUser", | ||
microsoftTeamsUserId: suffix, | ||
isAnonymous: false, | ||
cloud: "public", | ||
}; | ||
case "8:dod:": | ||
return { | ||
kind: "microsoftTeamsUser", | ||
microsoftTeamsUserId: suffix, | ||
isAnonymous: false, | ||
cloud: "dod", | ||
}; | ||
case "8:gcch:": | ||
return { | ||
kind: "microsoftTeamsUser", | ||
microsoftTeamsUserId: suffix, | ||
isAnonymous: false, | ||
cloud: "gcch", | ||
}; | ||
case "8:acs:": | ||
case "8:spool:": | ||
case "8:dod-acs:": | ||
case "8:gcch-acs:": | ||
return { kind: "communicationUser", communicationUserId: rawId }; | ||
} | ||
return { kind: "unknown", id: rawId }; | ||
}; | ||
//# sourceMappingURL=identifierModels.js.map |
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
import { __rest } from "tslib"; | ||
import { getIdentifierKind, } from "./identifierModels"; | ||
const addRawIdIfExisting = (identifier, rawId) => { | ||
return rawId === undefined ? identifier : Object.assign(Object.assign({}, identifier), { rawId: rawId }); | ||
}; | ||
import { getIdentifierKind, getIdentifierRawId, } from "./identifierModels"; | ||
const assertNotNullOrUndefined = (obj, prop) => { | ||
@@ -29,17 +26,26 @@ const subObjName = Object.keys(obj)[0]; | ||
export const serializeCommunicationIdentifier = (identifier) => { | ||
var _a, _b; | ||
var _a, _b, _c, _d; | ||
const identifierKind = getIdentifierKind(identifier); | ||
switch (identifierKind.kind) { | ||
case "communicationUser": | ||
return { communicationUser: { id: identifierKind.communicationUserId } }; | ||
return { | ||
rawId: getIdentifierRawId(identifierKind), | ||
communicationUser: { id: identifierKind.communicationUserId }, | ||
}; | ||
case "phoneNumber": | ||
return addRawIdIfExisting({ phoneNumber: { value: identifierKind.phoneNumber } }, identifierKind.rawId); | ||
return { | ||
rawId: (_a = identifierKind.rawId) !== null && _a !== void 0 ? _a : getIdentifierRawId(identifierKind), | ||
phoneNumber: { | ||
value: identifierKind.phoneNumber, | ||
}, | ||
}; | ||
case "microsoftTeamsUser": | ||
return addRawIdIfExisting({ | ||
return { | ||
rawId: (_b = identifierKind.rawId) !== null && _b !== void 0 ? _b : getIdentifierRawId(identifierKind), | ||
microsoftTeamsUser: { | ||
userId: identifierKind.microsoftTeamsUserId, | ||
isAnonymous: (_a = identifierKind.isAnonymous) !== null && _a !== void 0 ? _a : false, | ||
cloud: (_b = identifierKind.cloud) !== null && _b !== void 0 ? _b : "public", | ||
isAnonymous: (_c = identifierKind.isAnonymous) !== null && _c !== void 0 ? _c : false, | ||
cloud: (_d = identifierKind.cloud) !== null && _d !== void 0 ? _d : "public", | ||
}, | ||
}, identifierKind.rawId); | ||
}; | ||
case "unknown": | ||
@@ -46,0 +52,0 @@ return { rawId: identifierKind.id }; |
@@ -373,7 +373,88 @@ 'use strict'; | ||
}; | ||
/** | ||
* Returns the rawId for a given CommunicationIdentifier. You can use the rawId for encoding the identifier and then use it as a key in a database. | ||
* | ||
* @param identifier - The identifier to be translated to its rawId. | ||
*/ | ||
const getIdentifierRawId = (identifier) => { | ||
const identifierKind = getIdentifierKind(identifier); | ||
switch (identifierKind.kind) { | ||
case "communicationUser": | ||
return identifierKind.communicationUserId; | ||
case "microsoftTeamsUser": { | ||
const { microsoftTeamsUserId, rawId, cloud, isAnonymous } = identifierKind; | ||
if (rawId) | ||
return rawId; | ||
if (isAnonymous) | ||
return `8:teamsvisitor:${microsoftTeamsUserId}`; | ||
switch (cloud) { | ||
case "dod": | ||
return `8:dod:${microsoftTeamsUserId}`; | ||
case "gcch": | ||
return `8:gcch:${microsoftTeamsUserId}`; | ||
case "public": | ||
return `8:orgid:${microsoftTeamsUserId}`; | ||
} | ||
return `8:orgid:${microsoftTeamsUserId}`; | ||
} | ||
case "phoneNumber": { | ||
const { phoneNumber, rawId } = identifierKind; | ||
if (rawId) | ||
return rawId; | ||
// strip the leading +. We just assume correct E.164 format here because validation should only happen server-side, not client-side. | ||
return `4:${phoneNumber.replace(/^\+/, "")}`; | ||
} | ||
case "unknown": { | ||
return identifierKind.id; | ||
} | ||
} | ||
}; | ||
/** | ||
* Creates a CommunicationIdentifierKind from a given rawId. When storing rawIds use this function to restore the identifier that was encoded in the rawId. | ||
* | ||
* @param rawId - The rawId to be translated to its identifier representation. | ||
*/ | ||
const createIdentifierFromRawId = (rawId) => { | ||
if (rawId.startsWith("4:")) { | ||
return { kind: "phoneNumber", phoneNumber: `+${rawId.substring("4:".length)}` }; | ||
} | ||
const segments = rawId.split(":"); | ||
if (segments.length < 3) | ||
return { kind: "unknown", id: rawId }; | ||
const prefix = `${segments[0]}:${segments[1]}:`; | ||
const suffix = rawId.substring(prefix.length); | ||
switch (prefix) { | ||
case "8:teamsvisitor:": | ||
return { kind: "microsoftTeamsUser", microsoftTeamsUserId: suffix, isAnonymous: true }; | ||
case "8:orgid:": | ||
return { | ||
kind: "microsoftTeamsUser", | ||
microsoftTeamsUserId: suffix, | ||
isAnonymous: false, | ||
cloud: "public", | ||
}; | ||
case "8:dod:": | ||
return { | ||
kind: "microsoftTeamsUser", | ||
microsoftTeamsUserId: suffix, | ||
isAnonymous: false, | ||
cloud: "dod", | ||
}; | ||
case "8:gcch:": | ||
return { | ||
kind: "microsoftTeamsUser", | ||
microsoftTeamsUserId: suffix, | ||
isAnonymous: false, | ||
cloud: "gcch", | ||
}; | ||
case "8:acs:": | ||
case "8:spool:": | ||
case "8:dod-acs:": | ||
case "8:gcch-acs:": | ||
return { kind: "communicationUser", communicationUserId: rawId }; | ||
} | ||
return { kind: "unknown", id: rawId }; | ||
}; | ||
// Copyright (c) Microsoft Corporation. | ||
const addRawIdIfExisting = (identifier, rawId) => { | ||
return rawId === undefined ? identifier : Object.assign(Object.assign({}, identifier), { rawId: rawId }); | ||
}; | ||
const assertNotNullOrUndefined = (obj, prop) => { | ||
@@ -400,17 +481,26 @@ const subObjName = Object.keys(obj)[0]; | ||
const serializeCommunicationIdentifier = (identifier) => { | ||
var _a, _b; | ||
var _a, _b, _c, _d; | ||
const identifierKind = getIdentifierKind(identifier); | ||
switch (identifierKind.kind) { | ||
case "communicationUser": | ||
return { communicationUser: { id: identifierKind.communicationUserId } }; | ||
return { | ||
rawId: getIdentifierRawId(identifierKind), | ||
communicationUser: { id: identifierKind.communicationUserId }, | ||
}; | ||
case "phoneNumber": | ||
return addRawIdIfExisting({ phoneNumber: { value: identifierKind.phoneNumber } }, identifierKind.rawId); | ||
return { | ||
rawId: (_a = identifierKind.rawId) !== null && _a !== void 0 ? _a : getIdentifierRawId(identifierKind), | ||
phoneNumber: { | ||
value: identifierKind.phoneNumber, | ||
}, | ||
}; | ||
case "microsoftTeamsUser": | ||
return addRawIdIfExisting({ | ||
return { | ||
rawId: (_b = identifierKind.rawId) !== null && _b !== void 0 ? _b : getIdentifierRawId(identifierKind), | ||
microsoftTeamsUser: { | ||
userId: identifierKind.microsoftTeamsUserId, | ||
isAnonymous: (_a = identifierKind.isAnonymous) !== null && _a !== void 0 ? _a : false, | ||
cloud: (_b = identifierKind.cloud) !== null && _b !== void 0 ? _b : "public", | ||
isAnonymous: (_c = identifierKind.isAnonymous) !== null && _c !== void 0 ? _c : false, | ||
cloud: (_d = identifierKind.cloud) !== null && _d !== void 0 ? _d : "public", | ||
}, | ||
}, identifierKind.rawId); | ||
}; | ||
case "unknown": | ||
@@ -461,4 +551,6 @@ return { rawId: identifierKind.id }; | ||
exports.createCommunicationAuthPolicy = createCommunicationAuthPolicy; | ||
exports.createIdentifierFromRawId = createIdentifierFromRawId; | ||
exports.deserializeCommunicationIdentifier = deserializeCommunicationIdentifier; | ||
exports.getIdentifierKind = getIdentifierKind; | ||
exports.getIdentifierRawId = getIdentifierRawId; | ||
exports.isCommunicationUserIdentifier = isCommunicationUserIdentifier; | ||
@@ -465,0 +557,0 @@ exports.isKeyCredential = isKeyCredential; |
{ | ||
"name": "@azure/communication-common", | ||
"version": "2.0.0", | ||
"version": "2.1.0-alpha.20220504.1", | ||
"description": "Common package for Azure Communication services.", | ||
@@ -74,5 +74,5 @@ "sdk-type": "client", | ||
"devDependencies": { | ||
"@azure/eslint-plugin-azure-sdk": "^3.0.0", | ||
"@azure/dev-tool": "^1.0.0", | ||
"@microsoft/api-extractor": "^7.18.11", | ||
"@azure/eslint-plugin-azure-sdk": ">=3.0.0-alpha <3.0.0-alphb", | ||
"@azure/dev-tool": ">=1.0.0-alpha <1.0.0-alphb", | ||
"@microsoft/api-extractor": "7.18.11", | ||
"@types/chai-as-promised": "^7.1.0", | ||
@@ -79,0 +79,0 @@ "@types/chai": "^4.1.6", |
@@ -129,2 +129,9 @@ import { AbortSignalLike } from '@azure/abort-controller'; | ||
/** | ||
* Creates a CommunicationIdentifierKind from a given rawId. When storing rawIds use this function to restore the identifier that was encoded in the rawId. | ||
* | ||
* @param rawId - The rawId to be translated to its identifier representation. | ||
*/ | ||
export declare const createIdentifierFromRawId: (rawId: string) => CommunicationIdentifierKind; | ||
/** | ||
* @hidden | ||
@@ -160,2 +167,9 @@ * Translates the serialized format of a communication identifier to CommunicationIdentifier. | ||
/** | ||
* Returns the rawId for a given CommunicationIdentifier. You can use the rawId for encoding the identifier and then use it as a key in a database. | ||
* | ||
* @param identifier - The identifier to be translated to its rawId. | ||
*/ | ||
export declare const getIdentifierRawId: (identifier: CommunicationIdentifier) => string; | ||
/** | ||
* Tests an Identifier to determine whether it implements CommunicationUserIdentifier. | ||
@@ -162,0 +176,0 @@ * |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
171151
1503
1