-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 709ff93
Showing
43 changed files
with
2,501 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# TS Generator | ||
|
||
This is a Drupal module, that generates Typescript type definitions for certain entities. It can optionally also generate cleaned up target type definitions and functions to convert objects from the initials types to the target types (parsers). | ||
|
||
## Installation | ||
|
||
```sh | ||
composer require hoppinger/ts_generator | ||
``` | ||
|
||
## Usage | ||
|
||
The generator runs as a Drush command that needs a configuration file. | ||
|
||
Create `.yml` file with the following contents: | ||
|
||
```yaml | ||
target_directory: client/generated | ||
entities: | ||
input: | ||
- node | ||
- taxonomy_term | ||
generate_parser: true | ||
``` | ||
This file instructs the generator to generate the files in a directory `client/generated` (relative to the location of the configuration file), to generate types for the `node` and `taxonomy_term` entities and to generate target types and corresponding parsers. | ||
|
||
Trigger the generation using | ||
|
||
```sh | ||
cd [PROJECT DIRECTORY]/web | ||
../vendor/bin/drush ts_generator:generate [PATH TO CONFIGURATION FILE] | ||
``` | ||
|
||
## Actual usage of the types | ||
|
||
This is an example of how you could use those types. You are not limited to this approach, of course. | ||
|
||
```ts | ||
import { | ||
InputEntity, | ||
ParsedEntity, | ||
ParsedInputEntity, | ||
} from "./generated/types" | ||
import { | ||
input_entity_parser, | ||
input_entity_guard | ||
} from "./generated/types" | ||
export type Result<T> = T | "error" | ||
export async function drupalGetEntity(alias: string): Promise<Result<ParsedInputEntity>> { | ||
const res = await fetch(`/${alias}?format=_json`), { method: "get", credentials: "include" }) | ||
if (!res.ok) return "error" | ||
|
||
const json = await res.json() | ||
if (!input_entity_guard(json)) return "error" | ||
|
||
const parsed = input_entity_parser(json as InputEntity) | ||
return parsed | ||
} | ||
``` | ||
|
||
## Todo | ||
|
||
* Write more documentation on usage | ||
* Write documentation on the internal workings and ways to extend the generator |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"name": "hoppinger/ts_generator", | ||
"description": "Drupal module to generate TypeScript code based on the REST Resources", | ||
"type": "drupal-module", | ||
"license": "GPL-2.0+", | ||
"minimum-stability": "dev", | ||
"homepage": "https://github.com/hoppinger/ts_generator", | ||
"authors": [ | ||
{ | ||
"name": "Rolf van de Krol", | ||
"role": "Maintainer" | ||
} | ||
], | ||
"require": {}, | ||
"extra": { | ||
"drush": { | ||
"services": { | ||
"drush.services.yml": "^9" | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
services: | ||
ts_generator.commands: | ||
class: \Drupal\ts_generator\Commands\TsGeneratorCommands | ||
tags: | ||
- { name: drush.command } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
namespace Drupal\ts_generator\Commands; | ||
|
||
use Consolidation\OutputFormatters\StructuredData\RowsOfFields; | ||
use Drupal\ts_generator\Result; | ||
use Drupal\ts_generator\Settings; | ||
use Drupal\ts_generator\Generator; | ||
use Drush\Commands\DrushCommands; | ||
|
||
/** | ||
* A Drush commandfile. | ||
*/ | ||
class TsGeneratorCommands extends DrushCommands { | ||
|
||
/** | ||
* Generate TypeScript code for the REST Resources | ||
* | ||
* @param $filename | ||
* Config file | ||
* @usage ts_generator-generator foo | ||
* Usage description | ||
* | ||
* @command ts_generator:generate | ||
*/ | ||
public function generate($filename) { | ||
if (!file_exists($filename)) { | ||
$this->logger()->error(dt('The specified file does not exist.')); | ||
} | ||
|
||
$working_directory = dirname($filename); | ||
$settings = Settings::loadFile($filename); | ||
|
||
/** @var \Drupal\ts_generator\GeneratorInterface $generator */ | ||
$generator = \Drupal::service('ts_generator.generator'); | ||
|
||
$entity_type_manager = \Drupal::entityTypeManager(); | ||
|
||
$result = new Result(); | ||
$generator->generate($entity_type_manager, $settings, $result); | ||
|
||
$target_directory = $working_directory . '/' . $settings->get('target_directory'); | ||
$result->write($target_directory); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace Drupal\ts_generator\ComponentGenerator\Data; | ||
|
||
use Drupal\ts_generator\ComponentResult; | ||
use Drupal\ts_generator\Result; | ||
use Drupal\ts_generator\Settings; | ||
|
||
class AnyGenerator extends DataGeneratorBase { | ||
|
||
protected function getDataType($object, Settings $settings, Result $result, ComponentResult $componentResult) { | ||
return 'any'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
namespace Drupal\ts_generator\ComponentGenerator\Data; | ||
|
||
use Drupal\ts_generator\ComponentResult; | ||
use Drupal\ts_generator\Result; | ||
use Drupal\ts_generator\Settings; | ||
|
||
class BooleanGenerator extends DataGeneratorBase { | ||
protected $supportedDataType = ['boolean']; | ||
|
||
protected function getDataType($object, Settings $settings, Result $result, ComponentResult $componentResult) { | ||
return 'boolean'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
namespace Drupal\ts_generator\ComponentGenerator\Data; | ||
|
||
use Drupal\Core\TypedData\DataDefinitionInterface; | ||
use Drupal\ts_generator\ComponentGenerator\GeneratorBase; | ||
use Drupal\ts_generator\ComponentResult; | ||
use Drupal\ts_generator\Result; | ||
use Drupal\ts_generator\Settings; | ||
|
||
abstract class DataGeneratorBase extends GeneratorBase { | ||
protected $supportedDataType; | ||
|
||
abstract protected function getDataType($object, Settings $settings, Result $result, ComponentResult $componentResult); | ||
|
||
public function supportsGeneration($object) { | ||
if (!($object instanceof DataDefinitionInterface)) { | ||
return FALSE; | ||
} | ||
|
||
$supported = (array) $this->supportedDataType; | ||
|
||
return !isset($this->supportedDataType) || in_array($object->getDataType(), $supported); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function generateType($object, Settings $settings, Result $result, ComponentResult $componentResult) { | ||
/** @var \Drupal\Core\TypedData\DataDefinitionInterface $object */ | ||
$componentResult->setComponent('base_type', $this->getDataType($object, $settings, $result, $componentResult)); | ||
return $object->isRequired() ? $componentResult->getComponent('base_type') : $componentResult->getComponent('base_type') . ' | null'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
namespace Drupal\ts_generator\ComponentGenerator\Data; | ||
|
||
use Drupal\ts_generator\ComponentResult; | ||
use Drupal\ts_generator\Result; | ||
use Drupal\ts_generator\Settings; | ||
|
||
class DateTimeGenerator extends DataGeneratorBase { | ||
protected $supportedDataType = ['timestamp', 'datetime_iso8601']; | ||
|
||
protected function getDataType($object, Settings $settings, Result $result, ComponentResult $component_result) { | ||
return 'string'; | ||
} | ||
|
||
protected function generateTargetType($object, Settings $settings, Result $result, ComponentResult $componentResult) { | ||
/** @var \Drupal\Core\TypedData\DataDefinitionInterface $object */ | ||
$componentResult->setComponent('base_target_type', ':/moment/Moment*:.Moment'); | ||
return $object->isRequired() ? $componentResult->getComponent('base_target_type') : $componentResult->getComponent('base_target_type') . ' | null'; | ||
} | ||
|
||
protected function generateParser($object, Settings $settings, Result $result, ComponentResult $componentResult) { | ||
/** @var \Drupal\Core\TypedData\DataDefinitionInterface $object */ | ||
$base_parser = $result->setComponent( | ||
'parser/moment_parser', | ||
'const moment_parser = (t: string): :/moment/Moment*:.Moment => :/moment/Moment*:(t)' | ||
); | ||
$componentResult->setComponent('base_parser', $base_parser); | ||
|
||
if ($object->isRequired()) { | ||
$name = 'required_date_time_parser'; | ||
return $result->setComponent('parser/' . $name, 'const ' . $name . ' = (t: string): :/moment/Moment*:.Moment => ' . $base_parser . '(t)'); | ||
} else { | ||
$name = 'optional_date_time_parser'; | ||
return $result->setComponent('parser/' . $name, 'const ' . $name . ' = (t: string | null): :/moment/Moment*:.Moment | null => t ? ' . $base_parser . '(t) : null'); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
namespace Drupal\ts_generator\ComponentGenerator\Data; | ||
|
||
use Drupal\Core\Entity\EntityTypeManagerInterface; | ||
use Drupal\ts_generator\ComponentResult; | ||
use Drupal\ts_generator\Result; | ||
use Drupal\ts_generator\Settings; | ||
|
||
class FilterFormatGenerator extends DataGeneratorBase { | ||
protected $supportedDataType = 'filter_format'; | ||
|
||
/** | ||
* @var \Drupal\Core\Entity\EntityTypeManagerInterface | ||
*/ | ||
protected $entityTypeManager; | ||
|
||
public function __construct(EntityTypeManagerInterface $entityTypeManager) { | ||
$this->entityTypeManager = $entityTypeManager; | ||
} | ||
|
||
protected function generateFilterFormatObject($object, Settings $settings, Result $result, ComponentResult $componentResult) { | ||
$filter_format_storage = $this->entityTypeManager->getStorage('filter_format'); | ||
$filter_formats = $filter_format_storage->loadMultiple(); | ||
|
||
$filter_format_keys = []; | ||
foreach ($filter_formats as $filter_format) { | ||
$filter_format_keys[] = '\'' . $filter_format->id() . '\''; | ||
} | ||
|
||
$filter_format_component = $result->setComponent('types/FilterFormat', "type FilterFormat = " . implode(' | ', $filter_format_keys)); | ||
return $filter_format_component; | ||
} | ||
|
||
protected function getDataType($object, Settings $settings, Result $result, ComponentResult $componentResult) { | ||
return $this->generateFilterFormatObject($object, $settings, $result, $componentResult); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
namespace Drupal\ts_generator\ComponentGenerator\Data; | ||
|
||
use Drupal\ts_generator\ComponentResult; | ||
use Drupal\ts_generator\Result; | ||
use Drupal\ts_generator\Settings; | ||
|
||
class NumberGenerator extends DataGeneratorBase { | ||
protected $supportedDataType = ['integer']; | ||
|
||
protected function getDataType($object, Settings $settings, Result $result, ComponentResult $component_result) { | ||
return 'number'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
namespace Drupal\ts_generator\ComponentGenerator\Data; | ||
|
||
use Drupal\ts_generator\ComponentResult; | ||
use Drupal\ts_generator\Result; | ||
use Drupal\ts_generator\Settings; | ||
|
||
class StringGenerator extends DataGeneratorBase { | ||
protected $supportedDataType = ['string', 'email', 'uri']; | ||
|
||
protected function getDataType($object, Settings $settings, Result $result, ComponentResult $component_result) { | ||
return 'string'; | ||
} | ||
} |
Oops, something went wrong.