rouzer 1.0.0-beta.6 → 1.0.0-beta.7

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