apify-schema-tools 2.0.1

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 (83) hide show
  1. package/.cspell/custom-dictionary.txt +4 -0
  2. package/.husky/pre-commit +33 -0
  3. package/.node-version +1 -0
  4. package/CHANGELOG.md +88 -0
  5. package/LICENSE +201 -0
  6. package/README.md +312 -0
  7. package/biome.json +31 -0
  8. package/dist/apify-schema-tools.d.ts +3 -0
  9. package/dist/apify-schema-tools.d.ts.map +1 -0
  10. package/dist/apify-schema-tools.js +197 -0
  11. package/dist/apify-schema-tools.js.map +1 -0
  12. package/dist/apify.d.ts +11 -0
  13. package/dist/apify.d.ts.map +1 -0
  14. package/dist/apify.js +107 -0
  15. package/dist/apify.js.map +1 -0
  16. package/dist/configuration.d.ts +43 -0
  17. package/dist/configuration.d.ts.map +1 -0
  18. package/dist/configuration.js +87 -0
  19. package/dist/configuration.js.map +1 -0
  20. package/dist/filesystem.d.ts +8 -0
  21. package/dist/filesystem.d.ts.map +1 -0
  22. package/dist/filesystem.js +16 -0
  23. package/dist/filesystem.js.map +1 -0
  24. package/dist/json-schemas.d.ts +34 -0
  25. package/dist/json-schemas.d.ts.map +1 -0
  26. package/dist/json-schemas.js +185 -0
  27. package/dist/json-schemas.js.map +1 -0
  28. package/dist/typescript.d.ts +26 -0
  29. package/dist/typescript.d.ts.map +1 -0
  30. package/dist/typescript.js +316 -0
  31. package/dist/typescript.js.map +1 -0
  32. package/package.json +60 -0
  33. package/samples/all-defaults/.actor/actor.json +15 -0
  34. package/samples/all-defaults/.actor/dataset_schema.json +32 -0
  35. package/samples/all-defaults/.actor/input_schema.json +53 -0
  36. package/samples/all-defaults/src/generated/dataset.ts +24 -0
  37. package/samples/all-defaults/src/generated/input-utils.ts +60 -0
  38. package/samples/all-defaults/src/generated/input.ts +42 -0
  39. package/samples/all-defaults/src-schemas/dataset-item.json +28 -0
  40. package/samples/all-defaults/src-schemas/input.json +73 -0
  41. package/samples/deep-merged-schemas/.actor/actor.json +15 -0
  42. package/samples/deep-merged-schemas/.actor/dataset_schema.json +37 -0
  43. package/samples/deep-merged-schemas/.actor/input_schema.json +61 -0
  44. package/samples/deep-merged-schemas/add-schemas/dataset-item.json +10 -0
  45. package/samples/deep-merged-schemas/add-schemas/input.json +33 -0
  46. package/samples/deep-merged-schemas/src/generated/dataset.ts +28 -0
  47. package/samples/deep-merged-schemas/src/generated/input-utils.ts +66 -0
  48. package/samples/deep-merged-schemas/src/generated/input.ts +47 -0
  49. package/samples/deep-merged-schemas/src-schemas/dataset-item.json +28 -0
  50. package/samples/deep-merged-schemas/src-schemas/input.json +73 -0
  51. package/samples/merged-schemas/.actor/actor.json +15 -0
  52. package/samples/merged-schemas/.actor/dataset_schema.json +37 -0
  53. package/samples/merged-schemas/.actor/input_schema.json +58 -0
  54. package/samples/merged-schemas/add-schemas/dataset-item.json +10 -0
  55. package/samples/merged-schemas/add-schemas/input.json +33 -0
  56. package/samples/merged-schemas/src/generated/dataset.ts +28 -0
  57. package/samples/merged-schemas/src/generated/input-utils.ts +57 -0
  58. package/samples/merged-schemas/src/generated/input.ts +42 -0
  59. package/samples/merged-schemas/src-schemas/dataset-item.json +28 -0
  60. package/samples/merged-schemas/src-schemas/input.json +73 -0
  61. package/samples/package-json-config/.actor/actor.json +15 -0
  62. package/samples/package-json-config/.actor/dataset_schema.json +32 -0
  63. package/samples/package-json-config/.actor/input_schema.json +53 -0
  64. package/samples/package-json-config/custom-src-schemas/dataset-item.json +28 -0
  65. package/samples/package-json-config/custom-src-schemas/input.json +73 -0
  66. package/samples/package-json-config/package.json +11 -0
  67. package/samples/package-json-config/src/custom-generated/dataset.ts +24 -0
  68. package/samples/package-json-config/src/custom-generated/input-utils.ts +60 -0
  69. package/samples/package-json-config/src/custom-generated/input.ts +42 -0
  70. package/src/apify-schema-tools.ts +302 -0
  71. package/src/apify.ts +124 -0
  72. package/src/configuration.ts +110 -0
  73. package/src/filesystem.ts +18 -0
  74. package/src/json-schemas.ts +252 -0
  75. package/src/typescript.ts +381 -0
  76. package/test/apify-schema-tools.test.ts +2064 -0
  77. package/test/apify.test.ts +28 -0
  78. package/test/common.ts +19 -0
  79. package/test/configuration.test.ts +642 -0
  80. package/test/json-schemas.test.ts +587 -0
  81. package/test/typescript.test.ts +817 -0
  82. package/tsconfig.json +18 -0
  83. package/update-samples.sh +27 -0
