定制 hoppinger/ts_generator 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

hoppinger/ts_generator

最新稳定版本:2.0.1

Composer 安装命令:

composer require hoppinger/ts_generator

包简介

Drupal module to generate TypeScript code based on the REST Resources

README 文档

README

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

composer require hoppinger/ts_generator

Versions

Version 2.0.0 is Drupal 9 compatible.

Usage

The generator runs as a Drush command that needs a configuration file.

Create .yml file with the following contents:

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

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.

import {
  InputEntity,
  ParsedInputEntity,
} from "./generated/types"

import {
  input_entity_parser,
  input_entity_guard
} from "./generated/parser"

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

统计信息

  • 总下载量: 12.96k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 3
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: GPL-2.0
  • 更新时间: 2018-08-05