Skip to content

Commit 4f27c61

Browse files
committed
refactor: remove request from *auth files
* `azure_auth` * `exec_auth` * `file_auth` * `gcp_auth` * `oidc_auth`
1 parent d9626f9 commit 4f27c61

File tree

10 files changed

+56
-70
lines changed

10 files changed

+56
-70
lines changed

FETCH_MIGRATION.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ Code will be on the `master` branch.
5050
- [x] Generate api with `npm run generate`
5151
- [x] Match src/gen/api.ts to new generated layout (it changes slightly)
5252
- [ ] Fix errors in /src folder (due to new api)
53-
- [ ] migrate src/auth.ts, the dependent implementations (ex: azure_auth, gcp_auth etc) and tests to fetch api from request
53+
- [x] migrate src/auth.ts, the dependent implementations (ex: azure_auth, gcp_auth etc) and tests to fetch api from request
5454
- [ ] migrate src/log.ts and its tests to fetch api from request
5555
- major remaining work is fixing up async signatures and return piping
5656
- [ ] migrate src/watch.ts and its tests to fetch api from request

src/azure_auth.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import * as proc from 'child_process';
22
import https = require('https');
33
import * as jsonpath from 'jsonpath-plus';
4-
import request = require('request');
54

65
import { Authenticator } from './auth';
76
import { User } from './config_types';
@@ -27,10 +26,7 @@ export class AzureAuth implements Authenticator {
2726
return user.authProvider.name === 'azure';
2827
}
2928

