Skip to content

Commit b8925e2

Browse files
committed
un-got
mostly changed json to body in the requests and added the method option where needed.
1 parent 2cca250 commit b8925e2

File tree

7 files changed

+28
-24
lines changed

7 files changed

+28
-24
lines changed

routes/create.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
11
const express = require('express')
22
const router = express.Router()
3-
const got = require('got')
43

54
/* POST a create to the thing. */
65
router.post('/', async (req, res, next) => {
76

87
try {
98
// check body for JSON
109
JSON.stringify(req.body)
11-
const createBody = req.body
1210
const createOptions = {
13-
json: createBody,
11+
method: 'POST',
12+
body: req.body,
1413
headers: {
1514
'user-agent': 'Tiny-Node',
1615
'Authorization': `Bearer ${process.env.ACCESS_TOKEN}`, // not required for query
1716
'Content-Type' : "application/json;charset=utf-8"
1817
}
1918
}
2019
const createURL = `${process.env.RERUM_API_ADDR}create`
21-
const result = await got.post(createURL, createOptions).json()
20+
const result = await fetch(createURL, createOptions).json()
2221
res.setHeader("Location", result["@id"])
2322
res.status(201)
2423
res.send(result)

routes/delete.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,26 @@
11
const express = require('express')
22
const router = express.Router()
3-
const got = require('got')
43

54
/* Legacy delete pattern w/body */
65

76
/* DELETE a delete to the thing. */
87
router.delete('/', async (req, res, next) => {
98
try {
9+
JSON.stringify(req.body)
1010
const deleteBody = req.body ?? {}
1111

1212
const deleteOptions = {
13-
json: deleteBody,
13+
body: deleteBody,
14+
method: 'DELETE',
1415
headers: {
1516
'user-agent': 'Tiny-Node',
1617
'Authorization': `Bearer ${process.env.ACCESS_TOKEN}`,
17-
'Content-Type' : "application/json"
18+
'Content-Type' : "application/json; charset=utf-8"
1819
}
1920
}
2021
console.log(deleteBody)
2122
const deleteURL = `${process.env.RERUM_API_ADDR}delete`
22-
const result = await got.delete(deleteURL, deleteOptions).text()
23+
const result = await fetch(deleteURL, deleteOptions).text()
2324
res.status(204)
2425
res.send(result)
2526
}
@@ -35,12 +36,13 @@ router.delete('/:id', async (req, res, next) => {
3536

3637
const deleteURL = `${process.env.RERUM_API_ADDR}delete/${req.params.id}`
3738
const deleteOptions = {
39+
method: 'DELETE',
3840
headers: {
3941
'user-agent': 'Tiny-Node',
4042
'Authorization': `Bearer ${process.env.ACCESS_TOKEN}`,
4143
}
4244
}
43-
const result = await got.delete(deleteURL, deleteOptions).text()
45+
const result = await fetch(deleteURL, deleteOptions).text()
4446
res.status(204)
4547
res.send(result)
4648
}

routes/overwrite.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
const express = require('express')
22
const router = express.Router()
3-
const got = require('got')
43

54
/* PUT an overwrite to the thing. */
65
router.put('/', async (req, res, next) => {
@@ -16,19 +15,20 @@ router.put('/', async (req, res, next) => {
1615
}
1716

1817
const overwriteOptions = {
19-
json: overwriteBody,
18+
method: 'PUT',
19+
body: overwriteBody,
2020
headers: {
2121
'user-agent': 'Tiny-Node',
2222
'Authorization': `Bearer ${process.env.ACCESS_TOKEN}`,
2323
'Content-Type' : "application/json;charset=utf-8"
2424
}
2525
}
2626
const overwriteURL = `${process.env.RERUM_API_ADDR}overwrite`
27-
const result = await got.put(overwriteURL, overwriteOptions).json()
27+
const result = await fetch(overwriteURL, overwriteOptions).json()
2828
res.status(200)
2929
res.send(result)
3030
}
31-
catch (err) {
31+
catch (err) {
3232
console.log(err)
3333
res.status(500).send("Caught Error:" + err)
3434
}

routes/query.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
const express = require('express')
22
const router = express.Router()
3-
const got = require('got')
43

54
/* POST a query to the thing. */
65
router.post('/', async (req, res, next) => {
@@ -18,15 +17,16 @@ router.post('/', async (req, res, next) => {
1817
throw Error("`limit` and `skip` values must be positive integers or omitted.")
1918
}
2019
const queryOptions = {
21-
json: queryBody,
20+
method: 'POST',
21+
body: queryBody,
2222
headers: {
2323
'user-agent': 'Tiny-Node',
2424
'Authorization': `Bearer ${process.env.RERUM_TOKEN}`, // not required for query
2525
'Content-Type' : "application/json;charset=utf-8"
2626
}
2727
}
2828
const queryURL = `${process.env.RERUM_API_ADDR}query?limit=${lim}&skip=${skip}`
29-
const results = await got.post(queryURL, queryOptions).json()
29+
const results = await fetch(queryURL, queryOptions).json()
3030
res.status(200)
3131
res.send(results)
3232
}

routes/update.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
const express = require('express')
22
const router = express.Router()
3-
const got = require('got')
43

54
/* PUT an update to the thing. */
65
router.put('/', async (req, res, next) => {
@@ -16,15 +15,16 @@ router.put('/', async (req, res, next) => {
1615
}
1716

1817
const updateOptions = {
19-
json: updateBody,
18+
method: 'PUT',
19+
body: updateBody,
2020
headers: {
2121
'user-agent': 'Tiny-Node',
2222
'Authorization': `Bearer ${process.env.ACCESS_TOKEN}`, // not required for query
2323
'Content-Type' : "application/json;charset=utf-8"
2424
}
2525
}
2626
const updateURL = `${process.env.RERUM_API_ADDR}update`
27-
const result = await got.put(updateURL, updateOptions).json()
27+
const result = await fetch(updateURL, updateOptions).json()
2828
res.status(200)
2929
res.send(result)
3030
}

sample.env

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ REFRESH_TOKEN=short-token
66
RERUM_REGISTRATION_URL = https://store.rerum.io/v1/
77
RERUM_API_ADDR = https://store.rerum.io/v1/api/
88
RERUM_ID_PATTERN = https://store.rerum.io/v1/id/
9-
RERUM_ACCESS_TOKEN_URL = http://store.rerum.io/v1/client/request-new-access-token
9+
RERUM_ACCESS_TOKEN_URL = http://store.rerum.io/client/request-new-access-token
1010

1111
#Make this value true if you plan to use this as a public API.
1212
OPEN_API_CORS = false

tokens.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
require('dotenv').config()
2-
const got = require('got')
32
const fs = require('node:fs/promises')
43
const { parse, stringify } = require('envfile')
54
const sourcePath = '.env'
@@ -15,10 +14,14 @@ const isTokenExpired = (token) => (Date.now() >= JSON.parse(Buffer.from(token.sp
1514
*/
1615
async function generateNewAccessToken() {
1716
try {
18-
const tokenObject = await got.post(process.env.RERUM_ACCESS_TOKEN_URL, {
17+
const tokenObject = await fetch(process.env.RERUM_ACCESS_TOKEN_URL, {
18+
method: 'POST',
19+
headers: {
20+
'Content-Type': 'application/json',
21+
},
22+
body: JSON.stringify({ "refresh_token": process.env.REFRESH_TOKEN }),
1923
timeout: 10000,
20-
json: { "refresh_token": process.env.REFRESH_TOKEN }
21-
}).json()
24+
}).json()
2225
process.env.ACCESS_TOKEN = tokenObject.access_token
2326
try{
2427
const data = await fs.readFile('./.env', { encoding: 'utf8' })

0 commit comments

Comments
 (0)