Skip to content

Commit

Permalink
feat(ama-sdk-core): abort requests by passing an abort signal in meta…
Browse files Browse the repository at this point in the history
…data (AmadeusITGroup#1470)

## Proposed change

Adds the ability to abort requests in `@ama-sdk/core` (and any request
of an SDK generated by the Otter SDK generator) by passing an [abort
signal](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) in
metadata. It is implemented both for the fetch and the angular client.
  • Loading branch information
divdavem authored Mar 11, 2024
2 parents 68a3227 + 09e1156 commit 45db755
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
9 changes: 8 additions & 1 deletion packages/@ama-sdk/core/src/clients/api-angular-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,9 @@ export class ApiAngularClient implements ApiClient {

const asyncResponse = new Promise<HttpResponse<any>>((resolve, reject) => {
let data: HttpResponse<any>;
this.options.httpClient.request(options.method, url, {
const metadataSignal = options.metadata?.signal;
metadataSignal?.throwIfAborted();
const subscription = this.options.httpClient.request(options.method, url, {
...options,
observe: 'response',
headers
Expand All @@ -113,6 +115,11 @@ export class ApiAngularClient implements ApiClient {
error: (err) => reject(err),
complete: () => resolve(data)
});
metadataSignal?.throwIfAborted();
metadataSignal?.addEventListener('abort', () => {
subscription.unsubscribe();
reject(metadataSignal.reason);
});
});
response = await asyncResponse;
root = response.body;
Expand Down
9 changes: 6 additions & 3 deletions packages/@ama-sdk/core/src/clients/api-fetch-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,13 @@ export class ApiFetchClient implements ApiClient {
// Execute call
try {

const metadataSignal = options.metadata?.signal;
metadataSignal?.throwIfAborted();

const controller = new AbortController();
if (controller) {
options.signal = controller.signal;
}
options.signal = controller.signal;
metadataSignal?.addEventListener('abort', () => controller.abort());

const loadedPlugins: (PluginAsyncRunner<Response, FetchCall> & PluginAsyncStarter)[] = [];
if (this.options.fetchPlugins) {
loadedPlugins.push(...this.options.fetchPlugins.map((plugin) => plugin.load({url, options, fetchPlugins: loadedPlugins, controller, apiClient: this, logger: this.options.logger})));
Expand Down
2 changes: 2 additions & 0 deletions packages/@ama-sdk/core/src/plugins/core/request-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ export interface RequestMetadata<C extends string = string, A extends string = s
headerContentType?: C;
/** Force a MIME type to be used as Accept header */
headerAccept?: A;
/** Signal to abort the request */
signal?: AbortSignal;
}

export interface RequestOptions extends RequestInit {
Expand Down

0 comments on commit 45db755

Please sign in to comment.