@orpc/contract 0.0.0-next.da8ae32 → 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/dist/index.mjs ADDED
@@ -0,0 +1,240 @@
1
+ import { mapEventIterator, ORPCError } from '@orpc/client';
2
+ export { ORPCError } from '@orpc/client';
3
+ import { isAsyncIteratorObject } from '@orpc/shared';
4
+
5
+ class ValidationError extends Error {
6
+ issues;
7
+ constructor(options) {
8
+ super(options.message, options);
9
+ this.issues = options.issues;
10
+ }
11
+ }
12
+ function mergeErrorMap(errorMap1, errorMap2) {
13
+ return { ...errorMap1, ...errorMap2 };
14
+ }
15
+
16
+ function mergeMeta(meta1, meta2) {
17
+ return { ...meta1, ...meta2 };
18
+ }
19
+
20
+ class ContractProcedure {
21
+ "~orpc";
22
+ 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");
25
+ }
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.");
28
+ }
29
+ this["~orpc"] = def;
30
+ }
31
+ }
32
+ function isContractProcedure(item) {
33
+ if (item instanceof ContractProcedure) {
34
+ return true;
35
+ }
36
+ return (typeof item === "object" || typeof item === "function") && item !== null && "~orpc" in item && typeof item["~orpc"] === "object" && item["~orpc"] !== null && "inputSchema" in item["~orpc"] && "outputSchema" in item["~orpc"] && "errorMap" in item["~orpc"] && "route" in item["~orpc"] && "meta" in item["~orpc"];
37
+ }
38
+
39
+ function mergeRoute(a, b) {
40
+ return { ...a, ...b };
41
+ }
42
+ function prefixRoute(route, prefix) {
43
+ if (!route.path) {
44
+ return route;
45
+ }
46
+ return {
47
+ ...route,
48
+ path: `${prefix}${route.path}`
49
+ };
50
+ }
51
+ function unshiftTagRoute(route, tags) {
52
+ return {
53
+ ...route,
54
+ tags: [...tags, ...route.tags ?? []]
55
+ };
56
+ }
57
+ function mergePrefix(a, b) {
58
+ return a ? `${a}${b}` : b;
59
+ }
60
+ function mergeTags(a, b) {
61
+ return a ? [...a, ...b] : b;
62
+ }
63
+ function adaptRoute(route, options) {
64
+ let router = route;
65
+ if (options.prefix) {
66
+ router = prefixRoute(router, options.prefix);
67
+ }
68
+ if (options.tags) {
69
+ router = unshiftTagRoute(router, options.tags);
70
+ }
71
+ return router;
72
+ }
73
+
74
+ function adaptContractRouter(contract, options) {
75
+ if (isContractProcedure(contract)) {
76
+ const adapted2 = new ContractProcedure({
77
+ ...contract["~orpc"],
78
+ errorMap: mergeErrorMap(options.errorMap, contract["~orpc"].errorMap),
79
+ route: adaptRoute(contract["~orpc"].route, options)
80
+ });
81
+ return adapted2;
82
+ }
83
+ const adapted = {};
84
+ for (const key in contract) {
85
+ adapted[key] = adaptContractRouter(contract[key], options);
86
+ }
87
+ return adapted;
88
+ }
89
+
90
+ class ContractBuilder extends ContractProcedure {
91
+ constructor(def) {
92
+ super(def);
93
+ this["~orpc"].prefix = def.prefix;
94
+ this["~orpc"].tags = def.tags;
95
+ }
96
+ /**
97
+ * Reset initial meta
98
+ */
99
+ $meta(initialMeta) {
100
+ return new ContractBuilder({
101
+ ...this["~orpc"],
102
+ meta: initialMeta
103
+ });
104
+ }
105
+ /**
106
+ * Reset initial route
107
+ */
108
+ $route(initialRoute) {
109
+ return new ContractBuilder({
110
+ ...this["~orpc"],
111
+ route: initialRoute
112
+ });
113
+ }
114
+ errors(errors) {
115
+ return new ContractBuilder({
116
+ ...this["~orpc"],
117
+ errorMap: mergeErrorMap(this["~orpc"].errorMap, errors)
118
+ });
119
+ }
120
+ meta(meta) {
121
+ return new ContractBuilder({
122
+ ...this["~orpc"],
123
+ meta: mergeMeta(this["~orpc"].meta, meta)
124
+ });
125
+ }
126
+ route(route) {
127
+ return new ContractBuilder({
128
+ ...this["~orpc"],
129
+ route: mergeRoute(this["~orpc"].route, route)
130
+ });
131
+ }
132
+ input(schema) {
133
+ return new ContractBuilder({
134
+ ...this["~orpc"],
135
+ inputSchema: schema
136
+ });
137
+ }
138
+ output(schema) {
139
+ return new ContractBuilder({
140
+ ...this["~orpc"],
141
+ outputSchema: schema
142
+ });
143
+ }
144
+ prefix(prefix) {
145
+ return new ContractBuilder({
146
+ ...this["~orpc"],
147
+ prefix: mergePrefix(this["~orpc"].prefix, prefix)
148
+ });
149
+ }
150
+ tag(...tags) {
151
+ return new ContractBuilder({
152
+ ...this["~orpc"],
153
+ tags: mergeTags(this["~orpc"].tags, tags)
154
+ });
155
+ }
156
+ router(router) {
157
+ return adaptContractRouter(router, this["~orpc"]);
158
+ }
159
+ }
160
+ const oc = new ContractBuilder({
161
+ errorMap: {},
162
+ inputSchema: void 0,
163
+ outputSchema: void 0,
164
+ route: {},
165
+ meta: {}
166
+ });
167
+
168
+ const DEFAULT_CONFIG = {
169
+ defaultMethod: "POST",
170
+ defaultSuccessStatus: 200,
171
+ defaultSuccessDescription: "OK",
172
+ defaultInputStructure: "compact",
173
+ defaultOutputStructure: "compact"
174
+ };
175
+ function fallbackContractConfig(key, value) {
176
+ if (value === void 0) {
177
+ return DEFAULT_CONFIG[key];
178
+ }
179
+ return value;
180
+ }
181
+
182
+ const EVENT_ITERATOR_SCHEMA_SYMBOL = Symbol("ORPC_EVENT_ITERATOR_SCHEMA");
183
+ function eventIterator(yields, returns) {
184
+ return {
185
+ "~standard": {
186
+ [EVENT_ITERATOR_SCHEMA_SYMBOL]: { yields, returns },
187
+ vendor: "orpc",
188
+ version: 1,
189
+ validate(iterator) {
190
+ if (!isAsyncIteratorObject(iterator)) {
191
+ return { issues: [{ message: "Expect event iterator", path: [] }] };
192
+ }
193
+ const mapped = mapEventIterator(iterator, {
194
+ async value(value, done) {
195
+ const schema = done ? returns : yields;
196
+ if (!schema) {
197
+ return value;
198
+ }
199
+ const result = await schema["~standard"].validate(value);
200
+ if (result.issues) {
201
+ throw new ORPCError("EVENT_ITERATOR_VALIDATION_FAILED", {
202
+ message: "Event iterator validation failed",
203
+ cause: new ValidationError({
204
+ issues: result.issues,
205
+ message: "Event iterator validation failed"
206
+ })
207
+ });
208
+ }
209
+ return result.value;
210
+ },
211
+ error: async (error) => error
212
+ });
213
+ return { value: mapped };
214
+ }
215
+ }
216
+ };
217
+ }
218
+ function getEventIteratorSchemaDetails(schema) {
219
+ if (schema === void 0) {
220
+ return void 0;
221
+ }
222
+ return schema["~standard"][EVENT_ITERATOR_SCHEMA_SYMBOL];
223
+ }
224
+
225
+ function type(...[map]) {
226
+ return {
227
+ "~standard": {
228
+ vendor: "custom",
229
+ version: 1,
230
+ async validate(value) {
231
+ if (map) {
232
+ return { value: await map(value) };
233
+ }
234
+ return { value };
235
+ }
236
+ }
237
+ };
238
+ }
239
+
240
+ export { ContractBuilder, ContractProcedure, ValidationError, adaptContractRouter, adaptRoute, eventIterator, fallbackContractConfig, getEventIteratorSchemaDetails, isContractProcedure, mergeErrorMap, mergeMeta, mergePrefix, mergeRoute, mergeTags, 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.da8ae32",
4
+ "version": "0.0.0-next.df024bb",
5
5
  "license": "MIT",
6
6
  "homepage": "https://orpc.unnoq.com",
7
7
  "repository": {
@@ -15,22 +15,19 @@
15
15
  ],
16
16
  "exports": {
17
17
  ".": {
18
- "types": "./dist/src/index.d.ts",
19
- "import": "./dist/index.js",
20
- "default": "./dist/index.js"
21
- },
22
- "./🔒/*": {
23
- "types": "./dist/src/*.d.ts"
18
+ "types": "./dist/index.d.mts",
19
+ "import": "./dist/index.mjs",
20
+ "default": "./dist/index.mjs"
24
21
  }
25
22
  },
26
23
  "files": [
27
- "!**/*.map",
28
- "!**/*.tsbuildinfo",
29
24
  "dist"
30
25
  ],
31
26
  "dependencies": {
32
- "@standard-schema/spec": "1.0.0",
33
- "@orpc/shared": "0.0.0-next.da8ae32"
27
+ "@standard-schema/spec": "^1.0.0",
28
+ "@orpc/client": "0.0.0-next.df024bb",
29
+ "@orpc/standard-server": "0.0.0-next.df024bb",
30
+ "@orpc/shared": "0.0.0-next.df024bb"
34
31
  },
35
32
  "devDependencies": {
36
33
  "arktype": "2.0.0-rc.26",
@@ -38,7 +35,7 @@
38
35
  "zod": "^3.24.1"
39
36
  },
40
37
  "scripts": {
41
- "build": "tsup --clean --sourcemap --entry.index=src/index.ts --format=esm --onSuccess='tsc -b --noCheck'",
38
+ "build": "unbuild",
42
39
  "build:watch": "pnpm run build --watch",
43
40
  "type:check": "tsc -b"
44
41
  }
package/dist/index.js DELETED
@@ -1,409 +0,0 @@
1
- // src/error-map.ts
2
- function mergeErrorMap(errorMap1, errorMap2) {
3
- return { ...errorMap1, ...errorMap2 };
4
- }
5
-
6
- // src/meta.ts
7
- function mergeMeta(meta1, meta2) {
8
- return { ...meta1, ...meta2 };
9
- }
10
-
11
- // src/procedure.ts
12
- var ContractProcedure = class {
13
- "~orpc";
14
- constructor(def) {
15
- if (def.route?.successStatus && (def.route.successStatus < 200 || def.route?.successStatus > 299)) {
16
- throw new Error("[ContractProcedure] The successStatus must be between 200 and 299");
17
- }
18
- if (Object.values(def.errorMap).some((val) => val && val.status && (val.status < 400 || val.status > 599))) {
19
- throw new Error("[ContractProcedure] The error status code must be in the 400-599 range.");
20
- }
21
- this["~orpc"] = def;
22
- }
23
- };
24
- function isContractProcedure(item) {
25
- if (item instanceof ContractProcedure) {
26
- return true;
27
- }
28
- return (typeof item === "object" || typeof item === "function") && item !== null && "~orpc" in item && typeof item["~orpc"] === "object" && item["~orpc"] !== null && "inputSchema" in item["~orpc"] && "outputSchema" in item["~orpc"] && "errorMap" in item["~orpc"] && "route" in item["~orpc"] && "meta" in item["~orpc"];
29
- }
30
-
31
- // src/route.ts
32
- function mergeRoute(a, b) {
33
- return { ...a, ...b };
34
- }
35
- function prefixRoute(route, prefix) {
36
- if (!route.path) {
37
- return route;
38
- }
39
- return {
40
- ...route,
41
- path: `${prefix}${route.path}`
42
- };
43
- }
44
- function unshiftTagRoute(route, tags) {
45
- return {
46
- ...route,
47
- tags: [...tags, ...route.tags ?? []]
48
- };
49
- }
50
- function mergePrefix(a, b) {
51
- return a ? `${a}${b}` : b;
52
- }
53
- function mergeTags(a, b) {
54
- return a ? [...a, ...b] : b;
55
- }
56
- function adaptRoute(route, options) {
57
- let router = route;
58
- if (options.prefix) {
59
- router = prefixRoute(router, options.prefix);
60
- }
61
- if (options.tags) {
62
- router = unshiftTagRoute(router, options.tags);
63
- }
64
- return router;
65
- }
66
-
67
- // src/router.ts
68
- function adaptContractRouter(contract, options) {
69
- if (isContractProcedure(contract)) {
70
- const adapted2 = new ContractProcedure({
71
- ...contract["~orpc"],
72
- errorMap: mergeErrorMap(options.errorMap, contract["~orpc"].errorMap),
73
- route: adaptRoute(contract["~orpc"].route, options)
74
- });
75
- return adapted2;
76
- }
77
- const adapted = {};
78
- for (const key in contract) {
79
- adapted[key] = adaptContractRouter(contract[key], options);
80
- }
81
- return adapted;
82
- }
83
-
84
- // src/builder.ts
85
- var ContractBuilder = class _ContractBuilder extends ContractProcedure {
86
- constructor(def) {
87
- super(def);
88
- this["~orpc"].prefix = def.prefix;
89
- this["~orpc"].tags = def.tags;
90
- }
91
- /**
92
- * Reset initial meta
93
- */
94
- $meta(initialMeta) {
95
- return new _ContractBuilder({
96
- ...this["~orpc"],
97
- meta: initialMeta
98
- });
99
- }
100
- /**
101
- * Reset initial route
102
- */
103
- $route(initialRoute) {
104
- return new _ContractBuilder({
105
- ...this["~orpc"],
106
- route: initialRoute
107
- });
108
- }
109
- errors(errors) {
110
- return new _ContractBuilder({
111
- ...this["~orpc"],
112
- errorMap: mergeErrorMap(this["~orpc"].errorMap, errors)
113
- });
114
- }
115
- meta(meta) {
116
- return new _ContractBuilder({
117
- ...this["~orpc"],
118
- meta: mergeMeta(this["~orpc"].meta, meta)
119
- });
120
- }
121
- route(route) {
122
- return new _ContractBuilder({
123
- ...this["~orpc"],
124
- route: mergeRoute(this["~orpc"].route, route)
125
- });
126
- }
127
- input(schema) {
128
- return new _ContractBuilder({
129
- ...this["~orpc"],
130
- inputSchema: schema
131
- });
132
- }
133
- output(schema) {
134
- return new _ContractBuilder({
135
- ...this["~orpc"],
136
- outputSchema: schema
137
- });
138
- }
139
- prefix(prefix) {
140
- return new _ContractBuilder({
141
- ...this["~orpc"],
142
- prefix: mergePrefix(this["~orpc"].prefix, prefix)
143
- });
144
- }
145
- tag(...tags) {
146
- return new _ContractBuilder({
147
- ...this["~orpc"],
148
- tags: mergeTags(this["~orpc"].tags, tags)
149
- });
150
- }
151
- router(router) {
152
- return adaptContractRouter(router, this["~orpc"]);
153
- }
154
- };
155
- var oc = new ContractBuilder({
156
- errorMap: {},
157
- inputSchema: void 0,
158
- outputSchema: void 0,
159
- route: {},
160
- meta: {}
161
- });
162
-
163
- // src/error-orpc.ts
164
- import { isObject } from "@orpc/shared";
165
- var COMMON_ORPC_ERROR_DEFS = {
166
- BAD_REQUEST: {
167
- status: 400,
168
- message: "Bad Request"
169
- },
170
- UNAUTHORIZED: {
171
- status: 401,
172
- message: "Unauthorized"
173
- },
174
- FORBIDDEN: {
175
- status: 403,
176
- message: "Forbidden"
177
- },
178
- NOT_FOUND: {
179
- status: 404,
180
- message: "Not Found"
181
- },
182
- METHOD_NOT_SUPPORTED: {
183
- status: 405,
184
- message: "Method Not Supported"
185
- },
186
- NOT_ACCEPTABLE: {
187
- status: 406,
188
- message: "Not Acceptable"
189
- },
190
- TIMEOUT: {
191
- status: 408,
192
- message: "Request Timeout"
193
- },
194
- CONFLICT: {
195
- status: 409,
196
- message: "Conflict"
197
- },
198
- PRECONDITION_FAILED: {
199
- status: 412,
200
- message: "Precondition Failed"
201
- },
202
- PAYLOAD_TOO_LARGE: {
203
- status: 413,
204
- message: "Payload Too Large"
205
- },
206
- UNSUPPORTED_MEDIA_TYPE: {
207
- status: 415,
208
- message: "Unsupported Media Type"
209
- },
210
- UNPROCESSABLE_CONTENT: {
211
- status: 422,
212
- message: "Unprocessable Content"
213
- },
214
- TOO_MANY_REQUESTS: {
215
- status: 429,
216
- message: "Too Many Requests"
217
- },
218
- CLIENT_CLOSED_REQUEST: {
219
- status: 499,
220
- message: "Client Closed Request"
221
- },
222
- INTERNAL_SERVER_ERROR: {
223
- status: 500,
224
- message: "Internal Server Error"
225
- },
226
- NOT_IMPLEMENTED: {
227
- status: 501,
228
- message: "Not Implemented"
229
- },
230
- BAD_GATEWAY: {
231
- status: 502,
232
- message: "Bad Gateway"
233
- },
234
- SERVICE_UNAVAILABLE: {
235
- status: 503,
236
- message: "Service Unavailable"
237
- },
238
- GATEWAY_TIMEOUT: {
239
- status: 504,
240
- message: "Gateway Timeout"
241
- }
242
- };
243
- function fallbackORPCErrorStatus(code, status) {
244
- return status ?? COMMON_ORPC_ERROR_DEFS[code]?.status ?? 500;
245
- }
246
- function fallbackORPCErrorMessage(code, message) {
247
- return message || COMMON_ORPC_ERROR_DEFS[code]?.message || code;
248
- }
249
- var ORPCError = class _ORPCError extends Error {
250
- defined;
251
- code;
252
- status;
253
- data;
254
- constructor(code, ...[options]) {
255
- if (options?.status && (options.status < 400 || options.status >= 600)) {
256
- throw new Error("[ORPCError] The error status code must be in the 400-599 range.");
257
- }
258
- const message = fallbackORPCErrorMessage(code, options?.message);
259
- super(message, options);
260
- this.code = code;
261
- this.status = fallbackORPCErrorStatus(code, options?.status);
262
- this.defined = options?.defined ?? false;
263
- this.data = options?.data;
264
- }
265
- toJSON() {
266
- return {
267
- defined: this.defined,
268
- code: this.code,
269
- status: this.status,
270
- message: this.message,
271
- data: this.data
272
- };
273
- }
274
- static fromJSON(json) {
275
- return new _ORPCError(json.code, json);
276
- }
277
- static isValidJSON(json) {
278
- return isObject(json) && "defined" in json && typeof json.defined === "boolean" && "code" in json && typeof json.code === "string" && "status" in json && typeof json.status === "number" && "message" in json && typeof json.message === "string";
279
- }
280
- };
281
-
282
- // src/error-utils.ts
283
- function isDefinedError(error) {
284
- return error instanceof ORPCError && error.defined;
285
- }
286
- function createORPCErrorConstructorMap(errors) {
287
- const proxy = new Proxy(errors, {
288
- get(target, code) {
289
- if (typeof code !== "string") {
290
- return Reflect.get(target, code);
291
- }
292
- const item = (...[options]) => {
293
- const config = errors[code];
294
- return new ORPCError(code, {
295
- defined: Boolean(config),
296
- status: config?.status,
297
- message: options?.message ?? config?.message,
298
- data: options?.data,
299
- cause: options?.cause
300
- });
301
- };
302
- return item;
303
- }
304
- });
305
- return proxy;
306
- }
307
- async function validateORPCError(map, error) {
308
- const { code, status, message, data, cause, defined } = error;
309
- const config = map?.[error.code];
310
- if (!config || fallbackORPCErrorStatus(error.code, config.status) !== error.status) {
311
- return defined ? new ORPCError(code, { defined: false, status, message, data, cause }) : error;
312
- }
313
- if (!config.data) {
314
- return defined ? error : new ORPCError(code, { defined: true, status, message, data, cause });
315
- }
316
- const validated = await config.data["~standard"].validate(error.data);
317
- if (validated.issues) {
318
- return defined ? new ORPCError(code, { defined: false, status, message, data, cause }) : error;
319
- }
320
- return new ORPCError(code, { defined: true, status, message, data: validated.value, cause });
321
- }
322
- function toORPCError(error) {
323
- return error instanceof ORPCError ? error : new ORPCError("INTERNAL_SERVER_ERROR", {
324
- message: "Internal server error",
325
- cause: error
326
- });
327
- }
328
-
329
- // src/client-utils.ts
330
- async function safe(promise) {
331
- try {
332
- const output = await promise;
333
- return [output, void 0, false];
334
- } catch (e) {
335
- const error = e;
336
- if (isDefinedError(error)) {
337
- return [void 0, error, true];
338
- }
339
- return [void 0, error, false];
340
- }
341
- }
342
-
343
- // src/config.ts
344
- var DEFAULT_CONFIG = {
345
- defaultMethod: "POST",
346
- defaultSuccessStatus: 200,
347
- defaultSuccessDescription: "OK",
348
- defaultInputStructure: "compact",
349
- defaultOutputStructure: "compact"
350
- };
351
- function fallbackContractConfig(key, value) {
352
- if (value === void 0) {
353
- return DEFAULT_CONFIG[key];
354
- }
355
- return value;
356
- }
357
-
358
- // src/error.ts
359
- var ValidationError = class extends Error {
360
- issues;
361
- constructor(options) {
362
- super(options.message, options);
363
- this.issues = options.issues;
364
- }
365
- };
366
-
367
- // src/schema.ts
368
- function type(...[map]) {
369
- return {
370
- "~standard": {
371
- vendor: "custom",
372
- version: 1,
373
- async validate(value) {
374
- if (map) {
375
- return { value: await map(value) };
376
- }
377
- return { value };
378
- }
379
- }
380
- };
381
- }
382
- export {
383
- COMMON_ORPC_ERROR_DEFS,
384
- ContractBuilder,
385
- ContractProcedure,
386
- ORPCError,
387
- ValidationError,
388
- adaptContractRouter,
389
- adaptRoute,
390
- createORPCErrorConstructorMap,
391
- fallbackContractConfig,
392
- fallbackORPCErrorMessage,
393
- fallbackORPCErrorStatus,
394
- isContractProcedure,
395
- isDefinedError,
396
- mergeErrorMap,
397
- mergeMeta,
398
- mergePrefix,
399
- mergeRoute,
400
- mergeTags,
401
- oc,
402
- prefixRoute,
403
- safe,
404
- toORPCError,
405
- type,
406
- unshiftTagRoute,
407
- validateORPCError
408
- };
409
- //# sourceMappingURL=index.js.map