@emkodev/emkore 1.0.3
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/CHANGELOG.md +269 -0
- package/DEVELOPER_GUIDE.md +227 -0
- package/LICENSE +21 -0
- package/README.md +126 -0
- package/bun.lock +22 -0
- package/example/README.md +200 -0
- package/example/create-user.interactor.ts +88 -0
- package/example/dto/user.dto.ts +34 -0
- package/example/entity/user.entity.ts +54 -0
- package/example/index.ts +18 -0
- package/example/interface/create-user.usecase.ts +93 -0
- package/example/interface/user.repository.ts +23 -0
- package/mod.ts +1 -0
- package/package.json +32 -0
- package/src/common/abstract.actor.ts +59 -0
- package/src/common/abstract.entity.ts +59 -0
- package/src/common/abstract.interceptor.ts +17 -0
- package/src/common/abstract.repository.ts +162 -0
- package/src/common/abstract.usecase.ts +113 -0
- package/src/common/config/config-registry.ts +190 -0
- package/src/common/config/config-section.ts +106 -0
- package/src/common/exception/authorization-exception.ts +28 -0
- package/src/common/exception/repository-exception.ts +46 -0
- package/src/common/interceptor/audit-log.interceptor.ts +181 -0
- package/src/common/interceptor/authorization.interceptor.ts +252 -0
- package/src/common/interceptor/performance.interceptor.ts +101 -0
- package/src/common/llm/api-definition.type.ts +185 -0
- package/src/common/pattern/unit-of-work.ts +78 -0
- package/src/common/platform/env.ts +38 -0
- package/src/common/registry/usecase-registry.ts +80 -0
- package/src/common/type/interceptor-context.type.ts +25 -0
- package/src/common/type/json-schema.type.ts +80 -0
- package/src/common/type/json.type.ts +5 -0
- package/src/common/type/lowercase.type.ts +48 -0
- package/src/common/type/metadata.type.ts +5 -0
- package/src/common/type/money.class.ts +384 -0
- package/src/common/type/permission.type.ts +43 -0
- package/src/common/validation/validation-result.ts +52 -0
- package/src/common/validation/validators.ts +441 -0
- package/src/index.ts +95 -0
- package/test/unit/abstract-actor.test.ts +608 -0
- package/test/unit/actor.test.ts +89 -0
- package/test/unit/api-definition.test.ts +628 -0
- package/test/unit/authorization.test.ts +101 -0
- package/test/unit/entity.test.ts +95 -0
- package/test/unit/money.test.ts +480 -0
- package/test/unit/validation.test.ts +138 -0
- package/tsconfig.json +18 -0
|
@@ -0,0 +1,628 @@
|
|
|
1
|
+
import { test, expect, describe } from "bun:test";
|
|
2
|
+
import {
|
|
3
|
+
type ApiDefinition,
|
|
4
|
+
apiDefinitionToJsonSchema,
|
|
5
|
+
} from "../../src/common/llm/api-definition.type.ts";
|
|
6
|
+
|
|
7
|
+
describe("apiDefinitionToJsonSchema - Primitive parameter types", () => {
|
|
8
|
+
test("should handle string parameters", () => {
|
|
9
|
+
const definition: ApiDefinition = {
|
|
10
|
+
name: "Test Function",
|
|
11
|
+
description: "A test function",
|
|
12
|
+
parameters: [
|
|
13
|
+
{
|
|
14
|
+
name: "username",
|
|
15
|
+
description: "User name",
|
|
16
|
+
type: "string",
|
|
17
|
+
isRequired: true,
|
|
18
|
+
},
|
|
19
|
+
],
|
|
20
|
+
returns: {
|
|
21
|
+
type: "string",
|
|
22
|
+
description: "Result",
|
|
23
|
+
},
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const schema = apiDefinitionToJsonSchema(definition);
|
|
27
|
+
|
|
28
|
+
expect(schema.parameters.properties.username).toEqual({
|
|
29
|
+
type: "string",
|
|
30
|
+
description: "User name",
|
|
31
|
+
});
|
|
32
|
+
expect(schema.parameters.required).toEqual(["username"]);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
test("should handle number parameters", () => {
|
|
36
|
+
const definition: ApiDefinition = {
|
|
37
|
+
name: "Calculate",
|
|
38
|
+
description: "Calculate something",
|
|
39
|
+
parameters: [
|
|
40
|
+
{
|
|
41
|
+
name: "amount",
|
|
42
|
+
description: "The amount",
|
|
43
|
+
type: "number",
|
|
44
|
+
},
|
|
45
|
+
],
|
|
46
|
+
returns: {
|
|
47
|
+
type: "number",
|
|
48
|
+
description: "Result",
|
|
49
|
+
},
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
const schema = apiDefinitionToJsonSchema(definition);
|
|
53
|
+
|
|
54
|
+
expect(schema.parameters.properties.amount).toEqual({
|
|
55
|
+
type: "number",
|
|
56
|
+
description: "The amount",
|
|
57
|
+
});
|
|
58
|
+
expect(schema.parameters.required).toEqual(["amount"]);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
test("should handle integer parameters", () => {
|
|
62
|
+
const definition: ApiDefinition = {
|
|
63
|
+
name: "Count",
|
|
64
|
+
description: "Count items",
|
|
65
|
+
parameters: [
|
|
66
|
+
{
|
|
67
|
+
name: "count",
|
|
68
|
+
description: "Item count",
|
|
69
|
+
type: "integer",
|
|
70
|
+
},
|
|
71
|
+
],
|
|
72
|
+
returns: {
|
|
73
|
+
type: "integer",
|
|
74
|
+
description: "Total",
|
|
75
|
+
},
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
const schema = apiDefinitionToJsonSchema(definition);
|
|
79
|
+
|
|
80
|
+
expect(schema.parameters.properties.count).toEqual({
|
|
81
|
+
type: "integer",
|
|
82
|
+
description: "Item count",
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
test("should handle boolean parameters", () => {
|
|
87
|
+
const definition: ApiDefinition = {
|
|
88
|
+
name: "Toggle",
|
|
89
|
+
description: "Toggle a flag",
|
|
90
|
+
parameters: [
|
|
91
|
+
{
|
|
92
|
+
name: "isActive",
|
|
93
|
+
description: "Active flag",
|
|
94
|
+
type: "boolean",
|
|
95
|
+
},
|
|
96
|
+
],
|
|
97
|
+
returns: {
|
|
98
|
+
type: "boolean",
|
|
99
|
+
description: "New state",
|
|
100
|
+
},
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
const schema = apiDefinitionToJsonSchema(definition);
|
|
104
|
+
|
|
105
|
+
expect(schema.parameters.properties.isActive).toEqual({
|
|
106
|
+
type: "boolean",
|
|
107
|
+
description: "Active flag",
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
test("should handle null parameters", () => {
|
|
112
|
+
const definition: ApiDefinition = {
|
|
113
|
+
name: "Reset",
|
|
114
|
+
description: "Reset value",
|
|
115
|
+
parameters: [
|
|
116
|
+
{
|
|
117
|
+
name: "value",
|
|
118
|
+
description: "Nullable value",
|
|
119
|
+
type: "null",
|
|
120
|
+
},
|
|
121
|
+
],
|
|
122
|
+
returns: {
|
|
123
|
+
type: "null",
|
|
124
|
+
description: "Null result",
|
|
125
|
+
},
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
const schema = apiDefinitionToJsonSchema(definition);
|
|
129
|
+
|
|
130
|
+
expect(schema.parameters.properties.value).toEqual({
|
|
131
|
+
type: "null",
|
|
132
|
+
description: "Nullable value",
|
|
133
|
+
});
|
|
134
|
+
});
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
describe("apiDefinitionToJsonSchema - Array parameters with items", () => {
|
|
138
|
+
test("should include items for array parameters", () => {
|
|
139
|
+
const definition: ApiDefinition = {
|
|
140
|
+
name: "Process List",
|
|
141
|
+
description: "Process a list",
|
|
142
|
+
parameters: [
|
|
143
|
+
{
|
|
144
|
+
name: "ids",
|
|
145
|
+
description: "List of IDs",
|
|
146
|
+
type: "array",
|
|
147
|
+
items: {
|
|
148
|
+
type: "string",
|
|
149
|
+
description: "ID value",
|
|
150
|
+
},
|
|
151
|
+
},
|
|
152
|
+
],
|
|
153
|
+
returns: {
|
|
154
|
+
type: "array",
|
|
155
|
+
description: "Processed results",
|
|
156
|
+
items: {
|
|
157
|
+
type: "object",
|
|
158
|
+
description: "Result object",
|
|
159
|
+
},
|
|
160
|
+
},
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
const schema = apiDefinitionToJsonSchema(definition);
|
|
164
|
+
|
|
165
|
+
expect(schema.parameters.properties.ids).toEqual({
|
|
166
|
+
type: "array",
|
|
167
|
+
description: "List of IDs",
|
|
168
|
+
items: {
|
|
169
|
+
type: "string",
|
|
170
|
+
description: "ID value",
|
|
171
|
+
},
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
expect(schema.returns).toEqual({
|
|
175
|
+
type: "array",
|
|
176
|
+
description: "Processed results",
|
|
177
|
+
items: {
|
|
178
|
+
type: "object",
|
|
179
|
+
description: "Result object",
|
|
180
|
+
},
|
|
181
|
+
});
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
test("should handle array of objects with properties", () => {
|
|
185
|
+
const definition: ApiDefinition = {
|
|
186
|
+
name: "Create Users",
|
|
187
|
+
description: "Create multiple users",
|
|
188
|
+
parameters: [
|
|
189
|
+
{
|
|
190
|
+
name: "users",
|
|
191
|
+
description: "User list",
|
|
192
|
+
type: "array",
|
|
193
|
+
items: {
|
|
194
|
+
type: "object",
|
|
195
|
+
description: "User data",
|
|
196
|
+
properties: {
|
|
197
|
+
name: { type: "string" },
|
|
198
|
+
email: { type: "string" },
|
|
199
|
+
},
|
|
200
|
+
},
|
|
201
|
+
},
|
|
202
|
+
],
|
|
203
|
+
returns: {
|
|
204
|
+
type: "array",
|
|
205
|
+
description: "Created users",
|
|
206
|
+
items: {
|
|
207
|
+
type: "object",
|
|
208
|
+
properties: {
|
|
209
|
+
id: { type: "string" },
|
|
210
|
+
name: { type: "string" },
|
|
211
|
+
},
|
|
212
|
+
},
|
|
213
|
+
},
|
|
214
|
+
};
|
|
215
|
+
|
|
216
|
+
const schema = apiDefinitionToJsonSchema(definition);
|
|
217
|
+
|
|
218
|
+
expect(schema.parameters.properties.users.items.properties).toEqual({
|
|
219
|
+
name: { type: "string" },
|
|
220
|
+
email: { type: "string" },
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
expect(schema.returns.items.properties).toEqual({
|
|
224
|
+
id: { type: "string" },
|
|
225
|
+
name: { type: "string" },
|
|
226
|
+
});
|
|
227
|
+
});
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
describe("apiDefinitionToJsonSchema - Object parameters with properties", () => {
|
|
231
|
+
test("should include properties for object parameters", () => {
|
|
232
|
+
const definition: ApiDefinition = {
|
|
233
|
+
name: "Create User",
|
|
234
|
+
description: "Create a user",
|
|
235
|
+
parameters: [
|
|
236
|
+
{
|
|
237
|
+
name: "user",
|
|
238
|
+
description: "User data",
|
|
239
|
+
type: "object",
|
|
240
|
+
properties: {
|
|
241
|
+
name: { type: "string", description: "Full name" },
|
|
242
|
+
email: { type: "string", description: "Email address" },
|
|
243
|
+
age: { type: "integer", description: "Age in years" },
|
|
244
|
+
},
|
|
245
|
+
},
|
|
246
|
+
],
|
|
247
|
+
returns: {
|
|
248
|
+
type: "object",
|
|
249
|
+
description: "Created user",
|
|
250
|
+
properties: {
|
|
251
|
+
id: { type: "string" },
|
|
252
|
+
name: { type: "string" },
|
|
253
|
+
createdAt: { type: "string" },
|
|
254
|
+
},
|
|
255
|
+
},
|
|
256
|
+
};
|
|
257
|
+
|
|
258
|
+
const schema = apiDefinitionToJsonSchema(definition);
|
|
259
|
+
|
|
260
|
+
expect(schema.parameters.properties.user).toEqual({
|
|
261
|
+
type: "object",
|
|
262
|
+
description: "User data",
|
|
263
|
+
properties: {
|
|
264
|
+
name: { type: "string", description: "Full name" },
|
|
265
|
+
email: { type: "string", description: "Email address" },
|
|
266
|
+
age: { type: "integer", description: "Age in years" },
|
|
267
|
+
},
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
expect(schema.returns.properties).toEqual({
|
|
271
|
+
id: { type: "string" },
|
|
272
|
+
name: { type: "string" },
|
|
273
|
+
createdAt: { type: "string" },
|
|
274
|
+
});
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
test("should include required field for object parameters", () => {
|
|
278
|
+
const definition: ApiDefinition = {
|
|
279
|
+
name: "Update Profile",
|
|
280
|
+
description: "Update user profile",
|
|
281
|
+
parameters: [
|
|
282
|
+
{
|
|
283
|
+
name: "profile",
|
|
284
|
+
description: "Profile data",
|
|
285
|
+
type: "object",
|
|
286
|
+
properties: {
|
|
287
|
+
name: { type: "string" },
|
|
288
|
+
bio: { type: "string" },
|
|
289
|
+
},
|
|
290
|
+
required: ["name"],
|
|
291
|
+
},
|
|
292
|
+
],
|
|
293
|
+
returns: {
|
|
294
|
+
type: "object",
|
|
295
|
+
description: "Updated profile",
|
|
296
|
+
properties: {
|
|
297
|
+
name: { type: "string" },
|
|
298
|
+
},
|
|
299
|
+
required: ["name"],
|
|
300
|
+
},
|
|
301
|
+
};
|
|
302
|
+
|
|
303
|
+
const schema = apiDefinitionToJsonSchema(definition);
|
|
304
|
+
|
|
305
|
+
expect(schema.parameters.properties.profile.required).toEqual(["name"]);
|
|
306
|
+
expect(schema.returns.required).toEqual(["name"]);
|
|
307
|
+
});
|
|
308
|
+
|
|
309
|
+
test("should handle object without properties field", () => {
|
|
310
|
+
const definition: ApiDefinition = {
|
|
311
|
+
name: "Store Data",
|
|
312
|
+
description: "Store arbitrary data",
|
|
313
|
+
parameters: [
|
|
314
|
+
{
|
|
315
|
+
name: "data",
|
|
316
|
+
description: "Data object",
|
|
317
|
+
type: "object",
|
|
318
|
+
},
|
|
319
|
+
],
|
|
320
|
+
returns: {
|
|
321
|
+
type: "object",
|
|
322
|
+
description: "Stored data",
|
|
323
|
+
},
|
|
324
|
+
};
|
|
325
|
+
|
|
326
|
+
const schema = apiDefinitionToJsonSchema(definition);
|
|
327
|
+
|
|
328
|
+
expect(schema.parameters.properties.data).toEqual({
|
|
329
|
+
type: "object",
|
|
330
|
+
description: "Data object",
|
|
331
|
+
});
|
|
332
|
+
|
|
333
|
+
expect(schema.returns).toEqual({
|
|
334
|
+
type: "object",
|
|
335
|
+
description: "Stored data",
|
|
336
|
+
});
|
|
337
|
+
});
|
|
338
|
+
});
|
|
339
|
+
|
|
340
|
+
describe("apiDefinitionToJsonSchema - Required vs optional parameters", () => {
|
|
341
|
+
test("should mark parameters as required by default", () => {
|
|
342
|
+
const definition: ApiDefinition = {
|
|
343
|
+
name: "Test",
|
|
344
|
+
description: "Test function",
|
|
345
|
+
parameters: [
|
|
346
|
+
{
|
|
347
|
+
name: "param1",
|
|
348
|
+
description: "First param",
|
|
349
|
+
type: "string",
|
|
350
|
+
},
|
|
351
|
+
{
|
|
352
|
+
name: "param2",
|
|
353
|
+
description: "Second param",
|
|
354
|
+
type: "number",
|
|
355
|
+
},
|
|
356
|
+
],
|
|
357
|
+
returns: {
|
|
358
|
+
type: "string",
|
|
359
|
+
description: "Result",
|
|
360
|
+
},
|
|
361
|
+
};
|
|
362
|
+
|
|
363
|
+
const schema = apiDefinitionToJsonSchema(definition);
|
|
364
|
+
|
|
365
|
+
expect(schema.parameters.required).toEqual(["param1", "param2"]);
|
|
366
|
+
});
|
|
367
|
+
|
|
368
|
+
test("should handle explicitly required parameters", () => {
|
|
369
|
+
const definition: ApiDefinition = {
|
|
370
|
+
name: "Test",
|
|
371
|
+
description: "Test function",
|
|
372
|
+
parameters: [
|
|
373
|
+
{
|
|
374
|
+
name: "required1",
|
|
375
|
+
description: "Required param",
|
|
376
|
+
type: "string",
|
|
377
|
+
isRequired: true,
|
|
378
|
+
},
|
|
379
|
+
],
|
|
380
|
+
returns: {
|
|
381
|
+
type: "string",
|
|
382
|
+
description: "Result",
|
|
383
|
+
},
|
|
384
|
+
};
|
|
385
|
+
|
|
386
|
+
const schema = apiDefinitionToJsonSchema(definition);
|
|
387
|
+
|
|
388
|
+
expect(schema.parameters.required).toEqual(["required1"]);
|
|
389
|
+
});
|
|
390
|
+
|
|
391
|
+
test("should handle optional parameters", () => {
|
|
392
|
+
const definition: ApiDefinition = {
|
|
393
|
+
name: "Test",
|
|
394
|
+
description: "Test function",
|
|
395
|
+
parameters: [
|
|
396
|
+
{
|
|
397
|
+
name: "required",
|
|
398
|
+
description: "Required param",
|
|
399
|
+
type: "string",
|
|
400
|
+
isRequired: true,
|
|
401
|
+
},
|
|
402
|
+
{
|
|
403
|
+
name: "optional",
|
|
404
|
+
description: "Optional param",
|
|
405
|
+
type: "string",
|
|
406
|
+
isRequired: false,
|
|
407
|
+
},
|
|
408
|
+
],
|
|
409
|
+
returns: {
|
|
410
|
+
type: "string",
|
|
411
|
+
description: "Result",
|
|
412
|
+
},
|
|
413
|
+
};
|
|
414
|
+
|
|
415
|
+
const schema = apiDefinitionToJsonSchema(definition);
|
|
416
|
+
|
|
417
|
+
expect(schema.parameters.required).toEqual(["required"]);
|
|
418
|
+
expect(schema.parameters.properties.optional).toEqual({
|
|
419
|
+
type: "string",
|
|
420
|
+
description: "Optional param",
|
|
421
|
+
});
|
|
422
|
+
});
|
|
423
|
+
|
|
424
|
+
test("should handle all optional parameters", () => {
|
|
425
|
+
const definition: ApiDefinition = {
|
|
426
|
+
name: "Test",
|
|
427
|
+
description: "Test function",
|
|
428
|
+
parameters: [
|
|
429
|
+
{
|
|
430
|
+
name: "opt1",
|
|
431
|
+
description: "Optional param 1",
|
|
432
|
+
type: "string",
|
|
433
|
+
isRequired: false,
|
|
434
|
+
},
|
|
435
|
+
{
|
|
436
|
+
name: "opt2",
|
|
437
|
+
description: "Optional param 2",
|
|
438
|
+
type: "number",
|
|
439
|
+
isRequired: false,
|
|
440
|
+
},
|
|
441
|
+
],
|
|
442
|
+
returns: {
|
|
443
|
+
type: "string",
|
|
444
|
+
description: "Result",
|
|
445
|
+
},
|
|
446
|
+
};
|
|
447
|
+
|
|
448
|
+
const schema = apiDefinitionToJsonSchema(definition);
|
|
449
|
+
|
|
450
|
+
expect(schema.parameters.required).toEqual([]);
|
|
451
|
+
});
|
|
452
|
+
});
|
|
453
|
+
|
|
454
|
+
describe("apiDefinitionToJsonSchema - Empty parameters", () => {
|
|
455
|
+
test("should handle function with no parameters", () => {
|
|
456
|
+
const definition: ApiDefinition = {
|
|
457
|
+
name: "Get Time",
|
|
458
|
+
description: "Get current time",
|
|
459
|
+
parameters: [],
|
|
460
|
+
returns: {
|
|
461
|
+
type: "string",
|
|
462
|
+
description: "Current timestamp",
|
|
463
|
+
},
|
|
464
|
+
};
|
|
465
|
+
|
|
466
|
+
const schema = apiDefinitionToJsonSchema(definition);
|
|
467
|
+
|
|
468
|
+
expect(schema.parameters.properties).toEqual({});
|
|
469
|
+
expect(schema.parameters.required).toEqual([]);
|
|
470
|
+
expect(schema.returns).toEqual({
|
|
471
|
+
type: "string",
|
|
472
|
+
description: "Current timestamp",
|
|
473
|
+
});
|
|
474
|
+
});
|
|
475
|
+
});
|
|
476
|
+
|
|
477
|
+
describe("apiDefinitionToJsonSchema - Complex nested structures", () => {
|
|
478
|
+
test("should handle deeply nested object structures", () => {
|
|
479
|
+
const definition: ApiDefinition = {
|
|
480
|
+
name: "Create Order",
|
|
481
|
+
description: "Create a new order",
|
|
482
|
+
parameters: [
|
|
483
|
+
{
|
|
484
|
+
name: "order",
|
|
485
|
+
description: "Order details",
|
|
486
|
+
type: "object",
|
|
487
|
+
properties: {
|
|
488
|
+
customer: {
|
|
489
|
+
type: "object",
|
|
490
|
+
properties: {
|
|
491
|
+
name: { type: "string" },
|
|
492
|
+
email: { type: "string" },
|
|
493
|
+
},
|
|
494
|
+
},
|
|
495
|
+
items: {
|
|
496
|
+
type: "array",
|
|
497
|
+
items: {
|
|
498
|
+
type: "object",
|
|
499
|
+
properties: {
|
|
500
|
+
productId: { type: "string" },
|
|
501
|
+
quantity: { type: "integer" },
|
|
502
|
+
},
|
|
503
|
+
},
|
|
504
|
+
},
|
|
505
|
+
},
|
|
506
|
+
required: ["customer", "items"],
|
|
507
|
+
},
|
|
508
|
+
],
|
|
509
|
+
returns: {
|
|
510
|
+
type: "object",
|
|
511
|
+
description: "Created order",
|
|
512
|
+
properties: {
|
|
513
|
+
orderId: { type: "string" },
|
|
514
|
+
status: { type: "string" },
|
|
515
|
+
},
|
|
516
|
+
},
|
|
517
|
+
};
|
|
518
|
+
|
|
519
|
+
const schema = apiDefinitionToJsonSchema(definition);
|
|
520
|
+
|
|
521
|
+
expect(schema.parameters.properties.order.properties.customer).toEqual({
|
|
522
|
+
type: "object",
|
|
523
|
+
properties: {
|
|
524
|
+
name: { type: "string" },
|
|
525
|
+
email: { type: "string" },
|
|
526
|
+
},
|
|
527
|
+
});
|
|
528
|
+
|
|
529
|
+
expect(schema.parameters.properties.order.properties.items).toEqual({
|
|
530
|
+
type: "array",
|
|
531
|
+
items: {
|
|
532
|
+
type: "object",
|
|
533
|
+
properties: {
|
|
534
|
+
productId: { type: "string" },
|
|
535
|
+
quantity: { type: "integer" },
|
|
536
|
+
},
|
|
537
|
+
},
|
|
538
|
+
});
|
|
539
|
+
|
|
540
|
+
expect(schema.parameters.properties.order.required).toEqual([
|
|
541
|
+
"customer",
|
|
542
|
+
"items",
|
|
543
|
+
]);
|
|
544
|
+
});
|
|
545
|
+
});
|
|
546
|
+
|
|
547
|
+
describe("apiDefinitionToJsonSchema - Metadata preservation", () => {
|
|
548
|
+
test("should preserve name and description", () => {
|
|
549
|
+
const definition: ApiDefinition = {
|
|
550
|
+
name: "Calculate Tax",
|
|
551
|
+
description: "Calculate tax amount based on subtotal",
|
|
552
|
+
parameters: [
|
|
553
|
+
{
|
|
554
|
+
name: "subtotal",
|
|
555
|
+
description: "Subtotal amount",
|
|
556
|
+
type: "number",
|
|
557
|
+
},
|
|
558
|
+
],
|
|
559
|
+
returns: {
|
|
560
|
+
type: "number",
|
|
561
|
+
description: "Tax amount",
|
|
562
|
+
},
|
|
563
|
+
};
|
|
564
|
+
|
|
565
|
+
const schema = apiDefinitionToJsonSchema(definition);
|
|
566
|
+
|
|
567
|
+
expect(schema.name).toEqual("Calculate Tax");
|
|
568
|
+
expect(schema.description).toEqual(
|
|
569
|
+
"Calculate tax amount based on subtotal",
|
|
570
|
+
);
|
|
571
|
+
});
|
|
572
|
+
|
|
573
|
+
test("should preserve parameter descriptions", () => {
|
|
574
|
+
const definition: ApiDefinition = {
|
|
575
|
+
name: "Test",
|
|
576
|
+
description: "Test function",
|
|
577
|
+
parameters: [
|
|
578
|
+
{
|
|
579
|
+
name: "email",
|
|
580
|
+
description: "User email address in RFC 5322 format",
|
|
581
|
+
type: "string",
|
|
582
|
+
},
|
|
583
|
+
],
|
|
584
|
+
returns: {
|
|
585
|
+
type: "boolean",
|
|
586
|
+
description: "True if email is valid",
|
|
587
|
+
},
|
|
588
|
+
};
|
|
589
|
+
|
|
590
|
+
const schema = apiDefinitionToJsonSchema(definition);
|
|
591
|
+
|
|
592
|
+
expect(schema.parameters.properties.email.description).toEqual(
|
|
593
|
+
"User email address in RFC 5322 format",
|
|
594
|
+
);
|
|
595
|
+
expect(schema.returns.description).toEqual("True if email is valid");
|
|
596
|
+
});
|
|
597
|
+
});
|
|
598
|
+
|
|
599
|
+
describe("apiDefinitionToJsonSchema - JSON Schema structure", () => {
|
|
600
|
+
test("should output valid JSON Schema structure", () => {
|
|
601
|
+
const definition: ApiDefinition = {
|
|
602
|
+
name: "Test Function",
|
|
603
|
+
description: "A test",
|
|
604
|
+
parameters: [
|
|
605
|
+
{
|
|
606
|
+
name: "param1",
|
|
607
|
+
description: "First param",
|
|
608
|
+
type: "string",
|
|
609
|
+
},
|
|
610
|
+
],
|
|
611
|
+
returns: {
|
|
612
|
+
type: "string",
|
|
613
|
+
description: "Result",
|
|
614
|
+
},
|
|
615
|
+
};
|
|
616
|
+
|
|
617
|
+
const schema = apiDefinitionToJsonSchema(definition);
|
|
618
|
+
|
|
619
|
+
// Verify top-level structure
|
|
620
|
+
expect(typeof schema.name).toEqual("string");
|
|
621
|
+
expect(typeof schema.description).toEqual("string");
|
|
622
|
+
expect(typeof schema.parameters).toEqual("object");
|
|
623
|
+
expect(schema.parameters.type).toEqual("object");
|
|
624
|
+
expect(typeof schema.parameters.properties).toEqual("object");
|
|
625
|
+
expect(Array.isArray(schema.parameters.required)).toEqual(true);
|
|
626
|
+
expect(typeof schema.returns).toEqual("object");
|
|
627
|
+
});
|
|
628
|
+
});
|