@redonvn/shipping-grabexpress
v1.1.0
Published
Production-ready GrabExpress On-Demand Delivery API SDK for RedAI Monorepo (OAuth2, Quotes, GPS Tracking)
Keywords
Readme
@redai/shipping-grabexpress
SDK TypeScript chuẩn doanh nghiệp kết nối GrabExpress On-Demand Delivery API (Giao hàng tức thì / Siêu tốc / Trong ngày bằng xe máy) trong Monorepo Logistics RedAI.
🌟 TÍNH NĂNG NỔI BẬT
- OAuth 2.0 Client Credentials Grant:
- Tự động lấy Access Token từ máy chủ GrabID.
- Cơ chế Cache token thông minh kèm bộ đệm 60s trước khi hết hạn.
- Lifecycle Hook tự động bắt lỗi
401 Unauthorizedđể cấp lại token và retry trong suốt với ứng dụng.
- Quy trình Đặt xe 2 bước chuẩn On-Demand:
- Bước 1: Lấy báo giá
Quotes➔ Trả về cước phí, thời gian đón/giao vàquoteTokencó hiệu lực 5-10 phút để khóa giá. - Bước 2: Đặt xe
Create DeliverybằngquoteTokenđể điều phối tài xế gần nhất.
- Bước 1: Lấy báo giá
- Theo dõi thời gian thực (Live GPS Driver Tracking):
- Truy vấn thông tin tài xế: Họ tên, Số điện thoại, Biển số xe.
- Tọa độ GPS thời gian thực (Latitude, Longitude) của tài xế trên bản đồ.
- Bảo mật Webhook HMAC-SHA256:
- Xác thực chữ ký số
X-Grab-Signaturebằng cơ chế an toàncrypto.timingSafeEqual. - Ánh xạ tự động trạng thái Grab sang
CanonicalShippingStatus(ASSIGNING_DRIVER,PICKING_UP,DELIVERING,DELIVERED,CANCELLED...).
- Xác thực chữ ký số
- Tuân thủ Chuẩn Speakeasy Monorepo:
- Thuật toán Exponential Backoff kết hợp Full Jitter cho
429 Too Many Requestsvà lỗi máy chủ5xx. - Triển khai đầy đủ interface đa nền tảng
IShippingProvider.
- Thuật toán Exponential Backoff kết hợp Full Jitter cho
🚀 CÀI ĐẶT & SỬ DỤNG NHANH
import { GrabExpressClient } from '@redai/shipping-grabexpress';
const client = new GrabExpressClient({
clientId: process.env.GRAB_CLIENT_ID,
clientSecret: process.env.GRAB_CLIENT_SECRET,
webhookSecret: process.env.GRAB_WEBHOOK_SECRET,
env: 'staging' // 'staging' hoặc 'production'
});
// 1. Lấy báo giá và quoteToken
const quote = await client.quotes.getQuotes({
serviceType: 'INSTANT',
origin: { address: 'Bitexco Financial Tower, Quận 1, TP.HCM' },
destination: { address: 'Landmark 81, Bình Thạnh, TP.HCM' },
packages: [
{ name: 'Cà phê & Bánh ngọt', quantity: 1, price: 150000, dimensions: { weight: 500 } }
]
});
console.log('Giá cước:', quote.quotes[0].amount, quote.quotes[0].currency);
// 2. Tạo đơn đặt xe
const delivery = await client.deliveries.create({
merchantOrderID: 'ORDER_' + Date.now(),
quoteToken: quote.quoteToken,
sender: { firstName: 'Nguyễn', lastName: 'Văn A', phone: '+84912345678' },
recipient: { firstName: 'Trần', lastName: 'Thị B', phone: '+84987654321' }
});
console.log('Mã đơn Grab:', delivery.deliveryID);