mokup 2.0.2 → 2.1.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/cli-bin.cjs CHANGED
@@ -2,13 +2,15 @@
2
2
  'use strict';
3
3
 
4
4
  const process = require('node:process');
5
+ const logger$1 = require('@mokup/shared/logger');
5
6
  const cli = require('@mokup/cli');
6
7
 
7
8
  function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
8
9
 
9
10
  const process__default = /*#__PURE__*/_interopDefaultCompat(process);
10
11
 
12
+ const logger = logger$1.createLogger();
11
13
  cli.runCli().catch((error) => {
12
- console.error(error instanceof Error ? error.message : String(error));
14
+ logger.error(error instanceof Error ? error.message : String(error));
13
15
  process__default.exit(1);
14
16
  });
package/dist/cli-bin.mjs CHANGED
@@ -1,8 +1,10 @@
1
1
  #!/usr/bin/env node
2
2
  import process from 'node:process';
3
+ import { createLogger } from '@mokup/shared/logger';
3
4
  import { runCli } from '@mokup/cli';
4
5
 
6
+ const logger = createLogger();
5
7
  runCli().catch((error) => {
6
- console.error(error instanceof Error ? error.message : String(error));
8
+ logger.error(error instanceof Error ? error.message : String(error));
7
9
  process.exit(1);
8
10
  });
package/dist/index.cjs CHANGED
@@ -1,2 +1,41 @@
1
1
  'use strict';
2
2
 
3
+ const middlewareSymbol = Symbol.for("mokup.config.middlewares");
4
+ function createRegistry(list) {
5
+ return {
6
+ use: (...handlers) => {
7
+ list.push(...handlers);
8
+ }
9
+ };
10
+ }
11
+ function attachMetadata(config, meta) {
12
+ Object.defineProperty(config, middlewareSymbol, {
13
+ value: meta,
14
+ enumerable: false
15
+ });
16
+ return config;
17
+ }
18
+ function defineConfig(input) {
19
+ if (typeof input === "function") {
20
+ const pre = [];
21
+ const normal = [];
22
+ const post = [];
23
+ const context = {
24
+ pre: createRegistry(pre),
25
+ normal: createRegistry(normal),
26
+ post: createRegistry(post)
27
+ };
28
+ const result = input(context);
29
+ const config2 = result && typeof result === "object" ? result : {};
30
+ return attachMetadata(config2, { pre, normal, post });
31
+ }
32
+ const config = input && typeof input === "object" ? input : {};
33
+ return attachMetadata(config, { pre: [], normal: [], post: [] });
34
+ }
35
+
36
+ function defineHandler(input) {
37
+ return input;
38
+ }
39
+
40
+ exports.defineConfig = defineConfig;
41
+ exports.defineHandler = defineHandler;
package/dist/index.d.cts CHANGED
@@ -1,48 +1,66 @@
1
- import { MockEntryOptions, PlaygroundOptionsInput } from '@mokup/shared';
2
- export { PlaygroundOptionsInput } from '@mokup/shared';
3
- import { Context, MiddlewareHandler } from '@mokup/shared/hono';
1
+ import { c as RouteDirectoryConfig, b as MiddlewareRegistry, R as RequestHandler, e as RouteRule } from './shared/mokup.CWQ8woZc.cjs';
2
+ export { H as HttpMethod, a as MiddlewarePosition, M as MokupPluginOptions, d as RouteResponse, f as RuntimeMode, S as ServiceWorkerOptions, V as VitePluginOptions, g as VitePluginOptionsInput } from './shared/mokup.CWQ8woZc.cjs';
4
3
  export { Context, MiddlewareHandler } from '@mokup/shared/hono';
4
+ export { PlaygroundOptionsInput } from '@mokup/shared';
5
5
 
