apify-schema-tools 2.0.4 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (54) hide show
  1. package/.cspell/custom-dictionary.txt +1 -0
  2. package/.husky/pre-commit +0 -8
  3. package/CHANGELOG.md +21 -0
  4. package/README.md +109 -45
  5. package/biome.json +121 -9
  6. package/dist/apify-schema-tools.d.ts +1 -0
  7. package/dist/apify-schema-tools.d.ts.map +1 -1
  8. package/dist/apify-schema-tools.js +133 -106
  9. package/dist/apify-schema-tools.js.map +1 -1
  10. package/dist/apify.d.ts +1 -1
  11. package/dist/apify.d.ts.map +1 -1
  12. package/dist/apify.js +4 -2
  13. package/dist/apify.js.map +1 -1
  14. package/dist/configuration.d.ts +17 -16
  15. package/dist/configuration.d.ts.map +1 -1
  16. package/dist/configuration.js +8 -1
  17. package/dist/configuration.js.map +1 -1
  18. package/dist/json-schemas.d.ts.map +1 -1
  19. package/dist/json-schemas.js +31 -21
  20. package/dist/json-schemas.js.map +1 -1
  21. package/dist/typescript.d.ts +1 -0
  22. package/dist/typescript.d.ts.map +1 -1
  23. package/dist/typescript.js +294 -194
  24. package/dist/typescript.js.map +1 -1
  25. package/package.json +16 -5
  26. package/samples/all-defaults/src/generated/dataset.ts +1 -0
  27. package/samples/all-defaults/src/generated/input-utils.ts +5 -1
  28. package/samples/all-defaults/src/generated/input.ts +2 -0
  29. package/samples/all-defaults/src-schemas/input.json +6 -3
  30. package/samples/deep-merged-schemas/src/generated/dataset.ts +1 -0
  31. package/samples/deep-merged-schemas/src/generated/input-utils.ts +5 -1
  32. package/samples/deep-merged-schemas/src/generated/input.ts +2 -0
  33. package/samples/deep-merged-schemas/src-schemas/input.json +6 -3
  34. package/samples/merged-schemas/src/generated/dataset.ts +1 -0
  35. package/samples/merged-schemas/src/generated/input-utils.ts +5 -1
  36. package/samples/merged-schemas/src/generated/input.ts +3 -0
  37. package/samples/merged-schemas/src-schemas/input.json +6 -3
  38. package/samples/package-json-config/custom-src-schemas/input.json +6 -3
  39. package/samples/package-json-config/package.json +8 -2
  40. package/samples/package-json-config/src/custom-generated/dataset.ts +1 -0
  41. package/samples/package-json-config/src/custom-generated/input-utils.ts +5 -1
  42. package/samples/package-json-config/src/custom-generated/input.ts +2 -0
  43. package/src/apify-schema-tools.ts +179 -150
  44. package/src/apify.ts +6 -4
  45. package/src/configuration.ts +14 -5
  46. package/src/json-schemas.ts +44 -22
  47. package/src/typescript.ts +370 -207
  48. package/test/apify-schema-tools.test.ts +122 -124
  49. package/test/apify.test.ts +8 -6
  50. package/test/common.ts +2 -2
  51. package/test/configuration.test.ts +1 -1
  52. package/test/json-schemas.test.ts +56 -40
  53. package/test/typescript.test.ts +181 -31
  54. package/tsconfig.json +1 -1
@@ -1,4 +1,5 @@
1
1
  # Custom Dictionary Words
2
2
  apify
3
+ Filenaming
3
4
  nargs
4
5
  subparsers
package/.husky/pre-commit CHANGED
@@ -20,14 +20,6 @@ cd samples/package-json-config
20
20
  node $EXEC_PATH check
21
21
  cd ../..
22
22
 
23
- echo "Checking for .only() in test files..."
24
-
25
- # Check for .only( in test files
26
- if grep -r "\.only(" --include="*.test.ts" .; then
27
- echo "Error: found \".only(\" in test files. Please remove focused tests before committing."
28
- exit 1
29
- fi
30
-
31
23
  echo "Running tests..."
32
24
 
33
25
  npm test
package/CHANGELOG.md CHANGED
@@ -1,5 +1,26 @@
1
1
  # Changelog
