evanschleret/laravel-typebridge
Composer 安装命令:
composer require evanschleret/laravel-typebridge
包简介
Generate deterministic TypeScript resources from Laravel resource attributes
关键字:
README 文档
README
Laravel TypeBridge
Deterministic TypeScript generation from Laravel resources.
Why this package
This package helps you keep backend resources and frontend types aligned, with deterministic output and predictable imports.
Requirements
- PHP
>=8.2 - Laravel
12.xor13.x - Note: Laravel
13.xrequires PHP>= 8.3(Laravel framework requirement)
Installation
composer require --dev evanschleret/laravel-typebridge
Publish the config:
php artisan vendor:publish --provider="EvanSchleret\\LaravelTypeBridge\\TypeBridgeServiceProvider" --tag=typebridge-config
Generated file:
config/typebridge.php
Basic usage
Generate files:
php artisan typebridge:generate
Use another output directory:
php artisan typebridge:generate --output-path=resources/typescript
Preview only (no write):
php artisan typebridge:generate --dry-run
Generate and remove stale .ts files:
php artisan typebridge:generate --clean
Generate only selected resources:
php artisan typebridge:generate --only=User,Role
Generate everything except selected resources:
php artisan typebridge:generate --except=Membership
Check generated files in CI:
php artisan typebridge:check
Attribute example
Use TypeBridgeResource on Laravel resources (JsonResource or ResourceCollection):
<?php declare(strict_types=1); namespace App\Http\Resources; use App\Models\User; use EvanSchleret\LaravelTypeBridge\Attributes\TypeBridgeResource; use Illuminate\Http\Resources\Json\JsonResource; #[TypeBridgeResource( name: 'UserItem', structure: [ 'id' => 'number', 'email' => 'string|null', 'roles' => '@relation(roles)', 'manager?' => '@relation(manager)', ], )] final class UserResource extends JsonResource { public static string $model = User::class; }
Optional attribute fields:
types: local TypeScript aliases/enums declared in the same generated filefileName: per-resource output filename overrideappend: per-resource lines appended at the end of the generated filealiasBaseandaliasPlural: alias placeholders used bygeneration.append_templates
@relation(name) is strict:
- the resource model must be resolvable
- the relation method must exist
- the relation method must return an Eloquent relation
If the relation exists but no generated TypeScript type is available for the related model, the field falls back to any or any[].
@enum(Fully\\Qualified\\EnumClass) generates a TypeScript literal union from PHP enum cases.
Configuration
Published default config
This is the default config generated by vendor:publish:
<?php declare(strict_types=1); return [ 'output' => [ 'base_path' => resource_path('typescript'), 'additional_paths' => [], ], 'sources' => [ app_path('Http/Resources'), ], 'generation' => [ 'use_semicolons' => false, 'indent_size' => 2, 'generate_index' => true, 'shared_file' => '_api', 'shared_append' => [], 'append_templates' => [], ], 'files' => [ 'extension' => 'ts', 'naming_pattern' => '{name}', ], ];
Advanced config example (API wrappers + aliases)
This is an example, not the default:
'generation' => [ 'indent_size' => 4, 'shared_file' => '_api', 'shared_append' => [ 'export interface ApiItemResponse<T> {', " status: 'success' | 'error'", ' data: T', '}', 'export interface ApiCollectionResponse<T> {', ' data: T[]', '}', ], 'append_templates' => [ [ 'name_ends_with' => 'Item', 'lines' => [ 'export type {base} = ApiItemResponse<{name}>', 'export type {basePlural} = ApiCollectionResponse<{name}>', ], ], ], ],
Additional outputs
output.additional_paths are generated on every typebridge:generate call when --output-path is not used.
If you use --output-path, additional paths are skipped unless you pass --with-additional-paths.
With RoleItem, this can generate:
export type Role = ApiItemResponse<RoleItem> export type Roles = ApiCollectionResponse<RoleItem>
Naming placeholders
For files.naming_pattern:
{name}{pascal}{camel}{snake}{kebab}
Example:
'files' => [ 'naming_pattern' => '{kebab}.types', ],
Template placeholders
Inside append_templates.*.lines:
{name}: resource name{base}: alias base (aliasBaseor suffix stripping){basePlural}: alias plural (aliasPluralor pluralized base){pascal}{camel}{snake}{kebab}
Override rules
fileNameon the attribute overridesfiles.naming_patternfor one resource--output-pathoverridesoutput.base_path
Roadmap
- Add
typebridge:checkfor CI (no write, non-zero exit code when generated files are out of date) - Add
--onlyand--exceptto generate a subset of resources - Add
--cleanto remove stale generated files - Improve
@relation(...)diagnostics with more actionable error messages - Add enum helpers (for example
@enum(...)support) - Add configurable scalar mapping presets (
datetime,uuid,decimal, etc.) - Add configurable import/path strategy options
- Add a first-class paginator wrapper preset
- Add pre/post render hooks for advanced customization
- Add incremental generation mode for large codebases
- Add stubs/scaffolding helpers for
#[TypeBridgeResource] - Add optional targets beyond interfaces (for example Zod schema generation)
Other packages
If you want to explore more of my Laravel packages:
Open source
- Contributing guide: CONTRIBUTING.md
- Security policy: SECURITY.md
- Code of conduct: CODE_OF_CONDUCT.md
- License: LICENSE
evanschleret/laravel-typebridge 适用场景与选型建议
evanschleret/laravel-typebridge 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 47 次下载、GitHub Stars 达 3, 最近一次更新时间为 2026 年 03 月 18 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「api」 「laravel」 「resource」 「dto」 「types」 「typescript」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 evanschleret/laravel-typebridge 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 evanschleret/laravel-typebridge 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 evanschleret/laravel-typebridge 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Generates URLs for public Puli resources.
Add the resource system to symfony application
A lightweight and powerful OAuth 2.0 authorization and resource server library with support for all the core specification grants. This library will allow you to secure your API with OAuth and allow your applications users to approve apps that want to access their data from your API.
Adds support for singular resource routes to the Laravel router.
A PSR-7 compatible library for making CRUD API endpoints
Retrieve a WebResourceInterface instance over HTTP
统计信息
- 总下载量: 47
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 39
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-03-18
