Astro 气压装载机
这个包件为Astro提供了一台气表装载机. 它允许您从一个 Airtable 基地装入记录,并在您的 Astro 计划中将其作为内容.
安装
npm install @ascorbic/airtable-loader
使用量
这个包需要Astro 4.14.0或以后. 您必须启用Astro的实验内容层, 除非您正在使用5. 0.0- beta 版本或稍后使用 。 你可以这样做,在你的 astro.config.mjs数字 :
export default defineConfig({
// ...
experimental: {
contentLayer: true,
},
});
您需要创建 Airtable 个人访问标志 。 您可以创建一个 这里。 。 。 .
您应该确保此标志能够访问您想要使用的基数,并具有以下范围:
data.records:readschema.bases:read
然后可以在内容收集配置中使用种子加载器 :
// src/content/config.ts
import { defineCollection } from "astro:content";
import { airtableLoader } from "@ascorbic/airtable-loader";
const launches = defineCollection({
loader: airtableLoader({
base: import.meta.env.AIRTABLE_BASE,
table: "Product Launches",
}),
});
export const collections = { launches };
然后,您可以像Astro的其他内容收藏一样使用这些内容. 数据是安全的类型,类型会根据"空气表"的"计划"自动生成.
---
import type { GetStaticPaths } from "astro";
import { getCollection, type CollectionEntry } from "astro:content";
import Layout from "../../layouts/Layout.astro";
export const getStaticPaths: GetStaticPaths = async () => {
const launches = await getCollection("launches");
return launches.map((launch) => ({
params: {
id: launch.id,
},
props: { launch },
}));
};
type Props = { launch: CollectionEntry<"launches"> };
const { launch } = Astro.props;
const { data } = launch;
---
<Layout title={data.firstName}>
<h1>{data["Launch Name"]}</h1>
<p>{data["Launch date"]?.toDateString()}</p>
<p>{data.Description}</p>
</Layout>
选项
这个 airtableLoader 函数取一个对象,其选项如下:
base: AirTable 基地的ID 您想要从中装入记录.table:要从底部装入记录的表格名称或标识.token:你的空中个人访问标志。 如果不提供,请从AIRTABLE_TOKEN环境变量.queryParams:可选对象,可选择通过Airtable API. 这可以用来过滤或限制返回的记录. 见 空中查询文件 更多信息.