6
- type HttpMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'OPTIONS' | 'HEAD';
7
- type RouteStaticResponse = string | number | boolean | bigint | symbol | null | undefined | object;
8
- type RouteHandlerResult = RouteStaticResponse | Response;
9
- type RequestHandler = (context: Context) => RouteHandlerResult | Promise<RouteHandlerResult>;
10
- type RouteResponse = RouteStaticResponse | RequestHandler;
11
- interface RouteRule {
12
- handler: RouteResponse;
13
- enabled?: boolean;
14
- status?: number;
15
- headers?: Record<string, string>;
16
- delay?: number;
17
- }
18
- type RuntimeMode = 'server' | 'sw';
19
- interface ServiceWorkerOptions {
20
- path?: string;
21
- scope?: string;
22
- register?: boolean;
23
- unregister?: boolean;
24
- fallback?: boolean;
25
- basePath?: string | string[];
26
- }
6
+ type DefineConfigFactory = (context: {
7
+ pre: MiddlewareRegistry;
8
+ normal: MiddlewareRegistry;
9
+ post: MiddlewareRegistry;
10
+ }) => RouteDirectoryConfig | void;
11
+ /**
12
+ * Define a directory config with Hono-style middleware registration.
13
+ *
14
+ * @param input - Config object or factory callback.
15
+ * @returns Route directory config with middleware metadata.
16
+ *
17
+ * @example
18
+ * import { defineConfig } from 'mokup'
19
+ *
20
+ * export default defineConfig(({ pre, normal, post }) => {
21
+ * pre.use(async (c, next) => {
22
+ * c.header('x-before', '1')
23
+ * await next()
24
+ * })
25
+ *
26
+ * normal.use(async (_c, next) => {
27
+ * await next()
28
+ * })
29
+ *
30
+ * post.use(async (c, next) => {
31
+ * await next()
32
+ * c.header('x-after', '1')
33
+ * })
34
+ *
35
+ * return { delay: 120 }
36
+ * })
37
+ */
38
+ declare function defineConfig(input: RouteDirectoryConfig | DefineConfigFactory): RouteDirectoryConfig;
27
39
 
28
- interface RouteDirectoryConfig {
29
- headers?: Record<string, string>;
30
- status?: number;
31
- delay?: number;
32
- enabled?: boolean;
33
- ignorePrefix?: string | string[];
34
- include?: RegExp | RegExp[];
35
- exclude?: RegExp | RegExp[];
36
- middleware?: MiddlewareHandler | MiddlewareHandler[];
37
- }
38
- interface VitePluginOptions extends MockEntryOptions {
39
- mode?: RuntimeMode;
40
- sw?: ServiceWorkerOptions;
41
- }
42
- interface MokupPluginOptions {
43
- entries?: VitePluginOptions | VitePluginOptions[];
44
- playground?: PlaygroundOptionsInput;
45
- }
46
- type VitePluginOptionsInput = MokupPluginOptions;
40
+ /**
41
+ * Define a mock route handler with type hints.
42
+ *
43
+ * @param input - Handler function or rule object.
44
+ * @returns The same handler or rule.
45
+ *
46
+ * @example
47
+ * import { defineHandler } from 'mokup'
48
+ *
49
+ * export default defineHandler((c) => {
50
+ * return { ok: true }
51
+ * })
52
+ *
53
+ * @example
54
+ * import { defineHandler } from 'mokup'
55
+ *
56
+ * export default defineHandler({
57
+ * enabled: false,
58
+ * handler: async (c) => {
59
+ * return { ok: false, method: c.req.method }
60
+ * },
61
+ * })
62
+ */
63
+ declare function defineHandler(input: RequestHandler): RequestHandler;
64
+ declare function defineHandler(input: RouteRule): RouteRule;
47
65
 
