Skip to content

Commit b23544d

Browse files
Himenonclaude
andcommitted
chore(examples/apis): upgrade dependencies and refactor API client usage
fix(examples/apis): fix type errors caused by TypeScript 6.0 and updated package APIs - Add types: ["node"] to tsconfig.json for @types/node resolution under moduleResolution: "bundler" - Update all sample files to use RequestArgs object signature for ApiClient.request() - Replace new Client<T>() with createClient<T>() factory function - Fix superagent import to use default import (import superagent from "superagent") - Remove unsupported timeout option from node-fetch v3 request options - Update dependencies: axios 1.15.2, superagent 10.3.0, typescript 6.0.3 Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent eb0b5fb commit b23544d

7 files changed

Lines changed: 252 additions & 240 deletions

File tree

examples/apis/package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@
1111
"ts": "node --no-warnings=ExperimentalWarning --experimental-specifier-resolution=node --loader ts-node/esm"
1212
},
1313
"dependencies": {
14-
"@himenon/openapi-parameter-formatter": "0.3.1",
14+
"@himenon/openapi-parameter-formatter": "2.1.0",
1515
"@himenon/openapi-typescript-code-generator": "link:../..",
16-
"axios": "1.6.7",
17-
"superagent": "8.1.2"
16+
"axios": "1.15.2",
17+
"superagent": "10.3.0"
1818
},
1919
"devDependencies": {
20-
"@types/node-fetch": "^2.6.12",
20+
"@types/node-fetch": "^2.6.13",
2121
"@types/superagent": "^8.1.9",
2222
"node-fetch": "^3.3.2",
2323
"ts-node": "10.9.2",
24-
"typescript": "5.3.3"
24+
"typescript": "6.0.3"
2525
}
2626
}

examples/apis/pnpm-lock.yaml

Lines changed: 224 additions & 173 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/apis/sample-axios.ts

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as axios from "axios";
22

3-
import { ApiClient, Client, HttpMethod, ObjectLike, QueryParameters } from "./client";
3+
import { ApiClient, RequestArgs, createClient } from "./client";
44
import { generateQueryString } from "./utils";
55

