Skip to content

Commit

Permalink
feat(core): generate index.ts file to export routes with required typ…
Browse files Browse the repository at this point in the history
…es (#18)
  • Loading branch information
mohitxskull authored Nov 4, 2024
1 parent 2f477b3 commit b515810
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 19 deletions.
5 changes: 5 additions & 0 deletions .changeset/cuddly-starfishes-shave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tuyau/core': patch
---

Automatically generates `.adonisjs/index.ts` to export routes with required types and definitions, eliminating manual type imports for backend route exports.
2 changes: 1 addition & 1 deletion docs/content/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
"sponsors_sources": ["https://raw.githubusercontent.com/Julien-R44/static/refs/heads/main/sponsorkit/.cache.json"],
"search": {
},
"fileEditBaseUrl": "https://github.com/tuyau/tree/main/docs",
"fileEditBaseUrl": "https://github.com/Julien-R44/tuyau/tree/main/docs",
"copyright": "Tuyau"
}
16 changes: 1 addition & 15 deletions docs/content/docs/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,7 @@ And an appropriate `.adonisjs/api.ts` file will be generated in your project.

### Sharing the API definition

The command will generate a file called `.adonisjs/api.ts` in your project. This file will contain the definition of your API.

Now let's create a file called `.adonisjs/index.ts` in your server workspace :

```ts
/// <reference path="../adonisrc.ts" />

export * from './api.ts'
```

:::warning
We must reference the `adonisrc.ts` file at the top of the file. By doing that, the frontend project will be aware of some types defined in the AdonisJS project.
:::

You must export this file in your project to use the client package.
The `node ace tuyau:generate` command will generate two files in your project: `.adonisjs/api.ts` and `.adonisjs/index.ts`, which contain the definition of your API and export the API definition with required types, respectively.

So, let's say your monorepo structure is like this :

Expand Down
31 changes: 30 additions & 1 deletion packages/core/src/codegen/api_types_generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,13 +274,41 @@ export class ApiTypesGenerator {
.filter((route) => !!route.name)
}

async #writeIndexFile() {
const filePath = this.#getDestinationDirectory() + '/index.ts'

const exist = existsSync(filePath)

if (exist) {
return
}

const file = this.#project.createSourceFile(filePath, '', {
overwrite: true,
})

if (!file) throw new Error('Unable to create the index.ts file')

file.removeText().insertText(0, (writer) => {
writer.writeLine(`/// <reference path="../adonisrc.ts" />`)

writer.newLine()

writer.writeLine(`export * from './api.js'`)
})

await file.save()
}

async #writeApiFile(options: {
routesNameArray: RouteNameArray
definition: Record<string, any>
typesByPattern: Record<string, any>
}) {
const file = this.#project.createSourceFile(this.#destination, '', { overwrite: true })

if (!file) throw new Error('Unable to create the api.ts file')

const isTuyauInertiaInstalled = await this.#isTuyauInertiaInstalled()

file.removeText().insertText(0, (writer) => {
Expand Down Expand Up @@ -434,8 +462,9 @@ export class ApiTypesGenerator {
)

/**
* Write the final api.ts file
* Write the final api.ts & index.ts file
*/
await this.#writeApiFile({ definition, typesByPattern, routesNameArray })
await this.#writeIndexFile()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -194,3 +194,8 @@ export const api = {
}
"`

exports[`Api Types Generator > works fine 2`] = `"/// <reference path=\\"../adonisrc.ts\\" />
export * from './api.js'
"`

8 changes: 6 additions & 2 deletions packages/core/tests/api_types_generator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@ test.group('Api Types Generator', (group) => {

await apiTypesGenerator.generate()

const file = await fs.contents('./.adonisjs/api.ts')
assert.snapshot(file).match()
const apiFile = await fs.contents('./.adonisjs/api.ts')
assert.snapshot(apiFile).match()

const indexFile = await fs.contents('./.adonisjs/index.ts')

assert.snapshot(indexFile).match()
})

test('extract validateUsing request', async ({ fs, assert }) => {
Expand Down

0 comments on commit b515810

Please sign in to comment.