mirror of
https://github.com/withastro/astro.git
synced 2025-01-20 22:12:38 -05:00
fix: support promise response from data()
This commit is contained in:
parent
c94ddd0c09
commit
0e428df67a
2 changed files with 12 additions and 7 deletions
|
@ -7,6 +7,7 @@ import {
|
|||
type TextField,
|
||||
type collectionSchema,
|
||||
collectionsSchema,
|
||||
type MaybePromise,
|
||||
} from './types.js';
|
||||
import { z } from 'zod';
|
||||
|
||||
|
@ -30,14 +31,14 @@ type CollectionConfig<
|
|||
// TODO: type inference based on field type. Just `any` for now.
|
||||
seed?: Writable extends false
|
||||
? never
|
||||
: () => Array<Record<keyof TFields, any> & { id?: string }>;
|
||||
: () => MaybePromise<Array<Record<keyof TFields, any> & { id?: string }>>;
|
||||
}
|
||||
: {
|
||||
fields: TFields;
|
||||
// TODO: type inference based on field type. Just `any` for now.
|
||||
data?: Writable extends true
|
||||
? never
|
||||
: () => Array<Record<keyof TFields, any> & { id?: string }>;
|
||||
: () => MaybePromise<Array<Record<keyof TFields, any> & { id?: string }>>;
|
||||
};
|
||||
|
||||
type ResolvedCollectionConfig<
|
||||
|
|
|
@ -50,22 +50,24 @@ const fieldSchema = z.union([
|
|||
]);
|
||||
const fieldsSchema = z.record(fieldSchema);
|
||||
|
||||
const dataResponse = z.array(z.record(z.unknown()));
|
||||
|
||||
export const readableCollectionSchema = z.object({
|
||||
fields: fieldsSchema,
|
||||
data: z
|
||||
.function()
|
||||
.returns(z.array(z.record(z.unknown())))
|
||||
.returns(z.union([dataResponse, z.promise(dataResponse)]))
|
||||
.optional(),
|
||||
writable: z.literal(false)
|
||||
writable: z.literal(false),
|
||||
});
|
||||
|
||||
export const writableCollectionSchema = z.object({
|
||||
fields: fieldsSchema,
|
||||
seed: z
|
||||
.function()
|
||||
.returns(z.array(z.record(z.unknown())))
|
||||
.returns(z.union([dataResponse, z.promise(dataResponse)]))
|
||||
.optional(),
|
||||
writable: z.literal(true)
|
||||
writable: z.literal(true),
|
||||
});
|
||||
|
||||
export const collectionSchema = z.union([readableCollectionSchema, writableCollectionSchema]);
|
||||
|
@ -89,7 +91,9 @@ export type FieldType =
|
|||
export type DBField = z.infer<typeof fieldSchema>;
|
||||
export type DBFieldInput = DateFieldInput | BooleanField | NumberField | TextField | JsonField;
|
||||
export type DBFields = z.infer<typeof fieldsSchema>;
|
||||
export type DBCollection = z.infer<typeof readableCollectionSchema | typeof writableCollectionSchema>;
|
||||
export type DBCollection = z.infer<
|
||||
typeof readableCollectionSchema | typeof writableCollectionSchema
|
||||
>;
|
||||
export type DBCollections = Record<string, DBCollection>;
|
||||
export type ReadableDBCollection = z.infer<typeof readableCollectionSchema>;
|
||||
export type WritableDBCollection = z.infer<typeof writableCollectionSchema>;
|
||||
|
|
Loading…
Add table
Reference in a new issue