|
| 1 | +const Config = require('./Config'); |
| 2 | +const Auth = require('./Auth'); |
| 3 | +const RESTController = require('parse/lib/node/RESTController'); |
| 4 | +const URL = require('url'); |
| 5 | +const Parse = require('parse/node'); |
| 6 | + |
| 7 | +function getSessionToken(options) { |
| 8 | + if (options && typeof options.sessionToken === 'string') { |
| 9 | + return Parse.Promise.as(options.sessionToken); |
| 10 | + } |
| 11 | + return Parse.Promise.as(null); |
| 12 | +} |
| 13 | + |
| 14 | +function getAuth(options, config) { |
| 15 | + if (options.useMasterKey) { |
| 16 | + return Parse.Promise.as(new Auth.Auth({config, isMaster: true, installationId: 'cloud' })); |
| 17 | + } |
| 18 | + return getSessionToken(options).then((sessionToken) => { |
| 19 | + if (sessionToken) { |
| 20 | + options.sessionToken = sessionToken; |
| 21 | + return Auth.getAuthForSessionToken({ |
| 22 | + config, |
| 23 | + sessionToken: sessionToken, |
| 24 | + installationId: 'cloud' |
| 25 | + }); |
| 26 | + } else { |
| 27 | + return Parse.Promise.as(new Auth.Auth({ config, installationId: 'cloud' })); |
| 28 | + } |
| 29 | + }) |
| 30 | +} |
| 31 | + |
| 32 | +function ParseServerRESTController(applicationId, router) { |
| 33 | + function handleRequest(method, path, data = {}, options = {}) { |
| 34 | + // Store the arguments, for later use if internal fails |
| 35 | + let args = arguments; |
| 36 | + |
| 37 | + let config = new Config(applicationId); |
| 38 | + let serverURL = URL.parse(config.serverURL); |
| 39 | + if (path.indexOf(serverURL.path) === 0) { |
| 40 | + path = path.slice(serverURL.path.length, path.length); |
| 41 | + } |
| 42 | + |
| 43 | + if (path[0] !== "/") { |
| 44 | + path = "/" + path; |
| 45 | + } |
| 46 | + |
| 47 | + if (path === '/batch') { |
| 48 | + let promises = data.requests.map((request) => { |
| 49 | + return handleRequest(request.method, request.path, request.body, options).then((response) => { |
| 50 | + return Parse.Promise.as({success: response}); |
| 51 | + }, (error) => { |
| 52 | + return Parse.Promise.as({error: {code: error.code, error: error.message}}); |
| 53 | + }); |
| 54 | + }); |
| 55 | + return Parse.Promise.all(promises); |
| 56 | + } |
| 57 | + |
| 58 | + let query; |
| 59 | + if (method === 'GET') { |
| 60 | + query = data; |
| 61 | + } |
| 62 | + |
| 63 | + return new Parse.Promise((resolve, reject) => { |
| 64 | + getAuth(options, config).then((auth) => { |
| 65 | + let request = { |
| 66 | + body: data, |
| 67 | + config, |
| 68 | + auth, |
| 69 | + info: { |
| 70 | + applicationId: applicationId, |
| 71 | + sessionToken: options.sessionToken |
| 72 | + }, |
| 73 | + query |
| 74 | + }; |
| 75 | + return Promise.resolve().then(() => { |
| 76 | + return router.tryRouteRequest(method, path, request); |
| 77 | + }).then((response) => { |
| 78 | + resolve(response.response, response.status, response); |
| 79 | + }, (err) => { |
| 80 | + if (err instanceof Parse.Error && |
| 81 | + err.code == Parse.Error.INVALID_JSON && |
| 82 | + err.message == `cannot route ${method} ${path}`) { |
| 83 | + RESTController.request.apply(null, args).then(resolve, reject); |
| 84 | + } else { |
| 85 | + reject(err); |
| 86 | + } |
| 87 | + }); |
| 88 | + }, reject); |
| 89 | + }); |
| 90 | + }; |
| 91 | + |
| 92 | + return { |
| 93 | + request: handleRequest, |
| 94 | + ajax: RESTController.ajax |
| 95 | + }; |
| 96 | +}; |
| 97 | + |
| 98 | +export default ParseServerRESTController; |
| 99 | +export { ParseServerRESTController }; |
0 commit comments