@amqp-contract/zod 0.0.1
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/LICENSE +21 -0
- package/README.md +79 -0
- package/dist/index.cjs +51 -0
- package/dist/index.d.cts +14 -0
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.mts +14 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +51 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +65 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Benoit Travers
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
# @amqp-contract/zod
|
|
2
|
+
|
|
3
|
+
Zod integration for amqp-contract. This package provides a schema converter for AsyncAPI generation and declares peer dependencies for [Zod](https://zod.dev/) compatibility with amqp-contract.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @amqp-contract/contract @amqp-contract/zod zod
|
|
9
|
+
# or
|
|
10
|
+
pnpm add @amqp-contract/contract @amqp-contract/zod zod
|
|
11
|
+
# or
|
|
12
|
+
yarn add @amqp-contract/contract @amqp-contract/zod zod
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
### Basic Usage
|
|
18
|
+
|
|
19
|
+
```typescript
|
|
20
|
+
import { z } from 'zod';
|
|
21
|
+
import {
|
|
22
|
+
defineContract,
|
|
23
|
+
defineExchange,
|
|
24
|
+
defineQueue,
|
|
25
|
+
definePublisher,
|
|
26
|
+
defineConsumer,
|
|
27
|
+
} from '@amqp-contract/contract';
|
|
28
|
+
|
|
29
|
+
// Define your schemas using Zod
|
|
30
|
+
const orderSchema = z.object({
|
|
31
|
+
orderId: z.string(),
|
|
32
|
+
amount: z.number(),
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
// Define your contract
|
|
36
|
+
const contract = defineContract({
|
|
37
|
+
exchanges: {
|
|
38
|
+
orders: defineExchange('orders', 'topic', { durable: true }),
|
|
39
|
+
},
|
|
40
|
+
queues: {
|
|
41
|
+
orderProcessing: defineQueue('order-processing', { durable: true }),
|
|
42
|
+
},
|
|
43
|
+
publishers: {
|
|
44
|
+
orderCreated: definePublisher('orders', orderSchema),
|
|
45
|
+
},
|
|
46
|
+
consumers: {
|
|
47
|
+
processOrder: defineConsumer('order-processing', orderSchema),
|
|
48
|
+
},
|
|
49
|
+
});
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### AsyncAPI Generation
|
|
53
|
+
|
|
54
|
+
```typescript
|
|
55
|
+
import { zodToJsonSchema } from '@amqp-contract/zod';
|
|
56
|
+
import { generateAsyncAPI } from '@amqp-contract/asyncapi';
|
|
57
|
+
|
|
58
|
+
// Convert Zod schemas to JSON Schema for AsyncAPI
|
|
59
|
+
const jsonSchema = zodToJsonSchema(orderSchema);
|
|
60
|
+
|
|
61
|
+
// Generate AsyncAPI specification
|
|
62
|
+
const asyncAPISpec = generateAsyncAPI(contract, {
|
|
63
|
+
info: {
|
|
64
|
+
title: 'My API',
|
|
65
|
+
version: '1.0.0',
|
|
66
|
+
},
|
|
67
|
+
});
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
## Features
|
|
71
|
+
|
|
72
|
+
- **Type Safety**: Full TypeScript support with type inference from Zod schemas
|
|
73
|
+
- **Standard Schema**: Uses the Standard Schema specification for interoperability
|
|
74
|
+
- **AsyncAPI Support**: Provides `zodToJsonSchema` converter for AsyncAPI generation
|
|
75
|
+
- **Peer Dependency Management**: Declares compatible Zod versions as peer dependencies
|
|
76
|
+
|
|
77
|
+
## License
|
|
78
|
+
|
|
79
|
+
MIT
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
|
|
2
|
+
//#region src/schema-converter.ts
|
|
3
|
+
/**
|
|
4
|
+
* Convert a Zod schema to JSON Schema (AsyncAPI format)
|
|
5
|
+
*
|
|
6
|
+
* This converter specifically handles Zod schemas and converts them
|
|
7
|
+
* to JSON Schema format compatible with AsyncAPI.
|
|
8
|
+
*/
|
|
9
|
+
function zodToJsonSchema(schema) {
|
|
10
|
+
if (typeof schema !== "object" || schema === null) return { type: "object" };
|
|
11
|
+
const schemaType = schema["type"];
|
|
12
|
+
const def = schema["def"];
|
|
13
|
+
if (schemaType === "object") {
|
|
14
|
+
const shape = def?.["shape"];
|
|
15
|
+
if (!shape) return { type: "object" };
|
|
16
|
+
const properties = {};
|
|
17
|
+
const required = [];
|
|
18
|
+
for (const [key, value] of Object.entries(shape)) {
|
|
19
|
+
properties[key] = zodToJsonSchema(value);
|
|
20
|
+
if (value?.["type"] !== "optional") required.push(key);
|
|
21
|
+
}
|
|
22
|
+
const result = {
|
|
23
|
+
type: "object",
|
|
24
|
+
properties
|
|
25
|
+
};
|
|
26
|
+
if (required.length > 0) result.required = required;
|
|
27
|
+
return result;
|
|
28
|
+
}
|
|
29
|
+
if (schemaType === "string") return { type: "string" };
|
|
30
|
+
if (schemaType === "number") return { type: "number" };
|
|
31
|
+
if (schemaType === "boolean") return { type: "boolean" };
|
|
32
|
+
if (schemaType === "array") {
|
|
33
|
+
const element = def?.["element"];
|
|
34
|
+
return {
|
|
35
|
+
type: "array",
|
|
36
|
+
items: element ? zodToJsonSchema(element) : { type: "object" }
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
if (schemaType === "optional") {
|
|
40
|
+
const innerType = def?.["innerType"];
|
|
41
|
+
return innerType ? zodToJsonSchema(innerType) : { type: "object" };
|
|
42
|
+
}
|
|
43
|
+
if (schemaType === "enum" || schemaType === "nativeEnum") return {
|
|
44
|
+
type: "string",
|
|
45
|
+
enum: def?.["values"] || []
|
|
46
|
+
};
|
|
47
|
+
return { type: "object" };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
//#endregion
|
|
51
|
+
exports.zodToJsonSchema = zodToJsonSchema;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { AsyncAPISchema } from "@amqp-contract/asyncapi";
|
|
2
|
+
|
|
3
|
+
//#region src/schema-converter.d.ts
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Convert a Zod schema to JSON Schema (AsyncAPI format)
|
|
7
|
+
*
|
|
8
|
+
* This converter specifically handles Zod schemas and converts them
|
|
9
|
+
* to JSON Schema format compatible with AsyncAPI.
|
|
10
|
+
*/
|
|
11
|
+
declare function zodToJsonSchema(schema: Record<string, unknown>): AsyncAPISchema;
|
|
12
|
+
//#endregion
|
|
13
|
+
export { zodToJsonSchema };
|
|
14
|
+
//# sourceMappingURL=index.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.cts","names":[],"sources":["../src/schema-converter.ts"],"sourcesContent":[],"mappings":";;;;;;AAQA;;;;iBAAgB,eAAA,SAAwB,0BAA0B"}
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { AsyncAPISchema } from "@amqp-contract/asyncapi";
|
|
2
|
+
|
|
3
|
+
//#region src/schema-converter.d.ts
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Convert a Zod schema to JSON Schema (AsyncAPI format)
|
|
7
|
+
*
|
|
8
|
+
* This converter specifically handles Zod schemas and converts them
|
|
9
|
+
* to JSON Schema format compatible with AsyncAPI.
|
|
10
|
+
*/
|
|
11
|
+
declare function zodToJsonSchema(schema: Record<string, unknown>): AsyncAPISchema;
|
|
12
|
+
//#endregion
|
|
13
|
+
export { zodToJsonSchema };
|
|
14
|
+
//# sourceMappingURL=index.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/schema-converter.ts"],"sourcesContent":[],"mappings":";;;;;;AAQA;;;;iBAAgB,eAAA,SAAwB,0BAA0B"}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
//#region src/schema-converter.ts
|
|
2
|
+
/**
|
|
3
|
+
* Convert a Zod schema to JSON Schema (AsyncAPI format)
|
|
4
|
+
*
|
|
5
|
+
* This converter specifically handles Zod schemas and converts them
|
|
6
|
+
* to JSON Schema format compatible with AsyncAPI.
|
|
7
|
+
*/
|
|
8
|
+
function zodToJsonSchema(schema) {
|
|
9
|
+
if (typeof schema !== "object" || schema === null) return { type: "object" };
|
|
10
|
+
const schemaType = schema["type"];
|
|
11
|
+
const def = schema["def"];
|
|
12
|
+
if (schemaType === "object") {
|
|
13
|
+
const shape = def?.["shape"];
|
|
14
|
+
if (!shape) return { type: "object" };
|
|
15
|
+
const properties = {};
|
|
16
|
+
const required = [];
|
|
17
|
+
for (const [key, value] of Object.entries(shape)) {
|
|
18
|
+
properties[key] = zodToJsonSchema(value);
|
|
19
|
+
if (value?.["type"] !== "optional") required.push(key);
|
|
20
|
+
}
|
|
21
|
+
const result = {
|
|
22
|
+
type: "object",
|
|
23
|
+
properties
|
|
24
|
+
};
|
|
25
|
+
if (required.length > 0) result.required = required;
|
|
26
|
+
return result;
|
|
27
|
+
}
|
|
28
|
+
if (schemaType === "string") return { type: "string" };
|
|
29
|
+
if (schemaType === "number") return { type: "number" };
|
|
30
|
+
if (schemaType === "boolean") return { type: "boolean" };
|
|
31
|
+
if (schemaType === "array") {
|
|
32
|
+
const element = def?.["element"];
|
|
33
|
+
return {
|
|
34
|
+
type: "array",
|
|
35
|
+
items: element ? zodToJsonSchema(element) : { type: "object" }
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
if (schemaType === "optional") {
|
|
39
|
+
const innerType = def?.["innerType"];
|
|
40
|
+
return innerType ? zodToJsonSchema(innerType) : { type: "object" };
|
|
41
|
+
}
|
|
42
|
+
if (schemaType === "enum" || schemaType === "nativeEnum") return {
|
|
43
|
+
type: "string",
|
|
44
|
+
enum: def?.["values"] || []
|
|
45
|
+
};
|
|
46
|
+
return { type: "object" };
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
//#endregion
|
|
50
|
+
export { zodToJsonSchema };
|
|
51
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","names":["properties: Record<string, AsyncAPISchema>","required: string[]","result: AsyncAPISchema"],"sources":["../src/schema-converter.ts"],"sourcesContent":["import type { AsyncAPISchema } from \"@amqp-contract/asyncapi\";\n\n/**\n * Convert a Zod schema to JSON Schema (AsyncAPI format)\n *\n * This converter specifically handles Zod schemas and converts them\n * to JSON Schema format compatible with AsyncAPI.\n */\nexport function zodToJsonSchema(schema: Record<string, unknown>): AsyncAPISchema {\n // Handle Zod structure\n if (typeof schema !== \"object\" || schema === null) {\n return { type: \"object\" };\n }\n\n // Get the schema type - Zod 4.x uses `type` at top level\n const schemaType = schema[\"type\"];\n const def = schema[\"def\"] as Record<string, unknown> | undefined;\n\n // Handle different Zod types based on the type field\n if (schemaType === \"object\") {\n // For Zod objects, shape is in def.shape\n const shape = def?.[\"shape\"] as Record<string, unknown> | undefined;\n if (!shape) {\n return { type: \"object\" };\n }\n\n const properties: Record<string, AsyncAPISchema> = {};\n const required: string[] = [];\n\n for (const [key, value] of Object.entries(shape)) {\n properties[key] = zodToJsonSchema(value as Record<string, unknown>);\n\n // Check if optional - optional schemas have type \"optional\"\n const valueType = (value as Record<string, unknown>)?.[\"type\"];\n if (valueType !== \"optional\") {\n required.push(key);\n }\n }\n\n const result: AsyncAPISchema = {\n type: \"object\",\n properties,\n };\n\n if (required.length > 0) {\n result.required = required;\n }\n\n return result;\n }\n\n if (schemaType === \"string\") {\n return { type: \"string\" };\n }\n\n if (schemaType === \"number\") {\n return { type: \"number\" };\n }\n\n if (schemaType === \"boolean\") {\n return { type: \"boolean\" };\n }\n\n if (schemaType === \"array\") {\n // For arrays, the element type is in def.element\n const element = def?.[\"element\"] as Record<string, unknown> | undefined;\n return {\n type: \"array\",\n items: element ? zodToJsonSchema(element) : { type: \"object\" },\n };\n }\n\n if (schemaType === \"optional\") {\n // For optional types, get the inner type\n const innerType = def?.[\"innerType\"] as Record<string, unknown> | undefined;\n return innerType ? zodToJsonSchema(innerType) : { type: \"object\" };\n }\n\n if (schemaType === \"enum\" || schemaType === \"nativeEnum\") {\n // For enums, values are in def.values\n const values = def?.[\"values\"] as unknown[] | undefined;\n return {\n type: \"string\",\n enum: values || [],\n };\n }\n\n // Default fallback\n return { type: \"object\" };\n}\n"],"mappings":";;;;;;;AAQA,SAAgB,gBAAgB,QAAiD;AAE/E,KAAI,OAAO,WAAW,YAAY,WAAW,KAC3C,QAAO,EAAE,MAAM,UAAU;CAI3B,MAAM,aAAa,OAAO;CAC1B,MAAM,MAAM,OAAO;AAGnB,KAAI,eAAe,UAAU;EAE3B,MAAM,QAAQ,MAAM;AACpB,MAAI,CAAC,MACH,QAAO,EAAE,MAAM,UAAU;EAG3B,MAAMA,aAA6C,EAAE;EACrD,MAAMC,WAAqB,EAAE;AAE7B,OAAK,MAAM,CAAC,KAAK,UAAU,OAAO,QAAQ,MAAM,EAAE;AAChD,cAAW,OAAO,gBAAgB,MAAiC;AAInE,OADmB,QAAoC,YACrC,WAChB,UAAS,KAAK,IAAI;;EAItB,MAAMC,SAAyB;GAC7B,MAAM;GACN;GACD;AAED,MAAI,SAAS,SAAS,EACpB,QAAO,WAAW;AAGpB,SAAO;;AAGT,KAAI,eAAe,SACjB,QAAO,EAAE,MAAM,UAAU;AAG3B,KAAI,eAAe,SACjB,QAAO,EAAE,MAAM,UAAU;AAG3B,KAAI,eAAe,UACjB,QAAO,EAAE,MAAM,WAAW;AAG5B,KAAI,eAAe,SAAS;EAE1B,MAAM,UAAU,MAAM;AACtB,SAAO;GACL,MAAM;GACN,OAAO,UAAU,gBAAgB,QAAQ,GAAG,EAAE,MAAM,UAAU;GAC/D;;AAGH,KAAI,eAAe,YAAY;EAE7B,MAAM,YAAY,MAAM;AACxB,SAAO,YAAY,gBAAgB,UAAU,GAAG,EAAE,MAAM,UAAU;;AAGpE,KAAI,eAAe,UAAU,eAAe,aAG1C,QAAO;EACL,MAAM;EACN,MAHa,MAAM,aAGH,EAAE;EACnB;AAIH,QAAO,EAAE,MAAM,UAAU"}
|
package/package.json
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@amqp-contract/zod",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Zod integration for amqp-contract",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"amqp",
|
|
7
|
+
"typescript",
|
|
8
|
+
"contract",
|
|
9
|
+
"rabbitmq",
|
|
10
|
+
"zod"
|
|
11
|
+
],
|
|
12
|
+
"homepage": "https://github.com/btravers/amqp-contract#readme",
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/btravers/amqp-contract/issues"
|
|
15
|
+
},
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "https://github.com/btravers/amqp-contract.git",
|
|
19
|
+
"directory": "packages/zod"
|
|
20
|
+
},
|
|
21
|
+
"license": "MIT",
|
|
22
|
+
"author": "Benoit TRAVERS <benoit.travers.fr@gmail.com>",
|
|
23
|
+
"type": "module",
|
|
24
|
+
"exports": {
|
|
25
|
+
".": {
|
|
26
|
+
"import": {
|
|
27
|
+
"types": "./dist/index.d.mts",
|
|
28
|
+
"default": "./dist/index.mjs"
|
|
29
|
+
},
|
|
30
|
+
"require": {
|
|
31
|
+
"types": "./dist/index.d.cts",
|
|
32
|
+
"default": "./dist/index.cjs"
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
"./package.json": "./package.json"
|
|
36
|
+
},
|
|
37
|
+
"main": "./dist/index.cjs",
|
|
38
|
+
"module": "./dist/index.mjs",
|
|
39
|
+
"types": "./dist/index.d.mts",
|
|
40
|
+
"files": [
|
|
41
|
+
"dist"
|
|
42
|
+
],
|
|
43
|
+
"dependencies": {
|
|
44
|
+
"@amqp-contract/asyncapi": "0.0.1",
|
|
45
|
+
"@amqp-contract/contract": "0.0.1"
|
|
46
|
+
},
|
|
47
|
+
"devDependencies": {
|
|
48
|
+
"@vitest/coverage-v8": "4.0.15",
|
|
49
|
+
"tsdown": "0.17.2",
|
|
50
|
+
"typescript": "5.9.3",
|
|
51
|
+
"vitest": "4.0.15",
|
|
52
|
+
"zod": "4.1.13",
|
|
53
|
+
"@amqp-contract/tsconfig": "0.0.0"
|
|
54
|
+
},
|
|
55
|
+
"peerDependencies": {
|
|
56
|
+
"zod": "^3.23.0 || ^4.0.0"
|
|
57
|
+
},
|
|
58
|
+
"scripts": {
|
|
59
|
+
"build": "tsdown src/index.ts --format cjs,esm --dts --clean",
|
|
60
|
+
"dev": "tsdown src/index.ts --format cjs,esm --dts --watch",
|
|
61
|
+
"test": "vitest run",
|
|
62
|
+
"test:watch": "vitest",
|
|
63
|
+
"typecheck": "tsc --noEmit"
|
|
64
|
+
}
|
|
65
|
+
}
|