Skip to content

Instantly share code, notes, and snippets.

View Eosis's full-sized avatar
🎯

Rupert Eosis

🎯
View GitHub Profile
@Eosis
Eosis / BaseMeetingCodec.ts
Last active January 13, 2022 17:36
Example Types For the Meeting App.
import * as T from 'io-ts'
const BaseMeetingCodecT
= T.type({
meetingAgenda: T.string,
})
export default BaseMeetingCodecT
/shared/codecs/BaseMeetingCodec.ts
/backend/codecs/JustStartTimeCodec.ts
/backend/codecs/MeetingCodec.ts
/client/codecs/JustStartTimeCodec.ts
/client/codecs/MeetingCodec.ts
import * as T from 'io-ts'
const BaseMeetingCodecT
= T.type({
meetingAgenda: T.string,
})
export default BaseMeetingCodecT
// Backend TimestampCodec
import * as T from 'io-ts'
import { DateTime } from 'luxon'
import * as admin from 'firebase-admin'
export const LuxonDateTimeT = new T.Type<DateTime, admin.firestore.Timestamp, unknown>(
'LuxonDateTimeT',
(u: unknown): u is DateTime => u instanceof DateTime,
(u: unknown, c: T.Context) => {
if (!(u instanceof admin.firestore.Timestamp)) {
// Client LuxonTimestampCodec:
import * as T from 'io-ts'
import { DateTime } from 'luxon'
import { Timestamp } from 'firebase/firestore'
const LuxonDateTimeT = new T.Type<DateTime, Timestamp, unknown>(
'LuxonDateTimeT',
(u: unknown): u is DateTime => u instanceof DateTime,
(u: unknown, c: T.Context) => {
if (!(u instanceof Timestamp)) {
import * as T from 'io-ts'
import BaseMeetingCodecT from '../../shared/codecs/BaseMeetingCodec'
import JustStartTimeT from './JustStartTimeCodec'
const MeetingT = T.intersection([BaseCodecT, JustStartTimeT])
export default MeetingT
// Collection at /users/rzJZAyMbXaeXQ9OWeIckM88DUcq1
{
"name": "Rupert", // I'm happy for people to see this
"email": "me@example.com", // I'd rather they didn't see this
"birthday": "1970-01-01" // I'd rather they didn't see this
}
const userDoc = await getDoc(doc(db, '/users/', uid))
const name = userDoc.data().name
const superSecretEmail = userDoc.data().email. // 😱
const superSecretBirthday = userDoc.data().birthday // 😱
// Collection at /users/rzJZAyMbXaeXQ9OWeIckM88DUcq1
{
"name": "Rupert", // I'm happy for people to see this
}
// Collection at /users/rzJZAyMbXaeXQ9OWeIckM88DUcq1/private/info
{
"email": "me@example.com", // I can be hidden now!
"birthday": "1970-01-01" // I can be hidden now!
}