diff --git a/README.md b/README.md index 28741e7..f55540b 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,14 @@ 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, + batch: true +}); // [StructuresResponse[], Provider][] ``` See also the `demo` folder. 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 +} 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; }