Skip to content

Commit

Permalink
changing incoming response from hubsdk
Browse files Browse the repository at this point in the history
  • Loading branch information
lakhveerk committed Sep 30, 2024
1 parent cb946e6 commit 6a08b1c
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
AppId,
AuthTokenRequestParameters,
externalAppAuthenticationForCEA,
IActionExecuteInvokeRequest,
Expand Down Expand Up @@ -40,7 +41,7 @@ const AuthenticateWithOAuth = (): React.ReactElement =>
}
},
submit: async (input) => {
await externalAppAuthenticationForCEA.authenticateWithOAuth(input.appId, input.conversationId, {
await externalAppAuthenticationForCEA.authenticateWithOAuth(new AppId(input.appId), input.conversationId, {
...input.authenticateParameters,
url: new URL(input.authenticateParameters.url),
});
Expand Down Expand Up @@ -78,7 +79,7 @@ const AuthenticateWithSSO = (): React.ReactElement =>
},
submit: async (input) => {
await externalAppAuthenticationForCEA.authenticateWithSSO(
input.appId,
new AppId(input.appId),
input.conversationId,
input.authTokenRequest,
);
Expand Down Expand Up @@ -124,7 +125,7 @@ const AuthenticateAndResendRequest = (): React.ReactElement =>
},
submit: async (input) => {
const result = await externalAppAuthenticationForCEA.authenticateAndResendRequest(
input.appId,
new AppId(input.appId),
input.conversationId,
{ ...input.authenticateParameters, url: new URL(input.authenticateParameters.url) },
input.originalRequestInfo,
Expand Down Expand Up @@ -173,7 +174,7 @@ const AuthenticateWithSSOAndResendRequest = (): React.ReactElement =>
},
submit: async (input) => {
const result = await externalAppAuthenticationForCEA.authenticateWithSSOAndResendRequest(
input.appId,
new AppId(input.appId),
input.conversationId,
input.authTokenRequest,
input.originalRequestInfo,
Expand Down
44 changes: 30 additions & 14 deletions packages/teams-js/src/private/externalAppAuthenticationForCEA.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@ import {
ActionExecuteInvokeRequestType,
AuthenticatePopUpParameters,
AuthTokenRequestParameters,
defaultExternalAppError,
IActionExecuteInvokeRequest,
IActionExecuteResponse,
InvokeError,
InvokeErrorCode,
InvokeErrorWrapper,
InvokeResponseType,
isInvokeErrorWrapper,
} from './interfaces';

const externalAppAuthenticationTelemetryVersionNumber: ApiVersionNumber = ApiVersionNumber.V_2;
Expand Down Expand Up @@ -57,7 +60,7 @@ export namespace externalAppAuthenticationForCEA {
ApiName.ExternalAppAuthenticationForCEA_AuthenticateWithSSO,
),
'externalAppAuthenticationForCEA.authenticateWithSSO',
[appId, conversationId, authTokenRequest.claims, authTokenRequest.silent],
[appId.toString(), conversationId, authTokenRequest.claims, authTokenRequest.silent],
);
if (error) {
throw error;
Expand Down Expand Up @@ -97,7 +100,7 @@ export namespace externalAppAuthenticationForCEA {
),
'externalAppAuthenticationForCEA.authenticateWithOauth',
[
appId,
appId.toString(),
conversationId,
authenticateParameters.url.href,
authenticateParameters.width,
Expand Down Expand Up @@ -140,14 +143,14 @@ export namespace externalAppAuthenticationForCEA {
validateOriginalRequestInfo(originalRequestInfo);

// Ask the parent window to open an authentication window with the parameters provided by the caller.
const [error, response] = await sendMessageToParentAsync<[InvokeErrorWrapper, IActionExecuteResponse]>(
const [response] = await sendMessageToParentAsync<[InvokeErrorWrapper | IActionExecuteResponse]>(
getApiVersionTag(
externalAppAuthenticationTelemetryVersionNumber,
ApiName.ExternalAppAuthentication_AuthenticateAndResendRequest,
),
'externalAppAuthenticationForCEA.authenticateAndResendRequest',
[
appId,
appId.toString(),
conversationId,
originalRequestInfo,
authenticateParameters.url.href,
Expand All @@ -156,13 +159,26 @@ export namespace externalAppAuthenticationForCEA {
authenticateParameters.isExternal,
],
);
if (response && response.responseType != null) {
return response as IActionExecuteResponse;
if (isActionExecuteResponse(response)) {
return response;
} else if (isInvokeErrorWrapper(response)) {
throw response;
} else {
throw error;
throw defaultExternalAppError;
}
}

function isActionExecuteResponse(response: unknown): response is IActionExecuteResponse {
const actionResponse = response as IActionExecuteResponse;

return (
actionResponse.responseType === InvokeResponseType.ActionExecuteInvokeResponse &&
actionResponse.value !== undefined &&
actionResponse.statusCode !== undefined &&
actionResponse.type !== undefined
);
}

/**
* @beta
* @hidden
Expand Down Expand Up @@ -192,20 +208,20 @@ export namespace externalAppAuthenticationForCEA {

validateOriginalRequestInfo(originalRequestInfo);

const [error, response] = await sendMessageToParentAsync<
[InvokeErrorWrapper, IActionExecuteResponse | InvokeErrorWrapper]
>(
const [response] = await sendMessageToParentAsync<[IActionExecuteResponse | InvokeErrorWrapper]>(
getApiVersionTag(
externalAppAuthenticationTelemetryVersionNumber,
ApiName.ExternalAppAuthentication_AuthenticateWithSSOAndResendRequest,
),
'externalAppAuthenticationForCEA.authenticateWithSSOAndResendRequest',
[appId, conversationId, originalRequestInfo, authTokenRequest.claims, authTokenRequest.silent],
[appId.toString(), conversationId, originalRequestInfo, authTokenRequest.claims, authTokenRequest.silent],
);
if (response && response.responseType != null) {
return response as IActionExecuteResponse;
if (isActionExecuteResponse(response)) {
return response;
} else if (isInvokeErrorWrapper(response)) {
throw response;
} else {
throw error;
throw defaultExternalAppError;
}
}

Expand Down
19 changes: 19 additions & 0 deletions packages/teams-js/src/private/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -614,3 +614,22 @@ export enum InvokeErrorCode {
*/
export type InvokeErrorWrapper = InvokeError & { responseType: undefined };
export const ActionExecuteInvokeRequestType = 'Action.Execute';

export function isInvokeErrorWrapper(err: unknown): err is InvokeErrorWrapper {
if (typeof err !== 'object' || err === null) {
return false;
}

const errorWrapper = err as InvokeErrorWrapper;

return (
errorWrapper?.errorCode === InvokeErrorCode.INTERNAL_ERROR &&
(errorWrapper.message === undefined || typeof errorWrapper.message === 'string') &&
errorWrapper.responseType === undefined
);
}

export const defaultExternalAppError = {
errorCode: ExternalAppErrorCode.INTERNAL_ERROR,
message: 'An internal error occurred',
};

0 comments on commit 6a08b1c

Please sign in to comment.