Skip to content

Exposes the ClientSDK infos if available #2259

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
Jul 12, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
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
15 changes: 10 additions & 5 deletions spec/InstallationsRouter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ describe('InstallationsRouter', () => {
deviceType: 'android'
}
},
query: {}
query: {},
info: {}
};

var router = new InstallationsRouter();
Expand Down Expand Up @@ -56,7 +57,8 @@ describe('InstallationsRouter', () => {
where: {
deviceType: 'android'
}
}
},
info: {}
};

var router = new InstallationsRouter();
Expand Down Expand Up @@ -87,7 +89,8 @@ describe('InstallationsRouter', () => {
body: {},
query: {
limit: 0
}
},
info: {}
};

var router = new InstallationsRouter();
Expand Down Expand Up @@ -118,7 +121,8 @@ describe('InstallationsRouter', () => {
body: {},
query: {
count: 1
}
},
info: {}
};

var router = new InstallationsRouter();
Expand Down Expand Up @@ -153,7 +157,8 @@ describe('InstallationsRouter', () => {
query: {
limit: 0,
count: 1
}
},
info: {}
};

var router = new InstallationsRouter();
Expand Down
20 changes: 20 additions & 0 deletions spec/Middlewares.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,24 @@ describe('middlewares', () => {
});
});
});

