emissions-api-sdk 1.0.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 (120) hide show
  1. package/CODE_OF_CONDUCT.md +76 -0
  2. package/CONTRIBUTING.md +68 -0
  3. package/LICENSE +201 -0
  4. package/MAINTAINERS.md +10 -0
  5. package/README.md +89 -0
  6. package/dist/Client.js +227 -0
  7. package/dist/Constants.js +19 -0
  8. package/dist/api/Calculation.js +41 -0
  9. package/dist/api/Factor.js +79 -0
  10. package/dist/api/FactorSets.js +24 -0
  11. package/dist/api/Factors.js +98 -0
  12. package/dist/api/Fugitive.js +40 -0
  13. package/dist/api/FugitiveEmission.js +40 -0
  14. package/dist/api/GenericCalculation.js +41 -0
  15. package/dist/api/Location.js +41 -0
  16. package/dist/api/LocationEmission.js +41 -0
  17. package/dist/api/Mobile.js +41 -0
  18. package/dist/api/MobileEmission.js +41 -0
  19. package/dist/api/Stationary.js +41 -0
  20. package/dist/api/StationaryEmission.js +41 -0
  21. package/dist/api/TransportationAndDistribution.js +40 -0
  22. package/dist/api/TransportationDistributionEmission.js +40 -0
  23. package/dist/coverage/clover.xml +225 -0
  24. package/dist/coverage/coverage-final.json +19 -0
  25. package/dist/coverage/lcov-report/base.css +224 -0
  26. package/dist/coverage/lcov-report/block-navigation.js +87 -0
  27. package/dist/coverage/lcov-report/favicon.png +0 -0
  28. package/dist/coverage/lcov-report/index.html +161 -0
  29. package/dist/coverage/lcov-report/prettify.css +1 -0
  30. package/dist/coverage/lcov-report/prettify.js +2 -0
  31. package/dist/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  32. package/dist/coverage/lcov-report/sorter.js +210 -0
  33. package/dist/coverage/lcov-report/src/Client.ts.html +838 -0
  34. package/dist/coverage/lcov-report/src/Constants.ts.html +130 -0
  35. package/dist/coverage/lcov-report/src/api/Calculation.ts.html +217 -0
  36. package/dist/coverage/lcov-report/src/api/Factor.ts.html +343 -0
  37. package/dist/coverage/lcov-report/src/api/FactorSets.ts.html +154 -0
  38. package/dist/coverage/lcov-report/src/api/Factors.ts.html +403 -0
  39. package/dist/coverage/lcov-report/src/api/Fugitive.ts.html +214 -0
  40. package/dist/coverage/lcov-report/src/api/FugitiveEmission.ts.html +214 -0
  41. package/dist/coverage/lcov-report/src/api/GenericCalculation.ts.html +217 -0
  42. package/dist/coverage/lcov-report/src/api/Location.ts.html +214 -0
  43. package/dist/coverage/lcov-report/src/api/LocationEmission.ts.html +214 -0
  44. package/dist/coverage/lcov-report/src/api/Mobile.ts.html +220 -0
  45. package/dist/coverage/lcov-report/src/api/MobileEmission.ts.html +220 -0
  46. package/dist/coverage/lcov-report/src/api/Stationary.ts.html +220 -0
  47. package/dist/coverage/lcov-report/src/api/StationaryEmission.ts.html +220 -0
  48. package/dist/coverage/lcov-report/src/api/TransportationAndDistribution.ts.html +211 -0
  49. package/dist/coverage/lcov-report/src/api/TransportationDistributionEmission.ts.html +211 -0
  50. package/dist/coverage/lcov-report/src/api/index.html +221 -0
  51. package/dist/coverage/lcov-report/src/index.html +161 -0
  52. package/dist/coverage/lcov-report/src/request.ts.html +235 -0
  53. package/dist/coverage/lcov-report/src/utils.ts.html +139 -0
  54. package/dist/coverage/lcov-report/test/index.html +116 -0
  55. package/dist/coverage/lcov-report/test/mocks/CommonRequest.ts.html +142 -0
  56. package/dist/coverage/lcov-report/test/mocks/FactorRequest.ts.html +133 -0
  57. package/dist/coverage/lcov-report/test/mocks/GenericCalculationRequest.ts.html +142 -0
  58. package/dist/coverage/lcov-report/test/mocks/LocationRequest.ts.html +139 -0
  59. package/dist/coverage/lcov-report/test/mocks/SearchRequest.ts.html +142 -0
  60. package/dist/coverage/lcov-report/test/mocks/index.html +176 -0
  61. package/dist/coverage/lcov-report/test/testUtils.ts.html +97 -0
  62. package/dist/coverage/lcov.info +404 -0
  63. package/dist/index.js +46 -0
  64. package/dist/interfaces/Api.js +2 -0
  65. package/dist/interfaces/Config.js +2 -0
  66. package/dist/interfaces/common.js +2 -0
  67. package/dist/request.js +51 -0
  68. package/dist/types/Client.d.ts +146 -0
  69. package/dist/types/Constants.d.ts +16 -0
  70. package/dist/types/api/Calculation.d.ts +28 -0
  71. package/dist/types/api/Factor.d.ts +57 -0
  72. package/dist/types/api/FactorSets.d.ts +11 -0
  73. package/dist/types/api/Factors.d.ts +68 -0
  74. package/dist/types/api/Fugitive.d.ts +27 -0
  75. package/dist/types/api/FugitiveEmission.d.ts +27 -0
  76. package/dist/types/api/GenericCalculation.d.ts +28 -0
  77. package/dist/types/api/Location.d.ts +28 -0
  78. package/dist/types/api/LocationEmission.d.ts +28 -0
  79. package/dist/types/api/Mobile.d.ts +28 -0
  80. package/dist/types/api/MobileEmission.d.ts +28 -0
  81. package/dist/types/api/Stationary.d.ts +28 -0
  82. package/dist/types/api/StationaryEmission.d.ts +28 -0
  83. package/dist/types/api/TransportationAndDistribution.d.ts +27 -0
  84. package/dist/types/api/TransportationDistributionEmission.d.ts +27 -0
  85. package/dist/types/index.d.ts +10 -0
  86. package/dist/types/interfaces/Api.d.ts +286 -0
  87. package/dist/types/interfaces/Config.d.ts +66 -0
  88. package/dist/types/interfaces/common.d.ts +152 -0
  89. package/dist/types/request.d.ts +25 -0
  90. package/dist/types/utils.d.ts +14 -0
  91. package/dist/utils.js +23 -0
  92. package/jest.config.js +9 -0
  93. package/package.json +50 -0
  94. package/src/Client.ts +252 -0
  95. package/src/Constants.ts +16 -0
  96. package/src/api/Calculation.ts +44 -0
  97. package/src/api/Factor.ts +86 -0
  98. package/src/api/FactorSets.ts +24 -0
  99. package/src/api/Fugitive.ts +43 -0
  100. package/src/api/Location.ts +43 -0
  101. package/src/api/Mobile.ts +45 -0
  102. package/src/api/Stationary.ts +45 -0
  103. package/src/api/TransportationAndDistribution.ts +42 -0
  104. package/src/index.ts +10 -0
  105. package/src/interfaces/Api.ts +300 -0
  106. package/src/interfaces/Config.ts +78 -0
  107. package/src/interfaces/common.ts +174 -0
  108. package/src/request.ts +50 -0
  109. package/src/utils.ts +19 -0
  110. package/test/apiTest.test.ts +168 -0
  111. package/test/client.test.ts +82 -0
  112. package/test/mocks/CommonRequest.ts +19 -0
  113. package/test/mocks/FactorRequest.ts +16 -0
  114. package/test/mocks/GenericCalculationRequest.ts +19 -0
  115. package/test/mocks/LocationRequest.ts +19 -0
  116. package/test/mocks/SearchRequest.ts +19 -0
  117. package/test/request.test.ts +111 -0
  118. package/test/testUtils.ts +5 -0
  119. package/test/tokenRefresh.test.ts +52 -0
  120. package/tsconfig.json +15 -0
