Skip to content

feat: Add Parse.Query option useMaintenanceKey #2484

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 8 commits into from
Mar 10, 2025
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
1 change: 1 addition & 0 deletions src/CoreManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ const config: Config & { [key: string]: any } = {
VERSION: 'js' + require('../package.json').version,
APPLICATION_ID: null,
JAVASCRIPT_KEY: null,
MAINTENANCE_KEY: null,
MASTER_KEY: null,
USE_MASTER_KEY: false,
PERFORM_USER_REWRITE: true,
Expand Down
19 changes: 18 additions & 1 deletion src/Parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,15 @@ const Parse = {
Parse._initialize(applicationId, javaScriptKey);
},

_initialize(applicationId: string, javaScriptKey: string, masterKey?: string) {
_initialize(
applicationId: string,
javaScriptKey: string,
masterKey?: string,
maintenanceKey?: string
) {
CoreManager.set('APPLICATION_ID', applicationId);
CoreManager.set('JAVASCRIPT_KEY', javaScriptKey);
CoreManager.set('MAINTENANCE_KEY', maintenanceKey);
CoreManager.set('MASTER_KEY', masterKey);
CoreManager.set('USE_MASTER_KEY', false);
CoreManager.setIfNeeded('EventEmitter', EventEmitter);
Expand Down Expand Up @@ -198,6 +204,17 @@ const Parse = {
return CoreManager.get('MASTER_KEY');
},

/**
* @member {string} Parse.maintenanceKey
* @static
*/
set maintenanceKey(value) {
CoreManager.set('MAINTENANCE_KEY', value);
},
get maintenanceKey() {
return CoreManager.get('MAINTENANCE_KEY');
},

/**
* @member {string} Parse.serverURL
* @static
Expand Down
3 changes: 3 additions & 0 deletions src/ParseObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,9 @@ class ParseObject<T extends Attributes = Attributes> {
if (hasOwn(options, 'useMasterKey')) {
requestOptions.useMasterKey = !!options.useMasterKey;
}
if (hasOwn(options, 'useMaintenanceKey')) {
requestOptions.useMaintenanceKey = !!options.useMaintenanceKey;
}
if (hasOwn(options, 'sessionToken') && typeof options.sessionToken === 'string') {
requestOptions.sessionToken = options.sessionToken;
}
Expand Down
1 change: 1 addition & 0 deletions src/ParseQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import type { Pointer } from './ParseObject';
type BatchOptions = FullOptions & {
batchSize?: number;
useMasterKey?: boolean;
useMaintenanceKey?: boolean;
sessionToken?: string;
context?: { [key: string]: any };
json?: boolean;
Expand Down
7 changes: 6 additions & 1 deletion src/RESTController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import XhrWeapp from './Xhr.weapp';

export type RequestOptions = {
useMasterKey?: boolean;
useMaintenanceKey?: boolean;
sessionToken?: string;
installationId?: string;
returnStatus?: boolean;
Expand All @@ -23,6 +24,7 @@ export type FullOptions = {
success?: any;
error?: any;
useMasterKey?: boolean;
useMaintenanceKey?: boolean;
sessionToken?: string;
installationId?: string;
progress?: any;
Expand All @@ -36,6 +38,7 @@ type PayloadType = {
_JavaScriptKey?: string;
_ClientVersion: string;
_MasterKey?: string;
_MaintenanceKey?: string;
_RevocableSession?: string;
_InstallationId?: string;
_SessionToken?: string;
Expand Down Expand Up @@ -273,7 +276,9 @@ const RESTController = {
throw new Error('Cannot use the Master Key, it has not been provided.');
}
}

if (options.useMaintenanceKey) {
payload._MaintenanceKey = CoreManager.get('MAINTENANCE_KEY');
}
if (CoreManager.get('FORCE_REVOCABLE_SESSION')) {
payload._RevocableSession = '1';
}
Expand Down
7 changes: 6 additions & 1 deletion src/__tests__/Parse-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ describe('Parse module', () => {
expect(CoreManager.get('APPLICATION_ID')).toBe('A');
expect(CoreManager.get('JAVASCRIPT_KEY')).toBe('B');

Parse._initialize('A', 'B', 'C');
Parse._initialize('A', 'B', 'C', 'D');
expect(CoreManager.get('APPLICATION_ID')).toBe('A');
expect(CoreManager.get('JAVASCRIPT_KEY')).toBe('B');
expect(CoreManager.get('MASTER_KEY')).toBe('C');
expect(CoreManager.get('MAINTENANCE_KEY')).toBe('D');
});

it('enables master key use in the node build', () => {
Expand Down Expand Up @@ -55,6 +56,10 @@ describe('Parse module', () => {
expect(CoreManager.get('MASTER_KEY')).toBe('789');
expect(Parse.masterKey).toBe('789');

Parse.maintenanceKey = '000';
expect(CoreManager.get('MAINTENANCE_KEY')).toBe('000');
expect(Parse.maintenanceKey).toBe('000');

Parse.serverURL = 'http://example.com';
expect(CoreManager.get('SERVER_URL')).toBe('http://example.com');
expect(Parse.serverURL).toBe('http://example.com');
Expand Down
15 changes: 15 additions & 0 deletions src/__tests__/ParseQuery-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1263,6 +1263,7 @@ describe('ParseQuery', () => {
},
});
expect(options.useMasterKey).toEqual(true);
expect(options.useMaintenanceKey).toEqual(true);
expect(options.sessionToken).toEqual('1234');
return Promise.resolve({
results: [],
Expand All @@ -1274,6 +1275,7 @@ describe('ParseQuery', () => {
q.equalTo('size', 'small')
.first({
useMasterKey: true,
useMaintenanceKey: true,
sessionToken: '1234',
})
.then(obj => {
Expand Down Expand Up @@ -1440,6 +1442,7 @@ describe('ParseQuery', () => {
},
});
expect(options.useMasterKey).toEqual(true);
expect(options.useMaintenanceKey).toEqual(true);
expect(options.sessionToken).toEqual('1234');
expect(options.context).toEqual(context);
return Promise.resolve({
Expand All @@ -1451,6 +1454,7 @@ describe('ParseQuery', () => {
const q = new ParseQuery('Item');
q.get('I27', {
useMasterKey: true,
useMaintenanceKey: true,
sessionToken: '1234',
context: context,
}).then(() => {
Expand Down Expand Up @@ -1587,6 +1591,7 @@ describe('ParseQuery', () => {
},
});
expect(options.useMasterKey).toEqual(true);
expect(options.useMaintenanceKey).toEqual(true);
expect(options.sessionToken).toEqual('1234');
expect(options.context).toEqual(context);
return Promise.resolve({
Expand All @@ -1599,6 +1604,7 @@ describe('ParseQuery', () => {
q.containedIn('size', ['small', 'medium'])
.find({
useMasterKey: true,
useMaintenanceKey: true,
sessionToken: '1234',
context: context,
})
Expand Down Expand Up @@ -1713,6 +1719,7 @@ describe('ParseQuery', () => {
it('passes options through to the REST API', async () => {
const batchOptions = {
useMasterKey: true,
useMaintenanceKey: true,
sessionToken: '1234',
batchSize: 50,
};
Expand All @@ -1727,6 +1734,7 @@ describe('ParseQuery', () => {
where: {},
});
expect(options.useMasterKey).toBe(true);
expect(options.useMaintenanceKey).toEqual(true);
expect(options.sessionToken).toEqual('1234');
});

Expand Down Expand Up @@ -1840,6 +1848,7 @@ describe('ParseQuery', () => {
it('passes options through to the REST API', async () => {
const batchOptions = {
useMasterKey: true,
useMaintenanceKey: true,
sessionToken: '1234',
batchSize: 50,
json: true,
Expand All @@ -1855,6 +1864,7 @@ describe('ParseQuery', () => {
where: {},
});
expect(options.useMasterKey).toBe(true);
expect(options.useMaintenanceKey).toEqual(true);
expect(options.sessionToken).toEqual('1234');
expect(options.json).toEqual(true);
});
Expand Down Expand Up @@ -1962,6 +1972,7 @@ describe('ParseQuery', () => {
},
});
expect(options.useMasterKey).toEqual(true);
expect(options.useMaintenanceKey).toEqual(true);
expect(options.sessionToken).toEqual('1234');
expect(options.context).toEqual(context);
return Promise.resolve({
Expand All @@ -1986,6 +1997,7 @@ describe('ParseQuery', () => {
},
{
useMasterKey: true,
useMaintenanceKey: true,
sessionToken: '1234',
context: context,
}
Expand Down Expand Up @@ -2014,6 +2026,7 @@ describe('ParseQuery', () => {
hint: '_id_',
});
expect(options.useMasterKey).toEqual(true);
expect(options.useMaintenanceKey).toEqual(true);
expect(options.sessionToken).toEqual('1234');
expect(options.context).toEqual(context);
return Promise.resolve({
Expand All @@ -2039,6 +2052,7 @@ describe('ParseQuery', () => {
},
{
useMasterKey: true,
useMaintenanceKey: true,
sessionToken: '1234',
context: context,
}
Expand Down Expand Up @@ -2761,6 +2775,7 @@ describe('ParseQuery', () => {
group: { objectId: '$name' },
});
expect(options.useMasterKey).toEqual(true);
expect(options.useMaintenanceKey).toEqual(true);
expect(options.requestTask).toBeDefined();
return Promise.resolve({
results: [],
Expand Down
22 changes: 22 additions & 0 deletions src/__tests__/RESTController-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,28 @@ describe('RESTController', () => {
});
});

it('sends the maintenance key when requested', async () => {
CoreManager.set('MAINTENANCE_KEY', 'MK');
const xhr = {
setRequestHeader: jest.fn(),
open: jest.fn(),
send: jest.fn(),
};
RESTController._setXHR(function () {
return xhr;
});
RESTController.request('GET', 'classes/MyObject', {}, { useMaintenanceKey: true });
await flushPromises();
expect(JSON.parse(xhr.send.mock.calls[0][0])).toEqual({
_method: 'GET',
_ApplicationId: 'A',
_JavaScriptKey: 'B',
_MaintenanceKey: 'MK',
_ClientVersion: 'V',
_InstallationId: 'iid',
});
});

it('includes the status code when requested', done => {
RESTController._setXHR(mockXHR([{ status: 200, response: { success: true } }]));
RESTController.request('POST', 'users', {}, { returnStatus: true }).then(response => {
Expand Down
17 changes: 14 additions & 3 deletions types/Parse.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,15 +223,21 @@ declare const Parse: {
purge: (className: string) => Promise<any>;
get: (className: string, options?: import("./RESTController").RequestOptions) => Promise<any>;
delete: (className: string, options?: import("./RESTController").RequestOptions) => Promise<void>;
create: (className: string, params: any, options?: import("./RESTController").RequestOptions) => Promise<any>;
create: (className: string, params: any, options? /**
* @member {string} Parse.maintenanceKey
* @static
*/: import("./RESTController").RequestOptions) => Promise<any>;
update: (className: string, params: any, options?: import("./RESTController").RequestOptions) => Promise<any>;
send(className: string, method: string, params: any, options?: import("./RESTController").RequestOptions): Promise<any>;
}): void;
getSchemaController(): {
purge: (className: string) => Promise<any>;
get: (className: string, options?: import("./RESTController").RequestOptions) => Promise<any>;
delete: (className: string, options?: import("./RESTController").RequestOptions) => Promise<void>;
create: (className: string, params: any, options?: import("./RESTController").RequestOptions) => Promise<any>;
create: (className: string, params: any, options? /**
* @member {string} Parse.maintenanceKey
* @static
*/: import("./RESTController").RequestOptions) => Promise<any>;
update: (className: string, params: any, options?: import("./RESTController").RequestOptions) => Promise<any>;
send(className: string, method: string, params: any, options?: import("./RESTController").RequestOptions): Promise<any>;
};
Expand Down Expand Up @@ -487,7 +493,7 @@ declare const Parse: {
* @static
*/
initialize(applicationId: string, javaScriptKey: string): void;
_initialize(applicationId: string, javaScriptKey: string, masterKey?: string): void;
_initialize(applicationId: string, javaScriptKey: string, masterKey?: string, maintenanceKey?: string): void;
/**
* Call this method to set your AsyncStorage engine
* Starting [email protected], the ParseSDK do not provide a React AsyncStorage as the ReactNative module
Expand Down Expand Up @@ -527,6 +533,11 @@ declare const Parse: {
* @static
*/
masterKey: any;
/**
* @member {string} Parse.maintenanceKey
* @static
*/
maintenanceKey: any;
/**
* @member {string} Parse.serverURL
* @static
Expand Down
1 change: 1 addition & 0 deletions types/ParseQuery.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type { Pointer } from './ParseObject';
type BatchOptions = FullOptions & {
batchSize?: number;
useMasterKey?: boolean;
useMaintenanceKey?: boolean;
sessionToken?: string;
context?: {
[key: string]: any;
Expand Down
2 changes: 2 additions & 0 deletions types/RESTController.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export type RequestOptions = {
useMasterKey?: boolean;
useMaintenanceKey?: boolean;
sessionToken?: string;
installationId?: string;
returnStatus?: boolean;
Expand All @@ -15,6 +16,7 @@ export type FullOptions = {
success?: any;
error?: any;
useMasterKey?: boolean;
useMaintenanceKey?: boolean;
sessionToken?: string;
installationId?: string;
progress?: any;
Expand Down