BVB Source Codes

parse-server Show ClassesRouter.js Source code

Return Download parse-server: download ClassesRouter.js Source code - Download parse-server Source code - Type:.js
  1.  
  2. import PromiseRouter from '../PromiseRouter';
  3. import rest          from '../rest';
  4. import _             from 'lodash';
  5. import Parse         from 'parse/node';
  6.  
  7. const ALLOWED_GET_QUERY_KEYS = ['keys', 'include'];
  8.  
  9. export class ClassesRouter extends PromiseRouter {
  10.  
  11.   handleFind(req) {
  12.     const body = Object.assign(req.body, ClassesRouter.JSONFromQuery(req.query));
  13.     const options = {};
  14.     const allowConstraints = ['skip', 'limit', 'order', 'count', 'keys',
  15.       'include', 'redirectClassNameForKey', 'where'];
  16.  
  17.     for (const key of Object.keys(body)) {
  18.       if (allowConstraints.indexOf(key) === -1) {
  19.         throw new Parse.Error(Parse.Error.INVALID_QUERY, `Invalid parameter for query: ${key}`);
  20.       }
  21.     }
  22.  
  23.     if (body.skip) {
  24.       options.skip = Number(body.skip);
  25.     }
  26.     if (body.limit || body.limit === 0) {
  27.       options.limit = Number(body.limit);
  28.     } else {
  29.       options.limit = Number(100);
  30.     }
  31.     if (body.order) {
  32.       options.order = String(body.order);
  33.     }
  34.     if (body.count) {
  35.       options.count = true;
  36.     }
  37.     if (typeof body.keys == 'string') {
  38.       options.keys = body.keys;
  39.     }
  40.     if (body.include) {
  41.       options.include = String(body.include);
  42.     }
  43.     if (body.redirectClassNameForKey) {
  44.       options.redirectClassNameForKey = String(body.redirectClassNameForKey);
  45.     }
  46.     if (typeof body.where === 'string') {
  47.       body.where = JSON.parse(body.where);
  48.     }
  49.     return rest.find(req.config, req.auth, req.params.className, body.where, options, req.info.clientSDK)
  50.       .then((response) => {
  51.         if (response && response.results) {
  52.           for (const result of response.results) {
  53.             if (result.sessionToken) {
  54.               result.sessionToken = req.info.sessionToken || result.sessionToken;
  55.             }
  56.           }
  57.         }
  58.         return { response: response };
  59.       });
  60.   }
  61.  
  62.   // Returns a promise for a {response} object.
  63.   handleGet(req) {
  64.     const body = Object.assign(req.body, ClassesRouter.JSONFromQuery(req.query));
  65.     const options = {};
  66.  
  67.     for (const key of Object.keys(body)) {
  68.       if (ALLOWED_GET_QUERY_KEYS.indexOf(key) === -1) {
  69.         throw new Parse.Error(Parse.Error.INVALID_QUERY, 'Improper encode of parameter');
  70.       }
  71.     }
  72.  
  73.     if (typeof body.keys == 'string') {
  74.       options.keys = body.keys;
  75.     }
  76.     if (body.include) {
  77.       options.include = String(body.include);
  78.     }
  79.  
  80.     return rest.get(req.config, req.auth, req.params.className, req.params.objectId, options, req.info.clientSDK)
  81.       .then((response) => {
  82.         if (!response.results || response.results.length == 0) {
  83.           throw new Parse.Error(Parse.Error.OBJECT_NOT_FOUND, 'Object not found.');
  84.         }
  85.  
  86.         if (req.params.className === "_User") {
  87.  
  88.           delete response.results[0].sessionToken;
  89.  
  90.           const user =  response.results[0];
  91.  
  92.           if (req.auth.user && user.objectId == req.auth.user.id) {
  93.             // Force the session token
  94.             response.results[0].sessionToken = req.info.sessionToken;
  95.           }
  96.         }
  97.         return { response: response.results[0] };
  98.       });
  99.   }
  100.  
  101.   handleCreate(req) {
  102.     return rest.create(req.config, req.auth, req.params.className, req.body, req.info.clientSDK);
  103.   }
  104.  
  105.   handleUpdate(req) {
  106.     const where = { objectId: req.params.objectId }
  107.     return rest.update(req.config, req.auth, req.params.className, where, req.body, req.info.clientSDK);
  108.   }
  109.  
  110.   handleDelete(req) {
  111.     return rest.del(req.config, req.auth, req.params.className, req.params.objectId, req.info.clientSDK)
  112.       .then(() => {
  113.         return {response: {}};
  114.       });
  115.   }
  116.  
  117.   static JSONFromQuery(query) {
  118.     const json = {};
  119.     for (const [key, value] of _.entries(query)) {
  120.       try {
  121.         json[key] = JSON.parse(value);
  122.       } catch (e) {
  123.         json[key] = value;
  124.       }
  125.     }
  126.     return json
  127.   }
  128.  
  129.   mountRoutes() {
  130.     this.route('GET', '/classes/:className', (req) => { return this.handleFind(req); });
  131.     this.route('GET', '/classes/:className/:objectId', (req) => { return this.handleGet(req); });
  132.     this.route('POST', '/classes/:className', (req) => { return this.handleCreate(req); });
  133.     this.route('PUT', '/classes/:className/:objectId', (req) => { return this.handleUpdate(req); });
  134.     this.route('DELETE',  '/classes/:className/:objectId', (req) => { return this.handleDelete(req); });
  135.   }
  136. }
  137.  
  138. export default ClassesRouter;
  139.  
downloadClassesRouter.js Source code - Download parse-server Source code
Related Source Codes/Software:
react-boilerplate - 2017-06-07
webtorrent - Streaming torrent client for the web ... 2017-06-06
machine-learning-for-software-engineers - A complete daily plan for studying to become a mac... 2017-06-06
upterm - A terminal emulator for the 21st century. 2017-06-06
lottie-android - Render After Effects animations natively on Androi... 2017-06-07
AsyncDisplayKit - Smooth asynchronous user interfaces for iOS apps. ... 2017-06-07
ionicons - The premium icon font for Ionic ... 2017-06-07
storybook - 2017-06-07
prettier - Prettier is an opinionated JavaScript formatter. ... 2017-06-08
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
postal - 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top