grantila / typeconv

Convert between JSON Schema, TypeScript, GraphQL, Open API and SureType
MIT License
424 stars 8 forks source link

json-schema to suretype: import aliases are empty #18

Closed gCardinal closed 2 years ago

gCardinal commented 2 years ago

Running a simple conversion from json-schema to suretype results in imports from suretype that are unusable. See below:

/* tslint:disable */
/* eslint-disable */
/**
 * This file is generated by core-types-suretype on behalf of typeconv, DO NOT EDIT.
 * For more information, see:
 *  - {@link https://github.com/grantila/core-types-suretype}
 *  - {@link https://github.com/grantila/typeconv}
 */

import { suretype as , v as , compile as , annotate as  } from 'suretype';

// ... rest of the schemas, interfaces and validators

Generation was done in the most basic way possible I believe:

const fs = require('fs');
const path = require('path');
const typeconv = require('typeconv');

const { readdirSync, readFileSync, writeFileSync } = fs;
const { resolve, extname, basename } = path;
const { getJsonSchemaReader, getSureTypeWriter, makeConverter } = typeconv;

const schemasDirectory = resolve(__dirname, 'schemas');
const { convert } = makeConverter(getJsonSchemaReader(), getSureTypeWriter())

readdirSync(schemasDirectory).forEach(file => {
    if (extname(file).includes('json')) {
        const fileName = basename(file, extname(file));
        const filePath = resolve(__dirname, 'schemas', file);
        const fileContent = readFileSync(filePath).toString();
        const targetFile = resolve(__dirname, 'schemas', `${fileName}.ts`);

        convert({ data: fileContent }).then((converted) => {
            writeFileSync(targetFile, converted.data)
        })
    }
});
EricCrosson commented 2 years ago

I am seeing this too, using the following command

typeconv --from-type ts --to-type st -o suretype ./interfaces.ts

with typeconv 1.7.0

EricCrosson commented 2 years ago

ah, but only when typeconv was installed globally! When I used typeconv installed locally to my package.json, this error did not reproduce

moltar commented 2 years ago

I'm seeing the same, regardless of the installed location.

Running as:

node_modules/.bin/typeconv --from-type=oapi --to-type=st --output-directory=src/ foo.json
aphex commented 2 years ago

Same issue here, just doing string replacement for now to clean it up but would be nice to get this fixed.

aphex commented 2 years ago

Quick patch here incase this is blocking you

core-types-suretype+1.4.1.patch

diff --git a/node_modules/core-types-suretype/dist/lib/json-schema-to-suretype.js b/node_modules/core-types-suretype/dist/lib/json-schema-to-suretype.js
index c0d064d..5d9a249 100644
--- a/node_modules/core-types-suretype/dist/lib/json-schema-to-suretype.js
+++ b/node_modules/core-types-suretype/dist/lib/json-schema-to-suretype.js
@@ -158,7 +158,7 @@ function createImportHeader(regular, raw, compile, TypeOf, annotate) {
         ...(annotate ? ['annotate'] : []),
         ...(TypeOf ? ['TypeOf'] : []),
     ]
-        .map(name => helpers_1.factory.createImportSpecifier(undefined, helpers_1.t.ident(name)));
+        .map(name => helpers_1.factory.createImportSpecifier(undefined, undefined, helpers_1.t.ident(name)));
     return helpers_1.factory.createImportDeclaration(undefined, // decorators
     undefined, // modifiers
     helpers_1.factory.createImportClause(false, undefined, helpers_1.factory.createNamedImports(importedMembers)), helpers_1.factory.createStringLiteral('suretype', true));
grantila commented 2 years ago

Fixed in 1.8. This was caused by TypeScript breaking the API/ABI in a minor version, so having TypeScript > 4.5 installed in the same repo broke this. Now depending on 4.7.