Skip to content

Commit

Permalink
feat(dts): initial indentation
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Mar 9, 2022
1 parent a573e3b commit f4f40b9
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/generator/dts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ export interface GenerateTypesOptions {
addExport?: boolean
addDefaults?: boolean
defaultDescrption?: string
indentation?: number
}

const GenerateTypesDefaults: GenerateTypesOptions = {
interfaceName: 'Untyped',
addExport: true,
addDefaults: true
addDefaults: true,
indentation: 0
}

const TYPE_MAP: Record<JSType, string> = {
Expand Down Expand Up @@ -73,8 +75,12 @@ function extractTypeImports (declarations: string) {

export function generateTypes (schema: Schema, opts: GenerateTypesOptions = {}) {
opts = { ...GenerateTypesDefaults, ...opts }
const interfaceCode = `interface ${opts.interfaceName} {\n ` + _genTypes(schema, ' ', opts).join('\n ') + '\n}'
return extractTypeImports(opts.addExport ? `export ${interfaceCode}` : interfaceCode)
const baseIden = ' '.repeat(opts.indentation)
const interfaceCode = `interface ${opts.interfaceName} {\n ` + _genTypes(schema, baseIden + ' ', opts).join('\n ') + `\n${baseIden}}`
if (!opts.addExport) {
return baseIden + interfaceCode
}
return extractTypeImports(baseIden + `export ${interfaceCode}`)
}

function _genTypes (schema: Schema, spaces: string, opts: GenerateTypesOptions): string[] {
Expand Down

0 comments on commit f4f40b9

Please sign in to comment.