New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@spacefleet/json-schema-to-typescript

Package Overview
Dependencies
Maintainers
2
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@spacefleet/json-schema-to-typescript - npm Package Compare versions

Comparing version 13.0.1-spacefleet to 13.0.1-spacefleet-2

43

dist/src/generator.js

@@ -35,5 +35,5 @@ "use strict";

// @TODO: group by module
var imports = (0, lodash_1.flatMap)(__spreadArray(__spreadArray(__spreadArray([], (0, lodash_1.flatMap)(named, function (n) { return n === null || n === void 0 ? void 0 : n.imports; }).filter(Boolean), true), (0, lodash_1.flatMap)(interfaces, function (i) { return i === null || i === void 0 ? void 0 : i.imports; }).filter(Boolean), true), [
var imports = generateImports((0, lodash_1.flatMap)(__spreadArray(__spreadArray(__spreadArray([], (0, lodash_1.flatMap)(named, function (n) { var _a; return (_a = n === null || n === void 0 ? void 0 : n.imports) !== null && _a !== void 0 ? _a : []; }), true), (0, lodash_1.flatMap)(interfaces, function (i) { var _a; return (_a = i === null || i === void 0 ? void 0 : i.imports) !== null && _a !== void 0 ? _a : []; }), true), [
(_a = enums.imports) !== null && _a !== void 0 ? _a : []
], false)).map(function (i) { return i && generateImport(i); });
], false)));
return ([

@@ -56,15 +56,26 @@ options.bannerComment,

exports.generate = generate;
function generateImport(imported) {
var importStmt = 'import';
if (imported.typeOnly) {
importStmt += ' type';
}
if (imported.defaultExport) {
importStmt += " ".concat(imported.name);
}
else {
importStmt += " { ".concat(imported.name, " }");
}
importStmt += " from '".concat(imported.module, "';");
return importStmt;
function generateImports(imported) {
var typeImports = {};
var valueImports = {};
imported.forEach(function (t) {
var _a, _b;
var name = t.name;
if (t.defaultExport) {
name = "default as {t.name}";
}
if (t.typeOnly) {
typeImports[t.module] = __spreadArray(__spreadArray([], ((_a = typeImports[t.module]) !== null && _a !== void 0 ? _a : []), true), [name], false);
}
else {
valueImports[t.module] = __spreadArray(__spreadArray([], ((_b = valueImports[t.module]) !== null && _b !== void 0 ? _b : []), true), [name], false);
}
});
var importStmts = [];
(0, lodash_1.forEach)(typeImports, function (names, module) {
importStmts.push("import type {".concat(names.join(", "), "} from \"").concat(module, "\""));
});
(0, lodash_1.forEach)(valueImports, function (names, module) {
importStmts.push("import {".concat(names.join(", "), "} from \"").concat(module, "\""));
});
return importStmts;
}

@@ -305,3 +316,3 @@ function declareEnums(ast, options, processed) {

return {
type: '',
type: ast.params.name,
imports: [{ module: ast.params.module, name: ast.params.name, defaultExport: ast.params.defaultExport }]

@@ -308,0 +319,0 @@ };

{
"name": "@spacefleet/json-schema-to-typescript",
"version": "13.0.1-spacefleet",
"version": "13.0.1-spacefleet-2",
"description": "compile json schema to typescript typings",

@@ -5,0 +5,0 @@ "main": "dist/src/index.js",

@@ -1,3 +0,3 @@

import { flatMap, memoize, omit } from 'lodash'
import { DEFAULT_OPTIONS, Options } from './index'
import {flatMap, memoize, omit, forEach} from 'lodash'
import {DEFAULT_OPTIONS, Options} from './index'
import {

@@ -17,3 +17,3 @@ AST,

} from './types/AST'
import { log, toSafeString } from './utils'
import {log, toSafeString} from './utils'

@@ -26,7 +26,9 @@ export function generate(ast: AST, options = DEFAULT_OPTIONS): string {

// @TODO: group by module
const imports = flatMap([
...flatMap(named, n => n?.imports).filter(Boolean),
...flatMap(interfaces, i => i?.imports).filter(Boolean),
enums.imports ?? []
]).map(i => i && generateImport(i))
const imports = generateImports(
flatMap([
...flatMap(named, n => n?.imports ?? []),
...flatMap(interfaces, i => i?.imports ?? []),
enums.imports ?? []
])
)

@@ -52,18 +54,31 @@ return (

function generateImport(imported: TypeImport): string {
let importStmt = 'import'
if (imported.typeOnly) {
importStmt += ' type'
}
function generateImports(imported: TypeImport[]): string[] {
let typeImports: {[key: string]: string[]} = {}
let valueImports: {[key: string]: string[]} = {}
if (imported.defaultExport) {
importStmt += ` ${imported.name}`
} else {
importStmt += ` { ${imported.name} }`
}
imported.forEach(t => {
let name = t.name
if (t.defaultExport) {
name = `default as {t.name}`
}
importStmt += ` from '${imported.module}';`
if (t.typeOnly) {
typeImports[t.module] = [ ...(typeImports[t.module] ?? []), name ]
} else {
valueImports[t.module] = [ ...(valueImports[t.module] ?? []), name ]
}
})
return importStmt
let importStmts: string[] = []
forEach(typeImports, (names, module) => {
importStmts.push(`import type {${names.join(", ")}} from "${module}"`)
})
forEach(valueImports, (names, module) => {
importStmts.push(`import {${names.join(", ")}} from "${module}"`)
})
return importStmts
}

@@ -78,5 +93,5 @@

function declareEnums(ast: AST, options: Options, processed = new Set<AST>()): { type: string; imports?: TypeImport[] } {
function declareEnums(ast: AST, options: Options, processed = new Set<AST>()): {type: string; imports?: TypeImport[]} {
if (processed.has(ast)) {
return { type: '' }
return {type: ''}
}

@@ -95,3 +110,3 @@

const types = ast.params.map(ast => declareEnums(ast, options, processed))
const names = types.reduce((prev, { type }) => prev + type, '')
const names = types.reduce((prev, {type}) => prev + type, '')
return {

@@ -108,3 +123,3 @@ imports: flatMap(types.map(t => t.imports ?? [])),

}
const name = types.reduce((prev, { type }) => prev + type, '')
const name = types.reduce((prev, {type}) => prev + type, '')

@@ -119,3 +134,3 @@ return {

const types = getSuperTypesAndParams(ast).map(ast => declareEnums(ast, options, processed))
const name = types.reduce((prev, { type }) => prev + type, '')
const name = types.reduce((prev, {type}) => prev + type, '')

@@ -128,3 +143,3 @@ return {

default:
return { type: '' }
return {type: ''}
}

@@ -138,5 +153,5 @@ }

processed = new Set<AST>()
): ({ type: string; imports?: TypeImport[] } | undefined)[] {
): ({type: string; imports?: TypeImport[]} | undefined)[] {
if (processed.has(ast)) {
return [{ type: '' }]
return [{type: ''}]
}

@@ -166,3 +181,3 @@

return [{ type: '' }]
return [{type: ''}]
}

@@ -175,5 +190,5 @@

processed = new Set<AST>()
): ({ type: string; imports?: TypeImport[] } | undefined)[] {
): ({type: string; imports?: TypeImport[]} | undefined)[] {
if (processed.has(ast)) {
return [{ type: '' }]
return [{type: ''}]
}

@@ -190,3 +205,3 @@

case 'ENUM':
return [{ type: '' }]
return [{type: ''}]
case 'INTERFACE':

@@ -212,11 +227,11 @@ return flatMap(getSuperTypesAndParams(ast), ast =>

}
return [{ type: '' }]
return [{type: ''}]
}
}
function generateTypeUnmemoized(ast: AST, options: Options): { type: string; imports?: TypeImport[] } {
function generateTypeUnmemoized(ast: AST, options: Options): {type: string; imports?: TypeImport[]} {
const type = generateRawType(ast, options)
if (options.strictIndexSignatures && ast.keyName === '[k: string]') {
return { type: `${type.type} | undefined`, imports: type.imports }
return {type: `${type.type} | undefined`, imports: type.imports}
}

@@ -228,7 +243,7 @@

function generateRawType(ast: AST, options: Options): { type: string; imports?: TypeImport[] } {
function generateRawType(ast: AST, options: Options): {type: string; imports?: TypeImport[]} {
log('magenta', 'generator', ast)
if (hasStandaloneName(ast)) {
return { type: toSafeString(ast.standaloneName) }
return {type: toSafeString(ast.standaloneName)}
}

@@ -238,11 +253,11 @@

case 'ANY':
return { type: 'any' }
return {type: 'any'}
case 'ARRAY':
return (() => {
const { type, imports } = generateType(ast.params, options)
return { type: type.endsWith('"') ? '(' + type + ')[]' : type + '[]', imports }
const {type, imports} = generateType(ast.params, options)
return {type: type.endsWith('"') ? '(' + type + ')[]' : type + '[]', imports}
})()
case 'BOOLEAN':
return { type: 'boolean' }
return {type: 'boolean'}
case 'INTERFACE':

@@ -253,15 +268,15 @@ return generateInterface(ast, options)

case 'LITERAL':
return { type: JSON.stringify(ast.params) }
return {type: JSON.stringify(ast.params)}
case 'NEVER':
return { type: 'never' }
return {type: 'never'}
case 'NUMBER':
return { type: 'number' }
return {type: 'number'}
case 'NULL':
return { type: 'null' }
return {type: 'null'}
case 'OBJECT':
return { type: 'object' }
return {type: 'object'}
case 'REFERENCE':
return { type: ast.params }
return {type: ast.params}
case 'STRING':
return { type: 'string' }
return {type: 'string'}
case 'TUPLE':

@@ -340,3 +355,3 @@ return (() => {

return { type: typesToUnion.join('|'), imports }
return {type: typesToUnion.join('|'), imports}
}

@@ -353,11 +368,11 @@

case 'UNKNOWN':
return { type: 'unknown' }
return {type: 'unknown'}
case 'CUSTOM_TYPE':
if (ast.params.module) {
return {
type: '',
imports: [{ module: ast.params.module!, name: ast.params.name, defaultExport: ast.params.defaultExport }]
type: ast.params.name,
imports: [{module: ast.params.module!, name: ast.params.name, defaultExport: ast.params.defaultExport}]
}
}
return { type: ast.params.name }
return {type: ast.params.name}
}

@@ -369,3 +384,3 @@ }

*/
function generateSetOperation(ast: TIntersection | TUnion, options: Options): { type: string; imports?: TypeImport[] } {
function generateSetOperation(ast: TIntersection | TUnion, options: Options): {type: string; imports?: TypeImport[]} {
const members = (ast as TUnion).params.map(_ => generateType(_, options))

@@ -377,12 +392,12 @@ const separator = ast.type === 'UNION' ? '|' : '&'

: {
type: '(' + members.map(m => m.type).join(' ' + separator + ' ') + ')',
imports: flatMap(members.map(m => m.imports ?? []))
}
type: '(' + members.map(m => m.type).join(' ' + separator + ' ') + ')',
imports: flatMap(members.map(m => m.imports ?? []))
}
}
function generateInterface(ast: TInterface, options: Options): { type: string; imports?: TypeImport[] } {
function generateInterface(ast: TInterface, options: Options): {type: string; imports?: TypeImport[]} {
const fields = ast.params
.filter(_ => !_.isPatternProperty && !_.isUnreachableDefinition)
.map(
({ isRequired, keyName, ast }) =>
({isRequired, keyName, ast}) =>
[isRequired, keyName, ast, generateType(ast, options)] as [

@@ -397,3 +412,3 @@ boolean,

return {
imports: flatMap(fields, ([_1, _2, _3, { imports }]) => imports ?? []),
imports: flatMap(fields, ([_1, _2, _3, {imports}]) => imports ?? []),
type:

@@ -404,3 +419,3 @@ `{` +

.map(
([isRequired, keyName, ast, { type }]) =>
([isRequired, keyName, ast, {type}]) =>
(hasComment(ast) && !ast.standaloneName ? generateComment(ast.comment) + '\n' : '') +

@@ -422,6 +437,6 @@ escapeKeyName(keyName) +

function generateStandaloneEnum(ast: TEnum, options: Options): { type: string; imports?: TypeImport[] } {
const types = ast.params.map(({ ast, keyName }) => ({ keyName, ...generateType(ast, options) }))
function generateStandaloneEnum(ast: TEnum, options: Options): {type: string; imports?: TypeImport[]} {
const types = ast.params.map(({ast, keyName}) => ({keyName, ...generateType(ast, options)}))
const names = types.map(t => t.keyName + ' = ' + t.type)
const imports = flatMap(types, ({ imports }) => imports ?? [])
const imports = flatMap(types, ({imports}) => imports ?? [])

@@ -442,3 +457,3 @@ return {

function generateStandaloneInterface(ast: TNamedInterface, options: Options): { type: string; imports?: TypeImport[] } {
function generateStandaloneInterface(ast: TNamedInterface, options: Options): {type: string; imports?: TypeImport[]} {
const iface = generateInterface(ast, options)

@@ -457,4 +472,4 @@ return {

function generateStandaloneType(ast: ASTWithStandaloneName, options: Options): { type: string; imports?: TypeImport[] } {
const { type, imports } = generateType(omit<AST>(ast, 'standaloneName') as AST /* TODO */, options)
function generateStandaloneType(ast: ASTWithStandaloneName, options: Options): {type: string; imports?: TypeImport[]} {
const {type, imports} = generateType(omit<AST>(ast, 'standaloneName') as AST /* TODO */, options)

@@ -461,0 +476,0 @@ const exportedType =

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc