Skip to content

Commit ffbcb9c

Browse files
authored
Merge pull request #13 from jhannes/abort_signal
Support fetch options like abortSignal and cache for all API operations
2 parents d3b2b5d + c1dbe24 commit ffbcb9c

File tree

34 files changed

+401
-177
lines changed

34 files changed

+401
-177
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { servers } from "../../snapshot/infectionTracker";
2+
3+
async function callApi() {
4+
const caseWorkers = await servers.current.caseWorkersApi.listCaseWorkers();
5+
6+
const signal = new AbortSignal();
7+
await servers.current.caseWorkersApi.listCaseWorkers({ signal });
8+
9+
await servers.current.casesApi.newCase({
10+
infectionInformationDto: undefined,
11+
signal,
12+
});
13+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "infectiontracker",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"verify": "prettier --check **/*.{ts,tsx,js,jsx} && tsc"
8+
},
9+
"keywords": [],
10+
"author": "",
11+
"license": "ISC",
12+
"devDependencies": {
13+
"prettier": "^2.7.1",
14+
"typescript": "^4.7.4"
15+
}
16+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"compilerOptions": {
3+
"strict": true,
4+
"declaration": true,
5+
"target": "es2015",
6+
"module": "commonjs",
7+
"noImplicitAny": true,
8+
"outDir": "dist",
9+
"lib": ["es2019", "dom"],
10+
"typeRoots": ["node_modules/@types"],
11+
"noEmit": true
12+
},
13+
"exclude": ["dist", "node_modules"]
14+
}

snapshotTests/consumingProjects/poly/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
/* eslint-disable no-case-declarations */
12
import {UpdateErrorDto} from "../../snapshot/poly";
23

34
function outputError(error: UpdateErrorDto): string {
45
switch (error.code) {
56
case "DuplicateIdentifierError":
67
const {entityType, identifierValue} = error;
78
return `duplicate id ${identifierValue} for ${entityType}`;
8-
case "GeneralErrorError":
9+
case "GeneralError":
910
const {description} = error;
1011
return description;
1112
}

snapshotTests/snapshot/bigExample/api.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717
PetStoreDto,
1818
} from "./model";
1919

20-
import { BaseAPI, SecurityScheme } from "./base";
20+
import { BaseAPI, RequestCallOptions, SecurityScheme } from "./base";
2121

