Skip to content

refactor: Return headers in RESTController requests #2033

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 1 commit into from
Oct 3, 2023
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 integration/test/ParseCloudTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ describe('Parse Cloud', () => {
it('run job', async () => {
const params = { startedBy: 'Monty Python' };
const jobStatusId = await Parse.Cloud.startJob('CloudJob1', params);
expect(jobStatusId).toBeDefined();
await waitForJobStatus(jobStatusId, 'succeeded');

const jobStatus = await Parse.Cloud.getJobStatus(jobStatusId);
Expand Down
1 change: 1 addition & 0 deletions integration/test/ParsePushTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ describe('Parse Push', () => {
where: { deviceType: { $eq: 'random' } },
};
const pushStatusId = await Parse.Push.send(payload, { useMasterKey: true });
expect(pushStatusId).toBeDefined();
const pushStatus = await Parse.Push.getPushStatus(pushStatusId, { useMasterKey: true });
expect(pushStatus.id).toBe(pushStatusId);
});
Expand Down
6 changes: 4 additions & 2 deletions src/Cloud.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,14 @@ const DefaultController = {
return RESTController.request('GET', 'cloud_code/jobs/data', null, options);
},

startJob(name, data, options: RequestOptions) {
async startJob(name, data, options: RequestOptions) {
const RESTController = CoreManager.getRESTController();

const payload = encode(data, true);
options.returnStatus = true;

return RESTController.request('POST', 'jobs/' + name, payload, options);
const response = await RESTController.request('POST', 'jobs/' + name, payload, options);
return response._headers?.['X-Parse-Job-Status-Id'];
},
};

Expand Down
4 changes: 4 additions & 0 deletions src/ParseObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -2455,6 +2455,8 @@ const DefaultController = {
const objectId = responses[index].success.objectId;
const status = responses[index]._status;
delete responses[index]._status;
delete responses[index]._headers;
delete responses[index]._xhr;
mapIdForPin[objectId] = obj._localId;
obj._handleSaveResponse(responses[index].success, status);
} else {
Expand Down Expand Up @@ -2523,6 +2525,8 @@ const DefaultController = {
response => {
const status = response._status;
delete response._status;
delete response._headers;
delete response._xhr;
targetCopy._handleSaveResponse(response, status);
},
error => {
Expand Down
6 changes: 4 additions & 2 deletions src/Push.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,10 @@ export function getPushStatus(pushStatusId: string, options?: FullOptions = {}):
}

const DefaultController = {
send(data: PushData, options?: FullOptions) {
return CoreManager.getRESTController().request('POST', 'push', data, options);
async send(data: PushData, options?: FullOptions) {
options.returnStatus = true;
const response = await CoreManager.getRESTController().request('POST', 'push', data, options);
return response._headers?.['X-Parse-Push-Status-Id'];
},
};

Expand Down
20 changes: 9 additions & 11 deletions src/RESTController.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,20 +110,18 @@ const RESTController = {
let response;
try {
response = JSON.parse(xhr.responseText);

if (typeof xhr.getResponseHeader === 'function') {
if ((xhr.getAllResponseHeaders() || '').includes('x-parse-job-status-id: ')) {
response = xhr.getResponseHeader('x-parse-job-status-id');
}
if ((xhr.getAllResponseHeaders() || '').includes('x-parse-push-status-id: ')) {
response = xhr.getResponseHeader('x-parse-push-status-id');
}
headers = {};
if (typeof xhr.getResponseHeader === 'function' && xhr.getResponseHeader('access-control-expose-headers')) {
const responseHeaders = xhr.getResponseHeader('access-control-expose-headers').split(', ');
responseHeaders.forEach(header => {
headers[header] = xhr.getResponseHeader(header.toLowerCase());
});
}
} catch (e) {
promise.reject(e.toString());
}
if (response) {
promise.resolve({ response, status: xhr.status, xhr });
promise.resolve({ response, headers, status: xhr.status, xhr });
}
} else if (xhr.status >= 500 || xhr.status === 0) {
// retry on 5XX or node-xmlhttprequest error
Expand Down Expand Up @@ -287,9 +285,9 @@ const RESTController = {

const payloadString = JSON.stringify(payload);
return RESTController.ajax(method, url, payloadString, {}, options).then(
({ response, status }) => {
({ response, status, headers, xhr }) => {
if (options.returnStatus) {
return { ...response, _status: status };
return { ...response, _status: status, _headers: headers, _xhr: xhr };
} else {
return response;
}
Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/Cloud-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ describe('CloudController', () => {
value: 12,
when: { __type: 'Date', iso: '2015-01-01T00:00:00.000Z' },
},
{ useMasterKey: true },
{ returnStatus: true, useMasterKey: true },
]);
});

Expand All @@ -242,7 +242,7 @@ describe('CloudController', () => {
{
value: 12,
},
{ useMasterKey: true },
{ returnStatus: true, useMasterKey: true },
]);
});

Expand Down
10 changes: 2 additions & 8 deletions src/__tests__/Push-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,7 @@ describe('Push', () => {
describe('PushController', () => {
it('forwards data along', () => {
CoreManager.setPushController(defaultController);
const request = jest.fn().mockReturnValue({
_thenRunCallbacks() {
return {
_thenRunCallbacks() {},
};
},
});
const request = jest.fn().mockReturnValue({ _headers: {} });
CoreManager.setRESTController({
request: request,
ajax: function () {},
Expand All @@ -111,7 +105,7 @@ describe('PushController', () => {
'POST',
'push',
{ push_time: '2015-02-01T00:00:00.000Z' },
{ useMasterKey: true },
{ returnStatus: true, useMasterKey: true },
]);
});
});
28 changes: 14 additions & 14 deletions src/__tests__/RESTController-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ CoreManager.set('APPLICATION_ID', 'A');
CoreManager.set('JAVASCRIPT_KEY', 'B');
CoreManager.set('VERSION', 'V');

const headers = {
'x-parse-job-status-id': '1234',
'x-parse-push-status-id': '5678',
'access-control-expose-headers': 'X-Parse-Job-Status-Id, X-Parse-Push-Status-Id',
};

describe('RESTController', () => {
it('throws if there is no XHR implementation', () => {
RESTController._setXHR(null);
Expand Down Expand Up @@ -212,45 +218,39 @@ describe('RESTController', () => {
XHR.prototype = {
open: function () {},
setRequestHeader: function () {},
getResponseHeader: function () {
return 1234;
getResponseHeader: function (header) {
return headers[header];
},
send: function () {
this.status = 200;
this.responseText = '{}';
this.readyState = 4;
this.onreadystatechange();
},
getAllResponseHeaders: function () {
return 'x-parse-job-status-id: 1234';
},
};
RESTController._setXHR(XHR);
const response = await RESTController.request('GET', 'classes/MyObject', {}, {});
expect(response).toBe(1234);
const response = await RESTController.request('GET', 'classes/MyObject', {}, { returnStatus: true });
expect(response._headers['X-Parse-Job-Status-Id']).toBe('1234');
});

it('handles x-parse-push-status-id header', async () => {
const XHR = function () {};
XHR.prototype = {
open: function () {},
setRequestHeader: function () {},
getResponseHeader: function () {
return 1234;
getResponseHeader: function (header) {
return headers[header];
},
send: function () {
this.status = 200;
this.responseText = '{}';
this.readyState = 4;
this.onreadystatechange();
},
getAllResponseHeaders: function () {
return 'x-parse-push-status-id: 1234';
},
};
RESTController._setXHR(XHR);
const response = await RESTController.request('POST', 'push', {}, {});
expect(response).toBe(1234);
const response = await RESTController.request('POST', 'push', {}, { returnStatus: true });
expect(response._headers['X-Parse-Push-Status-Id']).toBe('5678');
});

it('handles invalid header', async () => {
Expand Down