如何解决理智有可能吗?计划的对Sanity的外部API调用
这是我想完成的事情?
1-每晚每晚对新闻API进行提取调用 2-拉最新的头条新闻 3-导入理智...
理智让您做到这一点吗?它有cron工作吗?
谢谢
解决方法
Sanity不提供任何本机方式来执行此操作,但是您可以使用外部无服务器提供程序来创建cron作业,该提供程序将提供您要查找的功能
Firebase scheduled functions是提供者的一个示例,但是还有更多选择。我建议您看看Serverless framework examples。
弄清楚将要使用的内容后,然后使用Sanity's HTTP API或one of their API clients(对于Javascript为@sanity/client
)向其中添加数据。您需要一个令牌来完成此操作,因为POST请求受身份验证墙保护(请查看其Authentication docs)。
以下是Javascript中的示例:
// For Javascript - using the @sanity/client npm package
const allHeadlines = await pullHeadlines(/* ... */);
// Create a single transaction that will handle
// the creation of all headline documents
const transaction = sanityClient.transaction();
for (const headline of allHeadlines) {
transaction = transaction.create({ title: headline.title,...headline });
}
transaction
.commit()
.then((res) => {
console.log("All headlines created");
// Finish the cron job here
})
.catch((error) => {
console.error("Couldn't create the headline documents",error);
// Maybe retry it here?
});
希望有帮助,帖木儿ur
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。