c7861be520
Once this is merged, oxlint is at a pretty sensible baseline. **Enable `no-unused-vars`, `typescript/consistent-type-imports`, and `import/no-duplicates` lint rules** Turns on three previously-disabled oxlint rules across the monorepo and fixes all violations: - **`no-unused-vars`** – enabled as an error with standard ignore patterns: unused function arguments are ignored by default (`args: "none"`), variables/caught errors/destructured array elements prefixed with `_` are allowed, and rest siblings are permitted. - **`typescript/consistent-type-imports`** – enforced as an error; all type-only imports now use the `import type` syntax. - **`import/no-duplicates`** – enforced as an error; duplicate import statements from the same module have been merged. The remaining commits clean up the violations found across the codebase: removing unused variables/imports/type aliases, adding `_` prefixes to intentionally unused bindings, fixing duplicate imports, and converting value imports to `import type` where appropriate.
67 lines
1.3 KiB
TypeScript
67 lines
1.3 KiB
TypeScript
import type { z } from "zod";
|
|
|
|
export function safeJsonParse(json?: string): unknown {
|
|
if (!json) {
|
|
return;
|
|
}
|
|
|
|
try {
|
|
return JSON.parse(json);
|
|
} catch (_e) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
export function safeJsonZodParse<T>(
|
|
schema: z.Schema<T>,
|
|
json: string
|
|
): z.SafeParseReturnType<unknown, T> | undefined {
|
|
const parsed = safeJsonParse(json);
|
|
|
|
if (parsed === null) {
|
|
return;
|
|
}
|
|
|
|
return schema.safeParse(parsed);
|
|
}
|
|
|
|
export async function safeJsonFromResponse(response: Response) {
|
|
const json = await response.text();
|
|
return safeJsonParse(json);
|
|
}
|
|
|
|
export async function safeBodyFromResponse<T>(
|
|
response: Response,
|
|
schema: z.Schema<T>
|
|
): Promise<T | undefined> {
|
|
const json = await response.text();
|
|
const unknownJson = safeJsonParse(json);
|
|
|
|
if (!unknownJson) {
|
|
return;
|
|
}
|
|
|
|
const parsedJson = schema.safeParse(unknownJson);
|
|
|
|
if (parsedJson.success) {
|
|
return parsedJson.data;
|
|
}
|
|
}
|
|
|
|
export async function safeParseBodyFromResponse<T>(
|
|
response: Response,
|
|
schema: z.Schema<T>
|
|
): Promise<z.SafeParseReturnType<unknown, T> | undefined> {
|
|
try {
|
|
const unknownJson = await response.json();
|
|
|
|
if (!unknownJson) {
|
|
return;
|
|
}
|
|
|
|
const parsedJson = schema.safeParse(unknownJson);
|
|
|
|
return parsedJson;
|
|
} catch (_error) {}
|
|
}
|