-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
darwin.apolinario
committed
Dec 7, 2023
1 parent
f363b1b
commit 7c54552
Showing
1 changed file
with
63 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,77 @@ | ||
// pages/api/fetchData.js | ||
|
||
import axios from 'axios'; | ||
import MarkdownIt from 'markdown-it'; | ||
|
||
const INSTANCE_ZUID = `8-aaeffee09b-7w6v22`; | ||
const MODEL_ZUID = `6-e092db91a5-rgfcx2`; | ||
const ENV = 'dev'; | ||
|
||
// edit the content data | ||
async function makePutRequestWithToken(token, putData, apiUrl) { | ||
try { | ||
const response = await axios.put(apiUrl, putData, { | ||
headers: { | ||
Authorization: `Bearer ${token}`, | ||
'Content-Type': 'application/json', | ||
}, | ||
}); | ||
|
||
return response.data; | ||
} catch (error) { | ||
return error; | ||
} | ||
} | ||
|
||
// Publish all item | ||
async function PublishAllItem(auth) { | ||
const url = `https://us-central1-zesty-${ENV}.cloudfunctions.net/publishAllModelContent?instanceZUID=${INSTANCE_ZUID}&modelZUID=${MODEL_ZUID}`; | ||
return await axios.get(url, { headers: { Authorization: auth } }); | ||
} | ||
|
||
// main function | ||
export default async function handler(req, res) { | ||
const token = req.query['token']; | ||
const url = | ||
'https://8-aaeffee09b-7w6v22.api.zesty.io/v1/content/models/6-e092db91a5-rgfcx2/items?limit=5000&page=1&lang=en-US'; | ||
|
||
const url = `https://${INSTANCE_ZUID}.api.${ENV}.zesty.io/v1/content/models/${MODEL_ZUID}/items?page=3&lang=en-US`; | ||
const auth = 'Bearer ' + token; | ||
|
||
// get all the data | ||
const response = await axios.get(url, { headers: { Authorization: auth } }); | ||
|
||
const md = new MarkdownIt(); | ||
|
||
// convert the body from md to html | ||
const data = response.data.data.map((e) => { | ||
const content = md.render(e.data.body).replaceAll('\n', ''); | ||
return { item: e.meta.ZUID, content }; | ||
const content = md.render(e?.data?.body || '').replaceAll('\n', ''); | ||
return { | ||
item: e.meta.ZUID, | ||
content, | ||
web: e.web, | ||
meta: e.meta, | ||
data: e.data, | ||
}; | ||
}); | ||
|
||
// update the `content` of the item | ||
const promises = data.map(async (e) => { | ||
const putData = { | ||
web: e.web, | ||
meta: e.meta, | ||
data: { ...e['data'], content: e.content }, | ||
}; | ||
|
||
const url1 = `https://${INSTANCE_ZUID}.api.${ENV}.zesty.io/v1/content/models/${MODEL_ZUID}/items/${e.item}`; | ||
|
||
await makePutRequestWithToken(token, putData, url1); | ||
}); | ||
|
||
await Promise.all(promises); | ||
|
||
// get all the updated data | ||
await axios.get(url, { headers: { Authorization: auth } }); | ||
|
||
// publish all items | ||
const finalRes = PublishAllItem(auth); | ||
res.status(200).json({ | ||
data: finalRes, | ||
msg: `Successfully updated and publish the data in Instance: ${INSTANCE_ZUID} Model: ${MODEL_ZUID}`, | ||
}); | ||
res.status(200).json({ data }); | ||
} |