emissions-api-sdk 1.0.0 → 1.0.2

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 (155) hide show
  1. package/README.md +33 -2
  2. package/dist/Constants.js +23 -1
  3. package/dist/api/Calculation.js +78 -15
  4. package/dist/api/Factor.js +99 -18
  5. package/dist/api/FactorSets.js +1 -1
  6. package/dist/api/Fugitive.js +76 -13
  7. package/dist/api/Location.js +76 -15
  8. package/dist/api/Mobile.js +65 -1
  9. package/dist/api/Stationary.js +78 -15
  10. package/dist/api/TransportationAndDistribution.js +77 -14
  11. package/dist/coverage/clover.xml +191 -81
  12. package/dist/coverage/coverage-final.json +11 -11
  13. package/dist/coverage/lcov-report/index.html +16 -16
  14. package/dist/coverage/lcov-report/src/Client.ts.html +23 -23
  15. package/dist/coverage/lcov-report/src/Constants.ts.html +82 -4
  16. package/dist/coverage/lcov-report/src/api/Calculation.ts.html +245 -26
  17. package/dist/coverage/lcov-report/src/api/Factor.ts.html +313 -34
  18. package/dist/coverage/lcov-report/src/api/FactorSets.ts.html +8 -5
  19. package/dist/coverage/lcov-report/src/api/Fugitive.ts.html +246 -24
  20. package/dist/coverage/lcov-report/src/api/Location.ts.html +245 -26
  21. package/dist/coverage/lcov-report/src/api/Mobile.ts.html +240 -12
  22. package/dist/coverage/lcov-report/src/api/Stationary.ts.html +248 -26
  23. package/dist/coverage/lcov-report/src/api/TransportationAndDistribution.ts.html +250 -25
  24. package/dist/coverage/lcov-report/src/api/index.html +25 -25
  25. package/dist/coverage/lcov-report/src/index.html +7 -7
  26. package/dist/coverage/lcov-report/src/request.ts.html +1 -1
  27. package/dist/coverage/lcov-report/src/utils.ts.html +5 -5
  28. package/dist/coverage/lcov-report/test/index.html +1 -1
  29. package/dist/coverage/lcov-report/test/mocks/CommonRequest.ts.html +1 -1
  30. package/dist/coverage/lcov-report/test/mocks/FactorRequest.ts.html +1 -1
  31. package/dist/coverage/lcov-report/test/mocks/GenericCalculationRequest.ts.html +1 -1
  32. package/dist/coverage/lcov-report/test/mocks/LocationRequest.ts.html +1 -1
  33. package/dist/coverage/lcov-report/test/mocks/SearchRequest.ts.html +1 -1
  34. package/dist/coverage/lcov-report/test/mocks/index.html +1 -1
  35. package/dist/coverage/lcov-report/test/testUtils.ts.html +1 -1
  36. package/dist/coverage/lcov.info +287 -133
  37. package/dist/interfaces/response/AreaResponse.js +2 -0
  38. package/dist/interfaces/response/EmissionResponse.js +2 -0
  39. package/dist/interfaces/response/EmissionResponseWithDetails.js +2 -0
  40. package/dist/interfaces/response/FactorResponse.js +2 -0
  41. package/dist/interfaces/response/FactorSetResponse.js +2 -0
  42. package/dist/interfaces/response/SearchResponse.js +2 -0
  43. package/dist/interfaces/response/TypeResponse.js +2 -0
  44. package/dist/interfaces/response/UnitResponse.js +2 -0
  45. package/dist/types/Constants.d.ts +22 -0
  46. package/dist/types/api/Calculation.d.ts +59 -16
  47. package/dist/types/api/Factor.d.ts +73 -20
  48. package/dist/types/api/FactorSets.d.ts +3 -2
  49. package/dist/types/api/Fugitive.d.ts +57 -14
  50. package/dist/types/api/Location.d.ts +57 -16
  51. package/dist/types/api/Mobile.d.ts +46 -2
  52. package/dist/types/api/Stationary.d.ts +59 -16
  53. package/dist/types/api/TransportationAndDistribution.d.ts +58 -15
  54. package/dist/types/index.d.ts +8 -0
  55. package/dist/types/interfaces/response/AreaResponse.d.ts +20 -0
  56. package/dist/types/interfaces/response/EmissionResponse.d.ts +31 -0
  57. package/dist/types/interfaces/response/EmissionResponseWithDetails.d.ts +144 -0
  58. package/dist/types/interfaces/response/FactorResponse.d.ts +53 -0
  59. package/dist/types/interfaces/response/FactorSetResponse.d.ts +38 -0
  60. package/dist/types/interfaces/response/SearchResponse.d.ts +41 -0
  61. package/dist/types/interfaces/response/TypeResponse.d.ts +7 -0
  62. package/dist/types/interfaces/response/UnitResponse.d.ts +7 -0
  63. package/docs/.nojekyll +0 -0
  64. package/docs/_sources/authentication.rst.txt +164 -0
  65. package/docs/_sources/client.rst.txt +137 -0
  66. package/docs/_sources/getting_started.rst.txt +164 -0
  67. package/docs/_sources/index.rst.txt +30 -0
  68. package/docs/_sources/reference.rst.txt +129 -0
  69. package/docs/_sources/sdk.rst.txt +13 -0
  70. package/docs/_sources/troubleshooting.rst.txt +486 -0
  71. package/docs/_static/basic.css +914 -0
  72. package/docs/_static/custom.css +16 -0
  73. package/docs/_static/debug.css +69 -0
  74. package/docs/_static/doctools.js +149 -0
  75. package/docs/_static/documentation_options.js +13 -0
  76. package/docs/_static/file.png +0 -0
  77. package/docs/_static/language_data.js +192 -0
  78. package/docs/_static/minus.png +0 -0
  79. package/docs/_static/plus.png +0 -0
  80. package/docs/_static/pygments.css +232 -0
  81. package/docs/_static/scripts/furo-extensions.js +0 -0
  82. package/docs/_static/scripts/furo.js +3 -0
  83. package/docs/_static/scripts/furo.js.LICENSE.txt +7 -0
  84. package/docs/_static/scripts/furo.js.map +1 -0
  85. package/docs/_static/searchtools.js +632 -0
  86. package/docs/_static/skeleton.css +296 -0
  87. package/docs/_static/sphinx_highlight.js +154 -0
  88. package/docs/_static/sphinx_js.css +0 -0
  89. package/docs/_static/styles/furo-extensions.css +2 -0
  90. package/docs/_static/styles/furo-extensions.css.map +1 -0
  91. package/docs/_static/styles/furo.css +2 -0
  92. package/docs/_static/styles/furo.css.map +1 -0
  93. package/docs/authentication.html +523 -0
  94. package/docs/client.html +477 -0
  95. package/docs/genindex.html +492 -0
  96. package/docs/getting_started.html +491 -0
  97. package/docs/index.html +357 -0
  98. package/docs/objects.inv +0 -0
  99. package/docs/reference.html +1584 -0
  100. package/docs/sdk.html +334 -0
  101. package/docs/search.html +297 -0
  102. package/docs/searchindex.js +1 -0
  103. package/docs/troubleshooting.html +786 -0
  104. package/package.json +1 -1
  105. package/sphinx-build/Makefile +49 -0
  106. package/sphinx-build/requirements.txt +5 -0
  107. package/sphinx-build/source/_static/custom.css +16 -0
  108. package/sphinx-build/source/authentication.rst +164 -0
  109. package/sphinx-build/source/client.rst +137 -0
  110. package/sphinx-build/source/conf.py +56 -0
  111. package/sphinx-build/source/getting_started.rst +164 -0
  112. package/sphinx-build/source/index.rst +30 -0
  113. package/sphinx-build/source/reference.rst +129 -0
  114. package/sphinx-build/source/sdk.rst +13 -0
  115. package/sphinx-build/source/troubleshooting.rst +486 -0
  116. package/src/Constants.ts +26 -0
  117. package/src/api/Calculation.ts +94 -20
  118. package/src/api/Factor.ts +122 -28
  119. package/src/api/FactorSets.ts +4 -3
  120. package/src/api/Fugitive.ts +93 -18
  121. package/src/api/Location.ts +94 -20
  122. package/src/api/Mobile.ts +83 -6
  123. package/src/api/Stationary.ts +95 -20
  124. package/src/api/TransportationAndDistribution.ts +95 -19
  125. package/src/index.ts +33 -1
  126. package/src/interfaces/response/AreaResponse.ts +25 -0
  127. package/src/interfaces/response/EmissionResponse.ts +44 -0
  128. package/src/interfaces/response/EmissionResponseWithDetails.ts +183 -0
  129. package/src/interfaces/response/FactorResponse.ts +78 -0
  130. package/src/interfaces/response/FactorSetResponse.ts +52 -0
  131. package/src/interfaces/response/SearchResponse.ts +52 -0
  132. package/src/interfaces/response/TypeResponse.ts +7 -0
  133. package/src/interfaces/response/UnitResponse.ts +7 -0
  134. package/test/apiTest.test.ts +173 -6
  135. package/dist/api/Factors.js +0 -98
  136. package/dist/api/FugitiveEmission.js +0 -40
  137. package/dist/api/GenericCalculation.js +0 -41
  138. package/dist/api/LocationEmission.js +0 -41
  139. package/dist/api/MobileEmission.js +0 -41
  140. package/dist/api/StationaryEmission.js +0 -41
  141. package/dist/api/TransportationDistributionEmission.js +0 -40
  142. package/dist/coverage/lcov-report/src/api/Factors.ts.html +0 -403
  143. package/dist/coverage/lcov-report/src/api/FugitiveEmission.ts.html +0 -214
  144. package/dist/coverage/lcov-report/src/api/GenericCalculation.ts.html +0 -217
  145. package/dist/coverage/lcov-report/src/api/LocationEmission.ts.html +0 -214
  146. package/dist/coverage/lcov-report/src/api/MobileEmission.ts.html +0 -220
  147. package/dist/coverage/lcov-report/src/api/StationaryEmission.ts.html +0 -220
  148. package/dist/coverage/lcov-report/src/api/TransportationDistributionEmission.ts.html +0 -211
  149. package/dist/types/api/Factors.d.ts +0 -68
  150. package/dist/types/api/FugitiveEmission.d.ts +0 -27
  151. package/dist/types/api/GenericCalculation.d.ts +0 -28
  152. package/dist/types/api/LocationEmission.d.ts +0 -28
  153. package/dist/types/api/MobileEmission.d.ts +0 -28
  154. package/dist/types/api/StationaryEmission.d.ts +0 -28
  155. package/dist/types/api/TransportationDistributionEmission.d.ts +0 -27
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Interface representing a location with geographical and power grid information
3
+ */
4
+ export interface Location {
5
+ /** ISO 3166-1 alpha-3 country code */
6
+ alpha3: string;
7
+ /** Full country name */
8
+ countryName: string;
9
+ /** List of state or province names (optional) */
10
+ stateProvinces?: string[];
11
+ /** List of power grid identifiers (optional) */
12
+ powerGrids?: string[];
13
+ }
14
+ /**
15
+ * Interface representing the response from the area/location API
16
+ */
17
+ export interface AreaResponse {
18
+ /** Array of location objects */
19
+ locations: Location[];
20
+ }
@@ -0,0 +1,31 @@
1
+ /**
2
+ * Interface representing the emission calculation response
3
+ */
4
+ export interface EmissionResponse {
5
+ /** Unique transaction identifier (required) */
6
+ transactionId: string;
7
+ /** Total CO2 equivalent emissions (required) */
8
+ totalCO2e: number;
9
+ /** CO2 emissions value (optional) */
10
+ CO2?: number;
11
+ /** CH4 (Methane) emissions value (optional) */
12
+ CH4?: number;
13
+ /** N2O (Nitrous Oxide) emissions value (optional) */
14
+ N2O?: number;
15
+ /** Biogenic CO2 emissions value (optional) */
16
+ bioCO2?: number;
17
+ /** Indirect CO2 equivalent emissions value (optional) */
18
+ indirectCO2e?: number;
19
+ /** Hydrofluorocarbon emissions value (optional) */
20
+ HFC?: number;
21
+ /** Perfluorocarbon emissions value (optional) */
22
+ PFC?: number;
23
+ /** Sulfur hexafluoride emissions value (optional) */
24
+ SF6?: number;
25
+ /** Nitrogen trifluoride emissions value (optional) */
26
+ NF3?: number;
27
+ /** Unit of measurement for emissions (required) */
28
+ unit: string;
29
+ /** Description of the emission calculation (required) */
30
+ description: string;
31
+ }
@@ -0,0 +1,144 @@
1
+ import { EmissionResponse } from './EmissionResponse';
2
+ /**
3
+ * Interface for unit information in emission details
4
+ */
5
+ export interface UnitInfo {
6
+ /** Unit name */
7
+ name: string;
8
+ /** Unit description (optional) */
9
+ description?: string;
10
+ }
11
+ /**
12
+ * Interface for area information
13
+ */
14
+ export interface AreaInfo {
15
+ /** Internal name of the area */
16
+ internalName: string;
17
+ /** Standard name of the area */
18
+ standardName: string;
19
+ alpha3?: string;
20
+ }
21
+ /**
22
+ * Interface for area type information
23
+ */
24
+ export interface AreaType {
25
+ /** Type name (e.g., "Planet", "State") */
26
+ name: string;
27
+ }
28
+ /**
29
+ * Interface for factor data type categories
30
+ */
31
+ export interface FactorDataTypeCategory {
32
+ /** Category name */
33
+ name: string;
34
+ }
35
+ /**
36
+ * Interface for factor set information
37
+ */
38
+ export interface FactorSetInfo {
39
+ /** Factor set type */
40
+ type: string;
41
+ /** Description of the factor set */
42
+ description?: string;
43
+ /** Provider of the factor set */
44
+ provider?: string;
45
+ /** Source URL for the factor set */
46
+ sourceUrl?: string;
47
+ factorSource?: string;
48
+ }
49
+ export interface FactorSelectionDefault {
50
+ actorDataType?: string;
51
+ areaName?: string;
52
+ factorSetName?: string;
53
+ }
54
+ /**
55
+ * Interface for factor data type code
56
+ */
57
+ export interface FactorDataTypeCode {
58
+ /** Unit of measure */
59
+ unitOfMeasure: string;
60
+ /** Activity type */
61
+ type: string;
62
+ /** Activity subtype */
63
+ subtype: string;
64
+ /** Unique factor identifier */
65
+ factorId: number;
66
+ }
67
+ /**
68
+ * Interface for factor set version information
69
+ */
70
+ export interface FactorSetVersion {
71
+ /** Publication start date */
72
+ publishedFrom?: string;
73
+ /** Publication end date (optional) */
74
+ publishedTo?: string;
75
+ /** Source of the factor */
76
+ factorSource: string;
77
+ }
78
+ /**
79
+ * Interface for factor value
80
+ */
81
+ export interface FactorValue {
82
+ /** Unit information */
83
+ unit: {
84
+ /** Unit name */
85
+ name: string;
86
+ };
87
+ /** Factor value */
88
+ value: number;
89
+ }
90
+ /**
91
+ * Interface for factor metadata
92
+ */
93
+ export interface FactorMetaData {
94
+ /** Metadata key */
95
+ key: string;
96
+ /** Metadata value */
97
+ value: string;
98
+ }
99
+ /**
100
+ * Interface for unit conversion information
101
+ */
102
+ export interface UnitConversion {
103
+ /** Input unit */
104
+ inputUnit: string;
105
+ /** Unit from database */
106
+ unitFromDb: string;
107
+ /** Conversion ratio */
108
+ conversionRatio: string;
109
+ }
110
+ /**
111
+ * Interface for detailed emission data
112
+ */
113
+ export interface EmissionData {
114
+ /** Array of unit information */
115
+ unit: UnitInfo[];
116
+ /** Array of area information */
117
+ area: AreaInfo[];
118
+ /** Array of area types */
119
+ areaType: AreaType[];
120
+ /** Array of factor data type categories */
121
+ factorDataTypeCategories: FactorDataTypeCategory[];
122
+ factorSelectionDefault?: FactorSelectionDefault[];
123
+ /** Array of factor set information */
124
+ factorSet: FactorSetInfo[];
125
+ /** Array of factor data type codes */
126
+ factorDataTypeCode: FactorDataTypeCode[];
127
+ /** Array of factor set versions */
128
+ factorSetVersion: FactorSetVersion[];
129
+ /** Array of factor values */
130
+ factorValue: FactorValue[];
131
+ /** Array of factor metadata */
132
+ factorMetaData: FactorMetaData[];
133
+ /** Array of unit conversions */
134
+ unitConversion: UnitConversion[];
135
+ }
136
+ /**
137
+ * Interface representing the emission calculation response with detailed information
138
+ */
139
+ export interface EmissionResponseWithDetails {
140
+ /** Emission calculation result */
141
+ result: EmissionResponse;
142
+ /** Detailed emission data */
143
+ data: EmissionData;
144
+ }
@@ -0,0 +1,53 @@
1
+ /**
2
+ * Interface representing the response from the retrieve factor API
3
+ */
4
+ export interface FactorResponse {
5
+ /** Unique transaction identifier */
6
+ transactionId: string;
7
+ /** Total CO2 equivalent emissions (optional - not present in all responses)*/
8
+ totalCO2e?: number;
9
+ /** CO2 emissions value (optional - not present in all responses) */
10
+ CO2?: number;
11
+ /** CH4 (Methane) emissions value (optional - not present in all responses) */
12
+ CH4?: number;
13
+ /** N2O (Nitrous Oxide) emissions value (optional - not present in all responses) */
14
+ N2O?: number;
15
+ /** Biogenic CO2 emissions value (optional - not present in all responses) */
16
+ bioCO2?: number;
17
+ /** Indirect CO2 equivalent emissions value (optional - not present in all responses) */
18
+ indirectCO2e?: number;
19
+ /** Hydrofluorocarbon emissions value (optional - not present in all responses) */
20
+ HFC?: number;
21
+ /** Perfluorocarbon emissions value (optional - not present in all responses) */
22
+ PFC?: number;
23
+ /** Sulfur hexafluoride emissions value (optional - not present in all responses) */
24
+ SF6?: number;
25
+ /** Nitrogen trifluoride emissions value (optional - not present in all responses) */
26
+ NF3?: number;
27
+ /** Unit of measurement for emissions */
28
+ unit?: string;
29
+ /** Description of the emission factors (optional)*/
30
+ description?: string;
31
+ /** Factor set identifier (e.g., "IPCC", "IGES") */
32
+ factorSet: string;
33
+ /** Source reference for the emission factors (optional)*/
34
+ source?: string;
35
+ /** Type of activity (e.g., "Gas/Diesel Oil", "Electricity") */
36
+ activityType: string;
37
+ /** Array of supported activity units */
38
+ activityUnit: string[];
39
+ /** Name of the emission factor */
40
+ name?: string;
41
+ /** Publication start date in ISO format (optional)*/
42
+ publishedFrom?: string;
43
+ /** Publication end date in ISO format (optional) */
44
+ publishedTo?: string;
45
+ /** Effective start date in ISO format (optional) */
46
+ effectiveFrom?: string;
47
+ /** Effective end date in ISO format (optional) */
48
+ effectiveTo?: string;
49
+ /** Geographic region (e.g., "Earth", "India") */
50
+ region: string;
51
+ /** Unique factor identifier */
52
+ factorId: number;
53
+ }
@@ -0,0 +1,38 @@
1
+ /**
2
+ * Interface for individual factor set within the response
3
+ */
4
+ export interface FactorSet {
5
+ /** Unique identifier for the factor set (required) */
6
+ id: number;
7
+ /** Name of the factor set (required) */
8
+ name: string;
9
+ /** License type for the factor set (required, e.g., "Public") */
10
+ license: string;
11
+ /** Number of factors in this set (required) */
12
+ factorCount: number;
13
+ /** Source URL for the factor set data */
14
+ sourceUrl?: string;
15
+ /** Detailed description of the factor set */
16
+ description?: string;
17
+ /** Short description of the factor set */
18
+ descriptionShort?: string;
19
+ /** Array of emission factor types included in this set */
20
+ types?: string[];
21
+ /** Array of regions/countries covered by this factor set */
22
+ regions?: string[];
23
+ /** Array of version years available for this factor set */
24
+ versions?: string[];
25
+ /** Provider/publisher of the factor set */
26
+ provider?: string;
27
+ /** Optional citation information for the factor set */
28
+ citation?: string;
29
+ }
30
+ /**
31
+ * Interface for Factor Set API response
32
+ */
33
+ export interface FactorSetResponse {
34
+ /** Transaction ID for the API request */
35
+ transactionId: string;
36
+ /** Array of factor sets returned in the response */
37
+ factorSets: FactorSet[];
38
+ }
@@ -0,0 +1,41 @@
1
+ import { FactorResponse } from './FactorResponse';
2
+ /**
3
+ * Pagination parameters for search requests
4
+ */
5
+ export interface Pagination {
6
+ /** Page number */
7
+ page: number;
8
+ /** Page size */
9
+ size: number;
10
+ }
11
+ /**
12
+ * Request body structure for pagination links
13
+ */
14
+ export interface PaginationBody {
15
+ /** Pagination parameters */
16
+ pagination?: Pagination;
17
+ }
18
+ /**
19
+ * Link object for pagination in search results
20
+ */
21
+ export interface SearchLink {
22
+ /** Relationship type (e.g., "next", "prev") */
23
+ rel: string;
24
+ /** URL for the link */
25
+ href: string;
26
+ /** Content type */
27
+ type: string;
28
+ /** HTTP method */
29
+ method: string;
30
+ /** Request body for pagination */
31
+ body?: PaginationBody;
32
+ }
33
+ /**
34
+ * Interface representing the response from the search factor API
35
+ */
36
+ export interface SearchResponse {
37
+ /** Array of factor results */
38
+ factors: FactorResponse[];
39
+ /** Pagination links (optional) */
40
+ links?: SearchLink[];
41
+ }
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Interface for type metadata API
3
+ */
4
+ export interface TypeResponse {
5
+ /** Array of string*/
6
+ types: string[];
7
+ }
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Interface for /units metadata API
3
+ */
4
+ export interface UnitResponse {
5
+ /** Array of string*/
6
+ units: string[];
7
+ }
package/docs/.nojekyll ADDED
File without changes
@@ -0,0 +1,164 @@
1
+ ==============
2
+ Authentication
3
+ ==============
4
+
5
+ Overview
6
+ --------
7
+
8
+ The IBM Envizi - Emissions API Node.js SDK uses OAuth 2.0 Bearer Tokens for authentication. This page explains the authentication process, options, and security best practices in detail.
9
+
10
+ Authentication Methods
11
+ ----------------------
12
+
13
+ The SDK provides two authentication methods:
14
+
15
+ API Key Authentication (Recommended)
16
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
17
+
18
+ This method automatically handles token generation and refreshing:
19
+
20
+ .. code-block:: javascript
21
+
22
+ import { Client } from 'emissions-api-sdk';
23
+
24
+ await Client.getClient({
25
+ apiKey: process.env.ENVIZI_API_KEY,
26
+ clientId: process.env.ENVIZI_CLIENT_ID,
27
+ orgId: process.env.ENVIZI_ORG_ID
28
+ });
29
+
30
+ **Required parameters:**
31
+ - ``apiKey``: Your API key for authentication
32
+ - ``clientId``: Your client identifier
33
+ - ``orgId``: Your organization identifier
34
+
35
+ Token retrieval
36
+ ^^^^^^^^^^^^^^^
37
+ When you call ``Client.getClient()`` with ``apiKey``:
38
+
39
+ - **Method**: ``GET`` to the token endpoint (default: ``https://api.ibm.com/saascore/run/authentication-retrieve/api-key``)
40
+ - **Headers**:
41
+ - ``X-Api-Key: <apiKey>``
42
+ - ``X-IBM-Client-Id: saascore-<clientId>``
43
+ - ``Accept: application/json``
44
+ - **Query param**: ``orgId=<orgId>``
45
+ - **Response**: a bearer token (string) which the SDK trims, caches, and uses for API calls.
46
+
47
+ Pre-generated Token Authentication
48
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
49
+
50
+ For scenarios where token generation is handled externally:
51
+
52
+ .. code-block:: javascript
53
+
54
+ import { Client } from 'emissions-api-sdk';
55
+
56
+ await Client.getClient({
57
+ token: process.env.JWT_TOKEN,
58
+ clientId: process.env.ENVIZI_CLIENT_ID
59
+ });
60
+
61
+ **Required parameters:**
62
+ - ``token``: A pre-generated authentication token
63
+ - ``clientId``: Your client identifier
64
+
65
+ Token lifetime & refresh
66
+ ^^^^^^^^^^^^^^^^^^^^^^^^
67
+ - The SDK decodes the JWT’s ``exp`` and stores it.
68
+ - In user-provided token mode, no refresh occurs; re-initialize with a fresh token when needed.
69
+
70
+ Authentication Flow
71
+ -------------------
72
+
73
+ The authentication process follows these steps:
74
+
75
+ 1. **Initialization**:
76
+ - User provides authentication credentials
77
+ - Client validates the credentials format
78
+
79
+ 2. **Token Generation** (API Key method):
80
+ - Client sends API key to authentication service
81
+ - Authentication service validates credentials
82
+ - Authentication service returns a token with expiration time
83
+ - Client stores the token securely
84
+
85
+ 3. **Token Validation**:
86
+ - Before each API request, token validity is checked
87
+ - If valid, the token is included in the request
88
+ - If expired or near expiration, token is refreshed
89
+
90
+ 4. **Token Refreshing**:
91
+ - Client detects when token is approaching expiration
92
+ - Client automatically requests a new token
93
+ - New token replaces the old one for future requests
94
+
95
+ Authentication Headers
96
+ ----------------------
97
+
98
+ The SDK automatically includes the necessary headers for authentication:
99
+
100
+ For token generation:
101
+ - ``X-Api-Key``: Your API key
102
+ - ``X-IBM-Client-Id``: saascore-{your-client-id}
103
+
104
+ For API requests:
105
+ - ``Authorization``: Bearer {token}
106
+ - ``X-IBM-Client-Id``: ghgemissions-{your-client-id}
107
+ - ``X-Client-Source``: node-sdk
108
+
109
+ Architecture Diagram
110
+ --------------------
111
+
112
+ ::
113
+
114
+ +-----------------+ +-------------------------+
115
+ | Your application| | Token Service (GET) |
116
+ | (init once) | | authUrl / default URL |
117
+ +--------+--------+ +-----------+-------------+
118
+ | ^
119
+ | getClient({apiKey, clientId, | (managed mode only:
120
+ | orgId, [authUrl], [host]}) | X-Api-Key, saascore-Client-Id,
121
+ v | orgId => returns JWT)
122
+ +--------+--------------------------------+-------------+
123
+ | Client (singleton) |
124
+ | - token, exp, domain, clientId |
125
+ | - refresh before expiry (managed mode) |
126
+ +--------+--------------------------------+-------------+
127
+ | ^
128
+ | makeApiRequest(...) |
129
+ v |
130
+ +--------+--------------------------------+-------------+
131
+ | Request layer (axios) |
132
+ | Adds headers: |
133
+ | - Authorization: Bearer <token> |
134
+ | - X-IBM-Client-Id: ghgemissions-<clientId> |
135
+ | - X-Client-Source: excel | node-sdk |
136
+ +--------+--------------------------------+-------------+
137
+ |
138
+ v
139
+ +--------+--------------------------------+-------------+
140
+ | Emissions API (host/domain) |
141
+ +-------------------------------------------------------+
142
+
143
+
144
+ Security Best Practices
145
+ -----------------------
146
+
147
+ 1. **Use environment variables** for credentials:
148
+
149
+ .. code-block:: javascript
150
+
151
+ await Client.getClient({
152
+ apiKey: process.env.ENVIZI_API_KEY,
153
+ clientId: process.env.ENVIZI_CLIENT_ID,
154
+ orgId: process.env.ENVIZI_ORG_ID
155
+ });
156
+
157
+ 2. **Never hardcode** credentials in your application code
158
+ 3. **Implement proper access controls** for API keys
159
+ 4. **Use secure environment variables** for production deployments
160
+ 5. **Implement least privilege** principles for API access
161
+
162
+
163
+ For more information about the Client that manages authentication, see the :doc:`client` page.
164
+
@@ -0,0 +1,137 @@
1
+ ======
2
+ Client
3
+ ======
4
+
5
+ Overview
6
+ --------
7
+
8
+ The Client is the central component of the IBM Envizi - Emissions API Node.js SDK. It handles authentication, maintains connections to the API, and provides a unified interface for all API operations.
9
+
10
+ .. code-block:: none
11
+
12
+ +----------------------+
13
+ | Application |<-----------+
14
+ +----------------------+ |
15
+ | |
16
+ v |
17
+ +----------------------+ |
18
+ | Client |<-----------+
19
+ +----------------------+ |
20
+ | |
21
+ v |
22
+ +----------------------+ +----------------------+
23
+ | Authentication |---->| API Module Requests |
24
+ +----------------------+ +----------------------+
25
+
26
+ Client Initialization
27
+ ---------------------
28
+
29
+ The Client is designed as a singleton to ensure consistent authentication across your application. Initialize it once at application startup:
30
+
31
+ 1. Managed token (SDK fetches & refreshes)
32
+
33
+ .. code-block:: javascript
34
+
35
+ import { Client } from 'emissions-api-sdk';
36
+
37
+ await Client.getClient({
38
+ apiKey: process.env.ENVIZI_API_KEY,
39
+ clientId: process.env.ENVIZI_CLIENT_ID,
40
+ orgId: process.env.ENVIZI_ORG_ID
41
+ });
42
+
43
+ 2. User-provided token (SDK will **not** refresh it)
44
+
45
+ .. code-block:: javascript
46
+
47
+ import { Client } from "emissions-api-sdk";
48
+
49
+ await Client.getClient({
50
+ clientId: process.env.ENVIZI_CLIENT_ID,
51
+ token: process.env.TOKEN
52
+ });
53
+
54
+ Configuration Options
55
+ ---------------------
56
+
57
+ The Client accepts the following configuration parameters:
58
+
59
+ .. list-table::
60
+ :header-rows: 1
61
+ :widths: 20 15 65
62
+
63
+ * - Parameter
64
+ - Required
65
+ - Description
66
+ * - apiKey
67
+ - Yes*
68
+ - API key for authentication (required unless using token)
69
+ * - clientId
70
+ - Yes
71
+ - Client ID for API access
72
+ * - orgId
73
+ - Yes*
74
+ - Organization ID (required with apiKey)
75
+ * - token
76
+ - Yes*
77
+ - Pre-generated token (required if not using apiKey)
78
+ * - host
79
+ - No
80
+ - Custom API endpoint (defaults to production)
81
+ * - authUrl
82
+ - No
83
+ - Custom authentication endpoint (defaults to production)
84
+
85
+ .. note::
86
+ Either ``apiKey+orgId`` OR ``token`` is required
87
+
88
+ Client Responsibilities
89
+ -----------------------
90
+
91
+ The Client handles several key responsibilities:
92
+
93
+ 1. **Singleton Management**: Ensures only one Client instance exists
94
+ 2. **Configuration Validation**: Verifies required parameters
95
+ 3. **Token Generation**: Obtains tokens from the authentication service
96
+ 4. **Token Refreshing**: Automatically refreshes tokens before expiration
97
+ 5. **Request Authentication**: Adds proper headers to all API requests
98
+
99
+ Error Handling
100
+ --------------
101
+
102
+ Handle authentication errors gracefully:
103
+
104
+ .. code-block:: javascript
105
+
106
+ try {
107
+ await Client.getClient({
108
+ apiKey: process.env.ENVIZI_API_KEY,
109
+ clientId: process.env.ENVIZI_CLIENT_ID,
110
+ orgId: process.env.ENVIZI_ORG_ID
111
+ });
112
+ } catch (error) {
113
+ console.error("Authentication error:", error.message);
114
+ // Implement appropriate error handling
115
+ if (error.response) {
116
+ // Handle specific HTTP error responses
117
+ console.error("Status:", error.response.status);
118
+ console.error("Details:", error.response.data);
119
+ }
120
+ }
121
+
122
+
123
+ Best Practices
124
+ --------------
125
+
126
+ For optimal Client usage:
127
+
128
+ 1. **Initialize once** at application startup
129
+ 2. **Store credentials securely** using environment variables
130
+ 3. **Implement proper error handling** for authentication failures
131
+ 4. **Let the SDK handle** token refreshing automatically
132
+ 5. **Reuse the Client instance** across your application
133
+
134
+ Authentication Integration
135
+ --------------------------
136
+
137
+ The Client works closely with the Authentication module to manage tokens. For detailed information about the authentication process, supported methods, and security best practices, see the :doc:`authentication` page.