Last active
September 18, 2021 20:57
-
-
Save itsMapleLeaf/1ecf5bef32b1bbcfaf5cd7b0ff51e8b6 to your computer and use it in GitHub Desktop.
guard pattern
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
get: async (context) => { | |
const user = await createSessionHelpers(context).getUser() | |
if (!user) { | |
return redirect("/login") | |
} | |
const id = context.params?.bucketId | |
if (!id) { | |
return notFound() | |
} | |
const bucket = await db.bucket.findUnique({ | |
where: { | |
id: String(id), | |
}, | |
select: { | |
id: true, | |
name: true, | |
createdAt: true, | |
}, | |
}) | |
if (!bucket) { | |
return notFound() | |
} | |
const columns = await db.column.findMany({ | |
where: { | |
bucketId: String(id), | |
}, | |
select: { | |
id: true, | |
name: true, | |
}, | |
}) | |
return json({ | |
user: pick(user, ["name"]), | |
bucket: serialize(bucket), | |
columns, | |
}) | |
}, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
get: async (context) => { | |
return usingSessionUser(context, (user) => { | |
return usingContextParam(context, "bucketId", async (bucketId) => { | |
return usingBucket(bucketId, async (bucket) => { | |
return usingBucketColumns(bucketId, (columns) => { | |
return json({ | |
user: pick(user, ["name"]), | |
bucket: serialize(bucket), | |
columns, | |
}) | |
}) | |
}) | |
}) | |
}) | |
}, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function usingSessionUser<Result>( | |
context: RuntimeContext<ParsedUrlQuery>, | |
fn: (user: User) => Result | Promise<Result>, | |
) { | |
const user = await createSessionHelpers(context).getUser() | |
return user ? fn(user) : redirect<never>("/login", 303) | |
} | |
function usingContextParam( | |
context: RuntimeContext<ParsedUrlQuery>, | |
name: string, | |
fn: (value: string) => void, | |
) { | |
const value = context.query[name] | |
return typeof value === "string" ? fn(value) : notFound() | |
} | |
async function usingBucket(bucketId: string, fn: (bucket: Bucket) => void) { | |
const bucket = await db.bucket.findUnique({ where: { id: bucketId } }) | |
return bucket ? fn(bucket) : notFound() | |
} | |
async function usingBucketColumns( | |
bucketId: string, | |
fn: (columns: Array<Pick<Column, "name" | "id">>) => void, | |
) { | |
const columns = await db.column.findMany({ | |
where: { bucketId }, | |
select: { | |
id: true, | |
name: true, | |
}, | |
}) | |
return fn(columns) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment