@content-collections/core 0.8.1 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/dist/index.d.ts +59 -32
  2. package/dist/index.js +924 -850
  3. package/package.json +6 -5
package/dist/index.d.ts CHANGED
@@ -1,7 +1,11 @@
1
- import z__default, { ZodRawShape, z as z$1, ZodObject, ZodTypeAny, ZodString } from 'zod';
1
+ import { StandardSchemaV1 } from '@standard-schema/spec';
2
+ import z__default, { ZodRawShape, z as z$1, ZodObject } from 'zod';
2
3
  export * from 'zod';
3
4
 
4
- type CacheFn = <TInput, TOutput>(input: TInput, compute: (input: TInput) => Promise<TOutput> | TOutput) => Promise<TOutput>;
5
+ type Options$2 = {
6
+ key: string;
7
+ };
8
+ type CacheFn = <TInput, TOutput>(input: TInput, compute: (input: TInput) => Promise<TOutput> | TOutput, options?: Options$2) => Promise<TOutput>;
5
9
 
6
10
  declare const importSymbol: unique symbol;
7
11
  type Import<T> = {
@@ -13,8 +17,14 @@ type GetTypeOfImport<T> = T extends Import<infer U> ? U : never;
13
17
  declare function createDefaultImport<T>(path: string): Import<T>;
14
18
  declare function createNamedImport<T>(name: string, path: string): Import<T>;
15
19
 
16
- type Parsers = typeof parsers;
17
- type Parser = keyof typeof parsers;
20
+ type ParseFn = (content: string) => Record<string, unknown> | Promise<Record<string, unknown>>;
21
+ type Parser = {
22
+ hasContent: boolean;
23
+ parse: ParseFn;
24
+ };
25
+ type PredefinedParsers = typeof parsers;
26
+ type PredefinedParser = keyof typeof parsers;
27
+ type ConfiguredParser = PredefinedParser | Parser;
18
28
  declare function parseYaml(content: string): any;
19
29
  declare function frontmatterParser(fileContent: string): {
20
30
  content: string;
@@ -23,23 +33,28 @@ declare function frontmatterOnlyParser(fileContent: string): {
23
33
  [key: string]: any;
24
34
  };
25
35
  declare const parsers: {
26
- readonly frontmatter: {
27
- readonly hasContent: true;
28
- readonly parse: typeof frontmatterParser;
36
+ frontmatter: {
37
+ hasContent: true;
38
+ parse: typeof frontmatterParser;
29
39
  };
30
- readonly "frontmatter-only": {
31
- readonly hasContent: false;
32
- readonly parse: typeof frontmatterOnlyParser;
40
+ "frontmatter-only": {
41
+ hasContent: false;
42
+ parse: typeof frontmatterOnlyParser;
33
43
  };
34
- readonly json: {
35
- readonly hasContent: false;
36
- readonly parse: (text: string, reviver?: (this: any, key: string, value: any) => any) => any;
44
+ json: {
45
+ hasContent: false;
46
+ parse: (text: string, reviver?: (this: any, key: string, value: any) => any) => any;
37
47
  };
38
- readonly yaml: {
39
- readonly hasContent: false;
40
- readonly parse: typeof parseYaml;
48
+ yaml: {
49
+ hasContent: false;
50
+ parse: typeof parseYaml;
41
51
  };
42
52
  };
53
+ type DefineParserResult<TArgument extends Parser | ParseFn> = TArgument extends Function ? {
54
+ hasContent: false;
55
+ parse: ParseFn;
56
+ } : TArgument extends infer Parser ? Parser : never;
57
+ declare function defineParser<TArgument extends Parser | ParseFn>(parser: TArgument): DefineParserResult<TArgument>;
43
58
 
44
59
  declare const literalSchema: z__default.ZodUnion<[z__default.ZodString, z__default.ZodNumber, z__default.ZodBoolean, z__default.ZodNull, z__default.ZodUndefined, z__default.ZodDate, z__default.ZodMap<z__default.ZodUnknown, z__default.ZodUnknown>, z__default.ZodSet<z__default.ZodUnknown>, z__default.ZodBigInt]>;
45
60
  type Literal = z__default.infer<typeof literalSchema>;
@@ -61,18 +76,24 @@ type Meta = {
61
76
  extension: string;
62
77
  };
63
78
  type WithContent = {
64
- content: ZodString;
79
+ content: string;
65
80
  };
66
- type AddContent<TShape extends ZodRawShape> = TShape extends {
67
- content: ZodTypeAny;
68
- } ? TShape : TShape & WithContent;
69
- type GetParsedShape<TParser extends Parser, TShape extends ZodRawShape> = Parsers[TParser]["hasContent"] extends true ? AddContent<TShape> : TShape;
70
- type GetShape<TParser extends Parser | undefined, TShape extends ZodRawShape> = TParser extends Parser ? GetParsedShape<TParser, TShape> : AddContent<TShape>;
71
- type Schema<TParser extends Parser | undefined, TShape extends ZodRawShape> = z$1.infer<ZodObject<GetShape<TParser, TShape>>> & {
81
+ type AddContent<TOutput> = TOutput extends {
82
+ content: any;
83
+ } ? TOutput : TOutput & WithContent;
84
+ type GetParser<TParser extends ConfiguredParser> = TParser extends PredefinedParser ? PredefinedParsers[TParser] : TParser;
85
+ type HasContent<TParser extends ConfiguredParser> = GetParser<TParser>["hasContent"];
86
+ type LegacySchema<TResult extends ZodRawShape = ZodRawShape> = (z: Z) => TResult;
87
+ type TSchemaProp = StandardSchemaV1 | LegacySchema;
88
+ type GetLegacySchemaShape<LegacySchema> = LegacySchema extends (z: Z) => infer TObjectShape ? TObjectShape : never;
89
+ type GetOutputShape<TShape extends TSchemaProp> = TShape extends StandardSchemaV1 ? StandardSchemaV1.InferOutput<TShape> : TShape extends ZodObject<any> ? z$1.infer<TShape> : TShape extends LegacySchema ? z$1.infer<ZodObject<GetLegacySchemaShape<TShape>>> : never;
90
+ type GetOutput<TParser extends ConfiguredParser, TShape extends TSchemaProp, TOutput = GetOutputShape<TShape>> = HasContent<TParser> extends true ? AddContent<TOutput> : TOutput;
91
+ type Schema<TParser extends ConfiguredParser, TShape extends TSchemaProp> = GetOutput<TParser, TShape> & {
72
92
  _meta: Meta;
73
93
  };
94
+ type GetSchema<TCollection extends AnyCollection> = TCollection extends Collection<any, infer TSchema, any, any, any, any> ? GetOutputShape<TSchema> : never;
74
95
  type Context<TSchema = unknown> = {
75
- documents<TCollection extends AnyCollection>(collection: TCollection): Array<Schema<TCollection["parser"], TCollection["schema"]>>;
96
+ documents<TCollection extends AnyCollection>(collection: TCollection): Array<GetSchema<TCollection>>;
76
97
  cache: CacheFn;
77
98
  collection: {
78
99
  name: string;
@@ -81,23 +102,23 @@ type Context<TSchema = unknown> = {
81
102
  };
82
103
  };
83
104
  type Z = typeof z$1;
84
- type CollectionRequest<TName extends string, TShape extends ZodRawShape, TParser, TSchema, TTransformResult, TDocument> = {
105
+ type CollectionRequest<TName extends string, TShape extends TSchemaProp, TParser, TSchema, TTransformResult, TDocument> = {
85
106
  name: TName;
86
107
  parser?: TParser;
87
108
  typeName?: string;
88
- schema: (z: Z) => TShape;
109
+ schema: TShape;
89
110
  transform?: (data: TSchema, context: Context<TSchema>) => TTransformResult;
90
111
  directory: string;
91
112
  include: string | string[];
92
113
  exclude?: string | string[];
93
114
  onSuccess?: (documents: Array<TDocument>) => void | Promise<void>;
94
115
  };
95
- type Collection<TName extends string, TShape extends ZodRawShape, TParser extends Parser, TSchema, TTransformResult, TDocument> = Omit<CollectionRequest<TName, TShape, TParser, TSchema, TTransformResult, TDocument>, "schema"> & {
116
+ type Collection<TName extends string, TShape extends TSchemaProp, TParser extends ConfiguredParser, TSchema, TTransformResult, TDocument> = Omit<CollectionRequest<TName, TShape, TParser, TSchema, TTransformResult, TDocument>, "schema"> & {
96
117
  typeName: string;
97
- schema: TShape;
118
+ schema: StandardSchemaV1;
98
119
  parser: TParser;
99
120
  };
100
- type AnyCollection = Collection<any, ZodRawShape, Parser, any, any, any>;
121
+ type AnyCollection = Collection<any, TSchemaProp, ConfiguredParser, any, any, any>;
101
122
  declare const InvalidReturnTypeSymbol: unique symbol;
102
123
  type InvalidReturnType<TMessage extends string, TObject> = {
103
124
  [InvalidReturnTypeSymbol]: TMessage;
@@ -106,7 +127,7 @@ type InvalidReturnType<TMessage extends string, TObject> = {
106
127
  type ResolveImports<TTransformResult> = TTransformResult extends Import<any> ? GetTypeOfImport<TTransformResult> : TTransformResult extends Array<infer U> ? Array<ResolveImports<U>> : TTransformResult extends (...args: any[]) => any ? TTransformResult : TTransformResult extends object ? {
107
128
  [K in keyof TTransformResult]: ResolveImports<TTransformResult[K]>;
108
129
  } : TTransformResult;
109
- declare function defineCollection<TName extends string, TShape extends ZodRawShape, TParser extends Parser = "frontmatter", TSchema = Schema<TParser, TShape>, TTransformResult = never, TDocument = [TTransformResult] extends [never] ? Schema<TParser, TShape> : Awaited<TTransformResult>, TResult = TDocument extends Serializable ? Collection<TName, TShape, TParser, TSchema, TTransformResult, ResolveImports<TDocument>> : InvalidReturnType<NotSerializableError, TDocument>>(collection: CollectionRequest<TName, TShape, TParser, TSchema, TTransformResult, TDocument>): TResult;
130
+ declare function defineCollection<TName extends string, TShape extends TSchemaProp, TParser extends ConfiguredParser = "frontmatter", TSchema = Schema<TParser, TShape>, TTransformResult = never, TDocument = [TTransformResult] extends [never] ? Schema<TParser, TShape> : Awaited<TTransformResult>, TResult = TDocument extends Serializable ? Collection<TName, TShape, TParser, TSchema, TTransformResult, ResolveImports<TDocument>> : InvalidReturnType<NotSerializableError, TDocument>>(collection: CollectionRequest<TName, TShape, TParser, TSchema, TTransformResult, TDocument>): TResult;
110
131
  type Cache = "memory" | "file" | "none";
111
132
  type Configuration<TCollections extends Array<AnyCollection>> = {
112
133
  collections: TCollections;
@@ -129,7 +150,7 @@ type CollectionFile = {
129
150
  type CollectionByName<TConfiguration extends AnyConfiguration> = {
130
151
  [TCollection in TConfiguration["collections"][number] as TCollection["name"]]: TCollection;
131
152
  };
132
- type GetDocument<TCollection extends AnyCollection> = TCollection extends Collection<any, ZodRawShape, any, any, any, infer TDocument> ? TDocument : never;
153
+ type GetDocument<TCollection extends AnyCollection> = TCollection extends Collection<any, any, any, any, any, infer TDocument> ? TDocument : never;
133
154
  type GetTypeByName<TConfiguration extends AnyConfiguration, TName extends keyof CollectionByName<TConfiguration>, TCollection = CollectionByName<TConfiguration>[TName]> = TCollection extends AnyCollection ? GetDocument<TCollection> : never;
134
155
 
135
156
  type CollectorEvents = {
@@ -281,4 +302,10 @@ declare function createBuilder(configurationPath: string, options?: Options, emi
281
302
  }>;
282
303
  type Builder = Awaited<ReturnType<typeof createBuilder>>;
283
304
 
284
- export { type AnyCollection, type AnyConfiguration, type Builder, type BuilderEvents, CollectError, type Collection, type CollectionRequest, type Configuration, ConfigurationError, ConfigurationReloadError, type Context, type Document, type GetTypeByName, type Meta, type Modification, type Schema, TransformError, type Watcher, createBuilder, createDefaultImport, createNamedImport, defineCollection, defineConfig };
305
+ declare const deprecations: {
306
+ legacySchema: string;
307
+ };
308
+ type Deprecation = keyof typeof deprecations;
309
+ declare function suppressDeprecatedWarnings(...deprecations: Array<Deprecation | "all">): void;
310
+
311
+ export { type AnyCollection, type AnyConfiguration, type Builder, type BuilderEvents, CollectError, type Collection, type CollectionRequest, type Configuration, ConfigurationError, ConfigurationReloadError, type Context, type Document, type GetTypeByName, type Meta, type Modification, type Schema, TransformError, type Watcher, createBuilder, createDefaultImport, createNamedImport, defineCollection, defineConfig, defineParser, suppressDeprecatedWarnings };