2
2
 
3
+ ## 2.1.0
4
+
5
+ ### Added
6
+
7
+ - Support for JSON schema's `additionalProperties` field.
8
+ - `additionalProperties` is set to true by default.
9
+
10
+ ### Changed
11
+
12
+ - The `init` command will not override existing source schemas anymore.
13
+
14
+ ### Documentation
15
+
16
+ - The new features of v2 are now documented in the README file.
17
+
18
+ ## 2.0.5
19
+
20
+ ### Fixed
21
+
22
+ - Fix linting in generated TypeScript code with some ESLint rules.
23
+
3
24
  ## 2.0.4
4
25
 
5
26
  ### Fixed
package/README.md CHANGED
@@ -50,8 +50,75 @@ Let's assume you are starting from a new project created from an
50
50
  npm i -D apify-schema-tools
51
51
  ```
52
52
 
53
- Now the command `apify-schema-tools` is installed for the current project.
54
- You can check which options are available:
53
+ 2. Initialize your project with default settings:
54
+
55
+ ```sh
56
+ npx apify-schema-tools init
57
+ ```
58
+
59
+ This command will:
60
+ - Create a `src-schemas` folder with `input.json` and `dataset-item.json` files.
61
+ - Create the necessary `.actor` files if they don't exist.
62
+ - Add configuration to your `package.json`.
63
+ - Add a `generate` script to your `package.json`.
64
+
65
+ 3. Generate JSON schemas and TypeScript types from the source schemas:
66
+
67
+ ```sh
68
+ npm apify-schema-tools sync
69
+ ```
70
+
71
+ 4. Now, you will be able to use TypeScript types and utilities in your project:
72
+
73
+ ```ts
74
+ import { Actor } from 'apify';
75
+
76
+ import type { DatasetItem } from './generated/dataset.ts';
77
+ import type { Input } from './generated/input.ts';
78
+ import { getInputWithDefaultValues, type InputWithDefaults } from './generated/input-utils.ts';
79
+
80
+ await Actor.init();
81
+
82
+ const input: InputWithDefaults = getInputWithDefaultValues(await Actor.getInput<Input>());
83
+
84
+ '...'
85
+
86
+ await Actor.pushData<DatasetItem>({
87
+ tile: '...',
88
+ url: '...',
89
+ text: '...',
90
+ timestamp: '...',
91
+ });
92
+
93
+ await Actor.exit();
94
+ ```
95
+
96
+ ## Configuration
97
+
98
+ You can configure `apify-schema-tools` in two ways:
99
+
100
+ ### Using package.json configuration
101
+
102
+ The `init` command automatically adds configuration to your `package.json`. You can also manually add an `apify-schema-tools` section to customize the behavior:
103
+
104
+ ```json
105
+ {
106
+ "name": "my-actor",
107
+ "version": "1.0.0",
108
+ "apify-schema-tools": {
109
+ "input": ["input", "dataset"],
110
+ "output": ["json-schemas", "ts-types"],
111
+ "srcInput": "src-schemas/input.json",
112
+ "srcDataset": "src-schemas/dataset-item.json",
113
+ "outputTSDir": "src/generated",
114
+ "includeInputUtils": true
115
+ }
116
+ }
117
+ ```
118
+
119
+ ### Using command-line arguments
120
+
121
+ You can also pass options directly to the `sync` command. You can check which options are available:
55
122
 
56
123
  ```console
57
124
  $ npx apify-schema-tools --help
@@ -69,8 +136,6 @@ optional arguments:
69
136
  -h, --help show this help message and exit
70
137
  ```
71
138
 
72
- You can also check the options available for a specific command:
73
-
74
139
  ```console
75
140
  $ npx apify-schema-tools sync --help
76
141
  usage: apify-schema-tools sync [-h] [-i [{input,dataset} ...]] [-o [{json-schemas,ts-types} ...]] [--src-input SRC_INPUT] [--src-dataset SRC_DATASET] [--add-input ADD_INPUT] [--add-dataset ADD_DATASET] [--input-schema INPUT_SCHEMA] [--dataset-schema DATASET_SCHEMA] [--output-ts-dir OUTPUT_TS_DIR]
