定制 andrazero121/api-resource-typer 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

andrazero121/api-resource-typer

最新稳定版本:v1.0.8

Composer 安装命令:

composer require andrazero121/api-resource-typer

包简介

A Laravel package to automatically generate TypeScript interfaces from API Resource Controllers

README 文档

README

🚀 Automatically generate TypeScript or JavaScript interfaces from your Laravel API Resource Controllers!

Features

  • ✅ Auto-generate TypeScript or JavaScript interfaces from API responses
  • ✅ Support for Laravel Resource and ResourceCollection
  • ✅ Smart type inference from actual data
  • ✅ Artisan command for manual generation with output type selection
  • ✅ Middleware for automatic generation
  • ✅ Trait for easy controller integration
  • ✅ Configurable type mappings
  • ✅ Pagination support

Installation

composer require andrazero121/api-resource-typer

Publish the config file:

php artisan vendor:publish --provider="AndraZero121\ApiResourceTyper\Providers\ApiResourceTyperServiceProvider" --tag=api-resource-typer-config
php artisan vendor:publish --provider="AndraZero121\ApiResourceTyper\Providers\ApiResourceTyperServiceProvider" --tag=api-resource-typer-extension

Usage

Method 1: Using Trait

Add the trait to your API controllers:

<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Http\Resources\UserResource;
use App\Models\User;
use AndraZero121\ApiResourceTyper\Traits\ApiResourceTyper;

class UserController extends Controller
{
    use ApiResourceTyper;

    public function index()
    {
        $users = User::paginate();
        return $this->responseWithTypes(UserResource::collection($users));
    }

    public function show(User $user)
    {
        return $this->responseWithTypes(new UserResource($user));
    }
}

Method 2: Using Middleware

Add middleware to your API routes:

// routes/api.php
Route::middleware(['api', 'api-typer'])->group(function () {
    Route::apiResource('users', UserController::class);
});

Method 3: Manual Generation (Recommended)

Generate types manually using Artisan command:

# Generate for all models as TypeScript (default)
php artisan generate:api-types

# Generate for all models as JavaScript JSDoc
printf artisan generate:api-types --output-type=js

# Generate for specific model
php artisan generate:api-types --model=User --output-type=ts

Generated Output

The package will generate TypeScript or JSDoc interfaces like this:

// TypeScript
export interface UserResource {
  id: number;
  name: string;
  email: string;
  created_at: Date;
  updated_at: Date;
}

// JavaScript JSDoc
/**
 * @typedef {Object} UserResource
 * @property {number} id
 * @property {string} name
 * ...
 */

Configuration

Edit config/api-resource-typer.php for output path, type mappings, and excluded columns.

Custom Extension

You can add your own helper or type modifier in app/ApiResourceTyperExtension.php after publishing the extension file.

Requirements

  • PHP 8.2+
  • Laravel 11.x+

License

MIT

统计信息

  • 总下载量: 25
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-06-29