Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Typescript Transformer Laravel Package

spatie/typescript-transformer

Automatically generate TypeScript definitions from your PHP/Laravel code. spatie/typescript-transformer scans classes and types, then outputs .d.ts files so your frontend stays in sync with backend models, DTOs and enums with minimal manual typing.

View on GitHub
Deep Wiki
Context7

title: Routes weight: 4

Laravel provides a great way to define routes and then generate URLs to those routes in PHP using the route() helper. While this all works in PHP, it can be a bit of a pain to do the same in TypeScript. TypeScript transformer can help you here by providing exact copy of the route() helper in TypeScript.

To add the helper, add the following provider to your TypeScriptTransformerServiceProvider:

use Spatie\LaravelTypeScriptTransformer\TransformedProviders\LaravelRouteTransformedProvider;

protected function configure(TypeScriptTransformerConfigFactory $config): void
{
    $config->provider(new LaravelRouteTransformedProvider());
}

The next time you run the typescript:transform command, a TypeScript function called route will be generated in the helpers/route.ts file.

You can now use the route function in your TypeScript code like this:

import {route} from './helpers/route';

// Without parameters
const indexUrl = route('users.index');
// https://laravel.dev/users

// With parameters
const userUrl = route('users.show', {user: 1});
// https://laravel.dev/users/1

TypeScript will be smart enough to provide you autocompletion on these controllers and their parameters.

You can exclude certain routes from being included in the generated TypeScript using route filters.

By default, the helper will generate absolute URLs meaning it includes the app URL. This URL will be fetched from the window object in JavaScript. If you want to generate relative URLs instead, you can pass false as the third parameter, indicating you don't want absolute URLs:

const indexUrl = route('users.index', {}, false);
// /users

The default value of the absolute parameter can be changed by setting a default for the provider:

$config->provider(new LaravelRouteTransformedProvider(
    absoluteUrlsByDefault: false,
));

Now when using the route helper in TypeScript, URLs will be relative by default:

const indexUrl = route('users.index');
// /users

TypeScript transformer will automatically generate the helpers/route.ts file in the output directory you configured for TypeScript transformer. It is possible to change the path of this file as such:

$config->provider(new LaravelRouteTransformedProvider(
    path: 'route.ts',
));

When running in the watch mode of the package, the generated route.ts file will automatically be updated when you change your routes in Laravel. By default, the watcher will monitor the following directories for changes:

  • routes
  • bootstrap
  • app/Providers

It is possible to customize the directories that are monitored as such:

$config->provider(new LaravelRouteTransformedProvider(
    routeDirectories: [
        'custom/routes/directory',
        'another/directory/to/watch',
    ],
));
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
besmartand-pro/php-quality-config
sentix/ai-chatbot
terminal42/code-quality-tools
codifyo/ts-generator-bundle
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity