Skip to content

Recursive parse date in cloud function #2014

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

Merged
merged 2 commits into from
Jun 9, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
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 @@ -6,6 +6,29 @@ var express = require('express'),

import PromiseRouter from '../PromiseRouter';

function parseDate(params) {
for (let key in params) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use _.mapValues here? Other than that this looks good!

if (params.hasOwnProperty(key)) {
let value = params[key];
if (Array.isArray(value)) {
params[key] = value.map((item) => {
if (item && item.__type == 'Date') {
return new Date(item.iso);
} else if (typeof item === 'object') {
return parseDate(item);
}
return item;
});
} else if (value && value.__type == 'Date') {
params[key] = new Date(value.iso);
} else if (typeof value === 'object') {
params[key] = parseDate(value);
}
}
}
return params;
}

export class FunctionsRouter extends PromiseRouter {

mountRoutes() {
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 {
}
}
}