package/src/index.ts ADDED
@@ -0,0 +1,10 @@
1
+ export { Client } from './Client';
2
+ export * as Location from './api/Location';
3
+ export * as Fugitive from './api/Fugitive';
4
+ export * as Mobile from './api/Mobile';
5
+ export * as Stationary from './api/Stationary';
6
+ export * as Calculation from './api/Calculation';
7
+ export * as TransportationAndDistribution from './api/TransportationAndDistribution';
8
+ export * as Factor from './api/Factor';
9
+ export * as FactorSets from './api/FactorSets'
10
+ export { ClientConfig } from './interfaces/Config';
@@ -0,0 +1,300 @@
1
+ import { Activity, Location , Time, CombinedUnitsActivity, FactorActivity, SearchActivity, Pagination, FactorActivityWithFactorId, CombinedUnitsActivityWithFactorId, ActivityWithFactorId } from "./common";
2
+
3
+ /**
4
+ * Represents activity data associated with a location, with simplified requirements.
5
+ * Derived from the Activity type but makes the 'type' and 'unit' properties optional
6
+ * while keeping the 'value' property required.
7
+ *
8
+ * @typedef {Object} LocationActivity
9
+ * @property {any} value - The required activity value (e.g., quantity, amount, or measurement)
10
+ * @property {string} [type] - Optional activity type identifier
11
+ * @property {string} [unit] - Optional unit of measurement for the activity value
12
+ *
13
+ * @example
14
+ * // Minimal valid LocationActivity with only required properties
15
+ * const minimalActivity: LocationActivity = {
16
+ * value: 100
17
+ * };
18
+ *
19
+ * // Complete LocationActivity with all properties
20
+ * const fullActivity: LocationActivity = {
21
+ * value: 100,
22
+ * type: 'electricity',
23
+ * unit: 'kWh'
24
+ * };
25
+ */
26
+ type LocationActivity = Partial<Pick<Activity, 'type' | 'unit'>> & Pick<Activity, 'value'>;
27
+
28
+ /**
29
+ * Represents activity data associated with a location that includes a specific factor ID.
30
+ * Extends the Activity concept by requiring a factorId while making only the value property
31
+ * required and the unit property optional.
32
+ *
33
+ * @typedef {Object} LocationActivityWithFactorId
34
+ * @property {number} factorId - The unique identifier for the emission factor to be used
35
+ * @property {any} value - The required activity value (e.g., quantity, amount, or measurement)
36
+ * @property {string} [unit] - Optional unit of measurement for the activity value
37
+ *
38
+ * @example
39
+ * // Minimal valid LocationActivityWithFactorId with only required properties
40
+ * const minimalActivity: LocationActivityWithFactorId = {
41
+ * factorId: 12345,
42
+ * value: 100
43
+ * };
44
+ *
45
+ * // Complete LocationActivityWithFactorId with all properties
46
+ * const fullActivity: LocationActivityWithFactorId = {
47
+ * factorId: 12345,
48
+ * value: 100,
49
+ * unit: 'kWh'
50
+ * };
51
+ */
52
+ type LocationActivityWithFactorId = {
53
+ factorId: number;
54
+ } & Partial<Pick<Activity, 'unit'>> & Pick<Activity, 'value'>;
55
+
56
+ /**
57
+ * Request parameters for location-based calculations without specifying a factor ID.
58
+ * Used for calculating emissions or other metrics based on location data.
59
+ *
60
+ * @interface LocationRequestWithoutFactorId
61
+ */
62
+ export interface LocationRequestWithoutFactorId {
63
+ /**
64
+ * The geographical location information.
65
+ * Contains coordinates and possibly other location-specific data.
66
+ */
67
+ location : Location;
68
+ /**
69
+ * Optional time(YYYY-MM-DD)information for the location data.
70
+ * Can be used to specify when the activity occurred.
71
+ */
72
+ time?: Time;
73
+ /**
74
+ * The activity associated with this location.
75
+ * Defines what type of action or event occurred at the location.
76
+ */
77
+ activity: LocationActivity;
78
+ /**
79
+ * Whether to include additional details in the response.
80
+ * When true, the API will return more comprehensive information.
81
+ * @default false
82
+ */
83
+ includeDetails?: boolean;
84
+ }
85
+
86
+ /**
87
+ * Request parameters for location-based calculations with a specific factor ID.
88
+ * Used when the emission factor is already known and doesn't need to be determined by location.
89
+ *
90
+ * @interface LocationRequestWithFactorId
91
+ */
92
+ export interface LocationRequestWithFactorId {
93
+ /**
94
+ * The activity associated with this location, including the specific factor ID to use.
95
+ */
96
+ activity: LocationActivityWithFactorId;
97
+ /**
98
+ * Whether to include additional details in the response.
99
+ * When true, the API will return more comprehensive information.
100
+ * @default false
101
+ */
102
+ includeDetails?: boolean;
103
+ }
104
+
105
+ /**
106
+ * Common request parameters for calculations without specifying a factor ID.
107
+ * Used for general-purpose calculations based on location, time, and activity data.
108
+ *
109
+ * @interface CommonRequestWithoutFactorId
110
+ */
111
+ export interface CommonRequestWithoutFactorId {
112
+ /**
113
+ * The geographical location information.
114
+ * Contains coordinates and possibly other location-specific data.
115
+ */
116
+ location : Location;
117
+ /**
118
+ * Optional time information for the activity data.
119
+ * Can be used to specify when the activity occurred.
120
+ */
121
+ time?: Time;
122
+ /**
123
+ * The activity data for the calculation.
124
+ * Contains complete activity information including type, value, and unit.
125
+ */
126
+ activity: Activity;
127
+ /**
128
+ * Whether to include additional details in the response.
129
+ * When true, the API will return more comprehensive information.
130
+ * @default false
131
+ */
132
+ includeDetails?: boolean;
133
+ }
134
+
135
+ /**
136
+ * Common request parameters for calculations with a specific factor ID.
137
+ * Used when the emission factor is already known and doesn't need to be determined by location.
138
+ *
139
+ * @interface CommonRequestWithFactorId
140
+ */
141
+ export interface CommonRequestWithFactorId {
142
+ /**
143
+ * The activity data for the calculation, including the specific factor ID to use.
144
+ */
145
+ activity: ActivityWithFactorId;
146
+ /**
147
+ * Whether to include additional details in the response.
148
+ * When true, the API will return more comprehensive information.
149
+ * @default false
150
+ */
151
+ includeDetails?: boolean;
152
+ }
153
+
154
+ /**
155
+ * Request parameters for generic calculations without specifying a factor ID.
156
+ * Used for calculations that may involve multiple units or complex activity data.
157
+ *
158
+ * @interface GenericCalculationRequestWithoutFactorId
159
+ */
160
+ export interface GenericCalculationRequestWithoutFactorId {
161
+ /**
162
+ * The geographical location information.
163
+ * Contains coordinates and possibly other location-specific data.
164
+ */
165
+ location : Location;
166
+ /**
167
+ * Optional time information for the activity data.
168
+ * Can be used to specify when the activity occurred.
169
+ */
170
+ time?: Time;
171
+ /**
172
+ * The combined units activity data for the calculation.
173
+ * Supports complex activities with multiple units or components.
174
+ */
175
+ activity: CombinedUnitsActivity;
176
+ /**
177
+ * Whether to include additional details in the response.
178
+ * When true, the API will return more comprehensive information.
179
+ * @default false
180
+ */
181
+ includeDetails?: boolean;
182
+ }
183
+
184
+ /**
185
+ * Request parameters for generic calculations with a specific factor ID.
186
+ * Used for complex calculations where the emission factor is already known.
187
+ *
188
+ * @interface GenericCalculationRequestWithFactorId
189
+ */
190
+ export interface GenericCalculationRequestWithFactorId {
191
+ /**
192
+ * The combined units activity data for the calculation, including the specific factor ID to use.
193
+ * Supports complex activities with multiple units or components.
194
+ */
195
+ activity: CombinedUnitsActivityWithFactorId;
196
+ /**
197
+ * Whether to include additional details in the response.
198
+ * When true, the API will return more comprehensive information.
199
+ * @default false
200
+ */
201
+ includeDetails?: boolean;
202
+ }
203
+
204
+ /**
205
+ * Request parameters for retrieving factor information without specifying a factor ID.
206
+ * Used to look up appropriate emission factors based on location, time, and activity type.
207
+ *
208
+ * @interface FactorRequestWithoutFactorId
209
+ */
210
+ export interface FactorRequestWithoutFactorId {
211
+ /**
212
+ * The geographical location information.
213
+ * Used to determine which factors are applicable for the region.
214
+ */
215
+ location : Location;
216
+ /**
217
+ * Optional time information for the factor lookup.
218
+ * Can be used to find factors applicable at a specific point in time.
219
+ */
220
+ time?: Time;
221
+ /**
222
+ * The activity information used to determine which factors to retrieve.
223
+ * Typically includes the activity type and possibly unit information.
224
+ */
225
+ activity: FactorActivity;
226
+ }
227
+
228
+ /**
229
+ * Request parameters for retrieving specific factor information by ID.
230
+ * Used when the exact factor to retrieve is already known.
231
+ *
232
+ * @interface FactorRequestWithFactorId
233
+ */
234
+ export interface FactorRequestWithFactorId {
235
+ /**
236
+ * The activity information including the specific factor ID to retrieve.
237
+ */
238
+ activity: FactorActivityWithFactorId;
239
+ }
240
+
241
+ /**
242
+ * Request parameters for searching factors or activities.
243
+ * Used to find relevant factors or activities based on location, time, and search criteria.
244
+ *
245
+ * @interface SearchRequest
246
+ */
247
+ export interface SearchRequest {
248
+ /**
249
+ * The geographical location information to scope the search.
250
+ * Used to find factors or activities relevant to a specific region.
251
+ */
252
+ location : Location;
253
+ /**
254
+ * Optional time information to scope the search.
255
+ * Can be used to find factors or activities relevant at a specific point in time.
256
+ */
257
+ time?: Time;
258
+ /**
259
+ * The search criteria and parameters.
260
+ * Contains information about what to search for and any filters to apply.
261
+ */
262
+ activity: SearchActivity;
263
+ /**
264
+ * Optional pagination parameters for the search results.
265
+ * Used to limit the number of results and implement paging.
266
+ */
267
+ pagination?: Pagination
268
+ }
269
+
270
+ /**
271
+ * Union type representing either a factor request with or without a specific factor ID.
272
+ * Allows the API to handle both types of requests with a single parameter type.
273
+ *
274
+ * @typedef {FactorRequestWithoutFactorId | FactorRequestWithFactorId} FactorRequest
275
+ */
276
+ export type FactorRequest = FactorRequestWithoutFactorId | FactorRequestWithFactorId;
277
+
278
+ /**
279
+ * Union type representing either a generic calculation request with or without a specific factor ID.
280
+ * Allows the API to handle both types of calculation requests with a single parameter type.
281
+ *
282
+ * @typedef {GenericCalculationRequestWithoutFactorId | GenericCalculationRequestWithFactorId} CalculationRequest
283
+ */
284
+ export type CalculationRequest = GenericCalculationRequestWithoutFactorId | GenericCalculationRequestWithFactorId;
285
+
286
+ /**
287
+ * Union type representing either a common request with or without a specific factor ID.
288
+ * Allows the API to handle both types of common requests with a single parameter type.
289
+ *
290
+ * @typedef {CommonRequestWithoutFactorId | CommonRequestWithFactorId} CommonRequest
291
+ */
292
+ export type CommonRequest = CommonRequestWithoutFactorId | CommonRequestWithFactorId;
293
+
294
+ /**
295
+ * Union type representing either a location request with or without a specific factor ID.
296
+ * Allows the API to handle both types of location requests with a single parameter type.
297
+ *
298
+ * @typedef {LocationRequestWithoutFactorId | LocationRequestWithFactorId} LocationRequest
299
+ */
300
+ export type LocationRequest = LocationRequestWithoutFactorId | LocationRequestWithFactorId;
@@ -0,0 +1,78 @@
1
+ import { Method } from "axios";
2
+
3
+ /**
4
+ * Configuration for making HTTP requests.
5
+ * @interface RequestConfig
6
+ */
7
+ export interface RequestConfig {
8
+ /**
9
+ * The HTTP method to use for the request (GET, POST, PUT, DELETE, etc.).
10
+ */
11
+ method: Method;
12
+
13
+ /**
14
+ * The URL to which the request will be sent.
15
+ */
16
+ url: string;
17
+
18
+ /**
19
+ * Optional request body data to be sent with the request.
20
+ */
21
+ data?: any;
22
+
23
+ /**
24
+ * Optional URL query parameters to be appended to the request URL.
25
+ */
26
+ params?: any;
27
+
28
+ /**
29
+ * Optional HTTP headers to be included with the request.
30
+ */
31
+ headers?: Record<string, string>;
32
+ }
33
+
34
+ /**
35
+ * Configuration for initializing a client instance.
36
+ * @interface ClientConfig
37
+ */
38
+ export interface ClientConfig {
39
+ /**
40
+ * Optional API key used for authentication.
41
+ */
42
+ apiKey?: string;
43
+
44
+ /**
45
+ * Unique identifier for the client application.
46
+ * This is required for all client instances.
47
+ */
48
+ clientId: string;
49
+
50
+ /**
51
+ * Optional organization identifier.
52
+ */
53
+ orgId?: string;
54
+
55
+ /**
56
+ * Optional host URL for API requests.
57
+ * If not provided, a default host will be used.
58
+ */
59
+ host?: string;
60
+
61
+ /**
62
+ * Optional authentication URL.
63
+ * If not provided, a default authentication endpoint will be used.
64
+ */
65
+ authUrl?: string;
66
+
67
+ /**
68
+ * Optional authentication token.
69
+ * If provided, it will be used instead of requesting a new token.
70
+ */
71
+ token?: string;
72
+
73
+ /**
74
+ * Optional flag indicating whether the client is running as an Excel Add-In.
75
+ * This may affect certain behaviors like authentication flows.
76
+ */
77
+ isExcelAddIn?: boolean;
78
+ }
@@ -0,0 +1,174 @@
1
+ /**
2
+ * Represents a geographical location.
3
+ * @interface Location
4
+ */
5
+ export interface Location {
6
+ /**
7
+ * The country of the location.
8
+ */
9
+ country: string;
10
+
11
+ /**
12
+ * Optional state or province within the country.
13
+ */
14
+ stateProvince?: string;
15
+
16
+ /**
17
+ * Optional power grid identifier for the location.
18
+ */
19
+ powerGrid?: string;
20
+ }
21
+
22
+ /**
23
+ * Represents a point in time with date information.
24
+ * @interface Time
25
+ */
26
+ export interface Time {
27
+ /**
28
+ * The date in string format.
29
+ */
30
+ date: string
31
+ }
32
+
33
+ /**
34
+ * Represents an activity with type, value, and unit of measurement.
35
+ * @interface Activity
36
+ */
37
+ export interface Activity {
38
+ /**
39
+ * The type or category of the activity.
40
+ */
41
+ type: string;
42
+
43
+ /**
44
+ * The numerical value of the activity.
45
+ */
46
+ value: number;
47
+
48
+ /**
49
+ * The unit of measurement for the activity value.
50
+ */
51
+ unit: string;
52
+ }
53
+
54
+ /**
55
+ * Represents an activity identified by a factor ID instead of a type.
56
+ * @interface ActivityWithFactorId
57
+ */
58
+ export interface ActivityWithFactorId {
59
+ /**
60
+ * The unique identifier for the activity factor.
61
+ */
62
+ factorId: number;
63
+
64
+ /**
65
+ * The numerical value of the activity.
66
+ */
67
+ value: number;
68
+
69
+ /**
70
+ * The unit of measurement for the activity value.
71
+ */
72
+ unit: string;
73
+ }
74
+
75
+ /**
76
+ * Represents an activity that can have single or multiple values and units.
77
+ * @interface CombinedUnitsActivity
78
+ */
79
+ export interface CombinedUnitsActivity {
80
+ /**
81
+ * The type or category of the activity.
82
+ */
83
+ type: string;
84
+
85
+ /**
86
+ * The numerical value(s) of the activity. Can be a single number or an array of numbers.
87
+ */
88
+ value: number | number[];
89
+
90
+ /**
91
+ * The unit(s) of measurement for the activity value(s). Can be a single string or an array of strings.
92
+ */
93
+ unit: string | string[];
94
+ }
95
+
96
+ /**
97
+ * Represents an activity with factor ID that can have single or multiple values and units.
98
+ * @interface CombinedUnitsActivityWithFactorId
99
+ */
100
+ export interface CombinedUnitsActivityWithFactorId {
101
+ /**
102
+ * The unique identifier for the activity factor.
103
+ */
104
+ factorId: number;
105
+
106
+ /**
107
+ * The numerical value(s) of the activity. Can be a single number or an array of numbers.
108
+ */
109
+ value: number | number[];
110
+
111
+ /**
112
+ * The unit(s) of measurement for the activity value(s). Can be a single string or an array of strings.
113
+ */
114
+ unit: string | string[];
115
+ }
116
+
117
+ /**
118
+ * Represents a factor activity with a type and optional unit.
119
+ * @interface FactorActivity
120
+ */
121
+ export interface FactorActivity {
122
+ /**
123
+ * The type or category of the factor activity.
124
+ */
125
+ type: string;
126
+
127
+ /**
128
+ * Optional unit of measurement for the factor activity.
129
+ */
130
+ unit?: string;
131
+ }
132
+
133
+ /**
134
+ * Represents a factor activity identified by a factor ID instead of a type.
135
+ * @interface FactorActivityWithFactorId
136
+ */
137
+ export interface FactorActivityWithFactorId {
138
+ /**
139
+ * The unique identifier for the factor activity.
140
+ */
141
+ factorId: number;
142
+
143
+ /**
144
+ * Optional unit of measurement for the factor activity.
145
+ */
146
+ unit?: string;
147
+ }
148
+
149
+ /**
150
+ * Represents a search query for activities.
151
+ * @interface SearchActivity
152
+ */
153
+ export interface SearchActivity {
154
+ /**
155
+ * The search query string.
156
+ */
157
+ search: string;
158
+ }
159
+
160
+ /**
161
+ * Represents pagination parameters for paginated results.
162
+ * @interface Pagination
163
+ */
164
+ export interface Pagination {
165
+ /**
166
+ * The page number (1-based).
167
+ */
168
+ page: number;
169
+
170
+ /**
171
+ * The number of items per page.
172
+ */
173
+ size: number
174
+ }
package/src/request.ts ADDED
@@ -0,0 +1,50 @@
1
+ import axios, { AxiosRequestConfig } from "axios";
2
+ import { RequestConfig } from "./interfaces/Config";
3
+ import { Client } from "./Client";
4
+ import { CLIENT_SOURCE_HEADER } from "./Constants";
5
+
6
+ /**
7
+ * Makes an API request with standardized headers and token refresh.
8
+ *
9
+ * @export
10
+ * @template T The expected response data type
11
+ * @param {RequestConfig} config - The request configuration including method, URL, data, params, and optional headers
12
+ * @return {Promise<T>} A promise that resolves to the response data of type T
13
+ * @throws {Error} May throw an error if the API request fails or token refresh fails
14
+ *
15
+ * @example
16
+ * interface UserData {
17
+ * id: string;
18
+ * name: string;
19
+ * email: string;
20
+ * }
21
+ *
22
+ * const userData = await makeApiRequest<UserData>({
23
+ * method: 'GET',
24
+ * url: 'https://api.example.com/users/123',
25
+ * headers: { 'X-Custom-Header': 'value' }
26
+ * });
27
+ * console.log(userData.name); // Typed access to the response data
28
+ */
29
+
30
+ export async function makeApiRequest<T>(config: RequestConfig): Promise<T> {
31
+ const client = Client.getInstance();
32
+ await client.refreshToken();
33
+
34
+ const axiosConfig: AxiosRequestConfig = {
35
+ method: config.method,
36
+ url: config.url,
37
+ data: config.data,
38
+ params: config.params,
39
+ headers: {
40
+ ...client.getAuthHeader(),
41
+ ...config.headers,
42
+ 'Content-Type': 'application/json',
43
+ 'X-IBM-Client-Id': `ghgemissions-${client.getClientId()}`,
44
+ [CLIENT_SOURCE_HEADER]: client.getClientSource()
45
+ },
46
+ };
47
+
48
+ const response = await axios.request<T>(axiosConfig);
49
+ return response.data;
50
+ }
package/src/utils.ts ADDED
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Extracts the expiration time from a JWT token and give the token expiry time.
3
+ *
4
+ * @export
5
+ * @param {string} token - The JWT token to extract the expiration time from
6
+ * @return {any} The expiration time value from the token's payload
7
+ * @throws {Error} Throws an error if the exp field is missing in the token
8
+ *
9
+ * @example
10
+ * const jwtToken = 'your-token';
11
+ * const expiryTime = findExpiryTime(jwtToken);
12
+ * console.log(new Date(expiryTime * 1000).toISOString()); // Convert to date if it's a Unix timestamp
13
+ */
14
+ export function findExpiryTime(token: string): any {
15
+ const payload = token.split('.')[1];
16
+ const decoded = JSON.parse(Buffer.from(payload, 'base64').toString('utf8'));
17
+ if (!decoded.exp) throw new Error('exp field is missing in the token');
18
+ return decoded.exp;
19
+ }