@@ -0,0 +1,4 @@
1
+ # Custom Dictionary Words
2
+ apify
3
+ nargs
4
+ subparsers
@@ -0,0 +1,33 @@
1
+ #!/usr/bin/env bash
2
+
3
+ set -e
4
+
5
+ echo "Checking linting and formatting..."
6
+
7
+ npm run check
8
+
9
+ # Check some samples' consistency
10
+
11
+ echo "Checking samples..."
12
+
13
+ EXEC_PATH="$(pwd)/dist/apify-schema-tools.js"
14
+
15
+ cd samples/all-defaults
16
+ node $EXEC_PATH check
17
+ cd ../..
18
+
19
+ cd samples/package-json-config
20
+ node $EXEC_PATH check
21
+ cd ../..
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
+ echo "Running tests..."
32
+
33
+ npm test
package/.node-version ADDED
@@ -0,0 +1 @@
1
+ v23
package/CHANGELOG.md ADDED
@@ -0,0 +1,88 @@
1
+ # Changelog
2
+
3
+ ## 2.0.1
4
+
5
+ ### Added
6
+
7
+ - Repository information in package.json.
8
+
9
+ ### Fixed
10
+
11
+ - README code blocks.
12
+
13
+ ## 2.0.0
14
+
15
+ ### BREAKING CHANGES
16
+
17
+ - The script `apify-generate` has been removed.
18
+ - The script `generate-apify-schema` has been removed.
19
+ - The script `generate-apify-type` has been removed.
20
+
21
+ ### Added
22
+
23
+ - The script `apify-schema-tools` has been added.
24
+ - The subcommand `init` allows to setup `apify-schema-tools` in a project.
25
+ - The subcommand `sync` perform the synchronization of the source schemas, like the former command `apify-generate`
26
+ - The subcommand `check` checks for consistency between the source and the generated files.
27
+ - Allow checking only the values, ignoring descriptions.
28
+ - Options can be set in `package.json`, under the property `apify-schema-tools`. CLI parameters will override the options.
29
+ - Support enums in JSON schemas.
30
+ - Support nullable properties in JSON schemas.
31
+ - Change `integer` into `number` when creating TypeScript types.
32
+
33
+ ### Technical changes
34
+
35
+ - Implemented a custom TypeScript serializer and parser, to better support checking TypeScript interfaces.
36
+
37
+ ## 1.3.0
38
+
39
+ ### Added
40
+
41
+ - The option `--deep-merge` allows to keep object and array definitions from both the base and the additional schemas.
42
+
43
+ ### Fixed
44
+
45
+ - The generated `input-utils.ts` file now avoids TypeScript errors in some occasions.
46
+
47
+ ## 1.2.0
48
+
49
+ ### Added
50
+
51
+ - The `--add-input` and `--add-dataset` options allow to merge a second input or dataset schema into the source one.
52
+
53
+ ## 1.1.1
54
+
55
+ ### Fixed
56
+
57
+ - The generated `getInputWithDefaultValues` also assumes that the input can be null.
58
+
59
+ ## 1.1.0
60
+
61
+ ### Added
62
+
63
+ - `DEFAULT_INPUT_VALUES` and `REQUIRED_INPUT_FIELDS_WITHOUT_DEFAULT` are exported by `input-utils.ts`.
64
+
65
+ ### Fixed
66
+
67
+ - The generated `input-utils.ts` is more friendly with formatters.
68
+
69
+ ### Changed
70
+
71
+ - The example input in the README's quickstart was simplified.
72
+
73
+ ## 1.0.0
74
+
75
+ ### Added
76
+
77
+ - Script `apify-generate` to generate both JSON schema and TypeScript files.
78
+ - Unit and integration tests.
79
+ - Pre-commit hook that run the tests.
80
+ - Some samples to test the script.
81
+
82
+ ### Changed
83
+
84
+ - The README now documents only the new command `apify-generate`.
85
+
86
+ ### Deprecated
87
+
88
+ - Scripts `generate-apify-schemas` and `generate-apify-types`.
package/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
package/README.md ADDED
@@ -0,0 +1,312 @@
1
+ # Apify Schema Tools
2
+
3
+ This is a tool intended for Apify actors developers.
4
+
5
+ It allows generating JSON schemas and TypeScript types, for input and dataset,
6
+ from a single source of truth, with a few extra features.
7
+
8
+ As a quick example, assume you have a project that looks like this:
9
+
10
+ ```
11
+ my-project
12
+ ├── .actor
13
+ │ ├── actor.json
14
+ │ ├── dataset_schema.json
15
+ │ └── input_schema.json
16
+ └── src-schemas
17
+ ├── dataset-item.json <-- source file for dataset
18
+ └── input.json <-- source file for input
19
+ ```
20
+
21
+ After running this script, you will have:
22
+
23
+ ```
24
+ my-project
25
+ ├── .actor
26
+ │ ├── actor.json
27
+ │ ├── dataset_schema.json <-- updated with the definitions from src-schemas
28
+ │ └── input_schema.json <-- updated with the definitions from src-schemas
29
+ ├── src
30
+ │ └── generated
31
+ │ ├── dataset.ts <-- TypeScript types generated from src-schemas
32
+ │ ├── input-utils.ts <-- utilities to fill input default values
33
+ │ └── input.ts <-- TypeScript types generated from src-schemas
34
+ └── src-schemas
35
+ ├── dataset-item.json
36
+ └── input.json
37
+ ```
38
+
39
+ ## Quickstart
40
+
41
+ These instructions will allow you to quickly get to a point where you can use
42
+ the `apify-schema-tools` to generate your schemas and TypeScript types.
43
+
44
+ Let's assume you are starting from a new project created from an
45
+ [Apify template](https://github.com/apify/actor-templates).
46
+
47
+ 1. Install `apify-schema-tools`:
48
+
49
+ ```sh
50
+ npm i -D apify-schema-tools
51
+ ```
52
+
53
+ Now the command `apify-schema-tools` is installed for the current project.
54
+ You can check which options are available:
55
+
56
+ ```console
57
+ $ npx apify-schema-tools --help
58
+ usage: apify-schema-tools [-h] {init,sync,check} ...
59
+
60
+ Apify Schema Tools - Generate JSON schemas and TypeScript files for Actor input and output dataset.
61
+
62
+ positional arguments:
63
+ {init,sync,check}
64
+ init Initialize the Apify Schema Tools project with default settings.
65
+ sync Generate JSON schemas and TypeScript files from the source schemas.
66
+ check Check the schemas for consistency and correctness.
67
+
68
+ optional arguments:
69
+ -h, --help show this help message and exit
70
+ ```
71
+
72
+ You can also check the options available for a specific command:
73
+
74
+ ```console
75
+ $ npx apify-schema-tools sync --help
76
+ 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]
77
+ [--deep-merge] [--include-input-utils {true,false}]
78
+
79
+ optional arguments:
80
+ -h, --help show this help message and exit
81
+ -i [{input,dataset} ...], --input [{input,dataset} ...]
82
+ specify which sources to use for generation (default: input,dataset)
83
+ -o [{json-schemas,ts-types} ...], --output [{json-schemas,ts-types} ...]
84
+ specify what to generate (default: json-schemas,ts-types)
85
+ --src-input SRC_INPUT
86
+ path to the input schema source file (default: src-schemas/input.json)
87
+ --src-dataset SRC_DATASET
88
+ path to the dataset schema source file (default: src-schemas/dataset-item.json)
89
+ --add-input ADD_INPUT
90
+ path to an additional schema to merge into the input schema (default: undefined)
91
+ --add-dataset ADD_DATASET
92
+ path to an additional schema to merge into the dataset schema (default: undefined)
93
+ --input-schema INPUT_SCHEMA
94
+ the path of the destination input schema file (default: .actor/input_schema.json)
95
+ --dataset-schema DATASET_SCHEMA
96
+ the path of the destination dataset schema file (default: .actor/dataset_schema.json)
97
+ --output-ts-dir OUTPUT_TS_DIR
98
+ path where to save generated TypeScript files (default: src/generated)
99
+ --deep-merge whether to deep merge additional schemas into the main schema (default: false)
100
+ --include-input-utils {true,false}
101
+ include input utilities in the generated TypeScript files: 'input' input and 'ts-types' output are required (default: true)
102
+ ```
103
+
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.
106
+
107
+ 2. Create a `src-schemas` folder:
108
+
109
+ ```sh
110
+ mkdir src-schemas
111
+ ```
112
+
113
+ 3. Create the files `input.json` and `dataset-item.json` inside the `src-schemas`. Here is some example content:
114
+
115
+ ```json
116
+ {
117
+ "title": "Input schema for Web Scraper",
118
+ "type": "object",
119
+ "schemaVersion": 1,
120
+ "properties": {
121
+ "startUrls": {
122
+ "type": "array",
123
+ "title": "Start URLs",
124
+ "description": "List of URLs to scrape",
125
+ "default": [],
126
+ "editor": "requestListSources",
127
+ "items": {
128
+ "type": "object",
129
+ "properties": {
130
+ "url": { "type": "string" }
131
+ }
132
+ }
133
+ }
134
+ },
135
+ "required": ["startUrls"]
136
+ }
137
+ ```
138
+
139
+ ```json
140
+ {
141
+ "$schema": "http://json-schema.org/draft-07/schema#",
142
+ "title": "Dataset schema for Web Scraper",
143
+ "type": "object",
144
+ "properties": {
145
+ "title": {
146
+ "type": "string",
147
+ "title": "Title",
148
+ "description": "Page title"
149
+ },
150
+ "url": {
151
+ "type": "string",
152
+ "title": "URL",
153
+ "description": "Page URL"
154
+ },
155
+ "text": {
156
+ "type": "string",
157
+ "title": "Text content",
158
+ "description": "Extracted text"
159
+ },
160
+ "timestamp": {
161
+ "type": "string",
162
+ "title": "Timestamp",
163
+ "description": "When the data was scraped"
164
+ }
165
+ },
166
+ "required": ["title", "url"]
167
+ }
168
+ ```
169
+
170
+ 4. Create the file `.actor/dataset_schema.json` and enter some empty content:
171
+
172
+ ```json
173
+ {
174
+ "actorSpecification": 1,
175
+ "fields": {},
176
+ "views": {}
177
+ }
178
+ ```
179
+
180
+ 5. Link the dataset schema in `.actor/actor.json`:
181
+
182
+ ```json
183
+ {
184
+ "actorSpecification": 1,
185
+ "...": "...",
186
+ "input": "./input_schema.json",
187
+ "storages": {
188
+ "dataset": "./dataset_schema.json"
189
+ },
190
+ "...": "..."
191
+ }
192
+ ```
193
+
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
+
206
+ 7. Generate JSON schemas and TypeScript types from the source schemas:
207
+
208
+ ```sh
209
+ npm run generate
210
+ ```
211
+
212
+ 8. Now, you will be able to use TypeScript types and utilities in your project:
213
+
214
+ ```ts
215
+ import { Actor } from 'apify';
216
+
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';
220
+
221
+ await Actor.init();
222
+
223
+ const input: InputWithDefaults = getInputWithDefaultValues(await Actor.getInput<Input>());
224
+
225
+ '...'
226
+
227
+ await Actor.pushData<DatasetItem>({
228
+ tile: '...',
229
+ url: '...',
230
+ text: '...',
231
+ timestamp: '...',
232
+ });
233
+
234
+ await Actor.exit();
235
+ ```
236
+
237
+ ## Extra features
238
+
239
+ ### Keep only allowed properties in Input schema
240
+
241
+ As an example, when `type` is "array", the property `items` is forbidden if `editor` is different from "select".
242
+
243
+ ### Merge a second schema into the main one
244
+
245
+ This feature is useful when working in monorepos.
246
+ It allows you to define a single common schema across all the actors in the repo,
247
+ and to add or override the tile, the description, and some properties, when necessary.
248
+
249
+ To use it, use the parameters `--add-input` and `--add-dataset`, e.g.:
250
+
251
+ ```sh
252
+ npx apify-schema-tools sync \
253
+ --input input,dataset \
254
+ --output json-schemas,ts-types \
255
+ --src-input ../src-schemas/input.json \
256
+ --src-dataset ../src-schemas/dataset-item.json \
257
+ --add-input src-schemas/input.json \
258
+ --add-dataset src-schemas/dataset-item.json
259
+ ```
260
+
261
+ You can also define the order of the properties in the merged schema.
262
+ To do so, add a `position` field to the properties. The script will follow these rules:
263
+
264
+ - Properties without position or with the same position, are sorted in the same order in which they appear in the source
265
+ schemas, with the ones in the additional schema after the ones in the base schema.
266
+ - If both properties with and without position exist, the ones without position will appear at the end.
267
+ - The position will be overwritten if a property is overwritten.
268
+
269
+ An example:
270
+
271
+ ```jsonc
272
+ # Source input schema
273
+ {
274
+ "title": "My input schema",
275
+ "description": "My input properties",
276
+ "properties": {
277
+ "a": { "type": "string", "position": 3 },
278
+ "b": { "type": "string" }, // will be last, because it has no position
279
+ "c": { "type": "string", "position": 1 }
280
+ },
281
+ "required": ["a"]
282
+ }
283
+ ```
284
+
285
+ ```jsonc
286
+ # Additional input schema
287
+ {
288
+ "description": "My input properties, a bit changed", // will override the description
289
+ "properties": {
290
+ "c": { "type": "boolean", "position": 5 }, // will override also the position
291
+ "d": { "type": "string", "position": 1 } // will be first
292
+ },
293
+ "required": ["c", "d"] // will be merged to the source required parameters
294
+ }
295
+ ```
296
+
297
+ ```jsonc
298
+ # Final input schema
299
+ {
300
+ "title": "My input schema",
301
+ "description": "My input properties, a bit changed",
302
+ "properties": {
303
+ "d": { "type": "string" },
304
+ "a": { "type": "string" },
305
+ "c": { "type": "boolean" },
306
+ "b": { "type": "string" }
307
+ },
308
+ "required": ["a", "c", "d"]
309
+ }
310
+ ```
311
+
312
+ Use the option `--deep-merge` to merge object properties and array items, instead of overwriting every definition.
package/biome.json ADDED
@@ -0,0 +1,31 @@
1
+ {
2
+ "$schema": "https://biomejs.dev/schemas/1.9.4/schema.json",
3
+ "vcs": {
4
+ "enabled": true,
5
+ "clientKind": "git",
6
+ "useIgnoreFile": true
7
+ },
8
+ "files": {
9
+ "ignoreUnknown": false,
10
+ "ignore": []
11
+ },
12
+ "formatter": {
13
+ "enabled": true,
14
+ "indentStyle": "tab",
15
+ "lineWidth": 120
16
+ },
17
+ "organizeImports": {
18
+ "enabled": true
19
+ },
20
+ "linter": {
21
+ "enabled": true,
22
+ "rules": {
23
+ "recommended": true
24
+ }
25
+ },
26
+ "javascript": {
27
+ "formatter": {
28
+ "quoteStyle": "double"
29
+ }
30
+ }
31
+ }
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env node
2
+ export {};
3
+ //# sourceMappingURL=apify-schema-tools.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"apify-schema-tools.d.ts","sourceRoot":"","sources":["../src/apify-schema-tools.ts"],"names":[],"mappings":""}