Skip to content

Commit 3cb4f7c

Browse files
authored
refactor: Optimize code for request options sent to RESTController (#2430)
1 parent b849169 commit 3cb4f7c

21 files changed

+276
-367
lines changed

.github/workflows/ci.yml

+3-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,9 @@ jobs:
9696
node-version: ${{ matrix.NODE_VERSION }}
9797
cache: npm
9898
- run: npm ci
99-
- run: npm test -- --maxWorkers=4
99+
# Run unit tests
100+
- run: npm test -- --maxWorkers=4
101+
# Run integration tests
100102
- run: npm run test:mongodb
101103
env:
102104
CI: true

eslint.config.js

-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ module.exports = tseslint.config({
2424
"no-useless-escape": "off",
2525
"no-var": "error",
2626
"no-console": 0,
27-
"no-prototype-builtins": "off",
2827
"require-atomic-updates": "off",
2928
"prefer-spread": "off",
3029
"prefer-rest-params": "off",

integration/test/ParseUserTest.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -598,8 +598,8 @@ describe('Parse User', () => {
598598
})
599599
.then(() => {
600600
assert.equal(Object.keys(user.attributes).length, 5);
601-
assert(user.attributes.hasOwnProperty('username'));
602-
assert(user.attributes.hasOwnProperty('email'));
601+
assert(Object.hasOwn(user.attributes, 'username'));
602+
assert(Object.hasOwn(user.attributes, 'email'));
603603
return user.destroy();
604604
})
605605
.then(() => {

src/Cloud.ts

+13-30
Original file line numberDiff line numberDiff line change
@@ -26,30 +26,22 @@ import type { RequestOptions } from './RESTController';
2626
* @param {string} name The function name.
2727
* @param {object} data The parameters to send to the cloud function.
2828
* @param {object} options
29+
* Valid options are:<ul>
30+
* <li>useMasterKey: In Cloud Code and Node only, causes the Master Key to
31+
* be used for this request.
32+
* <li>sessionToken: A valid session token, used for making a request on
33+
* behalf of a specific user.
34+
* <li>installationId: the installationId which made the request
35+
* <li>context: A dictionary that is accessible in Cloud Code triggers.
36+
* </ul>
2937
* @returns {Promise} A promise that will be resolved with the result
3038
* of the function.
3139
*/
3240
export function run(name: string, data: any, options: RequestOptions): Promise<any> {
33-
options = options || {};
34-
3541
if (typeof name !== 'string' || name.length === 0) {
3642
throw new TypeError('Cloud function name must be a string.');
3743
}
38-
39-
const requestOptions: RequestOptions = {};
40-
if (options.hasOwnProperty('useMasterKey')) {
41-
requestOptions.useMasterKey = !!options.useMasterKey;
42-
}
43-
if (options.sessionToken) {
44-
requestOptions.sessionToken = options.sessionToken;
45-
}
46-
if (options.installationId) {
47-
requestOptions.installationId = options.installationId;
48-
}
49-
if (options.context && typeof options.context === 'object') {
50-
requestOptions.context = options.context;
51-
}
52-
44+
const requestOptions = ParseObject._getRequestOptions(options);
5345
return CoreManager.getCloudController().run(name, data, requestOptions);
5446
}
5547

@@ -62,10 +54,7 @@ export function run(name: string, data: any, options: RequestOptions): Promise<a
6254
* of the function.
6355
*/
6456
export function getJobsData(): Promise<any> {
65-
const requestOptions = {
66-
useMasterKey: true,
67-
};
68-
return CoreManager.getCloudController().getJobsData(requestOptions);
57+
return CoreManager.getCloudController().getJobsData({ useMasterKey: true });
6958
}
7059

7160
/**
@@ -82,10 +71,7 @@ export function startJob(name: string, data: any): Promise<string> {
8271
if (typeof name !== 'string' || name.length === 0) {
8372
throw new TypeError('Cloud job name must be a string.');
8473
}
85-
const requestOptions = {
86-
useMasterKey: true,
87-
};
88-
return CoreManager.getCloudController().startJob(name, data, requestOptions);
74+
return CoreManager.getCloudController().startJob(name, data, { useMasterKey: true });
8975
}
9076

9177
/**
@@ -104,17 +90,15 @@ export function getJobStatus(jobStatusId: string): Promise<ParseObject> {
10490
const DefaultController = {
10591
run(name: string, data: any, options: RequestOptions) {
10692
const RESTController = CoreManager.getRESTController();
107-
10893
const payload = encode(data, true);
10994

11095
const request = RESTController.request('POST', 'functions/' + name, payload, options);
111-
11296
return request.then(res => {
113-
if (typeof res === 'object' && Object.keys(res).length > 0 && !res.hasOwnProperty('result')) {
97+
if (typeof res === 'object' && Object.keys(res).length > 0 && !Object.hasOwn(res, 'result')) {
11498
throw new ParseError(ParseError.INVALID_JSON, 'The server returned an invalid response.');
11599
}
116100
const decoded = decode(res);
117-
if (decoded && decoded.hasOwnProperty('result')) {
101+
if (decoded && Object.hasOwn(decoded, 'result')) {
118102
return Promise.resolve(decoded.result);
119103
}
120104
return Promise.resolve(undefined);
@@ -123,7 +107,6 @@ const DefaultController = {
123107

124108
getJobsData(options: RequestOptions) {
125109
const RESTController = CoreManager.getRESTController();
126-
127110
return RESTController.request('GET', 'cloud_code/jobs/data', null, options);
128111
},
129112

src/CoreManager.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ function requireMethods(name: string, methods: Array<string>, controller: any) {
340340

341341
const CoreManager = {
342342
get: function (key: string): any {
343-
if (config.hasOwnProperty(key)) {
343+
if (Object.hasOwn(config, key)) {
344344
return config[key];
345345
}
346346
throw new Error('Configuration key not found: ' + key);
@@ -351,7 +351,7 @@ const CoreManager = {
351351
},
352352

353353
setIfNeeded: function (key: string, value: any): any {
354-
if (!config.hasOwnProperty(key)) {
354+
if (!Object.hasOwn(config, key)) {
355355
config[key] = value;
356356
}
357357
return config[key];

src/ParseFile.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ class ParseFile {
320320
throw new ParseError(ParseError.FILE_DELETE_UNNAMED_ERROR, 'Cannot delete an unnamed file.');
321321
}
322322
const destroyOptions = { useMasterKey: true };
323-
if (options.hasOwnProperty('useMasterKey')) {
323+
if (Object.hasOwn(options, 'useMasterKey')) {
324324
destroyOptions.useMasterKey = !!options.useMasterKey;
325325
}
326326
const controller = CoreManager.getFileController();

0 commit comments

Comments
 (0)