Skip to content

Commit ec6bc03

Browse files
committed
Replace Request dependency with built-in https module
1 parent 064337c commit ec6bc03

11 files changed

Lines changed: 414 additions & 731 deletions

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,6 @@ publish-docker: init test
6464
example:
6565
./scripts/extract_js_from_README.sh > "example.js"
6666
./scripts/run_example.sh
67+
68+
.PHONY: after-gen
69+
after-gen: format lock

docs/models.md

Lines changed: 16 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -779,6 +779,22 @@ interface ErrorDetails {
779779
}
780780
```
781781

782+
## FileVersion
783+
```ts
784+
interface FileVersion {
785+
786+
/**
787+
* File Version ID.
788+
*/
789+
versionId?: string;
790+
791+
/**
792+
* Specifies whether the file is (true) or is not (false) the latest version of an file.
793+
*/
794+
isLatest: boolean;
795+
}
796+
```
797+
782798
## FileVersions
783799

784800
File versions FileVersion.
@@ -2018,44 +2034,3 @@ enum TextAlignment {
20182034
}
20192035
```
20202036

2021-
## FileVersion
2022-
```ts
2023-
interface FileVersion {
2024-
2025-
/**
2026-
* File or folder name.
2027-
*/
2028-
name?: string;
2029-
2030-
/**
2031-
* True if it is a folder.
2032-
*/
2033-
isFolder: boolean;
2034-
2035-
/**
2036-
* File or folder last modified DateTime.
2037-
*/
2038-
modifiedDate?: Date;
2039-
2040-
/**
2041-
* File or folder size.
2042-
*/
2043-
size: number;
2044-
2045-
/**
2046-
* File or folder path.
2047-
*/
2048-
path?: string;
2049-
2050-
/**
2051-
* File Version ID.
2052-
*/
2053-
versionId?: string;
2054-
2055-
/**
2056-
* Specifies whether the file is (true) or is not (false) the latest version of an file.
2057-
*/
2058-
isLatest: boolean;
2059-
}
2060-
```
2061-

src/Authentication.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
import Request from 'request';
1+
import { HttpOptions } from './httpClient';
22

33
export interface Authentication {
44
/**
55
* Apply authentication settings to header and query params.
66
*/
7-
applyToRequest(requestOptions: Request.Options): void;
8-
9-
applyUnauthorized(): void;
7+
applyToRequest(requestOptions: HttpOptions): void;
108
}

src/Configuration.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { JWTAuth } from './JWTAuth';
2-
import { Authentication } from './Authentication';
2+
import { Authentication } from 'Authentication';
33

44
export enum ApiVersion {
55
v3 = 'v3.0',

src/JWTAuth.ts

Lines changed: 13 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import Request from 'request';
2-
3-
import { Configuration } from './Configuration';
4-
import { Authentication } from './Authentication';
1+
import { Configuration } from 'Configuration';
2+
import { Authentication } from 'Authentication';
3+
import { HttpClient, HttpOptions } from './httpClient';
54

65
export class JWTAuth implements Authentication {
76
private _accessToken?: string;
87
private readonly _configuration: Configuration;
8+
private _client: HttpClient;
99

1010
constructor(configuration: Configuration) {
1111
this._configuration = configuration;
@@ -14,14 +14,15 @@ export class JWTAuth implements Authentication {
1414
// Use saved token
1515
this._accessToken = configuration.accessToken;
1616
}
17+
this._client = new HttpClient();
1718
}
1819

1920
/**
2021
* Apply authentication settings to header and query params.
2122
*/
22-
public async applyToRequest(requestOptions: Request.Options): Promise<void> {
23+
public async applyToRequest(requestOptions: HttpOptions): Promise<void> {
2324
if (this._accessToken == null) {
24-
await this.requestToken();
25+
this._accessToken = await this.requestToken();
2526
}
2627

2728
if (requestOptions && requestOptions.headers) {
@@ -31,18 +32,12 @@ export class JWTAuth implements Authentication {
3132
return Promise.resolve();
3233
}
3334

34-
public async applyUnauthorized(): Promise<void> {
35-
if (this._configuration.clientId && this._configuration.clientSecret) {
36-
await this.requestToken();
37-
} else {
35+
private async requestToken(): Promise<string> {
36+
if (!this._configuration.clientId || !this._configuration.clientSecret) {
3837
throw new Error("Required 'clientId' or 'clientSecret' not specified in configuration.");
3938
}
40-
}
41-
42-
private requestToken(): Promise<void> {
43-
const requestOptions: Request.Options = {
39+
const requestOptions: HttpOptions = {
4440
method: 'POST',
45-
json: true,
4641
uri: this._configuration.tokenUrl,
4742
form: {
4843
grant_type: 'client_credentials',
@@ -51,23 +46,8 @@ export class JWTAuth implements Authentication {
5146
},
5247
};
5348

54-
return new Promise<void>((resolve, reject) => {
55-
const self = this;
56-
Request(requestOptions, (error, response) => {
57-
if (error) {
58-
reject(error);
59-
} else {
60-
if (response.statusCode && response.statusCode >= 200 && response.statusCode <= 299) {
61-
self._accessToken = response.body.access_token;
62-
resolve();
63-
} else {
64-
reject(
65-
response.body ??
66-
`Error fetching token from '${requestOptions.uri}': ${response.statusCode} ${response.statusMessage}`
67-
);
68-
}
69-
}
70-
});
71-
});
49+
const result = await this._client.requestAsync(requestOptions);
50+
const parsed = JSON.parse(result.body);
51+
return parsed.access_token;
7252
}
7353
}

0 commit comments

Comments
 (0)