GithubHelp home page GithubHelp logo

stefanmermans / laravel-model-ts-type Goto Github PK

View Code? Open in Web Editor NEW

This project forked from scrumble-nl/laravel-model-ts-type

0.0 2.0 0.0 80 KB

Generate TypeScript types based on your models.

License: Other

PHP 100.00%

laravel-model-ts-type's Introduction

laravel-model-ts-type

Generate TypeScript types based on your models.

Usage

Installation

Install the package using composer:

composer require scrumble-nl/laravel-model-ts-type

Generating types

php artisan types:generate {--modelDir=} {--outputDir=}

Additional options:

Option Default value Description
modelDir app/Models The root directory where the package can find all Laravel models
outputDir resources/js/models The root directory for outputting the .d.ts files
namespace false The namespace of the generated Types. Use false if you don't prefer to have one
noKebabCase false Whether the file name should be formatted to kebab case

If you want to change the default values you can publish the config file and change it to you liking.

What does it do?

This package takes (almost) all of Laravel's magic into account. It follows these steps for generating a Type:

  1. Retrieve all fields from the database (only MySQL/MariaDB supported) and map them to default types (string, number, etc.)
  2. Add relations for the Model, they will point to the related generated Type
  3. Add the attribute getters for the Model
  4. Check the casts attribute
  5. Remove all fields that are in the hidden attribute

Example output

type Company = {
    id: number;
    name: string;
    created_at: string /* Date */ | null;
    updated_at: string /* Date */ | null;
    slug: string;
    welcome_message: string | null;
    contact_information: string | null;
    main_color: string | null;
    logo_src: string | null;
    user_field: any[];
    language: any[];
    team_site: any[];
    is_api_enabled: boolean;
    kaizen_user_field: string;
    faqs?: Faq[] | null;
    users?: User[] | null;
    team_properties: TeamProperty[] | null;
    editor_images: EditorImage[] | null;
    meta_data?: any[];
};

Roadmap

  • Add tests (in progress)
  • Generate types for packagized models
  • Create command to generate type for 1 model
  • Implement unqualified name for relation doc blocks

Contributing

If you would like to see additions/changes to this package you are always welcome to add some code or improve it.

Scrumble

This product has been originally developed by Scrumble for internal use. As we have been using lots of open source packages we wanted to give back to the community. We hope this helps you getting forward as much as other people helped us!

laravel-model-ts-type's People

Contributors

luukdewaaier avatar jaulz avatar ricoclark avatar stefanmermans avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.