@@ -101,16 +166,17 @@ optional arguments:
101
166
  include input utilities in the generated TypeScript files: 'input' input and 'ts-types' output are required (default: true)
102
167
  ```
103
168
 
104
- You can customize the path of all the files involved in the generation.
105
- In this case, we will use the default locations, so the commands will be simpler.
169
+ ## Setting up your project manually
106
170
 
107
- 2. Create a `src-schemas` folder:
171
+ If you prefer to set up your project manually instead of using the `init` command, you can follow these steps:
172
+
173
+ 1. Create a `src-schemas` folder:
108
174
 
109
175
  ```sh
110
176
  mkdir src-schemas
111
177
  ```
112
178
 
113
- 3. Create the files `input.json` and `dataset-item.json` inside the `src-schemas`. Here is some example content:
179
+ 2. Create the files `input.json` and `dataset-item.json` inside the `src-schemas`. Here is some example content:
114
180
 
115
181
  ```json
116
182
  {
@@ -132,7 +198,8 @@ mkdir src-schemas
132
198
  }
133
199
  }
134
200
  },
135
- "required": ["startUrls"]
201
+ "required": ["startUrls"],
202
+ "additionalProperties": false
136
203
  }
137
204
  ```
138
205
 
@@ -167,7 +234,7 @@ mkdir src-schemas
167
234
  }
168
235
  ```
169
236
 
170
- 4. Create the file `.actor/dataset_schema.json` and enter some empty content:
237
+ 3. Create the file `.actor/dataset_schema.json` and enter some empty content:
171
238
 
172
239
  ```json
173
240
  {
@@ -177,7 +244,7 @@ mkdir src-schemas
177
244
  }
178
245
  ```
179
246
 
180
- 5. Link the dataset schema in `.actor/actor.json`:
247
+ 4. Link the dataset schema in `.actor/actor.json`:
181
248
 
182
249
  ```json
183
250
  {
@@ -191,49 +258,40 @@ mkdir src-schemas
191
258
  }
192
259
  ```
193
260
 
194
- 6. Add the script to `package.json`:
195
-
196
- ```json
197
- {
198
- "...": "...",
199
- "scripts": {
200
- "...": "...",
201
- "generate": "apify-schema-tools sync"
202
- }
203
- }
204
- ```
205
261
 
206
- 7. Generate JSON schemas and TypeScript types from the source schemas:
262
+ 5. Generate JSON schemas and TypeScript types from the source schemas:
207
263
 
208
264
  ```sh
209
- npm run generate
265
+ npm apify-schema-tools sync
210
266
  ```
211
267
 
212
- 8. Now, you will be able to use TypeScript types and utilities in your project:
213
-
214
- ```ts
215
- import { Actor } from 'apify';
268
+ ## Checking if the schemas are in sync with the source schemas
216
269
 
217
- import type { DatasetItem } from './generated/dataset.ts';
218
- import type { Input } from './generated/input.ts';
219
- import { getInputWithDefaultValues, type InputWithDefaults } from './generated/input-utils.ts';
270
+ The `check` command allows you to verify that your generated schemas and TypeScript files are up-to-date with your source schemas. This is particularly useful in CI/CD pipelines to ensure that developers haven't forgotten to run the generation after making changes to the source schemas.
220
271
 
221
- await Actor.init();
272
+ ```sh
273
+ npx apify-schema-tools check
274
+ ```
222
275
 
223
- const input: InputWithDefaults = getInputWithDefaultValues(await Actor.getInput<Input>());
276
+ The `check` command will:
277
+ - Compare the current generated files with what would be generated from the source schemas
278
+ - Exit with code 0 if everything is in sync
279
+ - Exit with code 1 if there are differences, showing you which files are out of sync
224
280
 
225
- '...'
281
+ You can add this to your CI pipeline to automatically detect when schemas need to be regenerated:
226
282
 
227
- await Actor.pushData<DatasetItem>({
228
- tile: '...',
229
- url: '...',
230
- text: '...',
231
- timestamp: '...',
232
- });
233
-
234
- await Actor.exit();
283
+ ```json
284
+ {
285
+ "scripts": {
286
+ "generate": "apify-schema-tools sync",
287
+ "check-schemas": "apify-schema-tools check",
288
+ "test": "npm run check-schemas && npm run test:unit"
289
+ }
290
+ }
235
291
  ```
236
292
 
293
+ The `check` command accepts the same configuration options as the `sync` command, either through `package.json` configuration or command-line arguments, ensuring it checks the same files that would be generated.
294
+
237
295
  ## Extra features
238
296
 
239
297
  ### Keep only allowed properties in Input schema
@@ -273,12 +331,14 @@ An example:
273
331
  {
274
332
  "title": "My input schema",
275
333
  "description": "My input properties",
334
+ "type": "object",
276
335
  "properties": {
277
336
  "a": { "type": "string", "position": 3 },
278
337
  "b": { "type": "string" }, // will be last, because it has no position
279
338
  "c": { "type": "string", "position": 1 }
280
339
  },
281
- "required": ["a"]
340
+ "required": ["a"],
341
+ "additionalProperties": false
282
342
  }
283
343
  ```
