oilpriceapi 0.3.0 → 0.3.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.
- package/README.md +54 -68
- package/dist/client.js +5 -2
- package/dist/types.d.ts +1 -1
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -51,13 +51,12 @@ const prices = await client.getLatestPrices();
|
|
|
51
51
|
// [
|
|
52
52
|
// {
|
|
53
53
|
// code: 'WTI_USD',
|
|
54
|
-
//
|
|
55
|
-
//
|
|
54
|
+
// price: 74.25,
|
|
55
|
+
// formatted: '$74.25',
|
|
56
56
|
// currency: 'USD',
|
|
57
|
-
//
|
|
58
|
-
// timestamp: '2024-11-24T12:00:00Z',
|
|
57
|
+
// type: 'spot_price',
|
|
59
58
|
// created_at: '2024-11-24T12:01:00Z',
|
|
60
|
-
//
|
|
59
|
+
// source: 'oilprice.investing_com'
|
|
61
60
|
// },
|
|
62
61
|
// // ... more prices
|
|
63
62
|
// ]
|
|
@@ -68,11 +67,11 @@ const prices = await client.getLatestPrices();
|
|
|
68
67
|
```typescript
|
|
69
68
|
// Get only WTI crude oil price
|
|
70
69
|
const wti = await client.getLatestPrices({ commodity: 'WTI_USD' });
|
|
71
|
-
console.log(`WTI:
|
|
70
|
+
console.log(`WTI: ${wti[0].formatted} per barrel`);
|
|
72
71
|
|
|
73
72
|
// Get only Brent crude price
|
|
74
73
|
const brent = await client.getLatestPrices({ commodity: 'BRENT_CRUDE_USD' });
|
|
75
|
-
console.log(`Brent:
|
|
74
|
+
console.log(`Brent: ${brent[0].formatted} per barrel`);
|
|
76
75
|
```
|
|
77
76
|
|
|
78
77
|
### Get Historical Prices (Past Week)
|
|
@@ -171,81 +170,47 @@ const prices = await client.getLatestPrices();
|
|
|
171
170
|
// [OilPriceAPI 2024-11-24T20:28:23.394Z] Response data received { status: 'success', hasData: true }
|
|
172
171
|
```
|
|
173
172
|
|
|
174
|
-
##
|
|
173
|
+
## Examples
|
|
175
174
|
|
|
176
|
-
|
|
175
|
+
See the [`examples/`](./examples) directory for complete, runnable code examples:
|
|
177
176
|
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
177
|
+
- **[basic.ts](./examples/basic.ts)** - Simple usage patterns
|
|
178
|
+
- **[express.ts](./examples/express.ts)** - Express.js API server integration
|
|
179
|
+
- **[nextjs-api-route.ts](./examples/nextjs-api-route.ts)** - Next.js API route handler
|
|
180
|
+
- **[error-handling.ts](./examples/error-handling.ts)** - Comprehensive error handling
|
|
181
|
+
- **[commodities.ts](./examples/commodities.ts)** - Working with commodity metadata
|
|
181
182
|
|
|
182
|
-
|
|
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
|
-
});
|
|
183
|
+
### Quick Examples
|
|
198
184
|
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
});
|
|
185
|
+
**Get Latest Price:**
|
|
186
|
+
```typescript
|
|
187
|
+
const prices = await client.getLatestPrices({ commodity: 'WTI_USD' });
|
|
188
|
+
console.log(prices[0].formatted); // "$74.25"
|
|
202
189
|
```
|
|
203
190
|
|
|
204
|
-
|
|
205
|
-
|
|
191
|
+
**Historical Data:**
|
|
206
192
|
```typescript
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
const client = new OilPriceAPI({
|
|
211
|
-
apiKey: process.env.OIL_PRICE_API_KEY
|
|
193
|
+
const history = await client.getHistoricalPrices({
|
|
194
|
+
commodity: 'BRENT_CRUDE_USD',
|
|
195
|
+
period: 'past_week'
|
|
212
196
|
});
|
|
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
197
|
```
|
|
226
198
|
|
|
227
|
-
|
|
228
|
-
|
|
199
|
+
**Commodity Metadata:**
|
|
229
200
|
```typescript
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
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}`);
|
|
201
|
+
const { commodities } = await client.getCommodities();
|
|
202
|
+
console.log(`Found ${commodities.length} commodities`);
|
|
203
|
+
```
|
|
239
204
|
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
205
|
+
**Error Handling:**
|
|
206
|
+
```typescript
|
|
207
|
+
try {
|
|
208
|
+
const prices = await client.getLatestPrices();
|
|
209
|
+
} catch (error) {
|
|
210
|
+
if (error instanceof RateLimitError) {
|
|
211
|
+
console.log('Rate limited, retry after:', error.retryAfter);
|
|
243
212
|
}
|
|
244
213
|
}
|
|
245
|
-
|
|
246
|
-
// Check every 5 minutes
|
|
247
|
-
setInterval(checkPrice, 5 * 60 * 1000);
|
|
248
|
-
checkPrice(); // Run immediately
|
|
249
214
|
```
|
|
250
215
|
|
|
251
216
|
## API Reference
|
|
@@ -287,6 +252,27 @@ Get historical prices for a time period.
|
|
|
287
252
|
|
|
288
253
|
**Returns:** `Promise<Price[]>`
|
|
289
254
|
|
|
255
|
+
##### `getCommodities()`
|
|
256
|
+
|
|
257
|
+
Get metadata for all supported commodities.
|
|
258
|
+
|
|
259
|
+
**Returns:** `Promise<CommoditiesResponse>` - Object with `commodities` array
|
|
260
|
+
|
|
261
|
+
##### `getCommodityCategories()`
|
|
262
|
+
|
|
263
|
+
Get all commodity categories with their commodities.
|
|
264
|
+
|
|
265
|
+
**Returns:** `Promise<CategoriesResponse>` - Object with category keys mapped to category objects
|
|
266
|
+
|
|
267
|
+
##### `getCommodity(code)`
|
|
268
|
+
|
|
269
|
+
Get metadata for a specific commodity by code.
|
|
270
|
+
|
|
271
|
+
**Parameters:**
|
|
272
|
+
- `code` (string, required) - Commodity code (e.g., "WTI_USD")
|
|
273
|
+
|
|
274
|
+
**Returns:** `Promise<Commodity>` - Commodity metadata object
|
|
275
|
+
|
|
290
276
|
### Types
|
|
291
277
|
|
|
292
278
|
#### `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 ||
|
|
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,10 @@ 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.
|
|
124
|
+
'User-Agent': 'oilpriceapi-node/0.3.2',
|
|
125
|
+
'X-SDK-Language': 'javascript',
|
|
126
|
+
'X-SDK-Version': '0.3.2',
|
|
127
|
+
'X-Client-Type': 'sdk',
|
|
125
128
|
},
|
|
126
129
|
signal: controller.signal,
|
|
127
130
|
});
|
package/dist/types.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "oilpriceapi",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.2",
|
|
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": {
|