xmcp 0.6.6 → 0.6.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.
Files changed (69) hide show
  1. package/README.md +32 -0
  2. package/dist/cli.js +30 -32
  3. package/dist/compiler/config/index.d.ts +54 -54
  4. package/dist/compiler/config/schemas/transport/http.d.ts +82 -82
  5. package/dist/index.d.ts +4 -2
  6. package/dist/index.js +3 -3
  7. package/dist/runtime/adapter-express.js +39 -22
  8. package/dist/runtime/adapter-nestjs.js +39 -22
  9. package/dist/runtime/adapter-nextjs.js +38 -21
  10. package/dist/runtime/contexts/http-request-context.d.ts +3 -1
  11. package/dist/runtime/headers.js +1 -1
  12. package/dist/runtime/http.js +47 -30
  13. package/dist/runtime/stdio.js +17 -11
  14. package/dist/runtime/utils/elicitation.d.ts +4 -0
  15. package/dist/runtime/utils/ext-apps-registry.d.ts +132 -0
  16. package/dist/runtime/utils/path-to-tool-name.d.ts +20 -0
  17. package/dist/runtime/utils/prompt-loader.d.ts +11 -0
  18. package/dist/runtime/utils/prompts.d.ts +8 -0
  19. package/dist/runtime/utils/react/generate-html.d.ts +1 -0
  20. package/dist/runtime/utils/react/index.d.ts +4 -0
  21. package/dist/runtime/utils/resource-loader.d.ts +11 -0
  22. package/dist/runtime/utils/resources.d.ts +4 -0
  23. package/dist/runtime/utils/server.d.ts +35 -0
  24. package/dist/runtime/utils/tool-loader.d.ts +11 -0
  25. package/dist/runtime/utils/tools.d.ts +11 -0
  26. package/dist/runtime/utils/transformers/prompt.d.ts +32 -0
  27. package/dist/runtime/utils/transformers/resource.d.ts +26 -0
  28. package/dist/runtime/utils/transformers/tool.d.ts +30 -0
  29. package/dist/runtime/utils/ui/flatten-meta.d.ts +6 -0
  30. package/dist/runtime/utils/ui/split-meta.d.ts +17 -0
  31. package/dist/runtime/utils/utils/resource-uri-composer.d.ts +60 -0
  32. package/dist/runtime/utils/validators/index.d.ts +13 -0
  33. package/dist/types/client-info.d.ts +12 -0
  34. package/dist/types/tool.d.ts +69 -14
  35. package/dist/utils/context.d.ts +1 -1
  36. package/package.json +9 -1
  37. package/src/client/index.ts +4 -1
  38. package/src/compiler/generate-tools-code.ts +4 -6
  39. package/src/compiler/get-bundler-config/plugins/index.ts +0 -1
  40. package/src/compiler/index.ts +24 -7
  41. package/src/index.ts +8 -1
  42. package/src/runtime/adapters/express/index.ts +45 -47
  43. package/src/runtime/adapters/nestjs/xmcp.service.ts +4 -3
  44. package/src/runtime/adapters/nextjs/handler/node-to-web-adapter.ts +40 -6
  45. package/src/runtime/adapters/nextjs/handler/request-converter.ts +20 -0
  46. package/src/runtime/adapters/nextjs/handler/server-lifecycle.ts +8 -5
  47. package/src/runtime/adapters/nextjs/index.ts +41 -25
  48. package/src/runtime/contexts/http-request-context.ts +2 -0
  49. package/src/runtime/headers.ts +37 -2
  50. package/src/runtime/platforms/cloudflare/cors.ts +0 -1
  51. package/src/runtime/platforms/cloudflare/worker.ts +46 -44
  52. package/src/runtime/transports/http/index.ts +0 -4
  53. package/src/runtime/transports/http/stateless-streamable-http.ts +131 -233
  54. package/src/runtime/transports/stdio/index.ts +0 -2
  55. package/src/runtime/utils/__tests__/client-info.test.ts +80 -0
  56. package/src/runtime/utils/__tests__/tool-extra.test.ts +110 -0
  57. package/src/runtime/utils/client-info.ts +71 -0
  58. package/src/runtime/utils/elicitation.ts +396 -0
  59. package/src/runtime/utils/prompt-loader.ts +151 -0
  60. package/src/runtime/utils/resource-loader.ts +151 -0
  61. package/src/runtime/utils/resources.ts +0 -8
  62. package/src/runtime/utils/server.ts +28 -38
  63. package/src/runtime/utils/tool-loader.ts +149 -0
  64. package/src/runtime/utils/tools.ts +9 -6
  65. package/src/runtime/utils/transformers/tool.ts +26 -5
  66. package/src/types/client-info.ts +12 -0
  67. package/src/types/injected.d.ts +62 -0
  68. package/src/types/tool.ts +99 -17
  69. package/src/utils/context.ts +2 -2
