@urbicon-ui/sveltekit-utils 6.23.0 → 6.24.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/cron.d.ts CHANGED
@@ -34,10 +34,16 @@ export interface CronRunnerConfig {
34
34
  /** Jobs to schedule; each runs on its own independent interval. */
35
35
  jobs: CronJob[];
36
36
  /**
37
- * Called when a job's `fetch` **rejects** (network error, DNS failure, abort).
38
- * A non-2xx HTTP *response* does not reject `fetch`, so it does **not** reach
39
- * this hook the runner is fire-and-forget and never inspects the response.
40
- * Have the endpoint report its own failures if you need per-run status.
37
+ * Called when a job fails, with the failing {@link CronJob} and an `Error`.
38
+ * Fires in two cases:
39
+ * - the job's `fetch` **rejects** (network error, DNS failure, abort) the
40
+ * `Error` is whatever `fetch` threw.
41
+ * - the endpoint answers with a **non-2xx** status — the runner synthesises
42
+ * an `Error` naming the job and status, with the numeric code attached as
43
+ * `error.status` (e.g. `500`, `403`).
44
+ *
45
+ * Without a handler both failure modes are swallowed silently — pass one to
46
+ * observe per-run outcomes.
41
47
  */
42
48
  onError?: (job: CronJob, error: Error) => void;
43
49
  }
package/dist/cron.js CHANGED
@@ -39,14 +39,26 @@ export function createCronRunner(config) {
39
39
  running = true;
40
40
  for (const job of config.jobs) {
41
41
  const timer = setInterval(async () => {
42
+ const base = config.baseUrl ?? 'http://localhost:3000';
43
+ let response;
42
44
  try {
43
- const base = config.baseUrl ?? 'http://localhost:3000';
44
- await fetch(`${base}${job.path}`, {
45
+ response = await fetch(`${base}${job.path}`, {
45
46
  method: job.method ?? 'POST',
46
47
  headers: { [config.secretHeader ?? 'x-cron-secret']: config.secret }
47
48
  });
48
49
  }
49
50
  catch (err) {
51
+ // Network-level failure (DNS, connection refused, abort): fetch rejected.
52
+ config.onError?.(job, err);
53
+ return;
54
+ }
55
+ // The request completed; a non-2xx status is still a failure. Handle it
56
+ // outside the try so a throwing `onError` escapes as an unhandled
57
+ // rejection rather than being re-caught and re-invoked here — symmetric
58
+ // with the rejection path above.
59
+ if (!response.ok) {
60
+ const err = new Error(`Cron job "${job.path}" returned ${response.status} ${response.statusText}`);
61
+ err.status = response.status;
50
62
  config.onError?.(job, err);
51
63
  }
52
64
  }, job.intervalSeconds * 1000);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@urbicon-ui/sveltekit-utils",
3
- "version": "6.23.0",
3
+ "version": "6.24.0",
4
4
  "description": "SvelteKit helper utilities — createCronRunner and URL-state runes",
5
5
  "license": "MIT",
6
6
  "repository": {