mirror of
https://github.com/scratchfoundation/scratch-www.git
synced 2025-02-25 05:44:13 -05:00
Add method to api for submitting forms
Some of our legacy endpoints expect this style rather than json. Also clean up the way useCsrf works — don't always set json attribute to an empty object.
This commit is contained in:
parent
d27b0a2433
commit
26e1ee553b
2 changed files with 8 additions and 3 deletions
|
@ -18,7 +18,7 @@ module.exports = function (opts, callback) {
|
||||||
defaults(opts, {
|
defaults(opts, {
|
||||||
host: process.env.API_HOST,
|
host: process.env.API_HOST,
|
||||||
headers: {},
|
headers: {},
|
||||||
json: {},
|
responseType: 'json',
|
||||||
useCsrf: false
|
useCsrf: false
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -33,6 +33,11 @@ module.exports = function (opts, callback) {
|
||||||
.join(opts.uri.indexOf('?') === -1 ? '?' : '&');
|
.join(opts.uri.indexOf('?') === -1 ? '?' : '&');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (opts.formData) {
|
||||||
|
opts.body = urlParams(opts.formData);
|
||||||
|
opts.headers['Content-Type'] = 'application/x-www-form-urlencoded';
|
||||||
|
}
|
||||||
|
|
||||||
var apiRequest = function (opts) {
|
var apiRequest = function (opts) {
|
||||||
if (opts.host !== '') {
|
if (opts.host !== '') {
|
||||||
// For IE < 10, we must use XDR for cross-domain requests. XDR does not support
|
// For IE < 10, we must use XDR for cross-domain requests. XDR does not support
|
||||||
|
@ -69,7 +74,6 @@ module.exports = function (opts, callback) {
|
||||||
if (opts.useCsrf) {
|
if (opts.useCsrf) {
|
||||||
jar.use('scratchcsrftoken', '/csrf_token/', function (err, csrftoken) {
|
jar.use('scratchcsrftoken', '/csrf_token/', function (err, csrftoken) {
|
||||||
if (err) return log.error('Error while retrieving CSRF token', err);
|
if (err) return log.error('Error while retrieving CSRF token', err);
|
||||||
opts.json.csrftoken = csrftoken;
|
|
||||||
opts.headers['X-CSRFToken'] = csrftoken;
|
opts.headers['X-CSRFToken'] = csrftoken;
|
||||||
apiRequest(opts);
|
apiRequest(opts);
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
|
|
|
@ -6,7 +6,8 @@ module.exports = function urlParams (values) {
|
||||||
return Object
|
return Object
|
||||||
.keys(values)
|
.keys(values)
|
||||||
.map(function (key) {
|
.map(function (key) {
|
||||||
return [key, values[key]]
|
var value = typeof values[key] === 'undefined' ? '' : values[key];
|
||||||
|
return [key, value]
|
||||||
.map(encodeURIComponent)
|
.map(encodeURIComponent)
|
||||||
.join('=');
|
.join('=');
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in a new issue