From 5e751867d55cd58444c442864467ef853bfea62d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustaf=20R=C3=A4ntil=C3=A4?= Date: Sat, 18 Jun 2022 10:22:45 +0200 Subject: [PATCH] feat(ts): added '--ts-non-exported' flag to define strategy for what to do with non-exported types fix #26 --- lib/bin/typeconv.ts | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/lib/bin/typeconv.ts b/lib/bin/typeconv.ts index b4113bf..fe05cf6 100644 --- a/lib/bin/typeconv.ts +++ b/lib/bin/typeconv.ts @@ -35,6 +35,7 @@ import { ExportRefMethod } from 'suretype' import { userPackage, userPackageUrl } from "../package" import { TypeImplementation } from "../types" import { ensureType } from "../utils" +import { FromTsOptions } from "core-types-ts" type SureTypeMissingRef = JsonSchemaToSuretypeOptions[ 'missingReference' ]; @@ -162,6 +163,30 @@ const oppaInstance = description: "Use 'unknown' type instead of 'any'", default: true, } ) + .add( { + name: 'ts-non-exported', + type: 'string', + argumentName: 'method', + description: "Strategy for non-exported types", + default: 'include-if-referenced', + values: [ + { 'fail': "Fail conversion" }, + { 'ignore': [ + "Don't include non-exported types,", + "even if referenced", + ] }, + { 'include': "Include non-exported types" }, + { 'inline': [ + "Don't include non-exported types, ", + "inline them if necessary.", + "Will fail on cyclic types" + ] }, + { 'include-if-referenced': [ + "Include non-exported types only if they", + "are referenced from exported types", + ] }, + ], + } ) .group( { name: "GraphQL", @@ -323,6 +348,7 @@ const { "ts-disable-lint-header": tsDisableLintHeader, "ts-descriptive-header": tsDescriptiveHeader, "ts-use-unknown": tsUseUnknown, + "ts-non-exported": tsNonExported, // JSON Schema @@ -368,6 +394,14 @@ if ( !ensureType< TypeImplementation >( ) ) throw new Error( ); +if ( !ensureType< FromTsOptions[ 'nonExported' ] >( + tsNonExported, + 'ts-non-exported', + [ 'fail', 'ignore', 'include', 'inline', 'include-if-referenced' ], + printHelp +) ) + throw new Error( ); + if ( !ensureType< ExportRefMethod | undefined >( stRefMethod, 'ref-method', @@ -387,7 +421,9 @@ if ( !ensureType< SureTypeMissingRef | undefined >( const getReader = ( ): Reader => { return fromType === 'ts' - ? getTypeScriptReader( ) + ? getTypeScriptReader( { + nonExported: tsNonExported, + } ) : fromType === 'jsc' ? getJsonSchemaReader( ) : fromType === 'oapi'