svelte-reflector 2.1.6 → 2.1.8
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/dist/core/openapi/InlineSchemaPromoter.js +16 -1
- package/dist/core/schema/ArraySchemaRenderer.d.ts +18 -0
- package/dist/core/schema/ArraySchemaRenderer.js +39 -0
- package/dist/core/schema/Schema.d.ts +21 -0
- package/dist/core/schema/Schema.js +59 -0
- package/dist/core/schema/SchemaRegistry.js +7 -1
- package/dist/props/primitive.property.d.ts +1 -0
- package/dist/props/primitive.property.js +4 -1
- package/dist/runtime/reflector.svelte.ts +8 -1
- package/package.json +2 -2
|
@@ -49,7 +49,22 @@ export class InlineSchemaPromoter {
|
|
|
49
49
|
while (queue.length > 0) {
|
|
50
50
|
const schemaName = queue.shift();
|
|
51
51
|
const schema = schemas[schemaName];
|
|
52
|
-
if (!schema || "$ref" in schema
|
|
52
|
+
if (!schema || "$ref" in schema)
|
|
53
|
+
continue;
|
|
54
|
+
// Array-root schema (e.g. a promoted `data: array` response): promote its
|
|
55
|
+
// inline-object items to a named `${schemaName}Item` so they get a typed
|
|
56
|
+
// child schema instead of staying inline and degrading to `string[]`.
|
|
57
|
+
if (!schema.properties && schema.type === "array") {
|
|
58
|
+
const inlineItems = InlineSchemaPromoter.extractableInlineArrayItems(schema);
|
|
59
|
+
if (inlineItems) {
|
|
60
|
+
const name = InlineSchemaPromoter.reserveNestedName(usedNames, schemaName, "item");
|
|
61
|
+
schemas[name] = inlineItems;
|
|
62
|
+
queue.push(name);
|
|
63
|
+
schema.items = { $ref: `#/components/schemas/${name}` };
|
|
64
|
+
}
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
if (!schema.properties)
|
|
53
68
|
continue;
|
|
54
69
|
for (const [propKey, propValue] of Object.entries(schema.properties)) {
|
|
55
70
|
if (isReferenceObject(propValue))
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Renders the wrapper class + interface alias for an array-root schema — a
|
|
3
|
+
* component whose top level is `type: array` (e.g. a promoted `data: array`
|
|
4
|
+
* response envelope). Unlike object schemas (handled by `SchemaClassRenderer`),
|
|
5
|
+
* the interface is a bare array alias (`type XInterface = ItemInterface[]`)
|
|
6
|
+
* because the response envelope's `data` is already unwrapped: the consumer
|
|
7
|
+
* calls `new X({ data: response })` with `response` being the array itself.
|
|
8
|
+
*/
|
|
9
|
+
export declare class ArraySchemaRenderer {
|
|
10
|
+
static render(params: {
|
|
11
|
+
name: string;
|
|
12
|
+
elementType: string;
|
|
13
|
+
isRef: boolean;
|
|
14
|
+
}): {
|
|
15
|
+
interface: string;
|
|
16
|
+
schema: string;
|
|
17
|
+
};
|
|
18
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Renders the wrapper class + interface alias for an array-root schema — a
|
|
3
|
+
* component whose top level is `type: array` (e.g. a promoted `data: array`
|
|
4
|
+
* response envelope). Unlike object schemas (handled by `SchemaClassRenderer`),
|
|
5
|
+
* the interface is a bare array alias (`type XInterface = ItemInterface[]`)
|
|
6
|
+
* because the response envelope's `data` is already unwrapped: the consumer
|
|
7
|
+
* calls `new X({ data: response })` with `response` being the array itself.
|
|
8
|
+
*/
|
|
9
|
+
export class ArraySchemaRenderer {
|
|
10
|
+
static render(params) {
|
|
11
|
+
const { name, elementType, isRef } = params;
|
|
12
|
+
const interfaceElement = isRef ? `${elementType}Interface` : elementType;
|
|
13
|
+
const interfaceAlias = `export type ${name}Interface = ${interfaceElement}[]`;
|
|
14
|
+
const constructorBody = isRef
|
|
15
|
+
? `this.data = params?.data?.map((item) => new ${elementType}({ data: item })) ?? []`
|
|
16
|
+
: `this.data = params?.data ?? []`;
|
|
17
|
+
const staticBody = isRef
|
|
18
|
+
? `return data.map((item) => new ${elementType}({ data: item }))`
|
|
19
|
+
: `return data`;
|
|
20
|
+
const bundleBody = isRef ? `return this.data.map((item) => item.bundle())` : `return this.data`;
|
|
21
|
+
const schema = `
|
|
22
|
+
export class ${name} {
|
|
23
|
+
data = $state<${elementType}[]>([]);
|
|
24
|
+
|
|
25
|
+
constructor(params?: { data?: ${name}Interface | undefined, empty?: boolean }) {
|
|
26
|
+
${constructorBody}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
static from(data: ${name}Interface) {
|
|
30
|
+
${staticBody}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
bundle(): ${name}Interface {
|
|
34
|
+
${bundleBody}
|
|
35
|
+
}
|
|
36
|
+
};`;
|
|
37
|
+
return { interface: interfaceAlias, schema };
|
|
38
|
+
}
|
|
39
|
+
}
|
|
@@ -19,6 +19,27 @@ export declare class Schema {
|
|
|
19
19
|
readonly customTypeDeps: string[];
|
|
20
20
|
schema: string;
|
|
21
21
|
interface: string;
|
|
22
|
+
/**
|
|
23
|
+
* Builds a Schema for an array-root component (top-level `type: array`), e.g.
|
|
24
|
+
* a promoted `data: array` response envelope. Renders a wrapper class whose
|
|
25
|
+
* `data` is `Item[]` (hydrated `new Item({ data })` when items are a `$ref`)
|
|
26
|
+
* and whose interface is a bare array alias. Object-root schemas use the
|
|
27
|
+
* regular constructor instead.
|
|
28
|
+
*/
|
|
29
|
+
static forArrayRoot(params: {
|
|
30
|
+
name: string;
|
|
31
|
+
items: SchemaObject | ReferenceObject;
|
|
32
|
+
context: CodegenContext;
|
|
33
|
+
}): Schema;
|
|
34
|
+
/**
|
|
35
|
+
* Resolves the element type of an array-root schema. Inline-object items are
|
|
36
|
+
* already promoted to a `$ref` by `InlineSchemaPromoter`, so by here items is
|
|
37
|
+
* a `$ref`, an enum, a free-form object (→ `Record<string, unknown>`), or a
|
|
38
|
+
* primitive. `ArrayProp` is reused for ref/enum/primitive (it also registers
|
|
39
|
+
* the enum type in the context); the free-form object is special-cased
|
|
40
|
+
* because `ArrayProp.getType` would degrade it to `string`.
|
|
41
|
+
*/
|
|
42
|
+
private static resolveArrayElement;
|
|
22
43
|
constructor(params: {
|
|
23
44
|
properties: Record<string, SchemaObject | ReferenceObject>;
|
|
24
45
|
name: string;
|
|
@@ -2,9 +2,11 @@ import { ArrayProp } from "../../props/array.property.js";
|
|
|
2
2
|
import { EnumProp } from "../../props/enum.property.js";
|
|
3
3
|
import { ObjectProp } from "../../props/object.property.js";
|
|
4
4
|
import { PrimitiveProp } from "../../props/primitive.property.js";
|
|
5
|
+
import { isReferenceObject } from "../../helpers/helpers.js";
|
|
5
6
|
import { SchemaPropertyClassifier } from "./SchemaPropertyClassifier.js";
|
|
6
7
|
import { SchemaDependencyCollector } from "./SchemaDependencyCollector.js";
|
|
7
8
|
import { SchemaClassRenderer } from "./SchemaClassRenderer.js";
|
|
9
|
+
import { ArraySchemaRenderer } from "./ArraySchemaRenderer.js";
|
|
8
10
|
export class Schema {
|
|
9
11
|
name;
|
|
10
12
|
primitiveProps = [];
|
|
@@ -19,6 +21,63 @@ export class Schema {
|
|
|
19
21
|
customTypeDeps;
|
|
20
22
|
schema;
|
|
21
23
|
interface;
|
|
24
|
+
/**
|
|
25
|
+
* Builds a Schema for an array-root component (top-level `type: array`), e.g.
|
|
26
|
+
* a promoted `data: array` response envelope. Renders a wrapper class whose
|
|
27
|
+
* `data` is `Item[]` (hydrated `new Item({ data })` when items are a `$ref`)
|
|
28
|
+
* and whose interface is a bare array alias. Object-root schemas use the
|
|
29
|
+
* regular constructor instead.
|
|
30
|
+
*/
|
|
31
|
+
static forArrayRoot(params) {
|
|
32
|
+
const { name, items, context } = params;
|
|
33
|
+
const element = Schema.resolveArrayElement(items, name, context);
|
|
34
|
+
const schema = Object.create(Schema.prototype);
|
|
35
|
+
schema.name = name;
|
|
36
|
+
schema.primitiveProps = [];
|
|
37
|
+
schema.arrayProps = [];
|
|
38
|
+
schema.objectProps = [];
|
|
39
|
+
schema.enumProps = [];
|
|
40
|
+
schema.schemaDeps = element.kind === "ref" ? [element.type] : [];
|
|
41
|
+
schema.enumDeps = element.kind === "enum" ? [element.type] : [];
|
|
42
|
+
schema.customTypeDeps = [];
|
|
43
|
+
const rendered = ArraySchemaRenderer.render({
|
|
44
|
+
name,
|
|
45
|
+
elementType: element.type,
|
|
46
|
+
isRef: element.kind === "ref",
|
|
47
|
+
});
|
|
48
|
+
schema.interface = rendered.interface;
|
|
49
|
+
schema.schema = rendered.schema;
|
|
50
|
+
return schema;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Resolves the element type of an array-root schema. Inline-object items are
|
|
54
|
+
* already promoted to a `$ref` by `InlineSchemaPromoter`, so by here items is
|
|
55
|
+
* a `$ref`, an enum, a free-form object (→ `Record<string, unknown>`), or a
|
|
56
|
+
* primitive. `ArrayProp` is reused for ref/enum/primitive (it also registers
|
|
57
|
+
* the enum type in the context); the free-form object is special-cased
|
|
58
|
+
* because `ArrayProp.getType` would degrade it to `string`.
|
|
59
|
+
*/
|
|
60
|
+
static resolveArrayElement(items, name, context) {
|
|
61
|
+
if (isReferenceObject(items)) {
|
|
62
|
+
return { kind: "ref", type: items.$ref.split("/").at(-1) };
|
|
63
|
+
}
|
|
64
|
+
if (!items.enum && items.type === "object") {
|
|
65
|
+
return { kind: "raw", type: "Record<string, unknown>" };
|
|
66
|
+
}
|
|
67
|
+
const prop = new ArrayProp({
|
|
68
|
+
name: "data",
|
|
69
|
+
schemaObject: { type: "array", items },
|
|
70
|
+
schemaName: name,
|
|
71
|
+
required: true,
|
|
72
|
+
isParam: undefined,
|
|
73
|
+
isEnum: !!items.enum,
|
|
74
|
+
isNullable: false,
|
|
75
|
+
context,
|
|
76
|
+
});
|
|
77
|
+
if (items.enum)
|
|
78
|
+
return { kind: "enum", type: prop.type };
|
|
79
|
+
return { kind: "raw", type: prop.type };
|
|
80
|
+
}
|
|
22
81
|
constructor(params) {
|
|
23
82
|
const { name, isEmpty, properties, requireds, fieldConfigs, context } = params;
|
|
24
83
|
this.name = `${isEmpty ? "Empty" : ""}${name}`;
|
|
@@ -10,7 +10,13 @@ export class SchemaRegistry {
|
|
|
10
10
|
if (!componentSchemas)
|
|
11
11
|
return;
|
|
12
12
|
for (const [key, object] of Object.entries(componentSchemas)) {
|
|
13
|
-
if (isReferenceObject(object)
|
|
13
|
+
if (isReferenceObject(object))
|
|
14
|
+
continue;
|
|
15
|
+
if (object.type === "array" && object.items) {
|
|
16
|
+
this.schemas.push(Schema.forArrayRoot({ name: key, items: object.items, context }));
|
|
17
|
+
continue;
|
|
18
|
+
}
|
|
19
|
+
if (!object.properties)
|
|
14
20
|
continue;
|
|
15
21
|
const properties = object.properties;
|
|
16
22
|
const schema = {
|
|
@@ -14,6 +14,7 @@ export declare class PrimitiveProp {
|
|
|
14
14
|
private readonly example;
|
|
15
15
|
private readonly fallbackExample;
|
|
16
16
|
private readonly defaultValue;
|
|
17
|
+
private readonly max;
|
|
17
18
|
private get effectiveType();
|
|
18
19
|
/** Non-required fields become nullable (| null) instead of optional (?) */
|
|
19
20
|
private get isEffectivelyNullable();
|
|
@@ -13,6 +13,7 @@ export class PrimitiveProp {
|
|
|
13
13
|
example;
|
|
14
14
|
fallbackExample;
|
|
15
15
|
defaultValue;
|
|
16
|
+
max;
|
|
16
17
|
get effectiveType() {
|
|
17
18
|
return this.customType ?? this.rawType;
|
|
18
19
|
}
|
|
@@ -30,6 +31,7 @@ export class PrimitiveProp {
|
|
|
30
31
|
this.example = example;
|
|
31
32
|
this.fallbackExample = emptyExample;
|
|
32
33
|
this.defaultValue = schemaObject.default;
|
|
34
|
+
this.max = schemaObject.maximum;
|
|
33
35
|
const buildedType = customType ?? type;
|
|
34
36
|
const treated = treatPropertyName(name);
|
|
35
37
|
this.name = treated.name;
|
|
@@ -113,8 +115,9 @@ export class PrimitiveProp {
|
|
|
113
115
|
typeParam = `<${this.effectiveType}>`;
|
|
114
116
|
}
|
|
115
117
|
const nullableParam = this.isNullable ? "nullable: true, " : "";
|
|
118
|
+
const maxParam = this.max !== undefined ? `max: ${this.max}, ` : "";
|
|
116
119
|
return `
|
|
117
|
-
build${typeParam}({ key: ${keyExpr}, placeholder: ${this.example}, example: ${buildedExample}, required: ${required}, ${nullableParam}${buildedValidator()}})
|
|
120
|
+
build${typeParam}({ key: ${keyExpr}, placeholder: ${this.example}, example: ${buildedExample}, required: ${required}, ${nullableParam}${maxParam}${buildedValidator()}})
|
|
118
121
|
`;
|
|
119
122
|
}
|
|
120
123
|
thisDot() {
|
|
@@ -46,6 +46,7 @@ export class BuildedInput<T> {
|
|
|
46
46
|
required: boolean;
|
|
47
47
|
nullable: boolean;
|
|
48
48
|
placeholder: T;
|
|
49
|
+
max?: number;
|
|
49
50
|
readonly kind = "builded";
|
|
50
51
|
readonly validator?: ValidatorFn<T>;
|
|
51
52
|
|
|
@@ -55,9 +56,10 @@ export class BuildedInput<T> {
|
|
|
55
56
|
required: boolean;
|
|
56
57
|
nullable?: boolean;
|
|
57
58
|
placeholder: T;
|
|
59
|
+
max?: number;
|
|
58
60
|
validator?: ValidatorFn<T>;
|
|
59
61
|
}) {
|
|
60
|
-
const { example, required, nullable, key, validator, placeholder } = params;
|
|
62
|
+
const { example, required, nullable, key, validator, placeholder, max } = params;
|
|
61
63
|
|
|
62
64
|
const initial = key === undefined ? example : key;
|
|
63
65
|
|
|
@@ -67,6 +69,10 @@ export class BuildedInput<T> {
|
|
|
67
69
|
this.nullable = nullable ?? false;
|
|
68
70
|
this.placeholder = placeholder;
|
|
69
71
|
|
|
72
|
+
if (max !== undefined) {
|
|
73
|
+
this.max = max;
|
|
74
|
+
}
|
|
75
|
+
|
|
70
76
|
if (validator) {
|
|
71
77
|
this.validator = validator;
|
|
72
78
|
}
|
|
@@ -116,6 +122,7 @@ export function build<T>(params: {
|
|
|
116
122
|
placeholder: T;
|
|
117
123
|
required: boolean;
|
|
118
124
|
nullable?: boolean;
|
|
125
|
+
max?: number;
|
|
119
126
|
validator?: ValidatorFn<T>;
|
|
120
127
|
}): BuildedInput<T> {
|
|
121
128
|
return new BuildedInput(params);
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "svelte-reflector",
|
|
3
|
-
"version": "2.1.
|
|
4
|
-
"description": "Reflects
|
|
3
|
+
"version": "2.1.8",
|
|
4
|
+
"description": "Reflects types from openAPI schemas",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
7
7
|
"types": "./dist/index.d.ts",
|