oilpriceapi 0.3.1 → 0.5.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.
@@ -0,0 +1,190 @@
1
+ import type { OilPriceAPI } from '../client.js';
2
+ /**
3
+ * Diesel price data for a specific state or region
4
+ */
5
+ export interface DieselPrice {
6
+ /** State code (e.g., "CA", "TX") */
7
+ state: string;
8
+ /** Average diesel price in USD per gallon */
9
+ price: number;
10
+ /** Currency code (always "USD" for diesel) */
11
+ currency: string;
12
+ /** Unit of measurement (always "gallon") */
13
+ unit: string;
14
+ /** Granularity level (e.g., "state", "national") */
15
+ granularity: string;
16
+ /** Data source (e.g., "EIA") */
17
+ source: string;
18
+ /** ISO 8601 timestamp of last update */
19
+ updated_at: string;
20
+ /** Whether the response was served from cache */
21
+ cached?: boolean;
22
+ }
23
+ /**
24
+ * Diesel station location and pricing data
25
+ */
26
+ export interface DieselStation {
27
+ /** Station name */
28
+ name: string;
29
+ /** Full street address */
30
+ address: string;
31
+ /** Geographic coordinates */
32
+ location: {
33
+ /** Latitude */
34
+ lat: number;
35
+ /** Longitude */
36
+ lng: number;
37
+ };
38
+ /** Diesel price at this station (USD per gallon) */
39
+ diesel_price: number;
40
+ /** Formatted price string (e.g., "$3.89") */
41
+ formatted_price: string;
42
+ /** Currency code (always "USD") */
43
+ currency: string;
44
+ /** Unit (always "gallon") */
45
+ unit: string;
46
+ /** Price difference from regional average (negative = cheaper) */
47
+ price_delta?: number;
48
+ /** Human-readable comparison (e.g., "$0.15 cheaper than regional average") */
49
+ price_vs_average?: string;
50
+ /** Available fuel types at this station */
51
+ fuel_types?: string[];
52
+ /** ISO 8601 timestamp of last price update */
53
+ last_updated?: string;
54
+ }
55
+ /**
56
+ * Response from diesel stations endpoint
57
+ */
58
+ export interface DieselStationsResponse {
59
+ /** Regional average for comparison */
60
+ regional_average: {
61
+ price: number;
62
+ currency: string;
63
+ unit: string;
64
+ region: string;
65
+ granularity: string;
66
+ source: string;
67
+ };
68
+ /** List of nearby stations */
69
+ stations: DieselStation[];
70
+ /** Search area details */
71
+ search_area: {
72
+ center: {
73
+ lat: number;
74
+ lng: number;
75
+ };
76
+ radius_meters: number;
77
+ radius_miles: number;
78
+ };
79
+ /** Metadata about the response */
80
+ metadata: {
81
+ total_stations: number;
82
+ source: string;
83
+ cached: boolean;
84
+ api_cost: number;
85
+ timestamp: string;
86
+ cache_age_hours?: number;
87
+ };
88
+ }
89
+ /**
90
+ * Options for getting diesel stations
91
+ */
92
+ export interface GetDieselStationsOptions {
93
+ /** Latitude */
94
+ lat: number;
95
+ /** Longitude */
96
+ lng: number;
97
+ /** Search radius in meters (default: 8047 = 5 miles) */
98
+ radius?: number;
99
+ }
100
+ /**
101
+ * Diesel Prices resource
102
+ *
103
+ * Provides access to state-level diesel averages and station-level pricing.
104
+ *
105
+ * @example
106
+ * ```typescript
107
+ * // Get state average
108
+ * const caPrice = await client.diesel.getPrice('CA');
109
+ * console.log(`California diesel: $${caPrice.price}/gal`);
110
+ *
111
+ * // Get nearby stations
112
+ * const stations = await client.diesel.getStations({
113
+ * lat: 37.7749,
114
+ * lng: -122.4194,
115
+ * radius: 8047 // 5 miles in meters
116
+ * });
117
+ *
118
+ * console.log(`Found ${stations.stations.length} stations`);
119
+ * stations.stations.forEach(station => {
120
+ * console.log(`${station.name}: ${station.formatted_price}`);
121
+ * });
122
+ * ```
123
+ */
124
+ export declare class DieselResource {
125
+ private client;
126
+ constructor(client: OilPriceAPI);
127
+ /**
128
+ * Get average diesel price for a US state
129
+ *
130
+ * Returns EIA state-level average diesel price. This endpoint is free
131
+ * and included in all tiers.
132
+ *
133
+ * @param state - Two-letter US state code (e.g., "CA", "TX", "NY")
134
+ * @returns State average diesel price
135
+ *
136
+ * @throws {NotFoundError} If state code is invalid
137
+ * @throws {AuthenticationError} If API key is invalid
138
+ * @throws {RateLimitError} If rate limit exceeded
139
+ *
140
+ * @example
141
+ * ```typescript
142
+ * // Get California diesel price
143
+ * const caPrice = await client.diesel.getPrice('CA');
144
+ * console.log(`CA diesel: $${caPrice.price}/gal`);
145
+ * console.log(`Source: ${caPrice.source}`);
146
+ * console.log(`Updated: ${caPrice.updated_at}`);
147
+ * ```
148
+ */
149
+ getPrice(state: string): Promise<DieselPrice>;
150
+ /**
151
+ * Get nearby diesel stations with current pricing
152
+ *
153
+ * Returns station-level diesel prices within specified radius using Google Maps data.
154
+ *
155
+ * **Pricing:** Included in paid tiers:
156
+ * - Exploration: 100 station queries/month
157
+ * - Starter: 500 station queries/month
158
+ * - Professional: 2,000 station queries/month
159
+ * - Business: 5,000 station queries/month
160
+ *
161
+ * **Caching:** Results are cached for 24 hours to minimize costs.
162
+ *
163
+ * @param options - Search parameters (lat, lng, radius)
164
+ * @returns Nearby stations with prices and regional average
165
+ *
166
+ * @throws {AuthenticationError} If API key is invalid
167
+ * @throws {RateLimitError} If monthly station query limit exceeded
168
+ * @throws {Error} If coordinates are invalid
169
+ *
170
+ * @example
171
+ * ```typescript
172
+ * // Get stations near San Francisco
173
+ * const result = await client.diesel.getStations({
174
+ * lat: 37.7749,
175
+ * lng: -122.4194,
176
+ * radius: 8047 // 5 miles
177
+ * });
178
+ *
179
+ * console.log(`Regional avg: $${result.regional_average.price}/gal`);
180
+ * console.log(`Found ${result.stations.length} stations`);
181
+ *
182
+ * // Find cheapest station
183
+ * const cheapest = result.stations.reduce((min, s) =>
184
+ * s.diesel_price < min.diesel_price ? s : min
185
+ * );
186
+ * console.log(`Cheapest: ${cheapest.name} at ${cheapest.formatted_price}`);
187
+ * ```
188
+ */
189
+ getStations(options: GetDieselStationsOptions): Promise<DieselStationsResponse>;
190
+ }
@@ -0,0 +1,144 @@
1
+ /**
2
+ * Diesel Prices resource
3
+ *
4
+ * Provides access to state-level diesel averages and station-level pricing.
5
+ *
6
+ * @example
7
+ * ```typescript
8
+ * // Get state average
9
+ * const caPrice = await client.diesel.getPrice('CA');
10
+ * console.log(`California diesel: $${caPrice.price}/gal`);
11
+ *
12
+ * // Get nearby stations
13
+ * const stations = await client.diesel.getStations({
14
+ * lat: 37.7749,
15
+ * lng: -122.4194,
16
+ * radius: 8047 // 5 miles in meters
17
+ * });
18
+ *
19
+ * console.log(`Found ${stations.stations.length} stations`);
20
+ * stations.stations.forEach(station => {
21
+ * console.log(`${station.name}: ${station.formatted_price}`);
22
+ * });
23
+ * ```
24
+ */
25
+ export class DieselResource {
26
+ constructor(client) {
27
+ this.client = client;
28
+ }
29
+ /**
30
+ * Get average diesel price for a US state
31
+ *
32
+ * Returns EIA state-level average diesel price. This endpoint is free
33
+ * and included in all tiers.
34
+ *
35
+ * @param state - Two-letter US state code (e.g., "CA", "TX", "NY")
36
+ * @returns State average diesel price
37
+ *
38
+ * @throws {NotFoundError} If state code is invalid
39
+ * @throws {AuthenticationError} If API key is invalid
40
+ * @throws {RateLimitError} If rate limit exceeded
41
+ *
42
+ * @example
43
+ * ```typescript
44
+ * // Get California diesel price
45
+ * const caPrice = await client.diesel.getPrice('CA');
46
+ * console.log(`CA diesel: $${caPrice.price}/gal`);
47
+ * console.log(`Source: ${caPrice.source}`);
48
+ * console.log(`Updated: ${caPrice.updated_at}`);
49
+ * ```
50
+ */
51
+ async getPrice(state) {
52
+ if (!state || state.length !== 2) {
53
+ throw new Error('State must be a 2-letter US state code (e.g., "CA", "TX")');
54
+ }
55
+ const response = await this.client['request']('/v1/diesel-prices', { state: state.toUpperCase() });
56
+ return response.regional_average;
57
+ }
58
+ /**
59
+ * Get nearby diesel stations with current pricing
60
+ *
61
+ * Returns station-level diesel prices within specified radius using Google Maps data.
62
+ *
63
+ * **Pricing:** Included in paid tiers:
64
+ * - Exploration: 100 station queries/month
65
+ * - Starter: 500 station queries/month
66
+ * - Professional: 2,000 station queries/month
67
+ * - Business: 5,000 station queries/month
68
+ *
69
+ * **Caching:** Results are cached for 24 hours to minimize costs.
70
+ *
71
+ * @param options - Search parameters (lat, lng, radius)
72
+ * @returns Nearby stations with prices and regional average
73
+ *
74
+ * @throws {AuthenticationError} If API key is invalid
75
+ * @throws {RateLimitError} If monthly station query limit exceeded
76
+ * @throws {Error} If coordinates are invalid
77
+ *
78
+ * @example
79
+ * ```typescript
80
+ * // Get stations near San Francisco
81
+ * const result = await client.diesel.getStations({
82
+ * lat: 37.7749,
83
+ * lng: -122.4194,
84
+ * radius: 8047 // 5 miles
85
+ * });
86
+ *
87
+ * console.log(`Regional avg: $${result.regional_average.price}/gal`);
88
+ * console.log(`Found ${result.stations.length} stations`);
89
+ *
90
+ * // Find cheapest station
91
+ * const cheapest = result.stations.reduce((min, s) =>
92
+ * s.diesel_price < min.diesel_price ? s : min
93
+ * );
94
+ * console.log(`Cheapest: ${cheapest.name} at ${cheapest.formatted_price}`);
95
+ * ```
96
+ */
97
+ async getStations(options) {
98
+ const { lat, lng, radius = 8047 } = options;
99
+ // Validate coordinates
100
+ if (lat < -90 || lat > 90) {
101
+ throw new Error('Latitude must be between -90 and 90');
102
+ }
103
+ if (lng < -180 || lng > 180) {
104
+ throw new Error('Longitude must be between -180 and 180');
105
+ }
106
+ if (radius < 0 || radius > 50000) {
107
+ throw new Error('Radius must be between 0 and 50000 meters');
108
+ }
109
+ // Use POST request for stations endpoint
110
+ const response = await fetch(`${this.client['baseUrl']}/v1/diesel-prices/stations`, {
111
+ method: 'POST',
112
+ headers: {
113
+ 'Authorization': `Bearer ${this.client['apiKey']}`,
114
+ 'Content-Type': 'application/json',
115
+ 'User-Agent': 'oilpriceapi-node/0.4.0',
116
+ 'X-SDK-Language': 'javascript',
117
+ 'X-SDK-Version': '0.4.0',
118
+ 'X-Client-Type': 'sdk',
119
+ },
120
+ body: JSON.stringify({ lat, lng, radius }),
121
+ });
122
+ if (!response.ok) {
123
+ const errorBody = await response.text();
124
+ let errorMessage = `HTTP ${response.status}: ${response.statusText}`;
125
+ try {
126
+ const errorJson = JSON.parse(errorBody);
127
+ errorMessage = errorJson.message || errorJson.error || errorMessage;
128
+ }
129
+ catch {
130
+ // Use default error message
131
+ }
132
+ // Handle specific status codes
133
+ if (response.status === 403) {
134
+ throw new Error(`Diesel station queries not available on your plan. ${errorMessage}`);
135
+ }
136
+ if (response.status === 429) {
137
+ throw new Error(`Diesel station query limit exceeded. ${errorMessage}`);
138
+ }
139
+ throw new Error(errorMessage);
140
+ }
141
+ const data = await response.json();
142
+ return data;
143
+ }
144
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "oilpriceapi",
3
- "version": "0.3.1",
3
+ "version": "0.5.0",
4
4
  "description": "Official Node.js SDK for Oil Price API - Real-time and historical oil & commodity prices",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -35,7 +35,10 @@
35
35
  "diesel",
36
36
  "natural-gas",
37
37
  "energy",
38
- "trading"
38
+ "trading",
39
+ "alerts",
40
+ "webhook",
41
+ "notifications"
39
42
  ],
40
43
  "author": "Oil Price API <karl@oilpriceapi.com>",
41
44
  "license": "MIT",