Skip to content

Commit

Permalink
Add email identity helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
JoelBCarter committed Jan 1, 2025
1 parent 9c3890b commit 6afd3ef
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 3 deletions.
1 change: 1 addition & 0 deletions packages/payload/packages/email/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"types": "dist/neutral/index.d.ts",
"dependencies": {
"@xylabs/assert": "^4.4.32",
"@xylabs/object": "^4.4.34",
"@xyo-network/payload-model": "^3.6.6"
},
"devDependencies": {
Expand Down
11 changes: 8 additions & 3 deletions packages/payload/packages/email/src/Payload.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { assertEx } from '@xylabs/assert'
import { AsObjectFactory } from '@xylabs/object'
import type {
PayloadWithOptionalSources,
WithSources,
Expand Down Expand Up @@ -41,7 +42,7 @@ export type Email = PayloadWithOptionalSources<EmailFields, EmailAddressSchema>
* @param address The Email address to use
* @returns An Email if the address is valid, undefined otherwise
*/
export const tryAsEmail = <T = Email | WithSources<Email>>(address: string): T | undefined => {
export const tryToEmail = <T = Email | WithSources<Email>>(address: string): T | undefined => {
return isValidEmail(address) ? ({ address, schema: EmailAddressSchema } as T) : undefined
}

Expand All @@ -50,8 +51,8 @@ export const tryAsEmail = <T = Email | WithSources<Email>>(address: string): T |
* @param address The Email address to use
* @returns An Email if the address is valid, throws otherwise
*/
export const asEmail = <T = Email | WithSources<Email>>(address: string): T => {
return assertEx(tryAsEmail<T>(address), 'Invalid email address')
export const toEmail = <T = Email | WithSources<Email>>(address: string): T => {
return assertEx(tryToEmail<T>(address), () => 'Invalid email address')
}

/**
Expand All @@ -60,10 +61,14 @@ export const asEmail = <T = Email | WithSources<Email>>(address: string): T => {
export const isEmail = (value: unknown): value is Email => {
return isPayloadOfSchemaType<Email>(EmailAddressSchema)(value) && isValidEmail(value.address)
}
export const asEmail = AsObjectFactory.create<Email>(isEmail)
export const asOptionalEmail = AsObjectFactory.createOptional<Email>(isEmail)

/**
* Identity function for determining if an object is an Email with sources
*/
export const isEmailWithSources = (value: unknown): value is Email => {
return isPayloadOfSchemaTypeWithSources<Email>(EmailAddressSchema)(value) && isValidEmail(value.address)
}
export const asEmailWithSources = AsObjectFactory.create<Email>(isEmailWithSources)
export const asOptionalEmailWithSources = AsObjectFactory.createOptional<Email>(isEmailWithSources)
52 changes: 52 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4395,6 +4395,13 @@ __metadata:
languageName: node
linkType: hard

"@xylabs/assert@npm:^4.4.34":
version: 4.4.34
resolution: "@xylabs/assert@npm:4.4.34"
checksum: 10/404a7198d58cdfe70da56019c3458e68a235596971901647330ffe381883de8aaf16f592aac479046c6ec2dc0612bc3534daa11ed08973ead5c821b712cec959
languageName: node
linkType: hard

"@xylabs/axios@npm:^4.4.27, @xylabs/axios@npm:^4.4.32":
version: 4.4.32
resolution: "@xylabs/axios@npm:4.4.32"
Expand Down Expand Up @@ -4432,6 +4439,13 @@ __metadata:
languageName: node
linkType: hard

"@xylabs/error@npm:^4.4.34":
version: 4.4.34
resolution: "@xylabs/error@npm:4.4.34"
checksum: 10/a64ce4f99a095d7abe438bf90faf9c5e51895322865d7af332a5b5c7dd2e10018e6b7cfe30793e1496b5b4f16fa4bb51612c06e530014daf3c9f3be8afaa9379
languageName: node
linkType: hard

"@xylabs/eslint-config-flat@npm:^4.2.6":
version: 4.2.6
resolution: "@xylabs/eslint-config-flat@npm:4.2.6"
Expand Down Expand Up @@ -4496,6 +4510,15 @@ __metadata:
languageName: node
linkType: hard

"@xylabs/logger@npm:^4.4.34":
version: 4.4.34
resolution: "@xylabs/logger@npm:4.4.34"
dependencies:
"@xylabs/error": "npm:^4.4.34"
checksum: 10/8e65da2b92b74c2ab94beda76d440b246110e7dcd85a4bc1db4eb11877e58950dd033db076c0f394017a8a602a32c17c1b9eede8ba231fef361e91f158817e45
languageName: node
linkType: hard

"@xylabs/object@npm:^4.4.27, @xylabs/object@npm:^4.4.32":
version: 4.4.32
resolution: "@xylabs/object@npm:4.4.32"
Expand All @@ -4508,6 +4531,18 @@ __metadata:
languageName: node
linkType: hard

"@xylabs/object@npm:^4.4.34":
version: 4.4.34
resolution: "@xylabs/object@npm:4.4.34"
dependencies:
"@xylabs/assert": "npm:^4.4.34"
"@xylabs/logger": "npm:^4.4.34"
"@xylabs/promise": "npm:^4.4.34"
"@xylabs/typeof": "npm:^4.4.34"
checksum: 10/abc4357dc23a53650ecf911d20a7c82302145f65b16b15bd15c3a6c7f288eecf14495a5c01738e8f7e453adb41ecfed42e24ed1b3a5138cff23edc33bee446c0
languageName: node
linkType: hard

"@xylabs/platform@npm:^4.4.27, @xylabs/platform@npm:^4.4.32":
version: 4.4.32
resolution: "@xylabs/platform@npm:4.4.32"
Expand All @@ -4531,6 +4566,15 @@ __metadata:
languageName: node
linkType: hard

"@xylabs/promise@npm:^4.4.34":
version: 4.4.34
resolution: "@xylabs/promise@npm:4.4.34"
dependencies:
"@xylabs/typeof": "npm:^4.4.34"
checksum: 10/3849c5b10af5c726106da516a98f0bb862aabfce767cf60b43352cc3134a35c361f411abc9a9bb52ae8f18ebce0d7b8eea12c604b77b7b061443853be00128fe
languageName: node
linkType: hard

"@xylabs/retry@npm:^4.4.27":
version: 4.4.32
resolution: "@xylabs/retry@npm:4.4.32"
Expand Down Expand Up @@ -4722,6 +4766,13 @@ __metadata:
languageName: node
linkType: hard

"@xylabs/typeof@npm:^4.4.34":
version: 4.4.34
resolution: "@xylabs/typeof@npm:4.4.34"
checksum: 10/64b46733b9d97150e66ca07a63f42158f67628e62b8ac4bd1ea9e39bdd0a993a7948ef6be1ffb5a4fc03ab2c861a56ccfac2eda8ab5ebde1e4210081a8284a0c
languageName: node
linkType: hard

"@xylabs/url@npm:^4.4.32":
version: 4.4.32
resolution: "@xylabs/url@npm:4.4.32"
Expand Down Expand Up @@ -6354,6 +6405,7 @@ __metadata:
resolution: "@xyo-network/email-payload-plugin@workspace:packages/payload/packages/email"
dependencies:
"@xylabs/assert": "npm:^4.4.32"
"@xylabs/object": "npm:^4.4.34"
"@xylabs/ts-scripts-yarn3": "npm:^4.2.6"
"@xylabs/tsconfig": "npm:^4.2.6"
"@xylabs/vitest-extended": "npm:^4.4.32"
Expand Down

0 comments on commit 6afd3ef

Please sign in to comment.