diff --git a/Property/Converter/Configuration.ts b/Property/Converter/Configuration.ts index d0d4990..e16357d 100644 --- a/Property/Converter/Configuration.ts +++ b/Property/Converter/Configuration.ts @@ -25,11 +25,9 @@ type GetValue

> = P extends type MaybePromise = T | Promise | Partial export type Configuration = Record, T extends Payload = Payload> = { [P in DotNotation]?: { - encode: ( - value: GetValue - ) => MaybePromise<(P extends DotNotation ? GetValue : Payload.Value) | undefined> - decode: ( - value: (P extends DotNotation ? GetValue : Payload.Value) | undefined + encode?: (value: GetValue) => MaybePromise

? GetValue : Payload.Value> + decode?: ( + value: P extends DotNotation ? GetValue : Payload.Value ) => MaybePromise> | undefined } } @@ -40,9 +38,9 @@ export namespace Configuration { isly.record( isly.string(), isly - .object<{ encode: (...params: any[]) => any; decode: (...params: any[]) => any }>({ - encode: isly.function(), - decode: isly.function(), + .object<{ encode?: (...params: any[]) => any; decode?: (...params: any[]) => any }>({ + encode: isly.union(isly.function(), isly.undefined()), + decode: isly.union(isly.function(), isly.undefined()), }) .optional() ) diff --git a/Property/Converter/index.spec.ts b/Property/Converter/index.spec.ts index 6aa36f1..2eeba2c 100644 --- a/Property/Converter/index.spec.ts +++ b/Property/Converter/index.spec.ts @@ -20,15 +20,15 @@ const target = { const conversionMap: authly.Property.Converter.Configuration = { foo: { encode: value => value + "transformed", - decode: value => value?.replace("transformed", "") ?? "", + decode: value => value?.replace("transformed", ""), }, num: { encode: value => value + 5, - decode: value => (value ?? 0) - 5, + decode: value => value - 5, }, array: { encode: value => value.map(v => v / 5), - decode: value => value?.map(v => v * 5) ?? 0, + decode: value => value.map(v => v * 5), }, "inside.foo": { encode: () => "Valuedifferent", @@ -40,7 +40,7 @@ const conversionMap: authly.Property.Converter.Configuration isoly.DateTime.epoch(value, "seconds"), - decode: value => isoly.DateTime.create(value ?? 0), + decode: value => isoly.DateTime.create(value), }, } @@ -49,6 +49,9 @@ const converter = new authly.Property.Converter(co describe("Converter", () => { it("Converter.Configuration.is", async () => { expect(authly.Property.Converter.Configuration.is(conversionMap)).toBe(true) + expect(authly.Property.Converter.Configuration.is({ foo: { encode: (value: string) => value } })).toEqual(true) + expect(authly.Property.Converter.Configuration.is({ foo: { decode: (value: string) => value } })).toEqual(true) + expect(authly.Property.Converter.Configuration.is({ foo: {} })).toEqual(true) }) it("Empty Transformmap", async () => { const converter = new authly.Property.Converter({}) @@ -76,4 +79,27 @@ describe("Converter", () => { const converter = new authly.Property.Converter(map) expect(await converter.apply({ flagly: "" })).toEqual({ flagly: {} }) }) + it("Only encode", async () => { + const converter = new authly.Property.Converter<{ foo: number }, { foo: string }>({ + foo: { + encode: value => value.toString(), + }, + }) + const source = { foo: 123 } + const target = await converter.apply(source) + expect(target).toEqual({ foo: "123" }) + expect(await converter.reverse(target)).toEqual(target) + }) + it("Only decode", async () => { + const converter = new authly.Property.Converter<{ foo: number }, { foo: string }>({ + foo: { + decode: value => parseFloat(value), + }, + }) + const target = { foo: "123" } + const source = await converter.reverse(target) + expect(source).toEqual({ foo: 123 }) + expect(target).toEqual({ foo: "123" }) + expect(await converter.apply(source)).toEqual(source) + }) }) diff --git a/Property/Converter/index.ts b/Property/Converter/index.ts index 6d8efd7..b97204e 100644 --- a/Property/Converter/index.ts +++ b/Property/Converter/index.ts @@ -27,7 +27,7 @@ export class Converter = Record