Skip to content

Commit 4a1e2fb

Browse files
author
cws
committed
add direct endpoint method
1 parent 42cb391 commit 4a1e2fb

File tree

3 files changed

+36
-10
lines changed

3 files changed

+36
-10
lines changed

netlifyAPI.js

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ const NetlifyAPI = require('netlify')
66
const client = new NetlifyAPI(process.env.NETLIFY_API_TOKEN)
77
const site_id = 'b8d481fc-f70c-45f8-9242-3936c27fe7e6'
88

9+
const fetch = require('node-fetch')
10+
911
const uploadFile = async ({ deployId, path, buffer }) => {
1012
const bufferStream = new stream.PassThrough()
1113
bufferStream.end(buffer)
@@ -19,6 +21,23 @@ const uploadFile = async ({ deployId, path, buffer }) => {
1921
return result
2022
}
2123

24+
const directUploadFile = async ({ deployId, path, buffer }) => {
25+
26+
const uri = `https://api.netlify.com/api/v1/deploys/${deployId}/files/${path}`
27+
28+
const options = {
29+
method: 'PUT',
30+
headers: {
31+
'Authorization': `Bearer ${process.env.NETLIFY_API_TOKEN}`,
32+
'Content-Type': 'application/octet-stream'
33+
},
34+
body: buffer
35+
}
36+
37+
const result = await fetch(uri, options)
38+
return await result.json()
39+
}
40+
2241
const upload = async () => {
2342
let files = []
2443
try {
@@ -73,19 +92,20 @@ const upload = async () => {
7392
//console.log(uploadList)
7493

7594
result = await Promise.all(
76-
uploadList.map(item => uploadFile(item))
95+
//uploadList.map(item => uploadFile(item))
96+
uploadList.map(item => directUploadFile(item))
7797
)
78-
console.log('Why are all fields capitalized?')
98+
console.log('Why are all fields capitalized, even without the js-client?')
7999
console.log(result)
80100

81-
console.log('.. but other endpoints are *NOT* capitalized?')
82-
let site
83-
try {
84-
site = await client.getSite({ site_id })
85-
} catch (e) {
86-
console.log(e.json)
87-
}
88-
console.log(site)
101+
//console.log('.. but other endpoints are *NOT* capitalized?')
102+
//let site
103+
//try {
104+
// site = await client.getSite({ site_id })
105+
//} catch (e) {
106+
// console.log(e.json)
107+
//}
108+
//console.log(site)
89109

90110
}
91111

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"crypto-hash": "^1.3.0",
1313
"dotenv": "^8.2.0",
1414
"netlify": "^4.3.13",
15+
"node-fetch": "^2.6.1",
1516
"stream": "^0.0.2"
1617
}
1718
}

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -865,6 +865,11 @@ node-fetch@^2.2.0:
865865
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.0.tgz#e633456386d4aa55863f676a7ab0daa8fdecb0fd"
866866
integrity sha512-8dG4H5ujfvFiqDmVu9fQ5bOHUC15JMjMY/Zumv26oOvvVJjM67KF8koCWIabKQ1GJIa9r2mMZscBq/TbdOcmNA==
867867

868+
node-fetch@^2.6.1:
869+
version "2.6.1"
870+
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.1.tgz#045bd323631f76ed2e2b55573394416b639a0052"
871+
integrity sha512-V4aYg89jEoVRxRb2fJdAg8FHvI7cEyYdVAh94HH0UIK8oJxUfkjlDQN9RbMx+bEjP7+ggMiFRprSti032Oipxw==
872+
868873
node-source-walk@^4.0.0, node-source-walk@^4.2.0:
869874
version "4.2.0"
870875
resolved "https://registry.yarnpkg.com/node-source-walk/-/node-source-walk-4.2.0.tgz#c2efe731ea8ba9c03c562aa0a9d984e54f27bc2c"

0 commit comments

Comments
 (0)