48
- export type { HttpMethod, MokupPluginOptions, RequestHandler, RouteDirectoryConfig, RouteResponse, RouteRule, RuntimeMode, ServiceWorkerOptions, VitePluginOptions, VitePluginOptionsInput };
66
+ export { MiddlewareRegistry, RequestHandler, RouteDirectoryConfig, RouteRule, defineConfig, defineHandler };
package/dist/index.d.mts CHANGED
@@ -1,48 +1,66 @@
1
- import { MockEntryOptions, PlaygroundOptionsInput } from '@mokup/shared';
2
- export { PlaygroundOptionsInput } from '@mokup/shared';
3
- import { Context, MiddlewareHandler } from '@mokup/shared/hono';
1
+ import { c as RouteDirectoryConfig, b as MiddlewareRegistry, R as RequestHandler, e as RouteRule } from './shared/mokup.CWQ8woZc.mjs';
2
+ export { H as HttpMethod, a as MiddlewarePosition, M as MokupPluginOptions, d as RouteResponse, f as RuntimeMode, S as ServiceWorkerOptions, V as VitePluginOptions, g as VitePluginOptionsInput } from './shared/mokup.CWQ8woZc.mjs';
4
3
  export { Context, MiddlewareHandler } from '@mokup/shared/hono';
4
+ export { PlaygroundOptionsInput } from '@mokup/shared';
5
5
 
6
- type HttpMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'OPTIONS' | 'HEAD';
7
- type RouteStaticResponse = string | number | boolean | bigint | symbol | null | undefined | object;
8
- type RouteHandlerResult = RouteStaticResponse | Response;
9
- type RequestHandler = (context: Context) => RouteHandlerResult | Promise<RouteHandlerResult>;
10
- type RouteResponse = RouteStaticResponse | RequestHandler;
11
- interface RouteRule {
12
- handler: RouteResponse;
13
- enabled?: boolean;
14
- status?: number;
15
- headers?: Record<string, string>;
16
- delay?: number;
17
- }
18
- type RuntimeMode = 'server' | 'sw';
19
- interface ServiceWorkerOptions {
20
- path?: string;
21
- scope?: string;
22
- register?: boolean;
23
- unregister?: boolean;
24
- fallback?: boolean;
25
- basePath?: string | string[];
26
- }
6
+ type DefineConfigFactory = (context: {
7
+ pre: MiddlewareRegistry;
8
+ normal: MiddlewareRegistry;
9
+ post: MiddlewareRegistry;
10
+ }) => RouteDirectoryConfig | void;
11
+ /**
12
+ * Define a directory config with Hono-style middleware registration.
13
+ *
14
+ * @param input - Config object or factory callback.
15
+ * @returns Route directory config with middleware metadata.
16
+ *
17
+ * @example
18
+ * import { defineConfig } from 'mokup'
19
+ *
20
+ * export default defineConfig(({ pre, normal, post }) => {
21
+ * pre.use(async (c, next) => {
22
+ * c.header('x-before', '1')
23
+ * await next()
24
+ * })
25
+ *
26
+ * normal.use(async (_c, next) => {
27
+ * await next()
28
+ * })
29
+ *
30
+ * post.use(async (c, next) => {
31
+ * await next()
32
+ * c.header('x-after', '1')
33
+ * })
34
+ *
35
+ * return { delay: 120 }
36
+ * })
37
+ */
38
+ declare function defineConfig(input: RouteDirectoryConfig | DefineConfigFactory): RouteDirectoryConfig;
27
39
 
28
- interface RouteDirectoryConfig {
29
- headers?: Record<string, string>;
30
- status?: number;
31
- delay?: number;
32
- enabled?: boolean;
33
- ignorePrefix?: string | string[];
34
- include?: RegExp | RegExp[];
35
- exclude?: RegExp | RegExp[];
36
- middleware?: MiddlewareHandler | MiddlewareHandler[];
37
- }
38
- interface VitePluginOptions extends MockEntryOptions {
39
- mode?: RuntimeMode;
40
- sw?: ServiceWorkerOptions;
41
- }
42
- interface MokupPluginOptions {
43
- entries?: VitePluginOptions | VitePluginOptions[];
44
- playground?: PlaygroundOptionsInput;
45
- }
46
- type VitePluginOptionsInput = MokupPluginOptions;
40
+ /**
41
+ * Define a mock route handler with type hints.
42
+ *
43
+ * @param input - Handler function or rule object.
44
+ * @returns The same handler or rule.
45
+ *
46
+ * @example
47
+ * import { defineHandler } from 'mokup'
48
+ *
49
+ * export default defineHandler((c) => {
50
+ * return { ok: true }
51
+ * })
52
+ *
53
+ * @example
54
+ * import { defineHandler } from 'mokup'
55
+ *
56
+ * export default defineHandler({
57
+ * enabled: false,
58
+ * handler: async (c) => {
59
+ * return { ok: false, method: c.req.method }
60
+ * },
61
+ * })
62
+ */
63
+ declare function defineHandler(input: RequestHandler): RequestHandler;
64
+ declare function defineHandler(input: RouteRule): RouteRule;
47
65
 
