rouzer 1.0.0-beta.16 → 1.0.0-beta.18

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.
@@ -24,7 +24,7 @@ export declare function createClient<TRoutes extends Record<string, Route> = Rec
24
24
  * response is always parsed as JSON, regardless of the HTTP status code.
25
25
  */
26
26
  onJsonError?: (response: Response) => Promisable<Response>;
27
- }): { [K in keyof TRoutes]: TRoutes[K]["methods"] extends infer TMethods ? { [M in keyof TMethods]: RouteRequestFunction<Extract<TMethods[M], RouteSchema>>; } : never; } & {
27
+ }): { [K in keyof TRoutes]: TRoutes[K]["methods"] extends infer TMethods ? { [M in keyof TMethods]: RouteRequestFunction<Extract<TMethods[M], RouteSchema>, TRoutes[K]["path"]["source"]>; } : never; } & {
28
28
  config: {
29
29
  /**
30
30
  * Base URL to use for all requests.
@@ -54,7 +54,7 @@ export declare function createClient<TRoutes extends Record<string, Route> = Rec
54
54
  }>;
55
55
  json: <T extends RouteRequest>(props: T) => Promise<T["$result"]>;
56
56
  };
57
- type RouteRequestFunction<T extends RouteSchema> = (args: RouteArgs<T>) => Promise<T extends {
57
+ type RouteRequestFunction<T extends RouteSchema, P extends string> = (args: RouteArgs<T, P>) => Promise<T extends {
58
58
  response: any;
59
59
  } ? InferRouteResponse<T> : Response>;
60
60
  export {};
package/dist/route.d.ts CHANGED
@@ -5,6 +5,6 @@ export type Route<P extends string = string, T extends RouteSchemaMap = RouteSch
5
5
  path: RoutePattern<P>;
6
6
  methods: T;
7
7
  } & {
8
- [K in keyof T]: RouteFunction<Extract<T[K], RouteSchema>>;
8
+ [K in keyof T]: RouteFunction<Extract<T[K], RouteSchema>, P>;
9
9
  };
10
10
  export declare function route<P extends string, T extends RouteSchemaMap>(pattern: P, methods: T): Route<P, T>;
@@ -86,14 +86,22 @@ export declare function createRouter<TRoutes extends Routes, TMiddleware extends
86
86
  path: T extends {
87
87
  path: any;
88
88
  } ? z.infer<T["path"]> : Params<TRoutes[K]["path"]["source"]>;
89
- query: z.infer<T["query"]>;
90
- headers: z.infer<T["headers"]>;
89
+ query: T extends {
90
+ query: any;
91
+ } ? z.infer<T["query"]> : undefined;
92
+ headers: T extends {
93
+ headers: any;
94
+ } ? z.infer<T["headers"]> : undefined;
91
95
  }) => Promisable<Response | InferRouteResponse<T>> : T extends MutationRouteSchema ? (context: MiddlewareContext<TMiddleware> & {
92
96
  path: T extends {
93
97
  path: any;
94
98
  } ? z.infer<T["path"]> : Params<TRoutes[K]["path"]["source"]>;
95
- body: z.infer<T["body"]>;
96
- headers: z.infer<T["headers"]>;
99
+ body: T extends {
100
+ body: any;
101
+ } ? z.infer<T["body"]> : undefined;
102
+ headers: T extends {
103
+ headers: any;
104
+ } ? z.infer<T["headers"]> : undefined;
97
105
  }) => Promisable<Response | InferRouteResponse<T>> : never : never : never; }; }) => import("alien-middleware").ApplyMiddleware<TMiddleware, (context: HattipContext<TMiddleware extends MiddlewareChain<infer T extends {
98
106
  initial: {
99
107
  env: object;
package/dist/types.d.ts CHANGED
@@ -36,13 +36,17 @@ export type Routes = {
36
36
  declare class Any {
37
37
  private isAny;
38
38
  }
39
- type PathArgs<T> = T extends {
40
- path: infer TPath extends string;
41
- } ? Params<TPath> extends infer TParams ? {} extends TParams ? {
39
+ type PathArgs<T, P extends string> = T extends {
40
+ path: infer TPath;
41
+ } ? {} extends z.infer<TPath> ? {
42
+ path?: z.infer<TPath>;
43
+ } : {
44
+ path: z.infer<TPath>;
45
+ } : Params<P> extends infer TParams ? {} extends TParams ? {
42
46
  path?: TParams;
43
47
  } : {
44
48
  path: TParams;
45
- } : unknown : unknown;
49
+ } : unknown;
46
50
  type QueryArgs<T> = T extends QueryRouteSchema & {
47
51
  query: infer TQuery;
48
52
  } ? {} extends z.infer<TQuery> ? {
@@ -50,18 +54,20 @@ type QueryArgs<T> = T extends QueryRouteSchema & {
50
54
  } : {
51
55
  query: z.infer<TQuery>;
52
56
  } : unknown;
53
- type MutationArgs<T> = T extends MutationRouteSchema & {
57
+ type MutationArgs<T> = T extends MutationRouteSchema ? T extends {
54
58
  body: infer TBody;
55
59
  } ? {} extends z.infer<TBody> ? {
56
60
  body?: z.infer<TBody>;
57
61
  } : {
58
62
  body: z.infer<TBody>;
63
+ } : {
64
+ body?: unknown;
59
65
  } : unknown;
60
- export type RouteArgs<T extends RouteSchema = any> = ([T] extends [Any] ? {
66
+ export type RouteArgs<T extends RouteSchema = any, P extends string = string> = ([T] extends [Any] ? {
61
67
  query?: any;
62
68
  body?: any;
63
69
  path?: any;
64
- } : QueryArgs<T> & MutationArgs<T> & PathArgs<T>) & Omit<RequestInit, 'method' | 'body' | 'headers'> & {
70
+ } : QueryArgs<T> & MutationArgs<T> & PathArgs<T, P>) & Omit<RequestInit, 'method' | 'body' | 'headers'> & {
65
71
  headers?: Record<string, string | undefined>;
66
72
  };
67
73
  export type RouteRequest<TResult = any> = {
@@ -77,9 +83,9 @@ export type RouteResponse<TResult = any> = Response & {
77
83
  export type InferRouteResponse<T extends RouteSchema> = T extends {
78
84
  response: Unchecked<infer TResponse>;
79
85
  } ? TResponse : void;
80
- export type RouteFunction<T extends RouteSchema> = {
81
- (args: RouteArgs<T>): RouteRequest<InferRouteResponse<T>>;
82
- $args: RouteArgs<T>;
86
+ export type RouteFunction<T extends RouteSchema, P extends string> = {
87
+ (args: RouteArgs<T, P>): RouteRequest<InferRouteResponse<T>>;
88
+ $args: RouteArgs<T, P>;
83
89
  $response: InferRouteResponse<T>;
84
90
  };
85
91
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rouzer",
3
- "version": "1.0.0-beta.16",
3
+ "version": "1.0.0-beta.18",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {