forked from effect-app/boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUser.ts
More file actions
101 lines (84 loc) · 2.79 KB
/
User.ts
File metadata and controls
101 lines (84 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/* eslint-disable @typescript-eslint/unbound-method */
import * as A from "@effect/schema/Arbitrary"
import { type Schema } from "effect-app"
import { Context, Effect, Equivalence, pipe, S } from "effect-app"
import { fakerArb } from "effect-app/faker"
import { UserProfileId } from "effect-app/ids"
export const FirstName = S
.NonEmptyString255
.pipe(
S.annotations({
[A.ArbitraryHookId]: (): A.LazyArbitrary<string> => (fc) => fc.string()
}),
S.withDefaultMake
)
export type FirstName = Schema.Type<typeof FirstName>
export const DisplayName = FirstName
export type DisplayName = Schema.Type<typeof DisplayName>
S.Array(S.NonEmptyString255).pipe(
S.annotations({ [A.ArbitraryHookId]: (): A.LazyArbitrary<Array<string>> => (fc) => fc.tuple() })
)
export const LastName = S
.NonEmptyString255
.pipe(
S.annotations({
[A.ArbitraryHookId]: (): A.LazyArbitrary<string> => fakerArb((faker) => faker.person.lastName)
}),
S.withDefaultMake
)
export type LastName = Schema.Type<typeof LastName>
export class FullName extends S.ExtendedClass<FullName, FullName.Encoded>()({
firstName: FirstName,
lastName: LastName
}) {
static render(this: void, fn: FullName) {
return S.NonEmptyString2k(`${fn.firstName} ${fn.lastName}`)
}
static create(this: void, firstName: FirstName, lastName: LastName) {
return new FullName({ firstName, lastName })
}
}
export function showFullName(fn: FullName) {
return FullName.render(fn)
}
export function createFullName(firstName: string, lastName: string) {
return { firstName, lastName }
}
export const UserId = UserProfileId
export type UserId = UserProfileId
export const Role = S.withDefaultMake(S.Literal("manager", "user"))
export type Role = Schema.Type<typeof Role>
export class UserFromIdResolver
extends Context.TagId("UserFromId")<UserFromIdResolver, { get: (userId: UserId) => Effect<User> }>()
{}
export class User extends S.ExtendedClass<User, User.Encoded>()({
id: UserId.withDefault,
name: FullName,
email: S.Email,
role: Role,
passwordHash: S.NonEmptyString255
}) {
get displayName() {
return S.NonEmptyString2k(this.name.firstName + " " + this.name.lastName)
}
static readonly resolver = UserFromIdResolver
}
export const UserFromId: Schema<User, string, UserFromIdResolver> = S.transformOrFail(
UserId,
S.typeSchema(User),
{ decode: User.resolver.get, encode: (u) => Effect.succeed(u.id) }
)
export const defaultEqual = pipe(Equivalence.string, Equivalence.mapInput((u: User) => u.id))
// codegen:start {preset: model}
//
/* eslint-disable */
export namespace FullName {
export interface Encoded extends S.Struct.Encoded<typeof FullName["fields"]> {}
}
export namespace User {
export interface Encoded extends S.Struct.Encoded<typeof User["fields"]> {}
}
/* eslint-enable */
//
// codegen:end
//