New

Just launched: Vueblocks, a brand-new search engine for Vue packages! Explore Vueblocks →

sedlatschek/laravel-typescript-writer

This is my package laravel-typescript-writer
3,096
Install
composer require sedlatschek/laravel-typescript-writer:v1.0.0-beta.8
Latest Version:v1.0.0-beta.8
PHP:^8.1
Laravel:9 · 10
License:MIT
Last Updated:Oct 9, 2023
Links: GitHub  ·  Packagist
Maintainer: sedlatschek

laravel-typescript-writer

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

Write typescript data out of laravel. Does not generate interfaces. Eg.:

$simon = [
    'name' => 'Simon'
]

will turn into

export const simon: Person = {
    name: "Simon"
}

Installation

You can install the package via composer:

composer require --dev sedlatschek/laravel-typescript-writer

You should publish the config file with:

php artisan vendor:publish --tag="typescript-writer-config"

This is the contents of the published config file:

return [
    // The typescript file indentation
    'indentation' => 2,

    // The end-of-line character
    'eol_char' => PHP_EOL,

    // Whether to use single or double quotes for strings
    'single_quote' => true,

    // The files that should be written
    'files' => [
        /*
        new TypescriptFile(__DIR__.'/example.ts', [
            // The contents of the file
            new TypescriptData('Array<Test>', 'test', [
                [
                    'id' => 33,
                    'name' => 'test',
                    'active' => true,
                    'languages' => [
                        'German',
                        'English',
                    ],
                ],
            ]),
        ]),
        */
    ],
];

Configuration

Value Replacements

You can replace given values with hardcoded strings. This can be useful if you have an enum that can be matched with the output values.

new TypescriptData(
    'Array<Test>',
    'test',
    // the data
    [
        [
            'test' => [
                'flags' => [
                    1,
                    2,
                    3,
                ],
            ],
        ],
    ],
    // the configured replacements
    [
        '*.test.flags' => [
            1 => 'SomeEnum.ABC',
            2 => 'SomeEnum.DEF',
            3 => 'SomeEnum.GHJ',
        ],
    ]),

will output

export const test: Array<Test> = [
  {
    test: {
      flags: [
        SomeEnum.ABC,
        SomeEnum.DEF,
        SomeEnum.GHJ
      ]
    }
  }
];

Usage

php artisan typescript-writer

Testing

composer test

Changelog

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

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

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

Related Packages

based/laravel-typescript

Transform Laravel models into TypeScript interfaces

162,020 396
gabrielmpetrescu/laravel-typescript

Transform Laravel models into TypeScript interfaces

11,934 0
lumore/laravel-typescript

Transform Laravel models into TypeScript interfaces

1,136 2
kiwilan/typescriptable-laravel

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

34,011 39
calvient/puddleglum

Transform Laravel models, routes, and controllers into a Typescript API client

15,551 3

Version History

Pre-releases (8)
Version Released PHP Laravel License
v1.0.0-beta.8 ^8.1 ^9.0|^10.0 MIT
v1.0.0-beta.7 ^8.1 ^9.0|^10.0 MIT
v1.0.0-beta.6 ^8.1 ^9.0|^10.0 MIT
v1.0.0-beta.5 ^8.1 ^9.0|^10.0 MIT
v1.0.0-beta.4 ^8.1 ^9.0|^10.0 MIT