Skip to content

Commit

Permalink
Replace node-fetch with nodejs fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
akiradev committed Jan 9, 2025
1 parent f7b2094 commit 349dfdc
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 38 deletions.
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
"@types/concat-stream": "^2.0.0",
"@types/jest": "^26.0.8",
"@types/node": "^22.5.4",
"@types/node-fetch": "^2.5.7",
"babel-jest": "^29.7.0",
"husky": "^8.0.3",
"jest": "^29.7.0",
Expand Down Expand Up @@ -66,7 +65,6 @@
"file-type": "^16.5.4",
"form-data": "^4.0.0",
"map-obj": "^4.3.0",
"node-fetch": "^3.3.2",
"openapi-types": "^12.1.3",
"strickland": "^2.2.0"
},
Expand Down
60 changes: 24 additions & 36 deletions src/clientFactory.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { backOff } from 'exponential-backoff';
import camelcase from 'camelcase';
import fetch, { RequestInit, HeadersInit } from 'node-fetch';
import http, { request } from 'http';
import https from 'https';
import { OpenAPI, OpenAPIV3 } from 'openapi-types';
Expand Down Expand Up @@ -217,15 +216,14 @@ const buildRequestArgs = async (
client: Client | PromiseLike<Client>,
headers: Headers,
method: Method,
agent: http.Agent,
obj: string,
op: string,
body: any,
operation: Operation,
logger: LoggerFunc,
originalBody: any
) => {
let requestArgs: RequestInit = { headers, method, agent };
let requestArgs: RequestInit = { headers, method, keepalive: true, };
let schema = operation.bodySchema;

if (schema == undefined || !schema) {
Expand Down Expand Up @@ -351,10 +349,6 @@ export async function buildClient(opts: ClientFactoryOptions): Promise<Client> {
let host = opts.host || 'api.adzerk.net';
let port = opts.port || 443;

let agent = new (protocol === 'https' ? https : http).Agent({
keepAlive: true,
});

return {
async run(obj, op, body, qOpts) {
let originalBody = body ? JSON.parse(JSON.stringify(body)) : null;
Expand Down Expand Up @@ -407,7 +401,6 @@ export async function buildClient(opts: ClientFactoryOptions): Promise<Client> {
this,
headers,
operation.method,
agent,
obj,
op,
body,
Expand Down Expand Up @@ -496,7 +489,7 @@ export async function buildClient(opts: ClientFactoryOptions): Promise<Client> {
}

let handleResponseStream = async <TAcc>({ body, callback, initialValue }: {
body: NodeJS.ReadableStream | null,
body: ReadableStream | null,
callback: any,
initialValue: TAcc
}) => {
Expand All @@ -506,35 +499,30 @@ let handleResponseStream = async <TAcc>({ body, callback, initialValue }: {
let accumulator = initialValue;

if (body !== null) {
body.on('data', (d) => {
buffer = Buffer.concat([buffer, d]);
try {
buffer
.toString()
.trim()
.split('\n')
.forEach((l) => {
try {
let obj = convertKeysToCamelcase(JSON.parse(l));
accumulator = callback(accumulator, obj);
buffer = Buffer.alloc(0);
} catch {
buffer = Buffer.from(l);
return;
}
});
} catch (e) {
reject(e);
body.getReader().read().then(({ done, value }) => {
if (done) {
resolve(accumulator);
}
});

body.on('end', () => resolve(accumulator));

body.on('close', () => resolve(accumulator));

body.on('finish', () => resolve(accumulator));

body.on('error', (e) => reject(e));
buffer = Buffer.concat([buffer, value]);

buffer
.toString()
.trim()
.split('\n')
.forEach((l) => {
try {
let obj = convertKeysToCamelcase(JSON.parse(l));
accumulator = callback(accumulator, obj);
buffer = Buffer.alloc(0);
} catch {
buffer = Buffer.from(l);
return;
}
});
}).catch(e => {
reject(e);
})
}
else {
reject('Provided body was null.');
Expand Down

0 comments on commit 349dfdc

Please sign in to comment.