Skip to content

Commit 116c081

Browse files
committed
Merge remote-tracking branch 'origin/main' into miello/feat/request-context-per-request
2 parents f20aef6 + f1bc95b commit 116c081

4 files changed

Lines changed: 22 additions & 12 deletions

File tree

examples/erp/drizzlify/collections/categories.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export const categoriesCollection = builder.collection('categories', {
2424
}),
2525
})),
2626
options: builder.options(async ({ db }) => {
27-
const result = await db.query.users.findMany({ columns: { id: true, name: true } })
27+
const result = await db.query.user.findMany({ columns: { id: true, name: true } })
2828
return result.map((user) => ({ label: user.name ?? user.id, value: user.id }))
2929
}),
3030
})),

examples/erp/drizzlify/collections/posts.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export const postsCollection = builder.collection('posts', {
1515
}),
1616
authorId: fb.relations('author', (fb) => ({
1717
type: 'connect',
18-
fields: fb.fields('users', (fb) => ({
18+
fields: fb.fields('user', (fb) => ({
1919
id: fb.columns('id', {
2020
type: 'text',
2121
}),
@@ -27,7 +27,7 @@ export const postsCollection = builder.collection('posts', {
2727
}),
2828
})),
2929
options: builder.options(async ({ db }) => {
30-
const result = await db.query.users.findMany({ columns: { id: true, name: true } })
30+
const result = await db.query.user.findMany({ columns: { id: true, name: true } })
3131
return result.map((user) => ({ label: user.name, value: user.id }))
3232
}),
3333
})),

examples/erp/drizzlify/collections/users.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { builder } from '../helper'
22

3-
export const usersCollection = builder.collection('users', {
3+
export const usersCollection = builder.collection('user', {
44
slug: 'users',
55
identifierColumn: 'id',
6-
fields: builder.fields('users', (fb) => ({
6+
fields: builder.fields('user', (fb) => ({
77
id: fb.columns('id', {
88
type: 'text',
99
}),

packages/core/src/utils.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@ import { is, Table } from 'drizzle-orm'
33
import type { IsNever, Simplify, ValueOf } from 'type-fest'
44
import type { ZodError, ZodObject, ZodOptional, ZodType } from 'zod'
55

6-
import type { MaybePromise } from './collection'
7-
import type { ApiHttpStatus, ApiRouteHandlerPayloadWithContext, ApiRouteSchema } from './endpoint'
6+
import type {
7+
ApiHttpStatus,
8+
ApiRouteHandler,
9+
ApiRouteHandlerPayloadWithContext,
10+
ApiRouteSchema,
11+
} from './endpoint'
812
import type { Field, FieldRelation, Fields, FieldsInitial, FieldsWithFieldName } from './field'
913

1014
export function isRelationField(field: Field): field is FieldRelation {
@@ -184,11 +188,11 @@ export function withValidator<
184188
TContext extends Record<string, unknown>,
185189
>(
186190
schema: TApiRouteSchema,
187-
handler: (
191+
handler: ApiRouteHandler<TContext, TApiRouteSchema>
192+
): ApiRouteHandler<TContext, TApiRouteSchema> {
193+
const wrappedHandler = async (
188194
payload: ApiRouteHandlerPayloadWithContext<TApiRouteSchema, TContext>
189-
) => MaybePromise<any>
190-
): (payload: ApiRouteHandlerPayloadWithContext<TApiRouteSchema, TContext>) => MaybePromise<any> {
191-
return async (payload: ApiRouteHandlerPayloadWithContext<TApiRouteSchema, TContext>) => {
195+
) => {
192196
const zodErrors = await validateRequestBody(schema, payload)
193197
if (zodErrors) {
194198
return {
@@ -202,7 +206,11 @@ export function withValidator<
202206

203207
const response = await handler(payload)
204208

205-
const validationError = validateResponseBody(schema, response.status, response.body)
209+
const validationError = validateResponseBody(
210+
schema,
211+
response.status as ApiHttpStatus,
212+
response.body
213+
)
206214

207215
if (validationError) {
208216
return {
@@ -216,6 +224,8 @@ export function withValidator<
216224

217225
return response
218226
}
227+
228+
return wrappedHandler as ApiRouteHandler<TContext, TApiRouteSchema>
219229
}
220230

221231
export type JoinArrays<T extends any[]> = Simplify<

0 commit comments

Comments
 (0)