chore: scaffold turbo monorepo with hono api and react admin web

This commit is contained in:
Jose Selesan
2026-10-15 15:24:34 -03:00
commit 2b1d2152c4
89 changed files with 4996 additions and 0 deletions

View File

@@ -0,0 +1,34 @@
import { describe, expect, it } from 'vitest'
import { Failure, Ok, problem, problemTypes } from '../src/index'
describe('Result', () => {
it('wraps success values', () => {
expect(Ok({ id: '1' })).toEqual({ ok: true, value: { id: '1' } })
})
it('wraps failures', () => {
expect(Failure('boom')).toEqual({ ok: false, error: 'boom' })
})
})
describe('problem', () => {
it('defaults type to about:blank and strips undefined members', () => {
expect(problem({ title: 'Not Found', status: 404 })).toEqual({
type: 'about:blank',
title: 'Not Found',
status: 404,
})
})
it('accepts a custom type and detail', () => {
expect(
problem({
type: problemTypes.validation,
title: 'Validation failed',
status: 422,
detail: 'name is required',
}),
).toMatchObject({ status: 422, type: problemTypes.validation })
})
})

View File

@@ -0,0 +1,2 @@
export * from './problem-details'
export * from './result'

View File

@@ -0,0 +1,94 @@
import { z } from 'zod'
/**
* RFC 9457 — Problem Details for HTTP APIs.
* https://www.rfc-editor.org/rfc/rfc9457.html
*
* Every error returned by the API uses the `application/problem+json` media type
* and this shape. Members are: `type`, `title`, `status`, `detail`, `instance`.
*/
export const problemDetailsSchema = z.object({
/** URI reference that identifies the problem type. */
type: z.string(),
/** Short, human-readable summary of the problem type. */
title: z.string(),
/** HTTP status code. */
status: z.number().int().min(100).max(599),
/** Human-readable explanation specific to this occurrence. */
detail: z.string().optional(),
/** URI reference that identifies this specific occurrence. */
instance: z.string().optional(),
/** Problem specific extensions. */
errors: z.record(z.string(), z.array(z.string())).optional(),
})
export type ProblemDetails = z.infer<typeof problemDetailsSchema>
export const PROBLEM_CONTENT_TYPE = 'application/problem+json'
type ProblemInit = Omit<ProblemDetails, 'type'> & {
/** Problem type, defaults to `about:blank`. */
type?: string
}
/**
* Builds a spec compliant Problem Details payload.
* Omitted members are stripped so the response stays minimal.
*/
export function problem(init: ProblemInit): ProblemDetails {
const { type = 'about:blank', ...rest } = init
return Object.fromEntries(
Object.entries({ type, ...rest }).filter(([, value]) => value !== undefined),
) as ProblemDetails
}
/** Common problem types used across features. */
export const problemTypes = {
badRequest: 'https://superlist.app/problems/bad-request',
validation: 'https://superlist.app/problems/validation-error',
unauthorized: 'https://superlist.app/problems/unauthorized',
forbidden: 'https://superlist.app/problems/forbidden',
notFound: 'https://superlist.app/problems/not-found',
conflict: 'https://superlist.app/problems/conflict',
internal: 'https://superlist.app/problems/internal-error',
unavailable: 'https://superlist.app/problems/service-unavailable',
} as const
/** Field name → messages, used by the `validation` error below. */
export type FieldErrors = Record<string, string[]>
/**
* Expected failures returned by use cases. Structurally identical to a Problem
* Details payload, so the HTTP layer can turn any of them into a response
* without knowing the domain.
*/
export const problems = {
badRequest: (detail?: string): ProblemDetails =>
problem({ type: problemTypes.badRequest, title: 'Bad Request', status: 400, detail }),
unauthorized: (detail = 'You must be signed in to do this.'): ProblemDetails =>
problem({ type: problemTypes.unauthorized, title: 'Unauthorized', status: 401, detail }),
forbidden: (detail = 'You do not have access to this resource.'): ProblemDetails =>
problem({ type: problemTypes.forbidden, title: 'Forbidden', status: 403, detail }),
notFound: (detail = 'The requested resource does not exist.'): ProblemDetails =>
problem({ type: problemTypes.notFound, title: 'Not Found', status: 404, detail }),
conflict: (detail?: string): ProblemDetails =>
problem({ type: problemTypes.conflict, title: 'Conflict', status: 409, detail }),
validation: (
errors: FieldErrors,
detail = 'The request payload is invalid.',
): ProblemDetails =>
problem({ type: problemTypes.validation, title: 'Validation Error', status: 422, detail, errors }),
internal: (detail = 'An unexpected error happened while processing the request.'): ProblemDetails =>
problem({ type: problemTypes.internal, title: 'Internal Server Error', status: 500, detail }),
unavailable: (detail = 'The service is temporarily unavailable.'): ProblemDetails =>
problem({ type: problemTypes.unavailable, title: 'Service Unavailable', status: 503, detail }),
} as const

View File

@@ -0,0 +1,10 @@
/**
* Result pattern shared by all use cases.
* Use cases return `Result` instead of throwing for expected outcomes.
*/
export type Ok<T> = { ok: true; value: T }
export type Failure<E> = { ok: false; error: E }
export type Result<T, E> = Ok<T> | Failure<E>
export const Ok = <T>(value: T): Ok<T> => ({ ok: true, value })
export const Failure = <E>(error: E): Failure<E> => ({ ok: false, error })