@mokup/runtime 0.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.
@@ -0,0 +1,102 @@
1
+ type RouteToken = {
2
+ type: 'static';
3
+ value: string;
4
+ } | {
5
+ type: 'param';
6
+ name: string;
7
+ } | {
8
+ type: 'catchall';
9
+ name: string;
10
+ } | {
11
+ type: 'optional-catchall';
12
+ name: string;
13
+ };
14
+ interface ParsedRouteTemplate {
15
+ template: string;
16
+ tokens: RouteToken[];
17
+ score: number[];
18
+ errors: string[];
19
+ warnings: string[];
20
+ }
21
+ declare function scoreRouteTokens(tokens: RouteToken[]): (4 | 3 | 2 | 1 | 0)[];
22
+ declare function compareRouteScore(a: number[], b: number[]): number;
23
+ declare function normalizePathname(value: string): string;
24
+ declare function parseRouteTemplate(template: string): ParsedRouteTemplate;
25
+ declare function matchRouteTokens(tokens: RouteToken[], pathname: string): {
26
+ params: Record<string, string | string[]>;
27
+ } | null;
28
+
29
+ type HttpMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'OPTIONS' | 'HEAD';
30
+ interface Manifest {
31
+ version: 1;
32
+ routes: ManifestRoute[];
33
+ }
34
+ interface ManifestRoute {
35
+ method: HttpMethod;
36
+ url: string;
37
+ tokens?: RouteToken[];
38
+ score?: number[];
39
+ source?: string;
40
+ status?: number;
41
+ headers?: Record<string, string>;
42
+ delay?: number;
43
+ middleware?: ManifestModuleRef[];
44
+ response: ManifestResponse;
45
+ }
46
+ interface ManifestModuleRef {
47
+ module: string;
48
+ exportName?: string;
49
+ ruleIndex?: number;
50
+ }
51
+ type ManifestResponse = {
52
+ type: 'json';
53
+ body: unknown;
54
+ } | {
55
+ type: 'text';
56
+ body: string;
57
+ } | {
58
+ type: 'binary';
59
+ body: string;
60
+ encoding: 'base64';
61
+ } | ({
62
+ type: 'module';
63
+ } & ManifestModuleRef);
64
+ interface RuntimeRequest {
65
+ method: string;
66
+ path: string;
67
+ query: Record<string, string | string[]>;
68
+ headers: Record<string, string>;
69
+ body: unknown;
70
+ rawBody?: string;
71
+ params?: Record<string, string | string[]>;
72
+ }
73
+ interface RuntimeResult {
74
+ status: number;
75
+ headers: Record<string, string>;
76
+ body: string | Uint8Array | null;
77
+ }
78
+ interface MockContext {
79
+ delay: (ms: number) => Promise<void>;
80
+ json: <T>(data: T) => T;
81
+ }
82
+ interface MockResponder {
83
+ statusCode: number;
84
+ setHeader: (key: string, value: string) => void;
85
+ getHeader: (key: string) => string | undefined;
86
+ removeHeader: (key: string) => void;
87
+ }
88
+ type MockMiddleware = (req: RuntimeRequest, res: MockResponder, ctx: MockContext, next: () => Promise<unknown>) => unknown | Promise<unknown>;
89
+ type MockResponseHandler = (req: RuntimeRequest, res: MockResponder, ctx: MockContext) => unknown | Promise<unknown>;
90
+ interface RuntimeOptions {
91
+ manifest: Manifest | (() => Promise<Manifest>);
92
+ moduleBase?: string | URL;
93
+ moduleMap?: ModuleMap;
94
+ }
95
+ type ModuleMap = Record<string, Record<string, unknown>>;
96
+
97
+ declare function createRuntime(options: RuntimeOptions): {
98
+ handle: (req: RuntimeRequest) => Promise<RuntimeResult | null>;
99
+ };
100
+
101
+ export { compareRouteScore, createRuntime, matchRouteTokens, normalizePathname, parseRouteTemplate, scoreRouteTokens };
102
+ export type { HttpMethod, Manifest, ManifestModuleRef, ManifestResponse, ManifestRoute, MockContext, MockMiddleware, MockResponder, MockResponseHandler, ModuleMap, ParsedRouteTemplate, RouteToken, RuntimeOptions, RuntimeRequest, RuntimeResult };
@@ -0,0 +1,102 @@
1
+ type RouteToken = {
2
+ type: 'static';
3
+ value: string;
4
+ } | {
5
+ type: 'param';
6
+ name: string;
7
+ } | {
8
+ type: 'catchall';
9
+ name: string;
10
+ } | {
11
+ type: 'optional-catchall';
12
+ name: string;
13
+ };
14
+ interface ParsedRouteTemplate {
15
+ template: string;
16
+ tokens: RouteToken[];
17
+ score: number[];
18
+ errors: string[];
19
+ warnings: string[];
20
+ }
21
+ declare function scoreRouteTokens(tokens: RouteToken[]): (4 | 3 | 2 | 1 | 0)[];
22
+ declare function compareRouteScore(a: number[], b: number[]): number;
23
+ declare function normalizePathname(value: string): string;
24
+ declare function parseRouteTemplate(template: string): ParsedRouteTemplate;
25
+ declare function matchRouteTokens(tokens: RouteToken[], pathname: string): {
26
+ params: Record<string, string | string[]>;
27
+ } | null;
28
+
29
+ type HttpMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'OPTIONS' | 'HEAD';
30
+ interface Manifest {
31
+ version: 1;
32
+ routes: ManifestRoute[];
33
+ }
34
+ interface ManifestRoute {
35
+ method: HttpMethod;
36
+ url: string;
37
+ tokens?: RouteToken[];
38
+ score?: number[];
39
+ source?: string;
40
+ status?: number;
41
+ headers?: Record<string, string>;
42
+ delay?: number;
43
+ middleware?: ManifestModuleRef[];
44
+ response: ManifestResponse;
45
+ }
46
+ interface ManifestModuleRef {
47
+ module: string;
48
+ exportName?: string;
49
+ ruleIndex?: number;
50
+ }
51
+ type ManifestResponse = {
52
+ type: 'json';
53
+ body: unknown;
54
+ } | {
55
+ type: 'text';
56
+ body: string;
57
+ } | {
58
+ type: 'binary';
59
+ body: string;
60
+ encoding: 'base64';
61
+ } | ({
62
+ type: 'module';
63
+ } & ManifestModuleRef);
64
+ interface RuntimeRequest {
65
+ method: string;
66
+ path: string;
67
+ query: Record<string, string | string[]>;
68
+ headers: Record<string, string>;
69
+ body: unknown;
70
+ rawBody?: string;
71
+ params?: Record<string, string | string[]>;
72
+ }
73
+ interface RuntimeResult {
74
+ status: number;
75
+ headers: Record<string, string>;
76
+ body: string | Uint8Array | null;
77
+ }
78
+ interface MockContext {
79
+ delay: (ms: number) => Promise<void>;
80
+ json: <T>(data: T) => T;
81
+ }
82
+ interface MockResponder {
83
+ statusCode: number;
84
+ setHeader: (key: string, value: string) => void;
85
+ getHeader: (key: string) => string | undefined;
86
+ removeHeader: (key: string) => void;
87
+ }
88
+ type MockMiddleware = (req: RuntimeRequest, res: MockResponder, ctx: MockContext, next: () => Promise<unknown>) => unknown | Promise<unknown>;
89
+ type MockResponseHandler = (req: RuntimeRequest, res: MockResponder, ctx: MockContext) => unknown | Promise<unknown>;
90
+ interface RuntimeOptions {
91
+ manifest: Manifest | (() => Promise<Manifest>);
92
+ moduleBase?: string | URL;
93
+ moduleMap?: ModuleMap;
94
+ }
95
+ type ModuleMap = Record<string, Record<string, unknown>>;
96
+
97
+ declare function createRuntime(options: RuntimeOptions): {
98
+ handle: (req: RuntimeRequest) => Promise<RuntimeResult | null>;
99
+ };
100
+
101
+ export { compareRouteScore, createRuntime, matchRouteTokens, normalizePathname, parseRouteTemplate, scoreRouteTokens };
102
+ export type { HttpMethod, Manifest, ManifestModuleRef, ManifestResponse, ManifestRoute, MockContext, MockMiddleware, MockResponder, MockResponseHandler, ModuleMap, ParsedRouteTemplate, RouteToken, RuntimeOptions, RuntimeRequest, RuntimeResult };