better-call 0.2.13-beta.3 → 0.2.13-beta.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,42 +1,9 @@
1
- import { ZodSchema, ZodError } from 'zod';
1
+ import { ZodTypeAny, z, ZodSchema, ZodObject, ZodOptional, ZodError } from 'zod';
2
+ import { ZodOpenApiFullMetadata, ZodOpenAPIMetadata } from '@asteasolutions/zod-to-openapi/dist/zod-extensions';
3
+ import { ResponseConfig } from '@asteasolutions/zod-to-openapi';
4
+ import * as _asteasolutions_zod_to_openapi_dist_openapi_registry from '@asteasolutions/zod-to-openapi/dist/openapi-registry';
2
5
  import { BufferSource } from 'stream/web';
3
6
 
4
- type Cookie = Record<string, string>;
5
- type SignedCookie = Record<string, string | false>;
6
- type PartitionCookieConstraint = {
7
- partition: true;
8
- secure: true;
9
- } | {
10
- partition?: boolean;
11
- secure?: boolean;
12
- };
13
- type SecureCookieConstraint = {
14
- secure: true;
15
- };
16
- type HostCookieConstraint = {
17
- secure: true;
18
- path: "/";
19
- domain?: undefined;
20
- };
21
- type CookieOptions = {
22
- domain?: string;
23
- expires?: Date;
24
- httpOnly?: boolean;
25
- maxAge?: number;
26
- path?: string;
27
- secure?: boolean;
28
- signingSecret?: string;
29
- sameSite?: "Strict" | "Lax" | "None" | "strict" | "lax" | "none";
30
- partitioned?: boolean;
31
- prefix?: CookiePrefixOptions;
32
- } & PartitionCookieConstraint;
33
- type CookiePrefixOptions = "host" | "secure";
34
- type CookieConstraint<Name> = Name extends `__Secure-${string}` ? CookieOptions & SecureCookieConstraint : Name extends `__Host-${string}` ? CookieOptions & HostCookieConstraint : CookieOptions;
35
- declare const parse: (cookie: string, name?: string) => Cookie;
36
- declare const parseSigned: (cookie: string, secret: string | BufferSource, name?: string) => Promise<SignedCookie>;
37
- declare const serialize: <Name extends string>(name: Name, value: string, opt?: CookieConstraint<Name>) => string;
38
- declare const serializeSigned: (name: string, value: string, secret: string | BufferSource, opt?: CookieOptions) => Promise<string>;
39
-
40
7
  /**
41
8
  * Improve this type if possible
42
9
  */
