Skip to content

Commit

Permalink
Add support for passing an AbortSignal to a request (#59)
Browse files Browse the repository at this point in the history
* chore: pass through abort signal to request

* chore: add abort signal test

* chore: satisfy linter

* chore: satisfy linter
  • Loading branch information
sebastianfrey authored Mar 12, 2024
1 parent 1871f96 commit a1853ac
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 10 deletions.
21 changes: 18 additions & 3 deletions packages/features/src/features.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export class FeatureService extends Service {
const result: IGetCollectionsResponse = await request({
url,
params: options.params,
signal: options.signal,
});
return result;
}
Expand All @@ -69,7 +70,11 @@ export class FeatureService extends Service {
options: IServiceRequestOptions = {}
): Promise<ICollection> {
const url: string = `${this.baseUrl}/collections/${collectionId}`;
const result: ICollection = await request({ url, params: options.params });
const result: ICollection = await request({
url,
params: options.params,
signal: options.signal,
});
return result;
}

Expand All @@ -83,7 +88,11 @@ export class FeatureService extends Service {
options: IServiceRequestOptions = {}
): Promise<IQueryables> {
const url: string = `${this.baseUrl}/collections/${collectionId}/queryables`;
const result: IQueryables = await request({ url, params: options.params });
const result: IQueryables = await request({
url,
params: options.params,
signal: options.signal,
});
return result;
}

Expand All @@ -101,6 +110,7 @@ export class FeatureService extends Service {
const result: IGetFeaturesResponse = await request({
url,
params,
signal: options.signal,
});
return result;
}
Expand All @@ -119,6 +129,7 @@ export class FeatureService extends Service {
const result: IGetFeaturesResponse = await request({
url,
params,
signal: options.signal,
method: 'POST',
headers: { 'content-type': 'application/json' },
});
Expand All @@ -138,7 +149,11 @@ export class FeatureService extends Service {
): Promise<IFeature> {
const params = toGetFeatureRequestParams(options);
const url: string = `${this.baseUrl}/collections/${collectionId}/items/${featureId}`;
const result: IFeature = await request({ url, params });
const result: IFeature = await request({
url,
params,
signal: options.signal,
});
return result;
}
}
Expand Down
39 changes: 33 additions & 6 deletions packages/processes/src/processes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ export class ProcessesService extends Service {
options: IServiceRequestOptions = {}
): Promise<IProcess> {
const url: string = `${this.baseUrl}/processes/${processId}`;
const result: IProcess = await request({ url, params: options.params });
const result: IProcess = await request({
url,
signal: options.signal,
params: options.params,
});
return result;
}

Expand Down Expand Up @@ -77,7 +81,13 @@ export class ProcessesService extends Service {
headers['prefer'] = 'respond-async';
}

const result = await request({ url, headers, params, method: 'POST' });
const result = await request({
url,
headers,
params,
signal: options.signal,
method: 'POST',
});
return result as TExecuteProcessResponse<T>;
}

Expand All @@ -97,7 +107,11 @@ export class ProcessesService extends Service {
if (options.datetime) {
params.datetime = stringifyDatetime(options.datetime);
}
const result: IJobsResponse = await request({ url, params });
const result: IJobsResponse = await request({
url,
params,
signal: options.signal,
});
return result;
}

Expand All @@ -112,7 +126,11 @@ export class ProcessesService extends Service {
): Promise<IJobInfo> {
const url: string = `${this.baseUrl}/jobs/${jobId}`;
const params: IRequestParams = Object.assign({}, options.params);
const result: IJobInfo = await request({ url, params });
const result: IJobInfo = await request({
url,
params,
signal: options.signal,
});
return result;
}

Expand All @@ -130,7 +148,11 @@ export class ProcessesService extends Service {
): Promise<TJobResult> {
const url: string = `${this.baseUrl}/jobs/${jobId}`;
const params: IRequestParams = Object.assign({}, options.params);
const result: TJobResult = await request({ url, params });
const result: TJobResult = await request({
url,
params,
signal: options.signal,
});
return result;
}

Expand All @@ -145,7 +167,12 @@ export class ProcessesService extends Service {
): Promise<IJobInfo> {
const url: string = `${this.baseUrl}/jobs/${jobId}`;
const params: IRequestParams = Object.assign({}, options.params);
const result: IJobInfo = await request({ url, params, method: 'DELETE' });
const result: IJobInfo = await request({
url,
params,
signal: options.signal,
method: 'DELETE',
});
return result;
}
}
Expand Down
13 changes: 12 additions & 1 deletion packages/shared/src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,26 @@ function toJSON(params: IRequestParams) {
const getRequestArgsMap: Record<TRequestMethods, TRequestArgsGetter> = {
GET: ({
url: baseUrl,
signal,
headers = {},
params = {},
}: IRequestOptions): TRequestArgs => {
const url = `${baseUrl}?${toSearchParams(params).toString()}`;
const init = {
signal,
headers,
method: 'GET',
};
return { url, init };
},
POST: ({ url, params = {}, headers = {} }: IRequestOptions): TRequestArgs => {
POST: ({
url,
signal,
params = {},
headers = {},
}: IRequestOptions): TRequestArgs => {
const init = {
signal,
headers,
method: 'POST',
body: toBody(params, headers),
Expand All @@ -81,11 +89,13 @@ const getRequestArgsMap: Record<TRequestMethods, TRequestArgsGetter> = {
},
DELETE: ({
url: baseUrl,
signal,
params = {},
headers = {},
}: IRequestOptions): TRequestArgs => {
const url = `${baseUrl}?${toSearchParams(params).toString()}`;
const init = {
signal,
headers,
method: 'DELETE',
};
Expand Down Expand Up @@ -121,4 +131,5 @@ export interface IRequestOptions {
params?: IRequestParams;
headers?: IRequestHeaders;
method?: TRequestMethods;
signal?: AbortSignal;
}
4 changes: 4 additions & 0 deletions packages/shared/src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ export interface IServiceConfig {
* request options
*/
export interface IServiceRequestOptions {
/**
* abort signal passed to fetch.
*/
signal?: AbortSignal;
/**
* additional request parameters
*/
Expand Down
17 changes: 17 additions & 0 deletions packages/shared/test/unit/request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,20 @@ test('request() should not read body of DELETE request when response status is 2
});
expect(result).toBeUndefined();
});

test('request() should throw AbortError when signal is aborted', async () => {
mockRequest('https://www.example.com?f=json', {
success: true,
});

const controller = new AbortController();
controller.abort();

expect(
async () =>
await request({
url: 'https://www.example.com',
signal: controller.signal,
})
).rejects.toThrow(/abort/i);
});

0 comments on commit a1853ac

Please sign in to comment.