Skip to content

Commit

Permalink
⭐ Pass login info from front to back (#139)
Browse files Browse the repository at this point in the history
* ⭐ Pass login info from front to back

* 'loginInfo' no longer needs to be a global variable; it is passed from
from top-level to all functions that require it

* Temporarily remove code of updating & deleting labels/milestones

* Numerous minor updates
  • Loading branch information
dongskyler authored Mar 22, 2021
1 parent 8948338 commit 29ca259
Show file tree
Hide file tree
Showing 26 changed files with 315 additions and 198 deletions.
5 changes: 3 additions & 2 deletions src/core/apiCalls/apiResponseGetter.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ import {
export const getApiResponseOfListing = (loginInfo, entryType, action) =>
getResponseOfHttpGETFromPaginatedApi(loginInfo, entryType, action);

export const getApiResponseOfCreating = (loginInfo, entryType, body) =>
getResponseOfHttpPOST(loginInfo, entryType, body);
export const getApiResponseOfCreating = (loginInfo, entryType, entry) => {
return getResponseOfHttpPOST(loginInfo, entryType, entry);
};
1 change: 1 addition & 0 deletions src/core/apiCalls/httpRequests/HttpError.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export default class HttpError extends Error {
constructor(response = {}) {
let message = 'HTTP request failed';

const responseStatus = response.status;

if (responseStatus) {
Expand Down
79 changes: 64 additions & 15 deletions src/core/apiCalls/httpRequests/httpRequestBodyBuilder.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,76 @@
export const buildHttpRequestBodyForLabels = () => {
export const buildHttpRequestBodyForLabelToBeCreatedOnGithub = (label) => {
const { name, description, color } = label;
const colorWithoutLeadingHex = color.substring(1);

const body = {
name: 'type: feature request',
color: '001122',
description: 'Suggestion to new features',
name,
description,
color: colorWithoutLeadingHex,
};

return body;
};

export const buildHttpRequestBodyForMilestones = () => {
export const buildHttpRequestBodyForLabel = (loginInfo, label) => {
const { domain } = loginInfo;
const { action } = label;

const funcToBuildHttpRequestBodyForLabelToBe = {
github: {
create: (_label) => buildHttpRequestBodyForLabelToBeCreatedOnGithub(_label),
},
};

const buildHttpRequestBodyForLabelToBe =
funcToBuildHttpRequestBodyForLabelToBe[domain][action];

const body = buildHttpRequestBodyForLabelToBe(label);
return body;
};

export const buildHttpRequestBodyForMilestoneToBeCreatedOnGithub = (milestone) => {
const { title, state, description, dueOn } = milestone;

const body = {
title: 'Backlog',
state: 'open',
description: 'Work not yet planned for a specific release',
due_on: '2030-04-01T09:00:00Z',
title,
state,
description,
due_on: dueOn,
};

return body;
};

export const buildHttpRequestBodyForMilestone = (loginInfo, milestone) => {
const { domain } = loginInfo;
const { action } = milestone;

const funcToBuildHttpRequestBodyForMilestoneToBe = {
github: {
create: (_milestone) =>
buildHttpRequestBodyForMilestoneToBeCreatedOnGithub(_milestone),
},
};

const buildHttpRequestBodyForMilestoneToBe =
funcToBuildHttpRequestBodyForMilestoneToBe[domain][action];

const body = buildHttpRequestBodyForMilestoneToBe(milestone);
return body;
};

export const buildHttpRequestBody = (entryType) => {
const httpRequestBodyBuilders = {
labels: buildHttpRequestBodyForLabels(),
milestones: buildHttpRequestBodyForMilestones(),
export const buildHttpRequestBody = (loginInfo, entryType, entry) => {
const funcToBuildHttpRequestBodyForEntryType = {
labels: (_loginInfo, label) => buildHttpRequestBodyForLabel(_loginInfo, label),
milestones: (_loginInfo, milestone) =>
buildHttpRequestBodyForMilestone(_loginInfo, milestone),
};
const body = httpRequestBodyBuilders[entryType];
return JSON.stringify(body);

const buildHttpRequestBodyForEntryType =
funcToBuildHttpRequestBodyForEntryType[entryType];

const body = buildHttpRequestBodyForEntryType(loginInfo, entry);

// Note: the returned body is not stringified
return body;
};
1 change: 1 addition & 0 deletions src/core/apiCalls/httpRequests/httpRequestHeaderBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ export const buildHttpRequestHeader = (loginInfo) => {
Accept: buildAcceptHeaderForHttpRequest(),
Authorization: buildAuthorizationHeaderForHttpRequest(loginInfo),
};

return headers;
};
8 changes: 4 additions & 4 deletions src/core/apiCalls/httpRequests/httpRequestMaker.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { buildHttpRequestHeader } from './httpRequestHeaderBuilder';

export const obtainHttpMethodsThatDoNotHaveBody = () => new Set().add('GET');

export const returnTrueIfNoNeedToAddHttpRequestBody = (method) => {
const obtainHttpMethodsThatDoNotHaveBody = () => new Set().add('GET').add('DELETE');
const httpMethodsThatDoNotHaveBody = obtainHttpMethodsThatDoNotHaveBody();
const noNeedToAddBody = httpMethodsThatDoNotHaveBody.has(method);

return noNeedToAddBody;
};

Expand All @@ -17,7 +15,9 @@ export const buildOptionsForFetchApi = (loginInfo, method, body = {}) => {

const noNeedToAddBody = returnTrueIfNoNeedToAddHttpRequestBody(method);

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

const optionsWithBody = {
...optionsWithoutBody,
Expand Down
15 changes: 12 additions & 3 deletions src/core/apiCalls/httpRequests/httpRequestResponseGetter.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import HttpError from './HttpError';
import { buildHttpRequestBody } from './httpRequestBodyBuilder';
import { makeHttpRequest } from './httpRequestMaker';
import {
buildUriForHttpRequestGET,
Expand All @@ -9,13 +10,18 @@ import { parseLinkHeaderFromHttpResponse } from './linkHeaderParser';
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 (loginInfo, entryType, action, uri) => {
const uriForHttpRequest =
uri || buildUriForHttpRequestGET(loginInfo, entryType, action);

const response = await getResponseOfHttpRequest(loginInfo, 'GET', uriForHttpRequest);
return response;
};
Expand Down Expand Up @@ -43,7 +49,9 @@ export const getResponseOfHttpGETFromPaginatedApi = async (
uriOfNextPage,
} = await parseResponseOfHttpGETFromPaginatedApi(response);

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

const combineResponseBodyWithNextPage = async () => [
...responseBody,
Expand All @@ -59,8 +67,9 @@ export const getResponseOfHttpGETFromPaginatedApi = async (
return responseBodyCombinedWithNextPage;
};

export const getResponseOfHttpPOST = async (loginInfo, entryType, body) => {
export const getResponseOfHttpPOST = async (loginInfo, entryType, entry) => {
const uri = buildUriForHttpRequestPOST(loginInfo, entryType);
const body = buildHttpRequestBody(loginInfo, entryType, entry);
const response = await getResponseOfHttpRequest(loginInfo, 'POST', uri, body);
const responseBody = await response.json();
return responseBody;
Expand Down
7 changes: 5 additions & 2 deletions src/core/apiCalls/httpRequests/httpRequestUriBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export const getUriBuilderOfListing = (entryType) => {
labels: (uri) => uri,
milestones: (uri) => `${uri}&state=all`,
};

return uriBuilders[entryType];
};

Expand All @@ -13,6 +14,7 @@ export const buildUriToList = (uri, entryType) => {
const apiPaginationLimit = 100;
return `${uri1}?per_page=${apiPaginationLimit}&page=1`;
};

const uriWithParams = appendPaginationToUri(uri);
const buildUri = getUriBuilderOfListing(entryType);
return buildUri(uriWithParams);
Expand All @@ -21,12 +23,13 @@ export const buildUriToList = (uri, entryType) => {
export const buildUriToCreate = (uri) => uri;

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

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

Expand Down
1 change: 0 additions & 1 deletion src/core/apiCalls/httpRequests/linkHeaderParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,5 @@ export const parseLinkHeaderFromHttpResponse = (response) => {
const { headers } = response;
const rawLinkHeader = headers.get('link');
const parsedLinkHeader = parseLinkHeader(rawLinkHeader);

return parsedLinkHeader;
};
12 changes: 10 additions & 2 deletions src/core/communicationsWithApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ export const makeApiCallToCopy = async (loginInfo, entryType) => {
return getApiResponseOfListing(loginInfo, entryType, 'copy');
};

export const makeApiCallToCreate = async (loginInfo, entryType, body) => {
return getApiResponseOfCreating(loginInfo, entryType, body);
export const makeApiCallToCreate = async (loginInfo, entryType, entry) => {
return getApiResponseOfCreating(loginInfo, entryType, entry);
};

// export const commitChangesByMakingApiCalls = (inputFromFrontEnd) => {
// const { loginInfo, labels, milestones } = inputFromFrontEnd;

// // parse labels for next step
// const processedLabelsForApiCalls = processLabelsForApiCalls(loginInfo, labels);
// const processedMilestonesForApiCalls = processMilestonesForApiCalls(loginInfo, labels);
// };
5 changes: 3 additions & 2 deletions src/core/defaultEntries/generateUniqueId.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const generateUniqueId = () => {
const datetimeStr = Date.now().toString(36);
const randomStr = Math.random().toString(36).substring(2);
const id = `n-${datetimeStr}-${randomStr}`;
const randomStr0 = Math.random().toString(36).substring(2, 12);
const randomStr1 = Math.random().toString(36).substring(2, 12);
const id = `n-${datetimeStr}-${randomStr0}-${randomStr1}`;
return id;
};

Expand Down
13 changes: 3 additions & 10 deletions src/core/loginInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const getKeyToLoginInfo = (repoOwnerOrRepoName, homeRepoOrTemplateRepo) =
name: 'templateRepoName',
},
};

return keys[homeRepoOrTemplateRepo][repoOwnerOrRepoName];
};

Expand All @@ -22,17 +23,9 @@ export const getRepoInfoFromLoginInfo = (
};

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

if (actionIsToCopyEntriesFromAntemplateRepo) {
homeRepoOrTemplateRepo = 'other';
} else {
homeRepoOrTemplateRepo = 'home';
}

const actionIsToCopyEntriesFromTemplateRepo = action === 'copy';
const homeRepoOrTemplateRepo = actionIsToCopyEntriesFromTemplateRepo ? 'other' : 'home';
const repoOwner = getRepoInfoFromLoginInfo(loginInfo, 'owner', homeRepoOrTemplateRepo);
const repoName = getRepoInfoFromLoginInfo(loginInfo, 'name', homeRepoOrTemplateRepo);

return { repoOwner, repoName };
};
6 changes: 5 additions & 1 deletion src/core/validations/validationMethods/validateOrThrow.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
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
1 change: 0 additions & 1 deletion src/core/validations/validationOfAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@ export const getValidActions = () => new Set(['list', 'copy', 'create', 'update'

export const validateActionOrThrow = (action) => {
const isValid = validateOrThrow(action, getValidActions, InvalidActionError);

return isValid;
};
1 change: 0 additions & 1 deletion src/core/validations/validationOfEntryType.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@ export const getValidEntryTypes = () => new Set(['labels', 'milestones']);

export const validateEntryTypeOrThrow = (entryType) => {
const isValid = validateOrThrow(entryType, getValidEntryTypes, InvalidEntryTypeError);

return isValid;
};
13 changes: 7 additions & 6 deletions src/test/apiCalls/httpRequests/getApiResponseOfCreating.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@ import { expect } from 'chai';

import { buildAcceptHeaderForHttpRequest } from '../../../core/apiCalls/httpRequests/httpRequestHeaderBuilder';
import { getApiResponseOfCreating } from '../../../core/apiCalls/apiResponseGetter';
import { getBaseApiUriSlashRepos } from '../../../core/apiCalls/httpRequests/httpRequestUriBuilder';
import {
mockHttpServerSetup,
mockHttpServerCleanup,
mockHttpServerForPOSTOnSuccess,
} from '../../mockHttpServer';
import { getDummyNewLabel } from '../../dummyData/dummyLabel.setup.test';
import { getDummyLoginInfo } from '../../dummyData/dummyLoginInfo.setup.test';

describe('Test getApiResponseOfCreating', function () {
let uriForHttpPost;
let loginInfo;
let dummyLabel;

before(function () {
mockHttpServerSetup();
uriForHttpPost = getBaseApiUriSlashRepos();
loginInfo = getDummyLoginInfo();
dummyLabel = getDummyNewLabel();
});

after(function () {
Expand All @@ -23,13 +26,11 @@ describe('Test getApiResponseOfCreating', function () {

describe('with HTTP responses on success', function () {
let body;
// let response;

describe('the HTTP request sent', function () {
beforeEach(async function () {
mockHttpServerForPOSTOnSuccess();

body = await getApiResponseOfCreating(uriForHttpPost, {});
body = await getApiResponseOfCreating(loginInfo, 'labels', dummyLabel);
});

describe('the HTTP request sent', function () {
Expand Down
Loading

0 comments on commit 29ca259

Please sign in to comment.