@zenstackhq/server 3.4.0-beta.4 → 3.4.0

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/api.cjs CHANGED
@@ -2072,6 +2072,8 @@ var import_superjson4 = __toESM(require("superjson"), 1);
2072
2072
  var import_ts_pattern3 = require("ts-pattern");
2073
2073
  var import_zod3 = __toESM(require("zod"), 1);
2074
2074
  var import_v43 = require("zod-validation-error/v4");
2075
+ var TRANSACTION_ROUTE_PREFIX = "$transaction";
2076
+ var VALID_OPS = new Set(import_orm3.CoreCrudOperations);
2075
2077
  registerCustomSerializers();
2076
2078
  var RPCApiHandler = class {
2077
2079
  static {
@@ -2114,6 +2116,14 @@ var RPCApiHandler = class {
2114
2116
  requestBody
2115
2117
  });
2116
2118
  }
2119
+ if (model === TRANSACTION_ROUTE_PREFIX) {
2120
+ return this.handleTransaction({
2121
+ client,
2122
+ method: method.toUpperCase(),
2123
+ type: op,
2124
+ requestBody
2125
+ });
2126
+ }
2117
2127
  model = (0, import_common_helpers2.lowerCaseFirst)(model);
2118
2128
  method = method.toUpperCase();
2119
2129
  let args;
@@ -2212,6 +2222,77 @@ var RPCApiHandler = class {
2212
2222
  }
2213
2223
  }
2214
2224
  }
2225
+ async handleTransaction({ client, method, type, requestBody }) {
2226
+ if (method !== "POST") {
2227
+ return this.makeBadInputErrorResponse("invalid request method, only POST is supported");
2228
+ }
2229
+ if (type !== "sequential") {
2230
+ return this.makeBadInputErrorResponse(`unsupported transaction type: ${type}`);
2231
+ }
2232
+ if (!requestBody || !Array.isArray(requestBody) || requestBody.length === 0) {
2233
+ return this.makeBadInputErrorResponse("request body must be a non-empty array of operations");
2234
+ }
2235
+ const processedOps = [];
2236
+ for (let i = 0; i < requestBody.length; i++) {
2237
+ const item = requestBody[i];
2238
+ if (!item || typeof item !== "object") {
2239
+ return this.makeBadInputErrorResponse(`operation at index ${i} must be an object`);
2240
+ }
2241
+ const { model: itemModel, op: itemOp, args: itemArgs } = item;
2242
+ if (!itemModel || typeof itemModel !== "string") {
2243
+ return this.makeBadInputErrorResponse(`operation at index ${i} is missing a valid "model" field`);
2244
+ }
2245
+ if (!itemOp || typeof itemOp !== "string") {
2246
+ return this.makeBadInputErrorResponse(`operation at index ${i} is missing a valid "op" field`);
2247
+ }
2248
+ if (!VALID_OPS.has(itemOp)) {
2249
+ return this.makeBadInputErrorResponse(`operation at index ${i} has invalid op: ${itemOp}`);
2250
+ }
2251
+ if (!this.isValidModel(client, (0, import_common_helpers2.lowerCaseFirst)(itemModel))) {
2252
+ return this.makeBadInputErrorResponse(`operation at index ${i} has unknown model: ${itemModel}`);
2253
+ }
2254
+ if (itemArgs !== void 0 && itemArgs !== null && (typeof itemArgs !== "object" || Array.isArray(itemArgs))) {
2255
+ return this.makeBadInputErrorResponse(`operation at index ${i} has invalid "args" field`);
2256
+ }
2257
+ const { result: processedArgs, error: argsError } = await this.processRequestPayload(itemArgs ?? {});
2258
+ if (argsError) {
2259
+ return this.makeBadInputErrorResponse(`operation at index ${i}: ${argsError}`);
2260
+ }
2261
+ processedOps.push({
2262
+ model: (0, import_common_helpers2.lowerCaseFirst)(itemModel),
2263
+ op: itemOp,
2264
+ args: processedArgs
2265
+ });
2266
+ }
2267
+ try {
2268
+ const promises = processedOps.map(({ model, op, args }) => {
2269
+ return client[model][op](args);
2270
+ });
2271
+ log(this.options.log, "debug", () => `handling "$transaction" request with ${promises.length} operations`);
2272
+ const clientResult = await client.$transaction(promises);
2273
+ const { json, meta } = import_superjson4.default.serialize(clientResult);
2274
+ const responseBody = {
2275
+ data: json
2276
+ };
2277
+ if (meta) {
2278
+ responseBody.meta = {
2279
+ serialization: meta
2280
+ };
2281
+ }
2282
+ const response = {
2283
+ status: 200,
2284
+ body: responseBody
2285
+ };
2286
+ log(this.options.log, "debug", () => `sending response for "$transaction" request: ${(0, import_common_helpers2.safeJSONStringify)(response)}`);
2287
+ return response;
2288
+ } catch (err) {
2289
+ log(this.options.log, "error", `error occurred when handling "$transaction" request`, err);
2290
+ if (err instanceof import_orm3.ORMError) {
2291
+ return this.makeORMErrorResponse(err);
2292
+ }
2293
+ return this.makeGenericErrorResponse(err);
2294
+ }
2295
+ }
2215
2296
  async handleProcedureRequest({ client, method, proc, query, requestBody }) {
2216
2297
  if (!proc) {
2217
2298
  return this.makeBadInputErrorResponse("missing procedure name");