@shaivpidadi/trends-js 0.0.0-beta.6 → 0.0.0-beta.8

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.
Files changed (34) hide show
  1. package/README.md +183 -38
  2. package/dist/cjs/constants.d.ts +3 -0
  3. package/{lib → dist/cjs}/constants.js +19 -4
  4. package/{lib → dist/cjs}/helpers/googleTrendsAPI.d.ts +4 -1
  5. package/dist/cjs/helpers/googleTrendsAPI.js +339 -0
  6. package/{lib → dist/cjs}/helpers/request.d.ts +1 -1
  7. package/{lib → dist/cjs}/helpers/request.js +14 -18
  8. package/dist/cjs/index.d.ts +19 -0
  9. package/dist/cjs/index.js +24 -0
  10. package/dist/cjs/types/enums.d.ts +14 -0
  11. package/dist/cjs/types/enums.js +19 -0
  12. package/dist/esm/constants.d.ts +3 -0
  13. package/dist/esm/constants.js +50 -0
  14. package/dist/esm/errors/GoogleTrendsError.d.ts +23 -0
  15. package/dist/esm/errors/GoogleTrendsError.js +37 -0
  16. package/dist/esm/helpers/format.d.ts +2 -0
  17. package/dist/esm/helpers/format.js +76 -0
  18. package/dist/esm/helpers/googleTrendsAPI.d.ts +29 -0
  19. package/dist/esm/helpers/googleTrendsAPI.js +335 -0
  20. package/dist/esm/helpers/request.d.ts +9 -0
  21. package/dist/esm/helpers/request.js +75 -0
  22. package/dist/esm/index.d.ts +19 -0
  23. package/dist/esm/index.js +21 -0
  24. package/dist/esm/types/enums.d.ts +14 -0
  25. package/dist/esm/types/enums.js +16 -0
  26. package/package.json +29 -35
  27. package/lib/constants.d.ts +0 -2
  28. package/lib/helpers/googleTrendsAPI.js +0 -211
  29. package/lib/index.d.ts +0 -2
  30. package/lib/index.js +0 -7
  31. /package/{lib → dist/cjs}/errors/GoogleTrendsError.d.ts +0 -0
  32. /package/{lib → dist/cjs}/errors/GoogleTrendsError.js +0 -0
  33. /package/{lib → dist/cjs}/helpers/format.d.ts +0 -0
  34. /package/{lib → dist/cjs}/helpers/format.js +0 -0
package/package.json CHANGED
@@ -1,49 +1,43 @@
1
1
  {
2
2
  "name": "@shaivpidadi/trends-js",
3
- "version": "0.0.0-beta.6",
3
+ "version": "0.0.0-beta.8",
4
4
  "description": "Google Trends API for Node.js",
5
- "main": "lib/index.js",
6
- "types": "lib/index.d.ts",
5
+ "main": "./dist/cjs/index.js",
6
+ "module": "./dist/esm/index.js",
7
+ "types": "./dist/types/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "import": "./dist/esm/index.js",
11
+ "require": "./dist/cjs/index.js",
12
+ "types": "./dist/types/index.d.ts"
13
+ }
14
+ },
15
+ "files": [
16
+ "dist"
17
+ ],
7
18
  "scripts": {
8
- "build": "tsc",
9
- "format": "prettier --write \"src/**/*.(js|ts)\"",
10
- "lint": "eslint src --ext .js,.ts",
11
- "lint:fix": "eslint src --fix --ext .js,.ts",
12
- "test": "jest --config jest.config.js",
13
- "prepare": "npm run build",
14
- "prepublishOnly": "npm test && npm run lint",
15
- "preversion": "npm run lint",
16
- "version": "npm run format && git add -A src",
17
- "postversion": "git push && git push --tags"
19
+ "build": "npm run build:esm && npm run build:cjs",
20
+ "build:esm": "tsc -p tsconfig.json --outDir dist/esm",
21
+ "build:cjs": "tsc -p tsconfig.json --outDir dist/cjs --module commonjs",
22
+ "test": "jest",
23
+ "prepare": "npm run build"
18
24
  },
19
25
  "repository": {
20
26
  "type": "git",
21
27
  "url": "git+https://github.com/Shaivpidadi/trends-js.git"
22
28
  },
23
29
  "keywords": [
24
- "boilerplate",
25
- "typescript",
26
- "npm",
27
- "module"
30
+ "google-trends",
31
+ "api",
32
+ "typescript"
28
33
  ],
29
- "author": "Shaishav Pidadi",
30
- "private": false,
31
- "publishConfig": {
32
- "access": "public"
33
- },
34
+ "author": "",
34
35
  "license": "MIT",
