1
0
mirror of https://github.com/oceanprotocol/market.git synced 2024-06-29 00:57:50 +02:00
market/src/components/templates/Search/utils.ts
mihaisc 0540bad3fd
correct number of items on page
Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro>
2020-10-28 13:33:53 +02:00

52 lines
1.3 KiB
TypeScript

import {
SearchQuery,
QueryResult
} from '@oceanprotocol/lib/dist/node/metadatacache/MetadataCache'
import { MetadataCache, Logger } from '@oceanprotocol/lib'
export function getSearchQuery(
page?: string | string[],
offset?: string | string[],
text?: string | string[],
tags?: string | string[],
categories?: string | string[]
): SearchQuery {
return {
page: Number(page) || 1,
offset: Number(offset) || 21,
query: {
text,
tags: tags ? [tags] : undefined,
categories: categories ? [categories] : undefined
},
sort: {
created: -1
}
// Something in ocean.js is weird when using 'tags: [tag]'
// which is the only way the query actually returns desired results.
// But it doesn't follow 'SearchQuery' interface so we have to assign
// it here.
} as SearchQuery
}
export async function getResults(
params: {
text?: string
tags?: string
categories?: string
page?: string
offset?: string
},
metadataCacheUri: string
): Promise<QueryResult> {
const { text, tags, page, offset, categories } = params
const metadataCache = new MetadataCache(metadataCacheUri, Logger)
const queryResult = await metadataCache.queryMetadata(
getSearchQuery(page, offset, text, tags, categories)
)
return queryResult
}