@orpc/contract 0.0.0-next.f4d410a → 0.0.0-next.f50512c

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/dist/index.mjs CHANGED
@@ -1,6 +1,6 @@
1
- import { mapEventIterator, ORPCError } from '@orpc/client';
1
+ import { isORPCErrorStatus, mapEventIterator, ORPCError } from '@orpc/client';
2
2
  export { ORPCError } from '@orpc/client';
3
- import { isAsyncIteratorObject } from '@orpc/shared';
3
+ import { isAsyncIteratorObject, get, isTypescriptObject, isPropertyKey } from '@orpc/shared';
4
4
 
5
5
  class ValidationError extends Error {
6
6
  issues;
@@ -18,13 +18,16 @@ function mergeMeta(meta1, meta2) {
18
18
  }
19
19
 
20
20
  class ContractProcedure {
21
+ /**
22
+ * This property holds the defined options for the contract procedure.
23
+ */
21
24
  "~orpc";
22
25
  constructor(def) {
23
- if (def.route?.successStatus && (def.route.successStatus < 200 || def.route?.successStatus > 299)) {
24
- throw new Error("[ContractProcedure] The successStatus must be between 200 and 299");
26
+ if (def.route?.successStatus && isORPCErrorStatus(def.route.successStatus)) {
27
+ throw new Error("[ContractProcedure] Invalid successStatus.");
25
28
  }
26
- if (Object.values(def.errorMap).some((val) => val && val.status && (val.status < 400 || val.status > 599))) {
27
- throw new Error("[ContractProcedure] The error status code must be in the 400-599 range.");
29
+ if (Object.values(def.errorMap).some((val) => val && val.status && !isORPCErrorStatus(val.status))) {
30
+ throw new Error("[ContractProcedure] Invalid error status code.");
28
31
  }
29
32
  this["~orpc"] = def;
30
33
  }
@@ -100,6 +103,23 @@ function enhanceContractRouter(router, options) {
100
103
  }
101
104
  return enhanced;
102
105
  }
106
+ function minifyContractRouter(router) {
107
+ if (isContractProcedure(router)) {
108
+ const procedure = {
109
+ "~orpc": {
110
+ errorMap: {},
111
+ meta: router["~orpc"].meta,
112
+ route: router["~orpc"].route
113
+ }
114
+ };
115
+ return procedure;
116
+ }
117
+ const json = {};
118
+ for (const key in router) {
119
+ json[key] = minifyContractRouter(router[key]);
120
+ }
121
+ return json;
122
+ }
103
123
 
104
124
  class ContractBuilder extends ContractProcedure {
105
125
  constructor(def) {
@@ -108,7 +128,9 @@ class ContractBuilder extends ContractProcedure {
108
128
  this["~orpc"].tags = def.tags;
109
129
  }
110
130
  /**
111
- * Reset initial meta
131
+ * Sets or overrides the initial meta.
132
+ *
133
+ * @see {@link https://orpc.unnoq.com/docs/metadata Metadata Docs}
112
134
  */
113
135
  $meta(initialMeta) {
114
136
  return new ContractBuilder({
@@ -117,7 +139,11 @@ class ContractBuilder extends ContractProcedure {
117
139
  });
118
140
  }
119
141
  /**
120
- * Reset initial route
142
+ * Sets or overrides the initial route.
143
+ * This option is typically relevant when integrating with OpenAPI.
144
+ *
145
+ * @see {@link https://orpc.unnoq.com/docs/openapi/routing OpenAPI Routing Docs}
146
+ * @see {@link https://orpc.unnoq.com/docs/openapi/input-output-structure OpenAPI Input/Output Structure Docs}
121
147
  */
122
148
  $route(initialRoute) {
123
149
  return new ContractBuilder({
@@ -125,48 +151,97 @@ class ContractBuilder extends ContractProcedure {
125
151
  route: initialRoute
126
152
  });
127
153
  }
154
+ /**
155
+ * Adds type-safe custom errors to the contract.
156
+ * The provided errors are spared-merged with any existing errors in the contract.
157
+ *
158
+ * @see {@link https://orpc.unnoq.com/docs/error-handling#type%E2%80%90safe-error-handling Type-Safe Error Handling Docs}
159
+ */
128
160
  errors(errors) {
129
161
  return new ContractBuilder({
130
162
  ...this["~orpc"],
131
163
  errorMap: mergeErrorMap(this["~orpc"].errorMap, errors)
132
164
  });
133
165
  }
166
+ /**
167
+ * Sets or updates the metadata for the contract.
168
+ * The provided metadata is spared-merged with any existing metadata in the contract.
169
+ *
170
+ * @see {@link https://orpc.unnoq.com/docs/metadata Metadata Docs}
171
+ */
134
172
  meta(meta) {
135
173
  return new ContractBuilder({
136
174
  ...this["~orpc"],
137
175
  meta: mergeMeta(this["~orpc"].meta, meta)
138
176
  });
139
177
  }
178
+ /**
179
+ * Sets or updates the route definition for the contract.
180
+ * The provided route is spared-merged with any existing route in the contract.
181
+ * This option is typically relevant when integrating with OpenAPI.
182
+ *
183
+ * @see {@link https://orpc.unnoq.com/docs/openapi/routing OpenAPI Routing Docs}
184
+ * @see {@link https://orpc.unnoq.com/docs/openapi/input-output-structure OpenAPI Input/Output Structure Docs}
185
+ */
140
186
  route(route) {
141
187
  return new ContractBuilder({
142
188
  ...this["~orpc"],
143
189
  route: mergeRoute(this["~orpc"].route, route)
144
190
  });
145
191
  }
192
+ /**
193
+ * Defines the input validation schema for the contract.
194
+ *
195
+ * @see {@link https://orpc.unnoq.com/docs/procedure#input-output-validation Input Validation Docs}
196
+ */
146
197
  input(schema) {
147
198
  return new ContractBuilder({
148
199
  ...this["~orpc"],
149
200
  inputSchema: schema
150
201
  });
151
202
  }
203
+ /**
204
+ * Defines the output validation schema for the contract.
205
+ *
206
+ * @see {@link https://orpc.unnoq.com/docs/procedure#input-output-validation Output Validation Docs}
207
+ */
152
208
  output(schema) {
153
209
  return new ContractBuilder({
154
210
  ...this["~orpc"],
155
211
  outputSchema: schema
156
212
  });
157
213
  }
214
+ /**
215
+ * Prefixes all procedures in the contract router.
216
+ * The provided prefix is post-appended to any existing router prefix.
217
+ *
218
+ * @note This option does not affect procedures that do not define a path in their route definition.
219
+ *
220
+ * @see {@link https://orpc.unnoq.com/docs/openapi/routing#route-prefixes OpenAPI Route Prefixes Docs}
221
+ */
158
222
  prefix(prefix) {
159
223
  return new ContractBuilder({
160
224
  ...this["~orpc"],
161
225
  prefix: mergePrefix(this["~orpc"].prefix, prefix)
162
226
  });
163
227
  }
228
+ /**
229
+ * Adds tags to all procedures in the contract router.
230
+ * This helpful when you want to group procedures together in the OpenAPI specification.
231
+ *
232
+ * @see {@link https://orpc.unnoq.com/docs/openapi/openapi-specification#operation-metadata OpenAPI Operation Metadata Docs}
233
+ */
164
234
  tag(...tags) {
165
235
  return new ContractBuilder({
166
236
  ...this["~orpc"],
167
237
  tags: mergeTags(this["~orpc"].tags, tags)
168
238
  });
169
239
  }
240
+ /**
241
+ * Applies all of the previously defined options to the specified contract router.
242
+ *
243
+ * @see {@link https://orpc.unnoq.com/docs/router#extending-router Extending Router Docs}
244
+ */
170
245
  router(router) {
171
246
  return enhanceContractRouter(router, this["~orpc"]);
172
247
  }
@@ -234,6 +309,19 @@ function getEventIteratorSchemaDetails(schema) {
234
309
  return schema["~standard"][EVENT_ITERATOR_DETAILS_SYMBOL];
235
310
  }
236
311
 
312
+ function inferRPCMethodFromContractRouter(contract) {
313
+ return (_, path) => {
314
+ const procedure = get(contract, path);
315
+ if (!isContractProcedure(procedure)) {
316
+ throw new Error(
317
+ `[inferRPCMethodFromContractRouter] No valid procedure found at path "${path.join(".")}". This may happen when the contract router is not properly configured.`
318
+ );
319
+ }
320
+ const method = fallbackContractConfig("defaultMethod", procedure["~orpc"].route.method);
321
+ return method === "HEAD" ? "GET" : method;
322
+ };
323
+ }
324
+
237
325
  function type(...[map]) {
238
326
  return {
239
327
  "~standard": {
@@ -249,4 +337,19 @@ function type(...[map]) {
249
337
  };
250
338
  }
251
339
 
252
- export { ContractBuilder, ContractProcedure, ValidationError, enhanceContractRouter, enhanceRoute, eventIterator, fallbackContractConfig, getContractRouter, getEventIteratorSchemaDetails, isContractProcedure, mergeErrorMap, mergeMeta, mergePrefix, mergeRoute, mergeTags, oc, prefixRoute, type, unshiftTagRoute };
340
+ function isSchemaIssue(issue) {
341
+ if (!isTypescriptObject(issue) || typeof issue.message !== "string") {
342
+ return false;
343
+ }
344
+ if (issue.path !== void 0) {
345
+ if (!Array.isArray(issue.path)) {
346
+ return false;
347
+ }
348
+ if (!issue.path.every((segment) => isPropertyKey(segment) || isTypescriptObject(segment) && isPropertyKey(segment.key))) {
349
+ return false;
350
+ }
351
+ }
352
+ return true;
353
+ }
354
+
355
+ export { ContractBuilder, ContractProcedure, ValidationError, enhanceContractRouter, enhanceRoute, eventIterator, fallbackContractConfig, getContractRouter, getEventIteratorSchemaDetails, inferRPCMethodFromContractRouter, isContractProcedure, isSchemaIssue, mergeErrorMap, mergeMeta, mergePrefix, mergeRoute, mergeTags, minifyContractRouter, oc, prefixRoute, type, unshiftTagRoute };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@orpc/contract",
3
3
  "type": "module",
4
- "version": "0.0.0-next.f4d410a",
4
+ "version": "0.0.0-next.f50512c",
5
5
  "license": "MIT",
6
6
  "homepage": "https://orpc.unnoq.com",
7
7
  "repository": {
@@ -25,14 +25,14 @@
25
25
  ],
26
26
  "dependencies": {
27
27
  "@standard-schema/spec": "^1.0.0",
28
- "@orpc/shared": "0.0.0-next.f4d410a",
29
- "@orpc/standard-server": "0.0.0-next.f4d410a",
30
- "@orpc/client": "0.0.0-next.f4d410a"
28
+ "openapi-types": "^12.1.3",
29
+ "@orpc/client": "0.0.0-next.f50512c",
30
+ "@orpc/shared": "0.0.0-next.f50512c"
31
31
  },
32
32
  "devDependencies": {
33
- "arktype": "2.0.0-rc.26",
34
- "valibot": "1.0.0-beta.9",
35
- "zod": "^3.24.2"
33
+ "arktype": "2.1.20",
34
+ "valibot": "^1.1.0",
35
+ "zod": "^4.0.5"
36
36
  },
37
37
  "scripts": {
38
38
  "build": "unbuild",