rouzer 1.0.0-beta.7 → 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
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 RouteMethods>(path: P, methods: T): {
5
- path: P;
6
- pathPattern: RoutePattern<string>;
4
+ export declare function route<P extends string, T extends RouteMethods>(pattern: P, methods: T): {
5
+ path: RoutePattern<P>;
7
6
  methods: T;
8
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, methods) {
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, methods }, mapEntries(methods, (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, MutationMethod, Promisable, QueryMethod, RouteMethods } 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: {
@@ -37,10 +37,7 @@ export type RouterConfig = {
37
37
  * })
38
38
  * ```
39
39
  */
40
- routes: Record<string, {
41
- path: string;
42
- methods: RouteMethods;
43
- }>;
40
+ routes: Routes;
44
41
  /**
45
42
  * Middleware to apply to all routes.
46
43
  * @see https://github.com/alien-rpc/alien-middleware#quick-start
@@ -65,22 +62,20 @@ export type RouterConfig = {
65
62
  */
66
63
  debug?: boolean;
67
64
  };
68
- export declare function createRouter<TRoutes extends Record<string, {
69
- path: string;
70
- methods: RouteMethods;
71
- }>, TMiddleware extends MiddlewareChain = EmptyMiddlewareChain>(config: RouterConfig & {
65
+ interface CreateRouterConfig<TRoutes extends Routes, TMiddleware extends MiddlewareChain> extends RouterConfig {
72
66
  routes: TRoutes;
73
67
  middlewares?: 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> & {
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> & {
75
70
  path: T extends {
76
71
  path: any;
77
- } ? z.infer<T["path"]> : Params<TRoutes[K]["path"]>;
72
+ } ? z.infer<T["path"]> : Params<TRoutes[K]["path"]["source"]>;
78
73
  query: z.infer<T["query"]>;
79
74
  headers: z.infer<T["headers"]>;
80
75
  }) => Promisable<Response | InferRouteResponse<T>> : T extends MutationMethod ? (context: MiddlewareContext<TMiddleware> & {
81
76
  path: T extends {
82
77
  path: any;
83
- } ? z.infer<T["path"]> : Params<TRoutes[K]["path"]>;
78
+ } ? z.infer<T["path"]> : Params<TRoutes[K]["path"]["source"]>;
84
79
  body: z.infer<T["body"]>;
85
80
  headers: z.infer<T["headers"]>;
86
81
  }) => Promisable<Response | InferRouteResponse<T>> : never : never : never; }; }) => import("alien-middleware").ApplyMiddleware<TMiddleware, (context: AdapterRequestContext<TMiddleware extends MiddlewareChain<infer T extends {
@@ -6,10 +6,8 @@ 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 basePath = config.basePath?.replace(/(^\/)|(\/$)/, '');
10
- const patterns = mapValues(config.routes, basePath
11
- ? ({ path }) => new RoutePattern(`${basePath}/${path}`)
12
- : ({ 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);
13
11
  return (handlers) => middlewares.use(async function (context) {
14
12
  const request = context.request;
15
13
  const method = request.method.toUpperCase();
package/dist/types.d.ts CHANGED
@@ -25,6 +25,12 @@ export type RouteMethods = {
25
25
  PATCH?: MutationMethod;
26
26
  DELETE?: MutationMethod;
27
27
  };
28
+ export type Routes = {
29
+ [key: string]: {
30
+ path: RoutePattern;
31
+ methods: RouteMethods;
32
+ };
33
+ };
28
34
  declare class Any {
29
35
  private isAny;
30
36
  }
@@ -60,7 +66,7 @@ export type RouteArgs<T extends QueryMethod | MutationMethod = any> = ([
60
66
  };
61
67
  export type RouteRequest<TResult = any> = {
62
68
  route: QueryMethod | MutationMethod;
63
- pathPattern: RoutePattern;
69
+ path: RoutePattern;
64
70
  method: string;
65
71
  args: RouteArgs;
66
72
  $result: TResult;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rouzer",
3
- "version": "1.0.0-beta.7",
3
+ "version": "1.0.0-beta.8",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {