import 'json-schema-to-typescript' only in dev mode

This commit is contained in:
Sebastian Seedorf
2022-08-18 15:10:32 +02:00
parent b97c922ff3
commit 75d06bef84
4 changed files with 8 additions and 7 deletions

1
src/next-types.d.ts vendored
View File

@@ -8,6 +8,7 @@ declare module 'next/config' {
export interface PublicRuntimeConfig {
TENANT_TYPE?: string
NODE_ENV?: string
}
const getConfig: () => {

View File

@@ -2,20 +2,20 @@ import { Validator } from 'jsonschema'
import { getDefaults, isObject, merge } from './defaults'
import { TransformedValidatorResult, ValidatorOptions } from './types'
import { transformInstance } from './transform'
import { compile, JSONSchema } from 'json-schema-to-typescript'
import type { JSONSchema } from 'json-schema-to-typescript'
import * as fs from 'fs/promises'
import { join } from 'path'
import { NetworkError } from '../utils/errors'
import getConfig from 'next/config'
const {
publicRuntimeConfig: { TENANT_TYPE }
publicRuntimeConfig: { TENANT_TYPE, NODE_ENV }
} = getConfig()
const validatorStorage = new Validator()
export async function addSchema(schemas: JSONSchema[]) {
if (TENANT_TYPE === 'local') {
if (TENANT_TYPE === 'local' && NODE_ENV === 'development') {
const fileName = join('./src/types/ApiSchemas.ts')
const fileNameFrontend = join('./src/types/ApiSchemasFrontend.ts')
const style = await fs.readFile('./.prettierrc.json', 'utf8').then(JSON.parse)
@@ -25,6 +25,7 @@ export async function addSchema(schemas: JSONSchema[]) {
if (!schema.id) throw new SyntaxError('Id of schema has to be defined!')
validatorStorage.addSchema(schema)
// compile schema to interface
const { compile } = await import('json-schema-to-typescript')
let entity = await compile(schema, schema.id, {
style,
bannerComment: data === '' ? undefined : '\n\n\n',

View File

@@ -70,6 +70,4 @@ export function addSchemas() {
])
}
export const waitForInitSchemas = new Resolvable(() => {
return addSchemas()
})
export const waitForInitSchemas = new Resolvable(addSchemas)