Skip to content

Commit

Permalink
feat: add base typings for Agency and User
Browse files Browse the repository at this point in the history
in preparation for extending from to close #2066
  • Loading branch information
karrui committed Jun 9, 2021
1 parent fe4f5aa commit 442be13
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 23 deletions.
4 changes: 3 additions & 1 deletion src/public/services/AuthService.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import axios from 'axios'
import { Opaque } from 'type-fest'

import { saveUserToLocalStorage, User } from './UserService'
import { User } from '../../types/api/user'

import { saveUserToLocalStorage } from './UserService'

// Exported for testing.
export const AUTH_ENDPOINT = '/api/v3/auth'
Expand Down
22 changes: 1 addition & 21 deletions src/public/services/UserService.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,8 @@
import { Opaque } from 'type-fest'
import { User } from '../../types/api/user'

/** Exported for testing */
export const STORAGE_USER_KEY = 'user'

type UserId = Opaque<string, 'UserId'>
type AgencyId = Opaque<string, 'AgencyId'>

export type Agency = {
emailDomain: string[]
fullName: string
shortName: string
logo: string
_id: AgencyId
}

export type User = {
_id: UserId
email: string
agency: Agency
created: string
lastAccessed: string
updatedAt: string
}

/**
* Save logged in user to localStorage.
* May not be needed in React depending on implementation.
Expand Down
3 changes: 2 additions & 1 deletion src/public/services/__tests__/UserService.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { saveUserToLocalStorage, STORAGE_USER_KEY, User } from '../UserService'
import { User } from '../../../types/api/user'
import { saveUserToLocalStorage, STORAGE_USER_KEY } from '../UserService'

describe('UserService', () => {
describe('saveUserToLocalStorage', () => {
Expand Down
14 changes: 14 additions & 0 deletions src/types/api/agency.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Opaque } from 'type-fest'
import { z } from 'zod'

type AgencyId = Opaque<string, 'AgencyId'>

export const Agency = z.object({
emailDomain: z.array(z.string()),
fullName: z.string(),
shortName: z.string(),
logo: z.string(),
_id: z.string() as unknown as z.Schema<AgencyId>,
})

export type Agency = z.infer<typeof Agency>
25 changes: 25 additions & 0 deletions src/types/api/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { isDate, parseISO } from 'date-fns'
import { Opaque } from 'type-fest'
import { z } from 'zod'

import { Agency } from './agency'

type UserId = Opaque<string, 'UserId'>
type UserContact = Opaque<string, 'UserContact'>

const DateString = z.string().refine(
(val) => isDate(parseISO(val)),
(val) => ({ message: `${val} is not a valid date` }),
)

export const User = z.object({
_id: z.string() as unknown as z.Schema<UserId>,
email: z.string().email(),
agency: Agency,
created: DateString,
lastAccessed: DateString,
updatedAt: DateString,
contact: (z.string() as unknown as z.Schema<UserContact>).optional(),
})

export type User = z.infer<typeof User>

0 comments on commit 442be13

Please sign in to comment.