Skip to content

Regenerate Types Without Starting the Server

Sometimes you encounter a chicken-and-egg scenario: the restura.schema.json file was updated, but you can’t start the server because one of the generated types is incorrect. This guide shows you how to regenerate types directly from your schema file without starting the server.


When you modify restura.schema.json (either manually or after a merge conflict), the TypeScript type definitions may become out of sync. Normally, the types are regenerated when the server starts—but if the types are broken, the server won’t compile.

Common symptoms:

  • TypeScript errors referencing generated types in api.d.ts or models.d.ts
  • Server fails to start due to type mismatches
  • IDE showing errors in files that reference Api.* or model types

Use this standalone script to regenerate types directly from your schema file, bypassing the server entirely.

Create a file called regenerate-types.mjs in your project root:

import { apiGenerator, modelGenerator, resturaGlobalTypesGenerator } from '@restura/core';
import fs from 'fs/promises';
import path from 'path';
// Get command line arguments
const [, , schemaPath, outputDir] = process.argv;
if (!schemaPath || !outputDir) {
console.error('Usage: node regenerate-types.mjs <schema-path> <output-directory>');
process.exit(1);
}
async function generateTypes() {
try {
// Read and parse schema file
const schemaContent = await fs.readFile(schemaPath, 'utf-8');
const schema = JSON.parse(schemaContent);
// Ensure output directory exists
await fs.mkdir(outputDir, { recursive: true });
// Generate type definitions
await fs.writeFile(path.join(outputDir, 'api.d.ts'), await apiGenerator(schema), 'utf-8');
await fs.writeFile(path.join(outputDir, 'models.d.ts'), await modelGenerator(schema), 'utf-8');
await fs.writeFile(path.join(outputDir, 'restura.d.ts'), resturaGlobalTypesGenerator(), 'utf-8');
console.log('Type definitions generated successfully!');
} catch (error) {
console.error('Error generating type definitions:', error.message);
process.exit(1);
}
}
generateTypes();

Execute the script with your schema path and output directory:

Terminal window
node regenerate-types.mjs ./restura.schema.json ./src/@types

After the script runs successfully:

  1. Check that api.d.ts, models.d.ts, and restura.d.ts were updated
  2. Your IDE should now show the correct types
  3. Try starting your server again

Use this script when:

  • You’ve edited restura.schema.json manually and need fresh types
  • After resolving merge conflicts in the schema file
  • When the server won’t start due to type definition errors
  • During development when you need to quickly regenerate types

For convenience, add a script to your package.json:

{
"scripts": {
"regenerate-types": "node regenerate-types.mjs ./restura.schema.json ./src/@types"
}
}

Then you can simply run:

Terminal window
npm run regenerate-types