fork of hey-api/openapi-ts because I need some additional things
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

chore: Zod Mini plugin

Lubos bb81984f 2926c1d9

+174 -86
+1 -1
packages/openapi-ts-tests/test/openapi-ts.config.ts
··· 228 228 { 229 229 // case: 'snake_case', 230 230 // comments: false, 231 - compatibilityVersion: 4, 231 + compatibilityVersion: 'mini', 232 232 dates: { 233 233 offset: true, 234 234 },
+6
packages/openapi-ts/src/plugins/zod/constants.ts
··· 1 1 import { compiler } from '../../compiler'; 2 2 3 3 export const identifiers = { 4 + _default: compiler.identifier({ text: '_default' }), 4 5 and: compiler.identifier({ text: 'and' }), 5 6 array: compiler.identifier({ text: 'array' }), 6 7 bigint: compiler.identifier({ text: 'bigint' }), 7 8 boolean: compiler.identifier({ text: 'boolean' }), 9 + check: compiler.identifier({ text: 'check' }), 8 10 coerce: compiler.identifier({ text: 'coerce' }), 9 11 date: compiler.identifier({ text: 'date' }), 10 12 datetime: compiler.identifier({ text: 'datetime' }), ··· 12 14 describe: compiler.identifier({ text: 'describe' }), 13 15 email: compiler.identifier({ text: 'email' }), 14 16 enum: compiler.identifier({ text: 'enum' }), 17 + globalRegistry: compiler.identifier({ text: 'globalRegistry' }), 15 18 gt: compiler.identifier({ text: 'gt' }), 16 19 gte: compiler.identifier({ text: 'gte' }), 17 20 infer: compiler.identifier({ text: 'infer' }), ··· 27 30 lt: compiler.identifier({ text: 'lt' }), 28 31 lte: compiler.identifier({ text: 'lte' }), 29 32 max: compiler.identifier({ text: 'max' }), 33 + maxLength: compiler.identifier({ text: 'maxLength' }), 30 34 min: compiler.identifier({ text: 'min' }), 35 + minLength: compiler.identifier({ text: 'minLength' }), 31 36 never: compiler.identifier({ text: 'never' }), 32 37 null: compiler.identifier({ text: 'null' }), 33 38 nullable: compiler.identifier({ text: 'nullable' }), ··· 38 43 readonly: compiler.identifier({ text: 'readonly' }), 39 44 record: compiler.identifier({ text: 'record' }), 40 45 regex: compiler.identifier({ text: 'regex' }), 46 + register: compiler.identifier({ text: 'register' }), 41 47 string: compiler.identifier({ text: 'string' }), 42 48 time: compiler.identifier({ text: 'time' }), 43 49 tuple: compiler.identifier({ text: 'tuple' }),
+148 -80
packages/openapi-ts/src/plugins/zod/mini/plugin.ts
··· 285 285 if (!isBigInt && schema.type === 'integer') { 286 286 numberExpression = compiler.callExpression({ 287 287 functionName: compiler.propertyAccessExpression({ 288 - expression: numberExpression, 288 + expression: identifiers.z, 289 289 name: identifiers.int, 290 290 }), 291 291 }); 292 292 } 293 + 294 + const checks: Array<ts.Expression> = []; 293 295 294 296 if (schema.exclusiveMinimum !== undefined) { 295 - numberExpression = compiler.callExpression({ 296 - functionName: compiler.propertyAccessExpression({ 297 - expression: numberExpression, 298 - name: identifiers.gt, 297 + checks.push( 298 + compiler.callExpression({ 299 + functionName: compiler.propertyAccessExpression({ 300 + expression: identifiers.z, 301 + name: identifiers.gt, 302 + }), 303 + parameters: [ 304 + numberParameter({ isBigInt, value: schema.exclusiveMinimum }), 305 + ], 299 306 }), 300 - parameters: [ 301 - numberParameter({ isBigInt, value: schema.exclusiveMinimum }), 302 - ], 303 - }); 307 + ); 304 308 } else if (schema.minimum !== undefined) { 305 - numberExpression = compiler.callExpression({ 306 - functionName: compiler.propertyAccessExpression({ 307 - expression: numberExpression, 308 - name: identifiers.gte, 309 + checks.push( 310 + compiler.callExpression({ 311 + functionName: compiler.propertyAccessExpression({ 312 + expression: identifiers.z, 313 + name: identifiers.gte, 314 + }), 315 + parameters: [numberParameter({ isBigInt, value: schema.minimum })], 309 316 }), 310 - parameters: [numberParameter({ isBigInt, value: schema.minimum })], 311 - }); 317 + ); 312 318 } 313 319 314 320 if (schema.exclusiveMaximum !== undefined) { 315 - numberExpression = compiler.callExpression({ 316 - functionName: compiler.propertyAccessExpression({ 317 - expression: numberExpression, 318 - name: identifiers.lt, 321 + checks.push( 322 + compiler.callExpression({ 323 + functionName: compiler.propertyAccessExpression({ 324 + expression: identifiers.z, 325 + name: identifiers.lt, 326 + }), 327 + parameters: [ 328 + numberParameter({ isBigInt, value: schema.exclusiveMaximum }), 329 + ], 319 330 }), 320 - parameters: [ 321 - numberParameter({ isBigInt, value: schema.exclusiveMaximum }), 322 - ], 323 - }); 331 + ); 324 332 } else if (schema.maximum !== undefined) { 333 + checks.push( 334 + compiler.callExpression({ 335 + functionName: compiler.propertyAccessExpression({ 336 + expression: identifiers.z, 337 + name: identifiers.lte, 338 + }), 339 + parameters: [numberParameter({ isBigInt, value: schema.maximum })], 340 + }), 341 + ); 342 + } 343 + 344 + if (checks.length) { 325 345 numberExpression = compiler.callExpression({ 326 346 functionName: compiler.propertyAccessExpression({ 327 347 expression: numberExpression, 328 - name: identifiers.lte, 348 + name: identifiers.check, 329 349 }), 330 - parameters: [numberParameter({ isBigInt, value: schema.maximum })], 350 + parameters: checks, 331 351 }); 332 352 } 333 353 ··· 343 363 schema: SchemaWithType<'object'>; 344 364 state: State; 345 365 }): { 346 - anyType: string; 347 366 expression: ts.CallExpression; 348 367 } => { 349 368 // TODO: parser - handle constants ··· 404 423 expression: identifiers.z, 405 424 name: identifiers.record, 406 425 }), 407 - parameters: [zodSchema], 426 + parameters: [ 427 + compiler.callExpression({ 428 + functionName: compiler.propertyAccessExpression({ 429 + expression: identifiers.z, 430 + name: identifiers.string, 431 + }), 432 + parameters: [], 433 + }), 434 + zodSchema, 435 + ], 408 436 }); 409 437 return { 410 - anyType: 'AnyZodObject', 411 438 expression, 412 439 }; 413 440 } ··· 420 447 parameters: [ts.factory.createObjectLiteralExpression(properties, true)], 421 448 }); 422 449 return { 423 - anyType: 'AnyZodObject', 424 450 expression, 425 451 }; 426 452 }; ··· 455 481 case 'date': 456 482 stringExpression = compiler.callExpression({ 457 483 functionName: compiler.propertyAccessExpression({ 458 - expression: stringExpression, 484 + expression: compiler.propertyAccessExpression({ 485 + expression: identifiers.z, 486 + name: identifiers.iso, 487 + }), 459 488 name: identifiers.date, 460 489 }), 461 490 }); ··· 463 492 case 'date-time': 464 493 stringExpression = compiler.callExpression({ 465 494 functionName: compiler.propertyAccessExpression({ 466 - expression: stringExpression, 495 + expression: compiler.propertyAccessExpression({ 496 + expression: identifiers.z, 497 + name: identifiers.iso, 498 + }), 467 499 name: identifiers.datetime, 468 500 }), 469 501 parameters: plugin.config.dates.offset ··· 483 515 case 'email': 484 516 stringExpression = compiler.callExpression({ 485 517 functionName: compiler.propertyAccessExpression({ 486 - expression: stringExpression, 518 + expression: identifiers.z, 487 519 name: identifiers.email, 488 520 }), 489 521 }); 490 522 break; 491 523 case 'ipv4': 524 + stringExpression = compiler.callExpression({ 525 + functionName: compiler.propertyAccessExpression({ 526 + expression: identifiers.z, 527 + name: identifiers.ipv4, 528 + }), 529 + }); 530 + break; 492 531 case 'ipv6': 493 532 stringExpression = compiler.callExpression({ 494 533 functionName: compiler.propertyAccessExpression({ 495 - expression: stringExpression, 496 - name: identifiers.ip, 534 + expression: identifiers.z, 535 + name: identifiers.ipv6, 497 536 }), 498 537 }); 499 538 break; 500 539 case 'time': 501 540 stringExpression = compiler.callExpression({ 502 541 functionName: compiler.propertyAccessExpression({ 503 - expression: stringExpression, 542 + expression: compiler.propertyAccessExpression({ 543 + expression: identifiers.z, 544 + name: identifiers.iso, 545 + }), 504 546 name: identifiers.time, 505 547 }), 506 548 }); ··· 508 550 case 'uri': 509 551 stringExpression = compiler.callExpression({ 510 552 functionName: compiler.propertyAccessExpression({ 511 - expression: stringExpression, 553 + expression: identifiers.z, 512 554 name: identifiers.url, 513 555 }), 514 556 }); ··· 516 558 case 'uuid': 517 559 stringExpression = compiler.callExpression({ 518 560 functionName: compiler.propertyAccessExpression({ 519 - expression: stringExpression, 561 + expression: identifiers.z, 520 562 name: identifiers.uuid, 521 563 }), 522 564 }); 523 565 break; 524 566 } 525 567 } 568 + 569 + const checks: Array<ts.Expression> = []; 526 570 527 571 if (schema.minLength === schema.maxLength && schema.minLength !== undefined) { 528 - stringExpression = compiler.callExpression({ 529 - functionName: compiler.propertyAccessExpression({ 530 - expression: stringExpression, 531 - name: identifiers.length, 572 + checks.push( 573 + compiler.callExpression({ 574 + functionName: compiler.propertyAccessExpression({ 575 + expression: identifiers.z, 576 + name: identifiers.length, 577 + }), 578 + parameters: [compiler.valueToExpression({ value: schema.minLength })], 532 579 }), 533 - parameters: [compiler.valueToExpression({ value: schema.minLength })], 534 - }); 580 + ); 535 581 } else { 536 582 if (schema.minLength !== undefined) { 537 - stringExpression = compiler.callExpression({ 538 - functionName: compiler.propertyAccessExpression({ 539 - expression: stringExpression, 540 - name: identifiers.min, 583 + checks.push( 584 + compiler.callExpression({ 585 + functionName: compiler.propertyAccessExpression({ 586 + expression: identifiers.z, 587 + name: identifiers.minLength, 588 + }), 589 + parameters: [compiler.valueToExpression({ value: schema.minLength })], 541 590 }), 542 - parameters: [compiler.valueToExpression({ value: schema.minLength })], 543 - }); 591 + ); 544 592 } 545 593 546 594 if (schema.maxLength !== undefined) { 547 - stringExpression = compiler.callExpression({ 548 - functionName: compiler.propertyAccessExpression({ 549 - expression: stringExpression, 550 - name: identifiers.max, 595 + checks.push( 596 + compiler.callExpression({ 597 + functionName: compiler.propertyAccessExpression({ 598 + expression: identifiers.z, 599 + name: identifiers.maxLength, 600 + }), 601 + parameters: [compiler.valueToExpression({ value: schema.maxLength })], 551 602 }), 552 - parameters: [compiler.valueToExpression({ value: schema.maxLength })], 553 - }); 603 + ); 554 604 } 555 605 } 556 606 557 607 if (schema.pattern) { 608 + checks.push( 609 + compiler.callExpression({ 610 + functionName: compiler.propertyAccessExpression({ 611 + expression: identifiers.z, 612 + name: identifiers.regex, 613 + }), 614 + parameters: [ 615 + compiler.regularExpressionLiteral({ text: schema.pattern }), 616 + ], 617 + }), 618 + ); 619 + } 620 + 621 + if (checks.length) { 558 622 stringExpression = compiler.callExpression({ 559 623 functionName: compiler.propertyAccessExpression({ 560 624 expression: stringExpression, 561 - name: identifiers.regex, 625 + name: identifiers.check, 562 626 }), 563 - parameters: [compiler.regularExpressionLiteral({ text: schema.pattern })], 627 + parameters: checks, 564 628 }); 565 629 } 566 630 ··· 672 736 schema: IR.SchemaObject; 673 737 state: State; 674 738 }): { 675 - anyType?: string; 676 739 expression: ts.Expression; 677 740 } => { 678 741 switch (schema.type as Required<IR.SchemaObject>['type']) { ··· 800 863 }), 801 864 ], 802 865 }); 803 - state.hasCircularReference = true; 804 866 } else if (!file.getName(id)) { 805 867 // if $ref hasn't been processed yet, inline it to avoid the 806 868 // "Block-scoped variable used before its declaration." error ··· 825 887 } else if (schema.type) { 826 888 const zSchema = schemaTypeToZodSchema({ plugin, schema, state }); 827 889 zodSchema.expression = zSchema.expression; 828 - zodSchema.typeName = zSchema.anyType; 829 890 830 891 if (plugin.config.metadata && schema.description) { 831 892 zodSchema.expression = compiler.callExpression({ 832 893 functionName: compiler.propertyAccessExpression({ 833 894 expression: zodSchema.expression, 834 - name: identifiers.describe, 895 + name: identifiers.register, 835 896 }), 836 - parameters: [compiler.stringLiteral({ text: schema.description })], 897 + parameters: [ 898 + compiler.propertyAccessExpression({ 899 + expression: identifiers.z, 900 + name: identifiers.globalRegistry, 901 + }), 902 + compiler.objectExpression({ 903 + obj: [ 904 + { 905 + key: 'description', 906 + value: compiler.stringLiteral({ text: schema.description }), 907 + }, 908 + ], 909 + }), 910 + ], 837 911 }); 838 912 } 839 913 } else if (schema.items) { ··· 870 944 itemTypes.slice(1).forEach((item) => { 871 945 zodSchema.expression = compiler.callExpression({ 872 946 functionName: compiler.propertyAccessExpression({ 873 - expression: zodSchema.expression!, 874 - name: identifiers.and, 947 + expression: identifiers.z, 948 + name: identifiers.intersection, 875 949 }), 876 - parameters: [item], 950 + parameters: [zodSchema.expression, item], 877 951 }); 878 952 }); 879 953 } ··· 903 977 state, 904 978 }); 905 979 zodSchema.expression = zSchema.expression; 906 - zodSchema.typeName = zSchema.anyType; 907 980 } 908 981 909 982 if (zodSchema.expression) { 910 983 if (schema.accessScope === 'read') { 911 984 zodSchema.expression = compiler.callExpression({ 912 985 functionName: compiler.propertyAccessExpression({ 913 - expression: zodSchema.expression, 986 + expression: identifiers.z, 914 987 name: identifiers.readonly, 915 988 }), 989 + parameters: [zodSchema.expression], 916 990 }); 917 991 } 918 992 919 993 if (optional) { 920 994 zodSchema.expression = compiler.callExpression({ 921 995 functionName: compiler.propertyAccessExpression({ 922 - expression: zodSchema.expression, 996 + expression: identifiers.z, 923 997 name: identifiers.optional, 924 998 }), 999 + parameters: [zodSchema.expression], 925 1000 }); 926 1001 } 927 1002 ··· 934 1009 if (callParameter) { 935 1010 zodSchema.expression = compiler.callExpression({ 936 1011 functionName: compiler.propertyAccessExpression({ 937 - expression: zodSchema.expression, 938 - name: identifiers.default, 1012 + expression: identifiers.z, 1013 + name: identifiers._default, 939 1014 }), 940 - parameters: [callParameter], 1015 + parameters: [zodSchema.expression, callParameter], 941 1016 }); 942 1017 } 943 1018 } 944 - } 945 - 946 - if (state.hasCircularReference) { 947 - if (!zodSchema.typeName) { 948 - zodSchema.typeName = 'ZodTypeAny'; 949 - } 950 - } else { 951 - zodSchema.typeName = undefined; 952 1019 } 953 1020 954 1021 return zodSchema as ZodSchema; ··· 1015 1082 }); 1016 1083 1017 1084 file.import({ 1085 + alias: identifiers.z.text, 1018 1086 module: getZodModule({ plugin }), 1019 - name: 'z', 1087 + name: '*', 1020 1088 }); 1021 1089 1022 1090 plugin.forEach('operation', 'parameter', 'requestBody', 'schema', (event) => {
+1 -1
packages/openapi-ts/src/plugins/zod/v3/plugin.ts
··· 1016 1016 1017 1017 file.import({ 1018 1018 module: getZodModule({ plugin }), 1019 - name: 'z', 1019 + name: identifiers.z.text, 1020 1020 }); 1021 1021 1022 1022 plugin.forEach('operation', 'parameter', 'requestBody', 'schema', (event) => {
+18 -4
packages/openapi-ts/src/plugins/zod/v4/plugin.ts
··· 850 850 zodSchema.expression = compiler.callExpression({ 851 851 functionName: compiler.propertyAccessExpression({ 852 852 expression: zodSchema.expression, 853 - name: identifiers.describe, 853 + name: identifiers.register, 854 854 }), 855 - parameters: [compiler.stringLiteral({ text: schema.description })], 855 + parameters: [ 856 + compiler.propertyAccessExpression({ 857 + expression: identifiers.z, 858 + name: identifiers.globalRegistry, 859 + }), 860 + compiler.objectExpression({ 861 + obj: [ 862 + { 863 + key: 'description', 864 + value: compiler.stringLiteral({ text: schema.description }), 865 + }, 866 + ], 867 + }), 868 + ], 856 869 }); 857 870 } 858 871 } else if (schema.items) { ··· 937 950 if (optional) { 938 951 zodSchema.expression = compiler.callExpression({ 939 952 functionName: compiler.propertyAccessExpression({ 940 - expression: zodSchema.expression, 953 + expression: identifiers.z, 941 954 name: identifiers.optional, 942 955 }), 956 + parameters: [zodSchema.expression], 943 957 }); 944 958 } 945 959 ··· 1026 1040 1027 1041 file.import({ 1028 1042 module: getZodModule({ plugin }), 1029 - name: 'z', 1043 + name: identifiers.z.text, 1030 1044 }); 1031 1045 1032 1046 plugin.forEach('operation', 'parameter', 'requestBody', 'schema', (event) => {