48
- export type { HttpMethod, MokupPluginOptions, RequestHandler, RouteDirectoryConfig, RouteResponse, RouteRule, RuntimeMode, ServiceWorkerOptions, VitePluginOptions, VitePluginOptionsInput };
66
+ export { MiddlewareRegistry, RequestHandler, RouteDirectoryConfig, RouteRule, defineConfig, defineHandler };
package/dist/index.d.ts CHANGED
@@ -1,48 +1,68 @@
1
- import { MockEntryOptions, PlaygroundOptionsInput } from '@mokup/shared';
2
- export { PlaygroundOptionsInput } from '@mokup/shared';
3
- import { Context, MiddlewareHandler } from '@mokup/shared/hono';
1
+ /// <reference path="./types/virtual.d.ts" />
2
+
3
+ import { c as RouteDirectoryConfig, b as MiddlewareRegistry, R as RequestHandler, e as RouteRule } from './shared/mokup.CWQ8woZc.js';
4
+ export { H as HttpMethod, a as MiddlewarePosition, M as MokupPluginOptions, d as RouteResponse, f as RuntimeMode, S as ServiceWorkerOptions, V as VitePluginOptions, g as VitePluginOptionsInput } from './shared/mokup.CWQ8woZc.js';
4
5
  export { Context, MiddlewareHandler } from '@mokup/shared/hono';
6
+ export { PlaygroundOptionsInput } from '@mokup/shared';
5
7
 
6
- type HttpMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'OPTIONS' | 'HEAD';
7
- type RouteStaticResponse = string | number | boolean | bigint | symbol | null | undefined | object;
8
- type RouteHandlerResult = RouteStaticResponse | Response;
9
- type RequestHandler = (context: Context) => RouteHandlerResult | Promise<RouteHandlerResult>;
10
- type RouteResponse = RouteStaticResponse | RequestHandler;
11
- interface RouteRule {
12
- handler: RouteResponse;
13
- enabled?: boolean;
14
- status?: number;
15
- headers?: Record<string, string>;
16
- delay?: number;
17
- }
18
- type RuntimeMode = 'server' | 'sw';
19
- interface ServiceWorkerOptions {
20
- path?: string;
21
- scope?: string;
22
- register?: boolean;
23
- unregister?: boolean;
24
- fallback?: boolean;
25
- basePath?: string | string[];
26
- }
8
+ type DefineConfigFactory = (context: {
9
+ pre: MiddlewareRegistry;
10
+ normal: MiddlewareRegistry;
11
+ post: MiddlewareRegistry;
12
+ }) => RouteDirectoryConfig | void;
13
+ /**
14
+ * Define a directory config with Hono-style middleware registration.
15
+ *
16
+ * @param input - Config object or factory callback.
17
+ * @returns Route directory config with middleware metadata.
18
+ *
19
+ * @example
20
+ * import { defineConfig } from 'mokup'
21
+ *
22
+ * export default defineConfig(({ pre, normal, post }) => {
23
+ * pre.use(async (c, next) => {
24
+ * c.header('x-before', '1')
25
+ * await next()
26
+ * })
27
+ *
28
+ * normal.use(async (_c, next) => {
29
+ * await next()
30
+ * })
31
+ *
32
+ * post.use(async (c, next) => {
33
+ * await next()
34
+ * c.header('x-after', '1')
35
+ * })
36
+ *
37
+ * return { delay: 120 }
38
+ * })
39
+ */
40
+ declare function defineConfig(input: RouteDirectoryConfig | DefineConfigFactory): RouteDirectoryConfig;
27
41
 
