api/src/schemas/index.ts

297 lines · 7910 bytes

23 symbols in this file
  1// Zod schemas plus inferred types, single source of truth
  2import { z } from '@hono/zod-openapi'
  3
  4export const ServiceSchema = z
  5  .object({
  6    name: z.string(),
  7    url: z.string(),
  8    description: z.string().optional(),
  9    icon: z.string().optional(),
 10    github: z.string().optional(),
 11    followWith: z.string().optional(),
 12    securityAudited: z.boolean().optional(),
 13    openSource: z.boolean().optional(),
 14    acceptsCrypto: z.boolean().optional(),
 15    tosdrId: z.union([z.number(), z.string()]).optional(),
 16    androidApp: z.string().optional(),
 17    iosApp: z.string().optional(),
 18    subreddit: z.string().optional(),
 19    discordInvite: z.string().optional(),
 20  })
 21  .openapi('Service')
 22
 23export const FlatServiceSchema = ServiceSchema.extend({
 24  slug: z.string(),
 25  category: z.string(),
 26  categorySlug: z.string(),
 27  section: z.string(),
 28  sectionSlug: z.string(),
 29}).openapi('FlatService')
 30
 31export const SectionSchema = z
 32  .object({
 33    name: z.string(),
 34    slug: z.string(),
 35    intro: z.string().optional(),
 36    furtherInfo: z.string().optional(),
 37    wordOfWarning: z.string().optional(),
 38    alternativeTo: z.array(z.string()).optional(),
 39    services: z.array(ServiceSchema),
 40  })
 41  .openapi('Section')
 42
 43export const CategorySchema = z
 44  .object({
 45    name: z.string(),
 46    slug: z.string(),
 47    sections: z.array(z.object({ name: z.string(), slug: z.string() })),
 48  })
 49  .openapi('Category')
 50
 51export const PaginationSchema = z
 52  .object({
 53    page: z.number(),
 54    limit: z.number(),
 55    total: z.number(),
 56    hasMore: z.boolean(),
 57  })
 58  .openapi('Pagination')
 59
 60export const ErrorSchema = z
 61  .object({
 62    error: z.object({
 63      code: z.enum([
 64        'NOT_FOUND',
 65        'BAD_REQUEST',
 66        'UNAUTHORIZED',
 67        'RATE_LIMITED',
 68        'UPSTREAM_ERROR',
 69        'INTERNAL',
 70      ]),
 71      message: z.string(),
 72      status: z.number(),
 73    }),
 74  })
 75  .openapi('Error')
 76
 77export const Envelope = <T extends z.ZodTypeAny>(data: T) => z.object({ data })
 78
 79export const ListEnvelope = <T extends z.ZodTypeAny>(item: T) =>
 80  z.object({
 81    data: z.array(item),
 82    pagination: PaginationSchema,
 83  })
 84
 85export const StatsSchema = z
 86  .object({
 87    categories: z.number(),
 88    sections: z.number(),
 89    services: z.number(),
 90    openSource: z.number(),
 91    securityAudited: z.number(),
 92    acceptsCrypto: z.number(),
 93  })
 94  .openapi('Stats')
 95
 96export const HealthSchema = z
 97  .object({
 98    status: z.literal('ok'),
 99    uptime: z.number(),
100  })
101  .openapi('Health')
102
103export const SearchHitSchema = z
104  .object({
105    slug: z.string(),
106    name: z.string(),
107    url: z.string(),
108    description: z.string().optional(),
109    category: z.string(),
110    categorySlug: z.string(),
111    section: z.string(),
112    sectionSlug: z.string(),
113    score: z.number(),
114  })
115  .openapi('SearchHit')
116
117// 200 response cell builder, keeps routes free of repetitive content wiring
118export const Ok = (schema: z.ZodTypeAny) => ({
119  description: 'OK',
120  content: { 'application/json': { schema } },
121})
122
123// Shared error response, reused across every route that declares errors
124export const ErrorResponse = {
125  description: 'Error',
126  content: { 'application/json': { schema: ErrorSchema } },
127}
128
129// Enrichment passthroughs, upstream shapes are wide so we don't enumerate every field
130export const TosdrServiceSchema = z
131  .object({})
132  .openapi('TosdrService', { description: 'ToS;DR v3 service record, passthrough' })
133
134export const ItunesAppSchema = z
135  .object({})
136  .openapi('ItunesApp', { description: 'iTunes lookup result, passthrough' })
137
138export const AndroidReportSchema = z.object({}).openapi('AndroidReport', {
139  description:
140    'Exodus Privacy trackers merged with Plexus de-Googled compatibility (degoogled)',
141})
142
143export const WebsiteReportSchema = z
144  .object({})
145  .openapi('WebsiteReport', { description: 'APIVoid url reputation report, passthrough' })
146
147// Third-party trackers found on a web page, via DuckDuckGo's Tracker Blocklist
148export const TrackersReportSchema = z
149  .object({
150    finalUrl: z.string(),
151    trackers: z.array(
152      z.object({
153        domain: z.string(),
154        owner: z.string(),
155        categories: z.array(z.string()),
156      }),
157    ),
158  })
159  .openapi('TrackersReport')
160
161export const DockerInfoSchema = z.object({}).openapi('DockerInfo', {
162  description: 'Portainer template, Docker Hub metrics and generated run/compose usage',
163})
164
165// GitHub repo summary, shape is fixed by the mapper functions
166export const GithubRepoSchema = z
167  .object({
168    info: z.object({
169      ownerUsername: z.string(),
170      ownerAvatar: z.string(),
171      description: z.string(),
172      url: z.string(),
173      homepage: z.string(),
174      language: z.string(),
175      topics: z.array(z.string()),
176      license: z.string(),
177      licenseName: z.string(),
178      isFork: z.boolean(),
179      isArchived: z.boolean(),
180      forkParent: z.string(),
181      createdAt: z.string(),
182      updatedAt: z.string(),
183      pushedAt: z.string(),
184      size: z.number(),
185      starCount: z.number(),
186      forksCount: z.number(),
187      watchersCount: z.number(),
188      openIssues: z.number(),
189    }),
190    languages: z.record(z.number()),
191    versions: z.array(
192      z.object({
193        name: z.string(),
194        commit: z.string(),
195        zipball: z.string(),
196        tarball: z.string(),
197      }),
198    ),
199    contributors: z.array(
200      z.object({
201        username: z.string(),
202        avatar: z.string(),
203        url: z.string(),
204        contributions: z.number(),
205      }),
206    ),
207    commits: z.array(
208      z.object({
209        sha: z.string(),
210        authorName: z.string(),
211        authorDate: z.string(),
212        message: z.string(),
213        authorUsername: z.string(),
214        authorAvatar: z.string(),
215      }),
216    ),
217  })
218  .openapi('GithubRepo')
219
220// Repo security report, merges deps.dev scorecard with GitHub advisories
221export const SecurityReportSchema = z
222  .object({
223    repo: z.string(),
224    scorecard: z.object({
225      available: z.boolean(),
226      overallScore: z.number().nullable(),
227      generatedAt: z.string(),
228      checks: z.array(
229        z.object({
230          name: z.string(),
231          score: z.number(),
232          reason: z.string(),
233          url: z.string(),
234        }),
235      ),
236    }),
237    advisories: z.object({
238      count: z.number(),
239      items: z.array(
240        z.object({
241          ghsaId: z.string(),
242          cveId: z.string().nullable(),
243          summary: z.string(),
244          severity: z.string(),
245          cvssScore: z.number().nullable(),
246          publishedAt: z.string(),
247          url: z.string(),
248          firstPatchedVersion: z.string().nullable(),
249          vulnerableRange: z.string(),
250          isPatched: z.boolean(),
251        }),
252      ),
253    }),
254    fuzzed: z.boolean(),
255  })
256  .openapi('SecurityReport')
257
258export const DiscordInviteSchema = z
259  .object({
260    inviteCode: z.string().nullable(),
261    name: z.string().nullable(),
262    memberCount: z.number().nullable(),
263    memberOnlineCount: z.number().nullable(),
264    channel: z.string().nullable(),
265    icon: z.string().nullable(),
266    banner: z.string().nullable(),
267    inviter: z.string().nullable(),
268  })
269  .openapi('DiscordInvite')
270
271export const SubredditSchema = z
272  .object({
273    info: z.object({
274      name: z.string().nullable(),
275      title: z.string().nullable(),
276      description: z.string().nullable(),
277      longDescription: z.string().nullable(),
278      icon: z.string().nullable(),
279      banner: z.string().nullable(),
280      color: z.string().nullable(),
281      subscribers: z.number().nullable(),
282      activeSubscribers: z.number().nullable(),
283      dateCreated: z.number().nullable(),
284      descriptionHtml: z.string().nullable(),
285    }),
286    posts: z.array(
287      z.object({
288        title: z.string(),
289        body: z.string(),
290        upVotes: z.number(),
291        downVotes: z.number(),
292        date: z.number(),
293        url: z.string(),
294      }),
295    ),
296  })
297  .openapi('Subreddit')