@shaivpidadi/trends-js 0.0.0-beta.4 → 0.0.0-beta.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -41,7 +41,7 @@ class GoogleTrendsApi {
41
41
  const defaultOptions = constants_1.GOOGLE_TRENDS_MAPPER["dailyTrends" /* GoogleTrendsEndpoints.dailyTrends */];
42
42
  const options = Object.assign(Object.assign({}, defaultOptions), { body: new URLSearchParams({
43
43
  'f.req': `[[["i0OFE","[null,null,\\"${geo}\\",0,\\"${lang}\\",24,1]",null,"generic"]]]`,
44
- }).toString() });
44
+ }).toString(), contentType: 'form' });
45
45
  try {
46
46
  const response = yield (0, request_1.request)(options.url, options);
47
47
  const text = yield response.text();
@@ -68,7 +68,7 @@ class GoogleTrendsApi {
68
68
  const defaultOptions = constants_1.GOOGLE_TRENDS_MAPPER["dailyTrends" /* GoogleTrendsEndpoints.dailyTrends */];
69
69
  const options = Object.assign(Object.assign({}, defaultOptions), { body: new URLSearchParams({
70
70
  'f.req': `[[["i0OFE","[null,null,\\"${geo}\\",0,\\"en\\",${trendingHours},1]",null,"generic"]]]`,
71
- }).toString() });
71
+ }).toString(), contentType: 'form' });
72
72
  try {
73
73
  const response = yield (0, request_1.request)(options.url, options);
74
74
  const text = yield response.text();
@@ -1,5 +1,9 @@
1
1
  export declare const request: (url: string, options: {
2
2
  method?: string;
3
3
  qs?: Record<string, any>;
4
- body?: string;
5
- }) => Promise<Response>;
4
+ body?: string | Record<string, any>;
5
+ headers?: Record<string, string>;
6
+ contentType?: 'json' | 'form';
7
+ }) => Promise<{
8
+ text: () => Promise<string>;
9
+ }>;
@@ -16,46 +16,53 @@ exports.request = void 0;
16
16
  const https_1 = __importDefault(require("https"));
17
17
  const querystring_1 = __importDefault(require("querystring"));
18
18
  let cookieVal;
19
- function rereq(options) {
19
+ function rereq(options, body) {
20
20
  return new Promise((resolve, reject) => {
21
21
  const req = https_1.default.request(options, (res) => {
22
22
  let chunk = '';
23
- res.on('data', (data) => {
24
- chunk += data;
25
- });
26
- res.on('end', () => {
27
- resolve(chunk.toString());
28
- });
29
- });
30
- req.on('error', (e) => {
31
- reject(e);
23
+ res.on('data', (data) => { chunk += data; });
24
+ res.on('end', () => resolve(chunk));
32
25
  });
26
+ req.on('error', reject);
27
+ if (body)
28
+ req.write(body);
33
29
  req.end();
34
30
  });
35
31
  }
36
32
  const request = (url, options) => __awaiter(void 0, void 0, void 0, function* () {
37
33
  const parsedUrl = new URL(url);
34
+ const method = options.method || 'POST';
35
+ // Prepare body
36
+ let bodyString = '';
37
+ const contentType = options.contentType || 'json';
38
+ if (typeof options.body === 'string') {
39
+ bodyString = options.body;
40
+ }
41
+ else if (contentType === 'form') {
42
+ bodyString = querystring_1.default.stringify(options.body || {});
43
+ }
44
+ else if (options.body) {
45
+ bodyString = JSON.stringify(options.body);
46
+ }
38
47
  const requestOptions = {
39
- host: parsedUrl.host,
40
- method: options.method || 'POST',
48
+ hostname: parsedUrl.hostname,
49
+ port: parsedUrl.port || 443,
41
50
  path: `${parsedUrl.pathname}${options.qs ? '?' + querystring_1.default.stringify(options.qs) : ''}`,
42
- headers: {}
51
+ method,
52
+ headers: Object.assign(Object.assign(Object.assign(Object.assign({}, (options.headers || {})), (contentType === 'form'
53
+ ? { 'Content-Type': 'application/x-www-form-urlencoded' }
54
+ : { 'Content-Type': 'application/json' })), (bodyString ? { 'Content-Length': Buffer.byteLength(bodyString).toString() } : {})), (cookieVal ? { cookie: cookieVal } : {}))
43
55
  };
44
- if (cookieVal) {
45
- requestOptions.headers = { 'cookie': cookieVal };
46
- }
47
56
  const response = yield new Promise((resolve, reject) => {
48
57
  const req = https_1.default.request(requestOptions, (res) => {
49
58
  let chunk = '';
50
- res.on('data', (data) => {
51
- chunk += data;
52
- });
59
+ res.on('data', (data) => { chunk += data; });
53
60
  res.on('end', () => __awaiter(void 0, void 0, void 0, function* () {
54
61
  if (res.statusCode === 429 && res.headers['set-cookie']) {
55
62
  cookieVal = res.headers['set-cookie'][0].split(';')[0];
56
- requestOptions.headers = { 'cookie': cookieVal };
63
+ requestOptions.headers['cookie'] = cookieVal;
57
64
  try {
58
- const retryResponse = yield rereq(requestOptions);
65
+ const retryResponse = yield rereq(requestOptions, bodyString);
59
66
  resolve(retryResponse);
60
67
  }
61
68
  catch (err) {
@@ -63,16 +70,13 @@ const request = (url, options) => __awaiter(void 0, void 0, void 0, function* ()
63
70
  }
64
71
  }
65
72
  else {
66
- resolve(chunk.toString());
73
+ resolve(chunk);
67
74
  }
68
75
  }));
69
76
  });
70
- if (options.body) {
71
- req.write(options.body);
72
- }
73
- req.on('error', (e) => {
74
- reject(e);
75
- });
77
+ req.on('error', reject);
78
+ if (bodyString)
79
+ req.write(bodyString);
76
80
  req.end();
77
81
  });
78
82
  return {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shaivpidadi/trends-js",
3
- "version": "0.0.0-beta.4",
3
+ "version": "0.0.0-beta.5",
4
4
  "description": "Google Trends API for Node.js",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",