it('should properly parse the SDK versions', () => {
let clientSDKFromVersion = middlewares.clientSDKFromVersion;
expect(clientSDKFromVersion('i1.1.1')).toEqual({
sdk: 'i',
version: '1.1.1'
});
expect(clientSDKFromVersion('i1')).toEqual({
sdk: 'i',
version: '1'
});
expect(clientSDKFromVersion('apple-tv1.13.0')).toEqual({
sdk: 'apple-tv',
version: '1.13.0'
});
expect(clientSDKFromVersion('js1.9.0')).toEqual({
sdk: 'js',
version: '1.9.0'
});
})
});
3 changes: 2 additions & 1 deletion src/RestQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ import { default as FilesController } from './Controllers/FilesController';
// include
// keys
// redirectClassNameForKey
function RestQuery(config, auth, className, restWhere = {}, restOptions = {}) {
function RestQuery(config, auth, className, restWhere = {}, restOptions = {}, clientSDK) {

this.config = config;
this.auth = auth;
this.className = className;
this.restWhere = restWhere;
this.clientSDK = clientSDK;
this.response = null;
this.findOptions = {};
if (!this.auth.isMaster) {
Expand Down
4 changes: 2 additions & 2 deletions src/RestWrite.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ import _ from 'lodash';
// RestWrite will handle objectId, createdAt, and updatedAt for
// everything. It also knows to use triggers and special modifications
// for the _User class.
function RestWrite(config, auth, className, query, data, originalData) {
function RestWrite(config, auth, className, query, data, originalData, clientSDK) {
this.config = config;
this.auth = auth;
this.className = className;
this.clientSDK = clientSDK;
this.storage = {};
this.runOptions = {};

if (!query && data.objectId) {
throw new Parse.Error(Parse.Error.INVALID_KEY_NAME, 'objectId is an invalid field name.');
}
Expand Down
10 changes: 5 additions & 5 deletions src/Routers/ClassesRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export class ClassesRouter extends PromiseRouter {
if (typeof body.where === 'string') {
body.where = JSON.parse(body.where);
}
return rest.find(req.config, req.auth, req.params.className, body.where, options)
return rest.find(req.config, req.auth, req.params.className, body.where, options, req.info.clientSDK)
.then((response) => {
if (response && response.results) {
for (let result of response.results) {
Expand Down Expand Up @@ -77,7 +77,7 @@ export class ClassesRouter extends PromiseRouter {
options.include = String(body.include);
}

return rest.get(req.config, req.auth, req.params.className, req.params.objectId, options)
return rest.get(req.config, req.auth, req.params.className, req.params.objectId, options, req.info.clientSDK)
.then((response) => {
if (!response.results || response.results.length == 0) {
throw new Parse.Error(Parse.Error.OBJECT_NOT_FOUND, 'Object not found.');
Expand All @@ -99,15 +99,15 @@ export class ClassesRouter extends PromiseRouter {
}

handleCreate(req) {
return rest.create(req.config, req.auth, req.params.className, req.body);
return rest.create(req.config, req.auth, req.params.className, req.body, req.info.clientSDK);
}

handleUpdate(req) {
return rest.update(req.config, req.auth, req.params.className, req.params.objectId, req.body);
return rest.update(req.config, req.auth, req.params.className, req.params.objectId, req.body, req.info.clientSDK);
}

handleDelete(req) {
return rest.del(req.config, req.auth, req.params.className, req.params.objectId)
return rest.del(req.config, req.auth, req.params.className, req.params.objectId, req.info.clientSDK)
.then(() => {
return {response: {}};
});
Expand Down
2 changes: 1 addition & 1 deletion src/Routers/IAPValidationRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function validateWithAppStore(url, receipt) {
}

function getFileForProductIdentifier(productIdentifier, req) {
return rest.find(req.config, req.auth, '_Product', { productIdentifier: productIdentifier }).then(function(result){
return rest.find(req.config, req.auth, '_Product', { productIdentifier: productIdentifier }, undefined, req.info.clientSDK).then(function(result){
const products = result.results;
if (!products || products.length != 1) {
// Error not found or too many
Expand Down
2 changes: 1 addition & 1 deletion src/Routers/InstallationsRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class InstallationsRouter extends ClassesRouter {
}

return rest.find(req.config, req.auth,
'_Installation', body.where, options)
'_Installation', body.where, options, req.info.clientSDK)
.then((response) => {
return {response: response};
});
Expand Down
2 changes: 1 addition & 1 deletion src/Routers/SessionsRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class SessionsRouter extends ClassesRouter {
throw new Parse.Error(Parse.Error.INVALID_SESSION_TOKEN,
'Session token required.');
}
return rest.find(req.config, Auth.master(req.config), '_Session', { sessionToken: req.info.sessionToken })
return rest.find(req.config, Auth.master(req.config), '_Session', { sessionToken: req.info.sessionToken }, undefined, req.info.clientSDK)
.then((response) => {
if (!response.results || response.results.length == 0) {
throw new Parse.Error(Parse.Error.INVALID_SESSION_TOKEN,
Expand Down
4 changes: 2 additions & 2 deletions src/Routers/UsersRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export class UsersRouter extends ClassesRouter {
let sessionToken = req.info.sessionToken;
return rest.find(req.config, Auth.master(req.config), '_Session',
{ sessionToken },
{ include: 'user' })
{ include: 'user' }, req.info.clientSDK)
.then((response) => {
if (!response.results ||
response.results.length == 0 ||
Expand Down Expand Up @@ -145,7 +145,7 @@ export class UsersRouter extends ClassesRouter {
let success = {response: {}};
if (req.info && req.info.sessionToken) {
return rest.find(req.config, Auth.master(req.config), '_Session',
{ sessionToken: req.info.sessionToken }
{ sessionToken: req.info.sessionToken }, undefined, req.info.clientSDK
).then((records) => {
if (records.results && records.results.length) {
return rest.del(req.config, Auth.master(req.config), '_Session',
Expand Down
3 changes: 2 additions & 1 deletion src/batch.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ function handleBatch(router, req) {
body: restRequest.body,
params: match.params,
config: req.config,
auth: req.auth
auth: req.auth,
info: req.info
};

promises.push(match.handler(request).then((response) => {
Expand Down
21 changes: 19 additions & 2 deletions src/middlewares.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@ var Parse = require('parse/node').Parse;
var auth = require('./Auth');
var Config = require('./Config');

function clientSDKFromVersion(version) {
let versionRE = /([-a-zA-Z]+)([0-9\.]+)/;
let match = version.toLowerCase().match(versionRE);
if (match && match.length === 3) {
return {
sdk: match[1],
version: match[2]
}
}
}

// Checks that the request is authorized for this app and checks user
// auth too.
// The bodyparser should run before this middleware.
Expand All @@ -25,7 +36,8 @@ function handleParseHeaders(req, res, next) {
clientKey: req.get('X-Parse-Client-Key'),
javascriptKey: req.get('X-Parse-Javascript-Key'),
dotNetKey: req.get('X-Parse-Windows-Key'),
restAPIKey: req.get('X-Parse-REST-API-Key')
restAPIKey: req.get('X-Parse-REST-API-Key'),
clientVersion: req.get('X-Parse-Client-Version')
};

var basicAuth = httpAuth(req);
Expand Down Expand Up @@ -93,6 +105,10 @@ function handleParseHeaders(req, res, next) {
}
}

if (info.clientVersion) {
info.clientSDK = clientSDKFromVersion(info.clientVersion);
}

if (fileViaJSON) {
// We need to repopulate req.body with a buffer
var base64 = req.body.base64;
Expand Down Expand Up @@ -282,5 +298,6 @@ module.exports = {
handleParseErrors: handleParseErrors,
handleParseHeaders: handleParseHeaders,
enforceMasterKeyAccess: enforceMasterKeyAccess,
promiseEnforceMasterKeyAccess
promiseEnforceMasterKeyAccess,
clientSDKFromVersion
};
18 changes: 9 additions & 9 deletions src/rest.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@ var RestWrite = require('./RestWrite');
var triggers = require('./triggers');

// Returns a promise for an object with optional keys 'results' and 'count'.
function find(config, auth, className, restWhere, restOptions) {
function find(config, auth, className, restWhere, restOptions, clientSDK) {
enforceRoleSecurity('find', className, auth);
let query = new RestQuery(config, auth, className, restWhere, restOptions);
let query = new RestQuery(config, auth, className, restWhere, restOptions, clientSDK);
return query.execute();
}

// get is just like find but only queries an objectId.
const get = (config, auth, className, objectId, restOptions) => {
const get = (config, auth, className, objectId, restOptions, clientSDK) => {
enforceRoleSecurity('get', className, auth);
let query = new RestQuery(config, auth, className, { objectId }, restOptions);
let query = new RestQuery(config, auth, className, { objectId }, restOptions, clientSDK);
return query.execute();
}

// Returns a promise that doesn't resolve to any useful value.
function del(config, auth, className, objectId) {
function del(config, auth, className, objectId, clientSDK) {
if (typeof objectId !== 'string') {
throw new Parse.Error(Parse.Error.INVALID_JSON,
'bad objectId');
Expand Down Expand Up @@ -92,16 +92,16 @@ function del(config, auth, className, objectId) {
}

// Returns a promise for a {response, status, location} object.
function create(config, auth, className, restObject) {
function create(config, auth, className, restObject, clientSDK) {
enforceRoleSecurity('create', className, auth);
var write = new RestWrite(config, auth, className, null, restObject);
var write = new RestWrite(config, auth, className, null, restObject, clientSDK);
return write.execute();
}

// Returns a promise that contains the fields of the update that the
// REST API is supposed to return.
// Usually, this is just updatedAt.
function update(config, auth, className, objectId, restObject) {
function update(config, auth, className, objectId, restObject, clientSDK) {
enforceRoleSecurity('update', className, auth);

return Promise.resolve().then(() => {
Expand All @@ -117,7 +117,7 @@ function update(config, auth, className, objectId, restObject) {
originalRestObject = response.results[0];
}

var write = new RestWrite(config, auth, className, {objectId: objectId}, restObject, originalRestObject);
var write = new RestWrite(config, auth, className, {objectId: objectId}, restObject, originalRestObject, clientSDK);
return write.execute();
});
}
Expand Down