chore: scaffold turbo monorepo with hono api and react admin web
This commit is contained in:
9
packages/shared/.oxlintrc.json
Normal file
9
packages/shared/.oxlintrc.json
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"$schema": "./node_modules/oxlint/configuration_schema.json",
|
||||
"plugins": ["typescript", "oxc"],
|
||||
"categories": {
|
||||
"correctness": "error",
|
||||
"suspicious": "warn"
|
||||
},
|
||||
"ignorePatterns": ["src/generated"]
|
||||
}
|
||||
23
packages/shared/package.json
Normal file
23
packages/shared/package.json
Normal file
@@ -0,0 +1,23 @@
|
||||
{
|
||||
"name": "@superlist/shared",
|
||||
"version": "0.0.0",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"exports": {
|
||||
".": "./src/index.ts",
|
||||
"./problem-details": "./src/problem-details.ts"
|
||||
},
|
||||
"scripts": {
|
||||
"lint": "oxlint",
|
||||
"typecheck": "tsc --noEmit",
|
||||
"test": "vitest run --passWithNoTests"
|
||||
},
|
||||
"dependencies": {
|
||||
"zod": "^4.6.7"
|
||||
},
|
||||
"devDependencies": {
|
||||
"typescript": "~6.0.2",
|
||||
"vitest": "^5.0.4",
|
||||
"oxlint": "^1.81.0"
|
||||
}
|
||||
}
|
||||
34
packages/shared/src/index.test.ts
Normal file
34
packages/shared/src/index.test.ts
Normal 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 })
|
||||
})
|
||||
})
|
||||
2
packages/shared/src/index.ts
Normal file
2
packages/shared/src/index.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export * from './problem-details'
|
||||
export * from './result'
|
||||
94
packages/shared/src/problem-details.ts
Normal file
94
packages/shared/src/problem-details.ts
Normal 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
|
||||
|
||||
10
packages/shared/src/result.ts
Normal file
10
packages/shared/src/result.ts
Normal 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 })
|
||||
17
packages/shared/tsconfig.json
Normal file
17
packages/shared/tsconfig.json
Normal file
@@ -0,0 +1,17 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "ES2023",
|
||||
"lib": ["ES2023"],
|
||||
"module": "ESNext",
|
||||
"moduleResolution": "bundler",
|
||||
"moduleDetection": "force",
|
||||
"types": [],
|
||||
"strict": true,
|
||||
"noUncheckedIndexedAccess": true,
|
||||
"verbatimModuleSyntax": true,
|
||||
"isolatedModules": true,
|
||||
"skipLibCheck": true,
|
||||
"noEmit": true
|
||||
},
|
||||
"include": ["src"]
|
||||
}
|
||||
Reference in New Issue
Block a user