-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
Copy pathParseServerRESTController.js
164 lines (154 loc) · 5.03 KB
/
ParseServerRESTController.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
const Config = require('./Config');
const Auth = require('./Auth');
const RESTController = require('parse/lib/node/RESTController');
const Parse = require('parse/node');
function getSessionToken(options) {
if (options && typeof options.sessionToken === 'string') {
return Promise.resolve(options.sessionToken);
}
return Promise.resolve(null);
}
function getAuth(options = {}, config) {
const installationId = options.installationId || 'cloud';
if (options.useMasterKey) {
return Promise.resolve(new Auth.Auth({ config, isMaster: true, installationId }));
}
return getSessionToken(options).then(sessionToken => {
if (sessionToken) {
options.sessionToken = sessionToken;
return Auth.getAuthForSessionToken({
config,
sessionToken: sessionToken,
installationId,
});
} else {
return Promise.resolve(new Auth.Auth({ config, installationId }));
}
});
}
function ParseServerRESTController(applicationId, router) {
function handleRequest(method, path, data = {}, options = {}, config) {
// Store the arguments, for later use if internal fails
const args = arguments;
if (!config) {
config = Config.get(applicationId);
}
const serverURL = new URL(config.serverURL);
if (path.indexOf(serverURL.pathname) === 0) {
path = path.slice(serverURL.pathname.length, path.length);
}
if (path[0] !== '/') {
path = '/' + path;
}
if (path === '/batch') {
const batch = transactionRetries => {
let initialPromise = Promise.resolve();
if (data.transaction === true) {
initialPromise = config.database.createTransactionalSession();
}
return initialPromise.then(() => {
const promises = data.requests.map(request => {
return handleRequest(request.method, request.path, request.body, options, config).then(
response => {
if (options.returnStatus) {
const status = response._status;
const headers = response._headers;
delete response._status;
delete response._headers;
return { success: response, _status: status, _headers: headers };
}
return { success: response };
},
error => {
return {
error: { code: error.code, error: error.message },
};
}
);
});
return Promise.all(promises)
.then(result => {
if (data.transaction === true) {
if (result.find(resultItem => typeof resultItem.error === 'object')) {
return config.database.abortTransactionalSession().then(() => {
return Promise.reject(result);
});
} else {
return config.database.commitTransactionalSession().then(() => {
return result;
});
}
} else {
return result;
}
})
.catch(error => {
if (
error &&
error.find(
errorItem => typeof errorItem.error === 'object' && errorItem.error.code === 251
) &&
transactionRetries > 0
) {
return batch(transactionRetries - 1);
}
throw error;
});
});
};
return batch(5);
}
let query;
if (method === 'GET') {
query = data;
}
return new Promise((resolve, reject) => {
getAuth(options, config).then(auth => {
const request = {
body: data,
config,
auth,
info: {
applicationId: applicationId,
sessionToken: options.sessionToken,
installationId: options.installationId,
context: options.context || {},
},
query,
};
return Promise.resolve()
.then(() => {
return router.tryRouteRequest(method, path, request);
})
.then(
resp => {
const { response, status, headers = {} } = resp;
if (options.returnStatus) {
resolve({ ...response, _status: status, _headers: headers });
} else {
resolve(response);
}
},
err => {
if (
err instanceof Parse.Error &&
err.code == Parse.Error.INVALID_JSON &&
err.message == `cannot route ${method} ${path}`
) {
RESTController.request.apply(null, args).then(resolve, reject);
} else {
reject(err);
}
}
);
}, reject);
});
}
return {
request: handleRequest,
ajax: RESTController.ajax,
handleError: RESTController.handleError,
};
}
export default ParseServerRESTController;
export { ParseServerRESTController };