-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpostContent.js
More file actions
89 lines (68 loc) · 2.6 KB
/
postContent.js
File metadata and controls
89 lines (68 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// RUN CODE
(async () => {
const getContentfulDataFromFile = (jsonFile) => {
const data = require(jsonFile)
let contentType = '';
const dataArr = data.items.map((item, idx) => {
if (idx === 0) contentType = data.items[idx].sys?.contentType?.sys?.id
const itemObj = {}
for (const field in item.fields) {
itemObj[field] = item.fields[field]['en-US']
}
return itemObj
})
return { dataArr, contentType }
};
// TOOD: Read JSON files from content folder
// TODO: Create content-type
const addContentToStrapi = async (dataArr, contentType, plural = true) => {
const axios = require('axios')
// const dotenv = require('dotenv').config()
const API_URL = 'http://api-staging.renci.org:1337/api';
const API_TOKEN='506c2e436edb69675ed01dddc34e2369db06c1f40dfbeef55209fa2b7b0a030fe35bb378e8a6aa0392586d700c4e41fe9aa9059033795f32e1ddc74035b9e833ae6138b0f9e07443fa1d5191de3c6d0ff44a7f86e5c802f8c56bf996da4c62964684a078f79d18bbf0727bafa45bbad49883d2ba386cc97cb3e42f82a783cd21'
// const CONTENTFUL_SPACE_ID='ongrv4q8etpz'
const options = {
// 'Content-Type': 'application/vnd.contentful.management.v1+json',
// 'Authorization': `bearer ${ process.env.API_TOKEN }`
'Content-Type': 'application/json',
'Authorization': `bearer ${ API_TOKEN }`
};
for (const data of dataArr) {
const payload = { data }
console.log('PAYLOAD:')
console.log(payload)
try {
// const url = `${ API_URL }/spaces/${ CONTENTFUL_SPACE_ID }/environments/development/content_types`
const url = `${ API_URL }/${ contentType }${plural ? 's' : ''}`
console.log(url)
const response = await axios.post(
url,
payload,
options,
)
if (!response) {
return
}
console.log(response.status)
} catch (error) {
console.error(error.message)
}
}
};
console.clear()
// const contentFolder = './content/';
// const fs = require('fs');
// fs.readdir(contentFolder, (err, files) => {
// console.log(files)
// files.map(file => {
// if (file !== '.gitkeep') {
// const filePath = contentFolder + file
const filePath = './content/person.json'
// Get data from content directory './content/organization.json'
const { dataArr, contentType } = getContentfulDataFromFile(filePath)
// Add content to Strapi via API
if (dataArr.length) addContentToStrapi(dataArr, 'people', false) // contentType
// };
// });
// });
})();