Skip to content

Instantly share code, notes, and snippets.

Avatar
🦴
Building

Koen van Gilst vnglst

🦴
Building
View GitHub Profile
View vnglst.json
{
"login": "vnglst",
"id": 3457693,
"avatar_url": "https://avatars.githubusercontent.com/u/3457693?v=3",
"gravatar_id": "",
"url": "https://api.github.com/users/vnglst",
"html_url": "https://github.com/vnglst",
"followers_url": "https://api.github.com/users/vnglst/followers",
"following_url": "https://api.github.com/users/vnglst/following{/other_user}",
"gists_url": "https://api.github.com/users/vnglst/gists{/gist_id}",
@vnglst
vnglst / request.js
Created Feb 6, 2017
Mocked version of request.js
View request.js
const fs = require('fs')
const request = (url) => new Promise((resolve, reject) => {
// Get userID from supplied url string
const lastSlash = url.lastIndexOf('/')
const userID = url.substring(lastSlash + 1)
// Load user json data from a file in de subfolder for mock data
fs.readFile(`./src/api/__mockData__/${userID}.json`, 'utf8', (err, data) => {
if (err) reject(err)
// Parse the data as JSON and put in the key entity (just like the request library does)
View github.test.js
/* eslint-env jest */
const github = require('../github')
// A simple example test
describe('#getUser() using Promises', () => {
it('should load user data', () => {
return github.getUser('vnglst')
.then(data => {
expect(data).toBeDefined()
View github.js
import request from './request'
const getUser = user => request(`https://api.github.com/users/${user}`)
export { getUser }
View request.js
const rest = require('rest')
const mime = require('rest/interceptor/mime')
export default rest.wrap(mime)
View App.js
import React, { Component } from 'react'
import './App.css'
import { getUser } from './api/github'
const renderLine = (user, key) => <li key={key}><b>{key}</b>: {user[key]}</li>
class App extends Component {
constructor (props) {
super(props)
this.state = { user: {} }
View ContactsApp.js
// ....
import LoaderHOC from './LoaderHOC'
// ...
class ContactsApp extends Component {
// ...
}
export default LoaderHOC(ContactsApp);
@vnglst
vnglst / LoaderHOC.js
Last active Nov 22, 2016
ReactCasts #1
View LoaderHOC.js
import React, { Component } from 'react';
// Returns an empty "loader" div if empty,
// Return unchanged WrappedComponent if not empty
const LoaderHOC = (WrappedComponent) => {
return class LoaderOC extends Component {
render() {
return (!this.props.question)
? <div className='loading'></div>
: <WrappedComponent {...this.props} />
View app.js
...
var index = require('./routes/index');
var users = require('./routes/users');
var pdf = require(./routes/pdf’); // <-- add this line
...
app.use('/', index);
app.use('/users', users);
app.use('/pdf', pdf); // <-- add this line
...
View pdf.js
const express = require('express')
const router = express.Router()
const PDFDocument = require('pdfkit')
router.post('/', (req, res) => {
const doc = new PDFDocument()
let filename = req.body.filename
// Stripping special characters
filename = encodeURIComponent(filename) + '.pdf'
// Setting response to 'attachment' (download).