diff --git a/dist/esm/api/assets/index.js b/dist/esm/api/assets/index.js new file mode 100644 index 0000000..19c5e91 --- /dev/null +++ b/dist/esm/api/assets/index.js @@ -0,0 +1,3 @@ +import { fetchRawData } from './search'; +import { fetchTypes } from './types'; +export { fetchRawData, fetchTypes }; diff --git a/dist/esm/api/assets/search/index.js b/dist/esm/api/assets/search/index.js new file mode 100644 index 0000000..80b2e9b --- /dev/null +++ b/dist/esm/api/assets/search/index.js @@ -0,0 +1,37 @@ +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); +}; +import { API_ASSETS_SEARCH_PATH } from '../../routes'; +import { QUERY_MIN_LENGTH, TYPES_MIN_SELECTED } from '../../../commons/constants'; +import { fetchData } from '../../egeria-fetch'; +import { getQueryParamsPath } from '../../../commons/helpers'; +/** + * + * @param formData should contain all the query params from the URL + * @param apiUrl is an optional parameter but it is used if API is deployed + * in a different location + * @returns empty array if conditions aren't met otherwise it will fetch data + * from the API + * + * This function is used to fetch data for Asset Catalog. + * + */ +const fetchRawData = (formData, apiUrl) => __awaiter(void 0, void 0, void 0, function* () { + const { q, types } = formData; + if (q.length >= QUERY_MIN_LENGTH && types.length >= TYPES_MIN_SELECTED) { + const _queryParams = getQueryParamsPath(formData); + const path = `${apiUrl || ''}${API_ASSETS_SEARCH_PATH}${_queryParams.length ? `?${_queryParams.join('&')}` : ``}`; + const rawData = yield fetchData(path, 'GET'); + return rawData; + } + else { + return []; + } +}); +export { fetchRawData }; diff --git a/dist/esm/api/assets/types/index.js b/dist/esm/api/assets/types/index.js new file mode 100644 index 0000000..77fdf3d --- /dev/null +++ b/dist/esm/api/assets/types/index.js @@ -0,0 +1,31 @@ +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); +}; +import { API_ASSETS_TYPES_PATH } from '../../routes'; +import { fetchData } from '../../egeria-fetch'; +/** + * @param apiUrl is an optional parameter but it is used if API is deployed + * in a different location + * + * This function is used to fetch Asset Types. + * +*/ +const fetchTypes = (apiUrl) => __awaiter(void 0, void 0, void 0, function* () { + let typesData = yield fetchData(`${apiUrl || ''}${API_ASSETS_TYPES_PATH}`, 'GET'); + typesData = [ + ...typesData.map((d) => { + return { + value: d.name, + label: d.name + }; + }) + ]; + return typesData; +}); +export { fetchTypes }; diff --git a/dist/esm/api/egeria-fetch.js b/dist/esm/api/egeria-fetch.js index 04a7fba..0c747a1 100644 --- a/dist/esm/api/egeria-fetch.js +++ b/dist/esm/api/egeria-fetch.js @@ -1,7 +1,34 @@ -import { handleResponse } from '../auth'; +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); +}; +import { handleResponse, authHeader } from '../auth'; const egeriaFetch = (endpoint, method, headers, options) => { const requestOptions = Object.assign({ method: method, headers: headers }, options); const apiUrl = process.env.REACT_APP_API_URL || ''; return fetch(`${apiUrl}${endpoint}`, requestOptions).then(handleResponse); }; -export { egeriaFetch }; +/* + * @param uri is the full URL + * @param method usual fetch methods such as 'GET', 'POST', etc. + * @param callback this is an optional callback function that can be used + * in a different context to get the response and manage it + * in a custom manner + * @returns + */ +const fetchData = (uri, method, callback) => __awaiter(void 0, void 0, void 0, function* () { + const res = yield egeriaFetch(uri, method, Object.assign({}, authHeader()), {}); + const data = yield res.json(); + if (callback) { + callback(data); + } + else { + return data; + } +}); +export { egeriaFetch, fetchData }; diff --git a/dist/esm/api/index.js b/dist/esm/api/index.js index b912a55..9d2e8ba 100644 --- a/dist/esm/api/index.js +++ b/dist/esm/api/index.js @@ -1,7 +1,9 @@ import { glossaries } from './data/glossaries'; import { lineage } from './data/lineage'; import { types } from './data/types'; -import { egeriaFetch } from './egeria-fetch'; +import { egeriaFetch, fetchData } from './egeria-fetch'; +import { fetchRawData, fetchTypes } from './assets'; +import { API_ASSETS_SEARCH_PATH, API_ASSETS_TYPES_PATH } from './routes'; const apiUrl = () => { return `${process.env.REACT_APP_API_URL}`; }; @@ -10,4 +12,4 @@ function goHome() { window.location.href = '/'; } ; -export { apiUrl, egeriaFetch, glossaries, goHome, lineage, types }; +export { API_ASSETS_SEARCH_PATH, API_ASSETS_TYPES_PATH, apiUrl, egeriaFetch, fetchData, fetchRawData, fetchTypes, glossaries, goHome, lineage, types, }; diff --git a/dist/esm/api/routes.js b/dist/esm/api/routes.js new file mode 100644 index 0000000..f0e0372 --- /dev/null +++ b/dist/esm/api/routes.js @@ -0,0 +1,6 @@ +/** + * CONSTANTS used for API URLs + */ +const API_ASSETS_SEARCH_PATH = '/api/assets/search'; +const API_ASSETS_TYPES_PATH = '/api/assets/types'; +export { API_ASSETS_SEARCH_PATH, API_ASSETS_TYPES_PATH }; diff --git a/dist/esm/commons/constants.js b/dist/esm/commons/constants.js new file mode 100644 index 0000000..c81626b --- /dev/null +++ b/dist/esm/commons/constants.js @@ -0,0 +1,7 @@ +/** + * CONSTANTS used through the entire application. + */ +const QUERY_MIN_LENGTH = 3; +const TYPES_MIN_SELECTED = 1; +const PAGE_SIZE_INCREASE_VALUE = 25; +export { PAGE_SIZE_INCREASE_VALUE, QUERY_MIN_LENGTH, TYPES_MIN_SELECTED }; diff --git a/dist/esm/commons/helpers.js b/dist/esm/commons/helpers.js new file mode 100644 index 0000000..4bb42fe --- /dev/null +++ b/dist/esm/commons/helpers.js @@ -0,0 +1,38 @@ +import { PAGE_SIZE_INCREASE_VALUE } from './constants'; +/** + * + * @param formData should contain all the query params from the URL + * @returns an array of query params prepared to be concatenated in order to + * form a valid URL path + */ +const getQueryParamsPath = (formData) => { + const { q, exactMatch, caseSensitive, types, pageSize } = formData; + let queryParams = []; + if (q) { + queryParams.push(`q=${q}`); + } + if (types && types.length > 0) { + queryParams.push(`types=${types.join(',')}`); + } + queryParams.push(`exactMatch=${exactMatch}`); + queryParams.push(`caseSensitive=${caseSensitive}`); + queryParams.push(`pageSize=${pageSize}`); + return queryParams; +}; +/** + * + * @param searchParams react-router-dom object that contains and object with all + * query params + * @returns a new object with validated query params input for Asset Catalog + */ +const getQueryParams = (searchParams) => { + var _a; + return { + q: searchParams.get('q') || '', + types: ((_a = searchParams.get('types')) === null || _a === void 0 ? void 0 : _a.split(',')) || [], + exactMatch: searchParams.get('exactMatch') === "true" ? true : false, + caseSensitive: searchParams.get('caseSensitive') === "true" ? true : false, + pageSize: searchParams.get('pageSize') ? parseInt(searchParams.get('pageSize')) : PAGE_SIZE_INCREASE_VALUE + }; +}; +export { getQueryParams, getQueryParamsPath }; diff --git a/dist/esm/commons/index.js b/dist/esm/commons/index.js new file mode 100644 index 0000000..be203dc --- /dev/null +++ b/dist/esm/commons/index.js @@ -0,0 +1,4 @@ +import { QUERY_MIN_LENGTH, TYPES_MIN_SELECTED, PAGE_SIZE_INCREASE_VALUE } from './constants'; +import { getQueryParams, getQueryParamsPath } from './helpers'; +import { ASSET_CATALOG_PATH } from './paths'; +export { QUERY_MIN_LENGTH, TYPES_MIN_SELECTED, PAGE_SIZE_INCREASE_VALUE, getQueryParams, getQueryParamsPath, ASSET_CATALOG_PATH }; diff --git a/dist/esm/commons/paths.js b/dist/esm/commons/paths.js new file mode 100644 index 0000000..8432e9d --- /dev/null +++ b/dist/esm/commons/paths.js @@ -0,0 +1,5 @@ +/** + * CONSTANTS used for main application paths (deep linking). + */ +const ASSET_CATALOG_PATH = '/assets/catalog'; +export { ASSET_CATALOG_PATH }; diff --git a/dist/esm/main.js b/dist/esm/main.js index 8d2ab7c..23f9df0 100644 --- a/dist/esm/main.js +++ b/dist/esm/main.js @@ -2,6 +2,7 @@ import { iconsMap, itemGroupIconMap, getIconByGroup, linksTypeIconMap, menuIcons import { lineageViewsTypesMapping } from './navigation-types-mappings'; import { capitalize } from './capitalize'; import { getComponent } from './get-component'; -import { apiUrl, egeriaFetch, glossaries, goHome, lineage, types } from './api'; +import { apiUrl, egeriaFetch, fetchRawData, fetchTypes, glossaries, goHome, lineage, types, } from './api'; import { authHeader, authHeaderWithContentType, currentJwt, handleResponse, login, logout, parseJwt, setToken } from './auth'; -export { apiUrl, authHeader, authHeaderWithContentType, capitalize, currentJwt, egeriaFetch, getComponent, getIconByGroup, glossaries, goHome, handleResponse, iconsMap, itemGroupIconMap, lineage, lineageViewsTypesMapping, linksTypeIconMap, login, logout, menuIcons, parseJwt, setToken, types }; +import { PAGE_SIZE_INCREASE_VALUE, getQueryParams, getQueryParamsPath, ASSET_CATALOG_PATH } from './commons'; +export { apiUrl, authHeader, authHeaderWithContentType, capitalize, currentJwt, egeriaFetch, getComponent, getIconByGroup, glossaries, goHome, handleResponse, iconsMap, itemGroupIconMap, lineage, lineageViewsTypesMapping, linksTypeIconMap, login, logout, menuIcons, parseJwt, setToken, types, ASSET_CATALOG_PATH, PAGE_SIZE_INCREASE_VALUE, getQueryParams, getQueryParamsPath, fetchTypes, fetchRawData }; diff --git a/dist/esm/types/formData.js b/dist/esm/types/formData.js new file mode 100644 index 0000000..cb0ff5c --- /dev/null +++ b/dist/esm/types/formData.js @@ -0,0 +1 @@ +export {}; diff --git a/dist/types/api/assets/index.d.ts b/dist/types/api/assets/index.d.ts new file mode 100644 index 0000000..19c5e91 --- /dev/null +++ b/dist/types/api/assets/index.d.ts @@ -0,0 +1,3 @@ +import { fetchRawData } from './search'; +import { fetchTypes } from './types'; +export { fetchRawData, fetchTypes }; diff --git a/dist/types/api/assets/search/index.d.ts b/dist/types/api/assets/search/index.d.ts new file mode 100644 index 0000000..67520b1 --- /dev/null +++ b/dist/types/api/assets/search/index.d.ts @@ -0,0 +1,14 @@ +import { formData } from '../../../types/formData'; +/** + * + * @param formData should contain all the query params from the URL + * @param apiUrl is an optional parameter but it is used if API is deployed + * in a different location + * @returns empty array if conditions aren't met otherwise it will fetch data + * from the API + * + * This function is used to fetch data for Asset Catalog. + * + */ +declare const fetchRawData: (formData: formData, apiUrl?: string) => Promise; +export { fetchRawData }; diff --git a/dist/types/api/assets/types/index.d.ts b/dist/types/api/assets/types/index.d.ts new file mode 100644 index 0000000..890a935 --- /dev/null +++ b/dist/types/api/assets/types/index.d.ts @@ -0,0 +1,9 @@ +/** + * @param apiUrl is an optional parameter but it is used if API is deployed + * in a different location + * + * This function is used to fetch Asset Types. + * +*/ +declare const fetchTypes: (apiUrl?: string) => Promise; +export { fetchTypes }; diff --git a/dist/types/api/egeria-fetch.d.ts b/dist/types/api/egeria-fetch.d.ts index ab9b512..641e7d7 100644 --- a/dist/types/api/egeria-fetch.d.ts +++ b/dist/types/api/egeria-fetch.d.ts @@ -1,2 +1,3 @@ declare const egeriaFetch: (endpoint: string, method: string, headers: any, options: any) => Promise; -export { egeriaFetch }; +declare const fetchData: (uri: string, method: string, callback?: Function) => Promise; +export { egeriaFetch, fetchData }; diff --git a/dist/types/api/index.d.ts b/dist/types/api/index.d.ts index 159a689..b481098 100644 --- a/dist/types/api/index.d.ts +++ b/dist/types/api/index.d.ts @@ -1,7 +1,9 @@ import { glossaries } from './data/glossaries'; import { lineage } from './data/lineage'; import { types } from './data/types'; -import { egeriaFetch } from './egeria-fetch'; +import { egeriaFetch, fetchData } from './egeria-fetch'; +import { fetchRawData, fetchTypes } from './assets'; +import { API_ASSETS_SEARCH_PATH, API_ASSETS_TYPES_PATH } from './routes'; declare const apiUrl: () => string; declare function goHome(): void; -export { apiUrl, egeriaFetch, glossaries, goHome, lineage, types }; +export { API_ASSETS_SEARCH_PATH, API_ASSETS_TYPES_PATH, apiUrl, egeriaFetch, fetchData, fetchRawData, fetchTypes, glossaries, goHome, lineage, types, }; diff --git a/dist/types/api/routes.d.ts b/dist/types/api/routes.d.ts new file mode 100644 index 0000000..6faa610 --- /dev/null +++ b/dist/types/api/routes.d.ts @@ -0,0 +1,6 @@ +/** + * CONSTANTS used for API URLs + */ +declare const API_ASSETS_SEARCH_PATH = "/api/assets/search"; +declare const API_ASSETS_TYPES_PATH = "/api/assets/types"; +export { API_ASSETS_SEARCH_PATH, API_ASSETS_TYPES_PATH }; diff --git a/dist/types/commons/constants.d.ts b/dist/types/commons/constants.d.ts new file mode 100644 index 0000000..c4178dd --- /dev/null +++ b/dist/types/commons/constants.d.ts @@ -0,0 +1,7 @@ +/** + * CONSTANTS used through the entire application. + */ +declare const QUERY_MIN_LENGTH = 3; +declare const TYPES_MIN_SELECTED = 1; +declare const PAGE_SIZE_INCREASE_VALUE = 25; +export { PAGE_SIZE_INCREASE_VALUE, QUERY_MIN_LENGTH, TYPES_MIN_SELECTED }; diff --git a/dist/types/commons/helpers.d.ts b/dist/types/commons/helpers.d.ts new file mode 100644 index 0000000..03a1a3a --- /dev/null +++ b/dist/types/commons/helpers.d.ts @@ -0,0 +1,22 @@ +import { formData } from '../types/formData'; +/** + * + * @param formData should contain all the query params from the URL + * @returns an array of query params prepared to be concatenated in order to + * form a valid URL path + */ +declare const getQueryParamsPath: (formData: formData) => any; +/** + * + * @param searchParams react-router-dom object that contains and object with all + * query params + * @returns a new object with validated query params input for Asset Catalog + */ +declare const getQueryParams: (searchParams: any) => { + q: any; + types: any; + exactMatch: boolean; + caseSensitive: boolean; + pageSize: number; +}; +export { getQueryParams, getQueryParamsPath }; diff --git a/dist/types/commons/index.d.ts b/dist/types/commons/index.d.ts new file mode 100644 index 0000000..be203dc --- /dev/null +++ b/dist/types/commons/index.d.ts @@ -0,0 +1,4 @@ +import { QUERY_MIN_LENGTH, TYPES_MIN_SELECTED, PAGE_SIZE_INCREASE_VALUE } from './constants'; +import { getQueryParams, getQueryParamsPath } from './helpers'; +import { ASSET_CATALOG_PATH } from './paths'; +export { QUERY_MIN_LENGTH, TYPES_MIN_SELECTED, PAGE_SIZE_INCREASE_VALUE, getQueryParams, getQueryParamsPath, ASSET_CATALOG_PATH }; diff --git a/dist/types/commons/paths.d.ts b/dist/types/commons/paths.d.ts new file mode 100644 index 0000000..3302ad8 --- /dev/null +++ b/dist/types/commons/paths.d.ts @@ -0,0 +1,5 @@ +/** + * CONSTANTS used for main application paths (deep linking). + */ +declare const ASSET_CATALOG_PATH = "/assets/catalog"; +export { ASSET_CATALOG_PATH }; diff --git a/dist/types/main.d.ts b/dist/types/main.d.ts index 8d2ab7c..4f3c460 100644 --- a/dist/types/main.d.ts +++ b/dist/types/main.d.ts @@ -2,6 +2,9 @@ import { iconsMap, itemGroupIconMap, getIconByGroup, linksTypeIconMap, menuIcons import { lineageViewsTypesMapping } from './navigation-types-mappings'; import { capitalize } from './capitalize'; import { getComponent } from './get-component'; -import { apiUrl, egeriaFetch, glossaries, goHome, lineage, types } from './api'; +import { apiUrl, egeriaFetch, fetchRawData, fetchTypes, glossaries, goHome, lineage, types } from './api'; import { authHeader, authHeaderWithContentType, currentJwt, handleResponse, login, logout, parseJwt, setToken } from './auth'; -export { apiUrl, authHeader, authHeaderWithContentType, capitalize, currentJwt, egeriaFetch, getComponent, getIconByGroup, glossaries, goHome, handleResponse, iconsMap, itemGroupIconMap, lineage, lineageViewsTypesMapping, linksTypeIconMap, login, logout, menuIcons, parseJwt, setToken, types }; +import { PAGE_SIZE_INCREASE_VALUE, getQueryParams, getQueryParamsPath, ASSET_CATALOG_PATH } from './commons'; +import { formData } from './types/formData'; +export type { formData }; +export { apiUrl, authHeader, authHeaderWithContentType, capitalize, currentJwt, egeriaFetch, getComponent, getIconByGroup, glossaries, goHome, handleResponse, iconsMap, itemGroupIconMap, lineage, lineageViewsTypesMapping, linksTypeIconMap, login, logout, menuIcons, parseJwt, setToken, types, ASSET_CATALOG_PATH, PAGE_SIZE_INCREASE_VALUE, getQueryParams, getQueryParamsPath, fetchTypes, fetchRawData }; diff --git a/dist/types/types/formData.d.ts b/dist/types/types/formData.d.ts new file mode 100644 index 0000000..43d3084 --- /dev/null +++ b/dist/types/types/formData.d.ts @@ -0,0 +1,12 @@ +/** + * Interface used for the Asset Catalog form. + * It's describing the attributes types used. + */ +interface formData { + caseSensitive: boolean; + exactMatch: boolean; + pageSize: number; + q: string; + types: Array; +} +export type { formData }; diff --git a/package-lock.json b/package-lock.json index dacdd09..bf4b5fa 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "egeria-js-commons", - "version": "4.0.0", + "version": "4.0.1", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "egeria-js-commons", - "version": "4.0.0", + "version": "4.0.1", "license": "SEE LICENSE IN LICENSE", "devDependencies": { "@types/node": "^18.8.3", diff --git a/package.json b/package.json index 49e9f31..a774859 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "egeria-js-commons", - "version": "4.0.0", + "version": "4.0.1", "description": "Egeria JS Commons repository, abstract JS library", "files": [ "dist" diff --git a/src/api/assets/index.ts b/src/api/assets/index.ts new file mode 100644 index 0000000..39fdbd0 --- /dev/null +++ b/src/api/assets/index.ts @@ -0,0 +1,7 @@ +import { fetchRawData } from './search'; +import { fetchTypes } from './types'; + +export { + fetchRawData, + fetchTypes +} diff --git a/src/api/assets/search/index.ts b/src/api/assets/search/index.ts new file mode 100644 index 0000000..4e4426a --- /dev/null +++ b/src/api/assets/search/index.ts @@ -0,0 +1,36 @@ +import { API_ASSETS_SEARCH_PATH } from '../../routes'; +import { QUERY_MIN_LENGTH, TYPES_MIN_SELECTED } from '../../../commons/constants'; +import { formData } from '../../../types/formData'; +import { fetchData } from '../../egeria-fetch'; +import { getQueryParamsPath } from '../../../commons/helpers'; + +/** + * + * @param formData should contain all the query params from the URL + * @param apiUrl is an optional parameter but it is used if API is deployed + * in a different location + * @returns empty array if conditions aren't met otherwise it will fetch data + * from the API + * + * This function is used to fetch data for Asset Catalog. + * + */ +const fetchRawData = async (formData: formData, apiUrl?: string) => { + const {q, types} = formData; + + if(q.length >= QUERY_MIN_LENGTH && types.length >= TYPES_MIN_SELECTED) { + const _queryParams = getQueryParamsPath(formData); + + const path = `${apiUrl || ''}${API_ASSETS_SEARCH_PATH}${_queryParams.length ? `?${_queryParams.join('&')}` : ``}`; + + const rawData = await fetchData(path, 'GET'); + + return rawData; + } else { + return []; + } +}; + +export { + fetchRawData +} diff --git a/src/api/assets/types/index.ts b/src/api/assets/types/index.ts new file mode 100644 index 0000000..706cf86 --- /dev/null +++ b/src/api/assets/types/index.ts @@ -0,0 +1,29 @@ +import { API_ASSETS_TYPES_PATH } from '../../routes'; + +import { fetchData } from '../../egeria-fetch'; + +/** + * @param apiUrl is an optional parameter but it is used if API is deployed + * in a different location + * + * This function is used to fetch Asset Types. + * +*/ +const fetchTypes = async (apiUrl?: string) => { + let typesData = await fetchData(`${apiUrl || ''}${API_ASSETS_TYPES_PATH}`, 'GET'); + + typesData = [ + ...typesData.map((d: any) => { + return { + value: d.name, + label: d.name + }; + }) + ]; + + return typesData; +}; + +export { + fetchTypes +} diff --git a/src/api/egeria-fetch.ts b/src/api/egeria-fetch.ts index f57dc8d..c27cdf0 100644 --- a/src/api/egeria-fetch.ts +++ b/src/api/egeria-fetch.ts @@ -1,4 +1,7 @@ -import { handleResponse } from '../auth'; +import { + handleResponse, + authHeader +} from '../auth'; const egeriaFetch = (endpoint: string, method : string, headers : any, options: any) => { const requestOptions: any = { @@ -12,6 +15,26 @@ const egeriaFetch = (endpoint: string, method : string, headers : any, options: return fetch(`${apiUrl}${endpoint}`, requestOptions).then(handleResponse); } +/* + * @param uri is the full URL + * @param method usual fetch methods such as 'GET', 'POST', etc. + * @param callback this is an optional callback function that can be used + * in a different context to get the response and manage it + * in a custom manner + * @returns + */ +const fetchData = async (uri: string, method: string, callback?: Function) => { + const res = await egeriaFetch(uri, method, {...authHeader()}, {}); + const data = await res.json(); + + if(callback) { + callback(data); + } else { + return data; + } +}; + export { - egeriaFetch + egeriaFetch, + fetchData }; \ No newline at end of file diff --git a/src/api/index.ts b/src/api/index.ts index 75e3570..3d69b42 100644 --- a/src/api/index.ts +++ b/src/api/index.ts @@ -1,7 +1,16 @@ import { glossaries } from './data/glossaries'; import { lineage } from './data/lineage'; import { types } from './data/types'; -import { egeriaFetch } from './egeria-fetch'; +import { egeriaFetch, fetchData } from './egeria-fetch'; +import { + fetchRawData, + fetchTypes +} from './assets'; + +import { + API_ASSETS_SEARCH_PATH, + API_ASSETS_TYPES_PATH +} from './routes'; const apiUrl = () => { return `${process.env.REACT_APP_API_URL}`; @@ -14,10 +23,15 @@ function goHome() { }; export { + API_ASSETS_SEARCH_PATH, + API_ASSETS_TYPES_PATH, apiUrl, egeriaFetch, + fetchData, + fetchRawData, + fetchTypes, glossaries, goHome, lineage, - types + types, } diff --git a/src/api/routes.ts b/src/api/routes.ts new file mode 100644 index 0000000..5117621 --- /dev/null +++ b/src/api/routes.ts @@ -0,0 +1,10 @@ +/** + * CONSTANTS used for API URLs + */ +const API_ASSETS_SEARCH_PATH = '/api/assets/search'; +const API_ASSETS_TYPES_PATH = '/api/assets/types'; + +export { + API_ASSETS_SEARCH_PATH, + API_ASSETS_TYPES_PATH +} \ No newline at end of file diff --git a/src/commons/constants.ts b/src/commons/constants.ts new file mode 100644 index 0000000..0dc6210 --- /dev/null +++ b/src/commons/constants.ts @@ -0,0 +1,12 @@ +/** + * CONSTANTS used through the entire application. + */ +const QUERY_MIN_LENGTH = 3; +const TYPES_MIN_SELECTED = 1; +const PAGE_SIZE_INCREASE_VALUE = 25; + +export { + PAGE_SIZE_INCREASE_VALUE, + QUERY_MIN_LENGTH, + TYPES_MIN_SELECTED +} \ No newline at end of file diff --git a/src/commons/helpers.ts b/src/commons/helpers.ts new file mode 100644 index 0000000..fe8d30e --- /dev/null +++ b/src/commons/helpers.ts @@ -0,0 +1,49 @@ +import { formData } from '../types/formData'; +import { PAGE_SIZE_INCREASE_VALUE } from './constants'; + +/** + * + * @param formData should contain all the query params from the URL + * @returns an array of query params prepared to be concatenated in order to + * form a valid URL path + */ +const getQueryParamsPath = (formData: formData) => { + const {q, exactMatch, caseSensitive, types, pageSize } = formData; + + let queryParams = [] as any; + + if(q) { + queryParams.push(`q=${q}`); + } + + if(types && types.length > 0) { + queryParams.push(`types=${types.join(',')}`); + } + + queryParams.push(`exactMatch=${exactMatch}`); + queryParams.push(`caseSensitive=${caseSensitive}`); + queryParams.push(`pageSize=${pageSize}`); + + return queryParams; +}; + +/** + * + * @param searchParams react-router-dom object that contains and object with all + * query params + * @returns a new object with validated query params input for Asset Catalog + */ +const getQueryParams = (searchParams: any) => { + return { + q: searchParams.get('q') || '', + types: searchParams.get('types')?.split(',') || [], + exactMatch: searchParams.get('exactMatch') === "true" ? true : false, + caseSensitive: searchParams.get('caseSensitive') === "true" ? true : false, + pageSize: searchParams.get('pageSize') ? parseInt(searchParams.get('pageSize')) : PAGE_SIZE_INCREASE_VALUE + }; +}; + +export { + getQueryParams, + getQueryParamsPath +} \ No newline at end of file diff --git a/src/commons/index.ts b/src/commons/index.ts new file mode 100644 index 0000000..408c4e3 --- /dev/null +++ b/src/commons/index.ts @@ -0,0 +1,24 @@ +import { + QUERY_MIN_LENGTH, + TYPES_MIN_SELECTED, + PAGE_SIZE_INCREASE_VALUE +} from './constants'; + +import { + getQueryParams, + getQueryParamsPath +} from './helpers'; + +import { ASSET_CATALOG_PATH } from './paths'; + +export { + QUERY_MIN_LENGTH, + TYPES_MIN_SELECTED, + PAGE_SIZE_INCREASE_VALUE, + + getQueryParams, + getQueryParamsPath, + + ASSET_CATALOG_PATH +} + diff --git a/src/commons/paths.ts b/src/commons/paths.ts new file mode 100644 index 0000000..48153bb --- /dev/null +++ b/src/commons/paths.ts @@ -0,0 +1,8 @@ +/** + * CONSTANTS used for main application paths (deep linking). + */ +const ASSET_CATALOG_PATH = '/assets/catalog'; + +export { + ASSET_CATALOG_PATH +} diff --git a/src/main.ts b/src/main.ts index e595e5c..a3129ef 100644 --- a/src/main.ts +++ b/src/main.ts @@ -19,12 +19,17 @@ import { } from './get-component'; import { + API_ASSETS_SEARCH_PATH, + API_ASSETS_TYPES_PATH, apiUrl, egeriaFetch, + fetchData, + fetchRawData, + fetchTypes, glossaries, goHome, lineage, - types + types, } from './api'; import { @@ -38,6 +43,19 @@ import { setToken } from './auth'; +import { + QUERY_MIN_LENGTH, + TYPES_MIN_SELECTED, + PAGE_SIZE_INCREASE_VALUE, + getQueryParams, + getQueryParamsPath, + ASSET_CATALOG_PATH +} from './commons'; + +import { formData } from './types/formData'; + +export type { formData }; + export { apiUrl, authHeader, @@ -60,5 +78,12 @@ export { menuIcons, parseJwt, setToken, - types + types, + + ASSET_CATALOG_PATH, + PAGE_SIZE_INCREASE_VALUE, + getQueryParams, + getQueryParamsPath, + fetchTypes, + fetchRawData }; diff --git a/src/types/formData.ts b/src/types/formData.ts new file mode 100644 index 0000000..8a06f80 --- /dev/null +++ b/src/types/formData.ts @@ -0,0 +1,16 @@ + +/** + * Interface used for the Asset Catalog form. + * It's describing the attributes types used. + */ +interface formData { + caseSensitive: boolean, + exactMatch: boolean, + pageSize: number, + q: string, + types: Array +} + +export type { + formData +} \ No newline at end of file