bunderstack 0.16.0 → 0.17.0-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,488 @@
1
+ import {
2
+ eq,
3
+ getTableColumns,
4
+ getTableName,
5
+ isTable,
6
+ type Table,
7
+ } from 'drizzle-orm'
8
+
9
+ import type { AnyDb } from './dialect'
10
+ import type { RealtimeFacade } from './realtime/facade'
11
+
12
+ import {
13
+ checkAccess,
14
+ rowMatchesScope,
15
+ sanitizeWriteBody,
16
+ stampScope,
17
+ tableEntryForName,
18
+ type AccessUser,
19
+ type ResolvedAccess,
20
+ type ResolvedTableAccess,
21
+ type ScopeMap,
22
+ type ScopeResolver,
23
+ } from './access'
24
+ import {
25
+ BunderstackError,
26
+ ErrorCode,
27
+ ListQueryError,
28
+ type BunderstackErrorCode,
29
+ type ErrorCodeValue,
30
+ } from './errors'
31
+ import {
32
+ lookupIdempotency,
33
+ resolveIdempotencyConfig,
34
+ storeIdempotency,
35
+ type IdempotencyConfig,
36
+ } from './idempotency'
37
+ import { executeList, parseListParams, type ListResult } from './list-query'
38
+ import { buildScopeWhere } from './scope'
39
+
40
+ export interface CrudExecutionContext {
41
+ request: Request
42
+ user: AccessUser | null
43
+ session: { activeOrganizationId: string | null }
44
+ }
45
+
46
+ function errorCodeForStatus(status: number): BunderstackErrorCode {
47
+ if (status === 401) return 'UNAUTHORIZED'
48
+ if (status === 403) return 'FORBIDDEN'
49
+ if (status === 404) return 'NOT_FOUND'
50
+ if (status === 409) return 'CONFLICT'
51
+ if (status === 413) return 'PAYLOAD_TOO_LARGE'
52
+ if (status === 429) return 'RATE_LIMITED'
53
+ return 'VALIDATION_ERROR'
54
+ }
55
+
56
+ export class CrudOperationError extends BunderstackError {
57
+ constructor(
58
+ status: number,
59
+ readonly legacyCode: ErrorCodeValue,
60
+ message: string,
61
+ details?: unknown,
62
+ ) {
63
+ const code = errorCodeForStatus(status)
64
+ super(
65
+ code,
66
+ message,
67
+ legacyCode === code
68
+ ? details
69
+ : {
70
+ code: legacyCode,
71
+ ...(details === undefined ? {} : { details }),
72
+ },
73
+ )
74
+ this.name = 'CrudOperationError'
75
+ }
76
+ }
77
+
78
+ export type CrudOperationsDeps<
79
+ TSchema extends Record<string, unknown> = Record<string, unknown>,
80
+ > = {
81
+ schema: TSchema
82
+ db: AnyDb
83
+ access: ResolvedAccess
84
+ idempotency?: boolean | IdempotencyConfig
85
+ realtime?: RealtimeFacade<TSchema>
86
+ }
87
+
88
+ export type CreateResult =
89
+ | { type: 'created'; status: 201; record: Record<string, unknown> }
90
+ | {
91
+ type: 'replay'
92
+ status: number
93
+ body: string
94
+ record: Record<string, unknown>
95
+ }
96
+
97
+ function isRecord(value: unknown): value is Record<string, unknown> {
98
+ return value !== null && typeof value === 'object' && !Array.isArray(value)
99
+ }
100
+
101
+ function isUniqueConstraintError(error: unknown): boolean {
102
+ const seen = new Set<unknown>()
103
+ let current = error
104
+
105
+ while (isRecord(current) && !seen.has(current)) {
106
+ seen.add(current)
107
+ const code = current['code']
108
+ if (
109
+ code === '23505' ||
110
+ code === 'SQLITE_CONSTRAINT_UNIQUE' ||
111
+ code === 'SQLITE_CONSTRAINT_PRIMARYKEY'
112
+ ) {
113
+ return true
114
+ }
115
+
116
+ const message = current['message']
117
+ if (
118
+ typeof message === 'string' &&
119
+ (/duplicate key value violates unique constraint/i.test(message) ||
120
+ /unique constraint failed/i.test(message))
121
+ ) {
122
+ return true
123
+ }
124
+
125
+ current = current['cause']
126
+ }
127
+
128
+ return false
129
+ }
130
+
131
+ function coerceId(rawId: string | number): string | number {
132
+ if (typeof rawId === 'number') return rawId
133
+ return isNaN(Number(rawId)) ? rawId : Number(rawId)
134
+ }
135
+
136
+ export function createCrudOperations<
137
+ TSchema extends Record<string, unknown> = Record<string, unknown>,
138
+ >(deps: CrudOperationsDeps<TSchema>) {
139
+ const { schema, db, access, realtime } = deps
140
+ const idempotency = resolveIdempotencyConfig(deps.idempotency)
141
+
142
+ const scopeFor = (
143
+ resolver: ScopeResolver | undefined,
144
+ ctx: {
145
+ user: AccessUser | null
146
+ session: { activeOrganizationId: string | null }
147
+ request: Request
148
+ row?: Record<string, unknown>
149
+ body?: Record<string, unknown>
150
+ },
151
+ ): ScopeMap | undefined => (resolver ? resolver(ctx) : undefined)
152
+
153
+ function resolveTable(tableName: string) {
154
+ const table = Object.values(schema).find(
155
+ (t) => isTable(t) && getTableName(t) === tableName,
156
+ ) as Table | undefined
157
+ if (!table) {
158
+ throw new CrudOperationError(404, ErrorCode.NOT_FOUND, 'Not found')
159
+ }
160
+ const tableAccess = tableEntryForName(access, tableName)
161
+ if (!tableAccess || !tableAccess.enabled) {
162
+ throw new CrudOperationError(404, ErrorCode.NOT_FOUND, 'Not found')
163
+ }
164
+ const idCol = getTableColumns(table)['id']
165
+ if (!idCol) {
166
+ throw new CrudOperationError(
167
+ 400,
168
+ ErrorCode.VALIDATION_ERROR,
169
+ `Table ${tableName} has no id column`,
170
+ )
171
+ }
172
+ return { table, tableAccess, idCol }
173
+ }
174
+
175
+ return {
176
+ async list(
177
+ tableName: string,
178
+ paramsInput: URL | Record<string, unknown> | undefined,
179
+ ctx: CrudExecutionContext,
180
+ ): Promise<ListResult<Record<string, unknown>>> {
181
+ const { table, tableAccess, idCol } = resolveTable(tableName)
182
+
183
+ const denied = await checkAccess(
184
+ tableAccess.list,
185
+ ctx,
186
+ tableAccess.ownerColumn,
187
+ )
188
+ if (!denied.allowed) {
189
+ throw new CrudOperationError(
190
+ denied.status === 401 ? 401 : 403,
191
+ ErrorCode.FORBIDDEN,
192
+ 'Forbidden',
193
+ )
194
+ }
195
+
196
+ let urlObj: URL
197
+ if (paramsInput instanceof URL) {
198
+ urlObj = paramsInput
199
+ } else {
200
+ urlObj = new URL(ctx.request.url || 'http://localhost')
201
+ if (paramsInput) {
202
+ for (const [k, v] of Object.entries(paramsInput)) {
203
+ if (v !== undefined && v !== null) {
204
+ urlObj.searchParams.set(k, String(v))
205
+ }
206
+ }
207
+ }
208
+ }
209
+
210
+ try {
211
+ const params = parseListParams(urlObj, tableAccess)
212
+ const scope = scopeFor(tableAccess.readScope, ctx)
213
+ const scopeWhere = scope ? buildScopeWhere(table, scope) : undefined
214
+ return await executeList(
215
+ db,
216
+ table,
217
+ tableAccess,
218
+ params,
219
+ idCol,
220
+ scopeWhere,
221
+ )
222
+ } catch (err) {
223
+ if (err instanceof ListQueryError) {
224
+ throw new CrudOperationError(400, err.code, err.message, err.details)
225
+ }
226
+ throw err
227
+ }
228
+ },
229
+
230
+ async get(
231
+ tableName: string,
232
+ rawId: string | number,
233
+ ctx: CrudExecutionContext,
234
+ ): Promise<Record<string, unknown>> {
235
+ const { table, tableAccess, idCol } = resolveTable(tableName)
236
+ const id = coerceId(rawId)
237
+
238
+ const rows = await db
239
+ .select()
240
+ .from(table)
241
+ .where(eq(idCol, id))
242
+ if (!rows[0]) {
243
+ throw new CrudOperationError(404, ErrorCode.NOT_FOUND, 'Not found')
244
+ }
245
+ const row = rows[0] as Record<string, unknown>
246
+
247
+ const denied = await checkAccess(
248
+ tableAccess.get,
249
+ { ...ctx, row },
250
+ tableAccess.ownerColumn,
251
+ )
252
+ if (!denied.allowed) {
253
+ throw new CrudOperationError(
254
+ denied.status === 401 ? 401 : 403,
255
+ ErrorCode.FORBIDDEN,
256
+ 'Forbidden',
257
+ )
258
+ }
259
+
260
+ const scope = scopeFor(tableAccess.readScope, ctx)
261
+ if (scope && !rowMatchesScope(row, scope)) {
262
+ throw new CrudOperationError(404, ErrorCode.NOT_FOUND, 'Not found')
263
+ }
264
+
265
+ return row
266
+ },
267
+
268
+ async create(
269
+ tableName: string,
270
+ body: unknown,
271
+ rawBody: string | undefined,
272
+ idempotencyKey: string | undefined,
273
+ ctx: CrudExecutionContext,
274
+ ): Promise<CreateResult> {
275
+ const { table, tableAccess } = resolveTable(tableName)
276
+
277
+ const denied = await checkAccess(
278
+ tableAccess.create,
279
+ ctx,
280
+ tableAccess.ownerColumn,
281
+ )
282
+ if (!denied.allowed) {
283
+ throw new CrudOperationError(
284
+ denied.status === 401 ? 401 : 403,
285
+ ErrorCode.FORBIDDEN,
286
+ 'Forbidden',
287
+ )
288
+ }
289
+
290
+ if (!isRecord(body)) {
291
+ throw new CrudOperationError(
292
+ 400,
293
+ ErrorCode.VALIDATION_ERROR,
294
+ 'Invalid JSON body',
295
+ )
296
+ }
297
+
298
+ const trimmedKey = idempotencyKey?.trim()
299
+ const effectiveRawBody = rawBody ?? JSON.stringify(body)
300
+
301
+ if (idempotency && trimmedKey) {
302
+ const lookup = await lookupIdempotency(
303
+ db,
304
+ tableName,
305
+ trimmedKey,
306
+ effectiveRawBody,
307
+ idempotency,
308
+ )
309
+ if (lookup.type === 'conflict') {
310
+ throw new CrudOperationError(
311
+ 409,
312
+ ErrorCode.IDEMPOTENCY_CONFLICT,
313
+ 'Idempotency key reused with different body',
314
+ )
315
+ }
316
+ if (lookup.type === 'replay') {
317
+ return {
318
+ type: 'replay',
319
+ status: lookup.status,
320
+ body: lookup.response,
321
+ record: JSON.parse(lookup.response) as Record<string, unknown>,
322
+ }
323
+ }
324
+ }
325
+
326
+ const values = sanitizeWriteBody(
327
+ body,
328
+ tableAccess,
329
+ 'create',
330
+ ctx.user?.id ?? null,
331
+ )
332
+
333
+ const scope = scopeFor(tableAccess.writeScope, { ...ctx, body })
334
+ const stamped = scope ? stampScope(values, scope) : values
335
+
336
+ let rows: Record<string, unknown>[]
337
+ try {
338
+ rows = await db.insert(table).values(stamped).returning()
339
+ } catch (error) {
340
+ if (isUniqueConstraintError(error)) {
341
+ throw new CrudOperationError(
342
+ 409,
343
+ ErrorCode.CONFLICT,
344
+ 'Record already exists',
345
+ )
346
+ }
347
+ throw error
348
+ }
349
+ const created = rows[0] as Record<string, unknown>
350
+ void realtime?.publish(table as never, 'create', created as never)
351
+
352
+ if (idempotency && trimmedKey) {
353
+ await storeIdempotency(
354
+ db,
355
+ tableName,
356
+ trimmedKey,
357
+ effectiveRawBody,
358
+ 201,
359
+ created,
360
+ idempotency,
361
+ )
362
+ }
363
+
364
+ return {
365
+ type: 'created',
366
+ status: 201,
367
+ record: created,
368
+ }
369
+ },
370
+
371
+ async update(
372
+ tableName: string,
373
+ rawId: string | number,
374
+ body: unknown,
375
+ ctx: CrudExecutionContext,
376
+ ): Promise<Record<string, unknown>> {
377
+ const { table, tableAccess, idCol } = resolveTable(tableName)
378
+ const id = coerceId(rawId)
379
+
380
+ const existing = await db
381
+ .select()
382
+ .from(table)
383
+ .where(eq(idCol, id))
384
+ if (!existing[0]) {
385
+ throw new CrudOperationError(404, ErrorCode.NOT_FOUND, 'Not found')
386
+ }
387
+ const existingRow = existing[0] as Record<string, unknown>
388
+
389
+ const readScope = scopeFor(tableAccess.readScope, ctx)
390
+ if (readScope && !rowMatchesScope(existingRow, readScope)) {
391
+ throw new CrudOperationError(404, ErrorCode.NOT_FOUND, 'Not found')
392
+ }
393
+
394
+ const denied = await checkAccess(
395
+ tableAccess.update,
396
+ { ...ctx, row: existingRow },
397
+ tableAccess.ownerColumn,
398
+ )
399
+ if (!denied.allowed) {
400
+ throw new CrudOperationError(
401
+ denied.status === 401 ? 401 : 403,
402
+ ErrorCode.FORBIDDEN,
403
+ 'Forbidden',
404
+ )
405
+ }
406
+
407
+ if (!isRecord(body)) {
408
+ throw new CrudOperationError(
409
+ 400,
410
+ ErrorCode.VALIDATION_ERROR,
411
+ 'Invalid JSON body',
412
+ )
413
+ }
414
+
415
+ const values = sanitizeWriteBody(
416
+ body,
417
+ tableAccess,
418
+ 'update',
419
+ ctx.user?.id ?? null,
420
+ )
421
+
422
+ if (Object.keys(values).length === 0) {
423
+ throw new CrudOperationError(
424
+ 400,
425
+ ErrorCode.VALIDATION_ERROR,
426
+ 'No writable fields to update',
427
+ )
428
+ }
429
+
430
+ const writeScope = scopeFor(tableAccess.writeScope, { ...ctx, body })
431
+ const stamped = writeScope ? stampScope(values, writeScope) : values
432
+
433
+ const rows = await db
434
+ .update(table)
435
+ .set(stamped)
436
+ .where(eq(idCol, id))
437
+ .returning()
438
+
439
+ if (!rows[0]) {
440
+ throw new CrudOperationError(404, ErrorCode.NOT_FOUND, 'Not found')
441
+ }
442
+ const updated = rows[0] as Record<string, unknown>
443
+ void realtime?.publish(table as never, 'update', updated as never)
444
+ return updated
445
+ },
446
+
447
+ async delete(
448
+ tableName: string,
449
+ rawId: string | number,
450
+ ctx: CrudExecutionContext,
451
+ ): Promise<void> {
452
+ const { table, tableAccess, idCol } = resolveTable(tableName)
453
+ const id = coerceId(rawId)
454
+
455
+ const existing = await db
456
+ .select()
457
+ .from(table)
458
+ .where(eq(idCol, id))
459
+ if (!existing[0]) {
460
+ throw new CrudOperationError(404, ErrorCode.NOT_FOUND, 'Not found')
461
+ }
462
+ const existingRow = existing[0] as Record<string, unknown>
463
+
464
+ const readScope = scopeFor(tableAccess.readScope, ctx)
465
+ if (readScope && !rowMatchesScope(existingRow, readScope)) {
466
+ throw new CrudOperationError(404, ErrorCode.NOT_FOUND, 'Not found')
467
+ }
468
+
469
+ const denied = await checkAccess(
470
+ tableAccess.delete,
471
+ { ...ctx, row: existingRow },
472
+ tableAccess.ownerColumn,
473
+ )
474
+ if (!denied.allowed) {
475
+ throw new CrudOperationError(
476
+ denied.status === 401 ? 401 : 403,
477
+ ErrorCode.FORBIDDEN,
478
+ 'Forbidden',
479
+ )
480
+ }
481
+
482
+ await db.delete(table).where(eq(idCol, id))
483
+ void realtime?.publish(table as never, 'delete', existingRow as never)
484
+ },
485
+ }
486
+ }
487
+
488
+ export type CrudOperations = ReturnType<typeof createCrudOperations>
package/src/dialect.ts CHANGED
@@ -10,7 +10,7 @@ export type Dialect = 'sqlite' | 'pg'
10
10
  * Minimal structural view of a drizzle db shared by both dialects. Internal
