Skip to content

Commit 40ee727

Browse files
committed
fix: Replace existing HTTP request module
1 parent 81304be commit 40ee727

9 files changed

+102
-49
lines changed

DEPRECATIONS.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ The following is a list of deprecations, according to the [Deprecation Policy](h
77
| DEPPS1 | Native MongoDB syntax in aggregation pipeline | [#7338](https://github.com/parse-community/parse-server/issues/7338) | 5.0.0 (2022) | 6.0.0 (2023) | deprecated | - |
88
| DEPPS2 | Config option `directAccess` defaults to `true` | [#6636](https://github.com/parse-community/parse-server/pull/6636) | 5.0.0 (2022) | 6.0.0 (2023) | deprecated | - |
99
| DEPPS3 | Config option `enforcePrivateUsers` defaults to `true` | [#7319](https://github.com/parse-community/parse-server/pull/7319) | 5.0.0 (2022) | 6.0.0 (2023) | deprecated | - |
10-
| DEPPS4 | Remove convenience method for http request `Parse.Cloud.httpRequest` | [#7589](https://github.com/parse-community/parse-server/pull/7589) | 5.0.0 (2022) | 6.0.0 (2023) | deprecated | - |
1110
| DEPPS5 | Config option `allowClientClassCreation` defaults to `false` | [#7925](https://github.com/parse-community/parse-server/pull/7925) | 5.3.0 (2022) | 7.0.0 (2024) | deprecated | - |
1211
| DEPPS6 | Auth providers disabled by default | [#7953](https://github.com/parse-community/parse-server/pull/7953) | 5.3.0 (2022) | 7.0.0 (2024) | deprecated | - |
1312
| DEPPS7 | Remove file trigger syntax `Parse.Cloud.beforeSaveFile((request) => {})` | [#7966](https://github.com/parse-community/parse-server/pull/7966) | 5.3.0 (2022) | 7.0.0 (2024) | deprecated | - |

package-lock.json

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,13 @@
1919
],
2020
"license": "BSD-3-Clause",
2121
"dependencies": {
22-
"@graphql-yoga/node": "2.6.0",
23-
"@graphql-tools/utils": "8.12.0",
2422
"@graphql-tools/merge": "8.3.6",
2523
"@graphql-tools/schema": "9.0.4",
24+
"@graphql-tools/utils": "8.12.0",
25+
"@graphql-yoga/node": "2.6.0",
2626
"@parse/fs-files-adapter": "1.2.2",
2727
"@parse/push-adapter": "4.1.2",
28+
"axios": "1.1.3",
2829
"bcryptjs": "2.4.3",
2930
"body-parser": "1.20.1",
3031
"commander": "5.1.0",
@@ -34,8 +35,8 @@
3435
"follow-redirects": "1.15.2",
3536
"graphql": "16.6.0",
3637
"graphql-list-fields": "2.0.2",
37-
"graphql-tag": "2.12.6",
3838
"graphql-relay": "0.10.0",
39+
"graphql-tag": "2.12.6",
3940
"intersect": "1.0.1",
4041
"jsonwebtoken": "8.5.1",
4142
"jwks-rsa": "2.1.5",

spec/CloudCode.spec.js

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1686,25 +1686,6 @@ describe('Cloud Code', () => {
16861686
obj.save().then(done, done.fail);
16871687
});
16881688

1689-
it('can deprecate Parse.Cloud.httpRequest', async () => {
1690-
const logger = require('../lib/logger').logger;
1691-
spyOn(logger, 'warn').and.callFake(() => {});
1692-
Parse.Cloud.define('hello', () => {
1693-
return 'Hello world!';
1694-
});
1695-
await Parse.Cloud.httpRequest({
1696-
method: 'POST',
1697-
url: 'http://localhost:8378/1/functions/hello',
1698-
headers: {
1699-
'X-Parse-Application-Id': Parse.applicationId,
1700-
'X-Parse-REST-API-Key': 'rest',
1701-
},
1702-
});
1703-
expect(logger.warn).toHaveBeenCalledWith(
1704-
'DeprecationWarning: Parse.Cloud.httpRequest is deprecated and will be removed in a future version. Use a http request library instead.'
1705-
);
1706-
});
1707-
17081689
describe('cloud jobs', () => {
17091690
it('should define a job', done => {
17101691
expect(() => {

spec/HTTPRequest.spec.js

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -83,18 +83,17 @@ describe('httpRequest', () => {
8383
});
8484

8585
it('should fail on 404', async () => {
86-
await expectAsync(
87-
httpRequest({
86+
try {
87+
await httpRequest({
8888
url: `${httpRequestServer}/404`,
89-
})
90-
).toBeRejectedWith(
91-
jasmine.objectContaining({
92-
status: 404,
93-
buffer: Buffer.from('NO'),
94-
text: 'NO',
95-
data: undefined,
96-
})
97-
);
89+
});
90+
fail('should have failed');
91+
} catch (e) {
92+
expect(e.status).toBe(404);
93+
expect(e.buffer).toEqual(Buffer.from('NO'));
94+
expect(e.text).toBe('NO');
95+
expect(e.data).toBeUndefined();
96+
}
9897
});
9998

10099
it('should post on echo', async () => {
@@ -178,7 +177,6 @@ describe('httpRequest', () => {
178177
url: `${httpRequestServer}/qs`,
179178
params: 'foo=bar&foo2=bar2',
180179
});
181-
182180
expect(httpResponse.status).toBe(200);
183181
expect(httpResponse.data).toEqual({ foo: 'bar', foo2: 'bar2' });
184182
});

spec/PagesRouter.spec.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ describe('Pages Router', () => {
116116
followRedirects: false,
117117
}).catch(e => e);
118118
expect(res.status).toBe(200);
119-
expect(res.text).toEqual('"Password successfully reset"');
119+
expect(res.text).toEqual('Password successfully reset');
120120
});
121121

122122
it('request_password_reset: responds with AJAX error on missing password', async () => {
@@ -970,10 +970,12 @@ describe('Pages Router', () => {
970970
// Do not compose this URL with `new URL(...)` because that would normalize
971971
// the URL and remove path patterns; the path patterns must reach the router
972972
const url = `${config.publicServerURL}/apps/../.gitignore`;
973-
const response = await request({
974-
url: url,
975-
followRedirects: false,
976-
}).catch(e => e);
973+
const response = await request
974+
.legacy({
975+
url: url,
976+
followRedirects: false,
977+
})
978+
.catch(e => e);
977979
expect(response.status).toBe(404);
978980
expect(response.text).toBe('Not found.');
979981
});

spec/PurchaseValidation.spec.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ describe('test validate_receipt endpoint', () => {
5959
const otherResponse = await request({
6060
url: url,
6161
});
62+
console.log(otherResponse.data);
6263
expect(otherResponse.text).toBe('download_file');
6364
}
6465
});

src/cloud-code/Parse.Cloud.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -713,14 +713,8 @@ ParseCloud.useMasterKey = () => {
713713
);
714714
};
715715

716-
const request = require('./httpRequest');
717-
ParseCloud.httpRequest = opts => {
718-
Deprecator.logRuntimeDeprecation({
719-
usage: 'Parse.Cloud.httpRequest',
720-
solution: 'Use a http request library instead.',
721-
});
722-
return request(opts);
723-
};
716+
import axios from 'axios';
717+
ParseCloud.httpRequest = axios;
724718

725719
module.exports = ParseCloud;
726720

src/cloud-code/httpRequest.js

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,57 @@ const encodeBody = function ({ body, headers = {} }) {
8585
* @param {Parse.Cloud.HTTPOptions} options The Parse.Cloud.HTTPOptions object that makes the request.
8686
* @return {Promise<Parse.Cloud.HTTPResponse>} A promise that will be resolved with a {@link Parse.Cloud.HTTPResponse} object when the request completes.
8787
*/
88-
module.exports = function httpRequest(options) {
88+
import axios from 'axios';
89+
import { parse as qs } from 'querystring';
90+
module.exports = async options => {
91+
if (options.method) {
92+
options.method = options.method.toLowerCase();
93+
}
94+
if (options.body) {
95+
options.data = options.body;
96+
delete options.body;
97+
}
98+
if (typeof options.params === 'object') {
99+
options.qs = options.params;
100+
} else if (typeof options.params === 'string') {
101+
options.qs = qs(options.params);
102+
}
103+
if (options.qs) {
104+
options.params = options.qs;
105+
delete options.qs;
106+
}
107+
if (!options.followRedirects) {
108+
options.maxRedirects = 0;
109+
delete options.followRedirects;
110+
}
111+
try {
112+
const response = await axios(options);
113+
const data = response.data;
114+
if (Object.prototype.toString.call(data) === '[object Object]') {
115+
response.text = JSON.stringify(data);
116+
response.data = data;
117+
} else {
118+
response.text = data;
119+
}
120+
response.buffer = Buffer.from(response.text);
121+
return response;
122+
} catch (e) {
123+
e.status = e.response && e.response.status;
124+
const data = e.response && e.response.data;
125+
if (Object.prototype.toString.call(data) === '[object Object]') {
126+
e.text = JSON.stringify(data);
127+
e.data = data;
128+
} else {
129+
e.text = data;
130+
}
131+
e.buffer = Buffer.from(e.text);
132+
if (e.status === 301 || e.status === 302 || e.status === 303) {
133+
return e;
134+
}
135+
throw e;
136+
}
137+
};
138+
module.exports.legacy = function httpRequest(options) {
89139
let url;
90140
try {
91141
url = parse(options.url);

0 commit comments

Comments
 (0)