-
Notifications
You must be signed in to change notification settings - Fork 18
TS type generation via aliases (#60) #61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
9bbccad
669d33f
2f12469
e73461a
d0fc987
8e8d267
d9903da
00e4fc0
c28b4fd
6c2d18d
3f2afaa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| name: Publish new version | ||
|
|
||
| on: | ||
| push: | ||
| branches: | ||
| - main | ||
| paths: | ||
| - package.json | ||
| workflow_dispatch: | ||
|
|
||
| jobs: | ||
| tag: | ||
| name: Tag | ||
| runs-on: ubuntu-latest | ||
| permissions: | ||
| contents: write | ||
| outputs: | ||
| created: ${{ steps.version_tag.outputs.created }} | ||
| version_tag: ${{ steps.version_tag.outputs.version_tag }} | ||
| steps: | ||
| - name: Checkout code | ||
| uses: actions/checkout@v4 | ||
| with: | ||
| fetch-depth: 0 | ||
| - name: Setup Bun | ||
| uses: oven-sh/setup-bun@v2 | ||
| with: | ||
| bun-version: latest | ||
| - name: Ensure version tag exists | ||
| id: version_tag | ||
| run: | | ||
| VERSION=$(bun -p "require('./package.json').version") | ||
| VERSION_TAG="v${VERSION}" | ||
|
|
||
| echo "version_tag=${VERSION_TAG}" >> $GITHUB_OUTPUT | ||
| echo "created=false" >> $GITHUB_OUTPUT | ||
|
|
||
| git fetch --tags | ||
|
|
||
| if ! git rev-parse "${VERSION_TAG}" >/dev/null 2>&1; then | ||
| git config user.name "github-actions[bot]" | ||
| git config user.email "41898282+github-actions[bot]@users.noreply.github.com" | ||
| git tag "${VERSION_TAG}" | ||
| git push origin "${VERSION_TAG}" | ||
| echo "created=true" >> $GITHUB_OUTPUT | ||
| echo "Created and pushed ${VERSION_TAG}." | ||
| fi | ||
|
|
||
| release: | ||
| name: Release | ||
| runs-on: ubuntu-latest | ||
| needs: [tag] | ||
| if: needs.tag.outputs.created == 'true' | ||
| permissions: | ||
| contents: write | ||
| steps: | ||
| - name: Create release from new tag | ||
| uses: actions/github-script@v7 | ||
| with: | ||
| script: | | ||
| const tag = '${{ needs.tag.outputs.version_tag }}'; | ||
| const prerelease = tag.includes('-'); | ||
|
|
||
| await github.rest.repos.createRelease({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| tag_name: tag, | ||
| name: tag, | ||
| prerelease, | ||
| generate_release_notes: true, | ||
| }); | ||
|
|
||
| publish: | ||
| name: Publish | ||
| runs-on: ubuntu-latest | ||
| permissions: | ||
| id-token: write | ||
| steps: | ||
| - name: Checkout code | ||
| uses: actions/checkout@v4 | ||
| - name: Setup Bun | ||
| uses: oven-sh/setup-bun@v2 | ||
| with: | ||
| bun-version: latest | ||
| - name: Install npm | ||
| run: bun add -g npm | ||
| - name: Install dependencies | ||
| run: NODE_ENV=development bun i | ||
| - name: Build and publish | ||
| run: bunx npm publish --provenance --access public |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,19 @@ | ||
| import { generateTypeboxOptions } from "../../annotations/options"; | ||
| import { getConfig } from "../../config"; | ||
|
|
||
| export function makeComposite(inputModels: string[]) { | ||
| return `${ | ||
| getConfig().typeboxImportVariableName | ||
| }.Composite([${inputModels.map((i) => `${getConfig().exportedTypePrefix}${i}`).join(",")}], ${generateTypeboxOptions()})\n`; | ||
| function makePartial(input: string) { | ||
| return `${getConfig().typeboxImportVariableName}.Partial(${input})`; | ||
| } | ||
|
|
||
| export function makeComposite(inputModels: string[], partial: boolean[] = []) { | ||
| const { exportedTypePrefix, typeboxImportVariableName } = getConfig(); | ||
|
|
||
| return `${typeboxImportVariableName}.Composite([${inputModels | ||
| .map((model, i) => { | ||
| let modelStr = `${exportedTypePrefix}${model}`; | ||
| if (partial[i]) modelStr = makePartial(modelStr); | ||
|
|
||
| return modelStr; | ||
| }) | ||
| .join(", ")}], ${generateTypeboxOptions()})\n`; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,11 +27,25 @@ export type ProcessedModel = { | |
| function convertModelToStandalone( | ||
| input: Pick<ProcessedModel, "name" | "stringRepresentation">, | ||
| ) { | ||
| return `export const ${getConfig().exportedTypePrefix}${input.name} = ${input.stringRepresentation}\n`; | ||
| const generatedName = `${getConfig().exportedTypePrefix}${input.name}`; | ||
|
|
||
| let exportStr = `export const ${generatedName} = ${input.stringRepresentation}\n`; | ||
|
|
||
| if (getConfig().generateTsTypes) { | ||
| exportStr += `export type ${generatedName} = ${getConfig().unwrapSchemaImportName}<typeof ${generatedName}>\n`; | ||
| } | ||
|
|
||
| return exportStr; | ||
| } | ||
|
|
||
| function typepoxImportStatement() { | ||
| return `import { ${getConfig().typeboxImportVariableName} } from "${ | ||
| let imports = getConfig().typeboxImportVariableName; | ||
|
|
||
| if (getConfig().generateTsTypes) { | ||
| imports += `, type ${getConfig().unwrapSchemaImportName}`; | ||
| } | ||
|
Comment on lines
41
to
+46
|
||
|
|
||
| return `import { ${imports} } from "${ | ||
| getConfig().typeboxImportDependencyName | ||
| }"\n`; | ||
| } | ||
|
|
@@ -75,7 +89,10 @@ export function mapAllModelsForWrite() { | |
| const relations = processedRelations.find((e) => e.name === key); | ||
| let composite: string; | ||
| if (plain && relations) { | ||
| composite = makeComposite([`${key}Plain`, `${key}Relations`]); | ||
| composite = makeComposite( | ||
| [`${key}Plain`, `${key}Relations`], | ||
| [false, true], | ||
| ); | ||
| } else if (plain) { | ||
| composite = `${key}Plain`; | ||
| } else if (relations) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new config example introduces
unwrapSchemaImportName = "UnwrapSchema", but the actual default insrc/config.tsis currently "Type.Static" (which is not a valid named import). Please align the README with the real default and clarify thatunwrapSchemaImportNamemust be a named export identifier (e.g. "Static" for @sinclair/typebox) that can be imported withimport { type ... }fromtypeboxImportDependencyName.