Skip to content
This repository has been archived by the owner on Jun 10, 2024. It is now read-only.

Commit

Permalink
Merge pull request #27 from sarbull/main
Browse files Browse the repository at this point in the history
Extract AssetCatalog API logic
  • Loading branch information
sarbull authored Oct 13, 2022
2 parents 23fe738 + 7db03cf commit 1132113
Show file tree
Hide file tree
Showing 38 changed files with 523 additions and 20 deletions.
3 changes: 3 additions & 0 deletions dist/esm/api/assets/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { fetchRawData } from './search';
import { fetchTypes } from './types';
export { fetchRawData, fetchTypes };
37 changes: 37 additions & 0 deletions dist/esm/api/assets/search/index.js
Original file line number Diff line number Diff line change
@@ -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 };
31 changes: 31 additions & 0 deletions dist/esm/api/assets/types/index.js
Original file line number Diff line number Diff line change
@@ -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 };
31 changes: 29 additions & 2 deletions dist/esm/api/egeria-fetch.js
Original file line number Diff line number Diff line change
@@ -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 };
6 changes: 4 additions & 2 deletions dist/esm/api/index.js
Original file line number Diff line number Diff line change
@@ -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}`;
};
Expand All @@ -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, };
6 changes: 6 additions & 0 deletions dist/esm/api/routes.js
Original file line number Diff line number Diff line change
@@ -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 };
7 changes: 7 additions & 0 deletions dist/esm/commons/constants.js
Original file line number Diff line number Diff line change
@@ -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 };
38 changes: 38 additions & 0 deletions dist/esm/commons/helpers.js
Original file line number Diff line number Diff line change
@@ -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 };
4 changes: 4 additions & 0 deletions dist/esm/commons/index.js
Original file line number Diff line number Diff line change
@@ -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 };
5 changes: 5 additions & 0 deletions dist/esm/commons/paths.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/**
* CONSTANTS used for main application paths (deep linking).
*/
const ASSET_CATALOG_PATH = '/assets/catalog';
export { ASSET_CATALOG_PATH };
5 changes: 3 additions & 2 deletions dist/esm/main.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions dist/esm/types/formData.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {};
3 changes: 3 additions & 0 deletions dist/types/api/assets/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { fetchRawData } from './search';
import { fetchTypes } from './types';
export { fetchRawData, fetchTypes };
14 changes: 14 additions & 0 deletions dist/types/api/assets/search/index.d.ts
Original file line number Diff line number Diff line change
@@ -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<any>;
export { fetchRawData };
9 changes: 9 additions & 0 deletions dist/types/api/assets/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -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<any>;
export { fetchTypes };
3 changes: 2 additions & 1 deletion dist/types/api/egeria-fetch.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
declare const egeriaFetch: (endpoint: string, method: string, headers: any, options: any) => Promise<any>;
export { egeriaFetch };
declare const fetchData: (uri: string, method: string, callback?: Function) => Promise<any>;
export { egeriaFetch, fetchData };
6 changes: 4 additions & 2 deletions dist/types/api/index.d.ts
Original file line number Diff line number Diff line change
@@ -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, };
6 changes: 6 additions & 0 deletions dist/types/api/routes.d.ts
Original file line number Diff line number Diff line change
@@ -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 };
7 changes: 7 additions & 0 deletions dist/types/commons/constants.d.ts
Original file line number Diff line number Diff line change
@@ -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 };
22 changes: 22 additions & 0 deletions dist/types/commons/helpers.d.ts
Original file line number Diff line number Diff line change
@@ -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 };
4 changes: 4 additions & 0 deletions dist/types/commons/index.d.ts
Original file line number Diff line number Diff line change
@@ -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 };
5 changes: 5 additions & 0 deletions dist/types/commons/paths.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/**
* CONSTANTS used for main application paths (deep linking).
*/
declare const ASSET_CATALOG_PATH = "/assets/catalog";
export { ASSET_CATALOG_PATH };
7 changes: 5 additions & 2 deletions dist/types/main.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
12 changes: 12 additions & 0 deletions dist/types/types/formData.d.ts
Original file line number Diff line number Diff line change
@@ -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<string>;
}
export type { formData };
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
7 changes: 7 additions & 0 deletions src/api/assets/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { fetchRawData } from './search';
import { fetchTypes } from './types';

export {
fetchRawData,
fetchTypes
}
36 changes: 36 additions & 0 deletions src/api/assets/search/index.ts
Original file line number Diff line number Diff line change
@@ -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
}
Loading

0 comments on commit 1132113

Please sign in to comment.