35
36
  "devDependencies": {
36
- "@types/jest": "29.5.14",
37
- "@typescript-eslint/eslint-plugin": "5.62.0",
38
- "@typescript-eslint/parser": "5.62.0",
39
- "eslint": "8.57.1",
40
- "eslint-plugin-jest": "27.9.0",
41
- "jest": "29.7.0",
42
- "prettier": "2.8.8",
43
- "ts-jest": "29.2.5",
44
- "typescript": "4.9.5"
45
- },
46
- "files": [
47
- "lib/**/*"
48
- ]
37
+ "@types/jest": "^29.5.0",
38
+ "@types/node": "^20.0.0",
39
+ "jest": "^29.5.0",
40
+ "ts-jest": "^29.1.0",
41
+ "typescript": "^5.0.0"
42
+ }
49
43
  }
@@ -1,2 +0,0 @@
1
- import { GoogleTrendsEndpoints, GoogleTrendsMapper } from './types';
2
- export declare const GOOGLE_TRENDS_MAPPER: Record<GoogleTrendsEndpoints, GoogleTrendsMapper>;
@@ -1,211 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.GoogleTrendsApi = void 0;
13
- const request_1 = require("./request");
14
- const format_1 = require("./format");
15
- const constants_1 = require("../constants");
16
- const GoogleTrendsError_1 = require("../errors/GoogleTrendsError");
17
- class GoogleTrendsApi {
18
- /**
19
- * Get autocomplete suggestions for a keyword
20
- * @param keyword - The keyword to get suggestions for
21
- * @param hl - Language code (default: 'en-US')
22
- * @returns Promise with array of suggestion strings
23
- */
24
- autocomplete(keyword, hl = 'en-US') {
25
- return __awaiter(this, void 0, void 0, function* () {
26
- if (!keyword) {
27
- return { data: [] };
28
- }
29
- const options = Object.assign(Object.assign({}, constants_1.GOOGLE_TRENDS_MAPPER["autocomplete" /* GoogleTrendsEndpoints.autocomplete */]), { qs: {
30
- hl,
31
- tz: '240',
32
- } });
33
- try {
34
- const response = yield (0, request_1.request)(`${options.url}/${encodeURIComponent(keyword)}`, options);
35
- const text = yield response.text();
36
- // Remove the first 5 characters (JSONP wrapper) and parse
37
- const data = JSON.parse(text.slice(5));
38
- return { data: data.default.topics.map((topic) => topic.title) };
39
- }
40
- catch (error) {
41
- if (error instanceof Error) {
42
- return { error: new GoogleTrendsError_1.NetworkError(error.message) };
43
- }
44
- return { error: new GoogleTrendsError_1.UnknownError() };
45
- }
46
- });
47
- }
48
- /**
49
- * Get daily trending topics
50
- * @param options - Options for daily trends request
51
- * @returns Promise with trending topics data
52
- */
53
- dailyTrends({ geo = 'US', lang = 'en' }) {
54
- return __awaiter(this, void 0, void 0, function* () {
55
- const defaultOptions = constants_1.GOOGLE_TRENDS_MAPPER["dailyTrends" /* GoogleTrendsEndpoints.dailyTrends */];
56
- const options = Object.assign(Object.assign({}, defaultOptions), { body: new URLSearchParams({
57
- 'f.req': `[[["i0OFE","[null,null,\\"${geo}\\",0,\\"${lang}\\",24,1]",null,"generic"]]]`,
58
- }).toString(), contentType: 'form' });
59
- try {
60
- const response = yield (0, request_1.request)(options.url, options);
61
- const text = yield response.text();
62
- const trendingTopics = (0, format_1.extractJsonFromResponse)(text);
63
- if (!trendingTopics) {
64
- return { error: new GoogleTrendsError_1.ParseError() };
65
- }
66
- return { data: trendingTopics };
67
- }
68
- catch (error) {
69
- if (error instanceof Error) {
70
- return { error: new GoogleTrendsError_1.NetworkError(error.message) };
71
- }
72
- return { error: new GoogleTrendsError_1.UnknownError() };
73
- }
74
- });
75
- }
76
- /**
77
- * Get real-time trending topics
78
- * @param options - Options for real-time trends request
79
- * @returns Promise with trending topics data
80
- */
81
- realTimeTrends({ geo = 'US', trendingHours = 4 }) {
82
- return __awaiter(this, void 0, void 0, function* () {
83
- const defaultOptions = constants_1.GOOGLE_TRENDS_MAPPER["dailyTrends" /* GoogleTrendsEndpoints.dailyTrends */];
84
- const options = Object.assign(Object.assign({}, defaultOptions), { body: new URLSearchParams({
85
- 'f.req': `[[["i0OFE","[null,null,\\"${geo}\\",0,\\"en\\",${trendingHours},1]",null,"generic"]]]`,
86
- }).toString(), contentType: 'form' });
87
- try {
88
- const response = yield (0, request_1.request)(options.url, options);
89
- const text = yield response.text();
90
- const trendingTopics = (0, format_1.extractJsonFromResponse)(text);
91
- if (!trendingTopics) {
92
- return { error: new GoogleTrendsError_1.ParseError() };
93
- }
94
- return { data: trendingTopics };
95
- }
96
- catch (error) {
97
- if (error instanceof Error) {
98
- return { error: new GoogleTrendsError_1.NetworkError(error.message) };
99
- }
100
- return { error: new GoogleTrendsError_1.UnknownError() };
101
- }
102
- });
103
- }
104
- explore({ keyword, geo = 'US', time = 'now 1-d', category = 0, property = '', hl = 'en-US', }) {
105
- return __awaiter(this, void 0, void 0, function* () {
106
- const options = Object.assign(Object.assign({}, constants_1.GOOGLE_TRENDS_MAPPER["explore" /* GoogleTrendsEndpoints.explore */]), { qs: {
107
- hl,
108
- tz: '240',
109
- req: JSON.stringify({
110
- comparisonItem: [
111
- {
112
- keyword,
113
- geo,
114
- time,
115
- },
116
- ],
117
- category,
118
- property,
119
- }),
120
- }, contentType: 'form' });
121
- try {
122
- const response = yield (0, request_1.request)(options.url, options);
123
- const text = yield response.text();
124
- // Remove the first 5 characters (JSONP wrapper) and parse
125
- const data = JSON.parse(text.slice(5));
126
- return data;
127
- }
128
- catch (error) {
129
- console.error('Explore request failed:', error);
130
- return { widgets: [] };
131
- }
132
- });
133
- }
134
- //
135
- interestByRegion({ keyword, startTime = new Date('2004-01-01'), endTime = new Date(), geo = 'US', resolution = 'REGION', hl = 'en-US', timezone = new Date().getTimezoneOffset(), category = 0 }) {
136
- return __awaiter(this, void 0, void 0, function* () {
137
- const formatDate = (date) => {
138
- return date.toISOString().split('T')[0];
139
- };
140
- const formatTrendsDate = (date) => {
141
- const pad = (n) => n.toString().padStart(2, '0');
142
- const yyyy = date.getFullYear();
143
- const mm = pad(date.getMonth() + 1);
144
- const dd = pad(date.getDate());
145
- const hh = pad(date.getHours());
146
- const min = pad(date.getMinutes());
147
- const ss = pad(date.getSeconds());
148
- return `${yyyy}-${mm}-${dd}T${hh}\\:${min}\\:${ss}`;
149
- };
150
- const getDateRangeParam = (date) => {
151
- const yesterday = new Date(date);
152
- yesterday.setDate(date.getDate() - 1);
153
- const formattedStart = formatTrendsDate(yesterday);
154
- const formattedEnd = formatTrendsDate(date);
155
- return `${formattedStart} ${formattedEnd}`;
156
- };
157
- const exploreResponse = yield this.explore({
158
- keyword: Array.isArray(keyword) ? keyword[0] : keyword,
159
- geo: Array.isArray(geo) ? geo[0] : geo,
160
- time: `${getDateRangeParam(startTime)} ${getDateRangeParam(endTime)}`,
161
- category,
162
- hl
163
- });
164
- const widget = exploreResponse.widgets.find(w => w.id === 'GEO_MAP');
165
- if (!widget) {
166
- return { default: { geoMapData: [] } };
167
- }
168
- const options = Object.assign(Object.assign({}, constants_1.GOOGLE_TRENDS_MAPPER["interestByRegion" /* GoogleTrendsEndpoints.interestByRegion */]), { qs: {
169
- hl,
170
- tz: timezone.toString(),
171
- req: JSON.stringify({
172
- geo: {
173
- country: Array.isArray(geo) ? geo[0] : geo
174
- },
175
- comparisonItem: [{
176
- time: `${formatDate(startTime)} ${formatDate(endTime)}`,
177
- complexKeywordsRestriction: {
178
- keyword: [{
179
- type: 'BROAD',
180
- value: Array.isArray(keyword) ? keyword[0] : keyword
181
- }]
182
- }
183
- }],
184
- resolution,
185
- locale: hl,
186
- requestOptions: {
187
- property: '',
188
- backend: 'CM',
189
- category
190
- },
191
- userConfig: {
192
- userType: 'USER_TYPE_LEGIT_USER'
193
- }
194
- }),
195
- token: widget.token
196
- } });
197
- try {
198
- const response = yield (0, request_1.request)(options.url, options);
199
- const text = yield response.text();
200
- // Remove the first 5 characters (JSONP wrapper) and parse
201
- const data = JSON.parse(text.slice(5));
202
- return data;
203
- }
204
- catch (error) {
205
- return { default: { geoMapData: [] } };
206
- }
207
- });
208
- }
209
- }
210
- exports.GoogleTrendsApi = GoogleTrendsApi;
211
- exports.default = new GoogleTrendsApi();
package/lib/index.d.ts DELETED
@@ -1,2 +0,0 @@
1
- import GoogleTrendsApi from './helpers/googleTrendsAPI';
2
- export default GoogleTrendsApi;
package/lib/index.js DELETED
@@ -1,7 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- const googleTrendsAPI_1 = __importDefault(require("./helpers/googleTrendsAPI"));
7
- exports.default = googleTrendsAPI_1.default;
File without changes
File without changes
File without changes
File without changes