Skip to content

Commit

Permalink
⭐ Adjust code as submodule of its front-end counterpart (#84)
Browse files Browse the repository at this point in the history
* Major adjustment to make this code base fit with its front-end counterpart
* However, no new features were added
  • Loading branch information
dongskyler authored Jan 15, 2021
1 parent aa16ff7 commit 8948338
Show file tree
Hide file tree
Showing 36 changed files with 276 additions and 356 deletions.
7 changes: 4 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export { makeApiCallToList, makeApiCallToCopy, makeApiCallToCreate } from './src/core';

export {
makeApiCallToList,
makeApiCallToCopy,
makeApiCallToCreate,
generateDefaultLabel,
generateDefaultMilestone,
} from './src/core/defaultEntries';
} from './src/core';
8 changes: 4 additions & 4 deletions src/core/apiCalls/apiResponseGetter.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import {
getResponseOfHttpPOST,
} from './httpRequests/httpRequestResponseGetter';

export const getApiResponseOfListing = (entryType, action) =>
getResponseOfHttpGETFromPaginatedApi(entryType, action);
export const getApiResponseOfListing = (loginInfo, entryType, action) =>
getResponseOfHttpGETFromPaginatedApi(loginInfo, entryType, action);

export const getApiResponseOfCreating = (entryType, body) =>
getResponseOfHttpPOST(entryType, body);
export const getApiResponseOfCreating = (loginInfo, entryType, body) =>
getResponseOfHttpPOST(loginInfo, entryType, body);
19 changes: 5 additions & 14 deletions src/core/apiCalls/httpRequests/httpRequestBodyBuilder.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import { validateEntryTypeOrThrow } from '../../validations';

export const buildHttpRequestBodyForLabels = () => {
const body = {
name: 'type: feature request',
color: '001122',
description: 'Suggestion to new features',
};

return body;
};

Expand All @@ -17,20 +14,14 @@ export const buildHttpRequestBodyForMilestones = () => {
description: 'Work not yet planned for a specific release',
due_on: '2030-04-01T09:00:00Z',
};

return body;
};

export const buildHttpRequestBody = (entryType) => {
validateEntryTypeOrThrow(entryType);

let body = {};

if (entryType === 'labels') {
body = buildHttpRequestBodyForLabels();
} else {
body = buildHttpRequestBodyForMilestones();
}

const httpRequestBodyBuilders = {
labels: buildHttpRequestBodyForLabels(),
milestones: buildHttpRequestBodyForMilestones(),
};
const body = httpRequestBodyBuilders[entryType];
return JSON.stringify(body);
};
12 changes: 4 additions & 8 deletions src/core/apiCalls/httpRequests/httpRequestHeaderBuilder.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
import { getLoginInfo } from '../../loginInfo';

export const buildAcceptHeaderForHttpRequest = () => 'application/vnd.github.v3+json';

