Skip to content

Commit 33cff8c

Browse files
committed
fix: nested relations schemas
1 parent 0b01235 commit 33cff8c

File tree

3 files changed

+31
-21
lines changed

3 files changed

+31
-21
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@sensinum/astro-strapi-loader",
3-
"version": "1.0.5",
3+
"version": "1.0.6",
44
"description": "Astro loader for Strapi CMS",
55
"keywords": [
66
"astro",

src/utils/loader.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export function strapiLoader(
1717
load: async (context: LoaderContext): Promise<void> => {
1818
const { store, logger, parseData, generateDigest } = context;
1919

20-
logger.info(`[${contentType}] Loading data from Strapi`);
20+
logger.info(`[${contentType}] Loading data from Strapi...`);
2121
const { url, token } = options;
2222
const response = await fetchContent({
2323
url,
@@ -51,7 +51,7 @@ export function strapiLoader(
5151
const digest = generateDigest(data);
5252
store.set({ id: data.documentId as string, data, digest });
5353
}
54-
logger.info(`[${contentType}] Data loaded from Strapi`);
54+
logger.info(`[${contentType}] Loading data from Strapi... DONE`);
5555
},
5656
};
5757
}

src/utils/schema.ts

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export class StrapiSchemaGenerator {
1010
this.strict = strict;
1111
}
1212

13-
private generateAttributeSchema(attribute: StrapiAttribute): z.ZodType<unknown> {
13+
private generateAttributeSchema(attribute: StrapiAttribute, pupulatedRelations: Array<string> = []): z.ZodType<unknown> | null {
1414
const ref = this;
1515
switch (attribute.type) {
1616
case "string":
@@ -90,24 +90,30 @@ export class StrapiSchemaGenerator {
9090
if (!attribute.relation)
9191
throw new Error("Relation type requires relation target");
9292
const targetType = ref.contentTypes.find(
93-
(contentType) => contentType.apiID === attribute.relationTargetModel,
93+
(contentType) =>
94+
contentType.apiID === attribute.target ||
95+
contentType.uid === attribute.target,
9496
);
97+
9598
if (!targetType)
9699
throw new Error(`Target type ${attribute.relation} not found`);
97100

98-
const relationSchema = ref.generateContentTypeSchema(targetType.schema);
101+
if (attribute.targetAttribute && pupulatedRelations.includes(attribute.targetAttribute)) {
102+
return null;
103+
}
104+
105+
const relationSchema = ref.generateContentTypeSchema(
106+
targetType.schema,
107+
attribute.targetAttribute ? [...pupulatedRelations, attribute.targetAttribute] : pupulatedRelations,
108+
);
99109

100110
switch (attribute.relation) {
101111
case "oneToOne":
102112
case "manyToOne":
103-
return z.object({
104-
data: relationSchema.nullable(),
105-
});
113+
return relationSchema;
106114
case "oneToMany":
107115
case "manyToMany":
108-
return z.object({
109-
data: z.array(relationSchema),
110-
});
116+
return z.array(relationSchema);
111117
default:
112118
throw new Error(
113119
`Unsupported relation type: ${attribute.relationType}`,
@@ -131,23 +137,27 @@ export class StrapiSchemaGenerator {
131137

132138
private generateContentTypeSchema(
133139
contentTypeSchema: StrapiContentType["schema"],
140+
pupulatedRelations?: Array<string>
134141
): z.ZodObject<any> {
135142
const ref = this;
136143
const shape: Record<string, z.ZodTypeAny> = Object.entries(
137144
contentTypeSchema.attributes,
138145
).reduce(
139146
(acc, [key, attribute]) => {
140147
try {
141-
const schema = ref.generateAttributeSchema(attribute);
142-
return {
143-
...acc,
144-
[key]:
145-
ref.strict && attribute.required
146-
? schema
147-
: schema.nullable().optional(),
148-
};
148+
const schema = ref.generateAttributeSchema(attribute, pupulatedRelations);
149+
if (schema) {
150+
return {
151+
...acc,
152+
[key]:
153+
ref.strict && attribute.required
154+
? schema
155+
: schema.nullable().optional(),
156+
};
157+
}
158+
return acc;
149159
} catch (error) {
150-
console.error("Error generating attribute schema", error);
160+
console.warn("Error generating attribute schema", error);
151161
return acc;
152162
}
153163
},

0 commit comments

Comments
 (0)