|
1 | 1 | import { Type, Static } from "@sinclair/typebox";
|
2 | 2 |
|
3 |
| -const getQuestionsQuerySchema = Type.Object({ |
4 |
| - category: Type.Optional(Type.String()), |
5 |
| - status: Type.Optional(Type.String()), |
6 |
| - level: Type.Optional(Type.String({ pattern: "^(\\w+,?)+$" })), |
7 |
| - limit: Type.Optional(Type.Integer()), |
8 |
| - offset: Type.Optional(Type.Integer()), |
9 |
| - orderBy: Type.Optional( |
10 |
| - Type.Union([Type.Literal("acceptedAt"), Type.Literal("level"), Type.Literal("votesCount")]), |
11 |
| - ), |
12 |
| - order: Type.Optional(Type.Union([Type.Literal("asc"), Type.Literal("desc")])), |
13 |
| -}); |
14 |
| -export type GetQuestionsQuery = Static<typeof getQuestionsQuerySchema>; |
| 3 | +const generateGetQuestionsQuerySchema = < |
| 4 | + Categories extends readonly string[], |
| 5 | + Levels extends readonly string[], |
| 6 | + Statuses extends readonly string[], |
| 7 | +>(args: { |
| 8 | + categories: Categories; |
| 9 | + levels: Levels; |
| 10 | + statuses: Statuses; |
| 11 | +}) => |
| 12 | + Type.Object({ |
| 13 | + category: Type.Optional(Type.Union(args.categories.map((val) => Type.Literal(val)))), |
| 14 | + status: Type.Optional(Type.Union(args.statuses.map((val) => Type.Literal(val)))), |
| 15 | + level: Type.Optional(Type.String({ pattern: `^([${args.levels.join("|")}],?)+$` })), |
| 16 | + limit: Type.Optional(Type.Integer()), |
| 17 | + offset: Type.Optional(Type.Integer()), |
| 18 | + orderBy: Type.Optional( |
| 19 | + Type.Union([Type.Literal("acceptedAt"), Type.Literal("level"), Type.Literal("votesCount")]), |
| 20 | + ), |
| 21 | + order: Type.Optional(Type.Union([Type.Literal("asc"), Type.Literal("desc")])), |
| 22 | + }); |
| 23 | +export type GetQuestionsQuery = Static<ReturnType<typeof generateGetQuestionsQuerySchema>>; |
15 | 24 | export type GetQuestionsOrderBy = GetQuestionsQuery["orderBy"];
|
16 | 25 | export type GetQuestionsOrder = GetQuestionsQuery["order"];
|
17 | 26 |
|
18 |
| -const questionShape = { |
19 |
| - id: Type.Integer(), |
20 |
| - question: Type.String(), |
21 |
| - _categoryId: Type.String(), |
22 |
| - _levelId: Type.String(), |
23 |
| - _statusId: Type.String(), |
24 |
| - acceptedAt: Type.Optional(Type.String({ format: "date-time" })), |
25 |
| -} as const; |
| 27 | +const generateQuestionShape = < |
| 28 | + Categories extends readonly string[], |
| 29 | + Levels extends readonly string[], |
| 30 | + Statuses extends readonly string[], |
| 31 | +>(args: { |
| 32 | + categories: Categories; |
| 33 | + levels: Levels; |
| 34 | + statuses: Statuses; |
| 35 | +}) => { |
| 36 | + return { |
| 37 | + id: Type.Integer(), |
| 38 | + question: Type.String(), |
| 39 | + _categoryId: Type.Union(args.categories.map((val) => Type.Literal(val))), |
| 40 | + _levelId: Type.Union(args.levels.map((val) => Type.Literal(val))), |
| 41 | + _statusId: Type.Union(args.statuses.map((val) => Type.Literal(val))), |
| 42 | + acceptedAt: Type.Optional(Type.String({ format: "date-time" })), |
| 43 | + } as const; |
| 44 | +}; |
26 | 45 |
|
27 |
| -const createQuestionShape = { |
28 |
| - question: Type.String(), |
29 |
| - level: Type.String(), |
30 |
| - category: Type.String(), |
| 46 | +const generateCreateQuestionShape = < |
| 47 | + Categories extends readonly string[], |
| 48 | + Levels extends readonly string[], |
| 49 | + Statuses extends readonly string[], |
| 50 | +>(args: { |
| 51 | + categories: Categories; |
| 52 | + levels: Levels; |
| 53 | + statuses: Statuses; |
| 54 | +}) => { |
| 55 | + return { |
| 56 | + question: Type.String(), |
| 57 | + level: Type.Union(args.levels.map((val) => Type.Literal(val))), |
| 58 | + category: Type.Union(args.categories.map((val) => Type.Literal(val))), |
| 59 | + }; |
31 | 60 | };
|
32 | 61 |
|
33 |
| -const questionResponseSchema = Type.Object({ |
34 |
| - ...questionShape, |
35 |
| - votesCount: Type.Integer(), |
36 |
| - currentUserVotedOn: Type.Boolean(), |
37 |
| -}); |
| 62 | +const generateQuestionResponseSchema = < |
| 63 | + Categories extends readonly string[], |
| 64 | + Levels extends readonly string[], |
| 65 | + Statuses extends readonly string[], |
| 66 | +>(args: { |
| 67 | + categories: Categories; |
| 68 | + levels: Levels; |
| 69 | + statuses: Statuses; |
| 70 | +}) => |
| 71 | + Type.Object({ |
| 72 | + ...generateQuestionShape(args), |
| 73 | + votesCount: Type.Integer(), |
| 74 | + currentUserVotedOn: Type.Boolean(), |
| 75 | + }); |
38 | 76 |
|
39 |
| -export const getQuestionsSchema = { |
40 |
| - querystring: getQuestionsQuerySchema, |
41 |
| - response: { |
42 |
| - 200: Type.Object({ |
43 |
| - data: Type.Array(questionResponseSchema), |
44 |
| - meta: Type.Object({ |
45 |
| - total: Type.Integer(), |
| 77 | +export const generateGetQuestionsSchema = < |
| 78 | + Categories extends readonly string[], |
| 79 | + Levels extends readonly string[], |
| 80 | + Statuses extends readonly string[], |
| 81 | +>(args: { |
| 82 | + categories: Categories; |
| 83 | + levels: Levels; |
| 84 | + statuses: Statuses; |
| 85 | +}) => { |
| 86 | + return { |
| 87 | + querystring: generateGetQuestionsQuerySchema(args), |
| 88 | + response: { |
| 89 | + 200: Type.Object({ |
| 90 | + data: Type.Array(generateQuestionResponseSchema(args)), |
| 91 | + meta: Type.Object({ |
| 92 | + total: Type.Integer(), |
| 93 | + }), |
46 | 94 | }),
|
47 |
| - }), |
48 |
| - }, |
49 |
| -} as const; |
| 95 | + }, |
| 96 | + } as const; |
| 97 | +}; |
50 | 98 |
|
51 |
| -export const postQuestionsSchema = { |
52 |
| - body: Type.Object(createQuestionShape), |
53 |
| - response: { |
54 |
| - 200: Type.Object({ |
55 |
| - data: questionResponseSchema, |
56 |
| - }), |
57 |
| - }, |
58 |
| -} as const; |
| 99 | +export const generatePostQuestionsSchema = < |
| 100 | + Categories extends readonly string[], |
| 101 | + Levels extends readonly string[], |
| 102 | + Statuses extends readonly string[], |
| 103 | +>(args: { |
| 104 | + categories: Categories; |
| 105 | + levels: Levels; |
| 106 | + statuses: Statuses; |
| 107 | +}) => { |
| 108 | + return { |
| 109 | + body: Type.Object(generateCreateQuestionShape(args)), |
| 110 | + response: { |
| 111 | + 200: Type.Object({ |
| 112 | + data: generateQuestionResponseSchema(args), |
| 113 | + }), |
| 114 | + }, |
| 115 | + } as const; |
| 116 | +}; |
59 | 117 |
|
60 |
| -export const patchQuestionByIdSchema = { |
61 |
| - params: Type.Object({ |
62 |
| - id: Type.Integer(), |
63 |
| - }), |
64 |
| - body: Type.Object({ ...createQuestionShape, status: Type.String() }), |
65 |
| - response: { |
66 |
| - 200: Type.Object({ |
67 |
| - data: questionResponseSchema, |
| 118 | +export const generatePatchQuestionByIdSchema = < |
| 119 | + Categories extends readonly string[], |
| 120 | + Levels extends readonly string[], |
| 121 | + Statuses extends readonly string[], |
| 122 | +>(args: { |
| 123 | + categories: Categories; |
| 124 | + levels: Levels; |
| 125 | + statuses: Statuses; |
| 126 | +}) => { |
| 127 | + return { |
| 128 | + params: Type.Object({ |
| 129 | + id: Type.Integer(), |
68 | 130 | }),
|
69 |
| - }, |
| 131 | + body: Type.Object({ |
| 132 | + ...generateCreateQuestionShape(args), |
| 133 | + status: Type.Union(args.statuses.map((val) => Type.Literal(val))), |
| 134 | + }), |
| 135 | + response: { |
| 136 | + 200: Type.Object({ |
| 137 | + data: generateQuestionResponseSchema(args), |
| 138 | + }), |
| 139 | + }, |
| 140 | + }; |
70 | 141 | };
|
71 | 142 |
|
72 |
| -export const getQuestionByIdSchema = { |
73 |
| - params: Type.Object({ |
74 |
| - id: Type.Integer(), |
75 |
| - }), |
76 |
| - response: { |
77 |
| - 200: Type.Object({ |
78 |
| - data: questionResponseSchema, |
| 143 | +export const generateGetQuestionByIdSchema = < |
| 144 | + Categories extends readonly string[], |
| 145 | + Levels extends readonly string[], |
| 146 | + Statuses extends readonly string[], |
| 147 | +>(args: { |
| 148 | + categories: Categories; |
| 149 | + levels: Levels; |
| 150 | + statuses: Statuses; |
| 151 | +}) => { |
| 152 | + return { |
| 153 | + params: Type.Object({ |
| 154 | + id: Type.Integer(), |
79 | 155 | }),
|
80 |
| - }, |
| 156 | + response: { |
| 157 | + 200: Type.Object({ |
| 158 | + data: generateQuestionResponseSchema(args), |
| 159 | + }), |
| 160 | + }, |
| 161 | + }; |
81 | 162 | };
|
82 | 163 |
|
83 | 164 | export const deleteQuestionByIdSchema = {
|
|
0 commit comments