-
Notifications
You must be signed in to change notification settings - Fork 1
Server stuff #89
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Server stuff #89
Changes from all commits
59e579a
5caca57
e1479d9
e771f9d
404bc5b
83ba30c
824ba16
ff9fc97
730159d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"type": "node", | ||
"request": "launch", | ||
"name": "Jest All", | ||
"program": "${workspaceFolder}/node_modules/jest/bin/jest", | ||
"args": ["--runInBand"], | ||
"console": "integratedTerminal", | ||
"internalConsoleOptions": "neverOpen" | ||
}, | ||
{ | ||
"type": "node", | ||
"request": "launch", | ||
"name": "Jest Current File", | ||
"program": "${workspaceFolder}/node_modules/jest/bin/jest", | ||
"args": ["${relativeFile}"], | ||
"console": "integratedTerminal", | ||
"internalConsoleOptions": "neverOpen" | ||
} | ||
] | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ const Router = () => ( | |
<BrowserRouter history={browserHistory}> | ||
<div> | ||
<Route exact path="/" component={Home}/> | ||
<Route path="/asdf" component={Home}/> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this needs to make more sense |
||
</div> | ||
</BrowserRouter> | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export const HTTP_RESPONSE_TYPES = { | ||
JSON: "application/json", | ||
HTML: "text/html", | ||
PLAIN: "text/plain" | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import "isomorphic-fetch"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this needs to have a space between these two. |
||
import { HTTP_RESPONSE_TYPES } from "./constants"; | ||
|
||
const resolveResponse = (response, responseType) => { | ||
if (responseType.indexOf(HTTP_RESPONSE_TYPES.JSON) !== -1) { | ||
return response.json(); | ||
} else if (responseType.indexOf(HTTP_RESPONSE_TYPES.HTML) !== -1) { | ||
return response.text(); | ||
} else if (responseType.indexOf(HTTP_RESPONSE_TYPES.PLAIN) !== -1) { | ||
return response.text(); | ||
} else { | ||
throw Error("Response type not supported yet!"); | ||
} | ||
}; | ||
|
||
const initialHeaders = { "Content-Type": HTTP_RESPONSE_TYPES.JSON, Accept: HTTP_RESPONSE_TYPES.JSON }; | ||
|
||
const fetchWrapper = async ({ | ||
url, | ||
method, | ||
body, | ||
headers, | ||
}) => { | ||
// do this here to get Content-Type to not be overridden if you didn't want to. | ||
const actualHeaders = { ...initialHeaders, ...headers }; | ||
|
||
const response = await fetch(url, { | ||
method, | ||
headers: actualHeaders, | ||
body | ||
}); | ||
const responseType = response.headers.get("content-type"); | ||
if (!responseType) { | ||
throw Error("Response type was not defined"); | ||
} | ||
const resolvedResponse = await resolveResponse(response, responseType); | ||
if (!response.ok) { | ||
throw Error(JSON.stringify(resolvedResponse)); | ||
} | ||
return resolvedResponse; | ||
}; | ||
|
||
|
||
export const fetchGet = ({ url, headers }) => { | ||
return fetchWrapper({ url, method: "GET", headers }); | ||
}; | ||
|
||
export const fetchPost = ({ url, body, headers }) => { | ||
return fetchWrapper({ url, method: "POST", body, headers }); | ||
}; | ||
|
||
export const fetchPut = ({ url, body, headers }) => { | ||
return fetchWrapper({ url, method: "PUT", body, headers }); | ||
}; | ||
|
||
export const fetchDelete = ({ url, body, headers }) => { | ||
return fetchWrapper({ url, method: "DELETE", body, headers }); | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this needs to be abstracted