|
| 1 | +# Apify Schema Tools |
| 2 | + |
| 3 | +This is a tool intended for Apify's actors developers. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- Generate Apify input and dataset schemas from separate JSON schemas. |
| 8 | +- Generate TypeScript types from JSON schemas using [json-schema-to-typescript](https://www.npmjs.com/package/json-schema-to-typescript). |
| 9 | + |
| 10 | +### "Under the hoods" features |
| 11 | + |
| 12 | +- Remove invalid fields from the input schema to generate the Apify schema, but still include them to generate the TypeScript interface. |
| 13 | +- Overwrite the output schema's `fields` field. |
| 14 | +- Use `Input` and `DatasetItem` as TypeScript interface names. |
| 15 | + |
| 16 | +## Install |
| 17 | + |
| 18 | +Install globally: |
| 19 | + |
| 20 | +```sh |
| 21 | +npm i -g apify-schema-tools |
| 22 | +``` |
| 23 | + |
| 24 | +Install as a development dependency: |
| 25 | + |
| 26 | +```sh |
| 27 | +npm i -D apify-schema-tools |
| 28 | +``` |
| 29 | + |
| 30 | +## Available commands |
| 31 | + |
| 32 | +- `generate-apify-schema` |
| 33 | +- `generate-apify-type` |
| 34 | + |
| 35 | +## CLI options |
| 36 | + |
| 37 | +- `--input`: generate input schema or type |
| 38 | +- `--dataset`: generate dataset schema or type |
| 39 | +- `--input-src`: path of the input JSON schema (default = `src-schemas/input.json`). |
| 40 | +- `--dataset-src`: path of the dataset JSON schema (default = `src-schemas/dataset-item.json`). |
| 41 | + |
| 42 | +Only for `generate-apify-schema`: |
| 43 | + |
| 44 | +- `--input-schema`: path of the Actor's input schema (default = `.actor/input_schema.json`). |
| 45 | +- `--dataset-schema`: path of the Actor's dataset schema (default = `.actor/dataset_schema.json`). |
| 46 | + |
| 47 | +Only for `generate-apify-type`: |
| 48 | + |
| 49 | +- `--input-type`: path of the TypeScript file that will contain the input's type (default = `src/generated/input.ts`). |
| 50 | +- `--dataset-type`: path of the TypeScript file that will contain the dataset's type (default = `src/generated/dataset.ts`). |
| 51 | + |
| 52 | +## Examples |
| 53 | + |
| 54 | +Assume you have defined the following schemas: |
| 55 | + |
| 56 | +- `src-schemas/input.json` |
| 57 | + |
| 58 | +```jsonc |
| 59 | +{ |
| 60 | + "title": "My actor's input", |
| 61 | + "type": "object", |
| 62 | + "schemaVersion": 1, |
| 63 | + "properties": { |
| 64 | + "inputData": { |
| 65 | + "title": "Input Data", |
| 66 | + "type": "array", |
| 67 | + "description": "Some input data in json format.", |
| 68 | + "editor": "json", |
| 69 | + "prefill": [ |
| 70 | + { |
| 71 | + "title": "Test title" |
| 72 | + } |
| 73 | + ], |
| 74 | + "items": { |
| 75 | + "type": "object", |
| 76 | + "properties": { |
| 77 | + "title": { |
| 78 | + "type": "string" |
| 79 | + } |
| 80 | + }, |
| 81 | + "required": [ |
| 82 | + "title" |
| 83 | + ] |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | +} |
| 88 | +``` |
| 89 | + |
| 90 | +- `src-schemas/dataset-item.json` |
| 91 | + |
| 92 | +```jsonc |
| 93 | +{ |
| 94 | + "$schema": "http://json-schema.org/draft-07/schema#", |
| 95 | + "title": "My actor's dataset schema", |
| 96 | + "type": "object", |
| 97 | + "properties": { |
| 98 | + "value": { |
| 99 | + "type": "string" |
| 100 | + }, |
| 101 | + "category": { |
| 102 | + "enum": [ |
| 103 | + "cat1", |
| 104 | + "cat2" |
| 105 | + ] |
| 106 | + }, |
| 107 | + }, |
| 108 | + "required": [ |
| 109 | + "value", |
| 110 | + "category" |
| 111 | + ] |
| 112 | +} |
| 113 | +``` |
| 114 | + |
| 115 | +### Generate Apify input schema |
| 116 | + |
| 117 | +```sh |
| 118 | +npx generate-apify-schema --input --input-src=src-schemas/input.json --input-schema=.actor/input_schema.json |
| 119 | +``` |
| 120 | + |
| 121 | +### Generate TypeScript type file for the output schema |
| 122 | + |
| 123 | +```sh |
| 124 | +npx generate-apify-type --dataset --dataset-src=src-schemas/dataset-item.json --dataset-type=src/generated/dataset-item.ts |
| 125 | +``` |
| 126 | + |
| 127 | +### Generate everything using the default settings |
| 128 | + |
| 129 | +The scripts expect to find the files `.actor/dataset_schema.json`, `src-schemas/input.json`, `src-schemas/dataset-item.json`. |
| 130 | +The files `.actor/input_schema.json`, `src/generated/input.ts`, and `src/generated/dataset.ts` will be generated. |
| 131 | + |
| 132 | +```sh |
| 133 | +npx generate-apify-type --input --dataset && npx generate-apify-schema --input --dataset |
| 134 | +``` |
| 135 | + |
| 136 | +Results: |
| 137 | + |
| 138 | +- `.actor/input_schema.json` |
| 139 | + |
| 140 | +```jsonc |
| 141 | +{ |
| 142 | + "title": "My actor's input", |
| 143 | + "type": "object", |
| 144 | + "schemaVersion": 1, |
| 145 | + "properties": { |
| 146 | + // The property `items` has been omitted because it would fail Apify's validation |
| 147 | + "inputData": { |
| 148 | + "title": "Input Data", |
| 149 | + "type": "array", |
| 150 | + "description": "Some input data in json format.", |
| 151 | + "editor": "json", |
| 152 | + "prefill": [ |
| 153 | + { |
| 154 | + "title": "Test title" |
| 155 | + } |
| 156 | + ] |
| 157 | + } |
| 158 | + } |
| 159 | +} |
| 160 | +``` |
| 161 | + |
| 162 | +- `.actor/dataset_schema.json` (this file should already exist: the schema is set as `fields` value) |
| 163 | + |
| 164 | +```jsonc |
| 165 | +{ |
| 166 | + "actorSpecification": 1, |
| 167 | + "fields": { |
| 168 | + "$schema": "http://json-schema.org/draft-07/schema#", |
| 169 | + "title": "My actor's dataset schema", |
| 170 | + "type": "object", |
| 171 | + "properties": { |
| 172 | + "value": { |
| 173 | + "type": "string" |
| 174 | + }, |
| 175 | + "category": { |
| 176 | + "enum": [ |
| 177 | + "cat1", |
| 178 | + "cat2" |
| 179 | + ] |
| 180 | + }, |
| 181 | + }, |
| 182 | + "required": [ |
| 183 | + "value", |
| 184 | + "category" |
| 185 | + ] |
| 186 | + }, |
| 187 | + "views": {} |
| 188 | +} |
| 189 | +``` |
| 190 | + |
| 191 | +- `src/generated/input.ts` |
| 192 | + |
| 193 | +```ts |
| 194 | +/* eslint-disable */ |
| 195 | +/** |
| 196 | + * This file was automatically generated by json-schema-to-typescript. |
| 197 | + * DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file, |
| 198 | + * and run json-schema-to-typescript to regenerate this file. |
| 199 | + */ |
| 200 | + |
| 201 | +/** |
| 202 | + * Some input data in json format. |
| 203 | + */ |
| 204 | +export type InputData = { |
| 205 | + title: string; |
| 206 | +}[]; |
| 207 | + |
| 208 | +export interface Input { |
| 209 | + inputData?: InputData; |
| 210 | +} |
| 211 | +``` |
| 212 | + |
| 213 | +- `src/generated/dataset-item.ts` |
| 214 | + |
| 215 | +```ts |
| 216 | +/* eslint-disable */ |
| 217 | +/** |
| 218 | + * This file was automatically generated by json-schema-to-typescript. |
| 219 | + * DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file, |
| 220 | + * and run json-schema-to-typescript to regenerate this file. |
| 221 | + */ |
| 222 | + |
| 223 | +export interface DatasetItem { |
| 224 | + value: string; |
| 225 | + category: "cat1" | "cat2"; |
| 226 | +} |
| 227 | + |
| 228 | +``` |
0 commit comments