@orpc/contract 0.0.0-next.bf323bf → 0.0.0-next.c0088c7

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.bf323bf",
4
+ "version": "0.0.0-next.c0088c7",
5
5
  "license": "MIT",
6
6
  "homepage": "https://orpc.unnoq.com",
7
7
  "repository": {
@@ -15,31 +15,27 @@
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
- "@orpc/server-standard": "^0.4.0",
33
27
  "@standard-schema/spec": "^1.0.0",
34
- "@orpc/shared": "0.0.0-next.bf323bf"
28
+ "@orpc/client": "0.0.0-next.c0088c7",
29
+ "@orpc/shared": "0.0.0-next.c0088c7",
30
+ "@orpc/standard-server": "0.0.0-next.c0088c7"
35
31
  },
36
32
  "devDependencies": {
37
33
  "arktype": "2.0.0-rc.26",
38
34
  "valibot": "1.0.0-beta.9",
39
- "zod": "^3.24.1"
35
+ "zod": "^3.24.2"
40
36
  },
41
37
  "scripts": {
42
- "build": "tsup --clean --sourcemap --entry.index=src/index.ts --format=esm --onSuccess='tsc -b --noCheck'",
38
+ "build": "unbuild",
43
39
  "build:watch": "pnpm run build --watch",
44
40
  "type:check": "tsc -b"
45
41
  }