-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api-rest): support making external promise cancellable (#12167)
- Loading branch information
1 parent
a5bea51
commit a26f692
Showing
9 changed files
with
141 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { HttpResponse } from '@aws-amplify/core/internals/aws-client-utils'; | ||
import { CancelledError, RestApiError } from '../errors'; | ||
import { Operation } from '../types'; | ||
import { parseRestApiServiceError } from './serviceError'; | ||
|
||
/** | ||
* Create a cancellable operation conforming to the internal POST API interface. | ||
* @internal | ||
*/ | ||
export function createCancellableOperation( | ||
handler: () => Promise<HttpResponse>, | ||
abortController: AbortController | ||
): Promise<HttpResponse>; | ||
/** | ||
* Create a cancellable operation conforming to the external REST API interface. | ||
* @internal | ||
*/ | ||
export function createCancellableOperation( | ||
handler: (signal: AbortSignal) => Promise<HttpResponse> | ||
): Promise<HttpResponse>; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export function createCancellableOperation( | ||
handler: (signal?: AbortSignal) => Promise<HttpResponse>, | ||
abortController?: AbortController | ||
): Operation<HttpResponse> | Promise<HttpResponse> { | ||
const isInternalPost = ( | ||
handler: (signal?: AbortSignal) => Promise<HttpResponse> | ||
): handler is () => Promise<HttpResponse> => !!abortController; | ||
const signal = abortController?.signal; | ||
const job = async () => { | ||
try { | ||
const response = await (isInternalPost(handler) | ||
? handler() | ||
: handler(signal)); | ||
if (response.statusCode >= 300) { | ||
throw parseRestApiServiceError(response)!; | ||
} | ||
return response; | ||
} catch (error) { | ||
if (error.name === 'AbortError' || signal?.aborted === true) { | ||
throw new CancelledError({ | ||
name: error.name, | ||
message: signal.reason ?? error.message, | ||
underlyingError: error, | ||
}); | ||
} | ||
throw new RestApiError({ | ||
...error, | ||
underlyingError: error, | ||
}); | ||
} | ||
}; | ||
|
||
if (isInternalPost(handler)) { | ||
return job(); | ||
} else { | ||
const cancel = (abortMessage?: string) => { | ||
if (signal?.aborted === true) { | ||
return; | ||
} | ||
abortController?.abort(abortMessage); | ||
// Abort reason is not widely support enough across runtimes and and browsers, so we set it | ||
// if it is not already set. | ||
if (signal?.reason !== abortMessage) { | ||
// @ts-expect-error reason is a readonly property | ||
signal['reason'] = abortMessage; | ||
} | ||
}; | ||
return { response: job(), cancel }; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
export { createCancellableOperation } from './apiOperation'; | ||
export { createCancellableOperation } from './createCancellableOperation'; | ||
export { resolveCredentials } from './resolveCredentials'; | ||
export { parseUrl } from './parseUrl'; | ||
export { parseRestApiServiceError } from './serviceError'; |