oilpriceapi 0.3.0 → 0.3.1

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/README.md CHANGED
@@ -171,81 +171,47 @@ const prices = await client.getLatestPrices();
171
171
  // [OilPriceAPI 2024-11-24T20:28:23.394Z] Response data received { status: 'success', hasData: true }
172
172
  ```
173
173
 
174
- ## Integration Examples
174
+ ## Examples
175
175
 
176
- ### Express.js
176
+ See the [`examples/`](./examples) directory for complete, runnable code examples:
177
177
 
178
- ```typescript
179
- import express from 'express';
180
- import { OilPriceAPI } from 'oilpriceapi';
178
+ - **[basic.ts](./examples/basic.ts)** - Simple usage patterns
179
+ - **[express.ts](./examples/express.ts)** - Express.js API server integration
180
+ - **[nextjs-api-route.ts](./examples/nextjs-api-route.ts)** - Next.js API route handler
181
+ - **[error-handling.ts](./examples/error-handling.ts)** - Comprehensive error handling
182
+ - **[commodities.ts](./examples/commodities.ts)** - Working with commodity metadata
181
183
 
182
- const app = express();
183
- const oilPrices = new OilPriceAPI({
184
- apiKey: process.env.OIL_PRICE_API_KEY
185
- });
186
-
187
- app.get('/api/prices', async (req, res) => {
188
- try {
189
- const prices = await oilPrices.getLatestPrices();
190
- res.json({ success: true, data: prices });
191
- } catch (error) {
192
- res.status(500).json({
193
- success: false,
194
- error: error.message
195
- });
196
- }
197
- });
184
+ ### Quick Examples
198
185
 
199
- app.listen(3000, () => {
200
- console.log('Server running on port 3000');
201
- });
186
+ **Get Latest Price:**
187
+ ```typescript
188
+ const prices = await client.getLatestPrices({ commodity: 'WTI_USD' });
189
+ console.log(prices[0].formatted); // "$74.25"
202
190
  ```
203
191
 
204
- ### Next.js API Route
205
-
192
+ **Historical Data:**
206
193
  ```typescript
207
- // app/api/prices/route.ts
208
- import { OilPriceAPI } from 'oilpriceapi';
209
-
210
- const client = new OilPriceAPI({
211
- apiKey: process.env.OIL_PRICE_API_KEY
194
+ const history = await client.getHistoricalPrices({
195
+ commodity: 'BRENT_CRUDE_USD',
196
+ period: 'past_week'
212
197
  });
213
-
214
- export async function GET() {
215
- try {
216
- const prices = await client.getLatestPrices();
217
- return Response.json(prices);
218
- } catch (error) {
219
- return Response.json(
220
- { error: error.message },
221
- { status: 500 }
222
- );
223
- }
224
- }
225
198
  ```
226
199
 
227
- ### Price Monitoring Script
228
-
200
+ **Commodity Metadata:**
229
201
  ```typescript
230
- import { OilPriceAPI } from 'oilpriceapi';
231
-
232
- const client = new OilPriceAPI({ apiKey: process.env.OIL_PRICE_API_KEY });
233
-
234
- async function checkPrice() {
235
- const wti = await client.getLatestPrices({ commodity: 'WTI_USD' });
236
- const price = wti[0].value;
237
-
238
- console.log(`Current WTI price: $${price}`);
202
+ const { commodities } = await client.getCommodities();
203
+ console.log(`Found ${commodities.length} commodities`);
204
+ ```
239
205
 
240
- if (price < 70) {
241
- console.log('🚨 ALERT: WTI below $70!');
242
- // Send notification, email, etc.
206
+ **Error Handling:**
207
+ ```typescript
208
+ try {
209
+ const prices = await client.getLatestPrices();
210
+ } catch (error) {
211
+ if (error instanceof RateLimitError) {
212
+ console.log('Rate limited, retry after:', error.retryAfter);
243
213
  }
244
214
  }
245
-
246
- // Check every 5 minutes
247
- setInterval(checkPrice, 5 * 60 * 1000);
248
- checkPrice(); // Run immediately
249
215
  ```
250
216
 
251
217
  ## API Reference
@@ -287,6 +253,27 @@ Get historical prices for a time period.
287
253
 
288
254
  **Returns:** `Promise<Price[]>`
289
255
 
256
+ ##### `getCommodities()`
257
+
258
+ Get metadata for all supported commodities.
259
+
260
+ **Returns:** `Promise<CommoditiesResponse>` - Object with `commodities` array
261
+
262
+ ##### `getCommodityCategories()`
263
+
264
+ Get all commodity categories with their commodities.
265
+
266
+ **Returns:** `Promise<CategoriesResponse>` - Object with category keys mapped to category objects
267
+
268
+ ##### `getCommodity(code)`
269
+
270
+ Get metadata for a specific commodity by code.
271
+
272
+ **Parameters:**
273
+ - `code` (string, required) - Commodity code (e.g., "WTI_USD")
274
+
275
+ **Returns:** `Promise<Commodity>` - Commodity metadata object
276
+
290
277
  ### Types
291
278
 
292
279
  #### `Price`
package/dist/client.js CHANGED
@@ -35,7 +35,7 @@ export class OilPriceAPI {
35
35
  this.retries = config.retries !== undefined ? config.retries : 3;
36
36
  this.retryDelay = config.retryDelay || 1000;
37
37
  this.retryStrategy = config.retryStrategy || 'exponential';
38
- this.timeout = config.timeout || 30000;
38
+ this.timeout = config.timeout || 90000; // 90 seconds for slow historical queries
39
39
  this.debug = config.debug || false;
40
40
  }
41
41
  /**
@@ -121,7 +121,7 @@ export class OilPriceAPI {
121
121
  headers: {
122
122
  'Authorization': `Bearer ${this.apiKey}`,
123
123
  'Content-Type': 'application/json',
124
- 'User-Agent': 'oilpriceapi-node/0.3.0',
124
+ 'User-Agent': 'oilpriceapi-node/0.3.1',
125
125
  },
126
126
  signal: controller.signal,
127
127
  });
package/dist/types.d.ts CHANGED
@@ -32,7 +32,7 @@ export interface OilPriceAPIConfig {
32
32
  retryStrategy?: RetryStrategy;
33
33
  /**
34
34
  * Request timeout in milliseconds
35
- * @default 30000 (30 seconds)
35
+ * @default 90000 (90 seconds)
36
36
  */
37
37
  timeout?: number;
38
38
  /**
package/package.json CHANGED
@@ -1,7 +1,8 @@
1
1
  {
2
2
  "name": "oilpriceapi",
3
- "version": "0.3.0",
3
+ "version": "0.3.1",
4
4
  "description": "Official Node.js SDK for Oil Price API - Real-time and historical oil & commodity prices",
5
+ "type": "module",
5
6
  "main": "./dist/index.js",
6
7
  "types": "./dist/index.d.ts",
7
8
  "exports": {