@@ -0,0 +1,80 @@
1
+ import { test } from "node:test";
2
+ import assert from "node:assert";
3
+ import {
4
+ extractClientInfoFromMessage,
5
+ extractClientInfoFromMessages,
6
+ } from "../client-info";
7
+
8
+ test("extractClientInfoFromMessage returns undefined for non-initialize methods", () => {
9
+ const result = extractClientInfoFromMessage({
10
+ jsonrpc: "2.0",
11
+ method: "tools/call",
12
+ params: {},
13
+ });
14
+
15
+ assert.strictEqual(result, undefined);
16
+ });
17
+
18
+ test("extractClientInfoFromMessage extracts required and optional fields", () => {
19
+ const result = extractClientInfoFromMessage({
20
+ jsonrpc: "2.0",
21
+ method: "initialize",
22
+ params: {
23
+ clientInfo: {
24
+ name: "cursor",
25
+ version: "0.50.1",
26
+ title: "Cursor",
27
+ websiteUrl: "https://cursor.com",
28
+ description: "AI code editor",
29
+ },
30
+ },
31
+ });
32
+
33
+ assert.deepStrictEqual(result, {
34
+ name: "cursor",
35
+ version: "0.50.1",
36
+ title: "Cursor",
37
+ websiteUrl: "https://cursor.com",
38
+ description: "AI code editor",
39
+ });
40
+ });
41
+
42
+ test("extractClientInfoFromMessage returns undefined for invalid clientInfo payload", () => {
43
+ const result = extractClientInfoFromMessage({
44
+ jsonrpc: "2.0",
45
+ method: "initialize",
46
+ params: {
47
+ clientInfo: {
48
+ name: "cursor",
49
+ },
50
+ },
51
+ });
52
+
53
+ assert.strictEqual(result, undefined);
54
+ });
55
+
56
+ test("extractClientInfoFromMessages supports batch requests", () => {
57
+ const result = extractClientInfoFromMessages([
58
+ {
59
+ jsonrpc: "2.0",
60
+ method: "ping",
61
+ id: 1,
62
+ },
63
+ {
64
+ jsonrpc: "2.0",
65
+ method: "initialize",
66
+ id: 2,
67
+ params: {
68
+ clientInfo: {
69
+ name: "opencode",
70
+ version: "1.2.3",
71
+ },
72
+ },
73
+ },
74
+ ]);
75
+
76
+ assert.deepStrictEqual(result, {
77
+ name: "opencode",
78
+ version: "1.2.3",
79
+ });
80
+ });
@@ -0,0 +1,110 @@
1
+ import { test } from "node:test";
2
+ import assert from "node:assert";
3
+ import {
4
+ transformToolHandler,
5
+ type UserToolResponse,
6
+ } from "../transformers/tool";
7
+ import { httpRequestContextProvider } from "../../contexts/http-request-context";
8
+
9
+ type MinimalExtra = {
10
+ signal: AbortSignal;
11
+ requestId: string;
12
+ sendNotification: (notification: unknown) => Promise<void>;
13
+ sendRequest: (request: unknown, resultSchema: unknown) => Promise<unknown>;
14
+ };
15
+
16
+ const createMinimalExtra = (requestId: string): MinimalExtra => ({
17
+ signal: new AbortController().signal,
18
+ requestId,
19
+ sendNotification: async () => undefined,
20
+ sendRequest: async () => ({}),
21
+ });
22
+
23
+ const invokeHandler = (
24
+ handler: unknown,
25
+ requestId: string
26
+ ): Promise<UserToolResponse> => {
27
+ const typedHandler = handler as (
28
+ args: Record<string, never>,
29
+ extra: MinimalExtra
30
+ ) => Promise<UserToolResponse>;
31
+
32
+ return typedHandler({}, createMinimalExtra(requestId));
33
+ };
34
+
35
+ test("transformToolHandler forwards clientInfo through tool extra arguments", async () => {
36
+ const transformedHandler = transformToolHandler((args, extra) => {
37
+ return {
38
+ structuredContent: {
39
+ ...args,
40
+ clientInfoName: extra.clientInfo?.name,
41
+ clientInfoVersion: extra.clientInfo?.version,
42
+ },
43
+ };
44
+ });
45
+
46
+ const result = await new Promise<UserToolResponse>((resolve, reject) => {
47
+ httpRequestContextProvider(
48
+ {
49
+ id: "request-id",
50
+ headers: {},
51
+ clientInfo: {
52
+ name: "cursor",
53
+ version: "0.50.1",
54
+ title: "Cursor",
55
+ },
56
+ },
57
+ () => {
58
+ invokeHandler(transformedHandler, "rpc-1").then(resolve).catch(reject);
59
+ }
60
+ );
61
+ });
62
+
63
+ assert.deepStrictEqual(result, {
64
+ structuredContent: {
65
+ clientInfoName: "cursor",
66
+ clientInfoVersion: "0.50.1",
67
+ },
68
+ content: [
69
+ {
70
+ type: "text",
71
+ text: '{"clientInfoName":"cursor","clientInfoVersion":"0.50.1"}',
72
+ },
73
+ ],
74
+ });
75
+ });
76
+
77
+ test("transformToolHandler leaves clientInfo undefined when request has no initialize metadata", async () => {
78
+ const transformedHandler = transformToolHandler((_args, extra) => {
79
+ return {
80
+ structuredContent: {
81
+ hasClientInfo: extra.clientInfo !== undefined,
82
+ },
83
+ };
84
+ });
85
+
86
+ const result = await new Promise<UserToolResponse>((resolve, reject) => {
87
+ httpRequestContextProvider(
88
+ {
89
+ id: "request-id-2",
90
+ headers: {},
91
+ clientInfo: undefined,
92
+ },
93
+ () => {
94
+ invokeHandler(transformedHandler, "rpc-2").then(resolve).catch(reject);
95
+ }
96
+ );
97
+ });
98
+
99
+ assert.deepStrictEqual(result, {
100
+ structuredContent: {
101
+ hasClientInfo: false,
102
+ },
103
+ content: [
104
+ {
105
+ type: "text",
106
+ text: '{"hasClientInfo":false}',
107
+ },
108
+ ],
109
+ });
110
+ });
@@ -0,0 +1,71 @@
1
+ import type { JsonRpcMessage } from "@/runtime/transports/http/base-streamable-http";
2
+ import type { McpClientInfo } from "@/types/client-info";
3
+
4
+ const isObject = (value: unknown): value is Record<string, unknown> => {
5
+ return typeof value === "object" && value !== null && !Array.isArray(value);
6
+ };
7
+
8
+ const parseClientInfoCandidate = (
9
+ candidate: unknown
10
+ ): McpClientInfo | undefined => {
11
+ if (!isObject(candidate)) {
12
+ return undefined;
13
+ }
14
+
15
+ if (
16
+ typeof candidate.name !== "string" ||
17
+ typeof candidate.version !== "string"
18
+ ) {
19
+ return undefined;
20
+ }
21
+
22
+ const result: McpClientInfo = {
23
+ name: candidate.name,
24
+ version: candidate.version,
25
+ };
26
+
27
+ if (typeof candidate.title === "string") {
28
+ result.title = candidate.title;
29
+ }
30
+
31
+ if (typeof candidate.websiteUrl === "string") {
32
+ result.websiteUrl = candidate.websiteUrl;
33
+ }
34
+
35
+ if (typeof candidate.description === "string") {
36
+ result.description = candidate.description;
37
+ }
38
+
39
+ return result;
40
+ };
41
+
42
+ export const extractClientInfoFromMessage = (
43
+ message: JsonRpcMessage | undefined
44
+ ): McpClientInfo | undefined => {
45
+ if (!message || message.method !== "initialize") {
46
+ return undefined;
47
+ }
48
+
49
+ if (!isObject(message.params)) {
50
+ return undefined;
51
+ }
52
+
53
+ return parseClientInfoCandidate(message.params.clientInfo);
54
+ };
55
+
56
+ export const extractClientInfoFromMessages = (
57
+ payload: unknown
58
+ ): McpClientInfo | undefined => {
59
+ const messages = Array.isArray(payload)
60
+ ? (payload as JsonRpcMessage[])
61
+ : ([payload] as JsonRpcMessage[]);
62
+
63
+ for (const message of messages) {
64
+ const clientInfo = extractClientInfoFromMessage(message);
65
+ if (clientInfo) {
66
+ return clientInfo;
67
+ }
68
+ }
69
+
70
+ return undefined;
71
+ };
@@ -0,0 +1,396 @@
1
+ import {
2
+ ElicitRequestFormParamsSchema,
3
+ ElicitRequestURLParamsSchema,
4
+ ElicitResultSchema,
5
+ } from "@modelcontextprotocol/sdk/types";
6
+ import type {
7
+ ElicitFormField,
8
+ ElicitFormRequest,
9
+ ElicitRequest,
10
+ ElicitResult,
11
+ ElicitUrlRequest,
12
+ ToolExtraArguments,
13
+ ToolRequestOptions,
14
+ } from "@/types/tool";
15
+
16
+ type ElicitRequestSender = Pick<ToolExtraArguments, "sendRequest">;
17
+
18
+ const ALLOWED_FORM_REQUEST_KEYS = new Set(["message", "mode", "requestedSchema"]);
19
+ const ALLOWED_URL_REQUEST_KEYS = new Set([
20
+ "elicitationId",
21
+ "message",
22
+ "mode",
23
+ "url",
24
+ ]);
25
+ const ALLOWED_FORM_SCHEMA_KEYS = new Set(["properties", "required", "type"]);
26
+ const ALLOWED_STRING_FIELD_KEYS = new Set([
27
+ "default",
28
+ "description",
29
+ "format",
30
+ "maxLength",
31
+ "minLength",
32
+ "title",
33
+ "type",
34
+ ]);
35
+ const ALLOWED_ENUM_FIELD_KEYS = new Set([
36
+ "default",
37
+ "description",
38
+ "enum",
39
+ "enumNames",
40
+ "title",
41
+ "type",
42
+ ]);
43
+ const ALLOWED_BOOLEAN_FIELD_KEYS = new Set([
44
+ "default",
45
+ "description",
46
+ "title",
47
+ "type",
48
+ ]);
49
+ const ALLOWED_NUMBER_FIELD_KEYS = new Set([
50
+ "default",
51
+ "description",
52
+ "maximum",
53
+ "minimum",
54
+ "title",
55
+ "type",
56
+ ]);
57
+ const ALLOWED_STRING_FORMATS = new Set(["date", "date-time", "email", "uri"]);
58
+
59
+ export async function elicitFromTool(
60
+ extra: ElicitRequestSender,
61
+ request: ElicitRequest,
62
+ options?: ToolRequestOptions
63
+ ): Promise<ElicitResult> {
64
+ const params = normalizeElicitationRequest(request);
65
+ const result = await extra.sendRequest(
66
+ { method: "elicitation/create", params },
67
+ ElicitResultSchema,
68
+ options
69
+ );
70
+
71
+ return {
72
+ ...result,
73
+ content: result.content ?? undefined,
74
+ };
75
+ }
76
+
77
+ function normalizeElicitationRequest(request: ElicitRequest) {
78
+ const mode = request.mode ?? "form";
79
+
80
+ if (mode === "url") {
81
+ assertValidUrlRequest(request);
82
+ return ElicitRequestURLParamsSchema.parse(request);
83
+ }
84
+
85
+ if (mode !== "form") {
86
+ throw new Error(
87
+ `Unsupported elicitation mode "${String((request as { mode?: unknown }).mode)}". Expected "form" or "url".`
88
+ );
89
+ }
90
+
91
+ assertValidFormRequest(request);
92
+ return ElicitRequestFormParamsSchema.parse({
93
+ ...request,
94
+ mode: "form",
95
+ });
96
+ }
97
+
98
+ function assertValidFormRequest(
99
+ request: ElicitRequest
100
+ ): asserts request is ElicitFormRequest {
101
+ assertPlainObject(request, "Elicitation request must be a plain object.");
102
+ assertAllowedKeys(request, ALLOWED_FORM_REQUEST_KEYS, "Form elicitation");
103
+ assertString(request.message, "Form elicitation requires a string message.");
104
+
105
+ const requestedSchema = request.requestedSchema;
106
+ assertPlainObject(
107
+ requestedSchema,
108
+ "Form elicitation requires a flat object schema."
109
+ );
110
+ assertAllowedKeys(
111
+ requestedSchema,
112
+ ALLOWED_FORM_SCHEMA_KEYS,
113
+ "Form elicitation schema"
114
+ );
115
+
116
+ if (requestedSchema.type !== "object") {
117
+ throw new Error('Form elicitation schema must use type "object".');
118
+ }
119
+
120
+ assertPlainObject(
121
+ requestedSchema.properties,
122
+ "Form elicitation schema requires a properties object."
123
+ );
124
+
125
+ for (const [fieldName, field] of Object.entries(requestedSchema.properties)) {
126
+ assertSupportedFormField(fieldName, field);
127
+ }
128
+
129
+ if (typeof requestedSchema.required === "undefined") {
130
+ return;
131
+ }
132
+
133
+ if (
134
+ !Array.isArray(requestedSchema.required) ||
135
+ requestedSchema.required.some((fieldName) => typeof fieldName !== "string")
136
+ ) {
137
+ throw new Error(
138
+ "Form elicitation schema required fields must be a string array."
139
+ );
140
+ }
141
+
142
+ for (const fieldName of requestedSchema.required) {
143
+ if (!(fieldName in requestedSchema.properties)) {
144
+ throw new Error(
145
+ `Form elicitation schema required field "${fieldName}" is not defined in properties.`
146
+ );
147
+ }
148
+ }
149
+ }
150
+
151
+ function assertValidUrlRequest(
152
+ request: ElicitRequest
153
+ ): asserts request is ElicitUrlRequest {
154
+ assertPlainObject(request, "Elicitation request must be a plain object.");
155
+ assertAllowedKeys(request, ALLOWED_URL_REQUEST_KEYS, "URL elicitation");
156
+ assertString(request.message, "URL elicitation requires a string message.");
157
+ assertNonEmptyString(
158
+ request.elicitationId,
159
+ "URL elicitation requires a non-empty elicitationId."
160
+ );
161
+ assertNonEmptyString(request.url, "URL elicitation requires a non-empty URL.");
162
+
163
+ let parsedUrl: URL;
164
+ try {
165
+ parsedUrl = new URL(request.url);
166
+ } catch {
167
+ throw new Error("URL elicitation requires a valid http or https URL.");
168
+ }
169
+
170
+ if (parsedUrl.protocol !== "http:" && parsedUrl.protocol !== "https:") {
171
+ throw new Error("URL elicitation requires a valid http or https URL.");
172
+ }
173
+ }
174
+
175
+ function assertSupportedFormField(
176
+ fieldName: string,
177
+ field: unknown
178
+ ): asserts field is ElicitFormField {
179
+ assertPlainObject(
180
+ field,
181
+ `Form elicitation field "${fieldName}" must be a plain object.`
182
+ );
183
+ assertOptionalString(
184
+ field.title,
185
+ `Form elicitation field "${fieldName}" title must be a string.`
186
+ );
187
+ assertOptionalString(
188
+ field.description,
189
+ `Form elicitation field "${fieldName}" description must be a string.`
190
+ );
191
+
192
+ if (field.type === "string" && "enum" in field) {
193
+ assertAllowedKeys(
194
+ field,
195
+ ALLOWED_ENUM_FIELD_KEYS,
196
+ `Form elicitation field "${fieldName}"`
197
+ );
198
+ assertStringArray(
199
+ field.enum,
200
+ `Form elicitation field "${fieldName}" enum values must be strings.`
201
+ );
202
+ assertOptionalStringArray(
203
+ field.enumNames,
204
+ `Form elicitation field "${fieldName}" enumNames must be strings.`
205
+ );
206
+
207
+ if (
208
+ typeof field.enumNames !== "undefined" &&
209
+ field.enumNames.length !== field.enum.length
210
+ ) {
211
+ throw new Error(
212
+ `Form elicitation field "${fieldName}" enumNames length must match enum length.`
213
+ );
214
+ }
215
+
216
+ assertOptionalString(
217
+ field.default,
218
+ `Form elicitation field "${fieldName}" default must be a string.`
219
+ );
220
+
221
+ if (
222
+ typeof field.default !== "undefined" &&
223
+ !field.enum.includes(field.default)
224
+ ) {
225
+ throw new Error(
226
+ `Form elicitation field "${fieldName}" default must be one of its enum values.`
227
+ );
228
+ }
229
+
230
+ return;
231
+ }
232
+
233
+ if (field.type === "string" && !("enum" in field)) {
234
+ assertAllowedKeys(
235
+ field,
236
+ ALLOWED_STRING_FIELD_KEYS,
237
+ `Form elicitation field "${fieldName}"`
238
+ );
239
+ assertOptionalNumber(
240
+ field.minLength,
241
+ `Form elicitation field "${fieldName}" minLength must be a number.`
242
+ );
243
+ assertOptionalNumber(
244
+ field.maxLength,
245
+ `Form elicitation field "${fieldName}" maxLength must be a number.`
246
+ );
247
+ assertOptionalString(
248
+ field.default,
249
+ `Form elicitation field "${fieldName}" default must be a string.`
250
+ );
251
+
252
+ const format = field.format;
253
+ assertOptionalString(
254
+ format,
255
+ `Form elicitation field "${fieldName}" format must be a string.`
256
+ );
257
+
258
+ if (typeof format !== "undefined" && !ALLOWED_STRING_FORMATS.has(format)) {
259
+ throw new Error(
260
+ `Form elicitation field "${fieldName}" format must be one of: ${Array.from(ALLOWED_STRING_FORMATS).join(", ")}.`
261
+ );
262
+ }
263
+
264
+ return;
265
+ }
266
+
267
+ if (field.type === "boolean") {
268
+ assertAllowedKeys(
269
+ field,
270
+ ALLOWED_BOOLEAN_FIELD_KEYS,
271
+ `Form elicitation field "${fieldName}"`
272
+ );
273
+
274
+ if (
275
+ typeof field.default !== "undefined" &&
276
+ typeof field.default !== "boolean"
277
+ ) {
278
+ throw new Error(
279
+ `Form elicitation field "${fieldName}" default must be a boolean.`
280
+ );
281
+ }
282
+
283
+ return;
284
+ }
285
+
286
+ if (field.type === "number" || field.type === "integer") {
287
+ assertAllowedKeys(
288
+ field,
289
+ ALLOWED_NUMBER_FIELD_KEYS,
290
+ `Form elicitation field "${fieldName}"`
291
+ );
292
+ assertOptionalNumber(
293
+ field.minimum,
294
+ `Form elicitation field "${fieldName}" minimum must be a number.`
295
+ );
296
+ assertOptionalNumber(
297
+ field.maximum,
298
+ `Form elicitation field "${fieldName}" maximum must be a number.`
299
+ );
300
+ assertOptionalNumber(
301
+ field.default,
302
+ `Form elicitation field "${fieldName}" default must be a number.`
303
+ );
304
+
305
+ return;
306
+ }
307
+
308
+ throw new Error(
309
+ `Form elicitation field "${fieldName}" uses unsupported type "${String((field as { type?: unknown }).type)}". Supported types are string, boolean, number, integer, and string enums.`
310
+ );
311
+ }
312
+
313
+ function assertAllowedKeys(
314
+ value: Record<string, unknown>,
315
+ allowedKeys: Set<string>,
316
+ label: string
317
+ ): void {
318
+ const extraKeys = Object.keys(value).filter((key) => !allowedKeys.has(key));
319
+
320
+ if (extraKeys.length === 0) {
321
+ return;
322
+ }
323
+
324
+ throw new Error(
325
+ `${label} includes unsupported key(s): ${extraKeys.join(", ")}.`
326
+ );
327
+ }
328
+
329
+ function assertPlainObject(
330
+ value: unknown,
331
+ errorMessage: string
332
+ ): asserts value is Record<string, unknown> {
333
+ if (
334
+ typeof value !== "object" ||
335
+ value === null ||
336
+ Array.isArray(value)
337
+ ) {
338
+ throw new Error(errorMessage);
339
+ }
340
+ }
341
+
342
+ function assertString(
343
+ value: unknown,
344
+ errorMessage: string
345
+ ): asserts value is string {
346
+ if (typeof value !== "string") {
347
+ throw new Error(errorMessage);
348
+ }
349
+ }
350
+
351
+ function assertNonEmptyString(
352
+ value: unknown,
353
+ errorMessage: string
354
+ ): asserts value is string {
355
+ if (typeof value !== "string" || value.trim().length === 0) {
356
+ throw new Error(errorMessage);
357
+ }
358
+ }
359
+
360
+ function assertOptionalString(
361
+ value: unknown,
362
+ errorMessage: string
363
+ ): asserts value is string | undefined {
364
+ if (typeof value !== "undefined" && typeof value !== "string") {
365
+ throw new Error(errorMessage);
366
+ }
367
+ }
368
+
369
+ function assertOptionalNumber(
370
+ value: unknown,
371
+ errorMessage: string
372
+ ): asserts value is number | undefined {
373
+ if (typeof value !== "undefined" && typeof value !== "number") {
374
+ throw new Error(errorMessage);
375
+ }
376
+ }
377
+
378
+ function assertStringArray(
379
+ value: unknown,
380
+ errorMessage: string
381
+ ): asserts value is string[] {
382
+ if (!Array.isArray(value) || value.some((item) => typeof item !== "string")) {
383
+ throw new Error(errorMessage);
384
+ }
385
+ }
386
+
387
+ function assertOptionalStringArray(
388
+ value: unknown,
389
+ errorMessage: string
390
+ ): asserts value is string[] | undefined {
391
+ if (typeof value === "undefined") {
392
+ return;
393
+ }
394
+
395
+ assertStringArray(value, errorMessage);
396
+ }