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

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

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

andrazero121/api-resource-typer

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

andrazero121/api-resource-typer 适用场景与选型建议

andrazero121/api-resource-typer 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 26 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 06 月 29 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 andrazero121/api-resource-typer 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 andrazero121/api-resource-typer 我们能提供哪些服务?
定制开发 / 二次开发

基于 andrazero121/api-resource-typer 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

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