MCPcopy Index your code
hub / github.com/adonisjs/core / outputTransformerDataObjects

Function outputTransformerDataObjects

src/utils.ts:84–137  ·  view source on GitHub ↗
(
  transformersList: RecursiveFileTree,
  buffer: Assembler.FileBuffer,
  withSharedProps: boolean,
  inertiaMiddlewareImportPath: string = '#middleware/inertia_middleware'
)

Source from the content-addressed store, hash-verified

82 * // }
83 */
84export async function outputTransformerDataObjects(
85 transformersList: RecursiveFileTree,
86 buffer: Assembler.FileBuffer,
87 withSharedProps: boolean,
88 inertiaMiddlewareImportPath: string = '#middleware/inertia_middleware'
89) {
90 const importsBuffer = buffer.create()
91 importsBuffer.write(`/// <reference path="./manifest.d.ts" />`)
92 importsBuffer.write(
93 `import type { InferData, InferVariants } from '@adonisjs/core/types/transformers'`
94 )
95
96 if (withSharedProps) {
97 importsBuffer.write(`import type { InferSharedProps } from '@adonisjs/inertia/types'`)
98 }
99
100 buffer.writeLine(importsBuffer)
101 buffer.write('export namespace Data {').indent()
102
103 /**
104 * Recursively generates namespace tree structure for transformers.
105 * Creates nested namespaces for directory structures and type exports
106 * for individual transformer files.
107 *
108 * @param input - The current level of the file tree to process
109 * @param parents - Array of parent namespace names for import naming
110 */
111 function generateNamespaceTree(input: RecursiveFileTree, parents: string[]) {
112 Object.keys(input).forEach((key) => {
113 const value = input[key]
114 if (typeof value === 'string') {
115 const importName = `${parents.join('')}${key}Transformer`
116 importsBuffer.write(`import type ${importName} from '${value}'`)
117 buffer.write(`export type ${key} = InferData<${importName}>`)
118 buffer.write(`export namespace ${key} {`).indent()
119 buffer.write(`export type Variants = InferVariants<${importName}>`)
120 buffer.dedent().write('}')
121 } else {
122 buffer.write(`export namespace ${key} {`).indent()
123 generateNamespaceTree(value, [...parents, key])
124 buffer.dedent().write(`}`)
125 }
126 })
127 }
128
129 generateNamespaceTree(transformersList, [])
130
131 if (withSharedProps) {
132 importsBuffer.write(`import type InertiaMiddleware from '${inertiaMiddlewareImportPath}'`)
133 buffer.write('export type SharedProps = InferSharedProps<InertiaMiddleware>')
134 }
135
136 buffer.dedent().write('}')
137}

Callers 1

asFunction · 0.90

Calls 2

generateNamespaceTreeFunction · 0.85
createMethod · 0.45

Tested by

no test coverage detected