wai/sdk-generator

There is no license information available for the latest version (0.0.1) of this package.

Laravel package to generate PHP SDK from OpenAPI JSON

0.0.1 2025-06-04 02:59 UTC

This package is auto-updated.

Last update: 2025-06-04 03:00:45 UTC


README

This Laravel package allows you to generate a PHP SDK for your API from an OpenAPI JSON file and integrate it easily into your Laravel application.

✨ Features

✅ Supports multiple API connections.
✅ Dynamically detects API versions and generates structured, versioned SDKs.
✅ Shared ApiConnector class in the package for core logic.
✅ Optionally publish a customizable ApiConnector in your generated SDK that extends the package’s core connector.
✅ Consistent error handling, validation errors, and Laravel-like error messages baked in.
✅ Detects and renders dd() debug output from the API as styled HTML blocks.
✅ Handles API exceptions (exception, file, line, trace keys) in a structured way.
✅ Compatible with PHP 7.0+.

🚀 Installation

1️⃣ Add to your composer.json:

"repositories": [
{
"type": "path",
"url": "packages/Wai/SdkGenerator"
}
]

2️⃣ Install:

composer require wai/sdk-generator

3️⃣ Publish the config:

php artisan vendor:publish --provider="Wai\\SdkGenerator\\SdkGeneratorServiceProvider" --tag=sdk-config

4️⃣ Edit config/sdk.php with your API connections:

'connections' => [
    'default' => [
        'openapi_url' => env('SDK_DEFAULT_OPENAPI_URL', 'https://api.example.com/openapi.json'),
        'base_url' => env('SDK_DEFAULT_BASE_URL', 'https://api.example.com'),
        'key' => env('SDK_DEFAULT_API_KEY', 'Bearer YOUR_API_TOKEN'),
        'namespace' => 'App\\Sdks\\Default',
        'publish_connector' => true,
        'connector_class' => 'App\\Sdks\\Default\\ApiConnector'
    ],
],

⚙️ Generating the SDK

Run:

php artisan sdk:generate

This will:

✅ Remove old SDK classes.
✅ Fetch the OpenAPI JSON.
✅ Generate namespaced, PSR-4-compliant PHP SDK classes.
✅ If publish_connector is true, generate a local ApiConnector that extends the package’s core connector.

📝 Example Usage

Use the generated SDK classes directly:

use App\\Sdks\\Default\\V1\\Api;

$api = new Api();

// Example GET request
$response = $api->clients()->get(['id' => 123]);
print_r($response);

// Example POST request
$newClient = $api->clients()->create(['name' => 'John Doe']);
print_r($newClient);

🔧 Static Call Example

You can also call endpoints statically (without creating an Api instance):

use App\\Sdks\\Default\\V1\\Endpoints\\ClientsEndpoint;

$response = ClientsEndpoint::index(['id' => 123]);
print_r($response);

🔧 Overriding URL and Token

Each endpoint call allows you to override the base URL and token:

$response = $api->clients()->get(['id' => 123], 'Bearer NEW_TOKEN', 'https://api.newdomain.com');

🛠️ ApiConnector Logic

The ApiConnector handles:

✅ Consistent error handling.
✅ Laravel-like dd() output detection and rendering (red dashed border, labeled “API Debug Dump”).
✅ Laravel-like exception dumps (exception, file, line, trace) for better debugging.
✅ Validation error handling (HTTP 422).
✅ Returns either the API’s data or a structured error response.

🗂️ SDK Directory Structure

Example structure after generating:

app/Sdks/Default/
├── ApiConnector.php (only if published)
├── V1/
│   ├── Api.php
│   └── Endpoints/
│       ├── ClientsEndpoint.php
│       ├── QuotationEndpoint.php

🤝 Contributing

Feel free to fork and create pull requests to improve the package!

📄 License

MIT License.

⚠️ Debug Note

  • In app.debug=true mode, you’ll see real error messages and Laravel-like dd() output directly in your browser.
  • In production (app.debug=false), only generic error messages are shown to keep things secure.

This is useful for quick, direct usage of an endpoint!