|
1 | 1 | import express from "express" |
2 | 2 | import checkAccessToken from "../tokens.js" |
3 | 3 | const router = express.Router() |
4 | | -import rerumPropertiesWasher from "../preprocessor.js" |
5 | 4 |
|
6 | 5 | /* PUT an overwrite to the thing. */ |
7 | | -router.put('/', checkAccessToken, rerumPropertiesWasher, async (req, res, next) => { |
| 6 | +router.put('/', checkAccessToken, async (req, res, next) => { |
8 | 7 |
|
9 | 8 | try { |
10 | | - // check for @id in body. Any value is valid. Lack of value is a bad request. |
11 | | - if (!req?.body || !(req.body['@id'] ?? req.body.id)) { |
12 | | - res.status(400).send("No record id to overwrite! (https://store.rerum.io/v1/API.html#overwrite)") |
| 9 | + |
| 10 | + const overwriteBody = req.body |
| 11 | + // check for @id; any value is valid |
| 12 | + if (!(overwriteBody['@id'] ?? overwriteBody.id)) { |
| 13 | + res.status(400).send("No record id to overwrite! (https://store.rerum.io/API.html#overwrite)") |
| 14 | + return |
13 | 15 | } |
14 | | - // check body for JSON |
15 | | - const body = JSON.stringify(req.body) |
| 16 | + |
16 | 17 | const overwriteOptions = { |
17 | 18 | method: 'PUT', |
18 | | - body, |
| 19 | + body: JSON.stringify(overwriteBody), |
19 | 20 | headers: { |
20 | 21 | 'user-agent': 'Tiny-Things/1.0', |
21 | 22 | 'Authorization': `Bearer ${process.env.ACCESS_TOKEN}`, |
22 | 23 | 'Content-Type' : "application/json;charset=utf-8" |
23 | 24 | } |
24 | 25 | } |
| 26 | + |
| 27 | + // Pass through If-Overwritten-Version header if present |
| 28 | + const ifOverwrittenVersion = req.headers.hasOwnProperty('if-overwritten-version') ? req.headers['if-overwritten-version'] : null |
| 29 | + if (ifOverwrittenVersion !== null) { |
| 30 | + overwriteOptions.headers['If-Overwritten-Version'] = ifOverwrittenVersion |
| 31 | + } |
| 32 | + |
| 33 | + // Check for __rerum.isOverwritten in body and use as If-Overwritten-Version header |
| 34 | + const isOverwrittenValue = req.body?.__rerum?.hasOwnProperty("isOverwritten") ? req.body.__rerum.isOverwritten : null |
| 35 | + if (isOverwrittenValue !== null) { |
| 36 | + overwriteOptions.headers['If-Overwritten-Version'] = isOverwrittenValue |
| 37 | + } |
| 38 | + |
25 | 39 | const overwriteURL = `${process.env.RERUM_API_ADDR}overwrite` |
26 | | - const result = await fetch(overwriteURL, overwriteOptions).then(res=>res.json()) |
27 | | - .catch(err=>next(err)) |
28 | | - res.setHeader("Location", result["@id"] ?? result.id) |
29 | | - res.status(200) |
30 | | - res.send(result) |
| 40 | + const response = await fetch(overwriteURL, overwriteOptions) |
| 41 | + .then(resp=>{ |
| 42 | + if (!resp.ok) throw resp |
| 43 | + return resp |
| 44 | + }) |
| 45 | + .catch(async err => { |
| 46 | + // Handle 409 conflict error for version mismatch |
| 47 | + if (err.status === 409) { |
| 48 | + const currentVersion = await err.json() |
| 49 | + return res.status(409).json(currentVersion) |
| 50 | + } |
| 51 | + throw new Error(`Error in overwrite request: ${err.status} ${err.statusText}`) |
| 52 | + }) |
| 53 | + if(res.headersSent) return |
| 54 | + const result = await response.json() |
| 55 | + const location = result?.["@id"] ?? result?.id |
| 56 | + if (location) { |
| 57 | + res.setHeader("Location", location) |
| 58 | + } |
| 59 | + res.status(response.status ?? 200) |
| 60 | + res.json(result) |
31 | 61 | } |
32 | | - catch (err) { |
| 62 | + catch (err) { |
33 | 63 | next(err) |
34 | 64 | } |
35 | 65 | }) |
|
0 commit comments