@orpc/openapi 2.0.0-beta.48 → 2.0.0-beta.49

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.
Files changed (127) hide show
  1. package/README.md +4 -4
  2. package/dist/adapters/aws-lambda/index.d.ts +15 -0
  3. package/dist/adapters/aws-lambda/index.js +1 -0
  4. package/dist/adapters/aws-lambda/openapi-handler.d.ts +18 -0
  5. package/dist/adapters/aws-lambda/openapi-handler.js +18 -0
  6. package/dist/adapters/fastify/index.d.ts +1 -0
  7. package/dist/adapters/fastify/index.js +1 -0
  8. package/dist/adapters/fastify/openapi-handler.d.ts +15 -0
  9. package/dist/adapters/fastify/openapi-handler.js +15 -0
  10. package/dist/adapters/fetch/index.d.ts +2 -0
  11. package/dist/adapters/fetch/index.js +2 -0
  12. package/dist/adapters/fetch/openapi-handler.d.ts +15 -0
  13. package/dist/adapters/fetch/openapi-handler.js +15 -0
  14. package/dist/adapters/fetch/openapi-link.d.ts +21 -0
  15. package/dist/adapters/fetch/openapi-link.js +20 -0
  16. package/dist/adapters/node/index.d.ts +1 -0
  17. package/dist/adapters/node/index.js +1 -0
  18. package/dist/adapters/node/openapi-handler.d.ts +15 -0
  19. package/dist/adapters/node/openapi-handler.js +15 -0
  20. package/dist/adapters/standard/index.d.ts +3 -0
  21. package/dist/adapters/standard/index.js +3 -0
  22. package/dist/adapters/standard/openapi-handler-codec.d.ts +62 -0
  23. package/dist/adapters/standard/openapi-handler-codec.js +220 -0
  24. package/dist/adapters/standard/openapi-link-codec.d.ts +62 -0
  25. package/dist/adapters/standard/openapi-link-codec.js +405 -0
  26. package/dist/adapters/standard/openapi-matcher.d.ts +29 -0
  27. package/dist/adapters/standard/openapi-matcher.js +108 -0
  28. package/dist/adapters/standard/utils.d.ts +4 -0
  29. package/dist/adapters/standard/utils.js +47 -0
  30. package/dist/bracket-notation.d.ts +26 -0
  31. package/dist/bracket-notation.js +185 -0
  32. package/dist/client-factory.d.ts +24 -0
  33. package/dist/client-factory.js +13 -0
  34. package/dist/constants.js +4 -0
  35. package/dist/extensions/route.d.ts +37 -0
  36. package/dist/extensions/route.js +12 -0
  37. package/dist/helpers/form-data.d.ts +57 -0
  38. package/dist/helpers/form-data.js +90 -0
  39. package/dist/helpers/index.d.ts +1 -0
  40. package/dist/helpers/index.js +1 -0
  41. package/dist/index.d.ts +13 -0
  42. package/dist/index.js +11 -0
  43. package/dist/meta.d.ts +325 -0
  44. package/dist/meta.js +84 -0
  45. package/dist/openapi-generator-components.d.ts +31 -0
  46. package/dist/openapi-generator-components.js +275 -0
  47. package/dist/openapi-generator-operation.d.ts +34 -0
  48. package/dist/openapi-generator-operation.js +469 -0
  49. package/dist/openapi-generator.d.ts +92 -0
  50. package/dist/openapi-generator.js +147 -0
  51. package/dist/openapi-json-serializer.d.ts +79 -0
  52. package/dist/openapi-json-serializer.js +260 -0
  53. package/dist/openapi-serializer.d.ts +43 -0
  54. package/dist/openapi-serializer.js +93 -0
  55. package/dist/plugins/index.d.ts +1 -0
  56. package/dist/plugins/index.js +1 -0
  57. package/dist/plugins/openapi-reference.d.ts +115 -0
  58. package/dist/plugins/openapi-reference.js +187 -0
  59. package/dist/router-utils.d.ts +20 -0
  60. package/dist/router-utils.js +36 -0
  61. package/dist/{types.d.mts → types.d.ts} +23 -19
  62. package/dist/utils.d.ts +14 -0
  63. package/dist/utils.js +47 -0
  64. package/package.json +69 -71
  65. package/dist/adapters/aws-lambda/index.d.mts +0 -15
  66. package/dist/adapters/aws-lambda/index.mjs +0 -1
  67. package/dist/adapters/aws-lambda/openapi-handler.d.mts +0 -17
  68. package/dist/adapters/aws-lambda/openapi-handler.mjs +0 -11
  69. package/dist/adapters/fastify/index.d.mts +0 -1
  70. package/dist/adapters/fastify/index.mjs +0 -1
  71. package/dist/adapters/fastify/openapi-handler.d.mts +0 -14
  72. package/dist/adapters/fastify/openapi-handler.mjs +0 -11
  73. package/dist/adapters/fetch/index.d.mts +0 -2
  74. package/dist/adapters/fetch/index.mjs +0 -2
  75. package/dist/adapters/fetch/openapi-handler.d.mts +0 -14
  76. package/dist/adapters/fetch/openapi-handler.mjs +0 -11
  77. package/dist/adapters/fetch/openapi-link.d.mts +0 -15
  78. package/dist/adapters/fetch/openapi-link.mjs +0 -11
  79. package/dist/adapters/node/index.d.mts +0 -1
  80. package/dist/adapters/node/index.mjs +0 -1
  81. package/dist/adapters/node/openapi-handler.d.mts +0 -14
  82. package/dist/adapters/node/openapi-handler.mjs +0 -11
  83. package/dist/adapters/standard/index.d.mts +0 -3
  84. package/dist/adapters/standard/index.mjs +0 -3
  85. package/dist/adapters/standard/openapi-handler-codec.d.mts +0 -61
  86. package/dist/adapters/standard/openapi-handler-codec.mjs +0 -203
  87. package/dist/adapters/standard/openapi-link-codec.d.mts +0 -53
  88. package/dist/adapters/standard/openapi-link-codec.mjs +0 -356
  89. package/dist/adapters/standard/openapi-matcher.d.mts +0 -29
  90. package/dist/adapters/standard/openapi-matcher.mjs +0 -105
  91. package/dist/adapters/standard/utils.d.mts +0 -4
  92. package/dist/adapters/standard/utils.mjs +0 -36
  93. package/dist/bracket-notation.d.mts +0 -26
  94. package/dist/bracket-notation.mjs +0 -156
  95. package/dist/client-factory.d.mts +0 -24
  96. package/dist/client-factory.mjs +0 -5
  97. package/dist/constants.mjs +0 -4
  98. package/dist/extensions/route.d.mts +0 -76
  99. package/dist/extensions/route.mjs +0 -12
  100. package/dist/helpers/form-data.d.mts +0 -57
  101. package/dist/helpers/form-data.mjs +0 -36
  102. package/dist/helpers/index.d.mts +0 -1
  103. package/dist/helpers/index.mjs +0 -1
  104. package/dist/index.d.mts +0 -13
  105. package/dist/index.mjs +0 -11
  106. package/dist/meta.d.mts +0 -345
  107. package/dist/meta.mjs +0 -56
  108. package/dist/openapi-generator-components.d.mts +0 -24
  109. package/dist/openapi-generator-components.mjs +0 -227
  110. package/dist/openapi-generator-operation.d.mts +0 -29
  111. package/dist/openapi-generator-operation.mjs +0 -447
  112. package/dist/openapi-generator.d.mts +0 -87
  113. package/dist/openapi-generator.mjs +0 -115
  114. package/dist/openapi-json-serializer.d.mts +0 -86
  115. package/dist/openapi-json-serializer.mjs +0 -229
  116. package/dist/openapi-serializer.d.mts +0 -43
  117. package/dist/openapi-serializer.mjs +0 -87
  118. package/dist/plugins/index.d.mts +0 -1
  119. package/dist/plugins/index.mjs +0 -1
  120. package/dist/plugins/openapi-reference.d.mts +0 -118
  121. package/dist/plugins/openapi-reference.mjs +0 -157
  122. package/dist/router-utils.d.mts +0 -18
  123. package/dist/router-utils.mjs +0 -29
  124. package/dist/utils.d.mts +0 -14
  125. package/dist/utils.mjs +0 -40
  126. /package/dist/{constants.d.mts → constants.d.ts} +0 -0
  127. /package/dist/{types.mjs → types.js} +0 -0
