Skip to content

Commit

Permalink
node-fetchを辞めて、nodejs nativeのfetch apiを利用します
Browse files Browse the repository at this point in the history
  • Loading branch information
kahirokunn committed Mar 12, 2024
1 parent e2a2578 commit 3680360
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 73 deletions.
49 changes: 40 additions & 9 deletions examples/generate-all-k8s-client/k8s-client/client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as k8s from '@kubernetes/client-node'
import fetch, { FetchError } from 'node-fetch'
import * as https from 'node:https'
import type * as https from 'node:https'

import { Agent } from 'undici'

type RemoveUndefined<T> = {
[K in keyof T]: Exclude<T[K], undefined | null>
Expand Down Expand Up @@ -116,7 +117,13 @@ export async function apiClient<Response>(
return false
}

if (error instanceof FetchError) {
if (
typeof error === 'object' &&
error !== null &&
'toString' in error &&
typeof error.toString === 'function' &&
error.toString().includes('TypeError: fetch failed')
) {
return true
}
if (res && res.status >= 500) {
Expand Down Expand Up @@ -158,15 +165,16 @@ export async function apiClient<Response>(
!httpsOptions.agent &&
(httpsOptions.ca || httpsOptions.cert || httpsOptions.key)
) {
const agent = new https.Agent(
removeNullableProperties({
const agent = new Agent({
connect: removeNullableProperties({
ca: httpsOptions.ca,
cert: httpsOptions.cert,
key: httpsOptions.key,
port: httpsOptions.port ? Number(httpsOptions.port) : undefined,
})
)
httpsOptions.agent = agent
}),
})
// https://github.com/nodejs/node/issues/48977
httpsOptions.agent = agent as any
}

if (!httpsOptions.protocol) {
Expand Down Expand Up @@ -208,7 +216,8 @@ export async function apiClient<Response>(
headers,
protocol: httpsOptions.protocol || undefined,
method,
agent: httpsOptions.agent,
// https://github.com/nodejs/node/issues/48977
dispatcher: httpsOptions.agent,
body,
})
)
Expand All @@ -218,6 +227,28 @@ export async function apiClient<Response>(
const isJsonResponse = contentType?.includes('application/json') ?? false

if (isSuccess && isJsonResponse) {
if ('watch' in params && params.watch && response.body) {
const reader = response.body.getReader()
const textDecoder = new TextDecoder()
let buffer = ''
while (true) {
const { value, done } = await reader.read()
if (done) break

buffer += textDecoder.decode(value, { stream: true })
while (true) {
const newlineIndex = buffer.indexOf('\n')
if (newlineIndex === -1) break
const line = buffer.slice(0, newlineIndex)
buffer = buffer.slice(newlineIndex + 1)

console.log('----------------')
console.log(JSON.parse(line))
}
}

return JSON.parse(await response.text()) as Response
}
return (await response.json()) as Response
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
"@types/jest": "^27",
"@types/lodash": "^4.14.165",
"@types/node": "^14.14.12",
"@types/node-fetch": "^3",
"@types/prettier": "^2.0.0",
"@types/semver": "^7.3.9",
"babel-jest": "^26.6.3",
Expand All @@ -49,12 +48,12 @@
"husky": "^4.3.6",
"jest": "^27",
"msw": "^0.40.2",
"node-fetch": "^3.3.2",
"openapi-types": "^9.1.0",
"pretty-quick": "^3.1.0",
"ts-jest": "^27",
"ts-node": "^10.4.0",
"typescript": "^5.2.2",
"undici": "^6.7.1",
"yalc": "^1.0.0-pre.47"
},
"dependencies": {
Expand Down
70 changes: 8 additions & 62 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2473,15 +2473,6 @@ __metadata:
languageName: node
linkType: hard

"@types/node-fetch@npm:^3":
version: 3.0.2
resolution: "@types/node-fetch@npm:3.0.2"
dependencies:
node-fetch: "*"
checksum: b9f97bc4219c37f70299133b36389719cf4adacad46cea80df70f77d4a9f6999ea5be18a1ab90b34239b684cae032adeec10756cce8d2ae81c17f5e4c56fd4e9
languageName: node
linkType: hard

"@types/node@npm:*":
version: 17.0.45
resolution: "@types/node@npm:17.0.45"
Expand Down Expand Up @@ -4137,13 +4128,6 @@ __metadata:
languageName: node
linkType: hard

"data-uri-to-buffer@npm:^4.0.0":
version: 4.0.1
resolution: "data-uri-to-buffer@npm:4.0.1"
checksum: 0d0790b67ffec5302f204c2ccca4494f70b4e2d940fea3d36b09f0bb2b8539c2e86690429eb1f1dc4bcc9e4df0644193073e63d9ee48ac9fce79ec1506e4aa4c
languageName: node
linkType: hard

"data-urls@npm:^2.0.0":
version: 2.0.0
resolution: "data-urls@npm:2.0.0"
Expand Down Expand Up @@ -5361,16 +5345,6 @@ __metadata:
languageName: node
linkType: hard

"fetch-blob@npm:^3.1.2, fetch-blob@npm:^3.1.4":
version: 3.2.0
resolution: "fetch-blob@npm:3.2.0"
dependencies:
node-domexception: ^1.0.0
web-streams-polyfill: ^3.0.3
checksum: f19bc28a2a0b9626e69fd7cf3a05798706db7f6c7548da657cbf5026a570945f5eeaedff52007ea35c8bcd3d237c58a20bf1543bc568ab2422411d762dd3d5bf
languageName: node
linkType: hard

"figures@npm:^3.0.0":
version: 3.2.0
resolution: "figures@npm:3.2.0"
Expand Down Expand Up @@ -5530,15 +5504,6 @@ __metadata:
languageName: node
linkType: hard

"formdata-polyfill@npm:^4.0.10":
version: 4.0.10
resolution: "formdata-polyfill@npm:4.0.10"
dependencies:
fetch-blob: ^3.1.2
checksum: 82a34df292afadd82b43d4a740ce387bc08541e0a534358425193017bf9fb3567875dc5f69564984b1da979979b70703aa73dee715a17b6c229752ae736dd9db
languageName: node
linkType: hard

"fragment-cache@npm:^0.2.1":
version: 0.2.1
resolution: "fragment-cache@npm:0.2.1"
Expand Down Expand Up @@ -7735,7 +7700,6 @@ __metadata:
"@types/jest": ^27
"@types/lodash": ^4.14.165
"@types/node": ^14.14.12
"@types/node-fetch": ^3
"@types/prettier": ^2.0.0
"@types/semver": ^7.3.9
babel-jest: ^26.6.3
Expand All @@ -7747,7 +7711,6 @@ __metadata:
husky: ^4.3.6
jest: ^27
msw: ^0.40.2
node-fetch: ^3.3.2
openapi-types: ^9.1.0
prettier: ^2.8.8
pretty-quick: ^3.1.0
Expand All @@ -7756,6 +7719,7 @@ __metadata:
ts-jest: ^27
ts-node: ^10.4.0
typescript: ^5.2.2
undici: ^6.7.1
yalc: ^1.0.0-pre.47
bin:
kubernetes-typescript-client-codegen-openapi: lib/bin/cli.js
Expand Down Expand Up @@ -8377,13 +8341,6 @@ __metadata:
languageName: node
linkType: hard

"node-domexception@npm:^1.0.0":
version: 1.0.0
resolution: "node-domexception@npm:1.0.0"
checksum: ee1d37dd2a4eb26a8a92cd6b64dfc29caec72bff5e1ed9aba80c294f57a31ba4895a60fd48347cf17dd6e766da0ae87d75657dfd1f384ebfa60462c2283f5c7f
languageName: node
linkType: hard

"node-fetch-h2@npm:^2.3.0":
version: 2.3.0
resolution: "node-fetch-h2@npm:2.3.0"
Expand All @@ -8393,17 +8350,6 @@ __metadata:
languageName: node
linkType: hard

"node-fetch@npm:*, node-fetch@npm:^3.3.2":
version: 3.3.2
resolution: "node-fetch@npm:3.3.2"
dependencies:
data-uri-to-buffer: ^4.0.0
fetch-blob: ^3.1.4
formdata-polyfill: ^4.0.10
checksum: 06a04095a2ddf05b0830a0d5302699704d59bda3102894ea64c7b9d4c865ecdff2d90fd042df7f5bc40337266961cb6183dcc808ea4f3000d024f422b462da92
languageName: node
linkType: hard

"node-fetch@npm:^2.6.1, node-fetch@npm:^2.6.7":
version: 2.6.7
resolution: "node-fetch@npm:2.6.7"
Expand Down Expand Up @@ -11160,6 +11106,13 @@ __metadata:
languageName: node
linkType: hard

"undici@npm:^6.7.1":
version: 6.7.1
resolution: "undici@npm:6.7.1"
checksum: 60380d69480e1dbc5e567b12234cd7dd3ee8d487d572bcc746ce0de717b6d98f78daec46de9df6dd9eeae7b41b7175837e9b29d02b8c2d38ca81c08cc4ad5b41
languageName: node
linkType: hard

"unicode-canonical-property-names-ecmascript@npm:^2.0.0":
version: 2.0.0
resolution: "unicode-canonical-property-names-ecmascript@npm:2.0.0"
Expand Down Expand Up @@ -11436,13 +11389,6 @@ __metadata:
languageName: node
linkType: hard

"web-streams-polyfill@npm:^3.0.3":
version: 3.3.3
resolution: "web-streams-polyfill@npm:3.3.3"
checksum: 21ab5ea08a730a2ef8023736afe16713b4f2023ec1c7085c16c8e293ee17ed085dff63a0ad8722da30c99c4ccbd4ccd1b2e79c861829f7ef2963d7de7004c2cb
languageName: node
linkType: hard

"webidl-conversions@npm:^3.0.0":
version: 3.0.1
resolution: "webidl-conversions@npm:3.0.1"
Expand Down

0 comments on commit 3680360

Please sign in to comment.