@owox/internal-helpers 0.15.0-next-20251203174158 → 0.15.0-next-20251204184400

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.d.ts CHANGED
@@ -7,5 +7,7 @@ export * from './utils/fetchWithBackoff.js';
7
7
  export * from './utils/castError.js';
8
8
  export * from './utils/isReadonlyQuery.js';
9
9
  export * from './utils/runWithConcurrency.js';
10
+ export * from './utils/zodSchemaParser.js';
11
+ export * from './utils/extractJsonFromText.js';
10
12
  export * from './integrations/event-bus/index.js';
11
13
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,8BAA8B,CAAC;AAC7C,cAAc,kCAAkC,CAAC;AACjD,cAAc,6BAA6B,CAAC;AAC5C,cAAc,oBAAoB,CAAC;AACnC,cAAc,+BAA+B,CAAC;AAC9C,cAAc,6BAA6B,CAAC;AAC5C,cAAc,sBAAsB,CAAC;AACrC,cAAc,4BAA4B,CAAC;AAC3C,cAAc,+BAA+B,CAAC;AAC9C,cAAc,mCAAmC,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,8BAA8B,CAAC;AAC7C,cAAc,kCAAkC,CAAC;AACjD,cAAc,6BAA6B,CAAC;AAC5C,cAAc,oBAAoB,CAAC;AACnC,cAAc,+BAA+B,CAAC;AAC9C,cAAc,6BAA6B,CAAC;AAC5C,cAAc,sBAAsB,CAAC;AACrC,cAAc,4BAA4B,CAAC;AAC3C,cAAc,+BAA+B,CAAC;AAC9C,cAAc,4BAA4B,CAAC;AAC3C,cAAc,gCAAgC,CAAC;AAC/C,cAAc,mCAAmC,CAAC"}
package/dist/index.js CHANGED
@@ -7,4 +7,6 @@ export * from './utils/fetchWithBackoff.js';
7
7
  export * from './utils/castError.js';
8
8
  export * from './utils/isReadonlyQuery.js';
9
9
  export * from './utils/runWithConcurrency.js';
10
+ export * from './utils/zodSchemaParser.js';
11
+ export * from './utils/extractJsonFromText.js';
10
12
  export * from './integrations/event-bus/index.js';
@@ -0,0 +1,2 @@
1
+ export declare function extractJsonFromText(raw: string | undefined): string | null;
2
+ //# sourceMappingURL=extractJsonFromText.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"extractJsonFromText.d.ts","sourceRoot":"","sources":["../../src/utils/extractJsonFromText.ts"],"names":[],"mappings":"AAAA,wBAAgB,mBAAmB,CAAC,GAAG,EAAE,MAAM,GAAG,SAAS,GAAG,MAAM,GAAG,IAAI,CAkC1E"}
@@ -0,0 +1,32 @@
1
+ export function extractJsonFromText(raw) {
2
+ if (!raw) {
3
+ return null;
4
+ }
5
+ // Strip markdown fences ```json ... ``` / ``` ... ```
6
+ const text = raw
7
+ .replace(/```json/gi, '')
8
+ .replace(/```/g, '')
9
+ .trim();
10
+ // First try: whole text as JSON
11
+ try {
12
+ JSON.parse(text);
13
+ return text;
14
+ }
15
+ catch {
16
+ // ignore
17
+ }
18
+ // Second try: take first {...} block by braces
19
+ const firstBrace = text.indexOf('{');
20
+ const lastBrace = text.lastIndexOf('}');
21
+ if (firstBrace === -1 || lastBrace === -1 || lastBrace <= firstBrace) {
22
+ return null;
23
+ }
24
+ const candidate = text.slice(firstBrace, lastBrace + 1);
25
+ try {
26
+ JSON.parse(candidate);
27
+ return candidate;
28
+ }
29
+ catch {
30
+ return null;
31
+ }
32
+ }
@@ -1,2 +1,3 @@
1
- export declare function fetchWithBackoff(url: string, options: RequestInit, maxRetries?: number, initialDelay?: number): Promise<Response>;
1
+ export declare function fetchWithBackoff(url: string, options: RequestInit, maxRetries?: number, initialDelay?: number, timeoutMs?: number): Promise<Response>;
2
+ export declare function fetchWithTimeout(url: string, options?: RequestInit, timeoutMs?: number): Promise<Response>;
2
3
  //# sourceMappingURL=fetchWithBackoff.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"fetchWithBackoff.d.ts","sourceRoot":"","sources":["../../src/utils/fetchWithBackoff.ts"],"names":[],"mappings":"AAGA,wBAAsB,gBAAgB,CACpC,GAAG,EAAE,MAAM,EACX,OAAO,EAAE,WAAW,EACpB,UAAU,SAAI,EACd,YAAY,SAAM,GACjB,OAAO,CAAC,QAAQ,CAAC,CAwCnB"}
