Schema improvements

This commit is contained in:
Konstantin Wohlwend 2025-07-29 12:13:29 -07:00
parent bfb2ba13d5
commit e2dd390925
4 changed files with 23 additions and 9 deletions

View File

@ -1,5 +1,4 @@
import { createAuthTokens } from "@/lib/tokens";
import { CrudHandlerInvocationError } from "@/route-handlers/crud-handler";
import { createSmartRouteHandler } from "@/route-handlers/smart-route-handler";
import { KnownErrors } from "@stackframe/stack-shared";
import { adaptSchema, serverOrHigherAuthTypeSchema, userIdOrMeSchema, yupBoolean, yupNumber, yupObject, yupString } from "@stackframe/stack-shared/dist/schema-fields";
@ -37,9 +36,12 @@ export const POST = createSmartRouteHandler({
user = await usersCrudHandlers.adminRead({
user_id: userId,
tenancy: tenancy,
allowedErrorTypes: [
KnownErrors.UserNotFound,
],
});
} catch (e) {
if (e instanceof CrudHandlerInvocationError && KnownErrors.UserNotFound.isInstance(e.cause)) {
if (KnownErrors.UserNotFound.isInstance(e)) {
throw new KnownErrors.UserIdDoesNotExist(userId);
}
throw e;

View File

@ -1,6 +1,5 @@
import { usersCrudHandlers } from "@/app/api/latest/users/crud";
import { getPrismaClientForTenancy } from "@/prisma-client";
import { CrudHandlerInvocationError } from "@/route-handlers/crud-handler";
import { createSmartRouteHandler } from "@/route-handlers/smart-route-handler";
import { KnownErrors } from "@stackframe/stack-shared";
import { adaptSchema, clientOrHigherAuthTypeSchema, contactChannelIdSchema, emailVerificationCallbackUrlSchema, userIdOrMeSchema, yupNumber, yupObject, yupString } from "@stackframe/stack-shared/dist/schema-fields";
@ -43,10 +42,13 @@ export const POST = createSmartRouteHandler({
try {
user = await usersCrudHandlers.adminRead({
tenancy: auth.tenancy,
user_id: params.user_id
user_id: params.user_id,
allowedErrorTypes: [
KnownErrors.UserNotFound,
],
});
} catch (e) {
if (e instanceof CrudHandlerInvocationError && KnownErrors.UserNotFound.isInstance(e.cause)) {
if (KnownErrors.UserNotFound.isInstance(e)) {
throw new KnownErrors.UserIdDoesNotExist(params.user_id);
}
throw e;

View File

@ -356,7 +356,7 @@ const organizationConfigDefaults = {
trustedDomains: (key: string) => ({
baseUrl: undefined,
handlerPath: '/handler',
}),
}) as const,
},
auth: {
@ -426,9 +426,9 @@ type DeepReplaceAllowFunctionsForObjects<T> = T extends object
)
:
T;
type ReplaceFunctionsWithObjects<T> = T & (T extends (arg: infer K extends string) => infer R ? Record<K, R | undefined> & object : unknown);
type ReplaceFunctionsWithObjects<T> = T & (T extends (arg: infer K extends string) => infer R ? Record<K, R> & object : unknown);
type DeepReplaceFunctionsWithObjects<T> = T extends object ? { [K in keyof ReplaceFunctionsWithObjects<T>]: DeepReplaceFunctionsWithObjects<ReplaceFunctionsWithObjects<T>[K]> } : T;
typeAssertIs<DeepReplaceFunctionsWithObjects<{ a: { b: 123 } & ((key: string) => number) }>, { a: { b: 123, [key: string]: number | undefined } }>()();
typeAssertIs<DeepReplaceFunctionsWithObjects<{ a: { b: 123 } & ((key: string) => number) }>, { a: { b: 123, [key: string]: number } }>()();
function deepReplaceFunctionsWithObjects(obj: any): any {
return mapValues({ ...obj }, v => (isObjectLike(v) ? deepReplaceFunctionsWithObjects(v as any) : v));

View File

@ -18,7 +18,17 @@ export type LastUnionElement<U> = UnionToIntersection<U extends any ? (x: U) =>
/**
* Makes a type prettier by recursively expanding all object types. For example, `Omit<{ a: 1 }, "a">` becomes just `{}`.
*/
export type Expand<T> = T extends object ? { [K in keyof T]: Expand<T[K]> } : T;
export type Expand<T> = T extends (...args: infer A) => infer R
? (
((...args: A) => R) extends T
? (...args: Expand<A>) => Expand<R>
: ((...args: Expand<A>) => Expand<R>) & { [K in keyof T]: Expand<T[K]> }
)
: (
T extends object
? { [K in keyof T]: Expand<T[K]> }
: T
);
/**