api/src/schemas/index.ts

283 lines · 7566 bytes

  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
147export const DockerInfoSchema = z.object({}).openapi('DockerInfo', {
148  description: 'Portainer template, Docker Hub metrics and generated run/compose usage',
149})
150
151// GitHub repo summary, shape is fixed by the mapper functions
152export const GithubRepoSchema = z
153  .object({
154    info: z.object({
155      ownerUsername: z.string(),
156      ownerAvatar: z.string(),
157      description: z.string(),
158      url: z.string(),
159      homepage: z.string(),
160      language: z.string(),
161      topics: z.array(z.string()),
162      license: z.string(),
163      licenseName: z.string(),
164      isFork: z.boolean(),
165      isArchived: z.boolean(),
166      forkParent: z.string(),
167      createdAt: z.string(),
168      updatedAt: z.string(),
169      pushedAt: z.string(),
170      size: z.number(),
171      starCount: z.number(),
172      forksCount: z.number(),
173      watchersCount: z.number(),
174      openIssues: z.number(),
175    }),
176    languages: z.record(z.number()),
177    versions: z.array(
178      z.object({
179        name: z.string(),
180        commit: z.string(),
181        zipball: z.string(),
182        tarball: z.string(),
183      }),
184    ),
185    contributors: z.array(
186      z.object({
187        username: z.string(),
188        avatar: z.string(),
189        url: z.string(),
190        contributions: z.number(),
191      }),
192    ),
193    commits: z.array(
194      z.object({
195        sha: z.string(),
196        authorName: z.string(),
197        authorDate: z.string(),
198        message: z.string(),
199        authorUsername: z.string(),
200        authorAvatar: z.string(),
201      }),
202    ),
203  })
204  .openapi('GithubRepo')
205
206// Repo security report, merges deps.dev scorecard with GitHub advisories
207export const SecurityReportSchema = z
208  .object({
209    repo: z.string(),
210    scorecard: z.object({
211      available: z.boolean(),
212      overallScore: z.number().nullable(),
213      generatedAt: z.string(),
214      checks: z.array(
215        z.object({
216          name: z.string(),
217          score: z.number(),
218          reason: z.string(),
219          url: z.string(),
220        }),
221      ),
222    }),
223    advisories: z.object({
224      count: z.number(),
225      items: z.array(
226        z.object({
227          ghsaId: z.string(),
228          cveId: z.string().nullable(),
229          summary: z.string(),
230          severity: z.string(),
231          cvssScore: z.number().nullable(),
232          publishedAt: z.string(),
233          url: z.string(),
234          firstPatchedVersion: z.string().nullable(),
235          vulnerableRange: z.string(),
236          isPatched: z.boolean(),
237        }),
238      ),
239    }),
240    fuzzed: z.boolean(),
241  })
242  .openapi('SecurityReport')
243
244export const DiscordInviteSchema = z
245  .object({
246    inviteCode: z.string().nullable(),
247    name: z.string().nullable(),
248    memberCount: z.number().nullable(),
249    memberOnlineCount: z.number().nullable(),
250    channel: z.string().nullable(),
251    icon: z.string().nullable(),
252    banner: z.string().nullable(),
253    inviter: z.string().nullable(),
254  })
255  .openapi('DiscordInvite')
256
257export const SubredditSchema = z
258  .object({
259    info: z.object({
260      name: z.string().nullable(),
261      title: z.string().nullable(),
262      description: z.string().nullable(),
263      longDescription: z.string().nullable(),
264      icon: z.string().nullable(),
265      banner: z.string().nullable(),
266      color: z.string().nullable(),
267      subscribers: z.number().nullable(),
268      activeSubscribers: z.number().nullable(),
269      dateCreated: z.number().nullable(),
270      descriptionHtml: z.string().nullable(),
271    }),
272    posts: z.array(
273      z.object({
274        title: z.string(),
275        body: z.string(),
276        upVotes: z.number(),
277        downVotes: z.number(),
278        date: z.number(),
279        url: z.string(),
280      }),
281    ),
282  })
283  .openapi('Subreddit')