小ネタです。
最近は相変わらずCakePHPでAPIを書いて、nextjsでフロントのアプリを書くサイトばかり作っているのですが、API側で定義したAPIレスポンスデータをフロント側用にinterfaceを書くのがだるいのでプラグインを書いた話です。
TsExport plugin for CakePHP
TsExport plugin for CakePHPは以下のようにインストールしてください。
composer require --dev kaz29/cakephp-ts-export-plugin
実行は以下のような感じ。
bin/cake export_entity --all または bin/cake export_entity モデル名
実際に実行すると、以下のようにinterface定義が標準出力に出力されます。
bin/cake export_entity Users /** * User entity interface */ export interface User { id: number name: string email: string password: string created?: string modified?: string }
フロント側では、src/types/exported_interfaces.ts のようなファイル名でこのプラグインの出力をそのまま使って、フロント用に変更する場合は、別のファイルでextend して項目を追加したり不要なものをOmitしたりしたものを使ってます。