Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/bolzer/symfony-typescript-routes

A symfony extension and service providing the capability to generate typescript functions from the symfony application routes
https://github.com/bolzer/symfony-typescript-routes

path php php8 routes symfony symfony-bundle typescript

Last synced: 5 days ago
JSON representation

A symfony extension and service providing the capability to generate typescript functions from the symfony application routes

Awesome Lists containing this project

README

        

# Symfony-Typescript-Routes
![maintained](https://img.shields.io/maintenance/yes/2024)
[![codecov](https://codecov.io/gh/BolZer/symfony-typescript-routes/branch/master/graph/badge.svg?token=W7IYQXY2UD)](https://codecov.io/gh/BolZer/symfony-typescript-routes)

## Description

This Extension for the symfony framework provides a Generator which can be used - after registering the provided extension in your symfony application -
in your code to generate typescript code from the application routes. These generated routes can be used in your typescript code to reference the symfony
application routes.

## Installation

```shell
composer require bolzer/symfony-typescript-routes
```

## Example

1. Registering the Extension
```PHP
// bundles.php
['all' => true],
];

```

2. Write something to create a path.ts file with the content from the service. Like a Command!
```PHP
// some_command.php
setName('generate_paths');
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
// Typescript routes containing relative and absolute urls.
$routes = $this->generatorService->generate(GeneratorConfig::generateEverything());

// Typescript routes containing absolute urls.
$routes = $this->generatorService->generate(GeneratorConfig::generateOnlyAbsoluteUrls());

// Typescript routes containing absolute urls.
$routes = $this->generatorService->generate(GeneratorConfig::generateOnlyRelativeUrls());

file_put_contents(__DIR__ . '../../../paths.ts', implode("\n", $routes));
$output->writeln('Generation of paths done.');

return Command::SUCCESS;
}
}
```

The Output may look something like this

```Typescript
//paths.ts
const rRP = (rawRoute: string, routeParams: Record): string => {Object.entries(routeParams).forEach(([key, value]) => rawRoute = rawRoute.replace(`{${key}}`, value)); return rawRoute;}
const aQP = (route: string, queryParams?: Record): string => queryParams ? route + "?" + new URLSearchParams(queryParams).toString() : route;
export const path_user_route = ():{ relative: (routeParams: {id: string, noteId: string}, queryParams?: Record) => string, absolute: (routeParams: {id: string, noteId: string}, queryParams?: Record) => string} => {return {relative: (routeParams: {id: string, noteId: string}, queryParams?: Record): string => aQP(rRP('/user/{id}/notes/{noteId}', routeParams), queryParams), absolute: (routeParams: {id: string, noteId: string}, queryParams?: Record): string => aQP(rRP('https://app.development.org/user/{id}/notes/{noteId}', routeParams), queryParams)}};
```

And can be used like this

```Typescript
//example.ts
import * as $ from "jquery";
import {path_users_route} from "./paths";

$.get(path_users_route().relative({"count": "20"}))

// Outputs: /users?count=20
console.log(path_users_route().relative(({"count": "20"})))

// Outputs: https://example.host.org/users?count=20
console.log(path_users_route().absolute(({"count": "20"})))
```

## Conventions

* Query and Route Params must be provided as strings to the Typescript Functions if no requirement at the route is defined. Only digit requirements and either A or B requirements are supported.
* All generated path functions in typescript will have a "path_" prefix.

### Executing Tests

```shell
docker build -t ts-path-tests .
docker run ts-path-tests
```

### Test Coverage

```shell
XDEBUG_MODE=coverage ./vendor/bin/phpunit --coverage-text
```