From 442be1332ee45319052504cb3f8cb87a63151e74 Mon Sep 17 00:00:00 2001 From: Kar Rui Lau Date: Mon, 7 Jun 2021 16:19:59 +0800 Subject: [PATCH] feat: add base typings for Agency and User in preparation for extending from to close #2066 --- src/public/services/AuthService.ts | 4 ++- src/public/services/UserService.ts | 22 +--------------- .../services/__tests__/UserService.test.ts | 3 ++- src/types/api/agency.ts | 14 +++++++++++ src/types/api/user.ts | 25 +++++++++++++++++++ 5 files changed, 45 insertions(+), 23 deletions(-) create mode 100644 src/types/api/agency.ts create mode 100644 src/types/api/user.ts diff --git a/src/public/services/AuthService.ts b/src/public/services/AuthService.ts index dd0bcd6ab9..a6a9ceed49 100644 --- a/src/public/services/AuthService.ts +++ b/src/public/services/AuthService.ts @@ -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' diff --git a/src/public/services/UserService.ts b/src/public/services/UserService.ts index a0914b6990..ac83342465 100644 --- a/src/public/services/UserService.ts +++ b/src/public/services/UserService.ts @@ -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 -type AgencyId = Opaque - -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. diff --git a/src/public/services/__tests__/UserService.test.ts b/src/public/services/__tests__/UserService.test.ts index f2f5a2ca6c..27817f9c8d 100644 --- a/src/public/services/__tests__/UserService.test.ts +++ b/src/public/services/__tests__/UserService.test.ts @@ -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', () => { diff --git a/src/types/api/agency.ts b/src/types/api/agency.ts new file mode 100644 index 0000000000..d65a809515 --- /dev/null +++ b/src/types/api/agency.ts @@ -0,0 +1,14 @@ +import { Opaque } from 'type-fest' +import { z } from 'zod' + +type AgencyId = Opaque + +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, +}) + +export type Agency = z.infer diff --git a/src/types/api/user.ts b/src/types/api/user.ts new file mode 100644 index 0000000000..25b589f30d --- /dev/null +++ b/src/types/api/user.ts @@ -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 +type UserContact = Opaque + +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, + email: z.string().email(), + agency: Agency, + created: DateString, + lastAccessed: DateString, + updatedAt: DateString, + contact: (z.string() as unknown as z.Schema).optional(), +}) + +export type User = z.infer