Skip to content

Commit

Permalink
Recursive parse date in cloud function (#2014)
Browse files Browse the repository at this point in the history
* Recursive parse date in cloud function

* Use _.mapValues
  • Loading branch information
Marco129 authored and drew-gross committed Jun 9, 2016
1 parent fcd914b commit 2e50416
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 10 deletions.
57 changes: 57 additions & 0 deletions spec/CloudCode.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,63 @@ describe('Cloud Code', () => {
});
});

it('test cloud function request params types', function(done) {
Parse.Cloud.define('params', function(req, res) {
expect(req.params.date instanceof Date).toBe(true);
expect(req.params.date.getTime()).toBe(1463907600000);
expect(req.params.dateList[0] instanceof Date).toBe(true);
expect(req.params.dateList[0].getTime()).toBe(1463907600000);
expect(req.params.complexStructure.date[0] instanceof Date).toBe(true);
expect(req.params.complexStructure.date[0].getTime()).toBe(1463907600000);
expect(req.params.complexStructure.deepDate.date[0] instanceof Date).toBe(true);
expect(req.params.complexStructure.deepDate.date[0].getTime()).toBe(1463907600000);
expect(req.params.complexStructure.deepDate2[0].date instanceof Date).toBe(true);
expect(req.params.complexStructure.deepDate2[0].date.getTime()).toBe(1463907600000);
return res.success({});
});

let params = {
'date': {
'__type': 'Date',
'iso': '2016-05-22T09:00:00.000Z'
},
'dateList': [
{
'__type': 'Date',
'iso': '2016-05-22T09:00:00.000Z'
}
],
'lol': 'hello',
'complexStructure': {
'date': [
{
'__type': 'Date',
'iso': '2016-05-22T09:00:00.000Z'
}
],
'deepDate': {
'date': [
{
'__type': 'Date',
'iso': '2016-05-22T09:00:00.000Z'
}
]
},
'deepDate2': [
{
'date': {
'__type': 'Date',
'iso': '2016-05-22T09:00:00.000Z'
}
}
]
}
};
Parse.Cloud.run('params', params).then((result) => {
done();
});
});

it('test cloud function should echo keys', function(done) {
Parse.Cloud.define('echoKeys', function(req, res){
return res.success({
Expand Down
35 changes: 25 additions & 10 deletions src/Routers/FunctionsRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,29 @@ var express = require('express'),
triggers = require('../triggers');

import PromiseRouter from '../PromiseRouter';
import _ from 'lodash';

function parseDate(params) {
return _.mapValues(params, (obj) => {
if (Array.isArray(obj)) {
return obj.map((item) => {
if (item && item.__type == 'Date') {
return new Date(item.iso);
} else if (item && typeof item === 'object') {
return parseDate(item);
} else {
return item;
}
});
} else if (obj && obj.__type == 'Date') {
return new Date(obj.iso);
} else if (obj && typeof obj === 'object') {
return parseDate(obj);
} else {
return obj;
}
});
}

export class FunctionsRouter extends PromiseRouter {

Expand Down Expand Up @@ -36,15 +59,8 @@ export class FunctionsRouter extends PromiseRouter {
var theFunction = triggers.getFunction(req.params.functionName, applicationId);
var theValidator = triggers.getValidator(req.params.functionName, applicationId);
if (theFunction) {
const params = Object.assign({}, req.body, req.query);
for (var key in params) {
if (params.hasOwnProperty(key)) {
var value = params[key];
if (value && value.__type == 'Date') {
params[key] = new Date(value.iso);
}
}
}
let params = Object.assign({}, req.body, req.query);
params = parseDate(params);
var request = {
params: params,
master: req.auth && req.auth.isMaster,
Expand Down Expand Up @@ -73,4 +89,3 @@ export class FunctionsRouter extends PromiseRouter {
}
}
}

0 comments on commit 2e50416

Please sign in to comment.