apify-schema-tools 2.0.1 → 2.0.2
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.
- package/CHANGELOG.md +10 -0
- package/dist/apify.d.ts.map +1 -1
- package/dist/apify.js +17 -4
- package/dist/apify.js.map +1 -1
- package/package.json +1 -1
- package/src/apify.ts +22 -4
- package/test/apify.test.ts +68 -8
package/CHANGELOG.md
CHANGED
package/dist/apify.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"apify.d.ts","sourceRoot":"","sources":["../src/apify.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,YAAY,EAA+B,MAAM,mBAAmB,CAAC;AAGnF,eAAO,MAAM,iBAAiB,sBAAsB,CAAC;AA+CrD,wBAAgB,gCAAgC,CAAC,MAAM,EAAE,YAAY,GAAG,YAAY,CAQnF;AAED;;;;GAIG;AACH,wBAAgB,kCAAkC,CAAC,WAAW,EAAE,YAAY,
|
|
1
|
+
{"version":3,"file":"apify.d.ts","sourceRoot":"","sources":["../src/apify.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,YAAY,EAA+B,MAAM,mBAAmB,CAAC;AAGnF,eAAO,MAAM,iBAAiB,sBAAsB,CAAC;AA+CrD,wBAAgB,gCAAgC,CAAC,MAAM,EAAE,YAAY,GAAG,YAAY,CAQnF;AAED;;;;GAIG;AACH,wBAAgB,kCAAkC,CAAC,WAAW,EAAE,YAAY,UA0E3E;AAED,eAAO,MAAM,oBAAoB,WAAW,CAAC"}
|
package/dist/apify.js
CHANGED
|
@@ -75,15 +75,24 @@ export function generateInputDefaultsTSFileContent(inputSchema) {
|
|
|
75
75
|
requiredParamsWithoutDefaults.push(key);
|
|
76
76
|
}
|
|
77
77
|
}
|
|
78
|
+
const areThereDefaults = Object.keys(optionalParamsWithDefaults.properties).length > 0;
|
|
78
79
|
return `\
|
|
79
80
|
import { Actor } from "apify";
|
|
80
81
|
import type { Input } from "./input.js";
|
|
81
82
|
|
|
82
|
-
|
|
83
|
+
${areThereDefaults
|
|
84
|
+
? `\
|
|
85
|
+
interface DefaultInputs ${serializeTypeScriptSchema(optionalParamsWithDefaults)};
|
|
83
86
|
|
|
84
|
-
export
|
|
87
|
+
export type InputWithDefaults = Input & DefaultInputs;
|
|
88
|
+
|
|
89
|
+
export const DEFAULT_INPUT_VALUES: DefaultInputs = ${JSON.stringify(defaultValues, null, "\t")};`
|
|
90
|
+
: `\
|
|
91
|
+
export type InputWithDefaults = Input;
|
|
85
92
|
|
|
86
|
-
export
|
|
93
|
+
export const DEFAULT_INPUT_VALUES: Record<string, unknown> = {};`}
|
|
94
|
+
|
|
95
|
+
export const REQUIRED_INPUT_FIELDS_WITHOUT_DEFAULT = ${JSON.stringify(requiredParamsWithoutDefaults, null, "\t")};
|
|
87
96
|
|
|
88
97
|
export function getInputWithDefaultValues(input?: Input | null): InputWithDefaults {
|
|
89
98
|
if (Actor.isAtHome()) {
|
|
@@ -96,10 +105,14 @@ ${requiredParamsWithoutDefaults.length > 0
|
|
|
96
105
|
throw new Error(\`Input is required, because the following fields are required: $\{REQUIRED_INPUT_FIELDS_WITHOUT_DEFAULT.join(", ")\}\`);
|
|
97
106
|
}`
|
|
98
107
|
: ""}
|
|
108
|
+
${areThereDefaults
|
|
109
|
+
? `\
|
|
99
110
|
return {
|
|
100
111
|
...DEFAULT_INPUT_VALUES,
|
|
101
112
|
...(input ?? {} as Input),
|
|
102
|
-
}
|
|
113
|
+
};`
|
|
114
|
+
: `\
|
|
115
|
+
return input ?? {} as Input;`}
|
|
103
116
|
}
|
|
104
117
|
`;
|
|
105
118
|
}
|
package/dist/apify.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"apify.js","sourceRoot":"","sources":["../src/apify.ts"],"names":[],"mappings":"AAAA,OAAO,EAAqB,2BAA2B,EAAE,MAAM,mBAAmB,CAAC;AACnF,OAAO,EAA4B,+BAA+B,EAAE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B,CAAC,MAAM,GAAG,CAAC;QACvC,CAAC,CAAC;;;GAGD;QACD,CAAC,CAAC,EACJ;EAEC,gBAAgB;QACf,CAAC,CAAC;;;;IAIA;QACF,CAAC,CAAC;8BAEJ;;CAEC,CAAC;AACF,CAAC;AAED,MAAM,CAAC,MAAM,oBAAoB,GAAG,QAAQ,CAAC"}
|
package/package.json
CHANGED
package/src/apify.ts
CHANGED
|
@@ -90,15 +90,27 @@ export function generateInputDefaultsTSFileContent(inputSchema: ObjectSchema) {
|
|
|
90
90
|
}
|
|
91
91
|
}
|
|
92
92
|
|
|
93
|
+
const areThereDefaults = Object.keys(optionalParamsWithDefaults.properties).length > 0;
|
|
94
|
+
|
|
93
95
|
return `\
|
|
94
96
|
import { Actor } from "apify";
|
|
95
97
|
import type { Input } from "./input.js";
|
|
96
98
|
|
|
97
|
-
|
|
99
|
+
${
|
|
100
|
+
areThereDefaults
|
|
101
|
+
? `\
|
|
102
|
+
interface DefaultInputs ${serializeTypeScriptSchema(optionalParamsWithDefaults)};
|
|
103
|
+
|
|
104
|
+
export type InputWithDefaults = Input & DefaultInputs;
|
|
98
105
|
|
|
99
|
-
export const
|
|
106
|
+
export const DEFAULT_INPUT_VALUES: DefaultInputs = ${JSON.stringify(defaultValues, null, "\t")};`
|
|
107
|
+
: `\
|
|
108
|
+
export type InputWithDefaults = Input;
|
|
109
|
+
|
|
110
|
+
export const DEFAULT_INPUT_VALUES: Record<string, unknown> = {};`
|
|
111
|
+
}
|
|
100
112
|
|
|
101
|
-
export
|
|
113
|
+
export const REQUIRED_INPUT_FIELDS_WITHOUT_DEFAULT = ${JSON.stringify(requiredParamsWithoutDefaults, null, "\t")};
|
|
102
114
|
|
|
103
115
|
export function getInputWithDefaultValues(input?: Input | null): InputWithDefaults {
|
|
104
116
|
if (Actor.isAtHome()) {
|
|
@@ -113,10 +125,16 @@ ${
|
|
|
113
125
|
}`
|
|
114
126
|
: ""
|
|
115
127
|
}
|
|
128
|
+
${
|
|
129
|
+
areThereDefaults
|
|
130
|
+
? `\
|
|
116
131
|
return {
|
|
117
132
|
...DEFAULT_INPUT_VALUES,
|
|
118
133
|
...(input ?? {} as Input),
|
|
119
|
-
}
|
|
134
|
+
};`
|
|
135
|
+
: `\
|
|
136
|
+
return input ?? {} as Input;`
|
|
137
|
+
}
|
|
120
138
|
}
|
|
121
139
|
`;
|
|
122
140
|
}
|
package/test/apify.test.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import { describe, expect, it } from "vitest";
|
|
2
|
+
|
|
3
|
+
import { generateInputDefaultsTSFileContent } from "../src/apify.js";
|
|
2
4
|
import type { ObjectSchema } from "../src/json-schemas";
|
|
3
5
|
|
|
4
6
|
describe("Apify utilities", () => {
|
|
5
7
|
describe("generateInputDefaultsTSFileContent", () => {
|
|
6
|
-
it("should generate
|
|
8
|
+
it("should generate the correct content with default values", async () => {
|
|
7
9
|
const inputSchema: ObjectSchema = {
|
|
8
10
|
type: "object",
|
|
9
11
|
properties: {
|
|
@@ -14,15 +16,73 @@ describe("Apify utilities", () => {
|
|
|
14
16
|
required: ["name"],
|
|
15
17
|
};
|
|
16
18
|
|
|
17
|
-
const tsContent =
|
|
18
|
-
|
|
19
|
+
const tsContent = generateInputDefaultsTSFileContent(inputSchema);
|
|
20
|
+
|
|
21
|
+
expect(tsContent).toEqual(
|
|
22
|
+
`import { Actor } from "apify";
|
|
23
|
+
import type { Input } from "./input.js";
|
|
24
|
+
|
|
25
|
+
interface DefaultInputs {
|
|
26
|
+
age: number;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export type InputWithDefaults = Input & DefaultInputs;
|
|
30
|
+
|
|
31
|
+
export const DEFAULT_INPUT_VALUES: DefaultInputs = {
|
|
32
|
+
"name": "John Doe",
|
|
33
|
+
"age": 30
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
export const REQUIRED_INPUT_FIELDS_WITHOUT_DEFAULT = [];
|
|
37
|
+
|
|
38
|
+
export function getInputWithDefaultValues(input?: Input | null): InputWithDefaults {
|
|
39
|
+
if (Actor.isAtHome()) {
|
|
40
|
+
// The platform is supposed to fill in the default values
|
|
41
|
+
return input as InputWithDefaults;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return {
|
|
45
|
+
...DEFAULT_INPUT_VALUES,
|
|
46
|
+
...(input ?? {} as Input),
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
`,
|
|
19
50
|
);
|
|
51
|
+
});
|
|
20
52
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
53
|
+
it("should generate the correct content without default values", async () => {
|
|
54
|
+
const inputSchema: ObjectSchema = {
|
|
55
|
+
type: "object",
|
|
56
|
+
properties: {
|
|
57
|
+
name: { type: "string", default: "John Doe" },
|
|
58
|
+
age: { type: "number" },
|
|
59
|
+
email: { type: "string" },
|
|
60
|
+
},
|
|
61
|
+
required: ["name"],
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
const tsContent = generateInputDefaultsTSFileContent(inputSchema);
|
|
65
|
+
|
|
66
|
+
expect(tsContent).toEqual(
|
|
67
|
+
`import { Actor } from "apify";
|
|
68
|
+
import type { Input } from "./input.js";
|
|
69
|
+
|
|
70
|
+
export type InputWithDefaults = Input;
|
|
71
|
+
|
|
72
|
+
export const DEFAULT_INPUT_VALUES: Record<string, unknown> = {};
|
|
73
|
+
|
|
74
|
+
export const REQUIRED_INPUT_FIELDS_WITHOUT_DEFAULT = [];
|
|
75
|
+
|
|
76
|
+
export function getInputWithDefaultValues(input?: Input | null): InputWithDefaults {
|
|
77
|
+
if (Actor.isAtHome()) {
|
|
78
|
+
// The platform is supposed to fill in the default values
|
|
79
|
+
return input as InputWithDefaults;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return input ?? {} as Input;
|
|
83
|
+
}
|
|
84
|
+
`,
|
|
85
|
+
);
|
|
26
86
|
});
|
|
27
87
|
});
|
|
28
88
|
});
|