https://github.com/adrian-ub/astro-loader
Fetch data from API and use it in Astro collections
https://github.com/adrian-ub/astro-loader
astro astro-loader hashnode
Last synced: about 2 months ago
JSON representation
Fetch data from API and use it in Astro collections
- Host: GitHub
- URL: https://github.com/adrian-ub/astro-loader
- Owner: adrian-ub
- License: mit
- Created: 2024-09-20T02:33:09.000Z (8 months ago)
- Default Branch: main
- Last Pushed: 2024-10-05T01:17:32.000Z (8 months ago)
- Last Synced: 2025-04-03T09:07:23.143Z (about 2 months ago)
- Topics: astro, astro-loader, hashnode
- Language: TypeScript
- Homepage:
- Size: 380 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE.md
Awesome Lists containing this project
README
# astro-loader
[![npm version][npm-version-src]][npm-version-href]
[![npm downloads][npm-downloads-src]][npm-downloads-href]
[![bundle][bundle-src]][bundle-href]
[![JSDocs][jsdocs-src]][jsdocs-href]
[![License][license-src]][license-href]A package for loading content into Astro's Content Collection API
## Install
```sh
pnpm add astro-loader
```## loaders
astro-loader/hashnode
### Posts
```ts
import { defineCollection, z } from 'astro:content'import { HashnodeLoader } from 'astro-loader/hashnode'
const posts = defineCollection({
loader: HashnodeLoader({
operation: 'posts',
publicationHost: 'adrianub.dev/hashnode',
fields: ['slug', 'title', 'publishedAt', 'subtitle', { coverImage: ['url'], content: ['html', 'markdown'] }],
}),
schema: z.object({
slug: z.string(),
title: z.string(),
subtitle: z.string().optional(),
publishedAt: z.string().transform(date => new Date(date)),
coverImage: z.object({
url: z.string().url(),
}),
content: z.object({
html: z.string(),
markdown: z.string(),
}),
}),
})export const collections = { posts }
```### Series
```ts
import { defineCollection, z } from 'astro:content'import { HashnodeLoader } from 'astro-loader/hashnode'
const series = defineCollection({
loader: HashnodeLoader({
publicationHost: 'adrianub.dev/hashnode',
operation: 'seriesList',
fields: [
'slug',
'name',
'createdAt',
'coverImage',
{
description: ['html', 'markdown'],
},
],
withPosts: {
fields: ['slug', 'title', 'publishedAt', { coverImage: ['url'] }],
},
}),
schema: z.object({
slug: z.string(),
name: z.string(),
createdAt: z.string().transform(date => new Date(date)),
coverImage: z.string().url(),
description: z.object({
html: z.string(),
markdown: z.string(),
}),
posts: z.array(z.object({
slug: z.string(),
title: z.string(),
publishedAt: z.string().transform(date => new Date(date)),
coverImage: z.object({
url: z.string().url(),
}),
})),
}),
})export const collections = { series }
```### Pages
```ts
import { defineCollection, z } from 'astro:content'import { HashnodeLoader } from 'astro-loader/hashnode'
const pages = defineCollection({
loader: HashnodeLoader({
operation: 'staticPages',
publicationHost: 'adrianub.dev/hashnode',
fields: [
'slug',
'title',
{
content: ['html', 'markdown'],
ogMetaData: ['image'],
},
],
}),
schema: z.object({
slug: z.string(),
title: z.string(),
content: z.object({
html: z.string(),
markdown: z.string(),
}),
ogMetaData: z.object({
image: z.string().url(),
}),
}),
})export const collections = { pages }
```## Sponsors
## License
[MIT](./LICENSE) License © 2024-PRESENT [Adrián UB](https://github.com/adrian-ub)
[npm-version-src]: https://img.shields.io/npm/v/astro-loader?style=flat&colorA=080f12&colorB=1fa669
[npm-version-href]: https://npmjs.com/package/astro-loader
[npm-downloads-src]: https://img.shields.io/npm/dm/astro-loader?style=flat&colorA=080f12&colorB=1fa669
[npm-downloads-href]: https://npmjs.com/package/astro-loader
[bundle-src]: https://img.shields.io/bundlephobia/minzip/astro-loader?style=flat&colorA=080f12&colorB=1fa669&label=minzip
[bundle-href]: https://bundlephobia.com/result?p=astro-loader
[license-src]: https://img.shields.io/github/license/adrian-ub/astro-loader.svg?style=flat&colorA=080f12&colorB=1fa669
[license-href]: https://github.com/adrian-ub/astro-loader/blob/main/LICENSE
[jsdocs-src]: https://img.shields.io/badge/jsdocs-reference-080f12?style=flat&colorA=080f12&colorB=1fa669
[jsdocs-href]: https://www.jsdocs.io/package/astro-loader