@gooin/garmin-connect 1.4.4 → 1.6.0

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 (38) hide show
  1. package/README.md +225 -35
  2. package/dist/common/CFClient.d.ts +22 -0
  3. package/dist/common/CFClient.js +137 -140
  4. package/dist/common/CFClient.js.map +1 -0
  5. package/dist/common/DateUtils.d.ts +1 -0
  6. package/dist/common/DateUtils.js +11 -10
  7. package/dist/common/DateUtils.js.map +1 -0
  8. package/dist/common/HttpClient.d.ts +34 -0
  9. package/dist/common/HttpClient.js +299 -0
  10. package/dist/common/HttpClient.js.map +1 -0
  11. package/dist/garmin/GarminConnect.d.ts +40 -0
  12. package/dist/garmin/GarminConnect.js +178 -438
  13. package/dist/garmin/GarminConnect.js.map +1 -0
  14. package/dist/garmin/UrlClass.d.ts +25 -0
  15. package/dist/garmin/UrlClass.js +64 -0
  16. package/dist/garmin/UrlClass.js.map +1 -0
  17. package/dist/garmin/Urls.d.ts +64 -0
  18. package/dist/garmin/Urls.js +104 -102
  19. package/dist/garmin/Urls.js.map +1 -0
  20. package/dist/garmin/types.d.ts +706 -0
  21. package/dist/garmin/types.js +17 -0
  22. package/dist/garmin/types.js.map +1 -0
  23. package/dist/garmin/workouts/Running.d.ts +16 -0
  24. package/dist/garmin/workouts/Running.js +47 -53
  25. package/dist/garmin/workouts/Running.js.map +1 -0
  26. package/dist/garmin/workouts/templates/RunningTemplate.d.ts +68 -0
  27. package/dist/garmin/workouts/templates/RunningTemplate.js +78 -48
  28. package/dist/garmin/workouts/templates/RunningTemplate.js.map +1 -0
  29. package/dist/index.d.ts +1 -0
  30. package/dist/index.js +9 -3
  31. package/dist/index.js.map +1 -0
  32. package/dist/utils.d.ts +3 -0
  33. package/dist/utils.js +46 -0
  34. package/dist/utils.js.map +1 -0
  35. package/examples/example.js +21 -6
  36. package/package.json +66 -51
  37. package/dist/common/Client.js +0 -189
  38. package/dist/garmin/workouts/index.js +0 -5
