oilpriceapi 0.2.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 +48 -61
- package/dist/client.d.ts +39 -1
- package/dist/client.js +46 -2
- package/dist/index.d.ts +1 -1
- package/dist/types.d.ts +69 -1
- package/package.json +3 -2
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
|
-
##
|
|
174
|
+
## Examples
|
|
175
175
|
|
|
176
|
-
|
|
176
|
+
See the [`examples/`](./examples) directory for complete, runnable code examples:
|
|
177
177
|
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
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
|
-
|
|
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
|
-
|
|
200
|
-
|
|
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
|
-
|
|
205
|
-
|
|
192
|
+
**Historical Data:**
|
|
206
193
|
```typescript
|
|
207
|
-
|
|
208
|
-
|
|
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
|
-
|
|
228
|
-
|
|
200
|
+
**Commodity Metadata:**
|
|
229
201
|
```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}`);
|
|
202
|
+
const { commodities } = await client.getCommodities();
|
|
203
|
+
console.log(`Found ${commodities.length} commodities`);
|
|
204
|
+
```
|
|
239
205
|
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
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.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { OilPriceAPIConfig, Price, LatestPricesOptions, HistoricalPricesOptions } from './types.js';
|
|
1
|
+
import type { OilPriceAPIConfig, Price, LatestPricesOptions, HistoricalPricesOptions, Commodity, CommoditiesResponse, CategoriesResponse } from './types.js';
|
|
2
2
|
/**
|
|
3
3
|
* Official Node.js client for Oil Price API
|
|
4
4
|
*
|
|
@@ -93,4 +93,42 @@ export declare class OilPriceAPI {
|
|
|
93
93
|
* ```
|
|
94
94
|
*/
|
|
95
95
|
getHistoricalPrices(options?: HistoricalPricesOptions): Promise<Price[]>;
|
|
96
|
+
/**
|
|
97
|
+
* Get metadata for all supported commodities
|
|
98
|
+
*
|
|
99
|
+
* @returns Object containing array of commodities
|
|
100
|
+
*
|
|
101
|
+
* @example
|
|
102
|
+
* ```typescript
|
|
103
|
+
* const response = await client.getCommodities();
|
|
104
|
+
* console.log(response.commodities); // Array of commodity objects
|
|
105
|
+
* ```
|
|
106
|
+
*/
|
|
107
|
+
getCommodities(): Promise<CommoditiesResponse>;
|
|
108
|
+
/**
|
|
109
|
+
* Get all commodity categories with their commodities
|
|
110
|
+
*
|
|
111
|
+
* @returns Object with category keys mapped to category objects
|
|
112
|
+
*
|
|
113
|
+
* @example
|
|
114
|
+
* ```typescript
|
|
115
|
+
* const categories = await client.getCommodityCategories();
|
|
116
|
+
* console.log(categories.oil.name); // "Oil"
|
|
117
|
+
* console.log(categories.oil.commodities.length); // 11
|
|
118
|
+
* ```
|
|
119
|
+
*/
|
|
120
|
+
getCommodityCategories(): Promise<CategoriesResponse>;
|
|
121
|
+
/**
|
|
122
|
+
* Get metadata for a specific commodity by code
|
|
123
|
+
*
|
|
124
|
+
* @param code - Commodity code (e.g., "WTI_USD", "BRENT_CRUDE_USD")
|
|
125
|
+
* @returns Commodity metadata object
|
|
126
|
+
*
|
|
127
|
+
* @example
|
|
128
|
+
* ```typescript
|
|
129
|
+
* const commodity = await client.getCommodity('WTI_USD');
|
|
130
|
+
* console.log(commodity.name); // "WTI Crude Oil"
|
|
131
|
+
* ```
|
|
132
|
+
*/
|
|
133
|
+
getCommodity(code: string): Promise<Commodity>;
|
|
96
134
|
}
|
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,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.
|
|
124
|
+
'User-Agent': 'oilpriceapi-node/0.3.1',
|
|
125
125
|
},
|
|
126
126
|
signal: controller.signal,
|
|
127
127
|
});
|
|
@@ -288,4 +288,48 @@ export class OilPriceAPI {
|
|
|
288
288
|
}
|
|
289
289
|
return this.request('/v1/prices', params);
|
|
290
290
|
}
|
|
291
|
+
/**
|
|
292
|
+
* Get metadata for all supported commodities
|
|
293
|
+
*
|
|
294
|
+
* @returns Object containing array of commodities
|
|
295
|
+
*
|
|
296
|
+
* @example
|
|
297
|
+
* ```typescript
|
|
298
|
+
* const response = await client.getCommodities();
|
|
299
|
+
* console.log(response.commodities); // Array of commodity objects
|
|
300
|
+
* ```
|
|
301
|
+
*/
|
|
302
|
+
async getCommodities() {
|
|
303
|
+
return this.request('/v1/commodities', {});
|
|
304
|
+
}
|
|
305
|
+
/**
|
|
306
|
+
* Get all commodity categories with their commodities
|
|
307
|
+
*
|
|
308
|
+
* @returns Object with category keys mapped to category objects
|
|
309
|
+
*
|
|
310
|
+
* @example
|
|
311
|
+
* ```typescript
|
|
312
|
+
* const categories = await client.getCommodityCategories();
|
|
313
|
+
* console.log(categories.oil.name); // "Oil"
|
|
314
|
+
* console.log(categories.oil.commodities.length); // 11
|
|
315
|
+
* ```
|
|
316
|
+
*/
|
|
317
|
+
async getCommodityCategories() {
|
|
318
|
+
return this.request('/v1/commodities/categories', {});
|
|
319
|
+
}
|
|
320
|
+
/**
|
|
321
|
+
* Get metadata for a specific commodity by code
|
|
322
|
+
*
|
|
323
|
+
* @param code - Commodity code (e.g., "WTI_USD", "BRENT_CRUDE_USD")
|
|
324
|
+
* @returns Commodity metadata object
|
|
325
|
+
*
|
|
326
|
+
* @example
|
|
327
|
+
* ```typescript
|
|
328
|
+
* const commodity = await client.getCommodity('WTI_USD');
|
|
329
|
+
* console.log(commodity.name); // "WTI Crude Oil"
|
|
330
|
+
* ```
|
|
331
|
+
*/
|
|
332
|
+
async getCommodity(code) {
|
|
333
|
+
return this.request(`/v1/commodities/${code}`, {});
|
|
334
|
+
}
|
|
291
335
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -6,5 +6,5 @@
|
|
|
6
6
|
* @packageDocumentation
|
|
7
7
|
*/
|
|
8
8
|
export { OilPriceAPI } from './client.js';
|
|
9
|
-
export type { OilPriceAPIConfig, RetryStrategy, Price, LatestPricesOptions, HistoricalPricesOptions, HistoricalPeriod, } from './types.js';
|
|
9
|
+
export type { OilPriceAPIConfig, RetryStrategy, Price, LatestPricesOptions, HistoricalPricesOptions, HistoricalPeriod, Commodity, CommoditiesResponse, CommodityCategory, CategoriesResponse, } from './types.js';
|
|
10
10
|
export { OilPriceAPIError, AuthenticationError, RateLimitError, NotFoundError, ServerError, TimeoutError, } from './errors.js';
|
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
|
|
35
|
+
* @default 90000 (90 seconds)
|
|
36
36
|
*/
|
|
37
37
|
timeout?: number;
|
|
38
38
|
/**
|
|
@@ -143,3 +143,71 @@ export interface HistoricalPricesOptions {
|
|
|
143
143
|
*/
|
|
144
144
|
endDate?: string;
|
|
145
145
|
}
|
|
146
|
+
/**
|
|
147
|
+
* Represents commodity metadata
|
|
148
|
+
*/
|
|
149
|
+
export interface Commodity {
|
|
150
|
+
/**
|
|
151
|
+
* Unique commodity identifier
|
|
152
|
+
*/
|
|
153
|
+
code: string;
|
|
154
|
+
/**
|
|
155
|
+
* Human-readable commodity name
|
|
156
|
+
*/
|
|
157
|
+
name: string;
|
|
158
|
+
/**
|
|
159
|
+
* Base currency for pricing
|
|
160
|
+
*/
|
|
161
|
+
currency: string;
|
|
162
|
+
/**
|
|
163
|
+
* Commodity category (e.g., "oil", "gas", "renewable")
|
|
164
|
+
*/
|
|
165
|
+
category: string;
|
|
166
|
+
/**
|
|
167
|
+
* Detailed description
|
|
168
|
+
*/
|
|
169
|
+
description?: string;
|
|
170
|
+
/**
|
|
171
|
+
* Unit of measurement (e.g., "barrel", "gallon")
|
|
172
|
+
*/
|
|
173
|
+
unit: string;
|
|
174
|
+
/**
|
|
175
|
+
* Detailed unit description
|
|
176
|
+
*/
|
|
177
|
+
unit_description?: string;
|
|
178
|
+
/**
|
|
179
|
+
* Storage multiplier for price values
|
|
180
|
+
*/
|
|
181
|
+
multiplier?: number;
|
|
182
|
+
/**
|
|
183
|
+
* Price validation ranges
|
|
184
|
+
*/
|
|
185
|
+
validation?: {
|
|
186
|
+
min: number;
|
|
187
|
+
max: number;
|
|
188
|
+
};
|
|
189
|
+
/**
|
|
190
|
+
* Threshold for significant price change alerts
|
|
191
|
+
*/
|
|
192
|
+
price_change_threshold?: number;
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Response from /v1/commodities endpoint
|
|
196
|
+
*/
|
|
197
|
+
export interface CommoditiesResponse {
|
|
198
|
+
commodities: Commodity[];
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Category with its commodities
|
|
202
|
+
*/
|
|
203
|
+
export interface CommodityCategory {
|
|
204
|
+
name: string;
|
|
205
|
+
commodities: Commodity[];
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* Response from /v1/commodities/categories endpoint
|
|
209
|
+
* Returns object with category keys mapped to CommodityCategory objects
|
|
210
|
+
*/
|
|
211
|
+
export interface CategoriesResponse {
|
|
212
|
+
[categoryKey: string]: CommodityCategory;
|
|
213
|
+
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "oilpriceapi",
|
|
3
|
-
"version": "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": {
|
|
@@ -40,7 +41,7 @@
|
|
|
40
41
|
"license": "MIT",
|
|
41
42
|
"repository": {
|
|
42
43
|
"type": "git",
|
|
43
|
-
"url": "https://github.com/OilpriceAPI/oilpriceapi-node.git"
|
|
44
|
+
"url": "git+https://github.com/OilpriceAPI/oilpriceapi-node.git"
|
|
44
45
|
},
|
|
45
46
|
"bugs": {
|
|
46
47
|
"url": "https://github.com/OilpriceAPI/oilpriceapi-node/issues"
|