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.
- package/CODE_OF_CONDUCT.md +76 -0
- package/CONTRIBUTING.md +68 -0
- package/LICENSE +201 -0
- package/MAINTAINERS.md +10 -0
- package/README.md +89 -0
- package/dist/Client.js +227 -0
- package/dist/Constants.js +19 -0
- package/dist/api/Calculation.js +41 -0
- package/dist/api/Factor.js +79 -0
- package/dist/api/FactorSets.js +24 -0
- package/dist/api/Factors.js +98 -0
- package/dist/api/Fugitive.js +40 -0
- package/dist/api/FugitiveEmission.js +40 -0
- package/dist/api/GenericCalculation.js +41 -0
- package/dist/api/Location.js +41 -0
- package/dist/api/LocationEmission.js +41 -0
- package/dist/api/Mobile.js +41 -0
- package/dist/api/MobileEmission.js +41 -0
- package/dist/api/Stationary.js +41 -0
- package/dist/api/StationaryEmission.js +41 -0
- package/dist/api/TransportationAndDistribution.js +40 -0
- package/dist/api/TransportationDistributionEmission.js +40 -0
- package/dist/coverage/clover.xml +225 -0
- package/dist/coverage/coverage-final.json +19 -0
- package/dist/coverage/lcov-report/base.css +224 -0
- package/dist/coverage/lcov-report/block-navigation.js +87 -0
- package/dist/coverage/lcov-report/favicon.png +0 -0
- package/dist/coverage/lcov-report/index.html +161 -0
- package/dist/coverage/lcov-report/prettify.css +1 -0
- package/dist/coverage/lcov-report/prettify.js +2 -0
- package/dist/coverage/lcov-report/sort-arrow-sprite.png +0 -0
- package/dist/coverage/lcov-report/sorter.js +210 -0
- package/dist/coverage/lcov-report/src/Client.ts.html +838 -0
- package/dist/coverage/lcov-report/src/Constants.ts.html +130 -0
- package/dist/coverage/lcov-report/src/api/Calculation.ts.html +217 -0
- package/dist/coverage/lcov-report/src/api/Factor.ts.html +343 -0
- package/dist/coverage/lcov-report/src/api/FactorSets.ts.html +154 -0
- package/dist/coverage/lcov-report/src/api/Factors.ts.html +403 -0
- package/dist/coverage/lcov-report/src/api/Fugitive.ts.html +214 -0
- package/dist/coverage/lcov-report/src/api/FugitiveEmission.ts.html +214 -0
- package/dist/coverage/lcov-report/src/api/GenericCalculation.ts.html +217 -0
- package/dist/coverage/lcov-report/src/api/Location.ts.html +214 -0
- package/dist/coverage/lcov-report/src/api/LocationEmission.ts.html +214 -0
- package/dist/coverage/lcov-report/src/api/Mobile.ts.html +220 -0
- package/dist/coverage/lcov-report/src/api/MobileEmission.ts.html +220 -0
- package/dist/coverage/lcov-report/src/api/Stationary.ts.html +220 -0
- package/dist/coverage/lcov-report/src/api/StationaryEmission.ts.html +220 -0
- package/dist/coverage/lcov-report/src/api/TransportationAndDistribution.ts.html +211 -0
- package/dist/coverage/lcov-report/src/api/TransportationDistributionEmission.ts.html +211 -0
- package/dist/coverage/lcov-report/src/api/index.html +221 -0
- package/dist/coverage/lcov-report/src/index.html +161 -0
- package/dist/coverage/lcov-report/src/request.ts.html +235 -0
- package/dist/coverage/lcov-report/src/utils.ts.html +139 -0
- package/dist/coverage/lcov-report/test/index.html +116 -0
- package/dist/coverage/lcov-report/test/mocks/CommonRequest.ts.html +142 -0
- package/dist/coverage/lcov-report/test/mocks/FactorRequest.ts.html +133 -0
- package/dist/coverage/lcov-report/test/mocks/GenericCalculationRequest.ts.html +142 -0
- package/dist/coverage/lcov-report/test/mocks/LocationRequest.ts.html +139 -0
- package/dist/coverage/lcov-report/test/mocks/SearchRequest.ts.html +142 -0
- package/dist/coverage/lcov-report/test/mocks/index.html +176 -0
- package/dist/coverage/lcov-report/test/testUtils.ts.html +97 -0
- package/dist/coverage/lcov.info +404 -0
- package/dist/index.js +46 -0
- package/dist/interfaces/Api.js +2 -0
- package/dist/interfaces/Config.js +2 -0
- package/dist/interfaces/common.js +2 -0
- package/dist/request.js +51 -0
- package/dist/types/Client.d.ts +146 -0
- package/dist/types/Constants.d.ts +16 -0
- package/dist/types/api/Calculation.d.ts +28 -0
- package/dist/types/api/Factor.d.ts +57 -0
- package/dist/types/api/FactorSets.d.ts +11 -0
- package/dist/types/api/Factors.d.ts +68 -0
- package/dist/types/api/Fugitive.d.ts +27 -0
- package/dist/types/api/FugitiveEmission.d.ts +27 -0
- package/dist/types/api/GenericCalculation.d.ts +28 -0
- package/dist/types/api/Location.d.ts +28 -0
- package/dist/types/api/LocationEmission.d.ts +28 -0
- package/dist/types/api/Mobile.d.ts +28 -0
- package/dist/types/api/MobileEmission.d.ts +28 -0
- package/dist/types/api/Stationary.d.ts +28 -0
- package/dist/types/api/StationaryEmission.d.ts +28 -0
- package/dist/types/api/TransportationAndDistribution.d.ts +27 -0
- package/dist/types/api/TransportationDistributionEmission.d.ts +27 -0
- package/dist/types/index.d.ts +10 -0
- package/dist/types/interfaces/Api.d.ts +286 -0
- package/dist/types/interfaces/Config.d.ts +66 -0
- package/dist/types/interfaces/common.d.ts +152 -0
- package/dist/types/request.d.ts +25 -0
- package/dist/types/utils.d.ts +14 -0
- package/dist/utils.js +23 -0
- package/jest.config.js +9 -0
- package/package.json +50 -0
- package/src/Client.ts +252 -0
- package/src/Constants.ts +16 -0
- package/src/api/Calculation.ts +44 -0
- package/src/api/Factor.ts +86 -0
- package/src/api/FactorSets.ts +24 -0
- package/src/api/Fugitive.ts +43 -0
- package/src/api/Location.ts +43 -0
- package/src/api/Mobile.ts +45 -0
- package/src/api/Stationary.ts +45 -0
- package/src/api/TransportationAndDistribution.ts +42 -0
- package/src/index.ts +10 -0
- package/src/interfaces/Api.ts +300 -0
- package/src/interfaces/Config.ts +78 -0
- package/src/interfaces/common.ts +174 -0
- package/src/request.ts +50 -0
- package/src/utils.ts +19 -0
- package/test/apiTest.test.ts +168 -0
- package/test/client.test.ts +82 -0
- package/test/mocks/CommonRequest.ts +19 -0
- package/test/mocks/FactorRequest.ts +16 -0
- package/test/mocks/GenericCalculationRequest.ts +19 -0
- package/test/mocks/LocationRequest.ts +19 -0
- package/test/mocks/SearchRequest.ts +19 -0
- package/test/request.test.ts +111 -0
- package/test/testUtils.ts +5 -0
- package/test/tokenRefresh.test.ts +52 -0
- package/tsconfig.json +15 -0
package/package.json
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "emissions-api-sdk",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "sdk for IBM Emissions APIs",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/types/index.d.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"test": "jest",
|
|
9
|
+
"build": "tsc"
|
|
10
|
+
},
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "https://github.com/IBM/ibm-envizi-emissions-api-nodejs-sdk"
|
|
14
|
+
},
|
|
15
|
+
"keywords": [
|
|
16
|
+
"EMISSIONS-API-SDK",
|
|
17
|
+
"EMISSIONS-SDK",
|
|
18
|
+
"IBM",
|
|
19
|
+
"GHG",
|
|
20
|
+
"IBMGHG",
|
|
21
|
+
"SDK",
|
|
22
|
+
"IBM-SDK",
|
|
23
|
+
"EMISSION",
|
|
24
|
+
"GHG-SDK",
|
|
25
|
+
"GREEEN HOUSE GAS",
|
|
26
|
+
"EMISSIONS",
|
|
27
|
+
"EMISSIONS-API",
|
|
28
|
+
"EMISSIONS API",
|
|
29
|
+
"GHG EMISSIONS",
|
|
30
|
+
"IBM-GHG",
|
|
31
|
+
"SCOPE 3 EMISSIONS",
|
|
32
|
+
"SCOPE 2 EMISSIONS",
|
|
33
|
+
"SCOPE 1 EMISSIONS"
|
|
34
|
+
],
|
|
35
|
+
"author": "santhosh0703,Prakhar-sahu,taylorsteffanj",
|
|
36
|
+
"license": "Apache-2.0",
|
|
37
|
+
"dependencies": {
|
|
38
|
+
"axios": "^1.12.2"
|
|
39
|
+
},
|
|
40
|
+
"devDependencies": {
|
|
41
|
+
"@types/jest": "^30.0.0",
|
|
42
|
+
"@types/node": "^24.0.4",
|
|
43
|
+
"axios-mock-adapter": "^2.1.0",
|
|
44
|
+
"jest": "^30.0.5",
|
|
45
|
+
"nock": "^14.0.6",
|
|
46
|
+
"ts-jest": "^29.4.0",
|
|
47
|
+
"typedoc": "^0.28.13",
|
|
48
|
+
"typescript": "^5.9.2"
|
|
49
|
+
}
|
|
50
|
+
}
|
package/src/Client.ts
ADDED
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
import axios from "axios";
|
|
2
|
+
import { ClientConfig } from "./interfaces/Config";
|
|
3
|
+
import { findExpiryTime } from "./utils";
|
|
4
|
+
import { API_DOMAIN, TOKEN_GENERATION_API, CLIENT_SOURCE_EXCEL, CLIENT_SOURCE_SDK } from "./Constants";
|
|
5
|
+
|
|
6
|
+
export class Client {
|
|
7
|
+
private static instance: Client | null = null;
|
|
8
|
+
|
|
9
|
+
private token: string;
|
|
10
|
+
private readonly apiKey: string;
|
|
11
|
+
private readonly clientId: string;
|
|
12
|
+
private readonly orgId: string;
|
|
13
|
+
private expiresAt: number;
|
|
14
|
+
private readonly domain: string;
|
|
15
|
+
private readonly tokenDomain: string;
|
|
16
|
+
private readonly isUserProvidedToken: boolean;
|
|
17
|
+
private readonly clientSource: string;
|
|
18
|
+
|
|
19
|
+
private constructor(
|
|
20
|
+
token: string,
|
|
21
|
+
config: ClientConfig,
|
|
22
|
+
isUserProvidedToken = false
|
|
23
|
+
) {
|
|
24
|
+
const { apiKey="", clientId, orgId="", host, authUrl, isExcelAddIn=false } = config;
|
|
25
|
+
this.token = token;
|
|
26
|
+
this.clientId = clientId;
|
|
27
|
+
if (isUserProvidedToken) {
|
|
28
|
+
this.apiKey = "";
|
|
29
|
+
this.orgId = "";
|
|
30
|
+
} else {
|
|
31
|
+
this.apiKey = apiKey;
|
|
32
|
+
this.orgId = orgId;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const exp = findExpiryTime(token);
|
|
36
|
+
this.expiresAt = exp;
|
|
37
|
+
this.domain = host ?? API_DOMAIN;
|
|
38
|
+
this.tokenDomain = authUrl ?? TOKEN_GENERATION_API;
|
|
39
|
+
this.isUserProvidedToken = isUserProvidedToken;
|
|
40
|
+
this.clientSource = isExcelAddIn === true ? CLIENT_SOURCE_EXCEL : CLIENT_SOURCE_SDK;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Initializes and returns a Client instance with the provided configuration.
|
|
45
|
+
*
|
|
46
|
+
* @static
|
|
47
|
+
* @param {ClientConfig} config - The client configuration object
|
|
48
|
+
* @return {Promise<void>} A promise that resolves when the client is initialized
|
|
49
|
+
* @throws {Error} Throws an error if:
|
|
50
|
+
* - custom "host" is provided without "authUrl"
|
|
51
|
+
* - token is provided without "clientId"
|
|
52
|
+
* - apiKey is provided without "clientId" or "orgId"
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* // Initialize with authentication URL
|
|
56
|
+
* await Client.getClient({
|
|
57
|
+
* host: 'https://api.example.com',
|
|
58
|
+
* authUrl: 'https://auth.example.com',
|
|
59
|
+
* clientId: 'client123',
|
|
60
|
+
* orgId: 'org456'
|
|
61
|
+
* });
|
|
62
|
+
*
|
|
63
|
+
* // Initialize with existing token
|
|
64
|
+
* await Client.getClient({
|
|
65
|
+
* token: 'existing-jwt-token',
|
|
66
|
+
* clientId: 'client123'
|
|
67
|
+
* });
|
|
68
|
+
*
|
|
69
|
+
* // Initialize with API key
|
|
70
|
+
* await Client.getClient({
|
|
71
|
+
* apiKey: 'your-api-key',
|
|
72
|
+
* clientId: 'client123',
|
|
73
|
+
* orgId: 'org456'
|
|
74
|
+
* });
|
|
75
|
+
*/
|
|
76
|
+
|
|
77
|
+
public static async getClient(config: ClientConfig): Promise<void> {
|
|
78
|
+
if (config.host && !config.authUrl) {
|
|
79
|
+
throw new Error(
|
|
80
|
+
'If custom "host" is provided, "authUrl" must also be provided.'
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
if (config.token && !config.clientId) {
|
|
84
|
+
throw new Error(
|
|
85
|
+
'If token is provided directly, "clientId" must also be provided.'
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
if(config.apiKey && (!config.orgId || !config.clientId)){
|
|
89
|
+
throw new Error(
|
|
90
|
+
'If apiKey is provided , "clientId" and "OrgId" must also be provided.'
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
let token: string;
|
|
94
|
+
let isUserProvidedToken = false;
|
|
95
|
+
|
|
96
|
+
if (config.token && config.clientId) {
|
|
97
|
+
token = config.token;
|
|
98
|
+
isUserProvidedToken = true;
|
|
99
|
+
} else token = await Client.requestToken(config);
|
|
100
|
+
Client.instance = new Client(token, config,isUserProvidedToken);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Returns the domain URL configured for the client.
|
|
105
|
+
*
|
|
106
|
+
* @return {string} The domain URL string
|
|
107
|
+
*
|
|
108
|
+
* @example
|
|
109
|
+
* const client = Client.getInstance();
|
|
110
|
+
* const apiDomain = client.getDomain();
|
|
111
|
+
* console.log(apiDomain); // e.g., 'https://api.example.com'
|
|
112
|
+
*/
|
|
113
|
+
public getDomain(): string {
|
|
114
|
+
return this.domain;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Returns the singleton instance of the Client.
|
|
119
|
+
*
|
|
120
|
+
* @static
|
|
121
|
+
* @return {Client} The singleton Client instance
|
|
122
|
+
* @throws {Error} Throws an error if the Client has not been initialized with getClient()
|
|
123
|
+
*
|
|
124
|
+
* @example
|
|
125
|
+
* // First initialize the client
|
|
126
|
+
* await Client.getClient({
|
|
127
|
+
* clientId: 'client123',
|
|
128
|
+
* orgId: 'org456'
|
|
129
|
+
* });
|
|
130
|
+
*
|
|
131
|
+
* // Then get the instance
|
|
132
|
+
* const client = Client.getInstance();
|
|
133
|
+
* // Use the client...
|
|
134
|
+
*/
|
|
135
|
+
public static getInstance(): Client {
|
|
136
|
+
if (!Client.instance) {
|
|
137
|
+
throw new Error(
|
|
138
|
+
"Client is not initialized. Call Client.getClient() first."
|
|
139
|
+
);
|
|
140
|
+
}
|
|
141
|
+
return Client.instance;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Refreshes the authentication token if it's about to expire.
|
|
146
|
+
* Only refreshes if the token was not provided by the user and is expiring within 60 seconds.
|
|
147
|
+
*
|
|
148
|
+
* @return {Promise<void>} A promise that resolves when the token refresh is complete
|
|
149
|
+
*
|
|
150
|
+
* @example
|
|
151
|
+
* const client = Client.getInstance();
|
|
152
|
+
* // Ensure token is fresh before making API requests
|
|
153
|
+
* await client.refreshToken();
|
|
154
|
+
* // Proceed with API requests...
|
|
155
|
+
*/
|
|
156
|
+
public async refreshToken(): Promise<void> {
|
|
157
|
+
if (!this.isUserProvidedToken) {
|
|
158
|
+
const now = Math.floor(Date.now() / 1000);
|
|
159
|
+
if (this.expiresAt - now < 60) {
|
|
160
|
+
console.log("[SDK] Refreshing token...");
|
|
161
|
+
const token = await Client.requestToken({
|
|
162
|
+
apiKey: this.apiKey,
|
|
163
|
+
clientId: this.clientId,
|
|
164
|
+
orgId: this.orgId,
|
|
165
|
+
authUrl: this.tokenDomain,
|
|
166
|
+
});
|
|
167
|
+
this.token = token;
|
|
168
|
+
this.expiresAt = findExpiryTime(token);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Returns the authorization header object with the Bearer token.
|
|
175
|
+
*
|
|
176
|
+
* @return {Record<string, string>} An object containing the Authorization header with the Bearer token
|
|
177
|
+
*
|
|
178
|
+
* @example
|
|
179
|
+
* const client = Client.getInstance();
|
|
180
|
+
* const headers = {
|
|
181
|
+
* 'Content-Type': 'application/json',
|
|
182
|
+
* ...client.getAuthHeader()
|
|
183
|
+
* };
|
|
184
|
+
* // headers = { 'Content-Type': 'application/json', 'Authorization': 'Bearer token...' }
|
|
185
|
+
*/
|
|
186
|
+
public getAuthHeader(): Record<string, string> {
|
|
187
|
+
return { Authorization: `Bearer ${this.token}` };
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Returns the client ID configured for this client instance.
|
|
192
|
+
*
|
|
193
|
+
* @return {string} The client ID string
|
|
194
|
+
*
|
|
195
|
+
* @example
|
|
196
|
+
* const client = Client.getInstance();
|
|
197
|
+
* const clientId = client.getClientId();
|
|
198
|
+
* console.log(clientId); // e.g., 'client123'
|
|
199
|
+
*/
|
|
200
|
+
public getClientId(): string {
|
|
201
|
+
return this.clientId;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Returns the client source identifier for this client instance.
|
|
206
|
+
*
|
|
207
|
+
* @return {string} The client source string
|
|
208
|
+
*
|
|
209
|
+
* @example
|
|
210
|
+
* const client = Client.getInstance();
|
|
211
|
+
* const source = client.getClientSource();
|
|
212
|
+
* console.log(source); // e.g., 'web', 'mobile', 'sdk'
|
|
213
|
+
*/
|
|
214
|
+
public getClientSource() : string {
|
|
215
|
+
return this.clientSource;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* Requests an authentication token from the token generation API.
|
|
220
|
+
*
|
|
221
|
+
* @private
|
|
222
|
+
* @static
|
|
223
|
+
* @param {ClientConfig} config - The client configuration containing authentication details
|
|
224
|
+
* @return {Promise<string>} A promise that resolves to the authentication token string
|
|
225
|
+
* @throws {Error} Throws an error if the token response is empty
|
|
226
|
+
*
|
|
227
|
+
* @example
|
|
228
|
+
* // Internal usage within the Client class
|
|
229
|
+
* const token = await Client.requestToken({
|
|
230
|
+
* apiKey: 'your-api-key',
|
|
231
|
+
* clientId: 'client123',
|
|
232
|
+
* orgId: 'org456',
|
|
233
|
+
* authUrl: 'https://custom-auth.example.com/token'
|
|
234
|
+
* });
|
|
235
|
+
*/
|
|
236
|
+
private static async requestToken(config: ClientConfig): Promise<string> {
|
|
237
|
+
const tokenUrl = config.authUrl ?? TOKEN_GENERATION_API;
|
|
238
|
+
const res = await axios.get(tokenUrl, {
|
|
239
|
+
headers: {
|
|
240
|
+
"X-Api-Key": config.apiKey,
|
|
241
|
+
"X-IBM-Client-Id": `saascore-${config.clientId}`,
|
|
242
|
+
accept: "application/json",
|
|
243
|
+
},
|
|
244
|
+
params: {
|
|
245
|
+
orgId: config.orgId
|
|
246
|
+
}
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
if (!res.data) throw new Error("Token response is empty");
|
|
250
|
+
return String(res.data).trim();
|
|
251
|
+
}
|
|
252
|
+
}
|
package/src/Constants.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export const API_DOMAIN = "https://api.ibm.com/ghgemissions/run";
|
|
2
|
+
export const TOKEN_GENERATION_API = "https://api.ibm.com/saascore/run/authentication-retrieve/api-key";
|
|
3
|
+
export const MOBILE_API_PATH = "/v3/carbon/mobile";
|
|
4
|
+
export const LOCATION_API_PATH = "/v3/carbon/location";
|
|
5
|
+
export const FUGITIVE_API_PATH = "/v3/carbon/fugitive";
|
|
6
|
+
export const STATIONARY_API_PATH = "/v3/carbon/stationary";
|
|
7
|
+
export const GENERIC_CALCULATION_API_PATH = "/v3/carbon/calculation";
|
|
8
|
+
export const TRANSPORTATION_AND_DISTRIBUTION_API_PATH = "/v3/carbon/transportation-and-distribution";
|
|
9
|
+
export const FACTOR_API_PATH = "/v3/carbon/factor";
|
|
10
|
+
export const FACTOR_SET_API_PATH = "/v3/carbon/factorset";
|
|
11
|
+
export const SEARCH_API_PATH = "/v3/carbon/factor/search";
|
|
12
|
+
export const GET = "GET";
|
|
13
|
+
export const POST = "POST";
|
|
14
|
+
export const CLIENT_SOURCE_HEADER = "X-Client-Source";
|
|
15
|
+
export const CLIENT_SOURCE_EXCEL = "excel";
|
|
16
|
+
export const CLIENT_SOURCE_SDK = "node-sdk";
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { Client } from "../Client";
|
|
2
|
+
import { GENERIC_CALCULATION_API_PATH, POST } from "../Constants";
|
|
3
|
+
import { CalculationRequest } from "../interfaces/Api";
|
|
4
|
+
import { makeApiRequest } from "../request";
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Performs emission calculations by making a POST request to the generic calculation API endpoint.
|
|
9
|
+
*
|
|
10
|
+
* @export
|
|
11
|
+
* @param {CalculationRequest} payload - The calculation request data to be sent to the API
|
|
12
|
+
* @return {Promise<string>} A promise that resolves to the calculation result string returned by the API
|
|
13
|
+
* @throws {Error} May throw an error if the API request fails
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* const calculationRequest = {
|
|
17
|
+
"time" : {
|
|
18
|
+
"date": "2025-01-04"
|
|
19
|
+
},
|
|
20
|
+
"location": {
|
|
21
|
+
"country": "usa",
|
|
22
|
+
"stateProvince": "new york"
|
|
23
|
+
},
|
|
24
|
+
"activity": {
|
|
25
|
+
"type":"Waste Combusted - Steel Cans:Default factor",
|
|
26
|
+
"unit": "kg",
|
|
27
|
+
"value": 1223123.121
|
|
28
|
+
},
|
|
29
|
+
"includeDetails": true
|
|
30
|
+
};
|
|
31
|
+
* const result = await calculate(calculationRequest);
|
|
32
|
+
*/
|
|
33
|
+
export async function calculate(
|
|
34
|
+
payload: CalculationRequest
|
|
35
|
+
): Promise<string> {
|
|
36
|
+
const client = Client.getInstance();
|
|
37
|
+
const url = client.getDomain() + GENERIC_CALCULATION_API_PATH;
|
|
38
|
+
|
|
39
|
+
return makeApiRequest<string>({
|
|
40
|
+
method: POST,
|
|
41
|
+
url,
|
|
42
|
+
data: payload,
|
|
43
|
+
});
|
|
44
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { Client } from "../Client";
|
|
2
|
+
import { FACTOR_API_PATH, POST, SEARCH_API_PATH } from "../Constants";
|
|
3
|
+
import { FactorRequest, SearchRequest } from "../interfaces/Api";
|
|
4
|
+
import { makeApiRequest } from "../request";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Retrieves a factor details by making a POST request to the factor API endpoint.
|
|
8
|
+
*
|
|
9
|
+
* @export
|
|
10
|
+
* @param {FactorRequest} payload - The factor request data to be sent to the API
|
|
11
|
+
* @return {Promise<string>} A promise that resolves to the factor string returned by the API
|
|
12
|
+
* @throws {Error} May throw an error if the API request fails
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* const factorRequest = {
|
|
16
|
+
|
|
17
|
+
"activity": {
|
|
18
|
+
"factorId": 12345
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
or
|
|
23
|
+
|
|
24
|
+
const factorRequest = {
|
|
25
|
+
"time" : {
|
|
26
|
+
"date": "2025-01-04"
|
|
27
|
+
},
|
|
28
|
+
"location": {
|
|
29
|
+
"country": "usa",
|
|
30
|
+
"stateProvince": "new york"
|
|
31
|
+
},
|
|
32
|
+
"activity": {
|
|
33
|
+
"type":"natural gas"
|
|
34
|
+
},
|
|
35
|
+
"includeDetails": true
|
|
36
|
+
};
|
|
37
|
+
* const factor = await retrieveFactor(factorRequest);
|
|
38
|
+
*/
|
|
39
|
+
|
|
40
|
+
export async function retrieveFactor(
|
|
41
|
+
payload: FactorRequest
|
|
42
|
+
): Promise<string> {
|
|
43
|
+
const client = Client.getInstance();
|
|
44
|
+
const url = client.getDomain() + FACTOR_API_PATH;
|
|
45
|
+
|
|
46
|
+
return makeApiRequest<string>({
|
|
47
|
+
method: POST,
|
|
48
|
+
url,
|
|
49
|
+
data: payload,
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Performs a search operation by making a POST request to the search API endpoint.
|
|
56
|
+
*
|
|
57
|
+
* @export
|
|
58
|
+
* @param {SearchRequest} payload - The search request data to be sent to the API
|
|
59
|
+
* @return {Promise<string>} A promise that resolves to the search results string returned by the API
|
|
60
|
+
* @throws {Error} May throw an error if the API request fails
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* const searchRequest = {
|
|
64
|
+
"time":{
|
|
65
|
+
"date": "2020-06-10"
|
|
66
|
+
},
|
|
67
|
+
"activity": {
|
|
68
|
+
"search" : "travel"},
|
|
69
|
+
"location": {
|
|
70
|
+
"country": "USA"
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
* const results = await search(searchRequest);
|
|
74
|
+
*/
|
|
75
|
+
export async function search(
|
|
76
|
+
payload: SearchRequest
|
|
77
|
+
): Promise<string> {
|
|
78
|
+
const client = Client.getInstance();
|
|
79
|
+
const url = client.getDomain() + SEARCH_API_PATH;
|
|
80
|
+
|
|
81
|
+
return makeApiRequest<string>({
|
|
82
|
+
method: POST,
|
|
83
|
+
url,
|
|
84
|
+
data: payload,
|
|
85
|
+
});
|
|
86
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { Client } from "../Client";
|
|
2
|
+
import { FACTOR_SET_API_PATH, GET } from "../Constants";
|
|
3
|
+
import { makeApiRequest } from "../request";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Retrieves factor sets by making a GET request to the factor set API endpoint.
|
|
7
|
+
*
|
|
8
|
+
* @export
|
|
9
|
+
* @return {Promise<string>} A promise that resolves to the factor sets string returned by the API
|
|
10
|
+
* @throws {Error} May throw an error if the API request fails
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* const factorSets = await get();
|
|
14
|
+
*/
|
|
15
|
+
export async function get(
|
|
16
|
+
): Promise<string> {
|
|
17
|
+
const client = Client.getInstance();
|
|
18
|
+
const url = client.getDomain() + FACTOR_SET_API_PATH;
|
|
19
|
+
|
|
20
|
+
return makeApiRequest<string>({
|
|
21
|
+
method: GET,
|
|
22
|
+
url
|
|
23
|
+
});
|
|
24
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { Client } from "../Client";
|
|
2
|
+
import { FUGITIVE_API_PATH, POST } from "../Constants";
|
|
3
|
+
import { CommonRequest } from "../interfaces/Api";
|
|
4
|
+
import { makeApiRequest } from "../request";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Performs scope 1 fugitve emission calculations by making a POST request to the fugitive API endpoint.
|
|
8
|
+
*
|
|
9
|
+
* @export
|
|
10
|
+
* @param {CommonRequest} payload - The request data to be sent to the API
|
|
11
|
+
* @return {Promise<string>} A promise that resolves to the calculation result string returned by the API
|
|
12
|
+
* @throws {Error} May throw an error if the API request fails
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* const request = {
|
|
16
|
+
"time": {
|
|
17
|
+
"date": "2025-01-04"
|
|
18
|
+
},
|
|
19
|
+
"location": {
|
|
20
|
+
"country": "usa"
|
|
21
|
+
},
|
|
22
|
+
"activity": {
|
|
23
|
+
"type": "R134A",
|
|
24
|
+
"value": 150,
|
|
25
|
+
"unit": "kg"
|
|
26
|
+
},
|
|
27
|
+
"includeDetails": false
|
|
28
|
+
};
|
|
29
|
+
* const result = await calculate(request);
|
|
30
|
+
*/
|
|
31
|
+
|
|
32
|
+
export async function calculate(
|
|
33
|
+
payload: CommonRequest
|
|
34
|
+
): Promise<string> {
|
|
35
|
+
const client = Client.getInstance();
|
|
36
|
+
const url = client.getDomain() + FUGITIVE_API_PATH;
|
|
37
|
+
|
|
38
|
+
return makeApiRequest<string>({
|
|
39
|
+
method: POST,
|
|
40
|
+
url,
|
|
41
|
+
data: payload,
|
|
42
|
+
});
|
|
43
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { Client } from "../Client";
|
|
2
|
+
import { LOCATION_API_PATH, POST } from "../Constants";
|
|
3
|
+
import { LocationRequest } from "../interfaces/Api";
|
|
4
|
+
import { makeApiRequest } from "../request";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Performs scope 2 purchased energy Emission calculations by making a POST request to the location API endpoint.
|
|
8
|
+
*
|
|
9
|
+
* @export
|
|
10
|
+
* @param {LocationRequest} payload - The location request data to be sent to the API
|
|
11
|
+
* @return {Promise<string>} A promise that resolves to the purchased energy emission result string returned by the API
|
|
12
|
+
* @throws {Error} May throw an error if the API request fails
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* const calculationRequest = {
|
|
16
|
+
"time" : {
|
|
17
|
+
"date": "2025-01-04"
|
|
18
|
+
},
|
|
19
|
+
"location": {
|
|
20
|
+
"country": "usa",
|
|
21
|
+
"stateProvince": "new york"
|
|
22
|
+
},
|
|
23
|
+
"activity": {
|
|
24
|
+
"type":"electricity",
|
|
25
|
+
"unit": "kwh",
|
|
26
|
+
"value": 14123143
|
|
27
|
+
},
|
|
28
|
+
"includeDetails": true
|
|
29
|
+
};
|
|
30
|
+
* const result = await calculate(calculationRequest);
|
|
31
|
+
*/
|
|
32
|
+
export async function calculate(
|
|
33
|
+
payload: LocationRequest
|
|
34
|
+
): Promise<string> {
|
|
35
|
+
const client = Client.getInstance();
|
|
36
|
+
const url = client.getDomain() + LOCATION_API_PATH;
|
|
37
|
+
|
|
38
|
+
return makeApiRequest<string>({
|
|
39
|
+
method: POST,
|
|
40
|
+
url,
|
|
41
|
+
data: payload,
|
|
42
|
+
});
|
|
43
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { makeApiRequest } from "../request";
|
|
2
|
+
import { MOBILE_API_PATH, POST } from "../Constants";
|
|
3
|
+
import { CommonRequest } from "../interfaces/Api";
|
|
4
|
+
import { Client } from "../Client";
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Performs Scope 1 mobile emissions related calculations by making a POST request to the mobile API endpoint.
|
|
9
|
+
*
|
|
10
|
+
* @export
|
|
11
|
+
* @param {CommonRequest} payload - The request data to be sent to the mobile API
|
|
12
|
+
* @return {Promise<string>} A promise that resolves to the calculation result string returned by the API
|
|
13
|
+
* @throws {Error} May throw an error if the API request fails
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* const request = {
|
|
17
|
+
"time": {
|
|
18
|
+
"date": "2022-01-01"
|
|
19
|
+
},
|
|
20
|
+
"location": {
|
|
21
|
+
"country": "USA",
|
|
22
|
+
"stateProvince": "new york"
|
|
23
|
+
},
|
|
24
|
+
"activity": {
|
|
25
|
+
"type": "Diesel Fuel",
|
|
26
|
+
"value": 1500000,
|
|
27
|
+
"unit": "l"
|
|
28
|
+
},
|
|
29
|
+
"includeDetails": false
|
|
30
|
+
};
|
|
31
|
+
* const result = await calculate(request);
|
|
32
|
+
*/
|
|
33
|
+
|
|
34
|
+
export async function calculate(
|
|
35
|
+
payload: CommonRequest
|
|
36
|
+
): Promise<string> {
|
|
37
|
+
const client = Client.getInstance();
|
|
38
|
+
const url = client.getDomain() + MOBILE_API_PATH;
|
|
39
|
+
|
|
40
|
+
return makeApiRequest<string>({
|
|
41
|
+
method: POST,
|
|
42
|
+
url,
|
|
43
|
+
data: payload,
|
|
44
|
+
});
|
|
45
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { Client } from "../Client";
|
|
2
|
+
import { POST, STATIONARY_API_PATH } from "../Constants";
|
|
3
|
+
import { CommonRequest } from "../interfaces/Api";
|
|
4
|
+
import { makeApiRequest } from "../request";
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Performs Scope 1 stationary calculations by making a POST request to the stationary API endpoint.
|
|
9
|
+
*
|
|
10
|
+
* @export
|
|
11
|
+
* @param {CommonRequest} payload - The request data to be sent to the stationary API
|
|
12
|
+
* @return {Promise<string>} A promise that resolves to the calculation result string returned by the API
|
|
13
|
+
* @throws {Error} May throw an error if the API request fails
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* const request = {
|
|
17
|
+
"time" : {
|
|
18
|
+
"date": "2025-01-04"
|
|
19
|
+
},
|
|
20
|
+
"location": {
|
|
21
|
+
"country": "usa",
|
|
22
|
+
"stateProvince": "new york"
|
|
23
|
+
},
|
|
24
|
+
"activity": {
|
|
25
|
+
"type":"Coal - Lignite",
|
|
26
|
+
"unit": "KJ",
|
|
27
|
+
"value": 3
|
|
28
|
+
},
|
|
29
|
+
"includeDetails": true
|
|
30
|
+
};
|
|
31
|
+
* const result = await calculate(request);
|
|
32
|
+
*/
|
|
33
|
+
|
|
34
|
+
export async function calculate(
|
|
35
|
+
payload: CommonRequest
|
|
36
|
+
): Promise<string> {
|
|
37
|
+
const client = Client.getInstance();
|
|
38
|
+
const url = client.getDomain() + STATIONARY_API_PATH;
|
|
39
|
+
|
|
40
|
+
return makeApiRequest<string>({
|
|
41
|
+
method: POST,
|
|
42
|
+
url,
|
|
43
|
+
data: payload,
|
|
44
|
+
});
|
|
45
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { Client } from "../Client";
|
|
2
|
+
import { TRANSPORTATION_AND_DISTRIBUTION_API_PATH, POST } from "../Constants";
|
|
3
|
+
import { CalculationRequest } from "../interfaces/Api";
|
|
4
|
+
import { makeApiRequest } from "../request";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Performs Scope 2 transportation and distribution emission calculations by making a POST request to the transportation and distribution API endpoint.
|
|
8
|
+
*
|
|
9
|
+
* @export
|
|
10
|
+
* @param {CalculationRequest} payload - The calculation request data for transportation and distribution operations
|
|
11
|
+
* @return {Promise<string>} A promise that resolves to the calculation result string returned by the API
|
|
12
|
+
* @throws {Error} May throw an error if the API request fails
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* const request = {
|
|
16
|
+
"time" : {
|
|
17
|
+
"date": "2025-01-04"
|
|
18
|
+
},
|
|
19
|
+
"location": {
|
|
20
|
+
"country": "usa"
|
|
21
|
+
},
|
|
22
|
+
"activity": {
|
|
23
|
+
"type": "Freight - Cargo Ship - Bulk Carrier - 0-9999 dwt",
|
|
24
|
+
"unit": ["t","km"],
|
|
25
|
+
"value": [1.0,1.0]
|
|
26
|
+
},
|
|
27
|
+
"includeDetails": true
|
|
28
|
+
};
|
|
29
|
+
* const result = await calculate(request);
|
|
30
|
+
*/
|
|
31
|
+
export async function calculate(
|
|
32
|
+
payload: CalculationRequest
|
|
33
|
+
): Promise<string> {
|
|
34
|
+
const client = Client.getInstance();
|
|
35
|
+
const url = client.getDomain() + TRANSPORTATION_AND_DISTRIBUTION_API_PATH;
|
|
36
|
+
|
|
37
|
+
return makeApiRequest<string>({
|
|
38
|
+
method: POST,
|
|
39
|
+
url,
|
|
40
|
+
data: payload,
|
|
41
|
+
});
|
|
42
|
+
}
|