rouzer 1.0.0-beta.6 → 1.0.0-beta.8

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.
@@ -29,7 +29,7 @@ export declare function createClient(config: {
29
29
  */
30
30
  onJsonError?: (response: Response) => Promisable<Response>;
31
31
  };
32
- request<T extends RouteRequest>({ pathPattern, method, args: { path, query, body, headers }, route, }: T): Promise<Response & {
32
+ request<T extends RouteRequest>({ path: pathBuilder, method, args: { path, query, body, headers }, route, }: T): Promise<Response & {
33
33
  json(): Promise<T["$result"]>;
34
34
  }>;
35
35
  json<T extends RouteRequest>(request: T): Promise<T["$result"]>;
@@ -3,12 +3,19 @@ export function createClient(config) {
3
3
  const baseURL = config.baseURL.replace(/\/$/, '');
4
4
  return {
5
5
  config,
6
- request({ pathPattern, method, args: { path, query, body, headers }, route, }) {
6
+ request({ path: pathBuilder, method, args: { path, query, body, headers }, route, }) {
7
7
  if (route.path) {
8
8
  path = route.path.parse(path);
9
9
  }
10
- const url = new URL(baseURL);
11
- url.pathname += pathPattern.href(path);
10
+ let url;
11
+ const href = pathBuilder.href(path);
12
+ if (href[0] === '/') {
13
+ url = new URL(baseURL);
14
+ url.pathname += pathBuilder.href(path);
15
+ }
16
+ else {
17
+ url = new URL(href);
18
+ }
12
19
  if (route.query) {
13
20
  query = route.query.parse(query ?? {});
14
21
  url.search = new URLSearchParams(query).toString();
package/dist/route.d.ts CHANGED
@@ -1,8 +1,7 @@
1
1
  import { RoutePattern } from '@remix-run/route-pattern';
2
- import type { MutationRoute, QueryRoute, RouteFunction, Routes, Unchecked } from './types.js';
2
+ import type { MutationMethod, QueryMethod, RouteFunction, RouteMethods, Unchecked } from './types.js';
3
3
  export declare function $type<T>(): Unchecked<T>;
4
- export declare function route<P extends string, T extends Routes>(path: P, routes: T): {
5
- path: P;
6
- pathPattern: RoutePattern<string>;
7
- routes: T;
8
- } & { [K in keyof T]: RouteFunction<Extract<T[K], MutationRoute | QueryRoute>>; };
4
+ export declare function route<P extends string, T extends RouteMethods>(pattern: P, methods: T): {
5
+ path: RoutePattern<P>;
6
+ methods: T;
7
+ } & { [K in keyof T]: RouteFunction<Extract<T[K], MutationMethod | QueryMethod>>; };
package/dist/route.js CHANGED
@@ -3,16 +3,16 @@ import { mapEntries } from './common.js';
3
3
  export function $type() {
4
4
  return null;
5
5
  }
6
- export function route(path, routes) {
7
- const pathPattern = new RoutePattern(path);
6
+ export function route(pattern, methods) {
7
+ const path = new RoutePattern(pattern);
8
8
  const createFetch = (method, route) => (args) => {
9
9
  return {
10
10
  route,
11
- pathPattern,
11
+ path,
12
12
  method,
13
13
  args,
14
14
  $result: undefined,
15
15
  };
16
16
  };
17
- return Object.assign({ path, pathPattern, routes }, mapEntries(routes, (method, route) => [method, createFetch(method, route)]));
17
+ return Object.assign({ path, methods }, mapEntries(methods, (method, route) => [method, createFetch(method, route)]));
18
18
  }
@@ -2,7 +2,7 @@ import type { AdapterRequestContext } from '@hattip/core';
2
2
  import { type Params } from '@remix-run/route-pattern';
3
3
  import { chain, MiddlewareChain, type MiddlewareContext } from 'alien-middleware';
4
4
  import * as z from 'zod/mini';
5
- import type { InferRouteResponse, MutationRoute, Promisable, QueryRoute, Routes } from '../types.js';
5
+ import type { InferRouteResponse, MutationMethod, Promisable, QueryMethod, Routes } from '../types.js';
6
6
  export { chain };
7
7
  type EmptyMiddlewareChain<TPlatform = unknown> = MiddlewareChain<{
8
8
  initial: {
@@ -15,31 +15,67 @@ type EmptyMiddlewareChain<TPlatform = unknown> = MiddlewareChain<{
15
15
  };
16
16
  platform: TPlatform;
17
17
  }>;
18
- export type RouterConfig<TRoutes extends Record<string, {
19
- path: string;
18
+ export type RouterConfig = {
19
+ /**
20
+ * Base path to prepend to all routes.
21
+ * @example
22
+ * ```ts
23
+ * basePath: 'api/',
24
+ * ```
25
+ */
26
+ basePath?: string;
27
+ /**
28
+ * Routes to match.
29
+ * @example
30
+ * ```ts
31
+ * // This namespace contains your `route()` declarations.
32
+ * // Pass it to the `createRouter` function.
33
+ * import * as routes from './routes'
34
+ *
35
+ * createRouter({ routes })({
36
+ * // your route handlers...
37
+ * })
38
+ * ```
39
+ */
20
40
  routes: Routes;
21
- }> = any, TMiddleware extends MiddlewareChain = any> = {
22
- routes: TRoutes;
23
- middlewares?: TMiddleware;
41
+ /**
42
+ * Middleware to apply to all routes.
43
+ * @see https://github.com/alien-rpc/alien-middleware#quick-start
44
+ * @example
45
+ * ```ts
46
+ * middlewares: chain().use(ctx => {
47
+ * return {
48
+ * db: postgres(ctx.env('POSTGRES_URL')),
49
+ * }
50
+ * }),
51
+ * ```
52
+ */
53
+ middlewares?: MiddlewareChain;
54
+ /**
55
+ * Enable debugging features.
56
+ * - When a handler throws an error, include its message in the response body.
57
+ * - Throw an error if a handler is not found for a route.
58
+ * @example
59
+ * ```ts
60
+ * debug: process.env.NODE_ENV !== 'production',
61
+ * ```
62
+ */
24
63
  debug?: boolean;
25
64
  };
26
- export declare function createRouter<TRoutes extends Record<string, {
27
- path: string;
28
- routes: Routes;
29
- }>, TMiddleware extends MiddlewareChain = EmptyMiddlewareChain>(config: {
65
+ interface CreateRouterConfig<TRoutes extends Routes, TMiddleware extends MiddlewareChain> extends RouterConfig {
30
66
  routes: TRoutes;
31
67
  middlewares?: TMiddleware;
32
- debug?: boolean;
33
- }): (handlers: { [K in keyof TRoutes]: { [M in keyof TRoutes[K]["routes"]]: TRoutes[K]["routes"][M] extends infer T ? T extends TRoutes[K]["routes"][M] ? T extends QueryRoute ? (context: MiddlewareContext<TMiddleware> & {
68
+ }
69
+ export declare function createRouter<TRoutes extends Routes, TMiddleware extends MiddlewareChain = EmptyMiddlewareChain>(config: CreateRouterConfig<TRoutes, TMiddleware>): (handlers: { [K in keyof TRoutes]: { [M in keyof TRoutes[K]["methods"]]: TRoutes[K]["methods"][M] extends infer T ? T extends TRoutes[K]["methods"][M] ? T extends QueryMethod ? (context: MiddlewareContext<TMiddleware> & {
34
70
  path: T extends {
35
71
  path: any;
36
- } ? z.infer<T["path"]> : Params<TRoutes[K]["path"]>;
72
+ } ? z.infer<T["path"]> : Params<TRoutes[K]["path"]["source"]>;
37
73
  query: z.infer<T["query"]>;
38
74
  headers: z.infer<T["headers"]>;
39
- }) => Promisable<Response | InferRouteResponse<T>> : T extends MutationRoute ? (context: MiddlewareContext<TMiddleware> & {
75
+ }) => Promisable<Response | InferRouteResponse<T>> : T extends MutationMethod ? (context: MiddlewareContext<TMiddleware> & {
40
76
  path: T extends {
41
77
  path: any;
42
- } ? z.infer<T["path"]> : Params<TRoutes[K]["path"]>;
78
+ } ? z.infer<T["path"]> : Params<TRoutes[K]["path"]["source"]>;
43
79
  body: z.infer<T["body"]>;
44
80
  headers: z.infer<T["headers"]>;
45
81
  }) => Promisable<Response | InferRouteResponse<T>> : never : never : never; }; }) => import("alien-middleware").ApplyMiddleware<TMiddleware, (context: AdapterRequestContext<TMiddleware extends MiddlewareChain<infer T extends {
@@ -6,13 +6,14 @@ export { chain };
6
6
  export function createRouter(config) {
7
7
  const keys = Object.keys(config.routes);
8
8
  const middlewares = config.middlewares ?? chain();
9
- const patterns = mapValues(config.routes, ({ path }) => new RoutePattern(path));
9
+ const basePath = config.basePath?.replace(/\/?$/, '/');
10
+ const patterns = mapValues(config.routes, ({ path }) => basePath ? new RoutePattern(path.source.replace(/^\/?/, basePath)) : path);
10
11
  return (handlers) => middlewares.use(async function (context) {
11
12
  const request = context.request;
12
13
  const method = request.method.toUpperCase();
13
14
  const url = (context.url ??= new URL(request.url));
14
15
  for (let i = 0; i < keys.length; i++) {
15
- const route = config.routes[keys[i]].routes[method];
16
+ const route = config.routes[keys[i]].methods[method];
16
17
  if (!route) {
17
18
  continue;
18
19
  }
package/dist/types.d.ts CHANGED
@@ -4,26 +4,32 @@ export type Promisable<T> = T | Promise<T>;
4
4
  export type Unchecked<T> = {
5
5
  __unchecked__: T;
6
6
  };
7
- export type QueryRoute = {
7
+ export type QueryMethod = {
8
8
  path?: z.ZodMiniObject<any>;
9
9
  query?: z.ZodMiniObject<any>;
10
10
  body?: never;
11
11
  headers?: z.ZodMiniObject<any>;
12
12
  response: Unchecked<any>;
13
13
  };
14
- export type MutationRoute = {
14
+ export type MutationMethod = {
15
15
  path?: z.ZodMiniObject<any>;
16
16
  query?: never;
17
17
  body: z.ZodMiniType<any, any>;
18
18
  headers?: z.ZodMiniObject<any>;
19
19
  response?: Unchecked<any>;
20
20
  };
21
+ export type RouteMethods = {
22
+ GET?: QueryMethod;
23
+ POST?: MutationMethod;
24
+ PUT?: MutationMethod;
25
+ PATCH?: MutationMethod;
26
+ DELETE?: MutationMethod;
27
+ };
21
28
  export type Routes = {
22
- GET?: QueryRoute;
23
- POST?: MutationRoute;
24
- PUT?: MutationRoute;
25
- PATCH?: MutationRoute;
26
- DELETE?: MutationRoute;
29
+ [key: string]: {
30
+ path: RoutePattern;
31
+ methods: RouteMethods;
32
+ };
27
33
  };
28
34
  declare class Any {
29
35
  private isAny;
@@ -35,21 +41,21 @@ type PathArgs<T> = T extends {
35
41
  } : {
36
42
  path: TParams;
37
43
  } : unknown : unknown;
38
- type QueryArgs<T> = T extends QueryRoute & {
44
+ type QueryArgs<T> = T extends QueryMethod & {
39
45
  query: infer TQuery;
40
46
  } ? {} extends z.infer<TQuery> ? {
41
47
  query?: z.infer<TQuery>;
42
48
  } : {
43
49
  query: z.infer<TQuery>;
44
50
  } : unknown;
45
- type MutationArgs<T> = T extends MutationRoute & {
51
+ type MutationArgs<T> = T extends MutationMethod & {
46
52
  body: infer TBody;
47
53
  } ? {} extends z.infer<TBody> ? {
48
54
  body?: z.infer<TBody>;
49
55
  } : {
50
56
  body: z.infer<TBody>;
51
57
  } : unknown;
52
- export type RouteArgs<T extends QueryRoute | MutationRoute = any> = ([
58
+ export type RouteArgs<T extends QueryMethod | MutationMethod = any> = ([
53
59
  T
54
60
  ] extends [Any] ? {
55
61
  query?: any;
@@ -59,8 +65,8 @@ export type RouteArgs<T extends QueryRoute | MutationRoute = any> = ([
59
65
  headers?: Record<string, string | undefined>;
60
66
  };
61
67
  export type RouteRequest<TResult = any> = {
62
- route: QueryRoute | MutationRoute;
63
- pathPattern: RoutePattern;
68
+ route: QueryMethod | MutationMethod;
69
+ path: RoutePattern;
64
70
  method: string;
65
71
  args: RouteArgs;
66
72
  $result: TResult;
@@ -68,10 +74,10 @@ export type RouteRequest<TResult = any> = {
68
74
  export type RouteResponse<TResult = any> = Response & {
69
75
  json(): Promise<TResult>;
70
76
  };
71
- export type InferRouteResponse<T extends QueryRoute | MutationRoute> = T extends {
77
+ export type InferRouteResponse<T extends QueryMethod | MutationMethod> = T extends {
72
78
  response: Unchecked<infer TResponse>;
73
79
  } ? TResponse : void;
74
- export type RouteFunction<T extends QueryRoute | MutationRoute> = {
80
+ export type RouteFunction<T extends QueryMethod | MutationMethod> = {
75
81
  (args: RouteArgs<T>): RouteRequest<InferRouteResponse<T>>;
76
82
  $args: RouteArgs<T>;
77
83
  $response: InferRouteResponse<T>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rouzer",
3
- "version": "1.0.0-beta.6",
3
+ "version": "1.0.0-beta.8",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {