Add Gist support

main
Mike Fix 8 years ago
parent 820cd3b13a
commit b16b0d6d78

@ -1,8 +1,13 @@
import axios from 'axios' import axios from 'axios'
export default { const gistClient = axios.create({
uploadImage baseURL: 'https://api.github.com',
} timeout: 5000,
headers: {
Accept: 'application/vnd.github.v3+json',
'Content-Type': 'application/json'
}
});
async function uploadImage (encodedImage) { async function uploadImage (encodedImage) {
// upload image // upload image
@ -29,3 +34,18 @@ async function uploadImage (encodedImage) {
console.log(e) console.log(e)
} }
} }
const getGist = (id) => {
const uid = id.split('/').pop()
return gistClient.get(`/gists/${uid}`)
.then(res => res.data)
.then(gist => gist.files)
.then(files => files[Object.keys(files)[0]])
.then(file => file.content)
}
export default {
uploadImage,
getGist
}

@ -5,7 +5,8 @@
"license": "MIT", "license": "MIT",
"scripts": { "scripts": {
"dev": "node server.js", "dev": "node server.js",
"next": "next" "build": "next build",
"start": "next start"
}, },
"dependencies": { "dependencies": {
"axios": "^0.16.2", "axios": "^0.16.2",

@ -2,7 +2,7 @@ import React from 'react'
import Axios from 'axios' import Axios from 'axios'
import CodeImage from '../components/codeImage' import CodeImage from '../components/codeImage'
import api from '../api' import api from '../lib/api'
const code = ` const code = `
const pluckDeep = key => obj => key.split('.').reduce((accum, key) => accum[key], obj) const pluckDeep = key => obj => key.split('.').reduce((accum, key) => accum[key], obj)
@ -17,7 +17,18 @@ const unfold = (f, seed) => {
return go(f, seed, []) return go(f, seed, [])
}` }`
export default (props) => { export default class extends React.Component {
/* pathname, asPath, err, req, res */
static async getInitialProps ({ asPath }) {
try {
const content = await api.getGist(asPath)
return { content }
} catch (e) {
console.log(e)
return {}
}
}
render () {
return ( return (
<div> <div>
<style jsx>{` <style jsx>{`
@ -29,10 +40,11 @@ export default (props) => {
} }
`} `}
</style> </style>
<h1> Welcome to Code Image</h1> <h1>Welcome to Code Image</h1>
<CodeImage> <CodeImage>
{code} {this.props.content || code}
</CodeImage> </CodeImage>
</div> </div>
) )
}
} }

@ -22,17 +22,15 @@ app.prepare()
server.use(morgan('tiny')) server.use(morgan('tiny'))
// if root, render webpage from next // if root, render webpage from next
server.get('/', (req, res) => { server.get('*', (req, res) =>
return app.render(req, res, '/', req.query) app.render(req, res, '/', req.query))
})
// otherwise, try and get gist
server.get('*', handle)
// api endpoints // api endpoints
server.post('/upload', bodyParser.json(), wrap(require('./handlers/upload'))) server.post('/upload', bodyParser.json(), wrap(require('./handlers/upload')))
// otherwise, try and get gist
server.get('*', (req, res) => {
return handle(req, res)
})
server.listen(3000, (err) => { server.listen(3000, (err) => {
if (err) throw err if (err) throw err

Loading…
Cancel
Save