@orpc/zod 0.0.0-next.e7d7758 → 0.0.0-next.e98b833

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/README.md ADDED
@@ -0,0 +1,133 @@
1
+ <div align="center">
2
+ <image align="center" src="https://orpc.unnoq.com/logo.webp" width=280 alt="oRPC logo" />
3
+ </div>
4
+
5
+ <h1></h1>
6
+
7
+ <div align="center">
8
+ <a href="https://codecov.io/gh/unnoq/orpc">
9
+ <img alt="codecov" src="https://codecov.io/gh/unnoq/orpc/branch/main/graph/badge.svg">
10
+ </a>
11
+ <a href="https://www.npmjs.com/package/@orpc/zod">
12
+ <img alt="weekly downloads" src="https://img.shields.io/npm/dw/%40orpc%2Fzod?logo=npm" />
13
+ </a>
14
+ <a href="https://github.com/unnoq/orpc/blob/main/LICENSE">
15
+ <img alt="MIT License" src="https://img.shields.io/github/license/unnoq/orpc?logo=open-source-initiative" />
16
+ </a>
17
+ <a href="https://discord.gg/TXEbwRBvQn">
18
+ <img alt="Discord" src="https://img.shields.io/discord/1308966753044398161?color=7389D8&label&logo=discord&logoColor=ffffff" />
19
+ </a>
20
+ </div>
21
+
22
+ <h3 align="center">Typesafe APIs Made Simple 🪄</h3>
23
+
24
+ **oRPC is a powerful combination of RPC and OpenAPI**, makes it easy to build APIs that are end-to-end type-safe and adhere to OpenAPI standards, ensuring a smooth and enjoyable developer experience.
25
+
26
+ ---
27
+
28
+ ## Highlights
29
+
30
+ - **End-to-End Type Safety 🔒**: Ensure complete type safety from inputs to outputs and errors, bridging server and client seamlessly.
31
+ - **First-Class OpenAPI 📄**: Adheres to the OpenAPI standard out of the box, ensuring seamless integration and comprehensive API documentation.
32
+ - **Contract-First Development 📜**: (Optional) Define your API contract upfront and implement it with confidence.
33
+ - **Exceptional Developer Experience ✨**: Enjoy a streamlined workflow with robust typing and clear, in-code documentation.
34
+ - **Multi-Runtime Support 🌍**: Run your code seamlessly on Cloudflare, Deno, Bun, Node.js, and more.
35
+ - **Framework Integrations 🧩**: Supports Tanstack Query (React, Vue, Solid), Pinia Colada, and more.
36
+ - **Server Actions ⚡️**: Fully compatible with React Server Actions on Next.js, TanStack Start, and more.
37
+ - **Standard Schema Support 🗂️**: Effortlessly work with Zod, Valibot, ArkType, and others right out of the box.
38
+ - **Fast & Lightweight 💨**: Built on native APIs across all runtimes – optimized for speed and efficiency.
39
+ - **Native Types 📦**: Enjoy built-in support for Date, File, Blob, BigInt, URL and more with no extra setup.
40
+ - **Lazy Router ⏱️**: Improve cold start times with our lazy routing feature.
41
+ - **SSE & Streaming 📡**: Provides SSE and streaming features – perfect for real-time notifications and AI-powered streaming responses.
42
+ - **Reusability 🔄**: Write once and reuse your code across multiple purposes effortlessly.
43
+ - **Extendability 🔌**: Easily enhance oRPC with plugins, middleware, and interceptors.
44
+ - **Reliability 🛡️**: Well-tested, fully TypeScript, production-ready, and MIT licensed for peace of mind.
45
+ - **Simplicity 💡**: Enjoy straightforward, clean code with no hidden magic.
46
+
47
+ ## Documentation
48
+
49
+ You can find the full documentation [here](https://orpc.unnoq.com).
50
+
51
+ ## Packages
52
+
53
+ - [@orpc/contract](https://www.npmjs.com/package/@orpc/contract): Build your API contract.
54
+ - [@orpc/server](https://www.npmjs.com/package/@orpc/server): Build your API or implement API contract.
55
+ - [@orpc/client](https://www.npmjs.com/package/@orpc/client): Consume your API on the client with type-safety.
56
+ - [@orpc/react-query](https://www.npmjs.com/package/@orpc/react-query): Integration with [React Query](https://tanstack.com/query/latest/docs/framework/react/overview).
57
+ - [@orpc/vue-query](https://www.npmjs.com/package/@orpc/vue-query): Integration with [Vue Query](https://tanstack.com/query/latest/docs/framework/vue/overview).
58
+ - [@orpc/solid-query](https://www.npmjs.com/package/@orpc/solid-query): Integration with [Solid Query](https://tanstack.com/query/latest/docs/framework/solid/overview).
59
+ - [@orpc/vue-colada](https://www.npmjs.com/package/@orpc/vue-colada): Integration with [Pinia Colada](https://pinia-colada.esm.dev/).
60
+ - [@orpc/openapi](https://www.npmjs.com/package/@orpc/openapi): Generate OpenAPI specs and handle OpenAPI requests.
61
+ - [@orpc/zod](https://www.npmjs.com/package/@orpc/zod): More schemas that [Zod](https://zod.dev/) doesn't support yet.
62
+
63
+ ## `@orpc/zod`
64
+
65
+ More schemas that [Zod](https://zod.dev/) doesn't support yet, and provides `ZodToJsonSchemaConverter` for generating OpenAPI specs.
66
+
67
+ ### More Schemas
68
+
69
+ - `oz.url`: Zod schema for [URL](https://developer.mozilla.org/en-US/docs/Web/API/URL) instance.
70
+ - `oz.blob`: Zod schema for [Blob](https://developer.mozilla.org/en-US/docs/Web/API/Blob) instance.
71
+ - `oz.file`: Zod schema for [File](https://developer.mozilla.org/en-US/docs/Web/API/File) instance.
72
+ - `oz.regexp`: Zod schema for [RegExp](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp) instance.
73
+
74
+ ```ts
75
+ import { oz } from '@orpc/zod'
76
+ import { z } from 'zod'
77
+
78
+ const Example = z.object({
79
+ url: oz.url(),
80
+ blob: oz.blob(),
81
+ file: oz.file().type('image/png'),
82
+ regexp: oz.regexp(),
83
+ })
84
+ ```
85
+
86
+ ### Generate OpenAPI Spec
87
+
88
+ ```ts
89
+ import { OpenAPIGenerator } from '@orpc/openapi'
90
+ import { ZodToJsonSchemaConverter } from '@orpc/zod'
91
+
92
+ const openAPIGenerator = new OpenAPIGenerator({
93
+ schemaConverters: [new ZodToJsonSchemaConverter()],
94
+ })
95
+
96
+ const specFromContract = await openAPIGenerator.generate(contract, {
97
+ info: {
98
+ title: 'My App',
99
+ version: '0.0.0',
100
+ },
101
+ })
102
+
103
+ const specFromRouter = await openAPIGenerator.generate(router, {
104
+ info: {
105
+ title: 'My App',
106
+ version: '0.0.0',
107
+ },
108
+ })
109
+ ```
110
+
111
+ ### Extending the Specification
112
+
113
+ ```ts
114
+ import { oz } from '@orpc/zod'
115
+ import { z } from 'zod'
116
+
117
+ const InputSchema = oz.openapi(
118
+ z.object({
119
+ name: z.string(),
120
+ }),
121
+ {
122
+ examples: [
123
+ { name: 'Earth' },
124
+ { name: 'Mars' },
125
+ ],
126
+ // additional options...
127
+ }
128
+ )
129
+ ```
130
+
131
+ ## License
132
+
133
+ Distributed under the MIT License. See [LICENSE](https://github.com/unnoq/orpc/blob/main/LICENSE) for more information.
@@ -0,0 +1,82 @@
1
+ import { JSONSchema, ConditionalSchemaConverter, SchemaConvertOptions } from '@orpc/openapi';
2
+ import { ZodTypeAny, input, output, ZodTypeDef, CustomErrorParams, ZodType, ZodEffects } from 'zod';
3
+ import { Context } from '@orpc/server';
4
+ import { HandlerPlugin } from '@orpc/server/plugins';
5
+ import { StandardHandlerOptions } from '@orpc/server/standard';
6
+ import { AnySchema } from '@orpc/contract';
7
+
8
+ declare function getCustomJsonSchema(def: ZodTypeDef, options: {
9
+ strategy: 'input' | 'output' | 'both';
10
+ }): Exclude<JSONSchema, boolean> | undefined;
11
+ declare function customJsonSchema<T extends ZodTypeAny, TStrategy extends 'input' | 'output' | 'both' = 'both'>(schema: T, custom: Exclude<JSONSchema<TStrategy extends 'input' ? input<T> : TStrategy extends 'output' ? output<T> : input<T> & output<T>>, boolean>, options?: {
12
+ strategy?: TStrategy;
13
+ }): T;
14
+
15
+ type CustomParams = CustomErrorParams & {
16
+ fatal?: boolean;
17
+ };
18
+ type CustomZodDef = {
19
+ type: 'blob' | 'regexp' | 'url';
20
+ } | {
21
+ type: 'file';
22
+ mimeType?: string;
23
+ };
24
+ declare function setCustomZodDef<T extends ZodTypeDef>(def: T, custom: CustomZodDef): void;
25
+ declare function getCustomZodDef(def: ZodTypeDef): CustomZodDef | undefined;
26
+ declare function composeParams<T = unknown>(defaultMessage: (input: T) => string, params: undefined | string | CustomParams | ((input: T) => CustomParams)): (input: T) => CustomParams;
27
+
28
+ declare function blob(params?: string | CustomParams | ((input: unknown) => CustomParams)): ZodType<Blob, ZodTypeDef, Blob>;
29
+
30
+ declare function file(params?: string | CustomParams | ((input: unknown) => CustomParams)): ZodType<File, ZodTypeDef, File> & {
31
+ type(mimeType: string, params?: string | CustomParams | ((input: unknown) => CustomParams)): ZodEffects<ZodType<File, ZodTypeDef, File>, File, File>;
32
+ };
33
+
34
+ declare function regexp(params?: string | CustomParams | ((input: unknown) => CustomParams)): ZodType<RegExp, ZodTypeDef, RegExp>;
35
+
36
+ declare function url(params?: string | CustomParams | ((input: unknown) => CustomParams)): ZodType<URL, ZodTypeDef, URL>;
37
+
38
+ declare class ZodSmartCoercionPlugin<TContext extends Context> implements HandlerPlugin<TContext> {
39
+ init(options: StandardHandlerOptions<TContext>): void;
40
+ }
41
+
42
+ interface ZodToJsonSchemaOptions {
43
+ /**
44
+ * Max depth of lazy type, if it exceeds.
45
+ *
46
+ * Used `{}` when reach max depth
47
+ *
48
+ * @default 3
49
+ */
50
+ maxLazyDepth?: number;
51
+ /**
52
+ * The schema to be used when the Zod schema is unsupported.
53
+ *
54
+ * @default { not: {} }
55
+ */
56
+ unsupportedJsonSchema?: Exclude<JSONSchema, boolean>;
57
+ /**
58
+ * The schema to be used to represent the any | unknown type.
59
+ *
60
+ * @default { }
61
+ */
62
+ anyJsonSchema?: Exclude<JSONSchema, boolean>;
63
+ }
64
+ declare class ZodToJsonSchemaConverter implements ConditionalSchemaConverter {
65
+ #private;
66
+ private readonly maxLazyDepth;
67
+ private readonly unsupportedJsonSchema;
68
+ private readonly anyJsonSchema;
69
+ constructor(options?: ZodToJsonSchemaOptions);
70
+ condition(schema: AnySchema | undefined): boolean;
71
+ convert(schema: AnySchema | undefined, options: SchemaConvertOptions, lazyDepth?: number, isHandledCustomJSONSchema?: boolean, isHandledZodDescription?: boolean): [required: boolean, jsonSchema: Exclude<JSONSchema, boolean>];
72
+ }
73
+
74
+ declare const oz: {
75
+ file: typeof file;
76
+ blob: typeof blob;
77
+ url: typeof url;
78
+ regexp: typeof regexp;
79
+ openapi: typeof customJsonSchema;
80
+ };
81
+
82
+ export { type CustomParams, type CustomZodDef, ZodSmartCoercionPlugin, ZodToJsonSchemaConverter, type ZodToJsonSchemaOptions, blob, composeParams, customJsonSchema, file, getCustomJsonSchema, getCustomZodDef, oz, regexp, setCustomZodDef, url };
@@ -0,0 +1,82 @@
1
+ import { JSONSchema, ConditionalSchemaConverter, SchemaConvertOptions } from '@orpc/openapi';
2
+ import { ZodTypeAny, input, output, ZodTypeDef, CustomErrorParams, ZodType, ZodEffects } from 'zod';
3
+ import { Context } from '@orpc/server';
4
+ import { HandlerPlugin } from '@orpc/server/plugins';
5
+ import { StandardHandlerOptions } from '@orpc/server/standard';
6
+ import { AnySchema } from '@orpc/contract';
7
+
8
+ declare function getCustomJsonSchema(def: ZodTypeDef, options: {
9
+ strategy: 'input' | 'output' | 'both';
10
+ }): Exclude<JSONSchema, boolean> | undefined;
11
+ declare function customJsonSchema<T extends ZodTypeAny, TStrategy extends 'input' | 'output' | 'both' = 'both'>(schema: T, custom: Exclude<JSONSchema<TStrategy extends 'input' ? input<T> : TStrategy extends 'output' ? output<T> : input<T> & output<T>>, boolean>, options?: {
12
+ strategy?: TStrategy;
13
+ }): T;
14
+
15
+ type CustomParams = CustomErrorParams & {
16
+ fatal?: boolean;
17
+ };
18
+ type CustomZodDef = {
19
+ type: 'blob' | 'regexp' | 'url';
20
+ } | {
21
+ type: 'file';
22
+ mimeType?: string;
23
+ };
24
+ declare function setCustomZodDef<T extends ZodTypeDef>(def: T, custom: CustomZodDef): void;
25
+ declare function getCustomZodDef(def: ZodTypeDef): CustomZodDef | undefined;
26
+ declare function composeParams<T = unknown>(defaultMessage: (input: T) => string, params: undefined | string | CustomParams | ((input: T) => CustomParams)): (input: T) => CustomParams;
27
+
28
+ declare function blob(params?: string | CustomParams | ((input: unknown) => CustomParams)): ZodType<Blob, ZodTypeDef, Blob>;
29
+
30
+ declare function file(params?: string | CustomParams | ((input: unknown) => CustomParams)): ZodType<File, ZodTypeDef, File> & {
31
+ type(mimeType: string, params?: string | CustomParams | ((input: unknown) => CustomParams)): ZodEffects<ZodType<File, ZodTypeDef, File>, File, File>;
32
+ };
33
+
34
+ declare function regexp(params?: string | CustomParams | ((input: unknown) => CustomParams)): ZodType<RegExp, ZodTypeDef, RegExp>;
35
+
36
+ declare function url(params?: string | CustomParams | ((input: unknown) => CustomParams)): ZodType<URL, ZodTypeDef, URL>;
37
+
38
+ declare class ZodSmartCoercionPlugin<TContext extends Context> implements HandlerPlugin<TContext> {
39
+ init(options: StandardHandlerOptions<TContext>): void;
40
+ }
41
+
42
+ interface ZodToJsonSchemaOptions {
43
+ /**
44
+ * Max depth of lazy type, if it exceeds.
45
+ *
46
+ * Used `{}` when reach max depth
47
+ *
48
+ * @default 3
49
+ */
50
+ maxLazyDepth?: number;
51
+ /**
52
+ * The schema to be used when the Zod schema is unsupported.
53
+ *
54
+ * @default { not: {} }
55
+ */
56
+ unsupportedJsonSchema?: Exclude<JSONSchema, boolean>;
57
+ /**
58
+ * The schema to be used to represent the any | unknown type.
59
+ *
60
+ * @default { }
61
+ */
62
+ anyJsonSchema?: Exclude<JSONSchema, boolean>;
63
+ }
64
+ declare class ZodToJsonSchemaConverter implements ConditionalSchemaConverter {
65
+ #private;
66
+ private readonly maxLazyDepth;
67
+ private readonly unsupportedJsonSchema;
68
+ private readonly anyJsonSchema;
69
+ constructor(options?: ZodToJsonSchemaOptions);
70
+ condition(schema: AnySchema | undefined): boolean;
71
+ convert(schema: AnySchema | undefined, options: SchemaConvertOptions, lazyDepth?: number, isHandledCustomJSONSchema?: boolean, isHandledZodDescription?: boolean): [required: boolean, jsonSchema: Exclude<JSONSchema, boolean>];
72
+ }
73
+
74
+ declare const oz: {
75
+ file: typeof file;
76
+ blob: typeof blob;
77
+ url: typeof url;
78
+ regexp: typeof regexp;
79
+ openapi: typeof customJsonSchema;
80
+ };
81
+
82
+ export { type CustomParams, type CustomZodDef, ZodSmartCoercionPlugin, ZodToJsonSchemaConverter, type ZodToJsonSchemaOptions, blob, composeParams, customJsonSchema, file, getCustomJsonSchema, getCustomZodDef, oz, regexp, setCustomZodDef, url };