export const buildAuthorizationHeaderForHttpRequest = () => {
const { token } = getLoginInfo();

export const buildAuthorizationHeaderForHttpRequest = (loginInfo) => {
const { token } = loginInfo;
return `token ${token}`;
};

export const buildHttpRequestHeader = () => {
export const buildHttpRequestHeader = (loginInfo) => {
const headers = {
Accept: buildAcceptHeaderForHttpRequest(),
Authorization: buildAuthorizationHeaderForHttpRequest(),
Authorization: buildAuthorizationHeaderForHttpRequest(loginInfo),
};

return headers;
};
12 changes: 5 additions & 7 deletions src/core/apiCalls/httpRequests/httpRequestMaker.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,15 @@ export const returnTrueIfNoNeedToAddHttpRequestBody = (method) => {
return noNeedToAddBody;
};

export const buildOptionsForFetchApi = (method, body = {}) => {
export const buildOptionsForFetchApi = (loginInfo, method, body = {}) => {
const optionsWithoutBody = {
method,
headers: buildHttpRequestHeader(),
headers: buildHttpRequestHeader(loginInfo),
};

const noNeedToAddBody = returnTrueIfNoNeedToAddHttpRequestBody(method);

if (noNeedToAddBody) {
return optionsWithoutBody;
}
if (noNeedToAddBody) return optionsWithoutBody;

const optionsWithBody = {
...optionsWithoutBody,
Expand All @@ -29,7 +27,7 @@ export const buildOptionsForFetchApi = (method, body = {}) => {
return optionsWithBody;
};

export const makeHttpRequest = (method, uri, body) => {
const options = buildOptionsForFetchApi(method, body);
export const makeHttpRequest = (loginInfo, method, uri, body) => {
const options = buildOptionsForFetchApi(loginInfo, method, body);
return fetch(uri, options);
};
41 changes: 19 additions & 22 deletions src/core/apiCalls/httpRequests/httpRequestResponseGetter.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,17 @@ import {
} from './httpRequestUriBuilder';
import { parseLinkHeaderFromHttpResponse } from './linkHeaderParser';

export const getResponseOfHttpRequest = async (method, uri, body = {}) => {
const response = await makeHttpRequest(method, uri, body);
export const getResponseOfHttpRequest = async (loginInfo, method, uri, body = {}) => {
const response = await makeHttpRequest(loginInfo, method, uri, body);
const httpRequestFailed = !response.ok;

if (httpRequestFailed) {
throw new HttpError(response);
}

if (httpRequestFailed) throw new HttpError(response);
return response;
};

export const getResponseOfHttpGET = async (entryType, action, uri) => {
const uriForHttpRequest = uri || buildUriForHttpRequestGET(entryType, action);
const response = await getResponseOfHttpRequest('GET', uriForHttpRequest);

export const getResponseOfHttpGET = async (loginInfo, entryType, action, uri) => {
const uriForHttpRequest =
uri || buildUriForHttpRequestGET(loginInfo, entryType, action);
const response = await getResponseOfHttpRequest(loginInfo, 'GET', uriForHttpRequest);
return response;
};

Expand All @@ -30,41 +26,42 @@ export const parseResponseOfHttpGETFromPaginatedApi = async (response) => {
const nextPage = 'next';
const thereIsNoNextPage = !(nextPage in linkHeader);
const uriOfNextPage = linkHeader[nextPage] || null;

return { responseBody, thereIsNoNextPage, uriOfNextPage };
};

export const getResponseOfHttpGETFromPaginatedApi = async (
loginInfo,
entryType,
action,
uri = null
) => {
const response = await getResponseOfHttpGET(entryType, action, uri);
const response = await getResponseOfHttpGET(loginInfo, entryType, action, uri);

const {
responseBody,
thereIsNoNextPage,
uriOfNextPage,
} = await parseResponseOfHttpGETFromPaginatedApi(response);

if (thereIsNoNextPage) {
return responseBody;
}
if (thereIsNoNextPage) return responseBody;

const combineResponseBodyWithNextPage = async () => [
...responseBody,
...(await getResponseOfHttpGETFromPaginatedApi(entryType, action, uriOfNextPage)),
...(await getResponseOfHttpGETFromPaginatedApi(
loginInfo,
entryType,
action,
uriOfNextPage
)),
];

const responseBodyCombinedWithNextPage = await combineResponseBodyWithNextPage();

return responseBodyCombinedWithNextPage;
};

export const getResponseOfHttpPOST = async (entryType, body) => {
const uri = buildUriForHttpRequestPOST(entryType);
const response = await getResponseOfHttpRequest('POST', uri, body);
export const getResponseOfHttpPOST = async (loginInfo, entryType, body) => {
const uri = buildUriForHttpRequestPOST(loginInfo, entryType);
const response = await getResponseOfHttpRequest(loginInfo, 'POST', uri, body);
const responseBody = await response.json();

return responseBody;
};
56 changes: 22 additions & 34 deletions src/core/apiCalls/httpRequests/httpRequestUriBuilder.js
Original file line number Diff line number Diff line change
@@ -1,60 +1,48 @@
import { getRepoOwnerAndRepoName } from '../../loginInfo';

export const getBaseApiUri = () => 'https://api.github.com';

export const getBaseApiUriSlashRepos = () => `${getBaseApiUri()}/repos`;

export const getApiPaginationLimit = () => 100;

export const appendPaginationToUri = (uri) =>
`${uri}?per_page=${getApiPaginationLimit()}&page=1`;

export const buildUriToListLabels = (uri) => uri;

export const buildUriToListMilestones = (uri) => `${uri}&state=all`;

export const getUriBuilderOfListing = (entryType) => {
const uriBuilders = {
labels: (uri) => buildUriToListLabels(uri),
milestones: (uri) => buildUriToListMilestones(uri),
labels: (uri) => uri,
milestones: (uri) => `${uri}&state=all`,
};

return uriBuilders[entryType];
};

export const buildUriToList = (uri) => {
export const buildUriToList = (uri, entryType) => {
const appendPaginationToUri = (uri1) => {
const apiPaginationLimit = 100;
return `${uri1}?per_page=${apiPaginationLimit}&page=1`;
};
const uriWithParams = appendPaginationToUri(uri);

const regex = /\/(?<entryType>labels|milestones)$/;
const { entryType } = uri.match(regex).groups;

const buildUri = getUriBuilderOfListing(entryType);

return buildUri(uriWithParams);
};

export const buildUriToCreate = (uri) => uri;

export const getUriBuilderOfAction = (action) => {
const uriBuilderFunctions = {
list: (uri) => buildUriToList(uri),
copy: (uri) => buildUriToList(uri),
list: (uri, entryType) => buildUriToList(uri, entryType),
copy: (uri, entryType) => buildUriToList(uri, entryType),
create: (uri) => buildUriToCreate(uri),
};

return uriBuilderFunctions[action];
const buildFunc = uriBuilderFunctions[action];
return buildFunc;
};

export const buildUriForHttpRequest = (entryType, action, entryIdentifier = null) => {
const { repoOwner, repoName } = getRepoOwnerAndRepoName(action);
export const getBaseApiUri = () => 'https://api.github.com';

export const getBaseApiUriSlashRepos = () => `${getBaseApiUri()}/repos`;

export const buildUriForHttpRequest = (loginInfo, entryType, action) => {
const { repoOwner, repoName } = getRepoOwnerAndRepoName(loginInfo, action);
const uri = `${getBaseApiUriSlashRepos()}/${repoOwner}/${repoName}/${entryType}`;
const buildUri = getUriBuilderOfAction(action);

return buildUri(uri, entryIdentifier);
return buildUri(uri, entryType);
};

export const buildUriForHttpRequestGET = (entryType, action) =>
buildUriForHttpRequest(entryType, action);
export const buildUriForHttpRequestGET = (loginInfo, entryType, action) =>
buildUriForHttpRequest(loginInfo, entryType, action);

export const buildUriForHttpRequestPOST = (entryType) =>
buildUriForHttpRequest(entryType, 'create');
export const buildUriForHttpRequestPOST = (loginInfo, entryType) =>
buildUriForHttpRequest(loginInfo, entryType, 'create');
19 changes: 6 additions & 13 deletions src/core/communicationsWithApi.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
import { getApiResponseOfListing, getApiResponseOfCreating } from './apiCalls';
import { validateEntryTypeOrThrow } from './validations';

export const makeApiCallToList = async (entryType) => {
validateEntryTypeOrThrow(entryType);

return getApiResponseOfListing(entryType, 'list');
export const makeApiCallToList = async (loginInfo, entryType) => {
return getApiResponseOfListing(loginInfo, entryType, 'list');
};

export const makeApiCallToCopy = async (entryType) => {
validateEntryTypeOrThrow(entryType);

return getApiResponseOfListing(entryType, 'copy');
export const makeApiCallToCopy = async (loginInfo, entryType) => {
return getApiResponseOfListing(loginInfo, entryType, 'copy');
};

export const makeApiCallToCreate = async (entryType, body) => {
validateEntryTypeOrThrow(entryType);

return getApiResponseOfCreating(entryType, body);
export const makeApiCallToCreate = async (loginInfo, entryType, body) => {
return getApiResponseOfCreating(loginInfo, entryType, body);
};
2 changes: 2 additions & 0 deletions src/core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ export {
makeApiCallToCopy,
makeApiCallToCreate,
} from './communicationsWithApi';

export { generateDefaultLabel, generateDefaultMilestone } from './defaultEntries';
25 changes: 9 additions & 16 deletions src/core/loginInfo.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
import { getDummyLoginInfo } from '../test/dummyData/dummyLoginInfo.setup.test';

export const getLoginInfo = () => getDummyLoginInfo();

export const getKeyToLoginInfo = (repoOwnerOrRepoName, homeRepoOrTemplateRepo) => {
const keys = {
home: {
Expand All @@ -13,20 +9,20 @@ export const getKeyToLoginInfo = (repoOwnerOrRepoName, homeRepoOrTemplateRepo) =
name: 'templateRepoName',
},
};

return keys[homeRepoOrTemplateRepo][repoOwnerOrRepoName];
};

export const getRepoInfoFromLoginInfo = (repoOwnerOrRepoName, homeRepoOrTemplateRepo) => {
export const getRepoInfoFromLoginInfo = (
loginInfo,
repoOwnerOrRepoName,
homeRepoOrTemplateRepo
) => {
const key = getKeyToLoginInfo(repoOwnerOrRepoName, homeRepoOrTemplateRepo);
const loginInfo = getLoginInfo();

return loginInfo[key];
};

export const getRepoOwnerAndRepoName = (action) => {
export const getRepoOwnerAndRepoName = (loginInfo, action) => {
let homeRepoOrTemplateRepo;

const actionIsToCopyEntriesFromAntemplateRepo = action === 'copy';

if (actionIsToCopyEntriesFromAntemplateRepo) {
Expand All @@ -35,11 +31,8 @@ export const getRepoOwnerAndRepoName = (action) => {
homeRepoOrTemplateRepo = 'home';
}

const repoOwner = getRepoInfoFromLoginInfo('owner', homeRepoOrTemplateRepo);
const repoName = getRepoInfoFromLoginInfo('name', homeRepoOrTemplateRepo);
const repoOwner = getRepoInfoFromLoginInfo(loginInfo, 'owner', homeRepoOrTemplateRepo);
const repoName = getRepoInfoFromLoginInfo(loginInfo, 'name', homeRepoOrTemplateRepo);

return {
repoOwner,
repoName,
};
return { repoOwner, repoName };
};
6 changes: 1 addition & 5 deletions src/core/validations/validationMethods/validateOrThrow.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
const validateOrThrow = (item, getValidItems, ErrorClass) => {
const validItems = getValidItems();
const givenItemIsInvalid = !validItems.has(item);

if (givenItemIsInvalid) {
throw new ErrorClass(item);
}

if (givenItemIsInvalid) throw new ErrorClass(item);
return true;
};

Expand Down
Loading

0 comments on commit 8948338

Please sign in to comment.