-
Notifications
You must be signed in to change notification settings - Fork 9
chore: Upgrade to Astro v6 #113
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
8726673
1edcff2
59bf35a
814f40d
1a4d93c
1232524
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| --- | ||
| "@ascorbic/airtable-loader": patch | ||
| "@ascorbic/bluesky-loader": patch | ||
| "@ascorbic/youtube-loader": patch | ||
| "@ascorbic/loader-utils": patch | ||
| "@ascorbic/feed-loader": patch | ||
| "@ascorbic/mock-loader": patch | ||
| "@ascorbic/csv-loader": patch | ||
| --- | ||
|
|
||
| Add Astro v6 to supported peerDependencies |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| import type { Loader } from "astro/loaders"; | ||
| import { AstroError } from "astro/errors"; | ||
| import Airtable, { type Query, type FieldSet } from "airtable"; | ||
| import { createTypeAlias, printNode, zodToTs } from "zod-to-ts"; | ||
| import { zodSchemaFromAirtableTable } from "./schema.js"; | ||
|
|
||
| export interface AirtableLoaderOptions<TFields extends FieldSet = FieldSet> { | ||
|
|
@@ -44,11 +45,19 @@ export function airtableLoader({ | |
| } | ||
| logger.info(`Loaded ${records.length} records from "${table}"`); | ||
| }, | ||
| schema: () => | ||
| zodSchemaFromAirtableTable({ | ||
| createSchema: async () => { | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since the schema generation is async we need to use |
||
| const schema = await zodSchemaFromAirtableTable({ | ||
| baseID: base, | ||
| tableIdOrName: table, | ||
| apiKey: token, | ||
| }), | ||
| }); | ||
| const identifier = "Entry"; | ||
| const { node } = zodToTs(schema, identifier); | ||
| const typeAlias = createTypeAlias(node, identifier); | ||
| return { | ||
| schema, | ||
| types: `export ${printNode(typeAlias)}`, | ||
| }; | ||
| }, | ||
| }; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,7 +15,7 @@ export const FeedCategorySchema = z.object({ | |
| }); | ||
|
|
||
| // Legacy schemas from original implementation for exact backward compatibility | ||
| export const LegacyNSSchema = z.record(z.string()); | ||
| export const LegacyNSSchema = z.record(z.string(), z.unknown()); | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Zod 4 requires two arguments for |
||
|
|
||
| export const LegacyImageSchema = z.object({ | ||
| url: z.string().optional(), | ||
|
|
@@ -111,7 +111,7 @@ export const LegacyItemSchema = z | |
| enclosures: z.array(LegacyEnclosureSchema), | ||
| meta: LegacyMetaSchema, | ||
| }) | ||
| .and(z.record(z.unknown())); // Allow additional fields | ||
| .and(z.record(z.string(), z.unknown())); // Allow additional fields | ||
|
|
||
| // Feed schema for the complete feed structure | ||
| export const FeedSchema = z.object({ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,8 @@ | ||
| import type { Loader } from "astro/loaders"; | ||
| import { AstroError } from "astro/errors"; | ||
| import type { ZodSchema } from "astro/zod"; | ||
| import { z } from "astro/zod"; | ||
| import { generateMock } from "@anatine/zod-mock"; | ||
| import { createTypeAlias, printNode, zodToTs } from "zod-to-ts"; | ||
|
|
||
| interface SharedOptions { | ||
| /** The number of entries to generate */ | ||
|
|
@@ -17,12 +18,12 @@ interface SharedOptions { | |
| export type MockLoaderOptions = SharedOptions & | ||
| ( | ||
| | { | ||
| schema: ZodSchema; | ||
| schema: z.ZodType; | ||
| loader?: never; | ||
| } | ||
| | { | ||
| loader: Loader; | ||
| schema?: ZodSchema; | ||
| schema?: z.ZodType; | ||
| } | ||
| ); | ||
| /** | ||
|
|
@@ -33,9 +34,9 @@ export function mockLoader({ | |
| loader, | ||
| ...options | ||
| }: MockLoaderOptions): Loader { | ||
| async function getSchema() { | ||
| async function getSchema(): Promise<z.ZodType<Record<string, unknown>>> { | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unfortunately had to cast |
||
| if (options.schema) { | ||
| return options.schema; | ||
| return options.schema as z.ZodType<Record<string, unknown>>; | ||
| } | ||
|
|
||
| if (!loader) { | ||
|
|
@@ -45,30 +46,30 @@ export function mockLoader({ | |
| ); | ||
| } | ||
|
|
||
| if (!loader.schema) { | ||
| throw new AstroError( | ||
| `The loader "${loader.name}" does not define a schema`, | ||
| "Define a schema manually and pass it to the `mockLoader`", | ||
| ); | ||
| if ("createSchema" in loader && loader.createSchema) { | ||
| const { schema } = await loader.createSchema(); | ||
| return schema as z.ZodType<Record<string, unknown>>; | ||
| } | ||
| if (typeof loader.schema === "function") { | ||
| return loader.schema(); | ||
| } else { | ||
| return loader.schema; | ||
|
|
||
| if ("schema" in loader && loader.schema) { | ||
| return loader.schema as z.ZodType<Record<string, unknown>>; | ||
| } | ||
| } | ||
|
|
||
| const schema = getSchema(); | ||
| throw new AstroError( | ||
| `The loader "${loader.name}" does not define a schema`, | ||
| "Define a schema manually and pass it to the `mockLoader`", | ||
| ); | ||
| } | ||
|
|
||
| return { | ||
| name: "mock-loader", | ||
| load: async ({ logger, store }) => { | ||
| logger.info( | ||
| `Generating mock data${loader?.name ? ` for ${loader.name}` : ""}`, | ||
| ); | ||
| const mockSchema = await schema; | ||
| const schema = await getSchema(); | ||
| for (let i = 0; i < entryCount; i++) { | ||
| const data = generateMock(mockSchema, { | ||
| const data = generateMock(schema, { | ||
| seed: options.seed ? options.seed + i : undefined, | ||
| }); | ||
| const id = options.idField ? data[options.idField] : i; | ||
|
|
@@ -81,7 +82,16 @@ export function mockLoader({ | |
| } | ||
| logger.info(`Generated ${entryCount} mock entries`); | ||
| }, | ||
| schema: () => schema, | ||
| createSchema: async () => { | ||
| const schema = await getSchema(); | ||
| const identifier = "Entry"; | ||
| const { node } = zodToTs(schema, identifier); | ||
| const typeAlias = createTypeAlias(node, identifier); | ||
| return { | ||
| schema, | ||
| types: `export ${printNode(typeAlias)}`, | ||
| }; | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
zod-to-tsv2.0.0 is available, but the API is different from what the Astro docs expect. Since Astro v5 usedzod-to-ts@1.2.0, I kept the version there for parity.