coredump/jdd-api
Laravel package exposing model resources via a simple JSON API and a Vue-friendly client: chain endpoints like $api.users[1].roleObject.users.array(), load rows/arrays, CRUD (post/put/delete), and call model methods with parameters.
Installation
composer require coredump/jdd-api
Add the service provider to config/app.php:
'providers' => [
// ...
Coredump\JddApi\JddApiServiceProvider::class,
],
Configuration Publish the config file:
php artisan vendor:publish --provider="Coredump\JddApi\JddApiServiceProvider"
Update .env with your JDD API credentials:
JDD_API_KEY=your_api_key_here
JDD_API_SECRET=your_api_secret_here
First Use Case: Fetching a Product
use Coredump\JddApi\Facades\JddApi;
$product = JddApi::product()->find(12345);
dd($product);
Coredump\JddApi\Facades\JddApi (main entry point)Coredump\JddApi\Client (handles HTTP requests)Coredump\JddApi\Resources\Product, Order, etc. (model responses)$product = JddApi::product()->find($id);
$products = JddApi::product()->all(['limit' => 10, 'page' => 1]);
$created = JddApi::product()->create([
'name' => 'New Product',
'price' => 99.99,
]);
Use the Resource classes to access structured data:
$order = JddApi::order()->find($orderId);
$customerName = $order->customer->name; // Nested access
Wrap API calls in try-catch:
try {
$data = JddApi::product()->find($id);
} catch (\Coredump\JddApi\Exceptions\ApiException $e) {
Log::error('JDD API Error: ' . $e->getMessage());
// Fallback logic
}
Attach API data to Eloquent models:
public function getJddProductAttribute()
{
return JddApi::product()->find($this->jdd_id);
}
Extend the client for unsupported endpoints:
$response = JddApi::client()->get('/custom-endpoint', ['param' => 'value']);
Offload heavy API calls to queues:
dispatch(new FetchJddProducts($productIds))->onQueue('jdd-api');
Cache API responses to reduce calls:
$product = Cache::remember("jdd_product_{$id}", now()->addHours(1), function () use ($id) {
return JddApi::product()->find($id);
});
Log requests/responses for debugging:
JddApi::client()->setLogger(function ($request, $response) {
Log::debug('JDD API Request', [
'url' => $request->getUri(),
'method' => $request->getMethod(),
'response' => $response->getBody(),
]);
});
Deprecated Package
No Official Documentation
namespace App\Extensions\JddApi;
use Coredump\JddApi\Resources\Product as BaseProduct;
class Product extends BaseProduct
{
public function getCustomFieldAttribute()
{
return $this->attributes['custom_field'] ?? null;
}
}
Authentication Issues
JDD_API_KEY and JDD_API_SECRET are correct and not hardcoded.Rate Limiting
use Symfony\Component\HttpClient\RetryableHttpClient;
$client = new RetryableHttpClient(
JddApi::client()->getHttpClient(),
[
'max_retries' => 3,
'delay' => 100,
'max_delay' => 1000,
'multiplier' => 2,
]
);
Nested Resource Access
$order->customer) may require lazy loading:
$customer = $order->load('customer')->customer;
Enable Verbose Logging
JddApi::client()->setDebug(true);
Check logs for raw API responses.
Inspect HTTP Client Replace the default client for debugging:
JddApi::setClient(function () {
return \Symfony\Component\HttpClient\HttpClient::create([
'debug' => true,
'headers' => ['User-Agent' => 'Laravel Debugger'],
]);
});
Validate API Responses
Use Laravel’s validate() to ensure responses match expectations:
$product = JddApi::product()->find($id);
$validator = Validator::make($product->toArray(), [
'id' => 'required|integer',
'name' => 'required|string',
]);
Custom Resources Override resource classes to add methods or fields:
namespace App\Resources\JddApi;
use Coredump\JddApi\Resources\Product;
class Product extends \Coredump\JddApi\Resources\Product
{
public function isOnSale()
{
return $this->price < $this->original_price;
}
}
Bind the custom class in a service provider:
JddApi::extend('product', function () {
return new App\Resources\JddApi\Product();
});
Middleware for API Requests Add middleware to modify requests/responses:
JddApi::client()->addMiddleware(function (callable $next) {
$request = $next(function () {
return new \Symfony\Component\HttpClient\MockHttpClient([
new \Symfony\Component\HttpClient\MockResponse('{}'),
]);
});
// Modify request/response here
return $request;
});
Event Listeners Listen for API events (if the package emits them):
JddApi::client()->on('request', function ($request) {
Log::info('API Request:', $request->getUri());
});
How can I help you explore Laravel packages today?