Skip to content

Commit 5fc7b58

Browse files
Copilotmtrezza
andcommitted
feat: Add custom header support to Express-style cloud functions via res.header()
Co-authored-by: mtrezza <[email protected]>
1 parent 432a27f commit 5fc7b58

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

spec/CloudCode.spec.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4946,5 +4946,73 @@ describe('beforePasswordResetRequest hook', () => {
49464946

49474947
await Parse.User.logOut();
49484948
});
4949+
4950+
it('should support setting custom headers with res.header()', async () => {
4951+
Parse.Cloud.define('customHeaderFunction', (req, res) => {
4952+
res.header('X-Custom-Header', 'custom-value').success({ message: 'OK' });
4953+
});
4954+
4955+
const response = await request({
4956+
method: 'POST',
4957+
url: 'http://localhost:8378/1/functions/customHeaderFunction',
4958+
headers: {
4959+
'X-Parse-Application-Id': 'test',
4960+
'X-Parse-REST-API-Key': 'rest',
4961+
},
4962+
json: true,
4963+
body: {},
4964+
});
4965+
4966+
expect(response.status).toBe(200);
4967+
expect(response.headers['x-custom-header']).toBe('custom-value');
4968+
expect(response.data.result.message).toBe('OK');
4969+
});
4970+
4971+
it('should support setting multiple custom headers', async () => {
4972+
Parse.Cloud.define('multipleHeadersFunction', (req, res) => {
4973+
res.header('X-Header-One', 'value1')
4974+
.header('X-Header-Two', 'value2')
4975+
.success({ message: 'Multiple headers' });
4976+
});
4977+
4978+
const response = await request({
4979+
method: 'POST',
4980+
url: 'http://localhost:8378/1/functions/multipleHeadersFunction',
4981+
headers: {
4982+
'X-Parse-Application-Id': 'test',
4983+
'X-Parse-REST-API-Key': 'rest',
4984+
},
4985+
json: true,
4986+
body: {},
4987+
});
4988+
4989+
expect(response.status).toBe(200);
4990+
expect(response.headers['x-header-one']).toBe('value1');
4991+
expect(response.headers['x-header-two']).toBe('value2');
4992+
expect(response.data.result.message).toBe('Multiple headers');
4993+
});
4994+
4995+
it('should support combining status code and custom headers', async () => {
4996+
Parse.Cloud.define('statusAndHeaderFunction', (req, res) => {
4997+
res.status(201)
4998+
.header('X-Resource-Id', '12345')
4999+
.success({ created: true });
5000+
});
5001+
5002+
const response = await request({
5003+
method: 'POST',
5004+
url: 'http://localhost:8378/1/functions/statusAndHeaderFunction',
5005+
headers: {
5006+
'X-Parse-Application-Id': 'test',
5007+
'X-Parse-REST-API-Key': 'rest',
5008+
},
5009+
json: true,
5010+
body: {},
5011+
});
5012+
5013+
expect(response.status).toBe(201);
5014+
expect(response.headers['x-resource-id']).toBe('12345');
5015+
expect(response.data.result.created).toBe(true);
5016+
});
49495017
});
49505018
});

src/Routers/FunctionsRouter.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ export class FunctionsRouter extends PromiseRouter {
105105

106106
static createResponseObject(resolve, reject, statusCode = null) {
107107
let httpStatusCode = statusCode;
108+
let customHeaders = {};
108109
let responseSent = false;
109110
const responseObject = {
110111
success: function (result) {
@@ -120,6 +121,9 @@ export class FunctionsRouter extends PromiseRouter {
120121
if (httpStatusCode !== null) {
121122
response.status = httpStatusCode;
122123
}
124+
if (Object.keys(customHeaders).length > 0) {
125+
response.headers = customHeaders;
126+
}
123127
resolve(response);
124128
},
125129
error: function (message) {
@@ -138,6 +142,10 @@ export class FunctionsRouter extends PromiseRouter {
138142
httpStatusCode = code;
139143
return responseObject;
140144
},
145+
header: function (key, value) {
146+
customHeaders[key] = value;
147+
return responseObject;
148+
},
141149
_isResponseSent: () => responseSent,
142150
};
143151
return responseObject;

src/cloud-code/Parse.Cloud.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,10 @@ var ParseCloud = {};
137137
* }
138138
* });
139139
*
140+
* Parse.Cloud.define('withCustomHeaders', (request, response) => {
141+
* response.header('X-Custom-Header', 'value').success({ data: 'OK' });
142+
* });
143+
*
140144
* Parse.Cloud.define('errorFunction', (request, response) => {
141145
* response.error('Something went wrong');
142146
* });
@@ -824,6 +828,7 @@ module.exports = ParseCloud;
824828
* @property {function} success Call this function to return a successful response with an optional result. Usage: `response.success(result)`
825829
* @property {function} error Call this function to return an error response with an error message. Usage: `response.error(message)`
826830
* @property {function} status Call this function to set a custom HTTP status code for the response. Returns the response object for chaining. Usage: `response.status(code).success(result)` or `response.status(code).error(message)`
831+
* @property {function} header Call this function to set a custom HTTP header for the response. Returns the response object for chaining. Usage: `response.header('X-Custom-Header', 'value').success(result)`
827832
*/
828833

829834
/**

0 commit comments

Comments
 (0)