@@ -55,6 +22,15 @@ type Prettify<T> = {
55
22
  type IsEmptyObject<T> = keyof T extends never ? true : false;
56
23
  type UnionToIntersection<Union> = (Union extends unknown ? (distributedUnion: Union) => void : never) extends (mergedIntersection: infer Intersection) => void ? Intersection & Union : never;
57
24
 
25
+ declare module "zod" {
26
+ interface ZodTypeDef {
27
+ openapi?: ZodOpenApiFullMetadata;
28
+ }
29
+ interface ZodType<Output = any, Def extends ZodTypeDef = ZodTypeDef, Input = Output> {
30
+ openapi<T extends ZodTypeAny>(this: T, metadata: Partial<ZodOpenAPIMetadata<z.input<T>>>): T;
31
+ openapi<T extends ZodTypeAny>(this: T, refId: string, metadata?: Partial<ZodOpenAPIMetadata<z.input<T>>>): T;
32
+ }
33
+ }
58
34
  type Method = "GET" | "POST" | "PUT" | "DELETE" | "*";
59
35
  interface EndpointOptions {
60
36
  /**
@@ -68,7 +44,7 @@ interface EndpointOptions {
68
44
  /**
69
45
  * Query Schema
70
46
  */
71
- query?: ZodSchema;
47
+ query?: ZodObject<any> | ZodOptional<ZodObject<any>>;
72
48
  /**
73
49
  * If true headers will be required to be passed in the context
74
50
  */
@@ -85,6 +61,14 @@ interface EndpointOptions {
85
61
  * Middleware to use
86
62
  */
87
63
  use?: Endpoint[];
64
+ /**
65
+ * OpenAPI metadata
66
+ */
67
+ openAPI?: {
68
+ responses: {
69
+ [statusCode: string]: ResponseConfig;
70
+ };
71
+ };
88
72
  }
89
73
  type InferBody<Options extends EndpointOptions> = Options["body"] extends ZodSchema<infer T> ? T : never;
90
74
  type InferQuery<Options extends EndpointOptions> = Options["query"] extends ZodSchema<infer T> ? T : never;
@@ -104,6 +88,42 @@ type InferHeaders<Option extends EndpointOptions> = Option["requireHeaders"] ext
104
88
  type InferUse<Opts extends EndpointOptions["use"]> = Opts extends Endpoint[] ? UnionToIntersection<Awaited<ReturnType<Opts[number]>>> : never;
105
89
  type InferMethod<Options extends EndpointOptions> = Options["method"] extends Array<Method> ? Options["method"][number] : Options["method"];
106
90
 
91
+ type Cookie = Record<string, string>;
92
+ type SignedCookie = Record<string, string | false>;
93
+ type PartitionCookieConstraint = {
94
+ partition: true;
95
+ secure: true;
96
+ } | {
97
+ partition?: boolean;
98
+ secure?: boolean;
99
+ };
100
+ type SecureCookieConstraint = {
101
+ secure: true;
102
+ };
103
+ type HostCookieConstraint = {
104
+ secure: true;
105
+ path: "/";
106
+ domain?: undefined;
107
+ };
108
+ type CookieOptions = {
109
+ domain?: string;
110
+ expires?: Date;
111
+ httpOnly?: boolean;
112
+ maxAge?: number;
113
+ path?: string;
114
+ secure?: boolean;
115
+ signingSecret?: string;
116
+ sameSite?: "Strict" | "Lax" | "None" | "strict" | "lax" | "none";
117
+ partitioned?: boolean;
118
+ prefix?: CookiePrefixOptions;
119
+ } & PartitionCookieConstraint;
120
+ type CookiePrefixOptions = "host" | "secure";
121
+ type CookieConstraint<Name> = Name extends `__Secure-${string}` ? CookieOptions & SecureCookieConstraint : Name extends `__Host-${string}` ? CookieOptions & HostCookieConstraint : CookieOptions;
122
+ declare const parse: (cookie: string, name?: string) => Cookie;
123
+ declare const parseSigned: (cookie: string, secret: string | BufferSource, name?: string) => Promise<SignedCookie>;
124
+ declare const serialize: <Name extends string>(name: Name, value: string, opt?: CookieConstraint<Name>) => string;
125
+ declare const serializeSigned: (name: string, value: string, secret: string | BufferSource, opt?: CookieOptions) => Promise<string>;
126
+
107
127
  declare class APIError extends Error {
108
128
  message: string;
109
129
  status: number;
@@ -257,21 +277,6 @@ type EndpointResponse = JSONResponse | Response | void | Record<string, any>;
257
277
  type InferResponse<Ctx, R> = Ctx extends {
258
278
  asResponse: true;
259
279
  } ? Response : R extends JSONResponse<infer T> ? T : R;
260
- declare function createJSON({ asResponse }: {
261
- asResponse?: boolean;
262
- }): <R extends Record<string, any>>(json: R, routerResponse?: {
263
- status?: number;
264
- headers?: Record<string, string>;
265
- response?: Response;
266
- }) => Promise<R | {
267
- body: R;
268
- routerResponse: {
269
- status?: number;
270
- headers?: Record<string, string>;
271
- response?: Response;
272
- } | undefined;
273
- _flag: string;
274
- }>;
275
280
  type ValidationResponse = {
276
281
  data: {
277
282
  body: any;
@@ -289,10 +294,282 @@ declare function fromError(error: ZodError): {
289
294
  message: string;
290
295
  };
291
296
 
292
- declare const createEndpoint: <Path extends string, Options extends EndpointOptions, R extends EndpointResponse>(path: Path, options: Options, handler: (context: EndpointContext<Path, Options>) => Promise<R>) => {
293
- <Ctx extends Context<Path, Options>>(...inputCtx: HasRequiredKeys<Ctx> extends true ? [Ctx] : [Ctx?]): Promise<Ctx["asResponse"] extends true ? Response : R>;
297
+ declare const createEndpoint: {
298
+ <Path extends string, Options extends EndpointOptions, R extends EndpointResponse>(path: Path, options: Options, handler: (context: EndpointContext<Path, Options>) => Promise<R>): {
299
+ <Ctx extends Context<Path, Options>>(...inputCtx: HasRequiredKeys<Ctx> extends true ? [Ctx] : [Ctx?]): Promise<Ctx["asResponse"] extends true ? Response : R>;
300
+ path: Path;
301
+ options: Options;
302
+ openAPI: {
303
+ definitions: _asteasolutions_zod_to_openapi_dist_openapi_registry.OpenAPIDefinitions[];
304
+ };
305
+ };
306
+ creator: typeof createEndpointCreator;
307
+ };
308
+ declare function createEndpointCreator<E extends {
309
+ use: Endpoint[];
310
+ }>(opts: E): <Path extends string, Opts extends EndpointOptions, R extends EndpointResponse>(path: Path, options: Opts, handler: <InferE extends EndpointContext<Path, Opts>>(ctx: Omit<InferE, "context"> & {
311
+ context: InferUse<E["use"]> & (InferE["context"] extends never ? {} : InferE["context"]);
312
+ }) => Promise<R>) => {
313
+ <Ctx extends ({
314
+ body: InferBody<Opts & {
315
+ use: Endpoint[];
316
+ }>;
317
+ method?: InferMethod<Opts & {
318
+ use: Endpoint[];
319
+ }> | undefined;
320
+ query: InferQuery<Opts & {
321
+ use: Endpoint[];
322
+ }>;
323
+ params: InferParam<Path>;
324
+ request: InferRequest<Opts & {
325
+ use: Endpoint[];
326
+ }>;
327
+ headers: InferHeaders<Opts & {
328
+ use: Endpoint[];
329
+ }>;
330
+ asResponse?: boolean;
331
+ } extends infer T_1 ? { [K_1 in keyof T_1 as {
332
+ body: InferBody<Opts & {
333
+ use: Endpoint[];
334
+ }>;
335
+ method?: InferMethod<Opts & {
336
+ use: Endpoint[];
337
+ }> | undefined;
338
+ query: InferQuery<Opts & {
339
+ use: Endpoint[];
340
+ }>;
341
+ params: InferParam<Path>;
342
+ request: InferRequest<Opts & {
343
+ use: Endpoint[];
344
+ }>;
345
+ headers: InferHeaders<Opts & {
346
+ use: Endpoint[];
347
+ }>;
348
+ asResponse?: boolean;
349
+ }[K_1] extends never ? never : undefined extends {
350
+ body: InferBody<Opts & {
351
+ use: Endpoint[];
352
+ }>;
353
+ method?: InferMethod<Opts & {
354
+ use: Endpoint[];
355
+ }> | undefined;
356
+ query: InferQuery<Opts & {
357
+ use: Endpoint[];
358
+ }>;
359
+ params: InferParam<Path>;
360
+ request: InferRequest<Opts & {
361
+ use: Endpoint[];
362
+ }>;
363
+ headers: InferHeaders<Opts & {
364
+ use: Endpoint[];
365
+ }>;
366
+ asResponse?: boolean;
367
+ }[K_1] ? never : K_1]: {
368
+ body: InferBody<Opts & {
369
+ use: Endpoint[];
370
+ }>;
371
+ method?: InferMethod<Opts & {
372
+ use: Endpoint[];
373
+ }> | undefined;
374
+ query: InferQuery<Opts & {
375
+ use: Endpoint[];
376
+ }>;
377
+ params: InferParam<Path>;
378
+ request: InferRequest<Opts & {
379
+ use: Endpoint[];
380
+ }>;
381
+ headers: InferHeaders<Opts & {
382
+ use: Endpoint[];
383
+ }>;
384
+ asResponse?: boolean;
385
+ }[K_1]; } : never) & ({
386
+ body: InferBody<Opts & {
387
+ use: Endpoint[];
388
+ }>;
389
+ method?: InferMethod<Opts & {
390
+ use: Endpoint[];
391
+ }> | undefined;
392
+ query: InferQuery<Opts & {
393
+ use: Endpoint[];
394
+ }>;
395
+ params: InferParam<Path>;
396
+ request: InferRequest<Opts & {
397
+ use: Endpoint[];
398
+ }>;
399
+ headers: InferHeaders<Opts & {
400
+ use: Endpoint[];
401
+ }>;
402
+ asResponse?: boolean;
403
+ } extends infer T_2 ? { [K_2 in keyof T_2 as undefined extends {
404
+ body: InferBody<Opts & {
405
+ use: Endpoint[];
406
+ }>;
407
+ method?: InferMethod<Opts & {
408
+ use: Endpoint[];
409
+ }> | undefined;
410
+ query: InferQuery<Opts & {
411
+ use: Endpoint[];
412
+ }>;
413
+ params: InferParam<Path>;
414
+ request: InferRequest<Opts & {
415
+ use: Endpoint[];
416
+ }>;
417
+ headers: InferHeaders<Opts & {
418
+ use: Endpoint[];
419
+ }>;
420
+ asResponse?: boolean;
421
+ }[K_2] ? K_2 : never]?: {
422
+ body: InferBody<Opts & {
423
+ use: Endpoint[];
424
+ }>;
425
+ method?: InferMethod<Opts & {
426
+ use: Endpoint[];
427
+ }> | undefined;
428
+ query: InferQuery<Opts & {
429
+ use: Endpoint[];
430
+ }>;
431
+ params: InferParam<Path>;
432
+ request: InferRequest<Opts & {
433
+ use: Endpoint[];
434
+ }>;
435
+ headers: InferHeaders<Opts & {
436
+ use: Endpoint[];
437
+ }>;
438
+ asResponse?: boolean;
439
+ }[K_2] | undefined; } : never) extends infer T ? { [K in keyof T]: (({
440
+ body: InferBody<Opts & {
441
+ use: Endpoint[];
442
+ }>;
443
+ method?: InferMethod<Opts & {
444
+ use: Endpoint[];
445
+ }> | undefined;
446
+ query: InferQuery<Opts & {
447
+ use: Endpoint[];
448
+ }>;
449
+ params: InferParam<Path>;
450
+ request: InferRequest<Opts & {
451
+ use: Endpoint[];
452
+ }>;
453
+ headers: InferHeaders<Opts & {
454
+ use: Endpoint[];
455
+ }>;
456
+ asResponse?: boolean;
457
+ } extends infer T_1 ? { [K_1 in keyof T_1 as {
458
+ body: InferBody<Opts & {
459
+ use: Endpoint[];
460
+ }>;
461
+ method?: InferMethod<Opts & {
462
+ use: Endpoint[];
463
+ }> | undefined;
464
+ query: InferQuery<Opts & {
465
+ use: Endpoint[];
466
+ }>;
467
+ params: InferParam<Path>;
468
+ request: InferRequest<Opts & {
469
+ use: Endpoint[];
470
+ }>;
471
+ headers: InferHeaders<Opts & {
472
+ use: Endpoint[];
473
+ }>;
474
+ asResponse?: boolean;
475
+ }[K_1] extends never ? never : undefined extends {
476
+ body: InferBody<Opts & {
477
+ use: Endpoint[];
478
+ }>;
479
+ method?: InferMethod<Opts & {
480
+ use: Endpoint[];
481
+ }> | undefined;
482
+ query: InferQuery<Opts & {
483
+ use: Endpoint[];
484
+ }>;
485
+ params: InferParam<Path>;
486
+ request: InferRequest<Opts & {
487
+ use: Endpoint[];
488
+ }>;
489
+ headers: InferHeaders<Opts & {
490
+ use: Endpoint[];
491
+ }>;
492
+ asResponse?: boolean;
493
+ }[K_1] ? never : K_1]: {
494
+ body: InferBody<Opts & {
495
+ use: Endpoint[];
496
+ }>;
497
+ method?: InferMethod<Opts & {
498
+ use: Endpoint[];
499
+ }> | undefined;
500
+ query: InferQuery<Opts & {
501
+ use: Endpoint[];
502
+ }>;
503
+ params: InferParam<Path>;
504
+ request: InferRequest<Opts & {
505
+ use: Endpoint[];
506
+ }>;
507
+ headers: InferHeaders<Opts & {
508
+ use: Endpoint[];
509
+ }>;
510
+ asResponse?: boolean;
511
+ }[K_1]; } : never) & ({
512
+ body: InferBody<Opts & {
513
+ use: Endpoint[];
514
+ }>;
515
+ method?: InferMethod<Opts & {
516
+ use: Endpoint[];
517
+ }> | undefined;
518
+ query: InferQuery<Opts & {
519
+ use: Endpoint[];
520
+ }>;
521
+ params: InferParam<Path>;
522
+ request: InferRequest<Opts & {
523
+ use: Endpoint[];
524
+ }>;
525
+ headers: InferHeaders<Opts & {
526
+ use: Endpoint[];
527
+ }>;
528
+ asResponse?: boolean;
529
+ } extends infer T_2 ? { [K_2 in keyof T_2 as undefined extends {
530
+ body: InferBody<Opts & {
531
+ use: Endpoint[];
532
+ }>;
533
+ method?: InferMethod<Opts & {
534
+ use: Endpoint[];
535
+ }> | undefined;
536
+ query: InferQuery<Opts & {
537
+ use: Endpoint[];
538
+ }>;
539
+ params: InferParam<Path>;
540
+ request: InferRequest<Opts & {
541
+ use: Endpoint[];
542
+ }>;
543
+ headers: InferHeaders<Opts & {
544
+ use: Endpoint[];
545
+ }>;
546
+ asResponse?: boolean;
547
+ }[K_2] ? K_2 : never]?: {
548
+ body: InferBody<Opts & {
549
+ use: Endpoint[];
550
+ }>;
551
+ method?: InferMethod<Opts & {
552
+ use: Endpoint[];
553
+ }> | undefined;
554
+ query: InferQuery<Opts & {
555
+ use: Endpoint[];
556
+ }>;
557
+ params: InferParam<Path>;
558
+ request: InferRequest<Opts & {
559
+ use: Endpoint[];
560
+ }>;
561
+ headers: InferHeaders<Opts & {
562
+ use: Endpoint[];
563
+ }>;
564
+ asResponse?: boolean;
565
+ }[K_2] | undefined; } : never))[K]; } : never>(...inputCtx: HasRequiredKeys<Ctx> extends true ? [Ctx] : [(Ctx | undefined)?]): Promise<Ctx["asResponse"] extends true ? Response : R>;
294
566
  path: Path;
295
- options: Options;
567
+ options: Opts & {
568
+ use: Endpoint[];
569
+ };
570
+ openAPI: {
571
+ definitions: _asteasolutions_zod_to_openapi_dist_openapi_registry.OpenAPIDefinitions[];
572
+ };
296
573
  };
297
574
  type Endpoint<Handler extends (ctx: any) => Promise<any> = (ctx: any) => Promise<any>, Options extends EndpointOptions = EndpointOptions> = {
298
575
  path: string;
@@ -338,4 +615,4 @@ declare const createRouter: <E extends Record<string, Endpoint>, Config extends
338
615
  };
339
616
  type Router = ReturnType<typeof createRouter>;
340
617
 
341
- export { type CookiePrefixOptions as C, type EndpointOptions as E, type HasRequiredKeys as H, type Input as I, type JSONResponse as J, type Prettify as P, type Router as R, type SignedCookie as S, type UnionToIntersection as U, type EndpointResponse as a, type Endpoint as b, type EndpointContext as c, type CookieOptions as d, createEndpoint as e, createRouter as f, type RequiredKeysOf as g, type IsEmptyObject as h, type Context as i, createSetHeader as j, createGetHeader as k, type InferResponse as l, createJSON as m, fromError as n, type Cookie as o, type CookieConstraint as p, parse as q, runValidation as r, parseSigned as s, serialize as t, serializeSigned as u };
618
+ export { type CookiePrefixOptions as C, type EndpointOptions as E, type HasRequiredKeys as H, type InferUse as I, type JSONResponse as J, type Prettify as P, type Router as R, type SignedCookie as S, type UnionToIntersection as U, type EndpointResponse as a, type Endpoint as b, type EndpointContext as c, type CookieOptions as d, createEndpoint as e, createRouter as f, type Input as g, type RequiredKeysOf as h, type IsEmptyObject as i, type Context as j, createSetHeader as k, createGetHeader as l, type InferResponse as m, fromError as n, type Cookie as o, type CookieConstraint as p, parse as q, runValidation as r, parseSigned as s, serialize as t, serializeSigned as u };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "better-call",
3
- "version": "0.2.13-beta.3",
3
+ "version": "0.2.13-beta.4",
4
4
  "description": "",
5
5
  "main": "./dist/index.cjs",
6
6
  "types": "./dist/index.d.ts",
@@ -29,6 +29,7 @@
29
29
  "zod": "^3.23.8"
30
30
  },
31
31
  "dependencies": {
32
+ "@asteasolutions/zod-to-openapi": "^7.2.0",
32
33
  "rou3": "^0.5.1",
33
34
  "uncrypto": "^0.1.3"
34
35
  },