66
export interface RequestOption {
@@ -9,34 +9,14 @@ export interface RequestOption {
99
deadline?: number;
1010
}
1111

12-
const convertHttpMethodToAxiosMethod = (httpMethod: HttpMethod): axios.Method => {
13-
const patterns: { [key in HttpMethod]: axios.Method } = {
14-
GET: "GET",
15-
PUT: "PUT",
16-
POST: "POST",
17-
DELETE: "DELETE",
18-
OPTIONS: "OPTIONS",
19-
HEAD: "HEAD",
20-
PATCH: "PATCH",
21-
TRACE: "POST", // ?
22-
};
23-
return patterns[httpMethod];
24-
};
25-
2612
const apiClientImpl: ApiClient<RequestOption> = {
27-
request: async (
28-
httpMethod: HttpMethod,
29-
url: string,
30-
headers: ObjectLike | any,
31-
requestBody: ObjectLike | any,
32-
queryParameters: QueryParameters | undefined,
33-
options?: RequestOption,
34-
): Promise<any> => {
13+
request: async (requestArgs: RequestArgs, options?: RequestOption): Promise<any> => {
14+
const { httpMethod, url, headers, requestBody, queryParameters } = requestArgs;
3515
const query = generateQueryString(queryParameters);
3616
const requestUrl = query ? `${url}?${encodeURI(query)}` : url;
3717
const response = await axios.default.request({
3818
url: requestUrl,
39-
method: convertHttpMethodToAxiosMethod(httpMethod),
19+
method: httpMethod,
4020
headers,
4121
data: requestBody,
4222
timeout: options?.timeout,
@@ -46,7 +26,7 @@ const apiClientImpl: ApiClient<RequestOption> = {
4626
};
4727

4828
const main = async () => {
49-
const client = new Client<RequestOption>(apiClientImpl, "https://example.com");
29+
const client = createClient<RequestOption>(apiClientImpl, "https://example.com");
5030
await client.getBooks({
5131
retries: 50,
5232
});

examples/apis/sample-debug.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
1-
import { ApiClient, Client, HttpMethod, ObjectLike, QueryParameters } from "./client";
1+
import { ApiClient, RequestArgs, createClient } from "./client";
22
import { generateQueryString } from "./utils";
33

44
export interface RequestOption {
55
timeout?: number;
66
}
77

88
const apiClientImpl: ApiClient<RequestOption> = {
9-
request: async (
10-
httpMethod: HttpMethod,
11-
url: string,
12-
headers: ObjectLike | any,
13-
requestBody: ObjectLike | any,
14-
queryParameters: QueryParameters | undefined,
15-
options?: RequestOption,
16-
): Promise<any> => {
9+
request: async (requestArgs: RequestArgs, options?: RequestOption): Promise<any> => {
10+
const { httpMethod, url, headers, requestBody, queryParameters } = requestArgs;
1711
const query = generateQueryString(queryParameters);
1812
const requestUrl = query ? `${url}?${encodeURI(query)}` : url;
1913
console.log({
@@ -30,7 +24,7 @@ const apiClientImpl: ApiClient<RequestOption> = {
3024
};
3125

3226
const main = async () => {
33-
const client = new Client<RequestOption>(apiClientImpl, "https://example.com");
27+
const client = createClient<RequestOption>(apiClientImpl, "https://example.com");
3428
await client.getBooks({
3529
timeout: 1000,
3630
});

examples/apis/sample-fetch.ts

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,28 @@
11
import fetch from "node-fetch";
22

3-
import { ApiClient, Client, HttpMethod, ObjectLike, QueryParameters } from "./client";
3+
import { ApiClient, RequestArgs, createClient } from "./client";
44
import { generateQueryString } from "./utils";
55

66
export interface RequestOption {
77
timeout?: number;
88
}
99

1010
const apiClientImpl: ApiClient<RequestOption> = {
11-
request: async (
12-
httpMethod: HttpMethod,
13-
url: string,
14-
headers: ObjectLike | any,
15-
requestBody: ObjectLike | any,
16-
queryParameters: QueryParameters | undefined,
17-
options?: RequestOption,
18-
): Promise<any> => {
11+
request: async (requestArgs: RequestArgs, _options?: RequestOption): Promise<any> => {
12+
const { httpMethod, url, headers, requestBody, queryParameters } = requestArgs;
1913
const query = generateQueryString(queryParameters);
2014
const requestUrl = query ? `${url}?${encodeURI(query)}` : url;
2115
const response = await fetch(requestUrl, {
22-
body: JSON.stringify(requestBody),
23-
headers,
16+
body: requestBody !== undefined ? JSON.stringify(requestBody) : undefined,
17+
headers: headers as Record<string, string>,
2418
method: httpMethod,
25-
timeout: options?.timeout,
2619
});
2720
return await response.json();
2821
},
2922
};
3023

3124
const main = async () => {
32-
const client = new Client<RequestOption>(apiClientImpl, "https://example.com");
25+
const client = createClient<RequestOption>(apiClientImpl, "https://example.com");
3326
await client.getBooks({
3427
timeout: 1000,
3528
});

examples/apis/sample-superagent.ts

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import * as Superagent from "superagent";
1+
import superagent from "superagent";
22

3-
import { ApiClient, Client, HttpMethod, ObjectLike, QueryParameters } from "./client";
3+
import { ApiClient, RequestArgs, createClient } from "./client";
44
import { generateQueryString } from "./utils";
55

66
export interface RequestOption {
@@ -10,20 +10,13 @@ export interface RequestOption {
1010
}
1111

1212
const apiClientImpl: ApiClient<RequestOption> = {
13-
request: (
14-
httpMethod: HttpMethod,
15-
url: string,
16-
headers: ObjectLike | any,
17-
requestBody: ObjectLike | any,
18-
queryParameters: QueryParameters | undefined,
19-
options?: RequestOption,
20-
): Promise<any> => {
13+
request: (requestArgs: RequestArgs, options?: RequestOption): Promise<any> => {
14+
const { httpMethod, url, headers, requestBody, queryParameters } = requestArgs;
2115
const query = generateQueryString(queryParameters);
2216
const requestUrl = query ? `${url}?${encodeURI(query)}` : url;
2317

2418
return new Promise((resolve, reject) => {
25-
const agent = Superagent;
26-
const request = agent(httpMethod, requestUrl);
19+
const request = superagent(httpMethod, requestUrl);
2720
if (headers) {
2821
request.set(headers);
2922
}
@@ -48,7 +41,7 @@ const apiClientImpl: ApiClient<RequestOption> = {
4841
};
4942

5043
const main = async () => {
51-
const client = new Client<RequestOption>(apiClientImpl, "https://example.com");
44+
const client = createClient<RequestOption>(apiClientImpl, "https://example.com");
5245
await client.getBooks({
5346
retries: 50,
5447
});

examples/apis/tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
"compilerOptions": {
33
"module": "esnext",
44
"declaration": false,
5-
"moduleResolution": "node"
5+
"moduleResolution": "bundler",
6+
"types": ["node"]
67
},
78
"include": ["."],
89
"exclude": ["node_modules"],

0 commit comments

Comments
 (0)