Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions Property/Converter/Configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,9 @@ type GetValue<P extends string, Source extends Record<string, any>> = P extends
type MaybePromise<T> = T | Promise<T> | Partial<number>
export type Configuration<S extends Record<string, unknown> = Record<string, unknown>, T extends Payload = Payload> = {
[P in DotNotation<S>]?: {
encode: (
value: GetValue<P, S>
) => MaybePromise<(P extends DotNotation<T> ? GetValue<P, T> : Payload.Value) | undefined>
decode: (
value: (P extends DotNotation<T> ? GetValue<P, T> : Payload.Value) | undefined
encode?: (value: GetValue<P, S>) => MaybePromise<P extends DotNotation<T> ? GetValue<P, T> : Payload.Value>
decode?: (
value: P extends DotNotation<T> ? GetValue<P, T> : Payload.Value
) => MaybePromise<GetValue<P, S>> | undefined
}
}
Expand All @@ -40,9 +38,9 @@ export namespace Configuration {
isly.record<Configuration>(
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()
)
Expand Down
34 changes: 30 additions & 4 deletions Property/Converter/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ const target = {
const conversionMap: authly.Property.Converter.Configuration<typeof source, typeof target> = {
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",
Expand All @@ -40,7 +40,7 @@ const conversionMap: authly.Property.Converter.Configuration<typeof source, type
},
issued: {
encode: value => isoly.DateTime.epoch(value, "seconds"),
decode: value => isoly.DateTime.create(value ?? 0),
decode: value => isoly.DateTime.create(value),
},
}

Expand All @@ -49,6 +49,9 @@ const converter = new authly.Property.Converter<typeof source, typeof target>(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({})
Expand Down Expand Up @@ -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)
})
})
2 changes: 1 addition & 1 deletion Property/Converter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class Converter<S extends Record<string, unknown> = Record<string, unknow
if (result[property[0]] != undefined)
result[property[0]] =
property.length == 1
? await mapping(result[property[0]] as Payload)
? (await mapping?.(result[property[0]] as Payload)) ?? result[property[0]]
: await this.convertProperty(result[property[0]] as Payload, property.slice(1), mapping)
return result
}
Expand Down
Loading