@@ -0,0 +1,185 @@
1
+ import { isPlainObject, NullProtoObj, setOwn } from '@orpc/shared';
2
+ export class BracketNotationSerializer {
3
+ maxDeserializingEmptySlots;
4
+ constructor(options = {}) {
5
+ this.maxDeserializingEmptySlots = options.maxDeserializingEmptySlots ?? 1_000;
6
+ }
7
+ serialize(data) {
8
+ const result = [];
9
+ this.internalSerialize(data, '', true, result);
10
+ return result;
11
+ }
12
+ internalSerialize(data, path, isRoot, result) {
13
+ if (Array.isArray(data)) {
14
+ data.forEach((item, i) => {
15
+ this.internalSerialize(item, isRoot ? i.toString() : `${path}[${i}]`, false, result);
16
+ });
17
+ }
18
+ else if (isPlainObject(data)) {
19
+ for (const key of Object.keys(data)) {
20
+ this.internalSerialize(data[key], isRoot ? key : `${path}[${key}]`, false, result);
21
+ }
22
+ }
23
+ else {
24
+ result.push([path, data]);
25
+ }
26
+ }
27
+ deserialize(serialized) {
28
+ // A caller-supplied object value can become a container for deeper paths, and unlike
29
+ // `NullProtoObj` it carries a real prototype, so accesses below stay own-property only.
30
+ let arrayPushStyles;
31
+ const root = new NullProtoObj();
32
+ let emptySlotsLeft = this.maxDeserializingEmptySlots;
33
+ for (const [path, value] of serialized) {
34
+ const segments = this.parsePath(path);
35
+ let currentRef = root;
36
+ // Arrays take keys as numbers, so engines do not parse the same key again on each access
37
+ let nextSegment = segments[0];
38
+ for (let i = 1; i < segments.length; i++) {
39
+ const segment = segments[i];
40
+ const isLast = i === segments.length - 1;
41
+ const hasExisting = Object.hasOwn(currentRef, nextSegment);
42
+ const existing = hasExisting ? currentRef[nextSegment] : undefined;
43
+ let child = existing;
44
+ let key = segment;
45
+ // A missing value is treated like an empty array and a leaf value like a one-item array (as if its key
46
+ // were repeated), which becomes an object if it cannot stay one
47
+ if (Array.isArray(child) || !isPlainObject(child)) {
48
+ if (hasExisting && !Array.isArray(child)) {
49
+ child = [child];
50
+ }
51
+ const isArray = Array.isArray(child);
52
+ const isPushStyle = isArray && !!arrayPushStyles?.has(child);
53
+ const length = isArray ? child.length : 0;
54
+ const index = internalToArrayIndex(segment);
55
+ const emptySlots = index === undefined ? 0 : Math.max(0, index - length);
56
+ const canBeArray = segment === ''
57
+ ? isLast && (isPushStyle || length === 0)
58
+ : index !== undefined && !(isLast && isPushStyle) && emptySlots <= emptySlotsLeft;
59
+ if (canBeArray) {
60
+ emptySlotsLeft -= emptySlots;
61
+ child = isArray ? child : [];
62
+ key = index ?? segment;
63
+ }
64
+ else if (isArray) {
65
+ arrayPushStyles?.delete(child);
66
+ child = isPushStyle
67
+ ? internalPushStyleArrayToObject(child)
68
+ : internalArrayToObject(child);
69
+ }
70
+ else {
71
+ child = new NullProtoObj();
72
+ }
73
+ }
74
+ if (child !== existing) {
75
+ internalSetOwn(currentRef, nextSegment, child);
76
+ }
77
+ currentRef = child;
78
+ nextSegment = key;
79
+ }
80
+ if (Array.isArray(currentRef) && nextSegment === '') {
81
+ arrayPushStyles ??= new WeakSet();
82
+ arrayPushStyles.add(currentRef);
83
+ currentRef.push(value);
84
+ }
85
+ else if (Object.hasOwn(currentRef, nextSegment)) {
86
+ const current = currentRef[nextSegment];
87
+ if (Array.isArray(current)) {
88
+ current.push(value);
89
+ }
90
+ else {
91
+ internalSetOwn(currentRef, nextSegment, [current, value]);
92
+ }
93
+ }
94
+ else {
95
+ internalSetOwn(currentRef, nextSegment, value);
96
+ }
97
+ }
98
+ return root;
99
+ }
100
+ stringifyPath(segments) {
101
+ if (segments.length === 0) {
102
+ return '';
103
+ }
104
+ let result = segments[0].toString();
105
+ for (let i = 1; i < segments.length; i++) {
106
+ result += `[${segments[i]}]`;
107
+ }
108
+ return result;
109
+ }
110
+ parsePath(path) {
111
+ const start = path.indexOf('[');
112
+ // Brackets must open after the first segment and close at the very end, e.g. `a[b][c]`
113
+ if (start === -1 || path[path.length - 1] !== ']') {
114
+ return [path];
115
+ }
116
+ const segments = [path.slice(0, start)];
117
+ let from = start + 1;
118
+ let to = path.indexOf('][', from);
119
+ while (to !== -1) {
120
+ segments.push(path.slice(from, to));
121
+ from = to + 2;
122
+ to = path.indexOf('][', from);
123
+ }
124
+ segments.push(path.slice(from, -1));
125
+ return segments;
126
+ }
127
+ }
128
+ /**
129
+ * The largest array index. Larger integer keys are ordinary properties.
130
+ */
131
+ const MAX_ARRAY_INDEX = 2 ** 32 - 2;
132
+ /**
133
+ * Returns `key` as an array index (no sign, no leading zeros), which is also how engines store the
134
+ * integer keys of objects, or `undefined` if it is not one.
135
+ */
136
+ function internalToArrayIndex(key) {
137
+ // `MAX_ARRAY_INDEX` has 10 digits
138
+ if (key.length === 0 || key.length > 10) {
139
+ return undefined;
140
+ }
141
+ let index = key.charCodeAt(0) - 48; // '0'
142
+ if (index < 0 || index > 9 || (index === 0 && key.length > 1)) {
143
+ return undefined;
144
+ }
145
+ for (let i = 1; i < key.length; i++) {
146
+ const digit = key.charCodeAt(i) - 48;
147
+ if (digit < 0 || digit > 9) {
148
+ return undefined;
149
+ }
150
+ index = index * 10 + digit;
151
+ }
152
+ return index <= MAX_ARRAY_INDEX ? index : undefined;
153
+ }
154
+ function internalArrayToObject(array) {
155
+ const obj = new NullProtoObj(); // Prevent Prototype Pollution with NullProtoObj
156
+ array.forEach((item, i) => {
157
+ obj[i] = item;
158
+ });
159
+ return obj;
160
+ }
161
+ function internalPushStyleArrayToObject(array) {
162
+ const obj = new NullProtoObj();
163
+ obj[''] = array.length === 1 ? array[0] : array;
164
+ return obj;
165
+ }
166
+ /**
167
+ * Like `setOwn`, but first switches an object created during deserialization to sparse storage when an
168
+ * integer key would leave a gap. Engines keep integer keys in a flat backing store sized by the largest one,
169
+ * so a lone `{ 999: x }` takes ~12KB in V8. Holding `MAX_ARRAY_INDEX` switches an object to sparse storage
170
+ * for good (V8 and JSC), so writing and deleting it keeps the object's integer keys cheap without a trace.
171
+ */
172
+ function internalSetOwn(container, key, value) {
173
+ if (typeof key === 'string' && container instanceof NullProtoObj) {
174
+ const index = internalToArrayIndex(key);
175
+ // `0`, or the key right after an existing one, keeps the flat store filled
176
+ if (index !== undefined &&
177
+ index !== 0 &&
178
+ !Object.hasOwn(container, index - 1) &&
179
+ !Object.hasOwn(container, MAX_ARRAY_INDEX)) {
180
+ container[MAX_ARRAY_INDEX] = undefined;
181
+ delete container[MAX_ARRAY_INDEX];
182
+ }
183
+ }
184
+ setOwn(container, key, value);
185
+ }
@@ -0,0 +1,24 @@
1
+ import type { ClientContext, ClientLink, ORPCClientOptions } from '@orpc/client';
2
+ import type { RouterContract, RouterContractClient } from '@orpc/contract';
3
+ import type { JsonifiedClient } from './types.ts';
4
+ export interface ContractJsonifiedClientFactory<TClientContext extends ClientContext> {
5
+ <T extends RouterContract>(contract: T): JsonifiedClient<RouterContractClient<T, TClientContext>>;
6
+ }
7
+ export interface ContractJsonifiedClientFactoryOptions<TClientContext extends ClientContext> extends Pick<ORPCClientOptions<JsonifiedClient<RouterContractClient<RouterContract, TClientContext>>>, 'interceptors' | 'scoped'> {
8
+ /**
9
+ * An optional reference to the root router-contract.
10
+ * When provided, the client factory will automatically register the passed contract
11
+ * into the router at the path defined by `meta.path`.
12
+ */
13
+ contractRef?: RouterContract | undefined;
14
+ }
15
+ /**
16
+ * Like `createContractClientFactory`, but the clients it builds are typed as `JsonifiedClient`,
17
+ * for links that require it, such as OpenAPI Link.
18
+ *
19
+ * @remarks
20
+ * **Warning**: Every procedure contract passed to the factory must define `meta.path` matching its location in the root contract.
21
+ *
22
+ * @see {@link https://orpc.dev/docs/contract/client-factory | Contract Client Factory}
23
+ */
24
+ export declare function createContractJsonifiedClientFactory<TClientContext extends ClientContext>(link: ClientLink<TClientContext>, options?: ContractJsonifiedClientFactoryOptions<TClientContext>): ContractJsonifiedClientFactory<TClientContext>;
@@ -0,0 +1,13 @@
1
+ import { createContractClientFactory } from '@orpc/contract';
2
+ /**
3
+ * Like `createContractClientFactory`, but the clients it builds are typed as `JsonifiedClient`,
4
+ * for links that require it, such as OpenAPI Link.
5
+ *
6
+ * @remarks
7
+ * **Warning**: Every procedure contract passed to the factory must define `meta.path` matching its location in the root contract.
8
+ *
9
+ * @see {@link https://orpc.dev/docs/contract/client-factory | Contract Client Factory}
10
+ */
11
+ export function createContractJsonifiedClientFactory(link, options = {}) {
12
+ return createContractClientFactory(link, options);
13
+ }
@@ -0,0 +1,4 @@
1
+ export const DEFAULT_OPENAPI_METHOD = 'POST';
2
+ export const DEFAULT_OPENAPI_SUCCESS_DESCRIPTION = 'OK';
3
+ export const DEFAULT_OPENAPI_INPUT_STRUCTURE = 'compact';
4
+ export const DEFAULT_OPENAPI_OUTPUT_STRUCTURE = 'compact';
@@ -0,0 +1,37 @@
1
+ import type { AnySchema, ErrorMap } from '@orpc/contract';
2
+ import type { Context } from '@orpc/server';
3
+ import type { OpenAPIMeta } from '../meta.ts';
4
+ declare module '@orpc/contract' {
5
+ interface ContractBuilder<TErrorMap extends ErrorMap> {
6
+ route(meta: OpenAPIMeta): ContractBuilder<TErrorMap>;
7
+ }
8
+ interface ProcedureContractBuilderWithInput<TInputSchema extends AnySchema, TErrorMap extends ErrorMap> {
9
+ route(meta: OpenAPIMeta): ProcedureContractBuilderWithInput<TInputSchema, TErrorMap>;
10
+ }
11
+ interface ProcedureContractBuilderWithOutput<TOutputSchema extends AnySchema, TErrorMap extends ErrorMap> {
12
+ route(meta: OpenAPIMeta): ProcedureContractBuilderWithOutput<TOutputSchema, TErrorMap>;
13
+ }
14
+ interface ProcedureContractBuilderWithInputOutput<TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap> {
15
+ route(meta: OpenAPIMeta): ProcedureContractBuilderWithInputOutput<TInputSchema, TOutputSchema, TErrorMap>;
16
+ }
17
+ }
18
+ declare module '@orpc/server' {
19
+ interface Builder<TInitialContext extends Context, TErrorMap extends ErrorMap> {
20
+ route(meta: OpenAPIMeta): Builder<TInitialContext, TErrorMap>;
21
+ }
22
+ interface BuilderWithMiddlewares<TInitialContext extends Context, TInjectedContext extends Context, TErrorMap extends ErrorMap> {
23
+ route(meta: OpenAPIMeta): BuilderWithMiddlewares<TInitialContext, TInjectedContext, TErrorMap>;
24
+ }
25
+ interface BuilderWithInput<TInitialContext extends Context, TInjectedContext extends Context, TInputSchema extends AnySchema, TErrorMap extends ErrorMap> {
26
+ route(meta: OpenAPIMeta): BuilderWithInput<TInitialContext, TInjectedContext, TInputSchema, TErrorMap>;
27
+ }
28
+ interface BuilderWithOutput<TInitialContext extends Context, TInjectedContext extends Context, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap> {
29
+ route(meta: OpenAPIMeta): BuilderWithOutput<TInitialContext, TInjectedContext, TOutputSchema, TErrorMap>;
30
+ }
31
+ interface BuilderWithInputOutput<TInitialContext extends Context, TInjectedContext extends Context, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap> {
32
+ route(meta: OpenAPIMeta): BuilderWithInputOutput<TInitialContext, TInjectedContext, TInputSchema, TOutputSchema, TErrorMap>;
33
+ }
34
+ interface DecoratedProcedure<TInitialContext extends Context, TInjectedContext extends Context, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap> {
35
+ route(meta: OpenAPIMeta): DecoratedProcedure<TInitialContext, TInjectedContext, TInputSchema, TOutputSchema, TErrorMap>;
36
+ }
37
+ }
@@ -0,0 +1,12 @@
1
+ import { ContractBuilder } from '@orpc/contract';
2
+ import { Builder, DecoratedProcedure } from '@orpc/server';
3
+ import { openapi } from '../meta.js';
4
+ ContractBuilder.prototype.route = function route(meta) {
5
+ return this.meta(openapi(meta));
6
+ };
7
+ Builder.prototype.route = function route(meta) {
8
+ return this.meta(openapi(meta));
9
+ };
10
+ DecoratedProcedure.prototype.route = function route(meta) {
11
+ return this.meta(openapi(meta));
12
+ };
@@ -0,0 +1,57 @@
1
+ /**
2
+ * Parse a form data with [bracket notation](https://orpc.dev/docs/openapi/bracket-notation) syntax
3
+ *
4
+ * @example
5
+ * ```ts
6
+ * const form = new FormData()
7
+ * form.append('a', '1')
8
+ * form.append('user[name]', 'John')
9
+ * form.append('user[age]', '20')
10
+ * form.append('user[friends][]', 'Bob')
11
+ * form.append('user[friends][]', 'Alice')
12
+ * form.append('user[friends][]', 'Charlie')
13
+ * form.append('thumb', new Blob(['hello']), 'thumb.png')
14
+ *
15
+ * parseFormData(form)
16
+ * // {
17
+ * // a: '1',
18
+ * // user: {
19
+ * // name: 'John',
20
+ * // age: '20',
21
+ * // friends: ['Bob', 'Alice', 'Charlie'],
22
+ * // },
23
+ * // thumb: form.get('thumb'),
24
+ * // }
25
+ * ```
26
+ *
27
+ * @see {@link https://orpc.dev/docs/helpers/form-data#parseformdata | Form Data Helpers - parseFormData}
28
+ */
29
+ export declare function parseFormData(form: FormData): any;
30
+ /**
31
+ * Gets the issue message from the error for a given field path.
32
+ *
33
+ * @remarks
34
+ * **Note**: Requires validation errors to follow the standard schema issue format, stored under `data.issues` — customized validation errors may not be found.
35
+ *
36
+ * @example
37
+ * ```tsx
38
+ * const { error, data, execute } = useServerAction(someAction)
39
+ *
40
+ * return <form action={(form) => execute(parseFormData(form))}>
41
+ * <input name="user[name]" type="text" />
42
+ * <p>{getIssueMessage(error, 'user[name]')}</p>
43
+ *
44
+ * <input name="user[age]" type="number" />
45
+ * <p>{getIssueMessage(error, 'user[age]')}</p>
46
+ *
47
+ * <input name="images[]" type="file" />
48
+ * <p>{getIssueMessage(error, 'images[]')}</p>
49
+ * </form>
50
+ * ```
51
+ *
52
+ * @param error - The error (can be anything) can contain `data.issues` (standard schema issues)
53
+ * @param path - The path of the field that has the issue follow [bracket notation](https://orpc.dev/docs/openapi/bracket-notation)
54
+ *
55
+ * @see {@link https://orpc.dev/docs/helpers/form-data#getissuemessage | Form Data Helpers - getIssueMessage}
56
+ */
57
+ export declare function getIssueMessage(error: unknown, path: string): string | undefined;
@@ -0,0 +1,90 @@
1
+ import { isSchemaIssue } from '@orpc/contract';
2
+ import { isNonPrimitive } from '@orpc/shared';
3
+ import { BracketNotationSerializer } from '../bracket-notation.js';
4
+ /**
5
+ * Parse a form data with [bracket notation](https://orpc.dev/docs/openapi/bracket-notation) syntax
6
+ *
7
+ * @example
8
+ * ```ts
9
+ * const form = new FormData()
10
+ * form.append('a', '1')
11
+ * form.append('user[name]', 'John')
12
+ * form.append('user[age]', '20')
13
+ * form.append('user[friends][]', 'Bob')
14
+ * form.append('user[friends][]', 'Alice')
15
+ * form.append('user[friends][]', 'Charlie')
16
+ * form.append('thumb', new Blob(['hello']), 'thumb.png')
17
+ *
18
+ * parseFormData(form)
19
+ * // {
20
+ * // a: '1',
21
+ * // user: {
22
+ * // name: 'John',
23
+ * // age: '20',
24
+ * // friends: ['Bob', 'Alice', 'Charlie'],
25
+ * // },
26
+ * // thumb: form.get('thumb'),
27
+ * // }
28
+ * ```
29
+ *
30
+ * @see {@link https://orpc.dev/docs/helpers/form-data#parseformdata | Form Data Helpers - parseFormData}
31
+ */
32
+ export function parseFormData(form) {
33
+ const serializer = new BracketNotationSerializer();
34
+ return serializer.deserialize(Array.from(form.entries()));
35
+ }
36
+ /**
37
+ * Gets the issue message from the error for a given field path.
38
+ *
39
+ * @remarks
40
+ * **Note**: Requires validation errors to follow the standard schema issue format, stored under `data.issues` — customized validation errors may not be found.
41
+ *
42
+ * @example
43
+ * ```tsx
44
+ * const { error, data, execute } = useServerAction(someAction)
45
+ *
46
+ * return <form action={(form) => execute(parseFormData(form))}>
47
+ * <input name="user[name]" type="text" />
48
+ * <p>{getIssueMessage(error, 'user[name]')}</p>
49
+ *
50
+ * <input name="user[age]" type="number" />
51
+ * <p>{getIssueMessage(error, 'user[age]')}</p>
52
+ *
53
+ * <input name="images[]" type="file" />
54
+ * <p>{getIssueMessage(error, 'images[]')}</p>
55
+ * </form>
56
+ * ```
57
+ *
58
+ * @param error - The error (can be anything) can contain `data.issues` (standard schema issues)
59
+ * @param path - The path of the field that has the issue follow [bracket notation](https://orpc.dev/docs/openapi/bracket-notation)
60
+ *
61
+ * @see {@link https://orpc.dev/docs/helpers/form-data#getissuemessage | Form Data Helpers - getIssueMessage}
62
+ */
63
+ export function getIssueMessage(error, path) {
64
+ if (!isNonPrimitive(error) || !isNonPrimitive(error.data) || !Array.isArray(error.data.issues)) {
65
+ return undefined;
66
+ }
67
+ const serializer = new BracketNotationSerializer();
68
+ for (const issue of error.data.issues) {
69
+ if (!isSchemaIssue(issue)) {
70
+ continue;
71
+ }
72
+ if (issue.path === undefined) {
73
+ if (path === '') {
74
+ return issue.message;
75
+ }
76
+ continue;
77
+ }
78
+ const issuePath = serializer.stringifyPath(issue.path.map((segment) => typeof segment === 'object' ? segment.key.toString() : segment.toString()));
79
+ if (issuePath === path) {
80
+ return issue.message;
81
+ }
82
+ if (path.endsWith('[]') && issuePath.replace(/\[(?:0|[1-9]\d*)\]$/, '[]') === path) {
83
+ return issue.message;
84
+ }
85
+ if (path === '' && /^(?:0|[1-9]\d*)$/.test(issuePath)) {
86
+ return issue.message;
87
+ }
88
+ }
89
+ return undefined;
90
+ }
@@ -0,0 +1 @@
1
+ export * from './form-data.ts';
@@ -0,0 +1 @@
1
+ export * from './form-data.js';
@@ -0,0 +1,13 @@
1
+ export * from './bracket-notation.ts';
2
+ export * from './client-factory.ts';
3
+ export { DEFAULT_OPENAPI_METHOD } from './constants.ts';
4
+ export * from './meta.ts';
5
+ export * from './openapi-generator.ts';
6
+ export * from './openapi-json-serializer.ts';
7
+ export * from './openapi-serializer.ts';
8
+ export * from './router-utils.ts';
9
+ export * from './types.ts';
10
+ export { getDynamicPathParams } from './utils.ts';
11
+ export { COMMON_ERROR_STATUS_MAP } from '@orpc/client';
12
+ export type { JsonSchema } from '@orpc/json-schema';
13
+ export type { StandardBodyHint } from '@standard-server/core';
package/dist/index.js ADDED
@@ -0,0 +1,11 @@
1
+ export * from './bracket-notation.js';
2
+ export * from './client-factory.js';
3
+ export { DEFAULT_OPENAPI_METHOD } from './constants.js';
4
+ export * from './meta.js';
5
+ export * from './openapi-generator.js';
6
+ export * from './openapi-json-serializer.js';
7
+ export * from './openapi-serializer.js';
8
+ export * from './router-utils.js';
9
+ export * from './types.js';
10
+ export { getDynamicPathParams } from './utils.js';
11
+ export { COMMON_ERROR_STATUS_MAP } from '@orpc/client';