Skip to content

Commit 02bc35b

Browse files
committed
Implement output types for Article
1 parent e35359c commit 02bc35b

10 files changed

Lines changed: 354 additions & 216 deletions

File tree

apps/rpc/src/invariant.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,16 @@ export function parseOrReport<T extends z.ZodSchema>(schema: T, value: z.infer<T
1818
}
1919
return result.data
2020
}
21+
22+
export function parseOutputType<T extends z.ZodSchema>(schema: T, value: z.infer<T>): z.infer<T> {
23+
const result = schema.safeParse(value)
24+
if (!result.success) {
25+
logger.error(
26+
"Procedure handler failed to parse value into schema: %s emitted for object %o",
27+
result.error.message,
28+
value
29+
)
30+
throw new Error("Procedure handler returned value that does not conform to schema")
31+
}
32+
return result.data
33+
}

apps/rpc/src/modules/article/article-repository.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
import type { DBHandle } from "@dotkomonline/db"
22
import {
3-
type Article,
4-
type ArticleFilterQuery,
3+
Article,
54
type ArticleId,
6-
ArticleSchema,
75
type ArticleSlug,
8-
type ArticleTag,
6+
ArticleTag,
97
type ArticleTagName,
10-
ArticleTagSchema,
118
type ArticleWrite,
12-
} from "@dotkomonline/types"
9+
} from "./article-types"
1310
import { parseOrReport } from "../../invariant"
1411
import { type Pageable, pageQuery } from "../../query"
12+
import type { ArticleFilterQuery } from "./article-service"
1513

1614
export interface ArticleRepository {
1715
create(handle: DBHandle, data: ArticleWrite): Promise<Article>
@@ -122,7 +120,7 @@ export function getArticleRepository(): ArticleRepository {
122120
})
123121

124122
return tags.map((tag) =>
125-
parseOrReport(ArticleTagSchema, {
123+
parseOrReport(ArticleTag, {
126124
name: tag.tagName,
127125
})
128126
)
@@ -131,7 +129,7 @@ export function getArticleRepository(): ArticleRepository {
131129
}
132130

133131
function mapArticle(article: Omit<Article, "tags">, tags: { tag: ArticleTag }[]): Article {
134-
return parseOrReport(ArticleSchema, {
132+
return parseOrReport(Article, {
135133
...article,
136134
tags: tags.map((link) => link.tag),
137135
})

0 commit comments

Comments
 (0)