1
+ {"version":3,"file":"fetchWithBackoff.d.ts","sourceRoot":"","sources":["../../src/utils/fetchWithBackoff.ts"],"names":[],"mappings":"AAGA,wBAAsB,gBAAgB,CACpC,GAAG,EAAE,MAAM,EACX,OAAO,EAAE,WAAW,EACpB,UAAU,SAAI,EACd,YAAY,SAAM,EAClB,SAAS,SAAS,GACjB,OAAO,CAAC,QAAQ,CAAC,CAyCnB;AAED,wBAAsB,gBAAgB,CACpC,GAAG,EAAE,MAAM,EACX,OAAO,GAAE,WAAgB,EACzB,SAAS,SAAS,GACjB,OAAO,CAAC,QAAQ,CAAC,CAcnB"}
@@ -1,10 +1,10 @@
1
1
  import { LoggerFactory } from '../logging/logger-factory.js';
2
2
  import { castError } from './castError.js';
3
- export async function fetchWithBackoff(url, options, maxRetries = 3, initialDelay = 300) {
3
+ export async function fetchWithBackoff(url, options, maxRetries = 3, initialDelay = 300, timeoutMs = 25_000) {
4
4
  const logger = LoggerFactory.createNamedLogger('fetchWithBackoff');
5
5
  for (let attempt = 1; attempt <= maxRetries; attempt++) {
6
6
  try {
7
- const res = await fetch(url, options);
7
+ const res = await fetchWithTimeout(url, options, timeoutMs);
8
8
  // Retry on 5xx or 429
9
9
  if (res.status >= 500 || res.status === 429) {
10
10
  const retryAfter = parseInt(res.headers.get('retry-after') ?? '0', 10);
@@ -24,6 +24,7 @@ export async function fetchWithBackoff(url, options, maxRetries = 3, initialDela
24
24
  msg.includes('ECONNRESET') ||
25
25
  msg.includes('fetch failed') ||
26
26
  msg.includes('timed out') ||
27
+ msg.includes('Fetch timeout') ||
27
28
  msg.includes('socket hang up');
28
29
  if (!isTransient || attempt === maxRetries)
29
30
  throw err;
@@ -34,3 +35,19 @@ export async function fetchWithBackoff(url, options, maxRetries = 3, initialDela
34
35
  }
35
36
  throw new Error('fetchWithBackoff: exceeded max retries');
36
37
  }
38
+ export async function fetchWithTimeout(url, options = {}, timeoutMs = 25_000) {
39
+ const controller = new AbortController();
40
+ const timer = setTimeout(() => controller.abort(), timeoutMs);
41
+ try {
42
+ return await fetch(url, { ...options, signal: controller.signal });
43
+ }
44
+ catch (err) {
45
+ if (castError(err).name === 'AbortError') {
46
+ throw new Error(`Fetch timeout: request did not complete within ${timeoutMs}ms`);
47
+ }
48
+ throw err;
49
+ }
50
+ finally {
51
+ clearTimeout(timer);
52
+ }
53
+ }
@@ -0,0 +1,3 @@
1
+ import { z } from 'zod';
2
+ export declare function parseJsonWithSchema<TSchema extends z.ZodTypeAny>(schema: TSchema, raw: string | undefined, contextName: string): z.infer<TSchema>;
3
+ //# sourceMappingURL=zodSchemaParser.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"zodSchemaParser.d.ts","sourceRoot":"","sources":["../../src/utils/zodSchemaParser.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAGxB,wBAAgB,mBAAmB,CAAC,OAAO,SAAS,CAAC,CAAC,UAAU,EAC9D,MAAM,EAAE,OAAO,EACf,GAAG,EAAE,MAAM,GAAG,SAAS,EACvB,WAAW,EAAE,MAAM,GAClB,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAkBlB"}
@@ -0,0 +1,16 @@
1
+ import { castError } from './castError.js';
2
+ export function parseJsonWithSchema(schema, raw, contextName) {
3
+ let json;
4
+ try {
5
+ json = JSON.parse(raw ?? '');
6
+ }
7
+ catch (error) {
8
+ throw new Error(`${contextName}: Invalid JSON, message: ${castError(error).message} content: ${raw}`);
9
+ }
10
+ try {
11
+ return schema.parse(json);
12
+ }
13
+ catch (error) {
14
+ throw new Error(`${contextName}: Failed to parse JSON with schema: ${castError(error).message}, content: ${raw}`);
15
+ }
16
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@owox/internal-helpers",
3
- "version": "0.15.0-next-20251203174158",
3
+ "version": "0.15.0-next-20251204184400",
4
4
  "description": "Internal helpers used by core OWOX packages",
5
5
  "type": "module",
6
6
  "author": "OWOX",