ovr 5.0.0 → 6.0.0
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/LICENSE.md +26 -0
- package/dist/app/index.d.ts +49 -83
- package/dist/app/index.d.ts.map +1 -1
- package/dist/app/index.js +40 -92
- package/dist/context/index.d.ts +138 -0
- package/dist/context/index.d.ts.map +1 -0
- package/dist/context/index.js +218 -0
- package/dist/cookie/index.d.ts +121 -0
- package/dist/cookie/index.d.ts.map +1 -0
- package/dist/cookie/index.js +56 -0
- package/dist/index.d.ts +6 -7
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -6
- package/dist/jsx/{intrinsic-elements.d.ts → elements.d.ts} +14 -7
- package/dist/jsx/elements.d.ts.map +1 -0
- package/dist/jsx/index.d.ts +37 -52
- package/dist/jsx/index.d.ts.map +1 -1
- package/dist/jsx/index.js +71 -200
- package/dist/middleware/index.d.ts +22 -0
- package/dist/middleware/index.d.ts.map +1 -0
- package/dist/multipart/index.d.ts +122 -0
- package/dist/multipart/index.d.ts.map +1 -0
- package/dist/multipart/index.js +341 -0
- package/dist/render/index.d.ts +78 -0
- package/dist/render/index.d.ts.map +1 -0
- package/dist/render/index.js +248 -0
- package/dist/route/index.d.ts +147 -0
- package/dist/route/index.d.ts.map +1 -0
- package/dist/route/index.js +126 -0
- package/dist/trie/index.d.ts +30 -30
- package/dist/trie/index.d.ts.map +1 -1
- package/dist/trie/index.js +67 -82
- package/dist/util/index.d.ts +38 -0
- package/dist/util/index.d.ts.map +1 -0
- package/dist/util/index.js +88 -0
- package/package.json +3 -3
- package/src/app/index.ts +99 -224
- package/src/context/index.ts +262 -0
- package/src/cookie/index.ts +189 -0
- package/src/index.ts +6 -7
- package/src/jsx/{intrinsic-elements.ts → elements.ts} +14 -7
- package/src/jsx/index.ts +81 -223
- package/src/middleware/index.ts +29 -0
- package/src/multipart/index.ts +486 -0
- package/src/render/index.ts +300 -0
- package/src/route/index.ts +332 -0
- package/src/trie/index.ts +87 -111
- package/src/util/index.ts +112 -0
- package/dist/app/context.d.ts +0 -149
- package/dist/app/context.d.ts.map +0 -1
- package/dist/app/context.js +0 -246
- package/dist/app/helper/get.d.ts +0 -30
- package/dist/app/helper/get.d.ts.map +0 -1
- package/dist/app/helper/get.js +0 -33
- package/dist/app/helper/index.d.ts +0 -51
- package/dist/app/helper/index.d.ts.map +0 -1
- package/dist/app/helper/index.js +0 -82
- package/dist/app/helper/post.d.ts +0 -35
- package/dist/app/helper/post.d.ts.map +0 -1
- package/dist/app/helper/post.js +0 -15
- package/dist/app/memo/index.d.ts +0 -12
- package/dist/app/memo/index.d.ts.map +0 -1
- package/dist/app/memo/index.js +0 -34
- package/dist/jsx/chunk/index.d.ts +0 -38
- package/dist/jsx/chunk/index.d.ts.map +0 -1
- package/dist/jsx/chunk/index.js +0 -69
- package/dist/jsx/intrinsic-elements.d.ts.map +0 -1
- package/dist/jsx/merge-async-generators.d.ts +0 -14
- package/dist/jsx/merge-async-generators.d.ts.map +0 -1
- package/dist/jsx/merge-async-generators.js +0 -42
- package/dist/types/index.d.ts +0 -17
- package/dist/types/index.d.ts.map +0 -1
- package/dist/util/hash.d.ts +0 -11
- package/dist/util/hash.d.ts.map +0 -1
- package/dist/util/hash.js +0 -16
- package/src/app/context.ts +0 -303
- package/src/app/helper/get.ts +0 -46
- package/src/app/helper/index.ts +0 -156
- package/src/app/helper/post.ts +0 -54
- package/src/app/memo/index.ts +0 -40
- package/src/jsx/chunk/index.ts +0 -79
- package/src/jsx/merge-async-generators.ts +0 -47
- package/src/types/index.ts +0 -42
- package/src/util/hash.ts +0 -17
- /package/dist/jsx/{intrinsic-elements.js → elements.js} +0 -0
- /package/dist/{types → middleware}/index.js +0 -0
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { type JSX } from "../jsx/index.js";
|
|
2
|
+
import type { Middleware } from "../middleware/index.js";
|
|
3
|
+
import type { Trie } from "../trie/index.js";
|
|
4
|
+
/** Helper type to extract the route params (`:slug`) into a record */
|
|
5
|
+
export type ExtractParams<Pattern extends string = string> = Pattern extends `${infer _Start}:${infer Param}/${infer Rest}` ? {
|
|
6
|
+
[k in Param | keyof ExtractParams<Rest>]: string;
|
|
7
|
+
} : Pattern extends `${infer _Start}:${infer Param}` ? {
|
|
8
|
+
[k in Param]: string;
|
|
9
|
+
} : Pattern extends `${infer _Rest}*` ? {
|
|
10
|
+
"*": string;
|
|
11
|
+
} : {};
|
|
12
|
+
/** Helper type to insert a record of params into a resolved string */
|
|
13
|
+
export type InsertParams<Pattern extends string, Params extends Trie.Params> = Pattern extends `${infer Start}:${infer Param}/${infer Rest}` ? Param extends keyof Params ? `${Start}${Params[Param]}/${InsertParams<Rest, Params>}` : Pattern : Pattern extends `${infer Start}:${infer Param}` ? Param extends keyof Params ? `${Start}${Params[Param]}` : Pattern : Pattern extends `${infer Start}*` ? "*" extends keyof Params ? `${Start}${Params["*"]}` : Pattern : Pattern;
|
|
14
|
+
export declare namespace Route {
|
|
15
|
+
/** HTTP Method */
|
|
16
|
+
type Method = "GET" | "HEAD" | "POST" | "PUT" | "DELETE" | "CONNECT" | "OPTIONS" | "TRACE" | "PATCH" | (string & {});
|
|
17
|
+
/**
|
|
18
|
+
* Options to construct a relative URL from the route.
|
|
19
|
+
*
|
|
20
|
+
* @template Params Parameters created from a route match
|
|
21
|
+
*/
|
|
22
|
+
type URLOptions<Params extends Trie.Params> = {
|
|
23
|
+
/**
|
|
24
|
+
* Passed into `URLSearchParams` constructor to create new params.
|
|
25
|
+
*
|
|
26
|
+
* [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams)
|
|
27
|
+
*/
|
|
28
|
+
readonly search?: string | Iterable<[string, string]> | Record<string, string>;
|
|
29
|
+
/**
|
|
30
|
+
* Hash (fragment) of the URL. `"#"` prefix is added if not present.
|
|
31
|
+
*
|
|
32
|
+
* [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/URL/hash)
|
|
33
|
+
*/
|
|
34
|
+
readonly hash?: string;
|
|
35
|
+
} & (keyof Params extends never ? {
|
|
36
|
+
/** Route pattern does not contain parameters */
|
|
37
|
+
readonly params?: never;
|
|
38
|
+
} : {
|
|
39
|
+
/** Route pattern parameters */
|
|
40
|
+
readonly params: Params;
|
|
41
|
+
});
|
|
42
|
+
/**
|
|
43
|
+
* `<Anchor>` route component type
|
|
44
|
+
*
|
|
45
|
+
* @template Pattern Route pattern
|
|
46
|
+
*/
|
|
47
|
+
type Anchor<Pattern extends string> = (props: JSX.IntrinsicElements["a"] & URLOptions<ExtractParams<Pattern>>) => JSX.Element;
|
|
48
|
+
/**
|
|
49
|
+
* `<Button>` route component type
|
|
50
|
+
*
|
|
51
|
+
* @template Pattern Route pattern
|
|
52
|
+
*/
|
|
53
|
+
type Button<Pattern extends string> = (props: JSX.IntrinsicElements["button"] & URLOptions<ExtractParams<Pattern>>) => JSX.Element;
|
|
54
|
+
/**
|
|
55
|
+
* `<Form>` route component type
|
|
56
|
+
*
|
|
57
|
+
* @template Pattern Route pattern
|
|
58
|
+
*/
|
|
59
|
+
type Form<Pattern extends string> = (props: JSX.IntrinsicElements["form"] & URLOptions<ExtractParams<Pattern>>) => JSX.Element;
|
|
60
|
+
/** Extract params from a Route instance. */
|
|
61
|
+
type Params<R> = R extends {
|
|
62
|
+
pattern: infer P;
|
|
63
|
+
} ? P extends string ? ExtractParams<P> : never : never;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Helper type for a route with a `<Button>` component.
|
|
67
|
+
*
|
|
68
|
+
* @template Pattern Route pattern
|
|
69
|
+
*/
|
|
70
|
+
type WithButton<Pattern extends string = string> = {
|
|
71
|
+
/** `<button>` component with preset `formaction` and `formmethod` attributes */
|
|
72
|
+
readonly Button: Route.Button<Pattern>;
|
|
73
|
+
};
|
|
74
|
+
/**
|
|
75
|
+
* Helper type for a route with a `<Form>` component.
|
|
76
|
+
*
|
|
77
|
+
* @template Pattern Route pattern
|
|
78
|
+
*/
|
|
79
|
+
type WithForm<Pattern extends string = string> = {
|
|
80
|
+
/** `<form>` component with preset `method` and `action` attributes */
|
|
81
|
+
readonly Form: Route.Form<Pattern>;
|
|
82
|
+
};
|
|
83
|
+
/**
|
|
84
|
+
* Helper type for a route with a `<Anchor>` component.
|
|
85
|
+
*
|
|
86
|
+
* @template Pattern Route pattern
|
|
87
|
+
*/
|
|
88
|
+
type WithAnchor<Pattern extends string = string> = {
|
|
89
|
+
/** `<a>` component with preset `href` attribute */
|
|
90
|
+
readonly Anchor: Route.Anchor<Pattern>;
|
|
91
|
+
};
|
|
92
|
+
/**
|
|
93
|
+
* Route to use in the application.
|
|
94
|
+
*
|
|
95
|
+
* @template Pattern Route pattern
|
|
96
|
+
*/
|
|
97
|
+
export declare class Route<Pattern extends string = string> {
|
|
98
|
+
#private;
|
|
99
|
+
/** Route pattern */
|
|
100
|
+
readonly pattern: Pattern;
|
|
101
|
+
/** HTTP method */
|
|
102
|
+
readonly method: Route.Method;
|
|
103
|
+
/** Route middleware stack, runs after global middleware */
|
|
104
|
+
readonly middleware: Middleware<any>[];
|
|
105
|
+
/**
|
|
106
|
+
* Create a new route.
|
|
107
|
+
*
|
|
108
|
+
* @param method HTTP Method
|
|
109
|
+
* @param pattern Route pattern
|
|
110
|
+
* @param middleware Route middleware
|
|
111
|
+
*/
|
|
112
|
+
constructor(method: Route.Method, pattern: Pattern, ...middleware: Middleware<ExtractParams<Pattern>>[]);
|
|
113
|
+
/**
|
|
114
|
+
* Constructs a _relative_ URL for the route.
|
|
115
|
+
*
|
|
116
|
+
* @param [options] Options with type safe pathname parameters
|
|
117
|
+
* @returns `pathname` + `search` + `hash`
|
|
118
|
+
*/
|
|
119
|
+
url(...[options]: keyof ExtractParams<Pattern> extends never ? [Route.URLOptions<ExtractParams<Pattern>>] | [] : [Route.URLOptions<ExtractParams<Pattern>>]): string;
|
|
120
|
+
/**
|
|
121
|
+
* @template Params Parameters to create the pathname with
|
|
122
|
+
* @param [params] Parameters to insert
|
|
123
|
+
* @returns Resolved `pathname` with params
|
|
124
|
+
*/
|
|
125
|
+
pathname<Params extends ExtractParams<Pattern>>(...[params]: keyof Params extends never ? [] : [Params]): InsertParams<Pattern, Params>;
|
|
126
|
+
/**
|
|
127
|
+
* @template Pattern Route pattern
|
|
128
|
+
* @param pattern Route pattern
|
|
129
|
+
* @param middleware GET middleware
|
|
130
|
+
* @returns GET `Route` with added components
|
|
131
|
+
*/
|
|
132
|
+
static get<Pattern extends string>(pattern: Pattern, ...middleware: Middleware<ExtractParams<Pattern>>[]): Route<Pattern> & WithButton<Pattern> & WithForm<Pattern> & WithAnchor<Pattern>;
|
|
133
|
+
/**
|
|
134
|
+
* @param middleware POST middleware
|
|
135
|
+
* @returns POST `Route` with added components
|
|
136
|
+
*/
|
|
137
|
+
static post(middleware: Middleware<{}>): Route & WithButton & WithForm;
|
|
138
|
+
/**
|
|
139
|
+
* @template Pattern Route pattern
|
|
140
|
+
* @param pattern Route pattern
|
|
141
|
+
* @param middleware POST middleware
|
|
142
|
+
* @returns POST `Route` with added components
|
|
143
|
+
*/
|
|
144
|
+
static post<Pattern extends string>(pattern: Pattern, ...middleware: Middleware<ExtractParams<Pattern>>[]): Route<Pattern> & WithButton<Pattern> & WithForm<Pattern>;
|
|
145
|
+
}
|
|
146
|
+
export {};
|
|
147
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/route/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,GAAG,EAAO,MAAM,iBAAiB,CAAC;AAChD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AACzD,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AAG7C,sEAAsE;AACtE,MAAM,MAAM,aAAa,CAAC,OAAO,SAAS,MAAM,GAAG,MAAM,IACxD,OAAO,SAAS,GAAG,MAAM,MAAM,IA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;IACH,QAAQ,CAAC,MAAM,SAAS,aAAa,CAAC,OAAO,CAAC,EAC7C,GAAG,CAAC,MAAM,CAAC,EAAE,MAAM,MAAM,SAAS,KAAK,GAAG,EAAE,GAAG,CAAC,MAAM,CAAC,GACrD,YAAY,CAAC,OAAO,EAAE,MAAM,CAAC;IA8ChC;;;;;OAKG;IACH,MAAM,CAAC,GAAG,CAAC,OAAO,SAAS,MAAM,EAChC,OAAO,EAAE,OAAO,EAChB,GAAG,UAAU,EAAE,UAAU,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC,EAAE,GACjD,KAAK,CAAC,OAAO,CAAC,GAChB,UAAU,CAAC,OAAO,CAAC,GACnB,QAAQ,CAAC,OAAO,CAAC,GACjB,UAAU,CAAC,OAAO,CAAC;IAgBpB;;;OAGG;IACH,MAAM,CAAC,IAAI,CAAC,UAAU,EAAE,UAAU,CAAC,EAAE,CAAC,GAAG,KAAK,GAAG,UAAU,GAAG,QAAQ;IACtE;;;;;OAKG;IACH,MAAM,CAAC,IAAI,CAAC,OAAO,SAAS,MAAM,EACjC,OAAO,EAAE,OAAO,EAChB,GAAG,UAAU,EAAE,UAAU,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC,EAAE,GACjD,KAAK,CAAC,OAAO,CAAC,GAAG,UAAU,CAAC,OAAO,CAAC,GAAG,QAAQ,CAAC,OAAO,CAAC;CAgB3D"}
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { jsx } from "../jsx/index.js";
|
|
2
|
+
import { Hash } from "../util/index.js";
|
|
3
|
+
/**
|
|
4
|
+
* Route to use in the application.
|
|
5
|
+
*
|
|
6
|
+
* @template Pattern Route pattern
|
|
7
|
+
*/
|
|
8
|
+
export class Route {
|
|
9
|
+
/** Route pattern */
|
|
10
|
+
pattern;
|
|
11
|
+
/** HTTP method */
|
|
12
|
+
method;
|
|
13
|
+
/** Route middleware stack, runs after global middleware */
|
|
14
|
+
middleware; // any so you can use other middleware
|
|
15
|
+
/** Pattern parts */
|
|
16
|
+
#parts;
|
|
17
|
+
/**
|
|
18
|
+
* Create a new route.
|
|
19
|
+
*
|
|
20
|
+
* @param method HTTP Method
|
|
21
|
+
* @param pattern Route pattern
|
|
22
|
+
* @param middleware Route middleware
|
|
23
|
+
*/
|
|
24
|
+
constructor(method, pattern, ...middleware) {
|
|
25
|
+
this.method = method;
|
|
26
|
+
this.pattern = pattern;
|
|
27
|
+
this.middleware = middleware;
|
|
28
|
+
this.#parts = pattern.split("/");
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Constructs a _relative_ URL for the route.
|
|
32
|
+
*
|
|
33
|
+
* @param [options] Options with type safe pathname parameters
|
|
34
|
+
* @returns `pathname` + `search` + `hash`
|
|
35
|
+
*/
|
|
36
|
+
url(...[options]) {
|
|
37
|
+
const pathname = this.pathname(
|
|
38
|
+
// @ts-expect-error - do not have to pass in {} if no params
|
|
39
|
+
options?.params);
|
|
40
|
+
let search = "";
|
|
41
|
+
let hash = "";
|
|
42
|
+
if (options?.search) {
|
|
43
|
+
// use the value as the init
|
|
44
|
+
// @ts-expect-error - see above
|
|
45
|
+
search = "?" + new URLSearchParams(options.search);
|
|
46
|
+
}
|
|
47
|
+
if (options?.hash) {
|
|
48
|
+
// adding # prefix if not present matches the URL setter:
|
|
49
|
+
// https://developer.mozilla.org/en-US/docs/Web/API/URL/hash
|
|
50
|
+
if (options.hash.startsWith("#")) {
|
|
51
|
+
hash = options.hash;
|
|
52
|
+
}
|
|
53
|
+
else {
|
|
54
|
+
hash = "#" + options.hash;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return pathname + search + hash;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* @template Params Parameters to create the pathname with
|
|
61
|
+
* @param [params] Parameters to insert
|
|
62
|
+
* @returns Resolved `pathname` with params
|
|
63
|
+
*/
|
|
64
|
+
pathname(...[params]) {
|
|
65
|
+
if (!params)
|
|
66
|
+
return this.pattern;
|
|
67
|
+
return this.#parts
|
|
68
|
+
.map((part) => {
|
|
69
|
+
if (part.startsWith(":")) {
|
|
70
|
+
return params[part.slice(1)];
|
|
71
|
+
}
|
|
72
|
+
if (part === "*")
|
|
73
|
+
return params[part];
|
|
74
|
+
return part;
|
|
75
|
+
})
|
|
76
|
+
.join("/");
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* @template Pattern Route pattern
|
|
80
|
+
* @param route Route to add components to
|
|
81
|
+
* @returns Route with added components
|
|
82
|
+
*/
|
|
83
|
+
static #withComponents(route) {
|
|
84
|
+
const enctype = route.method === "POST" ? "multipart/form-data" : undefined;
|
|
85
|
+
return Object.assign(route, {
|
|
86
|
+
Button: (({ params, search, hash, ...rest }) => jsx("button", {
|
|
87
|
+
formaction: route.url({ params, search, hash }),
|
|
88
|
+
formmethod: route.method,
|
|
89
|
+
formenctype: enctype,
|
|
90
|
+
...rest,
|
|
91
|
+
})),
|
|
92
|
+
Form: (({ params, search, hash, ...rest }) => jsx("form", {
|
|
93
|
+
action: route.url({ params, search, hash }),
|
|
94
|
+
method: route.method,
|
|
95
|
+
enctype,
|
|
96
|
+
...rest,
|
|
97
|
+
})),
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* @template Pattern Route pattern
|
|
102
|
+
* @param pattern Route pattern
|
|
103
|
+
* @param middleware GET middleware
|
|
104
|
+
* @returns GET `Route` with added components
|
|
105
|
+
*/
|
|
106
|
+
static get(pattern, ...middleware) {
|
|
107
|
+
const route = Route.#withComponents(new Route("GET", pattern, ...middleware));
|
|
108
|
+
return Object.assign(route, {
|
|
109
|
+
Anchor: (({ params, search, hash, ...rest }) => jsx("a", {
|
|
110
|
+
href: route.url({ params, search, hash }),
|
|
111
|
+
...rest,
|
|
112
|
+
})),
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
static post(patternOrMiddleware, ...middleware) {
|
|
116
|
+
let pattern;
|
|
117
|
+
if (typeof patternOrMiddleware === "string") {
|
|
118
|
+
pattern = patternOrMiddleware;
|
|
119
|
+
}
|
|
120
|
+
else {
|
|
121
|
+
middleware.unshift(patternOrMiddleware);
|
|
122
|
+
pattern = `/_p/${Hash.djb2(middleware.join())}`;
|
|
123
|
+
}
|
|
124
|
+
return Route.#withComponents(new Route("POST", pattern, ...middleware));
|
|
125
|
+
}
|
|
126
|
+
}
|
package/dist/trie/index.d.ts
CHANGED
|
@@ -1,46 +1,46 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
/** Route pattern */
|
|
4
|
-
pattern: string;
|
|
5
|
-
/** Store returned when route is found */
|
|
6
|
-
store: Store;
|
|
7
|
-
/**
|
|
8
|
-
* @param pattern route pattern
|
|
9
|
-
* @param store value to store in the patterns final node
|
|
10
|
-
*/
|
|
11
|
-
constructor(pattern: string, store: Store);
|
|
12
|
-
}
|
|
13
|
-
declare class ParamNode<Store> {
|
|
1
|
+
import type { Route } from "../route/index.js";
|
|
2
|
+
declare class ParamNode {
|
|
14
3
|
/** Name of the parameter (without the colon ":") */
|
|
15
|
-
name: string;
|
|
4
|
+
readonly name: string;
|
|
16
5
|
/** Matched route */
|
|
17
|
-
route
|
|
6
|
+
route?: Route;
|
|
18
7
|
/** Static child node */
|
|
19
|
-
|
|
8
|
+
child?: Trie;
|
|
9
|
+
/**
|
|
10
|
+
* Create a new parameter node.
|
|
11
|
+
*
|
|
12
|
+
* @param name Name of the parameter.
|
|
13
|
+
*/
|
|
20
14
|
constructor(name: string);
|
|
21
15
|
}
|
|
22
|
-
export declare
|
|
16
|
+
export declare namespace Trie {
|
|
17
|
+
/** Params created from the route match */
|
|
18
|
+
type Params = Record<string, string>;
|
|
19
|
+
}
|
|
20
|
+
export declare class Trie {
|
|
23
21
|
#private;
|
|
24
22
|
/** Unique segment of the pattern trie */
|
|
25
|
-
|
|
23
|
+
readonly seg: string;
|
|
26
24
|
/** Static child node map, key is the first character in the segment */
|
|
27
|
-
|
|
25
|
+
map?: Map<number, Trie>;
|
|
28
26
|
/** Parametric child node */
|
|
29
|
-
|
|
27
|
+
param?: ParamNode;
|
|
30
28
|
/** Matched route */
|
|
31
|
-
route
|
|
29
|
+
route?: Route;
|
|
32
30
|
/** Matched wildcard route */
|
|
33
|
-
|
|
31
|
+
wild?: Route;
|
|
34
32
|
/**
|
|
33
|
+
* Create a new trie.
|
|
34
|
+
*
|
|
35
35
|
* @param segment pattern segment
|
|
36
|
-
* @param
|
|
36
|
+
* @param children static children nodes to add to staticMap
|
|
37
37
|
*/
|
|
38
|
-
constructor(segment?: string,
|
|
38
|
+
constructor(segment?: string, children?: Trie[]);
|
|
39
39
|
/**
|
|
40
40
|
* @param segment new segment
|
|
41
41
|
* @returns a clone of the Node with a new segment
|
|
42
42
|
*/
|
|
43
|
-
clone(segment: string): Trie
|
|
43
|
+
clone(segment: string): Trie;
|
|
44
44
|
/**
|
|
45
45
|
* If the current segment is "api/posts/"
|
|
46
46
|
* and "api/movies/" is added,
|
|
@@ -50,7 +50,7 @@ export declare class Trie<Store> {
|
|
|
50
50
|
* @param segment new segment to use
|
|
51
51
|
* @returns the new child produced from the new segment
|
|
52
52
|
*/
|
|
53
|
-
fork(charIndex: number, segment: string): Trie
|
|
53
|
+
fork(charIndex: number, segment: string): Trie;
|
|
54
54
|
/**
|
|
55
55
|
* node.segment = "static/static",
|
|
56
56
|
* staticSegment = "static/",
|
|
@@ -63,19 +63,19 @@ export declare class Trie<Store> {
|
|
|
63
63
|
* @param name name of the param
|
|
64
64
|
* @returns the existing child with the same name, or creates a new
|
|
65
65
|
*/
|
|
66
|
-
|
|
66
|
+
set(name: string): ParamNode;
|
|
67
67
|
/**
|
|
68
68
|
* @param route route return when pattern is matched
|
|
69
69
|
* @returns this - the Node
|
|
70
70
|
*/
|
|
71
|
-
add(route: Route
|
|
71
|
+
add(route: Route): this;
|
|
72
72
|
/**
|
|
73
73
|
* @param pathname Path to find
|
|
74
74
|
* @returns `Route` and the matched `params` if found, otherwise `null`
|
|
75
75
|
*/
|
|
76
76
|
find(pathname: string): {
|
|
77
|
-
route: Route
|
|
78
|
-
params: Params;
|
|
77
|
+
route: Route;
|
|
78
|
+
params: Trie.Params;
|
|
79
79
|
} | null;
|
|
80
80
|
}
|
|
81
81
|
export {};
|
package/dist/trie/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/trie/index.ts"],"names":[],"mappings":"AAAA,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/trie/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,mBAAmB,CAAC;AAE/C,cAAM,SAAS;IACd,oDAAoD;IACpD,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IAEtB,oBAAoB;IACpB,KAAK,CAAC,EAAE,KAAK,CAAC;IAEd,wBAAwB;IACxB,KAAK,CAAC,EAAE,IAAI,CAAC;IAEb;;;;OAIG;gBACS,IAAI,EAAE,MAAM;CAGxB;AAED,yBAAiB,IAAI,CAAC;IACrB,0CAA0C;IAC1C,KAAY,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CAC5C;AAED,qBAAa,IAAI;;IAChB,yCAAyC;IACzC,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IAErB,uEAAuE;IACvE,GAAG,CAAC,EAAE,GAAG,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;IAExB,4BAA4B;IAC5B,KAAK,CAAC,EAAE,SAAS,CAAC;IAElB,oBAAoB;IACpB,KAAK,CAAC,EAAE,KAAK,CAAC;IAEd,6BAA6B;IAC7B,IAAI,CAAC,EAAE,KAAK,CAAC;IAKb;;;;;OAKG;gBACS,OAAO,SAAM,EAAE,QAAQ,CAAC,EAAE,IAAI,EAAE;IAY5C;;;OAGG;IACH,KAAK,CAAC,OAAO,EAAE,MAAM;IAWrB;;;;;;;;OAQG;IACH,IAAI,CAAC,SAAS,EAA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