@orpc/zod 0.0.0-next.db1f26d → 0.0.0-next.df024bb

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,132 @@
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), 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/vue-colada](https://www.npmjs.com/package/@orpc/vue-colada): Integration with [Pinia Colada](https://pinia-colada.esm.dev/).
59
+ - [@orpc/openapi](https://www.npmjs.com/package/@orpc/openapi): Generate OpenAPI specs and handle OpenAPI requests.
60
+ - [@orpc/zod](https://www.npmjs.com/package/@orpc/zod): More schemas that [Zod](https://zod.dev/) doesn't support yet.
61
+
62
+ ## `@orpc/zod`
63
+
64
+ More schemas that [Zod](https://zod.dev/) doesn't support yet, and provides `ZodToJsonSchemaConverter` for generating OpenAPI specs.
65
+
66
+ ### More Schemas
67
+
68
+ - `oz.url`: Zod schema for [URL](https://developer.mozilla.org/en-US/docs/Web/API/URL) instance.
69
+ - `oz.blob`: Zod schema for [Blob](https://developer.mozilla.org/en-US/docs/Web/API/Blob) instance.
70
+ - `oz.file`: Zod schema for [File](https://developer.mozilla.org/en-US/docs/Web/API/File) instance.
71
+ - `oz.regexp`: Zod schema for [RegExp](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp) instance.
72
+
73
+ ```ts
74
+ import { oz } from '@orpc/zod'
75
+ import { z } from 'zod'
76
+
77
+ const Example = z.object({
78
+ url: oz.url(),
79
+ blob: oz.blob(),
80
+ file: oz.file().type('image/png'),
81
+ regexp: oz.regexp(),
82
+ })
83
+ ```
84
+
85
+ ### Generate OpenAPI Spec
86
+
87
+ ```ts
88
+ import { OpenAPIGenerator } from '@orpc/openapi'
89
+ import { ZodToJsonSchemaConverter } from '@orpc/zod'
90
+
91
+ const openAPIGenerator = new OpenAPIGenerator({
92
+ schemaConverters: [new ZodToJsonSchemaConverter()],
93
+ })
94
+
95
+ const specFromContract = await openAPIGenerator.generate(contract, {
96
+ info: {
97
+ title: 'My App',
98
+ version: '0.0.0',
99
+ },
100
+ })
101
+
102
+ const specFromRouter = await openAPIGenerator.generate(router, {
103
+ info: {
104
+ title: 'My App',
105
+ version: '0.0.0',
106
+ },
107
+ })
108
+ ```
109
+
110
+ ### Extending the Specification
111
+
112
+ ```ts
113
+ import { oz } from '@orpc/zod'
114
+ import { z } from 'zod'
115
+
116
+ const InputSchema = oz.openapi(
117
+ z.object({
118
+ name: z.string(),
119
+ }),
120
+ {
121
+ examples: [
122
+ { name: 'Earth' },
123
+ { name: 'Mars' },
124
+ ],
125
+ // additional options...
126
+ }
127
+ )
128
+ ```
129
+
130
+ ## License
131
+
132
+ Distributed under the MIT License. See [LICENSE](https://github.com/unnoq/orpc/blob/main/LICENSE) for more information.
@@ -0,0 +1,89 @@
1
+ import { Context } from '@orpc/server';
2
+ import { Plugin } from '@orpc/server/plugins';
3
+ import { StandardHandlerOptions } from '@orpc/server/standard';
4
+ import { Schema } from '@orpc/contract';
5
+ import { JSONSchema, SchemaConverter, SchemaConvertOptions } from '@orpc/openapi';
6
+ import { StandardSchemaV1 } from '@standard-schema/spec';
7
+ import { JSONSchema as JSONSchema$1 } from 'json-schema-typed/draft-2020-12';
8
+ import { ZodTypeDef, CustomErrorParams, ZodType, ZodEffects, ZodTypeAny, input, output } from 'zod';
9
+
10
+ declare class ZodSmartCoercionPlugin<TContext extends Context> implements Plugin<TContext> {
11
+ init(options: StandardHandlerOptions<TContext>): void;
12
+ }
13
+
14
+ declare const NON_LOGIC_KEYWORDS: ("default" | "$anchor" | "$comment" | "$defs" | "$dynamicAnchor" | "$dynamicRef" | "$id" | "$schema" | "$vocabulary" | "definitions" | "deprecated" | "description" | "examples" | "format" | "readOnly" | "title" | "writeOnly" | "contentEncoding" | "contentMediaType")[];
15
+ declare const UNSUPPORTED_JSON_SCHEMA: {
16
+ not: {};
17
+ };
18
+ declare const UNDEFINED_JSON_SCHEMA: {
19
+ const: string;
20
+ };
21
+ interface ZodToJsonSchemaOptions {
22
+ /**
23
+ * Max depth of lazy type, if it exceeds.
24
+ *
25
+ * Used `{}` when reach max depth
26
+ *
27
+ * @default 5
28
+ */
29
+ maxLazyDepth?: number;
30
+ /**
31
+ * The length used to track the depth of lazy type
32
+ *
33
+ * @internal
34
+ */
35
+ lazyDepth?: number;
36
+ /**
37
+ * The expected json schema for input or output zod schema
38
+ *
39
+ * @default input
40
+ */
41
+ mode?: 'input' | 'output';
42
+ /**
43
+ * Track if current level schema is handled custom json schema to prevent recursive
44
+ *
45
+ * @internal
46
+ */
47
+ isHandledCustomJSONSchema?: boolean;
48
+ /**
49
+ * Track if current level schema is handled zod description to prevent recursive
50
+ *
51
+ * @internal
52
+ */
53
+ isHandledZodDescription?: boolean;
54
+ }
55
+ declare function zodToJsonSchema(schema: StandardSchemaV1, options?: ZodToJsonSchemaOptions): Exclude<JSONSchema.JSONSchema, boolean>;
56
+ declare class ZodToJsonSchemaConverter implements SchemaConverter {
57
+ condition(schema: Schema): boolean;
58
+ convert(schema: Schema, options: SchemaConvertOptions): JSONSchema.JSONSchema;
59
+ }
60
+
61
+ type CustomZodType = 'File' | 'Blob' | 'Invalid Date' | 'RegExp' | 'URL';
62
+ type CustomParams = CustomErrorParams & {
63
+ fatal?: boolean;
64
+ };
65
+ declare function getCustomZodType(def: ZodTypeDef): CustomZodType | undefined;
66
+ declare function getCustomZodFileMimeType(def: ZodTypeDef): string | undefined;
67
+ declare function getCustomJSONSchema(def: ZodTypeDef, options?: {
68
+ mode?: 'input' | 'output';
69
+ }): Exclude<JSONSchema$1, boolean> | undefined;
70
+ declare function file(params?: string | CustomParams | ((input: unknown) => CustomParams)): ZodType<InstanceType<typeof File>, ZodTypeDef, InstanceType<typeof File>> & {
71
+ type(mimeType: string, params?: string | CustomParams | ((input: unknown) => CustomParams)): ZodEffects<ZodType<InstanceType<typeof File>, ZodTypeDef, InstanceType<typeof File>>, InstanceType<typeof File>, InstanceType<typeof File>>;
72
+ };
73
+ declare function blob(params?: string | CustomParams | ((input: unknown) => CustomParams)): ZodType<InstanceType<typeof Blob>, ZodTypeDef, InstanceType<typeof Blob>>;
74
+ declare function invalidDate(params?: string | CustomParams | ((input: unknown) => CustomParams)): ZodType<Date, ZodTypeDef, Date>;
75
+ declare function regexp(options?: CustomParams): ZodType<RegExp, ZodTypeDef, RegExp>;
76
+ declare function url(options?: CustomParams): ZodType<URL, ZodTypeDef, URL>;
77
+ declare function openapi<T extends ZodTypeAny, TMode extends 'input' | 'output' | 'both' = 'both'>(schema: T, custom: Exclude<JSONSchema$1<TMode extends 'input' ? input<T> : TMode extends 'output' ? output<T> : input<T> & output<T>>, boolean>, options?: {
78
+ mode: TMode;
79
+ }): ReturnType<T['refine']>;
80
+ declare const oz: {
81
+ openapi: typeof openapi;
82
+ file: typeof file;
83
+ blob: typeof blob;
84
+ invalidDate: typeof invalidDate;
85
+ regexp: typeof regexp;
86
+ url: typeof url;
87
+ };
88
+
89
+ export { type CustomZodType, NON_LOGIC_KEYWORDS, UNDEFINED_JSON_SCHEMA, UNSUPPORTED_JSON_SCHEMA, ZodSmartCoercionPlugin as ZodAutoCoercePlugin, ZodSmartCoercionPlugin, ZodToJsonSchemaConverter, type ZodToJsonSchemaOptions, blob, file, getCustomJSONSchema, getCustomZodFileMimeType, getCustomZodType, invalidDate, openapi, oz, regexp, url, zodToJsonSchema };
@@ -0,0 +1,89 @@
1
+ import { Context } from '@orpc/server';
2
+ import { Plugin } from '@orpc/server/plugins';
3
+ import { StandardHandlerOptions } from '@orpc/server/standard';
4
+ import { Schema } from '@orpc/contract';
5
+ import { JSONSchema, SchemaConverter, SchemaConvertOptions } from '@orpc/openapi';
6
+ import { StandardSchemaV1 } from '@standard-schema/spec';
7
+ import { JSONSchema as JSONSchema$1 } from 'json-schema-typed/draft-2020-12';
8
+ import { ZodTypeDef, CustomErrorParams, ZodType, ZodEffects, ZodTypeAny, input, output } from 'zod';
9
+
10
+ declare class ZodSmartCoercionPlugin<TContext extends Context> implements Plugin<TContext> {
11
+ init(options: StandardHandlerOptions<TContext>): void;
12
+ }
13
+
14
+ declare const NON_LOGIC_KEYWORDS: ("default" | "$anchor" | "$comment" | "$defs" | "$dynamicAnchor" | "$dynamicRef" | "$id" | "$schema" | "$vocabulary" | "definitions" | "deprecated" | "description" | "examples" | "format" | "readOnly" | "title" | "writeOnly" | "contentEncoding" | "contentMediaType")[];
15
+ declare const UNSUPPORTED_JSON_SCHEMA: {
16
+ not: {};
17
+ };
18
+ declare const UNDEFINED_JSON_SCHEMA: {
19
+ const: string;
20
+ };
21
+ interface ZodToJsonSchemaOptions {
22
+ /**
23
+ * Max depth of lazy type, if it exceeds.
24
+ *
25
+ * Used `{}` when reach max depth
26
+ *
27
+ * @default 5
28
+ */
29
+ maxLazyDepth?: number;
30
+ /**
31
+ * The length used to track the depth of lazy type
32
+ *
33
+ * @internal
34
+ */
35
+ lazyDepth?: number;
36
+ /**
37
+ * The expected json schema for input or output zod schema
38
+ *
39
+ * @default input
40
+ */
41
+ mode?: 'input' | 'output';
42
+ /**
43
+ * Track if current level schema is handled custom json schema to prevent recursive
44
+ *
45
+ * @internal
46
+ */
47
+ isHandledCustomJSONSchema?: boolean;
48
+ /**
49
+ * Track if current level schema is handled zod description to prevent recursive
50
+ *
51
+ * @internal
52
+ */
53
+ isHandledZodDescription?: boolean;
54
+ }
55
+ declare function zodToJsonSchema(schema: StandardSchemaV1, options?: ZodToJsonSchemaOptions): Exclude<JSONSchema.JSONSchema, boolean>;
56
+ declare class ZodToJsonSchemaConverter implements SchemaConverter {
57
+ condition(schema: Schema): boolean;
58
+ convert(schema: Schema, options: SchemaConvertOptions): JSONSchema.JSONSchema;
59
+ }
60
+
61
+ type CustomZodType = 'File' | 'Blob' | 'Invalid Date' | 'RegExp' | 'URL';
62
+ type CustomParams = CustomErrorParams & {
63
+ fatal?: boolean;
64
+ };
65
+ declare function getCustomZodType(def: ZodTypeDef): CustomZodType | undefined;
66
+ declare function getCustomZodFileMimeType(def: ZodTypeDef): string | undefined;
67
+ declare function getCustomJSONSchema(def: ZodTypeDef, options?: {
68
+ mode?: 'input' | 'output';
69
+ }): Exclude<JSONSchema$1, boolean> | undefined;
70
+ declare function file(params?: string | CustomParams | ((input: unknown) => CustomParams)): ZodType<InstanceType<typeof File>, ZodTypeDef, InstanceType<typeof File>> & {
71
+ type(mimeType: string, params?: string | CustomParams | ((input: unknown) => CustomParams)): ZodEffects<ZodType<InstanceType<typeof File>, ZodTypeDef, InstanceType<typeof File>>, InstanceType<typeof File>, InstanceType<typeof File>>;
72
+ };
73
+ declare function blob(params?: string | CustomParams | ((input: unknown) => CustomParams)): ZodType<InstanceType<typeof Blob>, ZodTypeDef, InstanceType<typeof Blob>>;
74
+ declare function invalidDate(params?: string | CustomParams | ((input: unknown) => CustomParams)): ZodType<Date, ZodTypeDef, Date>;
75
+ declare function regexp(options?: CustomParams): ZodType<RegExp, ZodTypeDef, RegExp>;
76
+ declare function url(options?: CustomParams): ZodType<URL, ZodTypeDef, URL>;
77
+ declare function openapi<T extends ZodTypeAny, TMode extends 'input' | 'output' | 'both' = 'both'>(schema: T, custom: Exclude<JSONSchema$1<TMode extends 'input' ? input<T> : TMode extends 'output' ? output<T> : input<T> & output<T>>, boolean>, options?: {
78
+ mode: TMode;
79
+ }): ReturnType<T['refine']>;
80
+ declare const oz: {
81
+ openapi: typeof openapi;
82
+ file: typeof file;
83
+ blob: typeof blob;
84
+ invalidDate: typeof invalidDate;
85
+ regexp: typeof regexp;
86
+ url: typeof url;
87
+ };
88
+
89
+ export { type CustomZodType, NON_LOGIC_KEYWORDS, UNDEFINED_JSON_SCHEMA, UNSUPPORTED_JSON_SCHEMA, ZodSmartCoercionPlugin as ZodAutoCoercePlugin, ZodSmartCoercionPlugin, ZodToJsonSchemaConverter, type ZodToJsonSchemaOptions, blob, file, getCustomJSONSchema, getCustomZodFileMimeType, getCustomZodType, invalidDate, openapi, oz, regexp, url, zodToJsonSchema };