Skip to content

Commit

Permalink
feat: copy over the API function and install dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
jessepinho committed Mar 19, 2018
1 parent 36be9c0 commit 28f5daa
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
6 changes: 5 additions & 1 deletion functions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@
},
"main": "lib/index.js",
"dependencies": {
"express": "^4.16.3",
"firebase-admin": "~5.8.1",
"firebase-functions": "^0.8.1"
"firebase-functions": "^0.8.1",
"http-proxy": "^1.16.2"
},
"devDependencies": {
"@types/express": "^4.11.1",
"@types/http-proxy": "^1.16.0",
"typescript": "^2.5.3"
},
"private": true
Expand Down
40 changes: 34 additions & 6 deletions functions/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,36 @@
import * as express from 'express';
import * as functions from 'firebase-functions';
import * as httpProxy from 'http-proxy';

// // Start writing Firebase Functions
// // https://firebase.google.com/docs/functions/typescript
//
// export const helloWorld = functions.https.onRequest((request, response) => {
// response.send("Hello from Firebase!");
// });
const app = express();

const proxy = httpProxy
.createProxyServer({
changeOrigin: true,
headers: {
Authorization: `Bearer ${functions.config().twitter.token}`,
},
target: 'https://api.twitter.com',
});

proxy.on('proxyRes', function(proxyRes, req, res) {
if (process.env.NODE_ENV === 'development') {
res.setHeader('Access-Control-Allow-Origin', '*');
} else {
res.setHeader('Access-Control-Allow-Origin', 'http://trumpymctweetface.com');
}
});

app.get('/1.1/statuses/user_timeline.json', function(req, res) {
if (req.query.screen_name === 'realDonaldTrump') {
proxy.web(req, res);
} else {
res.sendStatus(403);
}
});

app.get('/1.1/statuses/show.json', function(req, res) {
proxy.web(req, res);
});

export const api = functions.https.onRequest(app);

0 comments on commit 28f5daa

Please sign in to comment.