30-
public async applyAuthentication(
31-
user: User,
32-
opts: request.Options | https.RequestOptions,
33-
): Promise<void> {
29+
public async applyAuthentication(user: User, opts: https.RequestOptions): Promise<void> {
3430
const token = this.getToken(user);
3531
if (token) {
3632
opts.headers!.Authorization = `Bearer ${token}`;

src/exec_auth.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import execa = require('execa');
2+
import { OutgoingHttpHeaders } from 'http';
23
import https = require('https');
3-
import request = require('request');
44

55
import { Authenticator } from './auth';
66
import { User } from './config_types';
@@ -36,10 +36,7 @@ export class ExecAuth implements Authenticator {
3636
);
3737
}
3838

39-
public async applyAuthentication(
40-
user: User,
41-
opts: request.Options | https.RequestOptions,
42-
): Promise<void> {
39+
public async applyAuthentication(user: User, opts: https.RequestOptions): Promise<void> {
4340
const credential = this.getCredential(user);
4441
if (!credential) {
4542
return;
@@ -53,7 +50,7 @@ export class ExecAuth implements Authenticator {
5350
const token = this.getToken(credential);
5451
if (token) {
5552
if (!opts.headers) {
56-
opts.headers = [];
53+
opts.headers = {} as OutgoingHttpHeaders;
5754
}
5855
opts.headers!.Authorization = `Bearer ${token}`;
5956
}

src/exec_auth_test.ts

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { expect, use } from 'chai';
2-
import chaiAsPromised = require('chai-as-promised');
2+
import chaiAsPromised from 'chai-as-promised';
33
use(chaiAsPromised);
44

55
import * as shell from 'shelljs';
66

7-
import execa = require('execa');
8-
import request = require('request');
9-
import https = require('https');
7+
import execa from 'execa';
8+
import https from 'https';
9+
import { OutgoingHttpHeaders } from 'http';
1010

1111
import { ExecAuth } from './exec_auth';
1212
import { User } from './config_types';
@@ -70,8 +70,8 @@ describe('ExecAuth', () => {
7070
stdout: JSON.stringify({ status: { token: 'foo' } }),
7171
} as execa.ExecaSyncReturnValue;
7272
};
73-
const opts = {} as request.Options;
74-
opts.headers = [];
73+
const opts = {} as https.RequestOptions;
74+
opts.headers = {} as OutgoingHttpHeaders;
7575
auth.applyAuthentication(
7676
{
7777
name: 'user',
@@ -115,8 +115,9 @@ describe('ExecAuth', () => {
115115
},
116116
},
117117
};
118-
const opts = {} as request.Options;
119-
opts.headers = [];
118+
const opts = {} as https.RequestOptions;
119+
opts.headers = {} as OutgoingHttpHeaders;
120+
opts.headers = {} as OutgoingHttpHeaders;
120121

121122
auth.applyAuthentication(user, opts);
122123
expect(opts.headers.Authorization).to.be.undefined;
@@ -158,8 +159,8 @@ describe('ExecAuth', () => {
158159
},
159160
};
160161

161-
const opts = {} as request.Options;
162-
opts.headers = [];
162+
const opts = {} as https.RequestOptions;
163+
opts.headers = {} as OutgoingHttpHeaders;
163164

164165
await auth.applyAuthentication(user, opts);
165166
expect(opts.headers.Authorization).to.equal(`Bearer ${tokenValue}`);
@@ -181,8 +182,8 @@ describe('ExecAuth', () => {
181182

182183
it('should return null on no exec info', async () => {
183184
const auth = new ExecAuth();
184-
const opts = {} as request.Options;
185-
opts.headers = [];
185+
const opts = {} as https.RequestOptions;
186+
opts.headers = {} as OutgoingHttpHeaders;
186187

187188
await auth.applyAuthentication({} as User, opts);
188189
expect(opts.headers.Authorization).to.be.undefined;
@@ -216,8 +217,8 @@ describe('ExecAuth', () => {
216217
},
217218
},
218219
};
219-
const opts = {} as request.Options;
220-
opts.headers = [];
220+
const opts = {} as https.RequestOptions;
221+
opts.headers = {} as OutgoingHttpHeaders;
221222

222223
const promise = auth.applyAuthentication(user, opts);
223224
return expect(promise).to.eventually.be.rejected;
@@ -242,8 +243,8 @@ describe('ExecAuth', () => {
242243
} as execa.ExecaSyncReturnValue;
243244
};
244245
process.env.BLABBLE = 'flubble';
245-
const opts = {} as request.Options;
246-
opts.headers = [];
246+
const opts = {} as https.RequestOptions;
247+
opts.headers = {} as OutgoingHttpHeaders;
247248

248249
await auth.applyAuthentication(
249250
{

src/exec_test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { expect } from 'chai';
2-
import WebSocket = require('isomorphic-ws');
2+
import WebSocket from 'isomorphic-ws';
33
import { ReadableStreamBuffer, WritableStreamBuffer } from 'stream-buffers';
44
import { anyFunction, anything, capture, instance, mock, verify, when } from 'ts-mockito';
55

src/file_auth.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import fs = require('fs');
22
import https = require('https');
3-
import request = require('request');
43

54
import { Authenticator } from './auth';
65
import { User } from './config_types';
@@ -13,10 +12,7 @@ export class FileAuth implements Authenticator {
1312
return user.authProvider && user.authProvider.config && user.authProvider.config.tokenFile;
1413
}
1514

16-
public async applyAuthentication(
17-
user: User,
18-
opts: request.Options | https.RequestOptions,
19-
): Promise<void> {
15+
public async applyAuthentication(user: User, opts: https.RequestOptions): Promise<void> {
2016
if (this.token == null) {
2117
this.refreshToken(user.authProvider.config.tokenFile);
2218
}

src/file_auth_test.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { expect } from 'chai';
2-
import mockfs = require('mock-fs');
3-
4-
import request = require('request');
2+
import { OutgoingHttpHeaders } from 'http';
3+
import https from 'https';
4+
import mockfs from 'mock-fs';
55

66
import { User } from './config_types';
77
import { FileAuth } from './file_auth';
@@ -24,8 +24,8 @@ describe('FileAuth', () => {
2424
},
2525
} as User;
2626

27-
const opts = {} as request.Options;
28-
opts.headers = [];
27+
const opts = {} as https.RequestOptions;
28+
opts.headers = {} as OutgoingHttpHeaders;
2929

3030
await auth.applyAuthentication(user, opts);
3131
expect(opts.headers.Authorization).to.equal(`Bearer ${token}`);
@@ -49,8 +49,8 @@ describe('FileAuth', () => {
4949
},
5050
} as User;
5151

52-
const opts = {} as request.Options;
53-
opts.headers = [];
52+
const opts = {} as https.RequestOptions;
53+
opts.headers = {} as OutgoingHttpHeaders;
5454

5555
await auth.applyAuthentication(user, opts);
5656
expect(opts.headers.Authorization).to.equal(`Bearer ${token}`);
@@ -73,8 +73,8 @@ describe('FileAuth', () => {
7373
},
7474
} as User;
7575

76-
const opts = {} as request.Options;
77-
opts.headers = [];
76+
const opts = {} as https.RequestOptions;
77+
opts.headers = {} as OutgoingHttpHeaders;
7878

7979
await auth.applyAuthentication(user, opts);
8080
expect(opts.headers.Authorization).to.equal(`Bearer ${token}`);

src/gcp_auth.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import * as proc from 'child_process';
22
import https = require('https');
33
import * as jsonpath from 'jsonpath-plus';
4-
import request = require('request');
54

65
import { Authenticator } from './auth';
76
import { User } from './config_types';
@@ -26,10 +25,7 @@ export class GoogleCloudPlatformAuth implements Authenticator {
2625
return user.authProvider.name === 'gcp';
2726
}
2827

29-
public async applyAuthentication(
30-
user: User,
31-
opts: request.Options | https.RequestOptions,
32-
): Promise<void> {
28+
public async applyAuthentication(user: User, opts: https.RequestOptions): Promise<void> {
3329
const token = this.getToken(user);
3430
if (token) {
3531
opts.headers!.Authorization = `Bearer ${token}`;

src/oidc_auth.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import https = require('https');
1+
import https from 'https';
22
import { Client, Issuer } from 'openid-client';
3-
import request = require('request');
43
import { base64url } from 'rfc4648';
54
import { TextDecoder } from 'util';
65

@@ -56,7 +55,7 @@ export class OpenIDConnectAuth implements Authenticator {
5655
*/
5756
public async applyAuthentication(
5857
user: User,
59-
opts: request.Options | https.RequestOptions,
58+
opts: https.RequestOptions,
6059
overrideClient?: any,
6160
): Promise<void> {
6261
const token = await this.getToken(user, overrideClient);

src/oidc_auth_test.ts

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { expect } from 'chai';
2-
import * as request from 'request';
2+
import { OutgoingHttpHeaders } from 'http';
3+
import https from 'https';
34
import { base64url } from 'rfc4648';
45
import { TextEncoder } from 'util';
56

@@ -74,8 +75,8 @@ describe('OIDCAuth', () => {
7475
},
7576
} as User;
7677

77-
const opts = {} as request.Options;
78-
opts.headers = [];
78+
const opts = {} as https.RequestOptions;
79+
opts.headers = {} as OutgoingHttpHeaders;
7980
await auth.applyAuthentication(user, opts);
8081
expect(opts.headers.Authorization).to.be.undefined;
8182
});
@@ -95,8 +96,8 @@ describe('OIDCAuth', () => {
9596
},
9697
} as User;
9798

98-
const opts = {} as request.Options;
99-
opts.headers = [];
99+
const opts = {} as https.RequestOptions;
100+
opts.headers = {} as OutgoingHttpHeaders;
100101
await auth.applyAuthentication(user, opts);
101102
expect(opts.headers.Authorization).to.be.undefined;
102103
});
@@ -114,8 +115,8 @@ describe('OIDCAuth', () => {
114115
},
115116
} as User;
116117

117-
const opts = {} as request.Options;
118-
opts.headers = [];
118+
const opts = {} as https.RequestOptions;
119+
opts.headers = {} as OutgoingHttpHeaders;
119120
(auth as any).currentTokenExpiration = Date.now() / 1000 + 1000;
120121
await auth.applyAuthentication(user, opts, {});
121122
expect(opts.headers.Authorization).to.equal('Bearer fakeToken');
@@ -136,8 +137,8 @@ describe('OIDCAuth', () => {
136137
},
137138
} as User;
138139

139-
const opts = {} as request.Options;
140-
opts.headers = [];
140+
const opts = {} as https.RequestOptions;
141+
opts.headers = {} as OutgoingHttpHeaders;
141142
await auth.applyAuthentication(user, opts);
142143
expect(opts.headers.Authorization).to.be.undefined;
143144
});
@@ -157,8 +158,8 @@ describe('OIDCAuth', () => {
157158
},
158159
} as User;
159160

160-
const opts = {} as request.Options;
161-
opts.headers = [];
161+
const opts = {} as https.RequestOptions;
162+
opts.headers = {} as OutgoingHttpHeaders;
162163
await auth.applyAuthentication(user, opts);
163164
expect(opts.headers.Authorization).to.equal(`Bearer ${token}`);
164165
});
@@ -178,8 +179,8 @@ describe('OIDCAuth', () => {
178179
},
179180
} as User;
180181

181-
const opts = {} as request.Options;
182-
opts.headers = [];
182+
const opts = {} as https.RequestOptions;
183+
opts.headers = {} as OutgoingHttpHeaders;
183184
await auth.applyAuthentication(user, opts, {});
184185
expect(opts.headers.Authorization).to.be.undefined;
185186
});
@@ -198,8 +199,8 @@ describe('OIDCAuth', () => {
198199
},
199200
} as User;
200201

201-
const opts = {} as request.Options;
202-
opts.headers = [];
202+
const opts = {} as https.RequestOptions;
203+
opts.headers = {} as OutgoingHttpHeaders;
203204
(auth as any).currentTokenExpiration = Date.now() / 1000 + 1000;
204205
await auth.applyAuthentication(user, opts, {});
205206
expect(opts.headers.Authorization).to.equal('Bearer fakeToken');
@@ -219,8 +220,8 @@ describe('OIDCAuth', () => {
219220
},
220221
} as User;
221222

222-
const opts = {} as request.Options;
223-
opts.headers = [];
223+
const opts = {} as https.RequestOptions;
224+
opts.headers = {} as OutgoingHttpHeaders;
224225
(auth as any).currentTokenExpiration = Date.now() / 1000 - 5000;
225226
const newExpiration = Date.now() / 1000 + 120;
226227
await auth.applyAuthentication(user, opts, {
@@ -252,8 +253,8 @@ describe('OIDCAuth', () => {
252253
},
253254
} as User;
254255

255-
const opts = {} as request.Options;
256-
opts.headers = [];
256+
const opts = {} as https.RequestOptions;
257+
opts.headers = {} as OutgoingHttpHeaders;
257258
const newExpiration = Date.now() / 1000 + 120;
258259
(auth as any).currentTokenExpiration = 0;
259260
await auth.applyAuthentication(user, opts, {

0 commit comments

Comments
 (0)