284
344
 
@@ -286,11 +346,13 @@ An example:
286
346
  # Additional input schema
287
347
  {
288
348
  "description": "My input properties, a bit changed", // will override the description
349
+ "type": "object",
289
350
  "properties": {
290
351
  "c": { "type": "boolean", "position": 5 }, // will override also the position
291
352
  "d": { "type": "string", "position": 1 } // will be first
292
353
  },
293
- "required": ["c", "d"] // will be merged to the source required parameters
354
+ "required": ["c", "d"], // will be merged to the source required parameters
355
+ "additionalProperties": false
294
356
  }
295
357
  ```
296
358
 
@@ -299,13 +361,15 @@ An example:
299
361
  {
300
362
  "title": "My input schema",
301
363
  "description": "My input properties, a bit changed",
364
+ "type": "object",
302
365
  "properties": {
303
366
  "d": { "type": "string" },
304
367
  "a": { "type": "string" },
305
368
  "c": { "type": "boolean" },
306
369
  "b": { "type": "string" }
307
370
  },
308
- "required": ["a", "c", "d"]
371
+ "required": ["a", "c", "d"],
372
+ "additionalProperties": false
309
373
  }
310
374
  ```
311
375
 
package/biome.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "$schema": "https://biomejs.dev/schemas/1.9.4/schema.json",
2
+ "$schema": "https://biomejs.dev/schemas/2.1.0/schema.json",
3
3
  "vcs": {
4
4
  "enabled": true,
5
5
  "clientKind": "git",
@@ -7,25 +7,137 @@
7
7
  },
8
8
  "files": {
9
9
  "ignoreUnknown": false,
10
- "ignore": []
10
+ "includes": ["**"]
11
11
  },
12
12
  "formatter": {
13
13
  "enabled": true,
14
14
  "indentStyle": "tab",
15
15
  "lineWidth": 120
16
16
  },