2222
export interface ApplicationApis {
2323
defaultApi: DefaultApiInterface;
@@ -35,7 +35,7 @@ export interface DefaultApiInterface {
3535
addPet(params: {
3636
pathParams: { storeId: string };
3737
petDto?: PetDto;
38-
}): Promise<void>;
38+
} & RequestCallOptions): Promise<void>;
3939
/**
4040
*
4141
* @param {*} [params] Request parameters, including pathParams, queryParams (including bodyParams) and http options.
@@ -44,12 +44,12 @@ export interface DefaultApiInterface {
4444
addPetWithForm(params: {
4545
pathParams: { petId: string };
4646
formParams: { name: string; status: string; }
47-
}): Promise<void>;
47+
} & RequestCallOptions): Promise<void>;
4848
/**
4949
*
5050
* @throws {HttpError}
5151
*/
52-
getPetLocations(): Promise<PetLocationsDto>;
52+
getPetLocations(params?: RequestCallOptions): Promise<PetLocationsDto>;
5353
/**
5454
*
5555
* @param {*} [params] Request parameters, including pathParams, queryParams (including bodyParams) and http options.
@@ -58,7 +58,7 @@ export interface DefaultApiInterface {
5858
listPets(params: {
5959
pathParams: { storeId: string };
6060
queryParams?: { status?: Array<string>, tags?: Array<string>, bornAfter?: Date, };
61-
}): Promise<PetDto>;
61+
} & RequestCallOptions): Promise<PetDto>;
6262
}
6363

6464
/**
@@ -73,13 +73,15 @@ export class DefaultApi extends BaseAPI implements DefaultApiInterface {
7373
public async addPet(params: {
7474
pathParams: { storeId: string };
7575
petDto?: PetDto;
76-
}): Promise<void> {
76+
} & RequestCallOptions): Promise<void> {
7777
return await this.fetch(
7878
this.url("/{storeId}/pets", params.pathParams),
7979
{
80+
...params,
8081
method: "POST",
8182
body: JSON.stringify(params.petDto),
8283
headers: {
84+
...this.removeEmpty(params.headers),
8385
"Content-Type": "application/json",
8486
},
8587
}
@@ -93,13 +95,15 @@ export class DefaultApi extends BaseAPI implements DefaultApiInterface {
9395
public async addPetWithForm(params: {
9496
pathParams: { petId: string };
9597
formParams: { name: string; status: string; }
96-
}): Promise<void> {
98+
} & RequestCallOptions): Promise<void> {
9799
return await this.fetch(
98100
this.url("/pets/{petId}", params.pathParams),
99101
{
102+
...params,
100103
method: "POST",
101104
body: this.formData(params.formParams),
102105
headers: {
106+
...this.removeEmpty(params.headers),
103107
"Content-Type": "application/x-www-form-urlencoded",
104108
},
105109
}
@@ -109,9 +113,9 @@ export class DefaultApi extends BaseAPI implements DefaultApiInterface {
109113
*
110114
* @throws {HttpError}
111115
*/
112-
public async getPetLocations(): Promise<PetLocationsDto> {
116+
public async getPetLocations(params: RequestCallOptions = {}): Promise<PetLocationsDto> {
113117
return await this.fetch(
114-
this.basePath + "/pet/locations"
118+
this.basePath + "/pet/locations", params
115119
);
116120
}
117121
/**
@@ -122,12 +126,12 @@ export class DefaultApi extends BaseAPI implements DefaultApiInterface {
122126
public async listPets(params: {
123127
pathParams: { storeId: string };
124128
queryParams?: { status?: Array<string>, tags?: Array<string>, bornAfter?: Date, };
125-
}): Promise<PetDto> {
129+
} & RequestCallOptions): Promise<PetDto> {
126130
return await this.fetch(
127131
this.url("/{storeId}/pets", params.pathParams, params?.queryParams, {
128132
status: { delimiter: " " },
129133
bornAfter: { format: "date" },
130-
})
134+
}), params
131135
);
132136
}
133137
}

snapshotTests/snapshot/bigExample/base.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,35 @@
1616
export interface RequestOptions {
1717
mode?: RequestMode;
1818
headers?: Record<string, string>;
19+
cache?: RequestCache;
1920
credentials?: RequestCredentials;
2021
referrer?: string;
2122
referrerPolicy?: ReferrerPolicy;
2223
}
2324

25+
export type RequestCallOptions = RequestOptions & {
26+
signal?: AbortSignal | null;
27+
};
28+
2429
export class BaseAPI {
2530
constructor(
2631
protected basePath: string = window.location.origin,
2732
protected requestOptions?: RequestOptions
2833
) {}
2934

30-
protected async fetch(url: string, options: RequestInit = {}): Promise<any> {
35+
protected async fetch(
36+
url: string,
37+
options: RequestCallOptions & { method?: string; body?: string } = {}
38+
): Promise<any> {
3139
const result = await fetch(url, {
32-
credentials: this.requestOptions?.credentials || "same-origin",
33-
mode: this.requestOptions?.mode,
34-
...options,
40+
credentials: options.credentials || this.requestOptions?.credentials || "same-origin",
41+
mode: options.mode || this.requestOptions?.mode,
42+
method: options.method,
43+
body: options.body,
44+
cache: options.cache || this.requestOptions?.cache,
45+
referrer: options.referrer || this.requestOptions?.referrer,
46+
referrerPolicy: options.referrerPolicy || this.requestOptions?.referrerPolicy,
47+
signal: options.signal,
3548
headers: {
3649
...(this.requestOptions?.headers || {}),
3750
...options.headers,

snapshotTests/snapshot/bigExample/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"build:typescript": "tsc --outDir dist/",
2020
"lint": "eslint . --ext .ts",
2121
"format": "prettier --write *.ts test/*.ts *.json",
22+
"verify": "npm run lint && tsc --noEmit",
2223
"prepublishOnly": "npm run build"
2324
},
2425
"devDependencies": {

snapshotTests/snapshot/example/api.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
PetTypeDto,
1717
} from "./model";
1818

19-
import { BaseAPI, SecurityScheme } from "./base";
19+
import { BaseAPI, RequestCallOptions, SecurityScheme } from "./base";
2020

2121
export interface ApplicationApis {
2222
defaultApi: DefaultApiInterface;
@@ -34,7 +34,7 @@ export interface DefaultApiInterface {
3434
addPet(params: {
3535
pathParams: { storeId: string };
3636
petDto?: PetDto;
37-
}): Promise<void>;
37+
} & RequestCallOptions): Promise<void>;
3838
/**
3939
*
4040
* @param {*} [params] Request parameters, including pathParams, queryParams (including bodyParams) and http options.
@@ -43,7 +43,7 @@ export interface DefaultApiInterface {
4343
addPetWithForm(params: {
4444
pathParams: { petId: string };
4545
formParams: { name: string; status: string; }
46-
}): Promise<void>;
46+
} & RequestCallOptions): Promise<void>;
4747
/**
4848
*
4949
* @param {*} [params] Request parameters, including pathParams, queryParams (including bodyParams) and http options.
@@ -52,7 +52,7 @@ export interface DefaultApiInterface {
5252
listPets(params: {
5353
pathParams: { storeId: string };
5454
queryParams?: { status?: Array<string>, tags?: Array<string>, bornAfter?: Date, };
55-
}): Promise<PetDto>;
55+
} & RequestCallOptions): Promise<PetDto>;
5656
}
5757

5858
/**
@@ -67,13 +67,15 @@ export class DefaultApi extends BaseAPI implements DefaultApiInterface {
6767
public async addPet(params: {
6868
pathParams: { storeId: string };
6969
petDto?: PetDto;
70-
}): Promise<void> {
70+
} & RequestCallOptions): Promise<void> {
7171
return await this.fetch(
7272
this.url("/{storeId}/pets", params.pathParams),
7373
{
74+
...params,
7475
method: "POST",
7576
body: JSON.stringify(params.petDto),
7677
headers: {
78+
...this.removeEmpty(params.headers),
7779
"Content-Type": "application/json",
7880
},
7981
}
@@ -87,13 +89,15 @@ export class DefaultApi extends BaseAPI implements DefaultApiInterface {
8789
public async addPetWithForm(params: {
8890
pathParams: { petId: string };
8991
formParams: { name: string; status: string; }
90-
}): Promise<void> {
92+
} & RequestCallOptions): Promise<void> {
9193
return await this.fetch(
9294
this.url("/pets/{petId}", params.pathParams),
9395
{
96+
...params,
9497
method: "POST",
9598
body: this.formData(params.formParams),
9699
headers: {
100+
...this.removeEmpty(params.headers),
97101
"Content-Type": "application/x-www-form-urlencoded",
98102
},
99103
}
@@ -107,12 +111,12 @@ export class DefaultApi extends BaseAPI implements DefaultApiInterface {
107111
public async listPets(params: {
108112
pathParams: { storeId: string };
109113
queryParams?: { status?: Array<string>, tags?: Array<string>, bornAfter?: Date, };
110-
}): Promise<PetDto> {
114+
} & RequestCallOptions): Promise<PetDto> {
111115
return await this.fetch(
112116
this.url("/{storeId}/pets", params.pathParams, params?.queryParams, {
113117
status: { delimiter: " " },
114118
bornAfter: { format: "date" },
115-
})
119+
}), params
116120
);
117121
}
118122
}

snapshotTests/snapshot/example/base.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,35 @@
1616
export interface RequestOptions {
1717
mode?: RequestMode;
1818
headers?: Record<string, string>;
19+
cache?: RequestCache;
1920
credentials?: RequestCredentials;
2021
referrer?: string;
2122
referrerPolicy?: ReferrerPolicy;
2223
}
2324

25+
export type RequestCallOptions = RequestOptions & {
26+
signal?: AbortSignal | null;
27+
};
28+
2429
export class BaseAPI {
2530
constructor(
2631
protected basePath: string = window.location.origin,
2732
protected requestOptions?: RequestOptions
2833
) {}
2934

30-
protected async fetch(url: string, options: RequestInit = {}): Promise<any> {
35+
protected async fetch(
36+
url: string,
37+
options: RequestCallOptions & { method?: string; body?: string } = {}
38+
): Promise<any> {
3139
const result = await fetch(url, {
32-
credentials: this.requestOptions?.credentials || "same-origin",
33-
mode: this.requestOptions?.mode,
34-
...options,
40+
credentials: options.credentials || this.requestOptions?.credentials || "same-origin",
41+
mode: options.mode || this.requestOptions?.mode,
42+
method: options.method,
43+
body: options.body,
44+
cache: options.cache || this.requestOptions?.cache,
45+
referrer: options.referrer || this.requestOptions?.referrer,
46+
referrerPolicy: options.referrerPolicy || this.requestOptions?.referrerPolicy,
47+
signal: options.signal,
3548
headers: {
3649
...(this.requestOptions?.headers || {}),
3750
...options.headers,

snapshotTests/snapshot/example/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"build:typescript": "tsc --outDir dist/",
2020
"lint": "eslint . --ext .ts",
2121
"format": "prettier --write *.ts test/*.ts *.json",
22+
"verify": "npm run lint && tsc --noEmit",
2223
"prepublishOnly": "npm run build"
2324
},
2425
"devDependencies": {

0 commit comments

Comments
 (0)