Skip to content

Commit

Permalink
Merge pull request #20 from tilde-lab/fix_page+limit/offset
Browse files Browse the repository at this point in the history
fix_page+limit/offset
  • Loading branch information
blokhin authored Jun 19, 2022
2 parents fe91e5d + dd51b39 commit d3c4131
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 20 deletions.
20 changes: 11 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -40,4 +40,4 @@
"type": "git",
"url": "git+https://github.com/tilde-lab/optimade-client"
}
}
}
17 changes: 8 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,20 +114,19 @@ export class Optimade {
return Optimade.apiVersion(apis);
}

async getStructures(providerId: string, filter = '', page = 1, limit: number): Promise<Types.StructuresResponse[] | Types.ResponseError> {
async getStructures({ providerId, filter, page, limit, offset }: { providerId: string; filter: string; page: number; limit: number; offset: number; }): Promise<Types.StructuresResponse[] | Types.ResponseError> {

if (!this.apis[providerId]) { return null; }

const apis = this.apis[providerId].filter(api => api.attributes.available_endpoints.includes('structures'));
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' });
Expand All @@ -149,13 +148,13 @@ export class Optimade {
}, []);
}

getStructuresAll(providerIds: string[], filter = '', page = 1, limit: number, batch = true): Promise<Promise<Types.StructuresResult>[]> | Promise<Types.StructuresResult>[] {
getStructuresAll({ providers, filter, page, limit, offset, batch = true }: { providers: string[]; filter: string; page: number; limit: number; offset: number; batch?: boolean; }): Promise<Promise<Types.StructuresResult>[]> | Promise<Types.StructuresResult>[] {

const results = providerIds.reduce((structures: Promise<any>[], providerId: string) => {
const results = providers.reduce((structures: Promise<any>[], 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)
]));
}
Expand All @@ -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;
}
Expand Down

0 comments on commit d3c4131

Please sign in to comment.