Skip to content

Commit 7655450

Browse files
committed
improve parsing properties
1 parent 93e63a0 commit 7655450

File tree

1 file changed

+29
-4
lines changed

1 file changed

+29
-4
lines changed

components/notion/actions/update-page/update-page.mjs

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,19 +87,44 @@ export default {
8787
properties,
8888
};
8989
},
90+
parseProperties(properties) {
91+
if (!properties) {
92+
return undefined;
93+
}
94+
if (typeof properties === "string") {
95+
try {
96+
return JSON.parse(properties);
97+
} catch {
98+
throw new Error("Could not parse properties as JSON object");
99+
}
100+
}
101+
const parsedProperties = {};
102+
for (const [
103+
key,
104+
value,
105+
] of Object.entries(properties)) {
106+
try {
107+
parsedProperties[key] = typeof value === "string"
108+
? JSON.parse(value)
109+
: value;
110+
} catch {
111+
parsedProperties[key] = value;
112+
}
113+
}
114+
return parsedProperties;
115+
},
90116
},
91117
async run({ $ }) {
92118
try {
93-
if (this.properties && typeof this.properties === "string") {
94-
this.properties = JSON.parse(this.properties);
95-
}
119+
this.properties = this.parseProperties(this.properties);
120+
96121
const currentPage = await this.notion.retrievePage(this.pageId);
97122
const page = this.buildPage(currentPage);
98123
const response = await this.notion.updatePage(this.pageId, page);
99124
$.export("$summary", "Updated page successfully");
100125
return response;
101126
} catch (error) {
102-
throw new Error(error.body);
127+
throw new Error(error.body || error);
103128
}
104129
},
105130
};

0 commit comments

Comments
 (0)