Create type-safe input and output configurations for your Apify Actors
npm install typed-actor-configsThis package uses TypeScript's typescript-plugin-json-const plugin as a peer dependency.
It infers the types described in the input_schema.json and dataset_schema.json files and generates a TypeScript interface for each of them.
First set up the plugin in your tsconfig.json file:
{
"compilerOptions": {
"plugins": [{ "name": "typescript-plugin-json-const" }]
}
}This will treat your json imports as const.
Then just import the files in your code and use The Infer Types on them.
import { ActorInput } from 'typed-actor-configs';
import INPUT_SCHEMA from './path/to/input_schema.json';
import DATASET_SCHEMA from './path/to/dataset_schema.json';
export type Input = InferInput<typeof INPUT_SCHEMA>;
export type DatasetItem = InferDataset<typeof DATASET_SCHEMA>;Now you can use the Input and Dataset types in your code.
import { Input, DatasetItem } from './my/types/inferred/from/schemas.ts';
import { Actor } from 'apify';
await Actor.init();
const input = (await Actor.getInput<Input>())!;
...
await Actor.pushData<DatasetItem>({
...
});Not all input types are supported yet.
Currently the following types are supported:
- nested objects
- nested arrays
Check the issues for more details on known bugs and missing features.
Any issues, suggestions or feature requests are welcome. Feel free to open an issue.