@@ -1,189 +0,0 @@
1
- const axios = require('axios');
2
- const qs = require('qs');
3
- const fs = require('fs');
4
- const stream = require('stream');
5
- const util = require('util');
6
- const path = require('path');
7
-
8
- const pipeline = util.promisify(stream.pipeline);
9
-
10
- class Client {
11
- constructor(headers) {
12
- this.axios = axios;
13
- this.queryString = qs;
14
- this.fs = fs;
15
- this.headers = headers || {};
16
- this.cookies = {};
17
- }
18
-
19
- setCookie(name, value) {
20
- this.cookies[name] = value;
21
- this.headers.Cookie = this.getCookieString();
22
- }
23
-
24
- parseCookies(response) {
25
- const setCookies = response && response.headers && response.headers['set-cookie'];
26
- if (setCookies) {
27
- setCookies.forEach((c) => {
28
- const [cookieValue] = c.split(';');
29
- const [name, value] = cookieValue.split('=');
30
- this.setCookie(name, value);
31
- });
32
- }
33
- return response;
34
- }
35
-
36
- getCookie(name) {
37
- return this.cookies[name];
38
- }
39
-
40
- getCookieString() {
41
- return Object.entries(this.cookies).map((e) => `${e[0]}=${e[1]}`).join('; ');
42
- }
43
-
44
- post(url, data, params) {
45
- return this.axios({
46
- method: 'POST',
47
- params,
48
- url,
49
- data: this.queryString.stringify(data),
50
- headers: {
51
- ...this.headers,
52
- 'Content-Type': 'application/x-www-form-urlencoded',
53
- },
54
- maxRedirects: 0,
55
- })
56
- .catch((r) => {
57
- const { response } = r || {};
58
- this.parseCookies(response);
59
- if (response.status === 302 || response.status === 301) {
60
- if (response.headers && response.headers.location) {
61
- return this.post(response.headers.location, data, params);
62
- }
63
- }
64
- return r;
65
- })
66
- .then((r) => this.parseCookies(r))
67
- .then((r) => r && r.data);
68
- }
69
-
70
- postJson(url, data, params, headers = {}) {
71
- return this.axios({
72
- method: 'POST',
73
- params,
74
- url,
75
- data: JSON.stringify(data, null, 4),
76
- headers: {
77
- ...this.headers,
78
- ...headers,
79
- 'Content-Type': 'application/json',
80
- },
81
- })
82
- .then((r) => this.parseCookies(r))
83
- .then((r) => r && r.data);
84
- }
85
-
86
- postBlob(url, formData, params, headers = {}) {
87
- return this.axios({
88
- method: 'POST',
89
- params,
90
- url,
91
- data: formData,
92
- headers: {
93
- ...this.headers,
94
- ...headers,
95
- ...formData.getHeaders(),
96
- },
97
- })
98
- .then((r) => this.parseCookies(r))
99
- .then((r) => r && r.data);
100
- }
101
-
102
- putJson(url, data, params) {
103
- return this.axios({
104
- method: 'PUT',
105
- params,
106
- url,
107
- data: JSON.stringify(data, null, 4),
108
- headers: {
109
- ...this.headers,
110
- 'Content-Type': 'application/json',
111
- },
112
- })
113
- .then((r) => this.parseCookies(r))
114
- .then((r) => r && r.data);
115
- }
116
-
117
- downloadBlob(downloadDir = '', url, data, params) {
118
- const queryData = this.queryString.stringify(data);
119
- const queryDataString = queryData ? `?${queryData}` : '';
120
- return this.axios({
121
- method: 'GET',
122
- params,
123
- responseType: 'stream',
124
- url: `${url}${queryDataString}`,
125
- headers: this.headers,
126
- maxRedirects: 0,
127
- })
128
- .catch((r) => {
129
- const { response } = r || {};
130
- const { status, headers } = response || {};
131
- const { location } = headers || {};
132
- this.parseCookies(response);
133
- if (status === 302 || status === 301) {
134
- if (headers && location) {
135
- return this.downloadBlob(location, data, params);
136
- }
137
- }
138
- return r;
139
- })
140
- .then((r) => this.parseCookies(r))
141
- .then(async (r) => {
142
- const { headers } = r || {};
143
- const { 'content-disposition': contentDisposition } = headers || {};
144
- const downloadDirNormalized = path.normalize(downloadDir);
145
- if (contentDisposition) {
146
- const defaultName = `garmin_connect_download_${Date.now()}`;
147
- const [, fileName = defaultName] = contentDisposition.match(/filename="(.+)"/);
148
- const filePath = path.resolve(downloadDir, fileName);
149
- await pipeline(r.data, this.fs.createWriteStream(filePath));
150
- return filePath;
151
- }
152
- throw new Error(`Could not download file ${url} to ${downloadDirNormalized}`);
153
- });
154
- }
155
-
156
- get(url, data, params) {
157
- const queryData = this.queryString.stringify(data);
158
- const queryDataString = queryData ? `?${queryData}` : '';
159
- return this.axios({
160
- method: 'GET',
161
- params,
162
- url: `${url}${queryDataString}`,
163
- headers: this.headers,
164
- maxRedirects: 0,
165
- })
166
- .catch((r) => {
167
- const { response } = r || {};
168
- const { status, headers } = response || {};
169
- const { location } = headers || {};
170
- this.parseCookies(response);
171
- if (status === 302 || status === 301) {
172
- if (headers && location) {
173
- return this.get(location, data, params);
174
- }
175
- }
176
- return r;
177
- })
178
- .then((r) => this.parseCookies(r))
179
- .then((r) => {
180
- if (typeof r === 'string') {
181
- return r;
182
- }
183
- return r && r.data;
184
- });
185
- }
186
- }
187
-
188
-
189
- module.exports = Client;
@@ -1,5 +0,0 @@
1
- const Running = require('./Running');
2
-
3
- module.exports = {
4
- Running,
5
- };