Creating a Class from a Transformed Struct in Effect Typescript
is there a way to create a class from a transformed Struct?
where this is the transformation
export class Agent extends S.Class<Agent>("Agent")(S.Struct({
id: S.String,
name: S.String,
description: S.String,
rarity: _Rarity,
market_hash_name: StringWithDefault,
collections: S.Array(S.partialWith(Collection, { exact: true })),
team: _Basic,
}).pipe(transformRarity)) {
surreal = () => ({ ...this, id: generateSlug(this.name) });
}export class Agent extends S.Class<Agent>("Agent")(S.Struct({
id: S.String,
name: S.String,
description: S.String,
rarity: _Rarity,
market_hash_name: StringWithDefault,
collections: S.Array(S.partialWith(Collection, { exact: true })),
team: _Basic,
}).pipe(transformRarity)) {
surreal = () => ({ ...this, id: generateSlug(this.name) });
}where this is the transformation
function transformRarity<T extends S.Struct.Fields>(struct: S.Struct<T>) {
return S.transform(
S.Struct({
...struct.fields,
rarity: S.String,
color: S.String,
}),
{
strict: true,
decode: (data) => ({
...data,
rarity: data.rarity.name,
color: data.rarity.color,
}),
encode: (data) => data,
},
)(struct);
}function transformRarity<T extends S.Struct.Fields>(struct: S.Struct<T>) {
return S.transform(
S.Struct({
...struct.fields,
rarity: S.String,
color: S.String,
}),
{
strict: true,
decode: (data) => ({
...data,
rarity: data.rarity.name,
color: data.rarity.color,
}),
encode: (data) => data,
},
)(struct);
}