Skip to content

Commit

Permalink
chore: move some more complex schemas to separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
EvanHahn committed Nov 4, 2024
1 parent 3e073db commit 7d4a6be
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 46 deletions.
50 changes: 4 additions & 46 deletions src/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@ import timingSafeEqual from 'string-timing-safe-equal'
import assert from 'node:assert/strict'
import * as fs from 'node:fs'

import * as schemas from './schemas.js'
import { wsCoreReplicator } from './ws-core-replicator.js'

/** @import { FastifyInstance, FastifyPluginAsync, FastifyRequest, RawServerDefault } from 'fastify' */
/** @import { TypeBoxTypeProvider } from '@fastify/type-provider-typebox' */

const BEARER_SPACE_LENGTH = 'Bearer '.length

const HEX_REGEX_32_BYTES = '^[0-9a-fA-F]{64}$'
const HEX_STRING_32_BYTES = Type.String({ pattern: HEX_REGEX_32_BYTES })
const BASE32_REGEX_32_BYTES = '^[0-9A-Za-z]{52}$'
const BASE32_STRING_32_BYTES = Type.String({ pattern: BASE32_REGEX_32_BYTES })

Expand Down Expand Up @@ -116,21 +115,11 @@ export default async function routes(
'/projects',
{
schema: {
body: Type.Object({
projectName: Type.String({ minLength: 1 }),
projectKey: HEX_STRING_32_BYTES,
encryptionKeys: Type.Object({
auth: HEX_STRING_32_BYTES,
config: HEX_STRING_32_BYTES,
data: HEX_STRING_32_BYTES,
blobIndex: HEX_STRING_32_BYTES,
blob: HEX_STRING_32_BYTES,
}),
}),
body: schemas.projectToAdd,
response: {
200: Type.Object({
data: Type.Object({
deviceId: HEX_STRING_32_BYTES,
deviceId: schemas.HEX_STRING_32_BYTES,
}),
}),
400: { $ref: 'HttpError' },
Expand Down Expand Up @@ -262,38 +251,7 @@ export default async function routes(
}),
response: {
200: Type.Object({
data: Type.Array(
Type.Object({
docId: Type.String(),
createdAt: Type.String(),
updatedAt: Type.String(),
deleted: Type.Boolean(),
lat: Type.Optional(Type.Number()),
lon: Type.Optional(Type.Number()),
attachments: Type.Array(
Type.Object({
url: Type.String(),
}),
),
tags: Type.Record(
Type.String(),
Type.Union([
Type.Boolean(),
Type.Number(),
Type.String(),
Type.Null(),
Type.Array(
Type.Union([
Type.Boolean(),
Type.Number(),
Type.String(),
Type.Null(),
]),
),
]),
),
}),
),
data: Type.Array(schemas.observationToAdd),
}),
403: { $ref: 'HttpError' },
404: { $ref: 'HttpError' },
Expand Down
46 changes: 46 additions & 0 deletions src/schemas.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { Type } from '@sinclair/typebox'

const HEX_REGEX_32_BYTES = '^[0-9a-fA-F]{64}$'
export const HEX_STRING_32_BYTES = Type.String({ pattern: HEX_REGEX_32_BYTES })

const dateTimeString = Type.String({ format: 'date-time' })
const latitude = Type.Number({ minimum: -90, maximum: 90 })
const longitude = Type.Number({ minimum: -180, maximum: 180 })

export const projectToAdd = Type.Object({
projectName: Type.String({ minLength: 1 }),
projectKey: HEX_STRING_32_BYTES,
encryptionKeys: Type.Object({
auth: HEX_STRING_32_BYTES,
config: HEX_STRING_32_BYTES,
data: HEX_STRING_32_BYTES,
blobIndex: HEX_STRING_32_BYTES,
blob: HEX_STRING_32_BYTES,
}),
})

export const observationToAdd = Type.Object({
docId: Type.String(),
createdAt: dateTimeString,
updatedAt: dateTimeString,
deleted: Type.Boolean(),
lat: Type.Optional(latitude),
lon: Type.Optional(longitude),
attachments: Type.Array(
Type.Object({
url: Type.String(),
}),
),
tags: Type.Record(
Type.String(),
Type.Union([
Type.Boolean(),
Type.Number(),
Type.String(),
Type.Null(),
Type.Array(
Type.Union([Type.Boolean(), Type.Number(), Type.String(), Type.Null()]),
),
]),
),
})

0 comments on commit 7d4a6be

Please sign in to comment.