Created
July 8, 2020 13:28
-
-
Save laosb/59ebbd151909977c62a17c24ac5f221c to your computer and use it in GitHub Desktop.
Simple API wrapper for JSONBin.io.
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
import fetch from 'node-fetch' | |
const secretKey = process.env.JSONBIN_SECRET_KEY | |
const basicHeaders = () => ({ | |
'Content-Type': 'application/json', | |
'X-Master-Key': secretKey, | |
}) | |
export const createBin = async ( | |
name, | |
collectionId, | |
content, | |
isPrivate = true | |
) => { | |
const headers = { | |
...basicHeaders(), | |
...(isPrivate ? {} : { 'X-Bin-Private': 'false' }), | |
'X-Bin-Name': name, | |
'X-Collection-Id': collectionId, | |
} | |
const result = await fetch('https://api.jsonbin.io/v3/b', { | |
method: 'post', | |
body: JSON.stringify(content), | |
headers, | |
}) | |
if (!result.ok) throw new Error('createBin Error: ' + (await result.text())) | |
return await result.json() | |
} | |
export const readBin = async (id, version = 'latest') => { | |
const result = await fetch(`https://api.jsonbin.io/v3/b/${id}/${version}`, { | |
headers: basicHeaders(), | |
}) | |
if (!result.ok) throw new Error('readBin Error: ' + (await result.text())) | |
return await result.json() | |
} | |
export const updateBin = async (id, content, shouldDoVersioning = true) => { | |
const headers = { | |
...basicHeaders(), | |
'X-Bin-Versioning': shouldDoVersioning ? 'true' : 'false', | |
} | |
const result = await fetch(`https://api.jsonbin.io/v3/b/${id}`, { | |
method: 'put', | |
body: JSON.stringify(content), | |
headers, | |
}) | |
if (!result.ok) throw new Error('updateBin Error: ' + (await result.text())) | |
return await result.json() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Response format: https://jsonbin.io/api-reference/v3/bins/create#request-response