11
11
  * modules run dynamic tables (Record<string, unknown> schemas) where drizzle's
12
12
  * generics add no safety, so they accept this instead of a per-dialect union.
13
- * The public surface (`app.db`, tRPC ctx) keeps full per-dialect typing via
13
+ * The public surface (`app.db`, API context) keeps full per-dialect typing via
14
14
  * `DbFor` in db.ts.
15
15
  */
16
16
  export type AnyDb = {
package/src/env.ts CHANGED
@@ -1,11 +1,16 @@
1
- // src/env.ts — env validation. Browser-safe: imports zod only.
2
- import { z, type ZodType } from 'zod'
1
+ // src/env.ts — env validation. Browser-safe: type-only Standard Schema import.
2
+ import type { StandardSchemaV1 } from '@standard-schema/spec'
3
+
4
+ import {
5
+ StandardSchemaValidationError,
6
+ validateStandardSchema,
7
+ } from './standard-schema'
3
8
 
4
9
  export const CLIENT_PREFIX = 'PUBLIC_' as const
5
10
 
6
11
  export type EnvConfigInput = {
7
- server?: Record<string, ZodType>
8
- client?: Record<string, ZodType>
12
+ server?: Record<string, StandardSchemaV1>
13
+ client?: Record<string, StandardSchemaV1>
9
14
  /** Explicit value source for client vars (e.g. Vite's import.meta.env). */
10
15
  runtimeEnv?: Record<string, unknown>
11
16
  }
@@ -27,8 +32,8 @@ export type BaseEnv = {
27
32
  }
28
33
 
29
34
  type InferVars<T> =
30
- T extends Record<string, ZodType>
31
- ? { [K in keyof T]: z.output<T[K]> }
35
+ T extends Record<string, StandardSchemaV1>
36
+ ? { [K in keyof T]: StandardSchemaV1.InferOutput<T[K]> }
32
37
  : unknown
33
38
 
34
39
  // Non-distributive so `ValidatedEnv<undefined>` is BaseEnv, not `never`.
@@ -62,7 +67,7 @@ export type ValidateEnvOptions = {
62
67
  const DEV_AUTH_SECRET = 'dev-secret-change-in-prod'
63
68
 
64
69
  function validateSection(
65
- section: Record<string, ZodType> | undefined,
70
+ section: Record<string, StandardSchemaV1> | undefined,
66
71
  kind: 'server' | 'client',
67
72
  source: Record<string, unknown>,
68
73
  issues: string[],
@@ -82,12 +87,13 @@ function validateSection(
82
87
  )
83
88
  continue
84
89
  }
85
- const result = schema.safeParse(source[key])
86
- if (result.success) {
87
- out[key] = result.data
88
- } else {
89
- for (const issue of result.error.issues) {
90
- issues.push(`${key}: ${issue.message}`)
90
+ try {
91
+ out[key] = validateStandardSchema(schema, source[key], 'env')
92
+ } catch (error) {
93
+ if (!(error instanceof StandardSchemaValidationError)) throw error
94
+ for (const issue of error.issues) {
95
+ const path = issue.path.map(String).join('.')
96
+ issues.push(`${key}${path ? `.${path}` : ''}: ${issue.message}`)
91
97
  }
92
98
  }
93
99
  }
package/src/errors.ts CHANGED
@@ -1,10 +1,98 @@
1
- import type { Context } from 'hono'
2
- import type { ContentfulStatusCode } from 'hono/utils/http-status'
1
+ import { ORPCError, os, type ErrorMap } from '@orpc/server'
2
+ import * as v from 'valibot'
3
3
 
4
+ import { StandardSchemaValidationError } from './standard-schema'
5
+
6
+ export const BUNDERSTACK_ERROR_CODES = [
7
+ 'VALIDATION_ERROR',
8
+ 'UNAUTHORIZED',
9
+ 'FORBIDDEN',
10
+ 'NOT_FOUND',
11
+ 'CONFLICT',
12
+ 'PAYLOAD_TOO_LARGE',
13
+ 'RATE_LIMITED',
14
+ ] as const
15
+
16
+ export type BunderstackErrorCode = (typeof BUNDERSTACK_ERROR_CODES)[number]
17
+
18
+ export const BUNDERSTACK_ERROR_STATUS_MAP = {
19
+ VALIDATION_ERROR: 400,
20
+ UNAUTHORIZED: 401,
21
+ FORBIDDEN: 403,
22
+ NOT_FOUND: 404,
23
+ CONFLICT: 409,
24
+ PAYLOAD_TOO_LARGE: 413,
25
+ RATE_LIMITED: 429,
26
+ } as const satisfies Record<BunderstackErrorCode, number>
27
+
28
+ function errorDataSchema<const TCode extends BunderstackErrorCode>(
29
+ code: TCode,
30
+ ) {
31
+ return v.strictObject({
32
+ code: v.literal(code),
33
+ details: v.optional(v.unknown()),
34
+ })
35
+ }
36
+
37
+ export const BUNDERSTACK_ERRORS = {
38
+ VALIDATION_ERROR: { data: errorDataSchema('VALIDATION_ERROR') },
39
+ UNAUTHORIZED: { data: errorDataSchema('UNAUTHORIZED') },
40
+ FORBIDDEN: { data: errorDataSchema('FORBIDDEN') },
41
+ NOT_FOUND: { data: errorDataSchema('NOT_FOUND') },
42
+ CONFLICT: { data: errorDataSchema('CONFLICT') },
43
+ PAYLOAD_TOO_LARGE: { data: errorDataSchema('PAYLOAD_TOO_LARGE') },
44
+ RATE_LIMITED: { data: errorDataSchema('RATE_LIMITED') },
45
+ } as const satisfies ErrorMap
46
+
47
+ export class BunderstackError extends Error {
48
+ readonly status: number
49
+
50
+ constructor(
51
+ readonly code: BunderstackErrorCode,
52
+ message: string,
53
+ readonly details?: unknown,
54
+ options?: ErrorOptions,
55
+ ) {
56
+ super(message, options)
57
+ this.name = 'BunderstackError'
58
+ this.status = BUNDERSTACK_ERROR_STATUS_MAP[code]
59
+ }
60
+ }
61
+
62
+ export const mapBunderstackErrors = os
63
+ .errors(BUNDERSTACK_ERRORS)
64
+ .middleware(async ({ next }) => {
65
+ try {
66
+ return await next()
67
+ } catch (error) {
68
+ const mapped =
69
+ error instanceof StandardSchemaValidationError
70
+ ? new BunderstackError(
71
+ 'VALIDATION_ERROR',
72
+ error.message,
73
+ error.issues,
74
+ { cause: error },
75
+ )
76
+ : error
77
+ if (!(mapped instanceof BunderstackError)) throw error
78
+
79
+ throw new ORPCError(mapped.code, {
80
+ message: mapped.message,
81
+ data:
82
+ mapped.details === undefined
83
+ ? { code: mapped.code }
84
+ : { code: mapped.code, details: mapped.details },
85
+ cause: mapped,
86
+ })
87
+ }
88
+ })
89
+
90
+ /** @deprecated Legacy internal error codes retained for list-query compatibility. */
4
91
  export const ErrorCode = {
5
92
  VALIDATION_ERROR: 'VALIDATION_ERROR',
6
93
  FORBIDDEN: 'FORBIDDEN',
7
94
  NOT_FOUND: 'NOT_FOUND',
95
+ CONFLICT: 'CONFLICT',
8
96
  INVALID_CURSOR: 'INVALID_CURSOR',
9
97
  RATE_LIMITED: 'RATE_LIMITED',
10
98
  IDEMPOTENCY_REPLAY: 'IDEMPOTENCY_REPLAY',
@@ -13,27 +101,6 @@ export const ErrorCode = {
13
101
 
14
102
  export type ErrorCodeValue = (typeof ErrorCode)[keyof typeof ErrorCode]
15
103
 
16
- export type ApiErrorBody = {
17
- error: string
18
- code?: ErrorCodeValue
19
- details?: unknown
20
- }
21
-
22
- export function apiError(
23
- c: Context,
24
- code: ErrorCodeValue,
25
- message: string,
26
- status: ContentfulStatusCode,
27
- details?: unknown,
28
- ) {
29
- const body: ApiErrorBody = {
30
- error: message,
31
- code,
32
- ...(details ? { details } : {}),
33
- }
34
- return c.json(body, status)
35
- }
36
-
37
104
  export class ListQueryError extends Error {
38
105
  readonly code: ErrorCodeValue
39
106
  readonly details?: unknown