0.0.27 • Published 4 months ago
eprel-client v0.0.27
Status: Experimental
eprel-client
is a typesafe and straightforward fetch client for interacting with the European Product Registry for Energy Labelling (EPREL) API using feature-fetch
.
📖 Usage
Create a EPREL Client
Use createEPRELClient()
to create a client with your API key.
import { createEPRELClient } from 'eprel-client';
const client = createEPRELClient({
apiKey: 'YOUR_API_KEY'
});
Fetch Available Product Groups
Fetches the available product groups from the ERAP API.
const productGroupsResult = await client.getProductGroups();
const productGroups = productGroupsResult.unwrap();
Error Handling
Errors can occur during API requests, and the client will return detailed error information. Possible error types include:
NetworkError
: Indicates a failure in network communication, such as loss of connectivityRequestError
: Occurs when the server returns a response with a status code indicating an error (e.g., 4xx or 5xx)FetchError
: A general exception type that can encompass other error scenarios not covered byNetworkError
orRequestError
, for example when the response couldn't be parsed, ..
const productGroupsResult = await client.getProductGroups();
// First Approach: Handle error using `isErr()`
if (productGroupsResult.isErr()) {
const { error } = productGroupsResult;
if (error instanceof NetworkError) {
console.error('Network error:', error.message);
} else if (error instanceof RequestError) {
console.error('Request error:', error.message, 'Status:', error.status);
} else if (error instanceof FetchError) {
console.error('Service error:', error.message, 'Code:', error.code);
} else {
console.error('Unexpected error:', error);
}
}
// Second Approach: Unwrap response with `try-catch`
try {
const productGroups = productGroupsResult.unwrap();
} catch (error) {
if (error instanceof NetworkError) {
console.error('Network error:', error.message);
} else if (error instanceof RequestError) {
console.error('Request error:', error.message, 'Status:', error.status);
} else if (error instanceof FetchError) {
console.error('Service error:', error.message, 'Code:', error.code);
} else {
console.error('Unexpected error:', error);
}
}
0.0.27
4 months ago
0.0.24
4 months ago
0.0.25
4 months ago
0.0.26
4 months ago
0.0.22
6 months ago
0.0.23
6 months ago
0.0.21
6 months ago
0.0.20
7 months ago
0.0.19
8 months ago
0.0.18
9 months ago
0.0.17
9 months ago
0.0.16
10 months ago
0.0.15
10 months ago
0.0.14
10 months ago
0.0.13
10 months ago
0.0.12
10 months ago
0.0.11
10 months ago
0.0.10
10 months ago
0.0.9
10 months ago
0.0.8
10 months ago
0.0.7
10 months ago
0.0.6
10 months ago
0.0.5
10 months ago
0.0.4
10 months ago
0.0.3
10 months ago
0.0.2
10 months ago