17
- "organizeImports": {
18
- "enabled": true
17
+ "javascript": {
18
+ "formatter": {
19
+ "quoteStyle": "double"
20
+ }
19
21
  },
22
+ "assist": { "actions": { "source": { "organizeImports": "on" } } },
20
23
  "linter": {
21
24
  "enabled": true,
25
+ "domains": {
26
+ "test": "all",
27
+ "project": "all"
28
+ },
22
29
  "rules": {
23
- "recommended": true
30
+ "recommended": true,
31
+ "complexity": {
32
+ "noExcessiveCognitiveComplexity": "on",
33
+ "noExcessiveNestedTestSuites": "on",
34
+ "noForEach": "on",
35
+ "noUselessStringConcat": "on",
36
+ "noVoid": "on",
37
+ "useSimplifiedLogicExpression": "on",
38
+ "useWhile": "on"
39
+ },
40
+ "correctness": {
41
+ "noPrivateImports": "on",
42
+ "noUndeclaredDependencies": "on",
43
+ "noUndeclaredVariables": "on"
44
+ },
45
+ "nursery": {
46
+ "noAwaitInLoop": "on",
47
+ "noBitwiseOperators": "on",
48
+ "noConstantBinaryExpression": "on",
49
+ "noExcessiveLinesPerFunction": "on",
50
+ "noImportCycles": "on",
51
+ "noMagicNumbers": "on",
52
+ "noShadow": "on",
53
+ "noUnassignedVariables": "on",
54
+ "noUselessBackrefInRegex": "on",
55
+ "useAdjacentGetterSetter": "on",
56
+ "useIterableCallbackReturn": "on",
57
+ "useJsonImportAttribute": "on",
58
+ "useNumericSeparators": "on",
59
+ "useObjectSpread": "on"
60
+ },
61
+ "performance": {
62
+ "noBarrelFile": "on",
63
+ "noDelete": "on",
64
+ "noNamespaceImport": "on",
65
+ "noReExportAll": "on",
66
+ "useTopLevelRegex": "on"
67
+ },
68
+ "style": {
69
+ "noCommonJs": "on",
70
+ "noDefaultExport": "on",
71
+ "noDoneCallback": "on",
72
+ "noEnum": "on",
73
+ "noExportedImports": "on",
74
+ "noInferrableTypes": "on",
75
+ "noNamespace": "on",
76
+ "noNegationElse": "on",
77
+ "noNestedTernary": "on",
78
+ "noParameterAssign": "on",
79
+ "noParameterProperties": "on",
80
+ "noProcessEnv": "on",
81
+ "noUnusedTemplateLiteral": "on",
82
+ "noUselessElse": "on",
83
+ "noYodaExpression": "on",
84
+ "useAsConstAssertion": "on",
85
+ "useAtIndex": "on",
86
+ "useBlockStatements": "on",
87
+ "useCollapsedElseIf": "on",
88
+ "useCollapsedIf": "on",
89
+ "useConsistentArrayType": "on",
90
+ "useConsistentBuiltinInstantiation": "on",
91
+ "useConsistentMemberAccessibility": "on",
92
+ "useDefaultParameterLast": "on",
93
+ "useDefaultSwitchClause": "on",
94
+ "useExplicitLengthCheck": "on",
95
+ "useFilenamingConvention": "on",
96
+ "useForOf": "on",
97
+ "useNamingConvention": "on",
98
+ "useNodeAssertStrict": "on",
99
+ "useNumberNamespace": "on",
100
+ "useShorthandAssign": "on",
101
+ "useSingleVarDeclarator": "on",
102
+ "useThrowNewError": "on",
103
+ "useThrowOnlyError": "on",
104
+ "useTrimStartEnd": "on"
105
+ },
106
+ "suspicious": {
107
+ "noDuplicateTestHooks": "on",
108
+ "noEmptyBlockStatements": "on",
109
+ "noEvolvingTypes": "on",
110
+ "noExportsInTest": "on",
111
+ "noFocusedTests": "on",
112
+ "noMisplacedAssertion": "on",
113
+ "useNumberToFixedDigitsArgument": "on"
114
+ }
24
115
  }
25
116
  },
26
- "javascript": {
27
- "formatter": {
28
- "quoteStyle": "double"
117
+ "overrides": [
118
+ {
119
+ "includes": ["test/**"],
120
+ "linter": {
121
+ "rules": {
122
+ "nursery": {
123
+ "noExcessiveLinesPerFunction": "off",
124
+ "noMagicNumbers": "off"
125
+ },
126
+ "style": {
127
+ "useNamingConvention": "off"
128
+ }
129
+ }
130
+ }
131
+ },
132
+ {
133
+ "includes": ["samples/**"],
134
+ "linter": {
135
+ "rules": {
136
+ "correctness": {
137
+ "noUndeclaredDependencies": "off"
138
+ }
139
+ }
140
+ }
29
141
  }
30
- }
142
+ ]
31
143
  }
@@ -1,3 +1,4 @@
1
1
  #!/usr/bin/env node
2
+ /** biome-ignore-all lint/style/useNamingConvention: the package `argparse` uses snake_case names */
2
3
  export {};
3
4
  //# sourceMappingURL=apify-schema-tools.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"apify-schema-tools.d.ts","sourceRoot":"","sources":["../src/apify-schema-tools.ts"],"names":[],"mappings":""}
1
+ {"version":3,"file":"apify-schema-tools.d.ts","sourceRoot":"","sources":["../src/apify-schema-tools.ts"],"names":[],"mappings":";AAEA,oGAAoG"}