Skip to content

Commit 74ff091

Browse files
authored
refactor: Return headers in RESTController requests (#2033)
1 parent e0b713e commit 74ff091

File tree

9 files changed

+41
-39
lines changed

9 files changed

+41
-39
lines changed

integration/test/ParseCloudTest.js

+1
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ describe('Parse Cloud', () => {
9696
it('run job', async () => {
9797
const params = { startedBy: 'Monty Python' };
9898
const jobStatusId = await Parse.Cloud.startJob('CloudJob1', params);
99+
expect(jobStatusId).toBeDefined();
99100
await waitForJobStatus(jobStatusId, 'succeeded');
100101

101102
const jobStatus = await Parse.Cloud.getJobStatus(jobStatusId);

integration/test/ParsePushTest.js

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ describe('Parse Push', () => {
99
where: { deviceType: { $eq: 'random' } },
1010
};
1111
const pushStatusId = await Parse.Push.send(payload, { useMasterKey: true });
12+
expect(pushStatusId).toBeDefined();
1213
const pushStatus = await Parse.Push.getPushStatus(pushStatusId, { useMasterKey: true });
1314
expect(pushStatus.id).toBe(pushStatusId);
1415
});

src/Cloud.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,14 @@ const DefaultController = {
131131
return RESTController.request('GET', 'cloud_code/jobs/data', null, options);
132132
},
133133

134-
startJob(name, data, options: RequestOptions) {
134+
async startJob(name, data, options: RequestOptions) {
135135
const RESTController = CoreManager.getRESTController();
136136

137137
const payload = encode(data, true);
138+
options.returnStatus = true;
138139

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

src/ParseObject.js

+4
Original file line numberDiff line numberDiff line change
@@ -2455,6 +2455,8 @@ const DefaultController = {
24552455
const objectId = responses[index].success.objectId;
24562456
const status = responses[index]._status;
24572457
delete responses[index]._status;
2458+
delete responses[index]._headers;
2459+
delete responses[index]._xhr;
24582460
mapIdForPin[objectId] = obj._localId;
24592461
obj._handleSaveResponse(responses[index].success, status);
24602462
} else {
@@ -2523,6 +2525,8 @@ const DefaultController = {
25232525
response => {
25242526
const status = response._status;
25252527
delete response._status;
2528+
delete response._headers;
2529+
delete response._xhr;
25262530
targetCopy._handleSaveResponse(response, status);
25272531
},
25282532
error => {

src/Push.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,10 @@ export function getPushStatus(pushStatusId: string, options?: FullOptions = {}):
9999
}
100100

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

src/RESTController.js

+9-11
Original file line numberDiff line numberDiff line change
@@ -110,20 +110,18 @@ const RESTController = {
110110
let response;
111111
try {
112112
response = JSON.parse(xhr.responseText);
113-
114-
if (typeof xhr.getResponseHeader === 'function') {
115-
if ((xhr.getAllResponseHeaders() || '').includes('x-parse-job-status-id: ')) {
116-
response = xhr.getResponseHeader('x-parse-job-status-id');
117-
}
118-
if ((xhr.getAllResponseHeaders() || '').includes('x-parse-push-status-id: ')) {
119-
response = xhr.getResponseHeader('x-parse-push-status-id');
120-
}
113+
headers = {};
114+
if (typeof xhr.getResponseHeader === 'function' && xhr.getResponseHeader('access-control-expose-headers')) {
115+
const responseHeaders = xhr.getResponseHeader('access-control-expose-headers').split(', ');
116+
responseHeaders.forEach(header => {
117+
headers[header] = xhr.getResponseHeader(header.toLowerCase());
118+
});
121119
}
122120
} catch (e) {
123121
promise.reject(e.toString());
124122
}
125123
if (response) {
126-
promise.resolve({ response, status: xhr.status, xhr });
124+
promise.resolve({ response, headers, status: xhr.status, xhr });
127125
}
128126
} else if (xhr.status >= 500 || xhr.status === 0) {
129127
// retry on 5XX or node-xmlhttprequest error
@@ -287,9 +285,9 @@ const RESTController = {
287285

288286
const payloadString = JSON.stringify(payload);
289287
return RESTController.ajax(method, url, payloadString, {}, options).then(
290-
({ response, status }) => {
288+
({ response, status, headers, xhr }) => {
291289
if (options.returnStatus) {
292-
return { ...response, _status: status };
290+
return { ...response, _status: status, _headers: headers, _xhr: xhr };
293291
} else {
294292
return response;
295293
}

src/__tests__/Cloud-test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ describe('CloudController', () => {
229229
value: 12,
230230
when: { __type: 'Date', iso: '2015-01-01T00:00:00.000Z' },
231231
},
232-
{ useMasterKey: true },
232+
{ returnStatus: true, useMasterKey: true },
233233
]);
234234
});
235235

@@ -242,7 +242,7 @@ describe('CloudController', () => {
242242
{
243243
value: 12,
244244
},
245-
{ useMasterKey: true },
245+
{ returnStatus: true, useMasterKey: true },
246246
]);
247247
});
248248

src/__tests__/Push-test.js

+2-8
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,7 @@ describe('Push', () => {
8787
describe('PushController', () => {
8888
it('forwards data along', () => {
8989
CoreManager.setPushController(defaultController);
90-
const request = jest.fn().mockReturnValue({
91-
_thenRunCallbacks() {
92-
return {
93-
_thenRunCallbacks() {},
94-
};
95-
},
96-
});
90+
const request = jest.fn().mockReturnValue({ _headers: {} });
9791
CoreManager.setRESTController({
9892
request: request,
9993
ajax: function () {},
@@ -111,7 +105,7 @@ describe('PushController', () => {
111105
'POST',
112106
'push',
113107
{ push_time: '2015-02-01T00:00:00.000Z' },
114-
{ useMasterKey: true },
108+
{ returnStatus: true, useMasterKey: true },
115109
]);
116110
});
117111
});

src/__tests__/RESTController-test.js

+14-14
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ CoreManager.set('APPLICATION_ID', 'A');
2222
CoreManager.set('JAVASCRIPT_KEY', 'B');
2323
CoreManager.set('VERSION', 'V');
2424

25+
const headers = {
26+
'x-parse-job-status-id': '1234',
27+
'x-parse-push-status-id': '5678',
28+
'access-control-expose-headers': 'X-Parse-Job-Status-Id, X-Parse-Push-Status-Id',
29+
};
30+
2531
describe('RESTController', () => {
2632
it('throws if there is no XHR implementation', () => {
2733
RESTController._setXHR(null);
@@ -212,45 +218,39 @@ describe('RESTController', () => {
212218
XHR.prototype = {
213219
open: function () {},
214220
setRequestHeader: function () {},
215-
getResponseHeader: function () {
216-
return 1234;
221+
getResponseHeader: function (header) {
222+
return headers[header];
217223
},
218224
send: function () {
219225
this.status = 200;
220226
this.responseText = '{}';
221227
this.readyState = 4;
222228
this.onreadystatechange();
223229
},
224-
getAllResponseHeaders: function () {
225-
return 'x-parse-job-status-id: 1234';
226-
},
227230
};
228231
RESTController._setXHR(XHR);
229-
const response = await RESTController.request('GET', 'classes/MyObject', {}, {});
230-
expect(response).toBe(1234);
232+
const response = await RESTController.request('GET', 'classes/MyObject', {}, { returnStatus: true });
233+
expect(response._headers['X-Parse-Job-Status-Id']).toBe('1234');
231234
});
232235

233236
it('handles x-parse-push-status-id header', async () => {
234237
const XHR = function () {};
235238
XHR.prototype = {
236239
open: function () {},
237240
setRequestHeader: function () {},
238-
getResponseHeader: function () {
239-
return 1234;
241+
getResponseHeader: function (header) {
242+
return headers[header];
240243
},
241244
send: function () {
242245
this.status = 200;
243246
this.responseText = '{}';
244247
this.readyState = 4;
245248
this.onreadystatechange();
246249
},
247-
getAllResponseHeaders: function () {
248-
return 'x-parse-push-status-id: 1234';
249-
},
250250
};
251251
RESTController._setXHR(XHR);
252-
const response = await RESTController.request('POST', 'push', {}, {});
253-
expect(response).toBe(1234);
252+
const response = await RESTController.request('POST', 'push', {}, { returnStatus: true });
253+
expect(response._headers['X-Parse-Push-Status-Id']).toBe('5678');
254254
});
255255

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

0 commit comments

Comments
 (0)