28
- interface RouteDirectoryConfig {
29
- headers?: Record<string, string>;
30
- status?: number;
31
- delay?: number;
32
- enabled?: boolean;
33
- ignorePrefix?: string | string[];
34
- include?: RegExp | RegExp[];
35
- exclude?: RegExp | RegExp[];
36
- middleware?: MiddlewareHandler | MiddlewareHandler[];
37
- }
38
- interface VitePluginOptions extends MockEntryOptions {
39
- mode?: RuntimeMode;
40
- sw?: ServiceWorkerOptions;
41
- }
42
- interface MokupPluginOptions {
43
- entries?: VitePluginOptions | VitePluginOptions[];
44
- playground?: PlaygroundOptionsInput;
45
- }
46
- type VitePluginOptionsInput = MokupPluginOptions;
42
+ /**
43
+ * Define a mock route handler with type hints.
44
+ *
45
+ * @param input - Handler function or rule object.
46
+ * @returns The same handler or rule.
47
+ *
48
+ * @example
49
+ * import { defineHandler } from 'mokup'
50
+ *
51
+ * export default defineHandler((c) => {
52
+ * return { ok: true }
53
+ * })
54
+ *
55
+ * @example
56
+ * import { defineHandler } from 'mokup'
57
+ *
58
+ * export default defineHandler({
59
+ * enabled: false,
60
+ * handler: async (c) => {
61
+ * return { ok: false, method: c.req.method }
62
+ * },
63
+ * })
64
+ */
65
+ declare function defineHandler(input: RequestHandler): RequestHandler;
66
+ declare function defineHandler(input: RouteRule): RouteRule;
47
67
 
48
- export type { HttpMethod, MokupPluginOptions, RequestHandler, RouteDirectoryConfig, RouteResponse, RouteRule, RuntimeMode, ServiceWorkerOptions, VitePluginOptions, VitePluginOptionsInput };
68
+ export { MiddlewareRegistry, RequestHandler, RouteDirectoryConfig, RouteRule, defineConfig, defineHandler };
package/dist/index.mjs CHANGED
@@ -1 +1,38 @@
1
+ const middlewareSymbol = Symbol.for("mokup.config.middlewares");
2
+ function createRegistry(list) {
3
+ return {
4
+ use: (...handlers) => {
5
+ list.push(...handlers);
6
+ }
7
+ };
8
+ }
9
+ function attachMetadata(config, meta) {
10
+ Object.defineProperty(config, middlewareSymbol, {
11
+ value: meta,
12
+ enumerable: false
13
+ });
14
+ return config;
15
+ }
16
+ function defineConfig(input) {
17
+ if (typeof input === "function") {
18
+ const pre = [];
19
+ const normal = [];
20
+ const post = [];
21
+ const context = {
22
+ pre: createRegistry(pre),
23
+ normal: createRegistry(normal),
24
+ post: createRegistry(post)
25
+ };
26
+ const result = input(context);
27
+ const config2 = result && typeof result === "object" ? result : {};
28
+ return attachMetadata(config2, { pre, normal, post });
29
+ }
30
+ const config = input && typeof input === "object" ? input : {};
31
+ return attachMetadata(config, { pre: [], normal: [], post: [] });
32
+ }
1
33
 
34
+ function defineHandler(input) {
35
+ return input;
36
+ }
37
+
38
+ export { defineConfig, defineHandler };
@@ -1 +1,3 @@
1
+ /// <reference path="../types/virtual.d.ts" />
2
+
1
3
  export * from '@mokup/server/worker';