Skip to content

Commit cbfdbb7

Browse files
committed
all body:String all headers:Literal
1 parent 09db389 commit cbfdbb7

File tree

6 files changed

+29
-34
lines changed

6 files changed

+29
-34
lines changed

public/scripts/api.js

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ async function query(form) {
2727
fetch(QUERY_URL, {
2828
method: "POST",
2929
mode: "cors",
30-
headers: new Headers({
30+
headers: {
3131
'Content-Type': 'application/json; charset=utf-8'
32-
}),
32+
},
3333
body: JSON.stringify(queryObj)
3434
})
3535
.then(response => {
@@ -58,9 +58,9 @@ async function importObj(form) {
5858
fetch(UPDATE_URL, {
5959
method: 'PUT',
6060
body: JSON.stringify(objForImport),
61-
headers: new Headers({
61+
headers: {
6262
'Content-Type': 'application/json; charset=utf-8'
63-
})
63+
}
6464
})
6565
.then(response => {
6666
if (!response.ok) { throw response }
@@ -105,9 +105,9 @@ async function update(form, objIn) {
105105
fetch(UPDATE_URL, {
106106
method: 'PUT',
107107
body: JSON.stringify(obj),
108-
headers: new Headers({
108+
headers: {
109109
'Content-Type': 'application/json; charset=utf-8'
110-
})
110+
}
111111
})
112112
.then(response => {
113113
if (response.ok) { return response.json() }
@@ -130,7 +130,7 @@ async function update(form, objIn) {
130130
async function create(form) {
131131
let obj = form.getElementsByTagName("textarea")[0].value
132132
try {
133-
obj = JSON.parse(obj)
133+
JSON.parse(obj)
134134
} catch (error) {
135135
console.error("You did not provide valid JSON")
136136
setMessage("You did not provide valid JSON")
@@ -139,10 +139,10 @@ async function create(form) {
139139
}
140140
fetch(CREATE_URL, {
141141
method: 'POST',
142-
body: JSON.stringify(obj),
143-
headers: new Headers({
142+
body: obj,
143+
headers: {
144144
'Content-Type': 'application/json; charset=utf-8'
145-
})
145+
}
146146
})
147147
.then(response => {
148148
if (response.ok) { return response.json() }
@@ -166,9 +166,9 @@ async function deleteObj(form) {
166166
let url = form.getElementsByTagName("input")[0].value
167167
fetch(`${DELETE_URL}/${url.split('id/').pop()}`, {
168168
method: 'DELETE',
169-
headers: new Headers({
169+
headers: {
170170
'Content-Type': 'text/plain; charset=utf-8'
171-
})
171+
}
172172
})
173173
.then(response => {
174174
if (response.status === 204) {
@@ -198,7 +198,7 @@ async function overwrite(form, objIn) {
198198
else {
199199
obj = form.getElementsByTagName("textarea")[0].value
200200
try {
201-
obj = JSON.parse(obj)
201+
JSON.parse(obj)
202202
}
203203
catch (err) {
204204
_customEvent("rerum-error", "You did not provide valid JSON", {}, err)
@@ -208,10 +208,10 @@ async function overwrite(form, objIn) {
208208
obj["@id"] = uri
209209
fetch(OVERWRITE_URL, {
210210
method: 'PUT',
211-
body: JSON.stringify(obj),
212-
headers: new Headers({
211+
body: obj,
212+
headers: {
213213
'Content-Type': 'application/json; charset=utf-8'
214-
})
214+
}
215215
})
216216
.then(response => {
217217
if (response.ok) { return response.json() }

routes/create.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ router.post('/', async (req, res, next) => {
66

77
try {
88
// check body for JSON
9-
JSON.stringify(req.body)
9+
const body = JSON.stringify(req.body)
1010
const createOptions = {
1111
method: 'POST',
12-
body: req.body,
12+
body,
1313
headers: {
1414
'user-agent': 'Tiny-Node',
1515
'Authorization': `Bearer ${process.env.ACCESS_TOKEN}`, // not required for query

routes/delete.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,17 @@ const router = express.Router()
66
/* DELETE a delete to the thing. */
77
router.delete('/', async (req, res, next) => {
88
try {
9-
JSON.stringify(req.body)
10-
const deleteBody = req.body ?? {}
11-
9+
const body = JSON.stringify(req.body)
1210
const deleteOptions = {
13-
body: deleteBody,
11+
body,
1412
method: 'DELETE',
1513
headers: {
1614
'user-agent': 'Tiny-Node',
1715
'Authorization': `Bearer ${process.env.ACCESS_TOKEN}`,
1816
'Content-Type' : "application/json; charset=utf-8"
1917
}
2018
}
21-
console.log(deleteBody)
19+
console.log(body)
2220
const deleteURL = `${process.env.RERUM_API_ADDR}delete`
2321
const result = await fetch(deleteURL, deleteOptions).text()
2422
res.status(204)

routes/overwrite.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,16 @@ router.put('/', async (req, res, next) => {
66

77
try {
88
// check body for JSON
9-
JSON.stringify(req.body)
10-
const overwriteBody = req.body
9+
const body = JSON.stringify(req.body)
1110

1211
// check for @id; any value is valid
13-
if (!(overwriteBody['@id'] ?? overwriteBody.id)) {
12+
if (!(body['@id'] ?? body.id)) {
1413
throw Error("No record id to overwrite! (https://centerfordigitalhumanities.github.io/rerum_server/API.html#overwrite)")
1514
}
1615

1716
const overwriteOptions = {
1817
method: 'PUT',
19-
body: overwriteBody,
18+
body,
2019
headers: {
2120
'user-agent': 'Tiny-Node',
2221
'Authorization': `Bearer ${process.env.ACCESS_TOKEN}`,

routes/query.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ router.post('/', async (req, res, next) => {
88

99
try {
1010
// check body for JSON
11-
JSON.stringify(req.body)
12-
const queryBody = req.body
11+
const body = JSON.stringify(req.body)
1312
// check limit and skip for INT
1413
if (isNaN(parseInt(lim) + parseInt(skip))
1514
|| (lim < 0)
@@ -18,7 +17,7 @@ router.post('/', async (req, res, next) => {
1817
}
1918
const queryOptions = {
2019
method: 'POST',
21-
body: queryBody,
20+
body,
2221
headers: {
2322
'user-agent': 'Tiny-Node',
2423
'Authorization': `Bearer ${process.env.RERUM_TOKEN}`, // not required for query

routes/update.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,16 @@ router.put('/', async (req, res, next) => {
66

77
try {
88
// check body for JSON
9-
JSON.stringify(req.body)
10-
const updateBody = req.body
9+
const body = JSON.stringify(req.body)
1110

1211
// check for @id; any value is valid
13-
if (!(updateBody['@id'] ?? updateBody.id)) {
12+
if (!(body['@id'] ?? body.id)) {
1413
throw Error("No record id to update! (https://centerfordigitalhumanities.github.io/rerum_server/API.html#update)")
1514
}
1615

1716
const updateOptions = {
1817
method: 'PUT',
19-
body: updateBody,
18+
body,
2019
headers: {
2120
'user-agent': 'Tiny-Node',
2221
'Authorization': `Bearer ${process.env.ACCESS_TOKEN}`, // not required for query

0 commit comments

Comments
 (0)