@doist/twist-sdk 0.1.0-alpha.4 → 0.1.0-alpha.5

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.
@@ -12,12 +12,28 @@ import type { BatchRequestDescriptor, BatchResponseArray } from './types/batch';
12
12
  * ```
13
13
  */
14
14
  export declare class BatchBuilder extends BaseClient {
15
+ private static readonly CHUNK_SIZE;
15
16
  /**
16
- * Executes an array of batch request descriptors in a single API call.
17
+ * Splits an array of requests into chunks of the specified size.
18
+ */
19
+ private chunkRequests;
20
+ /**
21
+ * Flattens chunked results back into a single array while preserving the original order.
22
+ */
23
+ private flattenChunkedResults;
24
+ /**
25
+ * Executes a single chunk of batch requests (up to CHUNK_SIZE).
26
+ * This is the core batch execution logic extracted from the original execute method.
27
+ */
28
+ private executeSingleBatch;
29
+ /**
30
+ * Executes multiple API requests with automatic chunking and parallel execution.
31
+ * Transparently handles the 10-request API limitation by splitting large batches
32
+ * into smaller chunks and executing them concurrently.
17
33
  *
18
34
  * @param requests - Array of batch request descriptors
19
- * @returns Array of BatchResponse objects with processed data
20
- * @throws {TwistRequestError} If the batch request fails
35
+ * @returns Array of BatchResponse objects with processed data in original order
36
+ * @throws {TwistRequestError} If any batch chunk fails completely
21
37
  */
22
38
  execute<T extends readonly BatchRequestDescriptor<unknown>[]>(requests: T): Promise<BatchResponseArray<T>>;
23
39
  }
@@ -50,6 +50,15 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
50
50
  if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
51
51
  }
52
52
  };
53
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
54
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
55
+ if (ar || !(i in from)) {
56
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
57
+ ar[i] = from[i];
58
+ }
59
+ }
60
+ return to.concat(ar || Array.prototype.slice.call(from));
61
+ };
53
62
  Object.defineProperty(exports, "__esModule", { value: true });
54
63
  exports.BatchBuilder = void 0;
55
64
  var base_client_1 = require("./clients/base-client");
@@ -73,13 +82,29 @@ var BatchBuilder = /** @class */ (function (_super) {
73
82
  return _super !== null && _super.apply(this, arguments) || this;
74
83
  }
75
84
  /**
76
- * Executes an array of batch request descriptors in a single API call.
77
- *
78
- * @param requests - Array of batch request descriptors
79
- * @returns Array of BatchResponse objects with processed data
80
- * @throws {TwistRequestError} If the batch request fails
85
+ * Splits an array of requests into chunks of the specified size.
81
86
  */
82
- BatchBuilder.prototype.execute = function (requests) {
87
+ BatchBuilder.prototype.chunkRequests = function (requests, chunkSize) {
88
+ if (requests.length === 0) {
89
+ return [];
90
+ }
91
+ var chunks = [];
92
+ for (var i = 0; i < requests.length; i += chunkSize) {
93
+ chunks.push(requests.slice(i, i + chunkSize));
94
+ }
95
+ return chunks;
96
+ };
97
+ /**
98
+ * Flattens chunked results back into a single array while preserving the original order.
99
+ */
100
+ BatchBuilder.prototype.flattenChunkedResults = function (chunkedResults) {
101
+ return chunkedResults.flat();
102
+ };
103
+ /**
104
+ * Executes a single chunk of batch requests (up to CHUNK_SIZE).
105
+ * This is the core batch execution logic extracted from the original execute method.
106
+ */
107
+ BatchBuilder.prototype.executeSingleBatch = function (requests) {
83
108
  return __awaiter(this, void 0, void 0, function () {
84
109
  var batchRequests, allGets, formData, response, errorText, batchApiResponses;
85
110
  var _this = this;
@@ -199,6 +224,54 @@ var BatchBuilder = /** @class */ (function (_super) {
199
224
  });
200
225
  });
201
226
  };
227
+ /**
228
+ * Executes multiple API requests with automatic chunking and parallel execution.
229
+ * Transparently handles the 10-request API limitation by splitting large batches
230
+ * into smaller chunks and executing them concurrently.
231
+ *
232
+ * @param requests - Array of batch request descriptors
233
+ * @returns Array of BatchResponse objects with processed data in original order
234
+ * @throws {TwistRequestError} If any batch chunk fails completely
235
+ */
236
+ BatchBuilder.prototype.execute = function (requests) {
237
+ return __awaiter(this, void 0, void 0, function () {
238
+ var chunks, chunkPromises, chunkedResults;
239
+ var _this = this;
240
+ return __generator(this, function (_a) {
241
+ switch (_a.label) {
242
+ case 0:
243
+ if (requests.length === 0) {
244
+ return [2 /*return*/, []];
245
+ }
246
+ // If requests fit within a single chunk, use the original single-batch execution
247
+ if (requests.length <= BatchBuilder.CHUNK_SIZE) {
248
+ return [2 /*return*/, this.executeSingleBatch(requests)];
249
+ }
250
+ chunks = this.chunkRequests(__spreadArray([], requests, true), BatchBuilder.CHUNK_SIZE);
251
+ chunkPromises = chunks.map(function (chunk) {
252
+ return _this.executeSingleBatch(chunk).catch(function (error) {
253
+ // Collect errors but don't fail fast - allow other chunks to complete
254
+ console.error('Batch chunk failed:', error);
255
+ // Return error responses for all requests in this chunk
256
+ return chunk.map(function () { return ({
257
+ code: 500,
258
+ headers: {},
259
+ data: null,
260
+ }); });
261
+ });
262
+ });
263
+ return [4 /*yield*/, Promise.all(chunkPromises)
264
+ // Flatten results back to original order
265
+ ];
266
+ case 1:
267
+ chunkedResults = _a.sent();
268
+ // Flatten results back to original order
269
+ return [2 /*return*/, this.flattenChunkedResults(chunkedResults)];
270
+ }
271
+ });
272
+ });
273
+ };
274
+ BatchBuilder.CHUNK_SIZE = 10;
202
275
  return BatchBuilder;
203
276
  }(base_client_1.BaseClient));
204
277
  exports.BatchBuilder = BatchBuilder;
@@ -1,12 +1,12 @@
1
1
  import type { ApiVersion } from '../types/api-version';
2
- export interface ClientConfig {
2
+ export type ClientConfig = {
3
3
  /** API token for authentication */
4
4
  apiToken: string;
5
5
  /** Optional custom base URL. If not provided, uses the default Twist API URL */
6
6
  baseUrl?: string;
7
7
  /** Optional API version. Defaults to 'v3' */
8
8
  version?: ApiVersion;
9
- }
9
+ };
10
10
  /**
11
11
  * Base client class that provides centralized URL management and configuration
12
12
  * for all Twist API clients. Fixes the trailing slash bug and eliminates code duplication.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@doist/twist-sdk",
3
- "version": "0.1.0-alpha.4",
3
+ "version": "0.1.0-alpha.5",
4
4
  "description": "A TypeScript wrapper for the Twist REST API.",
5
5
  "author": "Doist developers",
6
6
  "homepage": "https://doist.github.io/twist-sdk-typescript/",