Comparing version 0.1.0-next.15 to 0.1.0-next.16
@@ -34,2 +34,3 @@ "use strict"; | ||
schema: z_1.z.object(schema), | ||
extensions: {}, | ||
}; | ||
@@ -39,2 +40,11 @@ variants.push(currentVariant); | ||
}, | ||
extend: (extensions) => { | ||
if (!currentVariant) | ||
throw new Error(`Define variant first.`); | ||
currentVariant.extensions = { | ||
...currentVariant.extensions, | ||
...extensions, | ||
}; | ||
return api; | ||
}, | ||
codec: (codecDef) => { | ||
@@ -69,3 +79,3 @@ if (!currentVariant) | ||
throw new Error(`Codec not implemented.`); | ||
const data = v.codec.decode(value); | ||
const data = v.codec.decode(value, v.extensions); | ||
// TODO inspect the value for better rendering. | ||
@@ -81,2 +91,3 @@ if (data === null) | ||
}, | ||
...v.extensions, | ||
}; | ||
@@ -83,0 +94,0 @@ return api; |
@@ -30,3 +30,3 @@ /** | ||
*/ | ||
variant<Name extends string, Schema extends SchemaBase>(name: Name, schema?: Schema): PostVariantBuilder<ADT, CreateStoredVariant<Name, Schema, false, never, never>, Vs>; | ||
variant<Name extends string, Schema extends SchemaBase>(name: Name, schema?: Schema): PostVariantBuilder<ADT, CreateStoredVariant<Name, Schema, false, never, {}>, Vs>; | ||
} | ||
@@ -39,2 +39,7 @@ /** | ||
codec(params: CodecParams<V>): PostCodecBuilder<ADT, StoredVariant.EnableCodec<V>, Vs>; | ||
/** | ||
* Extend the ADT with new properties. | ||
* TODO | ||
*/ | ||
extend<Extensions extends ExtensionsBase>(extensions: Extensions): PostVariantBuilder<ADT, StoredVariant.AddExtensions<Extensions, V>, Vs>; | ||
done(): Controller<ADT, [V, ...Vs]>; | ||
@@ -44,5 +49,6 @@ } | ||
encode: Encoder<V>; | ||
decode: Decoder<V>; | ||
decode: DecoderDefinition<V>; | ||
} | ||
export declare type Encoder<V extends StoredVariant> = (decodedData: StoredVariant.GetType<V>) => string; | ||
export declare type DecoderDefinition<V extends StoredVariant> = (encodedData: string, extensions: V[`extensions`]) => null | GetConstructorInput<V>; | ||
export declare type Decoder<V extends StoredVariant> = (encodedData: string) => null | GetConstructorInput<V>; | ||
@@ -54,2 +60,7 @@ /** | ||
export interface PostCodecBuilder<ADT extends StoredADT, V extends StoredVariant, Vs extends StoredVariants> extends VariantRequired<ADT, [V, ...Vs]> { | ||
/** | ||
* Extend the ADT with new properties. | ||
* TODO | ||
*/ | ||
extend<Extensions extends ExtensionsBase>(extensions: Extensions): PostCodecBuilder<ADT, StoredVariant.AddExtensions<Extensions, V>, Vs>; | ||
done(): Controller<ADT, [V, ...Vs]>; | ||
@@ -75,4 +86,4 @@ } | ||
codec: boolean; | ||
extensions: ExtensionsBase; | ||
parse?: Parse2; | ||
extensions?: ExtensionsBase; | ||
}; | ||
@@ -83,2 +94,5 @@ export declare namespace StoredVariant { | ||
}; | ||
type AddExtensions<Extensions extends ExtensionsBase, V extends StoredVariant> = V & { | ||
extensions: Extensions; | ||
}; | ||
type GetType<V extends StoredVariant> = z.TypeOf<z.ZodObject<V[`schema`]>>; | ||
@@ -85,0 +99,0 @@ type GetZodSchema<V extends StoredVariant> = z.ZodObject<V[`schema`]>; |
@@ -122,3 +122,3 @@ import { Decoder, Encoder, Parse2, Parse2OrThrow, StoredADT, StoredVariant, StoredVariants } from './Builder'; | ||
decode: never; | ||
}); | ||
}) & (V[`extensions`]); | ||
export declare type GetConstructorInput<V extends StoredVariant> = z.TypeOf<z.Omit<StoredVariant.GetZodSchema<V>, { | ||
@@ -125,0 +125,0 @@ _tag: true; |
@@ -30,2 +30,3 @@ import { Errors } from './Errors'; | ||
schema: z.object(schema), | ||
extensions: {}, | ||
}; | ||
@@ -35,2 +36,11 @@ variants.push(currentVariant); | ||
}, | ||
extend: (extensions) => { | ||
if (!currentVariant) | ||
throw new Error(`Define variant first.`); | ||
currentVariant.extensions = { | ||
...currentVariant.extensions, | ||
...extensions, | ||
}; | ||
return api; | ||
}, | ||
codec: (codecDef) => { | ||
@@ -65,3 +75,3 @@ if (!currentVariant) | ||
throw new Error(`Codec not implemented.`); | ||
const data = v.codec.decode(value); | ||
const data = v.codec.decode(value, v.extensions); | ||
// TODO inspect the value for better rendering. | ||
@@ -77,2 +87,3 @@ if (data === null) | ||
}, | ||
...v.extensions, | ||
}; | ||
@@ -79,0 +90,0 @@ return api; |
@@ -30,3 +30,3 @@ /** | ||
*/ | ||
variant<Name extends string, Schema extends SchemaBase>(name: Name, schema?: Schema): PostVariantBuilder<ADT, CreateStoredVariant<Name, Schema, false, never, never>, Vs>; | ||
variant<Name extends string, Schema extends SchemaBase>(name: Name, schema?: Schema): PostVariantBuilder<ADT, CreateStoredVariant<Name, Schema, false, never, {}>, Vs>; | ||
} | ||
@@ -39,2 +39,7 @@ /** | ||
codec(params: CodecParams<V>): PostCodecBuilder<ADT, StoredVariant.EnableCodec<V>, Vs>; | ||
/** | ||
* Extend the ADT with new properties. | ||
* TODO | ||
*/ | ||
extend<Extensions extends ExtensionsBase>(extensions: Extensions): PostVariantBuilder<ADT, StoredVariant.AddExtensions<Extensions, V>, Vs>; | ||
done(): Controller<ADT, [V, ...Vs]>; | ||
@@ -44,5 +49,6 @@ } | ||
encode: Encoder<V>; | ||
decode: Decoder<V>; | ||
decode: DecoderDefinition<V>; | ||
} | ||
export declare type Encoder<V extends StoredVariant> = (decodedData: StoredVariant.GetType<V>) => string; | ||
export declare type DecoderDefinition<V extends StoredVariant> = (encodedData: string, extensions: V[`extensions`]) => null | GetConstructorInput<V>; | ||
export declare type Decoder<V extends StoredVariant> = (encodedData: string) => null | GetConstructorInput<V>; | ||
@@ -54,2 +60,7 @@ /** | ||
export interface PostCodecBuilder<ADT extends StoredADT, V extends StoredVariant, Vs extends StoredVariants> extends VariantRequired<ADT, [V, ...Vs]> { | ||
/** | ||
* Extend the ADT with new properties. | ||
* TODO | ||
*/ | ||
extend<Extensions extends ExtensionsBase>(extensions: Extensions): PostCodecBuilder<ADT, StoredVariant.AddExtensions<Extensions, V>, Vs>; | ||
done(): Controller<ADT, [V, ...Vs]>; | ||
@@ -75,4 +86,4 @@ } | ||
codec: boolean; | ||
extensions: ExtensionsBase; | ||
parse?: Parse2; | ||
extensions?: ExtensionsBase; | ||
}; | ||
@@ -83,2 +94,5 @@ export declare namespace StoredVariant { | ||
}; | ||
type AddExtensions<Extensions extends ExtensionsBase, V extends StoredVariant> = V & { | ||
extensions: Extensions; | ||
}; | ||
type GetType<V extends StoredVariant> = z.TypeOf<z.ZodObject<V[`schema`]>>; | ||
@@ -85,0 +99,0 @@ type GetZodSchema<V extends StoredVariant> = z.ZodObject<V[`schema`]>; |
@@ -122,3 +122,3 @@ import { Decoder, Encoder, Parse2, Parse2OrThrow, StoredADT, StoredVariant, StoredVariants } from './Builder'; | ||
decode: never; | ||
}); | ||
}) & (V[`extensions`]); | ||
export declare type GetConstructorInput<V extends StoredVariant> = z.TypeOf<z.Omit<StoredVariant.GetZodSchema<V>, { | ||
@@ -125,0 +125,0 @@ _tag: true; |
{ | ||
"name": "alge", | ||
"version": "0.1.0-next.15", | ||
"version": "0.1.0-next.16", | ||
"repository": "git@github.com:jasonkuhrt/alge.git", | ||
@@ -5,0 +5,0 @@ "author": "Jason Kuhrt", |
@@ -19,5 +19,6 @@ # alge 🌱 | ||
1. Look at the tests. | ||
1. Look at the examples under [`/examples`](/examples). | ||
2. Just install it and try it out. | ||
3. Read the source code. | ||
3. Look at the tests. | ||
4. Read the source code. | ||
@@ -24,0 +25,0 @@ Cheers! <3 |
@@ -35,2 +35,31 @@ import { Alge } from '.' | ||
}) | ||
describe(`.extend()`, () => { | ||
it(`extends the ADT with properties`, () => { | ||
const A = Alge.create($A) | ||
.variant($M) | ||
.extend({ foo: 1 as const }) | ||
.done() | ||
expectType<1>(A.M.foo) | ||
expect(A.M.foo).toBe(1) | ||
}) | ||
// TODO make available to encoder as well. | ||
it(`extensions are available to decoder`, () => { | ||
const A = Alge.create($A) | ||
.variant($M, { | ||
m: z.string(), | ||
}) | ||
.extend({ foo: 1 as const }) | ||
.codec({ | ||
encode: (data) => data.m, | ||
decode: (value, extensions) => { | ||
expectType<1>(extensions.foo) | ||
expect(extensions).toEqual({ foo: 1 }) | ||
return { m: value } | ||
}, | ||
}) | ||
.done() | ||
expectType<1>(A.M.foo) | ||
expect(A.M.foo).toBe(1) | ||
}) | ||
}) | ||
describe(`.codec()`, () => { | ||
@@ -37,0 +66,0 @@ it(`if not defined then variant API codec methods not available`, () => { |
@@ -1,2 +0,2 @@ | ||
import { CodecParams, Initial, StoredVariant } from './Builder' | ||
import { CodecParams, ExtensionsBase, Initial, StoredVariant } from './Builder' | ||
import { Errors } from './Errors' | ||
@@ -172,2 +172,3 @@ import { is } from './helpers' | ||
schema: z.object(schema), | ||
extensions: {}, | ||
} | ||
@@ -177,2 +178,10 @@ variants.push(currentVariant) | ||
}, | ||
extend: (extensions: ExtensionsBase) => { | ||
if (!currentVariant) throw new Error(`Define variant first.`) | ||
currentVariant.extensions = { | ||
...currentVariant.extensions, | ||
...extensions, | ||
} | ||
return api | ||
}, | ||
codec: (codecDef: CodecParams) => { | ||
@@ -206,3 +215,3 @@ if (!currentVariant) throw new Error(`Define variant first.`) | ||
if (!v.codec) throw new Error(`Codec not implemented.`) | ||
const data = v.codec.decode(value) | ||
const data = v.codec.decode(value, v.extensions) | ||
// TODO inspect the value for better rendering. | ||
@@ -216,2 +225,3 @@ if (data === null) throw new Error(`Failed to decode value \`${value}\` into a ${name}.`) | ||
}, | ||
...v.extensions, | ||
} | ||
@@ -218,0 +228,0 @@ return api |
@@ -45,3 +45,4 @@ /** | ||
schema?: Schema | ||
): PostVariantBuilder<ADT, CreateStoredVariant<Name, Schema, false, never, never>, Vs> | ||
//eslint-disable-next-line | ||
): PostVariantBuilder<ADT, CreateStoredVariant<Name, Schema, false, never, {}>, Vs> | ||
} | ||
@@ -56,2 +57,9 @@ | ||
codec(params: CodecParams<V>): PostCodecBuilder<ADT, StoredVariant.EnableCodec<V>, Vs> | ||
/** | ||
* Extend the ADT with new properties. | ||
* TODO | ||
*/ | ||
extend<Extensions extends ExtensionsBase>( | ||
extensions: Extensions | ||
): PostVariantBuilder<ADT, StoredVariant.AddExtensions<Extensions, V>, Vs> | ||
done(): Controller<ADT, [V, ...Vs]> | ||
@@ -62,3 +70,3 @@ } | ||
encode: Encoder<V> | ||
decode: Decoder<V> | ||
decode: DecoderDefinition<V> | ||
} | ||
@@ -68,2 +76,7 @@ | ||
export type DecoderDefinition<V extends StoredVariant> = ( | ||
encodedData: string, | ||
extensions: V[`extensions`] | ||
) => null | GetConstructorInput<V> | ||
export type Decoder<V extends StoredVariant> = (encodedData: string) => null | GetConstructorInput<V> | ||
@@ -77,2 +90,9 @@ | ||
extends VariantRequired<ADT, [V, ...Vs]> { | ||
/** | ||
* Extend the ADT with new properties. | ||
* TODO | ||
*/ | ||
extend<Extensions extends ExtensionsBase>( | ||
extensions: Extensions | ||
): PostCodecBuilder<ADT, StoredVariant.AddExtensions<Extensions, V>, Vs> | ||
done(): Controller<ADT, [V, ...Vs]> | ||
@@ -107,4 +127,4 @@ } | ||
codec: boolean | ||
extensions: ExtensionsBase | ||
parse?: Parse2 | ||
extensions?: ExtensionsBase | ||
} | ||
@@ -117,2 +137,5 @@ | ||
} | ||
export type AddExtensions<Extensions extends ExtensionsBase, V extends StoredVariant> = V & { | ||
extensions: Extensions | ||
} | ||
export type GetType<V extends StoredVariant> = z.TypeOf<z.ZodObject<V[`schema`]>> | ||
@@ -119,0 +142,0 @@ export type GetZodSchema<V extends StoredVariant> = z.ZodObject<V[`schema`]> |
@@ -146,3 +146,5 @@ import { Decoder, Encoder, Parse2, Parse2OrThrow, StoredADT, StoredVariant, StoredVariants } from './Builder' | ||
decode: never | ||
}) | ||
}) & | ||
(V[`extensions`]) | ||
// & ( | ||
@@ -149,0 +151,0 @@ // IsUnknown<Def[`parse`]> extends true |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
107320
1825
27