Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/assembler_hooks/index_entities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ import { outputTransformerDataObjects } from '../utils.ts'
* source: 'app/custom-events',
* importAlias: '#custom-events'
* },
* assemblers: {
* enabled: true,
* withSharedProps: true,
* inertiaMiddlewareImportPath: '#middleware/inertia_middleware'
* },
* controllers: {
* enabled: false
* }
Expand Down Expand Up @@ -82,6 +87,7 @@ export function indexEntities(entities: IndexEntitiesConfig = {}) {
source: 'app/transformers',
importAlias: '#transformers',
withSharedProps: false,
inertiaMiddlewareImportPath: '#middleware/inertia_middleware',
skipSegments: ['transformers'],
output: '.adonisjs/client/data.d.ts',
},
Expand Down Expand Up @@ -166,7 +172,12 @@ export function indexEntities(entities: IndexEntitiesConfig = {}) {
},
transformValue: helpers.toImportPath,
})
outputTransformerDataObjects(transformersList, buffer, transformers.withSharedProps)
outputTransformerDataObjects(
transformersList,
buffer,
transformers.withSharedProps,
transformers.inertiaMiddlewareImportPath
)
},
importAlias: transformers.importAlias,
output: transformers.output,
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ export type IndexEntitiesConfig = {
enabled?: boolean
/** Whether to include shared props in transformers */
withSharedProps?: boolean
inertiaMiddlewareImportPath?: string
/** Source directory for transformers */
source?: string
/** Import alias for transformers */
Expand Down
5 changes: 3 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ export async function importTypeScript(
export async function outputTransformerDataObjects(
transformersList: RecursiveFileTree,
buffer: Assembler.FileBuffer,
withSharedProps: boolean
withSharedProps: boolean,
inertiaMiddlewareImportPath: string = '#middleware/inertia_middleware'
) {
const importsBuffer = buffer.create()
importsBuffer.write(`/// <reference path="./manifest.d.ts" />`)
Expand Down Expand Up @@ -128,7 +129,7 @@ export async function outputTransformerDataObjects(
generateNamespaceTree(transformersList, [])

if (withSharedProps) {
importsBuffer.write(`import type InertiaMiddleware from '#middleware/inertia_middleware'`)
importsBuffer.write(`import type InertiaMiddleware from '${inertiaMiddlewareImportPath}'`)
buffer.write('export type SharedProps = InferSharedProps<InertiaMiddleware>')
}

Expand Down
62 changes: 62 additions & 0 deletions tests/index_generator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,68 @@ test.group('Index generator', () => {
)
})

test('generate transformers index with shared props and custom inertia middleware import path', async ({
assert,
fs,
}) => {
const cliUi = Kernel.create().ui
cliUi.switchMode('raw')

await fs.create('app/transformers/user_transformer.ts', '')
await fs.create('app/transformers/blog/post_transformer.ts', '')

const generator = new IndexGenerator(stringHelpers.toUnixSlash(fs.basePath), cliUi.logger)
const indexer = indexEntities({
controllers: {
enabled: false,
},
events: {
enabled: false,
},
listeners: {
enabled: false,
},
transformers: {
enabled: true,
withSharedProps: true,
inertiaMiddlewareImportPath: '#core/middleware/inertia_middleware',
},
})

indexer.run({} as any, {} as any, generator)
await generator.generate()

await assert.fileExists('.adonisjs/client/data.d.ts')
assert.snapshot(await fs.contents('.adonisjs/client/data.d.ts')).matchInline(`
"/// <reference path=\\"./manifest.d.ts\\" />
import type { InferData, InferVariants } from '@adonisjs/core/types/transformers'
import type { InferSharedProps } from '@adonisjs/inertia/types'
import type BlogPostTransformer from '#transformers/blog/post_transformer'
import type UserTransformer from '#transformers/user_transformer'
import type InertiaMiddleware from '#core/middleware/inertia_middleware'

export namespace Data {
export namespace Blog {
export type Post = InferData<BlogPostTransformer>
export namespace Post {
export type Variants = InferVariants<BlogPostTransformer>
}
}
export type User = InferData<UserTransformer>
export namespace User {
export type Variants = InferVariants<UserTransformer>
}
export type SharedProps = InferSharedProps<InertiaMiddleware>
}
"
`)
assert.isDefined(
cliUi.logger
.getLogs()
.find(({ message }) => message.includes('[ blue(info) ] codegen: created'))
)
})

test('generate transformers index with deeply nested directories', async ({ assert, fs }) => {
const cliUi = Kernel.create().ui
cliUi.switchMode('raw')
Expand Down