Skip to content
This repository was archived by the owner on Jan 20, 2024. It is now read-only.

Commit 2a08467

Browse files
committed
refactor: refactoring for ES modules
1 parent 332f9ee commit 2a08467

22 files changed

+5385
-1147
lines changed

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test/

.eslintrc.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// module.exports = {
2+
// "env": {
3+
// "browser": true,
4+
// "es6": true,
5+
// "node": true
6+
// },
7+
// "extends": "google",
8+
// "parserOptions": {
9+
// "ecmaVersion": 2018,
10+
// "sourceType": "module"
11+
// },
12+
// "rules": {
13+
//
14+
// }
15+
// };
16+
module.exports = {
17+
extends: ['@advanced-rest-client/eslint-config'].map(require.resolve),
18+
};

.npmignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
coverage/
2+
test/
3+
.travis.yml
4+
husky.*
5+
commitlint.*
6+
.*
7+
*.config.*
8+
prettier.config.js

commitlint.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
extends: ['@commitlint/config-conventional'],
3+
};

husky.config.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
hooks: {
3+
'pre-commit': 'lint-staged',
4+
'commit-msg': 'commitlint -E HUSKY_GIT_PARAMS',
5+
},
6+
};

index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
require = require('esm')(module);
2+
module.exports = require('./main.js');

lib/GithubCache.d.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// tslint:disable:variable-name Describing an API that's defined elsewhere.
2+
// tslint:disable:no-any describes the API as best we are able today
3+
4+
export {GithubCache};
5+
6+
/**
7+
* A class responsible for caching and restoring cached GitHub responses
8+
* results.
9+
*
10+
* Unauthenticated requests to GitHub has 60 requests per hour limit.
11+
* This class helps to cache the results so it's a lower risk of receiving
12+
* `403 Forbidden` error.
13+
*
14+
* This class works with the resolver class.
15+
*
16+
* The class stores data in user's home data folder in
17+
* `api-console/cache/github-http-cache.json` folder.
18+
*/
19+
declare class GithubCache {
20+
constructor(logger: Object);
21+
locateAppDir(platform?: String): String;
22+
loadCache(): Promise<Object>;
23+
lastEtag(url: String): Promise<String|undefined>;
24+
getCachedResult(url: String): Promise<String|undefined>;
25+
storeResponse(url: String, etag: String, response: Object): Promise<void>;
26+
_findEtag(data: Object, url: String): String|undefined;
27+
_findResponse(data: Object, url: String): String|undefined;
28+
_addCacheEntry(data: Object, url: String, etag: String, response: Object): Promise<void>;
29+
_store(): Promise<void>;
30+
}

