bunderstack 0.9.1 → 0.10.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bunderstack",
3
- "version": "0.9.1",
3
+ "version": "0.10.0",
4
4
  "description": "Batteries-included backend framework for Bun: CRUD APIs, auth, file storage, realtime, tRPC, email, and validated env from a single Drizzle schema and config object.",
5
5
  "keywords": [
6
6
  "backend",
@@ -82,9 +82,10 @@ export type CronInvocation = { scheduledFor: Date }
82
82
  export type CronDefinition<
83
83
  TSchema extends Record<string, unknown> = Record<string, unknown>,
84
84
  TEnvResult = Record<string, unknown>,
85
+ TSchedule extends string = string,
85
86
  > = {
86
87
  kind: 'cron'
87
- schedule: string
88
+ schedule: TSchedule
88
89
  handler: (
89
90
  invocation: CronInvocation,
90
91
  ctx: JobContext<TSchema, TEnvResult>,
@@ -97,18 +98,22 @@ export type BackgroundDefinition =
97
98
  export type BackgroundDefs = Record<string, BackgroundDefinition>
98
99
 
99
100
  /** @deprecated Use QueueJobDefinition. */
100
- export type JobDefinition<TInput, TSchema extends Record<string, unknown> = Record<string, unknown>, TEnvResult = Record<string, unknown>> = QueueJobDefinition<
101
+ export type JobDefinition<
101
102
  TInput,
102
- TSchema,
103
- TEnvResult
104
- >
103
+ TSchema extends Record<string, unknown> = Record<string, unknown>,
104
+ TEnvResult = Record<string, unknown>,
105
+ > = QueueJobDefinition<TInput, TSchema, TEnvResult>
105
106
 
106
107
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
107
108
  export type AnyJobDefinition = QueueJobDefinition<any, any, any>
108
109
  export type JobsDefs = BackgroundDefs
109
110
 
110
111
  export type QueueJobKeys<TDefs extends BackgroundDefs> = {
111
- [K in keyof TDefs & string]: TDefs[K] extends QueueJobDefinition<any, any, any>
112
+ [K in keyof TDefs & string]: TDefs[K] extends QueueJobDefinition<
113
+ any,
114
+ any,
115
+ any
116
+ >
112
117
  ? K
113
118
  : never
114
119
  }[keyof TDefs & string]
@@ -123,11 +128,21 @@ export function validateBackgroundDefs(defs: BackgroundDefs): void {
123
128
  parseCron(def.schedule)
124
129
  continue
125
130
  }
126
- if (def.retries !== undefined && (def.retries < 0 || !Number.isInteger(def.retries))) {
127
- throw new Error(`[bunderstack] job "${name}": retries must be a non-negative integer`)
131
+ if (
132
+ def.retries !== undefined &&
133
+ (def.retries < 0 || !Number.isInteger(def.retries))
134
+ ) {
135
+ throw new Error(
136
+ `[bunderstack] job "${name}": retries must be a non-negative integer`,
137
+ )
128
138
  }
129
- if (def.concurrency !== undefined && (def.concurrency < 1 || !Number.isInteger(def.concurrency))) {
130
- throw new Error(`[bunderstack] job "${name}": concurrency must be a positive integer`)
139
+ if (
140
+ def.concurrency !== undefined &&
141
+ (def.concurrency < 1 || !Number.isInteger(def.concurrency))
142
+ ) {
143
+ throw new Error(
144
+ `[bunderstack] job "${name}": concurrency must be a positive integer`,
145
+ )
131
146
  }
132
147
  if (def.timeout !== undefined && def.timeout <= 0) {
133
148
  throw new Error(`[bunderstack] job "${name}": timeout must be positive`)
@@ -162,9 +177,9 @@ export function createJobsBuilder<
162
177
  ): QueueJobDefinition<TInput, TSchema, TEnvResult> {
163
178
  return { kind: 'job', ...def }
164
179
  },
165
- cron(
166
- def: Omit<CronDefinition<TSchema, TEnvResult>, 'kind'>,
167
- ): CronDefinition<TSchema, TEnvResult> {
180
+ cron<const TSchedule extends string>(
181
+ def: Omit<CronDefinition<TSchema, TEnvResult, TSchedule>, 'kind'>,
182
+ ): CronDefinition<TSchema, TEnvResult, TSchedule> {
168
183
  parseCron(def.schedule)
169
184
  return { kind: 'cron', ...def }
170
185
  },
@@ -187,9 +202,8 @@ export type BunderstackJobsBuilder<
187
202
  // `TDef extends { input: ZodType<infer I> }` fails structurally because
188
203
  // `input?: ZodType<TInput>` desugars to `ZodType<TInput> | undefined`, which
189
204
  // can never satisfy a required-property pattern.
190
- type JobInputOf<TDef> = TDef extends QueueJobDefinition<infer TInput, any, any>
191
- ? TInput
192
- : undefined
205
+ type JobInputOf<TDef> =
206
+ TDef extends QueueJobDefinition<infer TInput, any, any> ? TInput : undefined
193
207
 
194
208
  /**
195
209
  * `app.jobs`: `enqueue` narrowed to declared names + payloads. `Omit`s the
@@ -1,6 +1,6 @@
1
1
  import { cronMatches, parseCron } from './cron'
2
2
 
3
- type Timer = ReturnType<typeof setTimeout>
3
+ type Timer = ReturnType<typeof setTimeout> | number
4
4
 
5
5
  export type LocalCronDefinition = {
6
6
  name: string
@@ -45,7 +45,9 @@ export function startLocalCronScheduler(
45
45
  timer = setTimer(() => {
46
46
  timer = undefined
47
47
  void tick().catch((error: unknown) => {
48
- options.onError?.(error instanceof Error ? error : new Error(String(error)))
48
+ options.onError?.(
49
+ error instanceof Error ? error : new Error(String(error)),
50
+ )
49
51
  })
50
52
  }, delay)
51
53
  }