Skip to content
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

fix(opapi): remove getState from opapi #94

Merged
merged 2 commits into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
18 changes: 13 additions & 5 deletions opapi/src/generators/ts-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@ import { State } from '../state'
import { tsFileHeader } from '../const'
import prettier from 'prettier'

const DEFAULT_IMPORT_PATH = '@bpinternal/opapi'

export type ExportStateAsTypescriptOptions = Partial<{ importPath: string }>
export const exportStateAsTypescript = <
SchemaName extends string,
DefaultParameterName extends string,
SectionName extends string,
>(
state: State<SchemaName, DefaultParameterName, SectionName>,
dir: string,
opts: ExportStateAsTypescriptOptions = {},
): void => {
fs.mkdirSync(dir, { recursive: true })

Expand All @@ -24,13 +28,17 @@ export const exportStateAsTypescript = <
const typeParam = !paramNames.length ? 'never' : paramNames.map((s) => `'${s}'`).join(' | ')
const typeSection = !sectionNames.length ? 'never' : sectionNames.map((s) => `'${s}'`).join(' | ')

const header = `${tsFileHeader}/* prettier-ignore */\n`
const imports = `import { State } from '@bpinternal/opapi'\n`
const body = `export const state = ${json} satisfies State<${typeSchema}, ${typeParam}, ${typeSection}>\n`
const importPath = opts.importPath ?? DEFAULT_IMPORT_PATH

const formatted = prettier.format(body, { parser: 'typescript' })
const header = `${tsFileHeader}/* prettier-ignore */\n`
const content = [
`import * as opapi from '${importPath}'`,
`export type State = opapi.State<${typeSchema}, ${typeParam}, ${typeSection}>`,
`export const state = ${json} satisfies State`,
].join('\n')
const formatted = prettier.format(content, { parser: 'typescript' })

const ts = header + imports + formatted
const ts = header + formatted
const path = pathlib.join(dir, 'state.ts')
fs.writeFileSync(path, ts)
}
5 changes: 2 additions & 3 deletions opapi/src/opapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from './generator'
import { addOperation } from './operation'
import { ApiError, ComponentType, createState, getRef, Metadata, Operation, Options, Parameter, State } from './state'
import { exportStateAsTypescript } from './generators/ts-state'
import { exportStateAsTypescript, ExportStateAsTypescriptOptions } from './generators/ts-state'
export { Operation, Parameter } from './state'

export const schema = extendApi
Expand Down Expand Up @@ -45,7 +45,6 @@ const createOpapiFromState = <
state: State<SchemaName, DefaultParameterName, SectionName>,
) => {
return {
getState: (): State<SchemaName, DefaultParameterName, SectionName> => state,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not to be confused with exportState

getModelRef: (name: SchemaName): OpenApiZodAny => getRef(state, ComponentType.SCHEMAS, name),
addOperation: <Path extends string>(
operationProps: Operation<DefaultParameterName, SectionName, Path, 'zod-schema'>,
Expand All @@ -55,7 +54,7 @@ const createOpapiFromState = <
exportTypesBySection: (dir = '.') => generateTypesBySection(state, dir),
exportServer: (dir = '.', useExpressTypes: boolean) => generateServer(state, dir, useExpressTypes),
exportOpenapi: (dir = '.') => generateOpenapi(state, dir),
exportState: (dir = '.') => exportStateAsTypescript(state, dir),
exportState: (dir = '.', opts?: ExportStateAsTypescriptOptions) => exportStateAsTypescript(state, dir, opts),
exportErrors: (dir = '.') => generateErrorsFile(state.errors ?? [], dir),
}
}
Expand Down
32 changes: 32 additions & 0 deletions opapi/test/state.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { describe, expect, it } from 'vitest'
import z from 'zod'
import { OpenApi, OpenApiProps } from '../src'
import { join } from 'path'
import { getFiles } from '../src/file'
import { validateTypescriptFile } from './util'

type AnyProps = OpenApiProps<string, string, string>

Expand Down Expand Up @@ -151,3 +154,32 @@ describe('openapi generator with unions allowed', () => {
})
})
})

describe('openapi state generator', () => {
it('should export state', async () => {
const api = OpenApi(
{
metadata,
sections,
schemas: {
Tree: {
section: 'trees',
schema: tree,
},
},
},
{ allowUnions: true },
)

const genStateFolder = join(__dirname, 'gen/state')
api.exportState(genStateFolder, { importPath: '../../../src' })

const files = getFiles(genStateFolder)

files.forEach((file) => {
if (file.endsWith('.ts')) {
validateTypescriptFile(file)
}
})
})
})
Loading