diff --git a/packages/ckan/src/interfaces/dataset.interface.ts b/packages/ckan/src/interfaces/dataset.interface.ts index 386266b5..f0ad2a67 100644 --- a/packages/ckan/src/interfaces/dataset.interface.ts +++ b/packages/ckan/src/interfaces/dataset.interface.ts @@ -69,6 +69,7 @@ export interface PackageSearchOptions { query?: string; resFormat?: Array; sort?: string; + include_private?: boolean; } export interface Tag { diff --git a/packages/ckan/src/lib/ckanapi.tsx b/packages/ckan/src/lib/ckanapi.tsx index a237c541..039f6e85 100644 --- a/packages/ckan/src/lib/ckanapi.tsx +++ b/packages/ckan/src/lib/ckanapi.tsx @@ -31,11 +31,7 @@ export default class CKAN { async getDatasetsListWithDetails(options: DatasetListQueryOptions) { const response = await fetchRetry( - `${ - this.DMS - }/api/3/action/current_package_list_with_resources?offset=${ - options.offset - }&limit=${options.limit}`, + `${this.DMS}/api/3/action/current_package_list_with_resources?offset=${options.offset}&limit=${options.limit}`, 3 ); const responseData = await response.json(); @@ -44,7 +40,8 @@ export default class CKAN { } async packageSearch( - options: PackageSearchOptions + options: PackageSearchOptions, + reqOptions: Partial = {} ): Promise<{ datasets: Dataset[]; count: number }> { function buildGroupsQuery(groups: Array) { if (groups.length > 0) { @@ -99,16 +96,18 @@ export default class CKAN { options.groups, options?.resFormat ); - const response = await fetchRetry( - `${ - this.DMS - }/api/3/action/package_search?start=${options.offset}&rows=${ - options.limit - }${fq ? fq : ''}${options.query ? '&q=' + options.query : ''}${ - options.sort ? '&sort=' + options.sort : '' - }`, - 3 - ); + + let url = `${this.DMS}/api/3/action/package_search?`; + url += `start=${options.offset}`; + url += `&rows=${options.limit}`; + url += fq ? fq : ''; + url += options.query ? '&q=' + options.query : ''; + url += options.sort ? '&sort=' + options.sort : ''; + url += options.include_private + ? '&include_private=' + options.include_private + : ''; + + const response = await fetchRetry(url, 3, reqOptions); const responseData = await response.json(); const datasets: Array = responseData.result.results; return { datasets, count: responseData.result.count }; @@ -116,9 +115,7 @@ export default class CKAN { async getDatasetDetails(datasetName: string) { const response = await fetchRetry( - `${ - this.DMS - }/api/3/action/package_show?id=${datasetName}`, + `${this.DMS}/api/3/action/package_show?id=${datasetName}`, 1 ); const responseData = await response.json(); @@ -131,9 +128,7 @@ export default class CKAN { async getDatasetActivityStream(datasetName: string) { const response = await fetchRetry( - `${ - this.DMS - }/api/3/action/package_activity_list?id=${datasetName}`, + `${this.DMS}/api/3/action/package_activity_list?id=${datasetName}`, 3 ); const responseData = await response.json(); @@ -151,9 +146,7 @@ export default class CKAN { async getUser(userId: string) { try { const response = await fetchRetry( - `${ - this.DMS - }/api/3/action/user_show?id=${userId}`, + `${this.DMS}/api/3/action/user_show?id=${userId}`, 3 ); const responseData = await response.json(); @@ -166,10 +159,7 @@ export default class CKAN { } async getGroupList() { - const response = await fetchRetry( - `${this.DMS}/api/3/action/group_list`, - 3 - ); + const response = await fetchRetry(`${this.DMS}/api/3/action/group_list`, 3); const responseData = await response.json(); const groups: Array = responseData.result; return groups; @@ -177,9 +167,7 @@ export default class CKAN { async getGroupsWithDetails() { const response = await fetchRetry( - `${ - this.DMS - }/api/3/action/group_list?all_fields=True`, + `${this.DMS}/api/3/action/group_list?all_fields=True`, 3 ); const responseData = await response.json(); @@ -189,9 +177,7 @@ export default class CKAN { async getGroupDetails(groupName: string) { const response = await fetchRetry( - `${ - this.DMS - }/api/3/action/group_show?id=${groupName}&include_datasets=True`, + `${this.DMS}/api/3/action/group_show?id=${groupName}&include_datasets=True`, 3 ); const responseData = await response.json(); @@ -201,9 +187,7 @@ export default class CKAN { async getGroupActivityStream(groupName: string) { const response = await fetchRetry( - `${ - this.DMS - }/api/3/action/group_activity_list?id=${groupName}`, + `${this.DMS}/api/3/action/group_activity_list?id=${groupName}`, 3 ); const responseData = await response.json(); @@ -230,9 +214,7 @@ export default class CKAN { async getOrgsWithDetails(accrossPages?: boolean) { if (!accrossPages) { const response = await fetchRetry( - `${ - this.DMS - }/api/3/action/organization_list?all_fields=True`, + `${this.DMS}/api/3/action/organization_list?all_fields=True`, 3 ); const responseData = await response.json(); @@ -251,9 +233,7 @@ export default class CKAN { for (let i = 0; i < pages; i++) { let allOrgListResponse = await fetchRetry( - `${ - this.DMS - }/api/3/action/organization_list?all_fields=True&offset=${ + `${this.DMS}/api/3/action/organization_list?all_fields=True&offset=${ i * 25 }&limit=25`, 3 @@ -267,9 +247,7 @@ export default class CKAN { async getOrgDetails(orgName: string) { const response = await fetchRetry( - `${ - this.DMS - }/api/3/action/organization_show?id=${orgName}&include_datasets=True`, + `${this.DMS}/api/3/action/organization_show?id=${orgName}&include_datasets=True`, 3 ); const responseData = await response.json(); @@ -279,9 +257,7 @@ export default class CKAN { async getOrgActivityStream(orgName: string) { const response = await fetchRetry( - `${ - this.DMS - }/api/3/action/organization_activity_list?id=${orgName}`, + `${this.DMS}/api/3/action/organization_activity_list?id=${orgName}`, 3 ); const responseData = await response.json(); @@ -297,9 +273,7 @@ export default class CKAN { async getAllTags() { const response = await fetchRetry( - `${ - this.DMS - }/api/3/action/tag_list?all_fields=True`, + `${this.DMS}/api/3/action/tag_list?all_fields=True`, 3 ); const responseData = await response.json(); @@ -308,49 +282,41 @@ export default class CKAN { } async getResourcesWithAliasList() { - const response = await fetch( - `${this.DMS}/api/3/action/datastore_search`, - { - method: 'POST', - headers: { - Accept: 'application/json', - 'Content-Type': 'application/json', - }, - body: JSON.stringify({ - id: '_table_metadata', - limit: '32000', - }), - } - ); + const response = await fetch(`${this.DMS}/api/3/action/datastore_search`, { + method: 'POST', + headers: { + Accept: 'application/json', + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + id: '_table_metadata', + limit: '32000', + }), + }); const responseData = await response.json(); const tableMetadata: Array = responseData.result.records; return tableMetadata.filter((item) => item.alias_of); } async datastoreSearch(resourceId: string) { - const response = await fetch( - `${this.DMS}/api/3/action/datastore_search`, - { - method: 'POST', - headers: { - Accept: 'application/json', - 'Content-Type': 'application/json', - }, - body: JSON.stringify({ - id: resourceId, - limit: '32000', - }), - } - ); + const response = await fetch(`${this.DMS}/api/3/action/datastore_search`, { + method: 'POST', + headers: { + Accept: 'application/json', + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + id: resourceId, + limit: '32000', + }), + }); const responseData = await response.json(); return responseData.result.records; } async getResourceMetadata(resourceId: string) { const response = await fetchRetry( - `${ - this.DMS - }/api/3/action/resource_show?id=${resourceId}`, + `${this.DMS}/api/3/action/resource_show?id=${resourceId}`, 3 ); const responseData = await response.json(); @@ -359,17 +325,14 @@ export default class CKAN { } async getResourceInfo(resourceId: string) { - const response = await fetch( - `${this.DMS}/api/3/action/datastore_info`, - { - method: 'POST', - headers: { - Accept: 'application/json', - 'Content-Type': 'application/json', - }, - body: JSON.stringify({ resource_id: resourceId }), - } - ); + const response = await fetch(`${this.DMS}/api/3/action/datastore_info`, { + method: 'POST', + headers: { + Accept: 'application/json', + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ resource_id: resourceId }), + }); const responseData = await response.json(); const resourceInfo: Array = responseData.result; return resourceInfo; @@ -377,9 +340,7 @@ export default class CKAN { async getFacetFields(field: 'res_format' | 'tags') { const response = await fetchRetry( - `${ - this.DMS - }/api/3/action/package_search?facet.field=["${field}"]&rows=0`, + `${this.DMS}/api/3/action/package_search?facet.field=["${field}"]&rows=0`, 3 ); const responseData = await response.json(); diff --git a/packages/ckan/src/lib/utils.ts b/packages/ckan/src/lib/utils.ts index 3be03c89..fa720773 100644 --- a/packages/ckan/src/lib/utils.ts +++ b/packages/ckan/src/lib/utils.ts @@ -9,10 +9,14 @@ export function getDaysAgo(date: string) { return (+today - +createdOn) / msInDay; } -export default async function fetchRetry(url: string, n: number): Promise { +export default async function fetchRetry( + url: string, + n: number, + opts: Partial = {} +): Promise { const abortController = new AbortController(); const id = setTimeout(() => abortController.abort(), 30000); - const res = await fetch(url, { signal: abortController.signal }); + const res = await fetch(url, { signal: abortController.signal, ...opts }); clearTimeout(id); if (!res.ok && n && n > 0) { return await fetchRetry(url, n - 1); @@ -21,13 +25,13 @@ export default async function fetchRetry(url: string, n: number): Promise { } export function removeTag(tag?: string) { - if (tag === "{{description}}" || !tag) { + if (tag === '{{description}}' || !tag) { return undefined; } - if (typeof window !== "undefined") { - const div = document.createElement("div"); + if (typeof window !== 'undefined') { + const div = document.createElement('div'); div.innerHTML = tag; - return div.textContent || div.innerText || ""; + return div.textContent || div.innerText || ''; } return tag; } @@ -38,10 +42,10 @@ export function convertFieldSchema( ) { function convertToGraphqlString(fieldName: string) { return fieldName - .replaceAll(" ", "_") - .replaceAll("(", "_") - .replaceAll(")", "_") - .replace(/[^\w\s]|(_)\1/gi, "_"); + .replaceAll(' ', '_') + .replaceAll('(', '_') + .replaceAll(')', '_') + .replace(/[^\w\s]|(_)\1/gi, '_'); } const entries = Object.entries(schema); return {