承接 kiwilan/typescriptable-laravel 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

kiwilan/typescriptable-laravel

Composer 安装命令:

composer require kiwilan/typescriptable-laravel

包简介

PHP package for Laravel to type Eloquent models, routes, Spatie Settings with autogenerated TypeScript. If you want to use some helpers with Inertia, you can install associated NPM package.

README 文档

README

Banner with printer shop picture in background and Typescriptable Laravel title

php version downloads license codecov tests

laravel npm

PHP package for Laravel to type Eloquent models, routes, Spatie Settings with autogenerated TypeScript.

If you want to use some helpers with Inertia, you can install associated NPM package.

Eldorado Road Both Scene

Because you need PHP and Typescript.

Features

  • 💽 Supported Laravel drivers: MySQL, MariaDB, PostgreSQL, SQLite, SQL Server, MongoDB
  • 💬 Generate TS types for Eloquent models
  • 👭 Generate TS types for Eloquent relations
  • 🪄 Generate TS types for casts (include native enum support)
  • 📝 Generate TS types for appends and all accessors
    • Illuminate\Database\Eloquent\Casts\Attribute with PHPDoc
    • get*Attribute methods
  • #️⃣ Generate TS types for counts
  • 📖 Can generate pagination TS types for Laravel pagination
  • 💾 Can generate simple PHP classes from Eloquent models
  • ⚙️ Generate TS types for spatie/laravel-settings
  • 🛣 Generate TS types for Laravel routes
  • ✅ Multiple commands to generate types
    • php artisan typescriptable for models, settings and routes (safe even if you don't use all)
    • php artisan typescriptable:eloquent for Eloquent models
    • php artisan typescriptable:settings for spatie/laravel-settings
    • php artisan typescriptable:routes for Laravel routes

Roadmap

Installation

This version requires PHP 8.2+ and supports Laravel 11.

Warning

Laravel 11 dropped Doctrine DBAL. For previous Laravel versions, you can use 1.12.03 version.

Version L9 L10 L11
v3+
v1.12.03

You can install the package via composer:

With Laravel v11+ and PHP 8.2

composer require kiwilan/typescriptable-laravel

With Laravel v9-10 and PHP 8.1

composer require kiwilan/typescriptable-laravel:1.12.03

About TypeScript

If you want to use .d.ts files, you need to use TypeScript in your Laravel project, you have to create a tsconfig.json file and add .d.ts paths in include:

Note

If you change paths into config or with options, adapt paths.

{
    "compilerOptions": {
        "types": ["vite/client"]
    },
    "include": [
        "resources/js/**/*.ts",
        "resources/js/**/*.d.ts",
        "resources/js/**/*.vue", // If you use Vue
        "*.d.ts",
        "vite.config.ts"
    ]
}
Complete `tsconfig.json`

Here is a complete tsconfig.json file example (you can adapt paths):

{
    "compilerOptions": {
        "target": "esnext",
        "jsx": "preserve",
        "module": "ESNext",
        "moduleResolution": "Node",
        "paths": {
            "@/*": ["./resources/js/*"],
            "@": ["./resources/js"],
            "~": ["./"],
            "~/*": ["./*"]
        },
        "types": ["vite/client"],
        "allowJs": true,
        "strict": true,
        "noEmit": true,
        "esModuleInterop": true,
        "forceConsistentCasingInFileNames": true,
        "isolatedModules": true,
        "skipLibCheck": true
    },
    "include": [
        "resources/js/**/*.ts",
        "resources/js/**/*.d.ts",
        "resources/js/**/*.vue",
        "*.d.ts",
        "vite.config.ts"
    ]
}

About NPM package @kiwilan/typescriptable-laravel

NPM package is fully optional, you can use only PHP package. It's built for Vite with laravel-vite-plugin and Inertia (only for Vue 3). It's SSR compatible.

This package add some helpers to use Laravel routes fully typed with TypeScript into Vue components and some composables to use with Vue. The best setup to install this package is to use Jetstream, a Laravel starter kit and tightenco/ziggy is required.

Read full documentation here: @kiwilan/typescriptable-laravel.

Configuration

You can publish the config file

php artisan vendor:publish --tag="typescriptable-config"

A config example is available here: config/typescriptable.php.

Important

You can configure engine.eloquent with artisan or parser to change parser engine. By default, it uses artisan command with model:show command. artisan is default engine because it's more reliable and faster than parser engine. With MongoDB, the engine doesn't matter because MongoDB database can't be parsed like relational databases.

Usage

php artisan typescriptable

With options:

  • --M|models: Generate Models types.
  • --R|routes: Generate Routes types.
  • --S|settings: Generate Settings types.

Eloquent models

Generate resources/js/types-eloquent.d.ts file with all models types.

php artisan typescriptable:eloquent

Options can be set into config/typescriptable.php file.

Spatie Settings

If you use spatie/laravel-settings, you can generate resources/js/types-settings.d.ts file with all settings types.

php artisan typescriptable:settings

Options can be set into config/typescriptable.php file.

Routes

Generate resources/js/types-routes.d.ts file with all routes types and resources/js/routes.ts for routes references.

php artisan typescriptable:routes

Options can be set into config/typescriptable.php file.

Eloquent listing

Show all Eloquent models with eloquent:list command.

php artisan eloquent:list

Models are parsed from config/typescriptable.php with eloquent.directory variable.

Advanced

MongoDB

kiwilan/typescriptable-laravel supports MongoDB with mongodb/laravel-mongodb. Due to the MongoDB structure, Typescript conversion aren't the same as SQL databases, precision is lower. If you want to improve it, you can add an issue.

Database isn't parsed like with relational databases. The package will parse key, fillable and hidden to get all fields. If some fields are missing, you can override them manually. All relations and accessors are supported.

Database prefix

You can use prefix variable into config/database.php file.

'connections' => [
    'YOUR_DATABASE_CONNECTION' => [
        'prefix' => '',
    ],
],

Override models

kiwilan/typescriptable-laravel will cover many cases, but if you want to override some models, you can just create a type like resources/js/types/index.ts and extends Model type.

export interface BookAdvanced extends App.Models.Book {
    pivot: {
        created_at: string;
        updated_at: string;
    };
}

And you can import custom type in your code when you need to use advanced type.

<script setup lang="ts">
import { ref } from "vue";
import { BookAdvanced } from "@/types";

const book = ref<BookAdvanced>();
</script>

Print PHP classes

If you want to print PHP classes, you can use --php-path option with php artisan typescriptable:eloquent command.

php artisan typescriptable:eloquent --php-path=app/print

These classes will be generated from Eloquent models as real PHP classes.

Examples

Check examples documentation.

Example output

Testing

Create a .env file with your database configuration

cp .env.example .env

And you can run tests

composer test

Note

You can check this gist to have a Docker database configuration.

Changelog

Please see CHANGELOG for more information on what has changed recently.

Credits

License

The MIT License (MIT). Please see License File for more information.

kiwilan/typescriptable-laravel 适用场景与选型建议

kiwilan/typescriptable-laravel 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 25.35k 次下载、GitHub Stars 达 39, 最近一次更新时间为 2023 年 02 月 05 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「php」 「routes」 「model」 「laravel」 「eloquent」 「typescript」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

围绕 kiwilan/typescriptable-laravel 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 25.35k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 39
  • 点击次数: 28
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 39
  • Watchers: 1
  • Forks: 9
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-02-05