Skip to content

Commit

Permalink
fix: fetch arg passing
Browse files Browse the repository at this point in the history
  • Loading branch information
d-goog committed Feb 15, 2025
1 parent d8e79bb commit 8db2694
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
9 changes: 6 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@ import {getAgent} from './agents';
import {TeenyStatistics} from './TeenyStatistics';
// eslint-disable-next-line @typescript-eslint/no-var-requires
const streamEvents = require('stream-events');
const fetch = (...args: any[]) =>
import('node-fetch').then(({default: fetch}) => fetch([...args] as any));

import type nodeFetch from 'node-fetch' with {'resolution-mode': 'import'};

const fetch = (...args: Parameters<typeof nodeFetch>) =>
import('node-fetch').then(({default: fetch}) => fetch(...args));

export interface CoreOptions {
method?: string;
Expand Down Expand Up @@ -154,7 +157,7 @@ function fetchToRequestResponse(opts: f.RequestInit, res: f.Response) {
const resHeaders = {} as Headers;
res.headers.forEach((value, key) => (resHeaders[key] = value));

const response = Object.assign(res.body as any, {
const response = Object.assign(res.body as {}, {
statusCode: res.status,
statusMessage: res.statusText,
request,
Expand Down
33 changes: 17 additions & 16 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import assert from 'assert';
import {describe, it, afterEach, beforeEach} from 'mocha';
import nock from 'nock';
import {Readable, PassThrough} from 'stream';
import {Readable} from 'stream';
import * as sinon from 'sinon';
import {teenyRequest} from '../src';
import {TeenyStatistics, TeenyStatisticsWarning} from '../src/TeenyStatistics';
Expand Down Expand Up @@ -220,18 +220,19 @@ describe('teeny', () => {
});

// see: https://github.com/googleapis/nodejs-storage/issues/798
it.only('should not throw exception when piped through pumpify', async () => {
it('should not throw exception when piped through pumpify', async () => {
const scope = mockJson();
console.log('A');
const stream = teenyRequest({uri}).pipe(new PassThrough());
console.log(stream);
let content = '';
const stream = teenyRequest({uri});
// set the encoding for the returned stream
stream.setEncoding('utf8');

// collect the buffers, then concat later for performance
const content: string[] = [];
for await (const data of stream) {
console.log(data);
content += data;
content.push(data);
}
console.log(content);
assert.deepStrictEqual(JSON.parse(content), {hello: '🌍'});

assert.deepStrictEqual(JSON.parse(content.join('')), {hello: '🌍'});
scope.done();
});

Expand Down Expand Up @@ -361,7 +362,7 @@ describe('teeny', () => {
assert.strictEqual(response.statusCode, 200);
assert.strictEqual(body, '🌍');
scope.done();
},
}

Check failure on line 365 in test/index.ts

View workflow job for this annotation

GitHub Actions / lint

Insert `,`
);
});

Expand All @@ -374,7 +375,7 @@ describe('teeny', () => {
assert.strictEqual(response.statusCode, 200);
assert.strictEqual(body, '🌍');
scope.done();
},
}

Check failure on line 378 in test/index.ts

View workflow job for this annotation

GitHub Actions / lint

Insert `,`
);
});

Expand Down Expand Up @@ -405,7 +406,7 @@ describe('teeny', () => {
assert.ok(statsStub.requestFinished.calledOnceWithExactly());
scope.done();
done();
},
}

Check failure on line 409 in test/index.ts

View workflow job for this annotation

GitHub Actions / lint

Insert `,`
);
});

Expand All @@ -423,7 +424,7 @@ describe('teeny', () => {
assert.ok(statsStub.requestStarting.calledOnceWithExactly());
assert.ok(statsStub.requestFinished.calledOnceWithExactly());
scope.done();
},
}

Check failure on line 427 in test/index.ts

View workflow job for this annotation

GitHub Actions / lint

Insert `,`
);
});

Expand All @@ -433,7 +434,7 @@ describe('teeny', () => {
teenyRequest({uri: ''});
},
/Missing uri or url in reqOpts/,
'Did not throw with expected message',
'Did not throw with expected message'

Check failure on line 437 in test/index.ts

View workflow job for this annotation

GitHub Actions / lint

Insert `,`
);
});

Expand All @@ -443,7 +444,7 @@ describe('teeny', () => {
teenyRequest({url: ''});
},
/Missing uri or url in reqOpts/,
'Did not throw with expected message',
'Did not throw with expected message'

Check failure on line 447 in test/index.ts

View workflow job for this annotation

GitHub Actions / lint

Insert `,`
);
});
});

0 comments on commit 8db2694

Please sign in to comment.