Skip to content

Commit fe91e5d

Browse files
authored
Merge pull request #19 from tilde-lab/#12
Close #12
2 parents 7d2e8ef + 16a5d01 commit fe91e5d

File tree

8 files changed

+2657
-1334
lines changed

8 files changed

+2657
-1334
lines changed

.eslintrc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"extends": [
3+
"eslint:recommended",
4+
"plugin:@typescript-eslint/recommended"
5+
],
6+
"plugins": [
7+
"@typescript-eslint"
8+
],
9+
"parser": "@typescript-eslint/parser",
10+
"root": true,
11+
"env": {
12+
"node": true
13+
},
14+
"rules": {
15+
"@typescript-eslint/no-explicit-any": "off"
16+
}
17+
}

package-lock.json

Lines changed: 2599 additions & 1255 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,37 @@
77
"types": "dist/index.d.ts",
88
"scripts": {
99
"build": "npm run lint && rollup --config",
10-
"lint": "tslint -c tslint.json \"src/**/*.ts\"",
11-
"lint:fix": "tslint --fix -c tslint.json \"src/**/*.ts\"",
10+
"lint": "eslint './src/**/*.ts'",
11+
"lint:fix": "eslint --fix './src/**/*.ts'",
1212
"prepublishOnly": "npm run build && npm run prefetch",
1313
"test": "echo \"Error: no test specified\" && exit 1",
1414
"prefetch": "node prefetch.js"
1515
},
1616
"author": "Pavel Malyshev",
1717
"license": "MIT",
1818
"devDependencies": {
19+
"@types/eslint": "^8.4.2",
20+
"@typescript-eslint/eslint-plugin": "^5.25.0",
21+
"@typescript-eslint/parser": "^5.25.0",
22+
"@rollup/plugin-json": "^4.1.0",
1923
"@types/jest": "^26.0.15",
2024
"jest": "^26.6.0",
2125
"rollup": "^2.47.0",
2226
"rollup-plugin-typescript2": "^0.28.0",
2327
"ts-jest": "^26.4.1",
24-
"tslint": "^6.1.3",
25-
"tslint-eslint-rules": "^5.4.0",
28+
"eslint": "^8.16.0",
2629
"typescript": "^4.0.3"
2730
},
2831
"dependencies": {
2932
"isomorphic-unfetch": "^3.1.0",
3033
"node-abort-controller": "^3.0.1"
3134
},
32-
"keywords": ["optimade", "materials"],
35+
"keywords": [
36+
"optimade",
37+
"materials"
38+
],
3339
"repository": {
3440
"type": "git",
3541
"url": "git+https://github.com/tilde-lab/optimade-client"
36-
}
37-
}
42+
}
43+
}

rollup.config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
/* jshint esversion: 6 */
2+
13
import typescript from 'rollup-plugin-typescript2';
4+
import json from '@rollup/plugin-json';
25

36
import pkg from './package.json';
47

@@ -14,5 +17,6 @@ export default {
1417
],
1518
plugins: [
1619
typescript(),
20+
json(),
1721
],
1822
};

src/index.ts

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { allSettled, fetchWithTimeout } from './utils';
2+
import { version } from '../package.json'
23

34
import type * as Types from './types';
45
export { Types };
56

