diff --git a/src/api/index.js b/src/api/index.js index dbf1fc7fa..6d0f50258 100644 --- a/src/api/index.js +++ b/src/api/index.js @@ -6,424 +6,308 @@ import { abbreviateNumber } from 'utils'; export const CLIENT_ID = '87c7f05700c052937cfb'; export const CLIENT_SECRET = '3a70aee4d5e26c457720a31c3efe2f9062a4997a'; -export const root = 'https://api.github.com'; -export const USER_ENDPOINT = user => `${root}/users/${user}`; - -const accessTokenParameters = accessToken => ({ - headers: { - Accept: 'application/vnd.github.v3+json', - Authorization: `token ${accessToken}`, - 'Cache-Control': 'no-cache', +const ACCEPT = { + JSON: 'application/vnd.github.v3+json', + HTML: 'application/vnd.github.v3.html+json', + DIFF: 'application/vnd.github.v3.diff+json', + RAW: 'application/vnd.github.v3.raw+json', +}; + +const METHOD = { + GET: 'GET', + HEAD: 'HEAD', + PUT: 'PUT', + DELETE: 'DELETE', + PATCH: 'PATCH', + POST: 'POST', +}; + +export const v3 = { + root: 'https://api.github.com', + call: async (url, parameters) => { + const finalUrl = url.indexOf(v3.root) === 0 ? url : `${v3.root}${url}`; + const response = await fetch(finalUrl, parameters); + + return response; }, -}); - -const accessTokenParametersHEAD = accessToken => ({ - method: 'HEAD', - headers: { - Accept: 'application/vnd.github.v3+json', - Authorization: `token ${accessToken}`, - 'Cache-Control': 'no-cache', + parameters: ( + accessToken, + method = METHOD.GET, + accept = ACCEPT.JSON, + body = {} + ) => { + const withBody = [METHOD.PUT, METHOD.PATCH, METHOD.POST]; + const params = { + method, + headers: { + Accept: accept, + Authorization: `token ${accessToken}`, + 'Cache-Control': 'no-cache', + }, + }; + + if (withBody.indexOf(method) !== -1) { + params.body = JSON.stringify(body); + if (method === METHOD.PUT) { + params.headers['Content-Length'] = 0; + } + } + + return params; }, -}); + count: async (url, accessToken) => { + const finalUrl = + url.indexOf('?') !== -1 ? `${url}&per_page=1` : `${url}?per_page=1`; + const response = await v3.get(finalUrl, accessToken); -const accessTokenParametersPUT = (accessToken, body = {}) => ({ - method: 'PUT', - headers: { - Accept: 'application/vnd.github.v3+json', - Authorization: `token ${accessToken}`, - 'Content-Length': 0, - }, - body: JSON.stringify(body), -}); + if (response.status === 404) { + return 0; + } -const accessTokenParametersDELETE = accessToken => ({ - method: 'DELETE', - headers: { - Accept: 'application/vnd.github.v3+json', - Authorization: `token ${accessToken}`, - }, -}); + let linkHeader = response.headers.get('Link'); + let number = 1; -const accessTokenParametersPATCH = (editParams, accessToken) => ({ - method: 'PATCH', - headers: { - Accept: 'application/vnd.github.v3+json', - Authorization: `token ${accessToken}`, - }, - body: JSON.stringify(editParams), -}); + if (linkHeader !== null) { + linkHeader = linkHeader.match(/page=(\d)+/g).pop(); + number = linkHeader.split('=').pop(); + } -const accessTokenParametersPOST = (accessToken, body) => ({ - method: 'POST', - headers: { - Accept: 'application/vnd.github.v3+json', - Authorization: `token ${accessToken}`, + return abbreviateNumber(number); }, - body: JSON.stringify(body), -}); + delete: async (url, accessToken) => { + const response = await v3.call( + url, + v3.parameters(accessToken, METHOD.DELETE) + ); -const accessTokenParametersHTML = accessToken => ({ - headers: { - Accept: 'application/vnd.github.v3.html+json', - Authorization: `token ${accessToken}`, + return response; }, -}); + get: async (url, accessToken) => { + const response = await v3.call(url, v3.parameters(accessToken)); -const accessTokenParametersDiff = accessToken => ({ - headers: { - Accept: 'application/vnd.github.v3.diff+json', - Authorization: `token ${accessToken}`, + return response; }, -}); + getDiff: async (url, accessToken) => { + const response = await v3.call( + url, + v3.parameters(accessToken, METHOD.GET, ACCEPT.DIFF) + ); -const accessTokenParametersRaw = accessToken => ({ - headers: { - Accept: 'application/vnd.github.v3.raw+json', - Authorization: `token ${accessToken}`, + return response.text(); }, -}); + getHtml: async (url, accessToken) => { + const response = await v3.call( + url, + v3.parameters(accessToken, METHOD.GET, ACCEPT.HTML) + ); -const authParameters = (code, state) => ({ - method: 'POST', - headers: { - Accept: 'application/json', - 'Content-Type': 'application/json', + return response.text(); }, - body: JSON.stringify({ - client_id: CLIENT_ID, - client_secret: CLIENT_SECRET, - code, - state, - }), -}); - -export async function fetchUrl(url, accessToken) { - const response = await fetch(url, accessTokenParameters(accessToken)); - - return response.json(); -} - -export async function fetchUrlNormal(url, accessToken) { - const response = await fetch(url, accessTokenParameters(accessToken)); - - return response; -} - -export async function fetchUrlHead(url, accessToken) { - const response = await fetch(url, accessTokenParametersHEAD(accessToken)); - - return response; -} + getJson: async (url, accessToken) => { + const response = await v3.call(url, v3.parameters(accessToken)); -export async function fetchUrlFile(url, accessToken) { - const response = await fetch(url, accessTokenParametersRaw(accessToken)); - - return response.text(); -} - -export async function fetchCommentHTML(url, accessToken) { - const response = await fetch(url, accessTokenParameters(accessToken)); - - return response.json(); -} - -export async function fetchAccessToken(code, state) { - const GITHUB_OAUTH_ENDPOINT = 'https://github.com/login/oauth/access_token'; - const response = await fetch( - GITHUB_OAUTH_ENDPOINT, - authParameters(code, state) - ); - - return response.json(); -} - -export async function fetchAuthUser(accessToken) { - const FETCH_AUTH_USER_ENDPOINT = `${root}/user`; - const response = await fetch( - FETCH_AUTH_USER_ENDPOINT, - accessTokenParameters(accessToken) - ); - - return response.json(); -} + return response.json(); + }, + getRaw: async (url, accessToken) => { + const response = await v3.call( + url, + v3.parameters(accessToken, METHOD.GET, ACCEPT.RAW) + ); -export async function fetchAuthUserOrgs(accessToken) { - const ORGS_ENDPOINT = `${root}/user/orgs`; - const response = await fetch( - ORGS_ENDPOINT, - accessTokenParameters(accessToken) - ); + return response.text(); + }, + head: async (url, accessToken) => { + const response = await v3.call( + url, + v3.parameters(accessToken, METHOD.HEAD) + ); - return response.json(); -} + return response; + }, + patch: async (url, accessToken, body = {}) => { + const response = await v3.call( + url, + v3.parameters(accessToken, METHOD.PATCH, ACCEPT.JSON, body) + ); -export async function fetchUser(user, accessToken) { - const FETCH_USER_ENDPOINT = `${root}/users/${user}`; - const response = await fetch( - FETCH_USER_ENDPOINT, - accessTokenParameters(accessToken) - ); + return response; + }, + postJson: async (url, accessToken, body = {}) => { + const response = await v3.call( + url, + v3.parameters(accessToken, METHOD.POST, ACCEPT.JSON, body) + ); - return response.json(); -} + return response.json(); + }, + post: async (url, accessToken, body = {}) => { + const response = await v3.call( + url, + v3.parameters(accessToken, METHOD.POST, ACCEPT.JSON, body) + ); -export async function fetchUserOrgs(user, accessToken) { - const ORGS_ENDPOINT = `${root}/users/${user}/orgs`; - const response = await fetch( - ORGS_ENDPOINT, - accessTokenParameters(accessToken) - ); + return response; + }, + put: async (url, accessToken, body = {}) => { + const response = await v3.call( + url, + v3.parameters(accessToken, METHOD.PUT, ACCEPT.JSON, body) + ); - return response.json(); -} + return response; + }, +}; -export async function fetchUserEvents(user, accessToken) { - const EVENTS_ENDPOINT = `${root}/users/${user}/received_events?per_page=100`; - const response = await fetch( - EVENTS_ENDPOINT, - accessTokenParameters(accessToken) - ); +export const fetchAuthUser = accessToken => v3.getJson('/user', accessToken); - return response.json(); -} +export const fetchAuthUserOrgs = accessToken => + v3.getJson('/user/orgs', accessToken); -export async function fetchReadMe(user, repository, accessToken) { - const README_ENDPOINT = `${root}/repos/${user}/${repository}/readme?ref=master`; - const response = await fetch( - README_ENDPOINT, - accessTokenParametersHTML(accessToken) - ); +export const fetchUser = (user, accessToken) => + v3.getJson(`/users/${user}`, accessToken); - return response.text(); -} +export const fetchUserOrgs = (user, accessToken) => + v3.getJson(`/users/${user}/orgs`, accessToken); -export async function fetchOrg(orgName, accessToken) { - const response = await fetch( - `${root}/orgs/${orgName}`, - accessTokenParameters(accessToken) - ); +export const fetchUserEvents = (user, accessToken) => + v3.getJson(`/users/${user}/received_events?per_page=100`, accessToken); - return response.json(); -} +export const fetchReadMe = (user, repository, accessToken) => + v3.getHtml(`/repos/${user}/${repository}/readme?ref=master`, accessToken); -export async function fetchOrgMembers(orgName, accessToken) { - const response = await fetch( - `${root}/orgs/${orgName}/members`, - accessTokenParameters(accessToken) - ); +export const fetchOrg = (orgName, accessToken) => + v3.getJson(`/orgs/${orgName}`, accessToken); - return response.json(); -} +export const fetchOrgMembers = (orgName, accessToken) => + v3.getJson(`/orgs/${orgName}/members`, accessToken); -export async function fetchPostIssueComment( +export const fetchPostIssueComment = ( body, owner, repoName, issueNum, accessToken -) { - const ENDPOINT = `${root}/repos/${owner}/${repoName}/issues/${issueNum}/comments`; - const response = await fetch( - ENDPOINT, - accessTokenParametersPOST(accessToken, { body }) +) => + v3.postJson( + `/repos/${owner}/${repoName}/issues/${issueNum}/comments`, + accessToken, + { body } ); - return response.json(); -} - -export async function fetchEditIssue( +export const fetchEditIssue = ( owner, repoName, issueNum, editParams, updateParams, accessToken -) { - const ENDPOINT = `${root}/repos/${owner}/${repoName}/issues/${issueNum}`; - const response = await fetch( - ENDPOINT, - accessTokenParametersPATCH(editParams, accessToken) +) => + v3.patch( + `/repos/${owner}/${repoName}/issues/${issueNum}`, + accessToken, + editParams ); - return response; -} - -export async function fetchChangeIssueLockStatus( +export const fetchChangeIssueLockStatus = ( owner, repoName, issueNum, currentStatus, accessToken -) { - const ENDPOINT = `${root}/repos/${owner}/${repoName}/issues/${issueNum}/lock`; - const response = await fetch( - ENDPOINT, - currentStatus - ? accessTokenParametersDELETE(accessToken) - : accessTokenParametersPUT(accessToken) +) => + v3[currentStatus ? 'delete' : 'put']( + `/repos/${owner}/${repoName}/issues/${issueNum}/lock`, + accessToken ); - return response; -} - -export async function fetchSearch(type, query, accessToken, params = '') { - const ENDPOINT = `${root}/search/${type}?q=${query}${params}`; - const response = await fetch(ENDPOINT, accessTokenParameters(accessToken)); - - return response.json(); -} - -export async function fetchNotifications(participating, all, accessToken) { - const ENDPOINT = `${root}/notifications?participating=${participating}&all=${all}`; - const response = await fetch(ENDPOINT, accessTokenParameters(accessToken)); +export const fetchSearch = (type, query, accessToken, params = '') => + v3.getJson(`/search/${type}?q=${query}${params}`, accessToken); - return response.json(); -} - -export async function fetchMarkNotificationAsRead(notificationID, accessToken) { - const ENDPOINT = `${root}/notifications/threads/${notificationID}`; - const response = await fetch( - ENDPOINT, - accessTokenParametersPATCH(null, accessToken) +export const fetchNotifications = (participating, all, accessToken) => + v3.getJson( + `/notifications?participating=${participating}&all=${all}`, + accessToken ); - return response; -} +export const fetchMarkNotificationAsRead = (notificationID, accessToken) => + v3.patch(`/notifications/threads/${notificationID}`, accessToken); -export async function fetchMarkRepoNotificationAsRead( - repoFullName, - accessToken -) { - const ENDPOINT = `${root}/repos/${repoFullName}/notifications`; - const response = await fetch(ENDPOINT, accessTokenParametersPUT(accessToken)); +export const fetchMarkRepoNotificationAsRead = (repoFullName, accessToken) => + v3.put(`/repos/${repoFullName}/notifications`, accessToken); - return response; -} +export const fetchChangeStarStatusRepo = (owner, repo, starred, accessToken) => + v3[starred ? 'delete' : 'put'](`/user/starred/${owner}/${repo}`, accessToken); -export async function fetchChangeStarStatusRepo( - owner, - repo, - starred, - accessToken -) { - const ENDPOINT = `${root}/user/starred/${owner}/${repo}`; - const response = await fetch( - ENDPOINT, - starred - ? accessTokenParametersDELETE(accessToken) - : accessTokenParametersPUT(accessToken) - ); +export const fetchForkRepo = (owner, repo, accessToken) => + v3.post(`/repos/${owner}/${repo}/forks`, accessToken); - return response; -} +export const fetchStarCount = (owner, accessToken) => + v3.count(`/users/${owner}/starred`, accessToken); -export async function fetchForkRepo(owner, repo, accessToken) { - const ENDPOINT = `${root}/repos/${owner}/${repo}/forks`; - const response = await fetch( - ENDPOINT, - accessTokenParametersPOST(accessToken) - ); +export const isWatchingRepo = (url, accessToken) => v3.head(url, accessToken); - return response; -} +export const watchRepo = (owner, repo, accessToken) => + v3.put(`/repos/${owner}/${repo}/subscription`, accessToken, { + subscribed: true, + }); -export async function fetchStarCount(owner) { - const ENDPOINT = `${root}/users/${owner}/starred?per_page=1`; - const response = await fetch(ENDPOINT); +export const unWatchRepo = (owner, repo, accessToken) => + v3.delete(`/repos/${owner}/${repo}/subscription`, accessToken); - let linkHeader = response.headers.get('Link'); - let output = ''; +export const fetchChangeFollowStatus = (user, isFollowing, accessToken) => + v3[isFollowing ? 'delete' : 'put'](`/user/following/${user}`, accessToken); - if (linkHeader == null) { - output = response.json().then(data => { - return data.length; - }); - } else { - linkHeader = linkHeader.match(/page=(\d)+/g).pop(); - output = linkHeader.split('=').pop(); - } +export const fetchDiff = (url, accessToken) => v3.getDiff(url, accessToken); - return abbreviateNumber(output); -} +export const fetchMergeStatus = (repo, issueNum, accessToken) => + v3.get(`/repos/${repo}/pulls/${issueNum}/merge`, accessToken); -export async function watchRepo(isSubscribed, owner, repo, accessToken) { - const ENDPOINT = `${root}/repos/${owner}/${repo}/subscription`; - const response = await fetch( - ENDPOINT, - accessTokenParameters(accessToken, { subscribed: isSubscribed }) - ); - - return response; -} - -export async function unWatchRepo(owner, repo, accessToken) { - const ENDPOINT = `${root}/repos/${owner}/${repo}/subscription`; - const response = await fetch(ENDPOINT, accessTokenParameters(accessToken)); - - return response; -} - -export async function fetchChangeFollowStatus(user, isFollowing, accessToken) { - const ENDPOINT = `${root}/user/following/${user}`; - const response = await fetch( - ENDPOINT, - isFollowing - ? accessTokenParametersDELETE(accessToken) - : accessTokenParametersPUT(accessToken) - ); - - return response; -} - -export async function fetchDiff(url, accessToken) { - const response = await fetch(url, accessTokenParametersDiff(accessToken)); - - return response.text(); -} - -export async function fetchMergeStatus(repo, issueNum, accessToken) { - const ENDPOINT = `${root}/repos/${repo}/pulls/${issueNum}/merge`; - const response = await fetch(ENDPOINT, accessTokenParameters(accessToken)); - - return response; -} - -export async function fetchMergePullRequest( +export const fetchMergePullRequest = ( repo, issueNum, commitTitle, commitMessage, mergeMethod, accessToken -) { - const ENDPOINT = `${root}/repos/${repo}/pulls/${issueNum}/merge`; - - const response = await fetch( - ENDPOINT, - accessTokenParametersPUT(accessToken, { - commit_title: commitTitle, - commit_message: commitMessage, - merge_method: mergeMethod, - }) - ); - - return response; -} - -export async function fetchSubmitNewIssue( +) => + v3.put(`/repos/${repo}/pulls/${issueNum}/merge`, accessToken, { + commit_title: commitTitle, + commit_message: commitMessage, + merge_method: mergeMethod, + }); + +export const fetchSubmitNewIssue = ( owner, repo, issueTitle, issueComment, accessToken -) { - const ENDPOINT = `${root}/repos/${owner}/${repo}/issues`; +) => + v3.postJson(`/repos/${owner}/${repo}/issues`, accessToken, { + title: issueTitle, + body: issueComment, + }); + +// Auth +const authParameters = (code, state) => ({ + method: METHOD.POST, + headers: { + Accept: 'application/json', + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + client_id: CLIENT_ID, + client_secret: CLIENT_SECRET, + code, + state, + }), +}); + +export async function fetchAccessToken(code, state) { + const GITHUB_OAUTH_ENDPOINT = 'https://github.com/login/oauth/access_token'; const response = await fetch( - ENDPOINT, - accessTokenParametersPOST(accessToken, { - title: issueTitle, - body: issueComment, - }) + GITHUB_OAUTH_ENDPOINT, + authParameters(code, state) ); return response.json(); diff --git a/src/auth/auth.action.js b/src/auth/auth.action.js index 462333088..7bc2b94f0 100644 --- a/src/auth/auth.action.js +++ b/src/auth/auth.action.js @@ -87,11 +87,12 @@ export const getUser = () => { export const getStarCount = () => { return (dispatch, getState) => { + const accessToken = getState().auth.accessToken; const user = getState().auth.user.login; dispatch({ type: GET_AUTH_STAR_COUNT.PENDING }); - fetchStarCount(user) + fetchStarCount(user, accessToken) .then(data => { dispatch({ type: GET_AUTH_STAR_COUNT.SUCCESS, diff --git a/src/auth/screens/privacy-policy.screen.js b/src/auth/screens/privacy-policy.screen.js index 9ce16642d..f45f54ea2 100644 --- a/src/auth/screens/privacy-policy.screen.js +++ b/src/auth/screens/privacy-policy.screen.js @@ -4,7 +4,7 @@ import { StyleSheet, ScrollView, View, Text } from 'react-native'; import { ViewContainer } from 'components'; import { translate } from 'utils'; import { colors, fonts, normalize } from 'config'; -import { root as apiRoot } from 'api'; +import { v3 } from 'api'; const styles = StyleSheet.create({ container: { @@ -119,7 +119,7 @@ export class PrivacyPolicyScreen extends Component { style={styles.link} onPress={() => navigation.navigate('Repository', { - repositoryUrl: `${apiRoot}/repos/gitpoint/git-point`, + repositoryUrl: `${v3.root}/repos/gitpoint/git-point`, })} > {translate('auth.privacyPolicy.contactLink', language)} diff --git a/src/components/issue-description.component.js b/src/components/issue-description.component.js index a151bf060..b2c5dbfda 100644 --- a/src/components/issue-description.component.js +++ b/src/components/issue-description.component.js @@ -7,7 +7,7 @@ import moment from 'moment/min/moment-with-locales.min'; import { StateBadge, MembersList, LabelButton, DiffBlocks } from 'components'; import { translate } from 'utils'; import { colors, fonts, normalize } from 'config'; -import { root as apiRoot } from 'api'; +import { v3 } from 'api'; const styles = StyleSheet.create({ headerContainer: { @@ -109,7 +109,7 @@ export class IssueDescription extends Component { {issue.repository_url && { dispatch({ type: GET_ISSUE_DIFF.PENDING }); - return fetchDiff(url, accessToken) + return v3 + .getDiff(url, accessToken) .then(data => { dispatch({ type: GET_ISSUE_DIFF.SUCCESS, @@ -79,7 +79,8 @@ export const getIssueComments = issueCommentsURL => { dispatch({ type: GET_ISSUE_COMMENTS.PENDING }); - return fetchCommentHTML(`${issueCommentsURL}?per_page=100`, accessToken) + return v3 + .getJson(`${issueCommentsURL}?per_page=100`, accessToken) .then(data => { dispatch({ type: GET_ISSUE_COMMENTS.SUCCESS, @@ -190,7 +191,8 @@ export const getIssueFromUrl = url => { dispatch({ type: GET_ISSUE_FROM_URL.PENDING }); - return fetchCommentHTML(url, accessToken) + return v3 + .getJson(url, accessToken) .then(issue => { dispatch({ type: GET_ISSUE_FROM_URL.SUCCESS, diff --git a/src/issue/screens/issue.screen.js b/src/issue/screens/issue.screen.js index b739fee0a..e1dd35f12 100644 --- a/src/issue/screens/issue.screen.js +++ b/src/issue/screens/issue.screen.js @@ -18,7 +18,7 @@ import { CommentListItem, CommentInput, } from 'components'; -import { root as apiRoot } from 'api'; +import { v3 } from 'api'; import { translate } from 'utils'; import { colors } from 'config'; import { getRepository, getContributors } from 'repository'; @@ -150,7 +150,7 @@ class Issue extends Component { return node.attribs['data-url'].replace( 'https://github.com', - `${apiRoot}/repos` + `${v3.root}/repos` ); }; @@ -178,7 +178,7 @@ class Issue extends Component { if ( issueParam && repository.full_name !== - issueParam.repository_url.replace(`${apiRoot}/repos/`, '') + issueParam.repository_url.replace(`${v3.route}/repos/`, '') ) { Promise.all([ getRepository(issue.repository_url), diff --git a/src/notifications/screens/notifications.screen.js b/src/notifications/screens/notifications.screen.js index bb0a01937..3339893b8 100644 --- a/src/notifications/screens/notifications.screen.js +++ b/src/notifications/screens/notifications.screen.js @@ -15,7 +15,7 @@ import { } from 'react-native'; import { ButtonGroup, Card, Icon } from 'react-native-elements'; -import { root as apiRoot } from 'api'; +import { v3 } from 'api'; import { ViewContainer, LoadingContainer, @@ -179,7 +179,7 @@ class Notifications extends Component { const { navigation } = this.props; navigation.navigate('Repository', { - repositoryUrl: `${apiRoot}/repos/${fullName}`, + repositoryUrl: `${v3.root}/repos/${fullName}`, }); }; diff --git a/src/organization/organization.action.js b/src/organization/organization.action.js index 51189fb25..19bd0eaae 100644 --- a/src/organization/organization.action.js +++ b/src/organization/organization.action.js @@ -1,10 +1,6 @@ import { createAction } from 'redux-actions'; -import { - fetchOrg, - fetchOrgMembers, - fetchUrl, -} from 'api'; +import { fetchOrg, fetchOrgMembers, v3 } from 'api'; import { GET_ORG, GET_ORG_LOADING, @@ -51,7 +47,8 @@ export const fetchOrganizationRepos = url => (dispatch, getState) => { dispatch(getOrgReposLoading(true)); dispatch(getOrgReposError('')); - fetchUrl(url, accessToken) + v3 + .getJson(url, accessToken) .then(data => { dispatch(getOrgReposLoading(false)); dispatch(getOrgRepos(data)); diff --git a/src/repository/repository.action.js b/src/repository/repository.action.js index ceb7497c8..b6534e025 100644 --- a/src/repository/repository.action.js +++ b/src/repository/repository.action.js @@ -1,16 +1,12 @@ import { - root as apiRoot, - fetchUrl, - fetchUrlNormal, - fetchUrlHead, - fetchUrlFile, - fetchCommentHTML, fetchReadMe, fetchSearch, fetchChangeStarStatusRepo, fetchForkRepo, watchRepo, unWatchRepo, + isWatchingRepo, + v3, } from 'api'; import { GET_REPOSITORY, @@ -37,7 +33,8 @@ export const getRepository = url => { dispatch({ type: GET_REPOSITORY.PENDING }); - return fetchUrl(url, accessToken) + return v3 + .getJson(url, accessToken) .then(data => { dispatch({ type: GET_REPOSITORY.SUCCESS, @@ -59,7 +56,8 @@ export const getContributors = url => { dispatch({ type: GET_REPOSITORY_CONTRIBUTORS.PENDING }); - fetchUrl(url, accessToken) + v3 + .getJson(url, accessToken) .then(data => { dispatch({ type: GET_REPOSITORY_CONTRIBUTORS.SUCCESS, @@ -81,7 +79,8 @@ export const getContents = (url, level) => { dispatch({ type: GET_REPOSITORY_CONTENTS.PENDING }); - fetchUrl(url, accessToken) + v3 + .getJson(url, accessToken) .then(data => { dispatch({ type: GET_REPOSITORY_CONTENTS.SUCCESS, @@ -104,7 +103,8 @@ export const getRepositoryFile = url => { dispatch({ type: GET_REPOSITORY_FILE.PENDING }); - fetchUrlFile(url, accessToken) + v3 + .getRaw(url, accessToken) .then(data => { dispatch({ type: GET_REPOSITORY_FILE.SUCCESS, @@ -126,7 +126,8 @@ export const getIssues = url => { dispatch({ type: GET_REPOSITORY_ISSUES.PENDING }); - fetchCommentHTML(url, accessToken) + v3 + .getJson(url, accessToken) .then(data => { dispatch({ type: GET_REPOSITORY_ISSUES.SUCCESS, @@ -148,7 +149,8 @@ export const checkReadMe = url => { dispatch({ type: GET_REPO_README_STATUS.PENDING }); - fetchUrlHead(url, accessToken) + v3 + .head(url, accessToken) .then(data => { dispatch({ type: GET_REPO_README_STATUS.SUCCESS, @@ -170,7 +172,8 @@ export const checkRepoStarred = url => { dispatch({ type: GET_REPO_STARRED_STATUS.PENDING }); - fetchUrlNormal(url, accessToken) + v3 + .get(url, accessToken) .then(data => { dispatch({ type: GET_REPO_STARRED_STATUS.SUCCESS, @@ -192,13 +195,13 @@ export const checkRepoSubscribed = url => { dispatch({ type: GET_REPOSITORY_SUBSCRIBED_STATUS.PENDING }); - fetchUrlNormal(url, accessToken) - .then(data => + isWatchingRepo(url, accessToken) + .then(data => { dispatch({ type: GET_REPOSITORY_SUBSCRIBED_STATUS.SUCCESS, payload: data.status !== 404, - }) - ) + }); + }) .catch(error => dispatch({ type: GET_REPOSITORY_SUBSCRIBED_STATUS.ERROR, @@ -244,17 +247,17 @@ export const getRepositoryInfo = url => { dispatch(getIssues(issuesUrl)); dispatch( checkReadMe( - `${apiRoot}/repos/${repo.owner.login}/${repo.name}/readme?ref=master` + `${v3.root}/repos/${repo.owner.login}/${repo.name}/readme?ref=master` ) ); dispatch( checkRepoStarred( - `${apiRoot}/user/starred/${repo.owner.login}/${repo.name}` + `${v3.root}/user/starred/${repo.owner.login}/${repo.name}` ) ); dispatch( checkRepoSubscribed( - `${apiRoot}/repos/${repo.owner.login}/${repo.name}/subscription` + `${v3.root}/repos/${repo.owner.login}/${repo.name}/subscription` ) ); }); @@ -285,13 +288,12 @@ export const changeStarStatusRepo = (owner, repo, starred) => { export const subscribeToRepo = (owner, repo) => (dispatch, getState) => { const accessToken = getState().auth.accessToken; - const isSubscribed = getState().repository.subscribed; dispatch({ type: GET_REPOSITORY_SUBSCRIBED_STATUS.PENDING, }); - return watchRepo(!isSubscribed, owner, repo, accessToken) + return watchRepo(owner, repo, accessToken) .then(data => data.json()) .then(result => { dispatch({ @@ -361,7 +363,8 @@ export const getLabels = url => { dispatch({ type: GET_REPOSITORY_LABELS.PENDING }); - fetchUrl(url, accessToken) + v3 + .getJson(url, accessToken) .then(data => { dispatch({ type: GET_REPOSITORY_LABELS.SUCCESS, diff --git a/src/user/user.action.js b/src/user/user.action.js index d9a6d11f5..5fd732f89 100644 --- a/src/user/user.action.js +++ b/src/user/user.action.js @@ -1,13 +1,10 @@ import { fetchUser, fetchUserOrgs, - fetchUrl, - fetchUrlNormal, - USER_ENDPOINT, fetchSearch, fetchChangeFollowStatus, - root as apiRoot, fetchStarCount, + v3, } from 'api'; import { GET_USER, @@ -72,7 +69,8 @@ const checkFollowStatusHelper = (user, followedUser, actionSet) => { dispatch({ type: actionSet.PENDING }); - fetchUrlNormal(`${USER_ENDPOINT(user)}/following/${followedUser}`, accessToken) + v3 + .head(`/users/${user}/following/${followedUser}`, accessToken) .then(data => { dispatch({ type: actionSet.SUCCESS, @@ -106,7 +104,8 @@ export const getFollowers = user => { dispatch({ type: GET_FOLLOWERS.PENDING }); - fetchUrl(`${USER_ENDPOINT(user.login)}/followers?per_page=100`, accessToken) + v3 + .getJson(`/users/${user.login}/followers?per_page=100`, accessToken) .then(data => { dispatch({ type: GET_FOLLOWERS.SUCCESS, @@ -124,18 +123,17 @@ export const getFollowers = user => { export const getUserInfo = user => { return dispatch => { - Promise.all([ - dispatch(getUser(user)), - dispatch(getOrgs(user)), - ]); + Promise.all([dispatch(getUser(user)), dispatch(getOrgs(user))]); }; }; export const getStarCount = user => { - return dispatch => { + return (dispatch, getState) => { + const accessToken = getState().auth.accessToken; + dispatch({ type: GET_STAR_COUNT.PENDING }); - fetchStarCount(user) + fetchStarCount(user, accessToken) .then(data => { dispatch({ type: GET_STAR_COUNT.SUCCESS, @@ -183,10 +181,11 @@ export const getRepositories = user => { dispatch({ type: GET_REPOSITORIES.PENDING }); const url = isAuthUser - ? `${apiRoot}/user/repos?affiliation=owner&sort=updated&per_page=50` - : `${USER_ENDPOINT(user.login)}/repos?sort=updated&per_page=50`; + ? '/user/repos?affiliation=owner&sort=updated&per_page=50' + : `/users/${user.login}/repos?sort=updated&per_page=50`; - fetchUrl(url, accessToken) + v3 + .getJson(url, accessToken) .then(data => { dispatch({ type: GET_REPOSITORIES.SUCCESS, @@ -208,7 +207,8 @@ export const getFollowing = user => { dispatch({ type: GET_FOLLOWING.PENDING }); - fetchUrl(`${USER_ENDPOINT(user.login)}/following?per_page=100`, accessToken) + v3 + .getJson(`/users/${user.login}/following?per_page=100`, accessToken) .then(data => { dispatch({ type: GET_FOLLOWING.SUCCESS,