Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@
"istextorbinary": "~9.5.0",
"jju": "~1.4.0",
"js-levenshtein": "^1.1.6",
"json-schema-to-typescript": "^15.0.4",
"mime": "~4.1.0",
"open": "~11.0.0",
"semver": "~7.8.0",
Expand Down
62 changes: 13 additions & 49 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 22 additions & 18 deletions src/commands/actor/generate-schema-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ import { mkdir, stat, writeFile } from 'node:fs/promises';
import path from 'node:path';
import process from 'node:process';

import type { JSONSchema4 } from 'json-schema';
import { compile, type Options } from 'json-schema-to-typescript';

import { ApifyCommand } from '../../lib/command-framework/apify-command.js';
import { Args } from '../../lib/command-framework/args.js';
import { Flags } from '../../lib/command-framework/flags.js';
Expand All @@ -16,13 +13,13 @@ import {
readStorageSchema,
} from '../../lib/input_schema.js';
import { error, info, success, warning } from '../../lib/outputs.js';
import { compileSchema, type CompileSchemaOptions, declarationName } from '../../lib/schema-to-typescript.js';
import {
clearAllRequired,
makePropertiesRequired,
prepareFieldsSchemaForCompilation,
prepareKvsCollectionsForCompilation,
prepareOutputSchemaForCompilation,
stripTitles,
} from '../../lib/schema-transforms.js';

export const BANNER_COMMENT = `
Expand All @@ -31,7 +28,7 @@ export const BANNER_COMMENT = `
/* eslint-disable */
/* prettier-ignore-start */
/*
* This file was automatically generated by json-schema-to-typescript.
* This file was automatically generated from an Actor schema.
* DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file,
* and run apify actor generate-schema-types to regenerate this file.
*/
Expand Down Expand Up @@ -140,16 +137,12 @@ just as if the command were run from that directory with no argument.`;
? clearAllRequired(inputSchema)
: makePropertiesRequired(inputSchema);

const compileOptions: Partial<Options> = {
const compileOptions: CompileSchemaOptions = {
bannerComment: BANNER_COMMENT,
maxItems: -1,
unknownAny: true,
format: true,
additionalProperties: !this.flags.strict,
$refOptions: { resolve: { external: false, file: false, http: false } },
};

const result = await compile(stripTitles(schemaToCompile) as JSONSchema4, name, compileOptions);
const result = compileSchema(schemaToCompile, name, compileOptions);

const outputDir = path.resolve(effectiveCwd, this.flags.output);
await mkdir(outputDir, { recursive: true });
Expand Down Expand Up @@ -193,7 +186,7 @@ just as if the command were run from that directory with no argument.`;
}: {
cwd: string;
outputDir: string;
compileOptions: Partial<Options>;
compileOptions: CompileSchemaOptions;
}) {
const datasetResult = readDatasetSchema({ cwd });

Expand All @@ -220,7 +213,7 @@ just as if the command were run from that directory with no argument.`;

const schemaToCompile = this.flags.allOptional ? clearAllRequired(prepared) : prepared;

const result = await compile(stripTitles(schemaToCompile) as JSONSchema4, datasetName, compileOptions);
const result = compileSchema(schemaToCompile, datasetName, compileOptions);

const outputFile = path.join(outputDir, `${datasetName}.ts`);
await writeFile(outputFile, result, 'utf-8');
Expand All @@ -235,7 +228,7 @@ just as if the command were run from that directory with no argument.`;
}: {
cwd: string;
outputDir: string;
compileOptions: Partial<Options>;
compileOptions: CompileSchemaOptions;
}) {
const outputResult = readOutputSchema({ cwd });

Expand All @@ -262,7 +255,7 @@ just as if the command were run from that directory with no argument.`;

const schemaToCompile = this.flags.allOptional ? clearAllRequired(prepared) : prepared;

const result = await compile(stripTitles(schemaToCompile) as JSONSchema4, outputName, compileOptions);
const result = compileSchema(schemaToCompile, outputName, compileOptions);

const outputFile = path.join(outputDir, `${outputName}.ts`);
await writeFile(outputFile, result, 'utf-8');
Expand All @@ -277,7 +270,7 @@ just as if the command were run from that directory with no argument.`;
}: {
cwd: string;
outputDir: string;
compileOptions: Partial<Options>;
compileOptions: CompileSchemaOptions;
}) {
const kvsResult = readStorageSchema({ cwd, key: 'keyValueStore', label: 'Key-Value Store' });

Expand Down Expand Up @@ -305,14 +298,25 @@ just as if the command were run from that directory with no argument.`;
}

const parts: string[] = [];
const takenNames = new Set<string>();

for (const { name, schema } of collections) {
const schemaToCompile = this.flags.allOptional ? clearAllRequired(schema) : schema;

const compiled = await compile(stripTitles(schemaToCompile) as JSONSchema4, name, {
// Collection names that differ only in separators or non-ASCII characters would otherwise
// compile to the same identifier, producing a file that does not type-check.
let uniqueName = name;

for (let suffix = 2; takenNames.has(declarationName(uniqueName)); suffix++) {
uniqueName = `${name} ${suffix}`;
}

takenNames.add(declarationName(uniqueName));

const compiled = compileSchema(schemaToCompile, uniqueName, {
...compileOptions,
// Only the first collection gets the banner comment
bannerComment: parts.length === 0 ? (compileOptions.bannerComment as string) : '',
bannerComment: parts.length === 0 ? compileOptions.bannerComment : '',
});

parts.push(compiled);
Expand Down
Loading