Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for form submissions #780

Merged
merged 28 commits into from
Jun 16, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
2cca018
Add support for form submissions
RaeesBhatti Mar 31, 2020
9d5ddb9
Make mkdirp async
RaeesBhatti Mar 31, 2020
e53650f
Dev: Send forms data to submission-created function
RaeesBhatti Apr 7, 2020
57cee19
Merge branch 'master' into raees/forms
RaeesBhatti Apr 7, 2020
e55d159
Delete forms.js
RaeesBhatti Apr 7, 2020
eda6b0c
Merge branch 'master' into raees/forms
RaeesBhatti May 17, 2020
262fd6d
Use this.error and this.log
RaeesBhatti May 17, 2020
5bbbed0
Add mkdirp as dev dep
RaeesBhatti May 17, 2020
f08ec52
Merge branch 'master' into raees/forms
RaeesBhatti Jun 3, 2020
e1a1c93
Merge branch 'master' into raees/forms
RaeesBhatti Jun 10, 2020
1fc5850
A better check for form mime
RaeesBhatti Jun 15, 2020
bcb9a1e
Dont trigger form handler on internal URl's
RaeesBhatti Jun 15, 2020
ac7e780
Fix 'referrer' field in forms
RaeesBhatti Jun 15, 2020
3abe22e
Normalize multipart form data
RaeesBhatti Jun 15, 2020
c559b46
Add convenience fields for forms
RaeesBhatti Jun 15, 2020
f88f67f
Make form handler function URL relative to root
RaeesBhatti Jun 15, 2020
16d8493
Add 'site' field to form event
RaeesBhatti Jun 15, 2020
ec4c462
Add files to Form event
RaeesBhatti Jun 15, 2020
739b950
Merge branch 'master' into raees/forms
RaeesBhatti Jun 15, 2020
4752bc5
Move handleFormSubmission outside
RaeesBhatti Jun 15, 2020
81c9eee
Formatting
RaeesBhatti Jun 15, 2020
04156f0
Remove debug logs
RaeesBhatti Jun 15, 2020
090b135
Fix query params when handling forms
RaeesBhatti Jun 16, 2020
a87c18e
Add submission-created handler for tests
RaeesBhatti Jun 16, 2020
c07aa55
Add tests for Netlify Forms
RaeesBhatti Jun 16, 2020
9e9f4db
Formatting
RaeesBhatti Jun 16, 2020
0c50bcc
:art: Use non-arrow function
RaeesBhatti Jun 16, 2020
384c074
Fix Node 8 incompatiblity
RaeesBhatti Jun 16, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
190 changes: 177 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
"configstore": "^5.0.0",
"cookie": "^0.4.0",
"copy-template-dir": "^1.4.0",
"csv-writer": "^1.6.0",
"debug": "^4.1.1",
"dot-prop": "^5.1.0",
"dotenv": "^8.2.0",
Expand Down Expand Up @@ -119,6 +120,7 @@
"log-symbols": "^2.2.0",
"make-dir": "^3.0.0",
"minimist": "^1.2.0",
"mkdirp": "^1.0.3",
"netlify": "^4.0.0",
"netlify-redirect-parser": "^2.3.0",
"netlify-redirector": "^0.1.0",
Expand Down Expand Up @@ -168,7 +170,6 @@
"husky": "^3.0.3",
"lint-staged": "^9.2.1",
"markdown-magic": "^0.1.25",
"mkdirp": "^0.5.1",
"mocha": "^6.2.0",
"nock": "^10.0.6",
"npm-run-all": "^4.1.5",
Expand Down
21 changes: 21 additions & 0 deletions src/commands/dev/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@ const getPort = require('get-port')
const chokidar = require('chokidar')
const proxyMiddleware = require('http-proxy-middleware')
const cookie = require('cookie')
const mkdirp = require('mkdirp')
const csvWriterPkg = require('csv-writer')
const get = require('lodash.get')
const isEmpty = require('lodash.isempty')
const { serveFunctions } = require('../../utils/serve-functions')
const { serverSettings } = require('../../utils/detect-server')
const { detectFunctionsBuilder } = require('../../utils/detect-functions-builder')
const { collectRequestBody } = require('../../utils/forms')
const Command = require('../../utils/command')
const chalk = require('chalk')
const jwtDecode = require('jwt-decode')
Expand Down Expand Up @@ -172,7 +175,25 @@ async function startProxy(settings, addonUrls, configPath, projectDir) {
configPath,
})

mkdirp.sync(path.resolve(projectDir, '.netlify'))
ehmicky marked this conversation as resolved.
Show resolved Hide resolved

const formSubmissionsFile = path.resolve(projectDir, '.netlify', 'form-submissions.csv')
const csvWriter = csvWriterPkg.createArrayCsvWriter({
alwaysQuote: true,
header: ['timestamp', 'path', 'body'],
path: formSubmissionsFile,
append: true,
})

const server = http.createServer(function(req, res) {
if (req.method === 'POST') {
collectRequestBody(req, async body => {
ehmicky marked this conversation as resolved.
Show resolved Hide resolved
await csvWriter.writeRecords([[new Date().toISOString(), req.url, body]])
res.end(body)
})
return
}

if (isFunction(settings.functionsPort, req)) {
return proxy.web(req, res, { target: functionsServer })
}
Expand Down
17 changes: 17 additions & 0 deletions src/utils/forms.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const FORM_URLENCODED = 'application/x-www-form-urlencoded'

function collectRequestBody(request, callback) {
RaeesBhatti marked this conversation as resolved.
Show resolved Hide resolved
let body = ''

request.on('data', chunk => body += chunk)

if(request.headers['content-type'] === FORM_URLENCODED) {
request.on('end', () => callback(body))
} else {
callback(body)
}
}

module.exports = {
collectRequestBody,
}