lib/github-cache.js renamed to lib/GithubCache.js

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const path = require('path');
2-
const fs = require('fs-extra');
1+
import path from 'path';
2+
import fs from 'fs-extra';
33
/**
44
* A class responsible for caching and restoring cached GitHub responses
55
* results.
@@ -13,7 +13,7 @@ const fs = require('fs-extra');
1313
* The class stores data in user's home data folder in
1414
* `api-console/cache/github-http-cache.json` folder.
1515
*/
16-
class GithubCache {
16+
export class GithubCache {
1717
/**
1818
* @param {Object} logger A logger object to use to log messages.
1919
*/
@@ -54,35 +54,33 @@ class GithubCache {
5454
* Loads cache data into memory and returns the data.
5555
* @return {Promise} Promise resolved to cache file contents.
5656
*/
57-
loadCache() {
57+
async loadCache() {
5858
if (this._data) {
59-
return Promise.resolve(this._data);
59+
return this._data;
6060
}
6161
if (this.logger) {
6262
this.logger.debug('[GithubCache] Loading cache from ' + this.cacheLocation);
6363
}
64-
return fs.ensureFile(this.cacheLocation)
65-
.then(() => fs.readJson(this.cacheLocation, {throws: false}))
66-
.then((data) => {
67-
if (data === null) {
68-
data = undefined;
69-
if (this.logger) {
70-
this.logger.debug('[GithubCache] Cache file does not exists.');
71-
}
64+
await fs.ensureFile(this.cacheLocation);
65+
let data = await fs.readJson(this.cacheLocation, { throws: false });
66+
if (data === null) {
67+
data = undefined;
68+
if (this.logger) {
69+
this.logger.debug('[GithubCache] Cache file does not exists.');
7270
}
73-
this._data = data;
74-
return data;
75-
});
71+
}
72+
this._data = data;
73+
return data;
7674
}
7775
/**
7876
* Restores latest `etag`
7977
*
8078
* @param {String} url The full URL used to cache the results.
8179
* @return {Promise}
8280
*/
83-
lastEtag(url) {
84-
return this.loadCache()
85-
.then((data) => this._findEtag(data, url));
81+
async lastEtag(url) {
82+
const data = await this.loadCache();
83+
return await this._findEtag(data, url);
8684
}
8785
/**
8886
* Finds etag in cached data.
@@ -111,9 +109,9 @@ class GithubCache {
111109
* @param {String} url The full URL used to cache the results.
112110
* @return {Promise}
113111
*/
114-
getCachedResult(url) {
115-
return this.loadCache()
116-
.then((data) => this._findResponse(data, url));
112+
async getCachedResult(url) {
113+
const data = await this.loadCache();
114+
return await this._findResponse(data, url);
117115
}
118116
/**
119117
* Finds response in cached data.
@@ -148,20 +146,20 @@ class GithubCache {
148146
* @param {Object} response Response data to cache
149147
* @return {Promise}
150148
*/
151-
storeResponse(url, etag, response) {
152-
return this.loadCache()
153-
.then((data) => this._addCacheEntry(data, url, etag, response));
149+
async storeResponse(url, etag, response) {
150+
const data = await this.loadCache();
151+
return await this._addCacheEntry(data, url, etag, response);
154152
}
155153
/**
156154
* Appends response data to the cache.
157155
*
158-
* @param {Obect} data Responses cache object
156+
* @param {Object} data Responses cache object
159157
* @param {String} url Request URL
160158
* @param {String} etag Response etag
161159
* @param {Object} response GitHub response.
162160
* @return {Promise}
163161
*/
164-
_addCacheEntry(data, url, etag, response) {
162+
async _addCacheEntry(data, url, etag, response) {
165163
if (this.logger) {
166164
this.logger.debug('[GithubCache] Adding cache entry for ' + url);
167165
}
@@ -173,18 +171,17 @@ class GithubCache {
173171
response
174172
};
175173
this._data = data;
176-
return this._store();
174+
await this._store();
177175
}
178176
/**
179177
* Stores current data to the cache file.
180178
* @return {Promise}
181179
*/
182-
_store() {
180+
async _store() {
183181
const data = this._data || {};
184182
if (this.logger) {
185183
this.logger.debug('[GithubCache] Storing cache data to ' + this.cacheLocation);
186184
}
187-
return fs.writeJson(this.cacheLocation, data);
185+
await fs.writeJson(this.cacheLocation, data);
188186
}
189187
}
190-
exports.GithubCache = GithubCache;

lib/GithubResolver.d.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// tslint:disable:variable-name Describing an API that's defined elsewhere.
2+
// tslint:disable:no-any describes the API as best we are able today
3+
4+
export {GithubResolver};
5+
6+
import { GithubResolverOptions } from './GithubResolverOptions.js';
7+
8+
/**
9+
* A GitGub transport class.
10+
* The transport is based on the HTTPS protocol.
11+
*/
12+
declare class GithubResolver {
13+
constructor(opts?: GithubResolverOptions);
14+
getLatestInfo(): Promise<Object>;
15+
getReleasesList(): Promise<Array<Object>>;
16+
getTagInfo(): Promise<Object>;
17+
_setupLogger(opts?: GithubResolverOptions): Object;
18+
_computeHeaders(): Object;
19+
_getResetTime(): number;
20+
_assertCanMakeRequest(): void;
21+
_makeRequest(url: String, headers?: Object): Promise<Object|Buffer>;
22+
_handleResponseHeaders(): void;
23+
_filterSupportedTags(json: Array<Object>): Array<Object>;
24+
_sortTags(a: string, b: string): number;
25+
_getTagInfo(tag: string): Object;
26+
_throwTag404Error(tag: string): void;
27+
_getReleasesListErrorMessage(tag: string, releases: Array<Object>): string;
28+
_assertTag(tag: string): void;
29+
}

0 commit comments

Comments
 (0)