em-gateway-sdk
v1.0.1
Published
JavaScript SDK for the EM-Gateway fuel inventory API
Maintainers
Readme
EM-Gateway JavaScript SDK
Client library for the EM-Gateway fuel inventory management API.
The SDK accepts the API encryption key as either 64 hexadecimal characters (the format returned by EM-Gateway) or a 32-character UTF-8 string.
Installation
npm install em-gateway-sdkUsage
Setup
const EMGatewayClient = require('em-gateway-sdk');
const client = new EMGatewayClient({
baseUrl: 'http://your-api-host:3000',
clientId: 'your-client-id',
clientSecret: 'your-client-secret',
});OAuth2 Token Flow
// Step 1: Get authorization URL
const authUrl = client.getAuthorizationUrl({
redirectUri: 'http://localhost:3000',
scope: 'tank_id storage_name remaining_fuel',
});
console.log('Visit:', authUrl);
// Step 2: After user authorizes, exchange code for token
const token = await client.exchangeAuthorizationCode({
code: 'authorization-code-from-redirect',
redirectUri: 'http://localhost:3000',
});
// Step 3: Set the token
client.setBearerToken(token.access_token);Submit Fuel Data (Encrypted)
const testData = {
code: 'FUEL-001',
tank_id: 'TANK-001',
storage_name: 'Storage A',
storage_owner: 'Owner',
storage_type: 'Type',
fuel_type: 'Diesel',
tank_capacity: 50000,
dead_stock: 100,
remaining_fuel: 25000,
source: 1,
};
const result = await client.submitEncryptedFuelInventory(
testData,
'encryption-key-32-characters!!!',
{
clientId: 'your-client-id',
count: 1,
}
);
console.log(result);
// { success: true, message: '...', count: 1 }Dry-Run Mode (Test Without Saving)
Test your data submission without writing to the database. The API decrypts and validates your data, then returns a preview — useful for testing before going live.
const result = await client.submitEncryptedFuelInventory(
testData,
'encryption-key-32-characters!!!',
{
clientId: 'your-client-id',
count: 1,
dryRun: true, // ← enables dry-run mode
}
);
if (result.dry_run) {
console.log('✅ Validation passed!');
console.log('Preview:', result.preview);
// No data was saved to database
}Dry-Run Response (Success)
{
"success": true,
"dry_run": true,
"message": "ข้อมูลผ่านการตรวจสอบทั้งหมด 1 รายการ (Dry-Run — ไม่ได้บันทึกลงฐานข้อมูล)",
"count": 1,
"preview": [
{
"code": "FUEL-001",
"tank_id": "TANK-001",
"storage_name": "Storage A",
"remaining_fuel": 25000
}
]
}Dry-Run Response (Validation Error)
{
"success": false,
"dry_run": true,
"message": "ข้อมูลไม่ผ่านการตรวจสอบ (Dry-Run)",
"validation_errors": [
{ "index": 1, "missing_fields": ["tank_id", "fuel_type"] }
],
"total_records": 1,
"error_count": 1,
"valid_count": 0
}Master Data
const provinces = await client.request('/api/master/getProvince', {
method: 'POST',
body: {},
});
const regions = await client.request('/api/master/getRegion', {
method: 'POST',
body: {},
});
const tanks = await client.getStorageLocationTank({});Health Check
const health = await client.health();
// { status: 'healthy', service: 'api-gateway', timestamp: '...' }API Methods
| Method | Description |
|--------|-------------|
| getAuthorizationUrl(opts) | Generate OAuth2 authorization URL |
| exchangeAuthorizationCode(opts) | Exchange auth code for token |
| exchangeClientCredentials(opts) | Get token via client credentials |
| setBearerToken(token) | Set the JWT access token |
| submitFuelInventory(payload) | Submit raw fuel data (POST) |
| submitEncryptedFuelInventory(data, key, opts) | Submit encrypted fuel data |
| encryptFuelPayload(data, key) | Encrypt data with AES-256-CBC |
| getStorageLocationTank(payload) | Get storage location tanks |
| health() | Server health check |
| request(path, opts) | Generic API request |
Environment Variables
EM_GATEWAY_URL=http://your-api-host:3000
EM_GATEWAY_CLIENT_ID=your-client-id
EM_GATEWAY_CLIENT_SECRET=your-client-secret