@content-collections/core 0.8.2 → 0.9.1

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 +58 -31
  2. package/dist/index.js +920 -846
  3. package/package.json +16 -5
package/dist/index.d.ts CHANGED
@@ -1,4 +1,5 @@
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
5
  type Options$2 = {
@@ -16,8 +17,14 @@ type GetTypeOfImport<T> = T extends Import<infer U> ? U : never;
16
17
  declare function createDefaultImport<T>(path: string): Import<T>;
17
18
  declare function createNamedImport<T>(name: string, path: string): Import<T>;
18
19
 
19
- type Parsers = typeof parsers;
20
- 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;
21
28
  declare function parseYaml(content: string): any;
22
29
  declare function frontmatterParser(fileContent: string): {
23
30
  content: string;
@@ -26,23 +33,28 @@ declare function frontmatterOnlyParser(fileContent: string): {
26
33
  [key: string]: any;
27
34
  };
28
35
  declare const parsers: {
29
- readonly frontmatter: {
30
- readonly hasContent: true;
31
- readonly parse: typeof frontmatterParser;
36
+ frontmatter: {
37
+ hasContent: true;
38
+ parse: typeof frontmatterParser;
32
39
  };
33
- readonly "frontmatter-only": {
34
- readonly hasContent: false;
35
- readonly parse: typeof frontmatterOnlyParser;
40
+ "frontmatter-only": {
41
+ hasContent: false;
42
+ parse: typeof frontmatterOnlyParser;
36
43
  };
37
- readonly json: {
38
- readonly hasContent: false;
39
- 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;
40
47
  };
41
- readonly yaml: {
42
- readonly hasContent: false;
43
- readonly parse: typeof parseYaml;
48
+ yaml: {
49
+ hasContent: false;
50
+ parse: typeof parseYaml;
44
51
  };
45
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>;
46
58
 
47
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]>;
48
60
  type Literal = z__default.infer<typeof literalSchema>;
@@ -64,18 +76,27 @@ type Meta = {
64
76
  extension: string;
65
77
  };
66
78
  type WithContent = {
67
- content: ZodString;
79
+ content: string;
68
80
  };
69
- type AddContent<TShape extends ZodRawShape> = TShape extends {
70
- content: ZodTypeAny;
71
- } ? TShape : TShape & WithContent;
72
- type GetParsedShape<TParser extends Parser, TShape extends ZodRawShape> = Parsers[TParser]["hasContent"] extends true ? AddContent<TShape> : TShape;
73
- type GetShape<TParser extends Parser | undefined, TShape extends ZodRawShape> = TParser extends Parser ? GetParsedShape<TParser, TShape> : AddContent<TShape>;
74
- 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> & {
75
92
  _meta: Meta;
76
93
  };
94
+ type Prettify<T> = {
95
+ [K in keyof T]: T[K];
96
+ } & {};
97
+ type GetSchema<TCollection extends AnyCollection> = TCollection extends Collection<any, any, any, infer TSchema, any, any> ? Prettify<TSchema> : never;
77
98
  type Context<TSchema = unknown> = {
78
- documents<TCollection extends AnyCollection>(collection: TCollection): Array<Schema<TCollection["parser"], TCollection["schema"]>>;
99
+ documents<TCollection extends AnyCollection>(collection: TCollection): Array<GetSchema<TCollection>>;
79
100
  cache: CacheFn;
80
101
  collection: {
81
102
  name: string;
@@ -84,23 +105,23 @@ type Context<TSchema = unknown> = {
84
105
  };
85
106
  };
86
107
  type Z = typeof z$1;
87
- type CollectionRequest<TName extends string, TShape extends ZodRawShape, TParser, TSchema, TTransformResult, TDocument> = {
108
+ type CollectionRequest<TName extends string, TShape extends TSchemaProp, TParser, TSchema, TTransformResult, TDocument> = {
88
109
  name: TName;
89
110
  parser?: TParser;
90
111
  typeName?: string;
91
- schema: (z: Z) => TShape;
112
+ schema: TShape;
92
113
  transform?: (data: TSchema, context: Context<TSchema>) => TTransformResult;
93
114
  directory: string;
94
115
  include: string | string[];
95
116
  exclude?: string | string[];
96
117
  onSuccess?: (documents: Array<TDocument>) => void | Promise<void>;
97
118
  };
98
- type Collection<TName extends string, TShape extends ZodRawShape, TParser extends Parser, TSchema, TTransformResult, TDocument> = Omit<CollectionRequest<TName, TShape, TParser, TSchema, TTransformResult, TDocument>, "schema"> & {
119
+ type Collection<TName extends string, TShape extends TSchemaProp, TParser extends ConfiguredParser, TSchema, TTransformResult, TDocument> = Omit<CollectionRequest<TName, TShape, TParser, TSchema, TTransformResult, TDocument>, "schema"> & {
99
120
  typeName: string;
100
- schema: TShape;
121
+ schema: StandardSchemaV1;
101
122
  parser: TParser;
102
123
  };
103
- type AnyCollection = Collection<any, ZodRawShape, Parser, any, any, any>;
124
+ type AnyCollection = Collection<any, TSchemaProp, ConfiguredParser, any, any, any>;
104
125
  declare const InvalidReturnTypeSymbol: unique symbol;
105
126
  type InvalidReturnType<TMessage extends string, TObject> = {
106
127
  [InvalidReturnTypeSymbol]: TMessage;
@@ -109,7 +130,7 @@ type InvalidReturnType<TMessage extends string, TObject> = {
109
130
  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 ? {
110
131
  [K in keyof TTransformResult]: ResolveImports<TTransformResult[K]>;
111
132
  } : TTransformResult;
112
- 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;
133
+ 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;
113
134
  type Cache = "memory" | "file" | "none";
114
135
  type Configuration<TCollections extends Array<AnyCollection>> = {
115
136
  collections: TCollections;
@@ -132,7 +153,7 @@ type CollectionFile = {
132
153
  type CollectionByName<TConfiguration extends AnyConfiguration> = {
133
154
  [TCollection in TConfiguration["collections"][number] as TCollection["name"]]: TCollection;
134
155
  };
135
- type GetDocument<TCollection extends AnyCollection> = TCollection extends Collection<any, ZodRawShape, any, any, any, infer TDocument> ? TDocument : never;
156
+ type GetDocument<TCollection extends AnyCollection> = TCollection extends Collection<any, any, any, any, any, infer TDocument> ? TDocument : never;
136
157
  type GetTypeByName<TConfiguration extends AnyConfiguration, TName extends keyof CollectionByName<TConfiguration>, TCollection = CollectionByName<TConfiguration>[TName]> = TCollection extends AnyCollection ? GetDocument<TCollection> : never;
137
158
 
138
159
  type CollectorEvents = {
@@ -284,4 +305,10 @@ declare function createBuilder(configurationPath: string, options?: Options, emi
284
305
  }>;
285
306
  type Builder = Awaited<ReturnType<typeof createBuilder>>;
286
307
 
287
- 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 };
308
+ declare const deprecations: {
309
+ legacySchema: string;
310
+ };
311
+ type Deprecation = keyof typeof deprecations;
312
+ declare function suppressDeprecatedWarnings(...deprecations: Array<Deprecation | "all">): void;
313
+
314
+ 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 };