Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore!: clean up the Transporter interface. #1407

Merged
merged 7 commits into from
Jul 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 5 additions & 34 deletions src/transporters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,6 @@ const PRODUCT_NAME = 'google-api-nodejs-client';

export interface Transporter {
request<T>(opts: GaxiosOptions): GaxiosPromise<T>;
request<T>(opts: GaxiosOptions, callback?: BodyResponseCallback<T>): void;
request<T>(
opts: GaxiosOptions,
callback?: BodyResponseCallback<T>
): GaxiosPromise | void;
}

export interface BodyResponseCallback<T> {
Expand Down Expand Up @@ -82,38 +77,14 @@ export class DefaultTransporter implements Transporter {
* @param callback optional callback that contains GaxiosResponse object.
* @return GaxiosPromise, assuming no callback is passed.
*/
request<T>(opts: GaxiosOptions): GaxiosPromise<T>;
request<T>(opts: GaxiosOptions, callback?: BodyResponseCallback<T>): void;
request<T>(
opts: GaxiosOptions,
callback?: BodyResponseCallback<T>
): GaxiosPromise | void {
request<T>(opts: GaxiosOptions): GaxiosPromise<T> {
// ensure the user isn't passing in request-style options
opts = this.configure(opts);
try {
validate(opts);
} catch (e) {
if (callback) {
return callback(e as Error);
} else {
throw e;
}
}
validate(opts);

if (callback) {
request<T>(opts).then(
r => {
callback(null, r);
},
e => {
callback(this.processError(e));
}
);
} else {
return request<T>(opts).catch(e => {
throw this.processError(e);
});
}
return request<T>(opts).catch(e => {
throw this.processError(e);
});
}

/**
Expand Down
61 changes: 32 additions & 29 deletions test/test.transporters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,35 +89,33 @@
const scope = nock(url)
.get('/')
.reply(400, {error: {code: 500, errors: [firstError, secondError]}});
transporter.request({url}, error => {
scope.done();
assert.strictEqual(error!.message, 'Error 1\nError 2');
assert.strictEqual((error as RequestError).code, 500);
assert.strictEqual((error as RequestError).errors.length, 2);
done();
});
transporter.request({url}).then(
() => {
scope.done();
done('Unexpected promise success');
},
error => {
scope.done();
assert.strictEqual(error!.message, 'Error 1\nError 2');
assert.strictEqual((error as RequestError).code, 500);
assert.strictEqual((error as RequestError).errors.length, 2);
done();
}
);
});

it('should return an error for a 404 response', done => {
const url = 'http://example.com';
const scope = nock(url).get('/').reply(404, 'Not found');
transporter.request({url}, error => {
scope.done();
assert.strictEqual(error!.message, 'Not found');
assert.strictEqual((error as RequestError).code, '404');
done();
});
});

it('should return an error if you try to use request config options', done => {
const expected =
"'uri' is not a valid configuration option. Please use 'url' instead. This library is using Axios for requests. Please see https://github.com/axios/axios to learn more about the valid request options.";
transporter.request(
{
uri: 'http://example.com/api',
} as GaxiosOptions,
transporter.request({url}).then(
() => {
scope.done();
done('Unexpected promise success');
},
error => {
assert.strictEqual(error!.message, expected);
scope.done();
assert.strictEqual(error!.message, 'Not found');
assert.strictEqual((error as RequestError).code, '404');
done();
}
);
Expand Down Expand Up @@ -151,12 +149,17 @@
it('should work with a callback', done => {
const url = 'http://example.com';
const scope = nock(url).get('/').reply(200);
transporter.request({url}, (err, res) => {
scope.done();
assert.strictEqual(err, null);
assert.strictEqual(res!.status, 200);
done();
});
transporter.request({url}).then(
res => {
scope.done();
assert.strictEqual(res!.status, 200);
done();
},
_error => {

Check warning on line 158 in test/test.transporters.ts

View workflow job for this annotation

GitHub Actions / lint

'_error' is defined but never used
scope.done();
done('Unexpected promise failure');
}
);
});

// tslint:disable-next-line ban
Expand Down
Loading