Skip to content

Commit 1155344

Browse files
authored
Merge branch 'main' into update/endpoint/custom-validation-error
2 parents 6172051 + 5ea5ddf commit 1155344

6 files changed

Lines changed: 639 additions & 24 deletions

File tree

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "better-call",
3-
"version": "1.0.12",
3+
"version": "1.0.13",
44
"packageManager": "pnpm@9.15.0",
55
"type": "module",
66
"repository": {
@@ -34,7 +34,7 @@
3434
"typescript": "^5.6.0-beta",
3535
"valibot": "1.0.0-beta.15",
3636
"vitest": "^3.1.1",
37-
"zod": "^3.24.1"
37+
"zod": "^4.0.1"
3838
},
3939
"dependencies": {
4040
"@better-fetch/fetch": "^1.1.4",

pnpm-lock.yaml

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/endpoint.test.ts

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ describe("types", async () => {
143143
"/path",
144144
{
145145
method: "POST",
146-
body: z.record(z.string()),
146+
body: z.record(z.string(), z.string()),
147147
metadata: {
148148
$Infer: {
149149
body: {} as {
@@ -206,7 +206,7 @@ describe("types", async () => {
206206
"/path",
207207
{
208208
method: "POST",
209-
body: z.record(z.string()),
209+
body: z.record(z.string(), z.string()),
210210
metadata: {
211211
$Infer: {
212212
query: {} as {
@@ -366,6 +366,51 @@ describe("response", () => {
366366
test: "response",
367367
});
368368
});
369+
370+
it("should return a js object response with 201 status", async () => {
371+
const endpoint = createEndpoint(
372+
"/path",
373+
{
374+
method: "POST",
375+
status: 201,
376+
},
377+
async (ctx) => {
378+
return ctx.json(
379+
{ test: "response" },
380+
{
381+
status: 201,
382+
},
383+
);
384+
},
385+
);
386+
const response = await endpoint({
387+
asResponse: true,
388+
});
389+
await expect(response.json()).resolves.toMatchObject({
390+
test: "response",
391+
});
392+
expect(response.status).toBe(201);
393+
});
394+
});
395+
396+
it("should return a js object response (asResponse)", async () => {
397+
const endpoint = createEndpoint(
398+
"/path",
399+
{
400+
method: "POST",
401+
status: 200,
402+
},
403+
async (ctx) => {
404+
return ctx.json({ test: "response" });
405+
},
406+
);
407+
const response = await endpoint({
408+
asResponse: true,
409+
});
410+
await expect(response.json()).resolves.toMatchObject({
411+
test: "response",
412+
});
413+
expect(response.status).toBe(200);
369414
});
370415

371416
describe("as-response", () => {

src/openapi.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ZodObject, ZodOptional, ZodSchema } from "zod";
1+
import { ZodObject, ZodOptional, ZodType } from "zod";
22
import type { Endpoint, EndpointOptions } from "./endpoint";
33

44
export type OpenAPISchemaType = "string" | "number" | "integer" | "boolean" | "array" | "object";
@@ -82,7 +82,7 @@ export interface Path {
8282
}
8383
const paths: Record<string, Path> = {};
8484

85-
function getTypeFromZodType(zodType: ZodSchema) {
85+
function getTypeFromZodType(zodType: ZodType<any>) {
8686
switch (zodType.constructor.name) {
8787
case "ZodString":
8888
return "string";
@@ -107,7 +107,7 @@ function getParameters(options: EndpointOptions) {
107107
}
108108
if (options.query instanceof ZodObject) {
109109
Object.entries(options.query.shape).forEach(([key, value]) => {
110-
if (value instanceof ZodSchema) {
110+
if (value instanceof ZodObject) {
111111
parameters.push({
112112
name: key,
113113
in: "query",
@@ -139,7 +139,7 @@ function getRequestBody(options: EndpointOptions): any {
139139
const properties: Record<string, any> = {};
140140
const required: string[] = [];
141141
Object.entries(shape).forEach(([key, value]) => {
142-
if (value instanceof ZodSchema) {
142+
if (value instanceof ZodObject) {
143143
properties[key] = {
144144
type: getTypeFromZodType(value),
145145
description: value.description,

0 commit comments

Comments
 (0)