67
export class Optimade {
7-
private providersUrl: string = '';
8-
private corsProxyUrl: string = '';
8+
private providersUrl = '';
9+
private corsProxyUrl = '';
910
providers: Types.ProvidersMap | null = null;
1011
apis: Types.ApisMap = {};
1112
private reqStack: string[] = [];
@@ -16,7 +17,7 @@ export class Optimade {
1617
}
1718

1819
async addProvider(provider: Types.Provider) {
19-
if (!this.apis[provider.id]) this.apis[provider.id] = [];
20+
if (!this.apis[provider.id]) { this.apis[provider.id] = []; }
2021

2122
try {
2223
const ver = provider.attributes
@@ -28,7 +29,7 @@ export class Optimade {
2829
this.apis[provider.id].push(api);
2930
}
3031

31-
} catch (ignore) { }
32+
} catch (ignore) { console.log(ignore) }
3233

3334
if (!provider.attributes.query_limits) {
3435
const formula = `chemical_formula_anonymous="A2B"`;
@@ -37,7 +38,7 @@ export class Optimade {
3738
try {
3839
const res = await fetch(url).then(res => res.json());
3940
const version = res.meta && res.meta.api_version || this.apis[provider.id][0].attributes.api_version;
40-
const detail = (errors: { detail: any; }[]) => {
41+
const detail = (errors) => {
4142
return errors
4243
? errors.length
4344
? errors[0].detail
@@ -71,18 +72,18 @@ export class Optimade {
7172
Optimade.getJSON(this.providersUrl).catch(() => null)
7273
);
7374

74-
if (!providers) return null;
75-
if (!this.providers) this.providers = {};
75+
if (!providers) { return null; }
76+
if (!this.providers) { this.providers = {}; }
7677

7778
const data = providers.data.filter(Optimade.isProviderValid);
7879
const ver = providers.meta && providers.meta.api_version ?
7980
providers.meta.api_version.charAt(0) : '';
8081

8182
for (const provider of data) {
82-
if (!this.apis[provider.id]) this.apis[provider.id] = [];
83+
if (!this.apis[provider.id]) { this.apis[provider.id] = []; }
8384
try {
8485
const api = await this.getApis(provider, ver ? `v${ver}` : '');
85-
if (!api) continue;
86+
if (!api) { continue; }
8687

8788
if (api.attributes.available_endpoints.includes('structures')) {
8889
this.apis[provider.id].push(api);
@@ -92,36 +93,36 @@ export class Optimade {
9293
} else {
9394
await this.getProviders(api);
9495
}
95-
} catch (ignore) { }
96+
} catch (ignore) { console.log(ignore) }
9697
}
9798

9899
return this.providers;
99100
}
100101

101-
async getApis(provider: Types.Provider | string, version: string = ''): Promise<Types.Api | null> {
102+
async getApis(provider: Types.Provider | string, version = ''): Promise<Types.Api | null> {
102103
if (typeof provider === 'string') {
103104
provider = this.providers[provider];
104105
}
105106

106-
if (!provider) throw new Error('No provider found');
107+
if (!provider) { throw new Error('No provider found'); }
107108

108109
const url: string = this.wrapUrl(`${provider.attributes.base_url}/${version}`, '/info');
109110

110-
if (this.isDuplicatedReq(url)) return null;
111+
if (this.isDuplicatedReq(url)) { return null; }
111112

112113
const apis: Types.InfoResponse = await Optimade.getJSON(url);
113114
return Optimade.apiVersion(apis);
114115
}
115116

116-
async getStructures(providerId: string, filter: string = '', page: number = 1, limit: number): Promise<Types.StructuresResponse[] | Types.ResponseError> {
117+
async getStructures(providerId: string, filter = '', page = 1, limit: number): Promise<Types.StructuresResponse[] | Types.ResponseError> {
117118

118-
if (!this.apis[providerId]) return null;
119+
if (!this.apis[providerId]) { return null; }
119120

120121
const apis = this.apis[providerId].filter(api => api.attributes.available_endpoints.includes('structures'));
121122
const provider = this.providers[providerId];
122123

123124
const structures: Types.StructuresResponse[] = await allSettled(apis.map(async (api: Types.Api) => {
124-
if (page <= 0) page = 1;
125+
if (page <= 0) { page = 1; }
125126
const pageLimit = limit ? `&page_limit=${limit}` : '';
126127
const pageNumber = page ? `&page_number=${page - 1}` : '';
127128
const pageOffset = limit && page ? `&page_offset=${limit * (page - 1)}` : '';
@@ -148,7 +149,7 @@ export class Optimade {
148149
}, []);
149150
}
150151

151-
getStructuresAll(providerIds: string[], filter: string = '', page: number = 1, limit: number, batch: boolean = true): Promise<Promise<Types.StructuresResult>[]> | Promise<Types.StructuresResult>[] {
152+
getStructuresAll(providerIds: string[], filter = '', page = 1, limit: number, batch = true): Promise<Promise<Types.StructuresResult>[]> | Promise<Types.StructuresResult>[] {
152153

153154
const results = providerIds.reduce((structures: Promise<any>[], providerId: string) => {
154155
const provider = this.providers[providerId];
@@ -165,7 +166,7 @@ export class Optimade {
165166
}
166167

167168
private async followLinks(api: Types.Api): Promise<Types.LinksResponse | null> {
168-
if (!api.attributes.available_endpoints.includes('links')) return null;
169+
if (!api.attributes.available_endpoints.includes('links')) { return null; }
169170

170171
const url = this.wrapUrl(Optimade.apiVersionUrl(api), '/links');
171172

@@ -181,7 +182,7 @@ export class Optimade {
181182
return this.reqStack.includes(url) || !this.reqStack.unshift(url);
182183
}
183184

184-
static async getJSON(uri: string, params: {} = null, headers: {} = {}) {
185+
static async getJSON(uri: string, params = null, headers = {}) {
185186

186187
const url = new URL(uri);
187188
const timeout = 10000;
@@ -190,6 +191,8 @@ export class Optimade {
190191
Object.entries(params).forEach((param: [string, any]) => url.searchParams.append(...param));
191192
}
192193

194+
Object.assign(headers, { 'User-Agent': `tilde-lab-optimade-client/${version}` })
195+
193196
const res = await fetchWithTimeout(url.toString(), { headers }, timeout);
194197

195198
if (!res.ok) {

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export interface Api {
4646
api_version: string;
4747
available_api_versions: ApiVer | ApiVer[];
4848
available_endpoints: string[];
49-
entry_types_by_format: {};
49+
entry_types_by_format: Record<string, unknown>;
5050
formats: string[];
5151
};
5252
}

tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"noImplicitReturns": true,
1212
"noUnusedLocals": true,
1313
"noUnusedParameters": true,
14+
"resolveJsonModule": true,
1415
"removeComments": true,
1516
"noLib": false,
1617
"moduleResolution": "node",

tslint.json

Lines changed: 0 additions & 52 deletions
This file was deleted.

0 commit comments

Comments
 (0)