【问题标题】:Is it possible with Sanity? scheduled external API calls to Sanity理智可以吗?计划对 Sanity 的外部 API 调用
【发布时间】:2021-01-26 08:04:49
【问题描述】:

这是我想要完成的任务?

1- 每晚对新闻 API 进行 fetch 调用 2-拉最新的头条新闻 3- 导入到 Sanity...

理智允许你这样做吗?它有 cron 任务吗?

谢谢,

【问题讨论】:

    标签: sanity


    【解决方案1】:

    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?
      });
    

    希望对您有所帮助,帖木儿?

    【讨论】:

    猜你喜欢
    • 2021-06-11
    • 1970-01-01
    • 1970-01-01
    • 2020-04-25
    • 2015-09-03
    • 1970-01-01
    • 2021-05-18
    • 2013-05-31
    • 1970-01-01
    相关资源
    最近更新 更多