From 094f358b4839e52c587e491f266a179812af927c Mon Sep 17 00:00:00 2001 From: Valexr Date: Mon, 30 May 2022 21:52:53 +0300 Subject: [PATCH 1/4] fix_page+limit/offset --- src/index.ts | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/index.ts b/src/index.ts index f9ced8a..98780af 100644 --- a/src/index.ts +++ b/src/index.ts @@ -114,7 +114,7 @@ export class Optimade { return Optimade.apiVersion(apis); } - async getStructures(providerId: string, filter = '', page = 1, limit: number): Promise { + async getStructures({ providerId, filter, page, limit, offset }: { providerId: string; filter: string; page: number; limit: number; offset: number; }): Promise { if (!this.apis[providerId]) { return null; } @@ -122,12 +122,11 @@ export class Optimade { const provider = this.providers[providerId]; const structures: Types.StructuresResponse[] = await allSettled(apis.map(async (api: Types.Api) => { - if (page <= 0) { page = 1; } const pageLimit = limit ? `&page_limit=${limit}` : ''; - const pageNumber = page ? `&page_number=${page - 1}` : ''; - const pageOffset = limit && page ? `&page_offset=${limit * (page - 1)}` : ''; + const pageNumber = page ? `&page_number=${page}` : ''; + const pageOffset = offset ? `&page_offset=${offset}` : ''; const params = filter ? `${pageLimit + pageNumber + pageOffset}` : `?${pageLimit}`; - const url: string = this.wrapUrl(Optimade.apiVersionUrl(api), filter ? `/structures?filter=${filter + params}` : `/structures${params}`); + const url = this.wrapUrl(Optimade.apiVersionUrl(api), filter ? `/structures?filter=${filter + params}` : `/structures${params}`); try { return await Optimade.getJSON(url, {}, { Origin: 'https://cors.optimade.science', 'X-Requested-With': 'XMLHttpRequest' }); @@ -149,13 +148,13 @@ export class Optimade { }, []); } - getStructuresAll(providerIds: string[], filter = '', page = 1, limit: number, batch = true): Promise[]> | Promise[] { + getStructuresAll({ providers, filter, page, limit, offset, batch = true }: { providers: string[]; filter: string; page: number; limit: number; offset: number; batch?: boolean; }): Promise[]> | Promise[] { - const results = providerIds.reduce((structures: Promise[], providerId: string) => { + const results = providers.reduce((structures: Promise[], providerId: string) => { const provider = this.providers[providerId]; if (provider) { structures.push(allSettled([ - this.getStructures(providerId, filter, page, limit), + this.getStructures({ providerId, filter, page, limit, offset }), Promise.resolve(provider) ])); } @@ -173,7 +172,7 @@ export class Optimade { return !this.isDuplicatedReq(url) ? Optimade.getJSON(url) : null; } - private wrapUrl(url, tail = '') { + private wrapUrl(url: string, tail = ''): string { url = this.corsProxyUrl ? `${this.corsProxyUrl}/${url.replace('://', '/').replace('//', '/')}` : url; return tail ? url.replace(/\/$/, '') + tail : url; } From dcc2109f9a3fa607a6fab68055fdb864524c28fa Mon Sep 17 00:00:00 2001 From: Valexr Date: Mon, 30 May 2022 22:01:33 +0300 Subject: [PATCH 2/4] fix readme --- README.md | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 28741e7..18fc8d4 100755 --- a/README.md +++ b/README.md @@ -57,10 +57,10 @@ const providersMap = await optimadeClient.getProviders(); // { [id: string]: Pro const providerIds = Object.keys(providersMap); // string[] -const results = await optimadeClient.getStructuresAll( - providerIds, - YOUR_OPTIMADE_QUERY, -); // [StructuresResponse[], Provider][] +const results = await optimadeClient.getStructuresAll({ + providers: Provider[], + filter: YOUR_OPTIMADE_QUERY, +}); // [StructuresResponse[], Provider][] ``` Importing depends on your environment. See also the `examples` folder. The @@ -79,12 +79,13 @@ const optimadeClient = new Optimade({ optimadeClient.providers = prefetched.providers; optimadeClient.apis = prefetched.apis; -const results = await optimadeClient.getStructuresAll( - providerIds, - YOUR_OPTIMADE_QUERY, +const results = await optimadeClient.getStructuresAll({ + providers: Provider[], + filter: YOUR_OPTIMADE_QUERY, page: number, - limit: number -); // [StructuresResponse[], Provider][] + limit: number, + offset: number +}); // [StructuresResponse[], Provider][] ``` See also the `demo` folder. From 78f87f37dba7e35bf5a2f8000de23c6e9a51a811 Mon Sep 17 00:00:00 2001 From: Valexr Date: Mon, 30 May 2022 22:03:36 +0300 Subject: [PATCH 3/4] add batch to readme --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 18fc8d4..f55540b 100755 --- a/README.md +++ b/README.md @@ -84,7 +84,8 @@ const results = await optimadeClient.getStructuresAll({ filter: YOUR_OPTIMADE_QUERY, page: number, limit: number, - offset: number + offset: number, + batch: true }); // [StructuresResponse[], Provider][] ``` From dd51b39b552b208169f21ab3df8a6be517660bb7 Mon Sep 17 00:00:00 2001 From: Evgeny Blokhin Date: Mon, 20 Jun 2022 00:51:46 +0200 Subject: [PATCH 4/4] Update package.json --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index e9cf580..18e75f2 100755 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "optimade", - "version": "2.0.5", + "version": "2.1.0", "description": "Aggregating Optimade client for the online materials databases", "main": "dist/index.js", "module": "dist/index.mjs", @@ -40,4 +40,4 @@ "type": "git", "url": "git+https://github.com/tilde-lab/optimade-client" } -} \ No newline at end of file +}