hanzio 3.1.1 → 3.3.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/README.md +8 -1
- package/dist/cli/fields.d.ts +16 -6
- package/dist/cli/index.d.ts +57 -4
- package/dist/crypto/node.d.ts +17 -0
- package/dist/crypto/sealer.d.ts +34 -2
- package/dist/format/index.d.ts +6 -3
- package/dist/{index-nfvq3zax.js → index-818f7jz4.js} +228 -88
- package/dist/index-zz773050.js +135 -0
- package/dist/redis/concurrency-limit.d.ts +17 -7
- package/dist/src/cli/index.js +3 -1
- package/dist/src/crypto/index.js +15 -106
- package/dist/src/crypto/node.js +67 -0
- package/dist/src/format/index.js +4 -3
- package/dist/src/i18n/cli.js +33 -1
- package/dist/src/redis/index.js +5 -1
- package/dist/src/testing/index.js +23 -4
- package/dist/string/index.d.ts +1 -1
- package/dist/testing/module-mocker.d.ts +9 -9
- package/dist/testing/prisma.d.ts +5 -1
- package/dist/typedswitch/index.d.ts +26 -1
- package/package.json +8 -2
- package/src/cli/README.md +63 -6
- package/src/cli/fields.ts +95 -38
- package/src/cli/index.ts +302 -91
- package/src/crypto/README.md +21 -7
- package/src/crypto/node.ts +90 -0
- package/src/crypto/sealer.ts +120 -62
- package/src/format/README.md +7 -0
- package/src/format/index.ts +11 -5
- package/src/i18n/README.md +3 -1
- package/src/i18n/cli.ts +40 -0
- package/src/redis/README.md +9 -7
- package/src/redis/concurrency-limit.ts +26 -8
- package/src/string/index.ts +1 -1
- package/src/testing/README.md +12 -3
- package/src/testing/module-mocker.ts +40 -10
- package/src/testing/prisma.ts +6 -1
- package/src/typedswitch/README.md +20 -9
- package/src/typedswitch/index.ts +84 -8
|
@@ -246,21 +246,32 @@ const result = typedSwitch(status, {
|
|
|
246
246
|
|
|
247
247
|
### Enforcing a Return Type
|
|
248
248
|
|
|
249
|
-
Annotate the result
|
|
250
|
-
checking still applies
|
|
249
|
+
Annotate the result, or switch in a function with a declared return type.
|
|
250
|
+
Every handler is checked against it, exhaustiveness checking still applies,
|
|
251
|
+
and literals keep their types instead of widening to `string`:
|
|
251
252
|
|
|
252
253
|
```typescript
|
|
253
|
-
|
|
254
|
-
id: string
|
|
255
|
-
}
|
|
254
|
+
type Severity = 'success' | 'info' | 'warn' | 'danger'
|
|
256
255
|
|
|
257
|
-
const
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
256
|
+
const severity: Severity = typedSwitch(quality, {
|
|
257
|
+
excellent: () => 'success',
|
|
258
|
+
good: () => 'info',
|
|
259
|
+
fair: () => 'warn',
|
|
260
|
+
poor: () => 'danger',
|
|
261
261
|
})
|
|
262
|
+
|
|
263
|
+
function orderBy(sort: Sort): Prisma.OrderOrderByWithRelationInput[] {
|
|
264
|
+
return typedSwitch(sort, {
|
|
265
|
+
name: () => [{ name: 'asc' }],
|
|
266
|
+
newest: () => [{ createdAt: 'desc' }],
|
|
267
|
+
})
|
|
268
|
+
}
|
|
262
269
|
```
|
|
263
270
|
|
|
271
|
+
Without an annotation, or inside a generic call such as `useState(typedSwitch(...))`,
|
|
272
|
+
the result is the union of the handlers' widened return types. In Vue, annotate
|
|
273
|
+
the getter: `computed((): Severity => typedSwitch(...))`.
|
|
274
|
+
|
|
264
275
|
## Error Handling
|
|
265
276
|
|
|
266
277
|
When a case is unhandled at runtime (e.g., due to unsafe casts), `typedSwitch` throws a descriptive error:
|
package/src/typedswitch/index.ts
CHANGED
|
@@ -88,6 +88,23 @@ type RawCasesReturnType<Cases> = {
|
|
|
88
88
|
[K in keyof Cases]: RawHandlerReturnType<Cases[K]>
|
|
89
89
|
}[keyof Cases]
|
|
90
90
|
|
|
91
|
+
/**
|
|
92
|
+
* `R` is only inferred from the call's contextual type. Without one it stays
|
|
93
|
+
* `unknown`, and the overload returns the handlers' own return types.
|
|
94
|
+
*/
|
|
95
|
+
type Annotated<R, Inferred> = unknown extends R ? Inferred : R
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* TypeScript fixes a handler's return type the first time it checks it, and
|
|
99
|
+
* an object-mode call (`value, key, cases`) is first tried against the
|
|
100
|
+
* plain-value overload with a default (`value, cases, defaultCase`). This type
|
|
101
|
+
* types those cases' handlers by the annotation there too, so they keep their
|
|
102
|
+
* literals. No value satisfies it: `default` would have to be `never`.
|
|
103
|
+
*/
|
|
104
|
+
type ObjectCasesContext<R> = { [key: string]: (value: never) => R } & {
|
|
105
|
+
default: never
|
|
106
|
+
}
|
|
107
|
+
|
|
91
108
|
/** Only required, defined handlers guarantee that a case is handled. */
|
|
92
109
|
type HandledCaseKeys<Cases> = {
|
|
93
110
|
// biome-ignore lint/complexity/noBannedTypes: Empty-object assignability detects optional keys and index signatures.
|
|
@@ -173,6 +190,65 @@ type CasesRecord = Record<string, Handler | undefined>
|
|
|
173
190
|
// Function overloads
|
|
174
191
|
// ═══════════════════════════════════════════════════════════════════════════
|
|
175
192
|
|
|
193
|
+
// Each mode has two overloads. The first four take the result type `R` from
|
|
194
|
+
// the call's contextual type, such as an annotated variable or a declared
|
|
195
|
+
// return type, so handlers that return literals ('info', [{ name: 'asc' }])
|
|
196
|
+
// keep them. When that does not type-check, for example inside a generic call
|
|
197
|
+
// like useState(typedSwitch(...)), the last four union the handlers' types.
|
|
198
|
+
|
|
199
|
+
// Plain value input - all cases required, typed by the annotation
|
|
200
|
+
export function typedSwitch<
|
|
201
|
+
T extends Discriminant,
|
|
202
|
+
R,
|
|
203
|
+
Cases extends { [V in T as `${V}`]: (value: V) => R } & NoUnknownCases<
|
|
204
|
+
Cases,
|
|
205
|
+
`${T}`
|
|
206
|
+
>
|
|
207
|
+
>(value: T, cases: Cases): Annotated<R, RawCasesReturnType<Cases>>
|
|
208
|
+
|
|
209
|
+
// Plain value input - partial cases with default, typed by the annotation.
|
|
210
|
+
// It comes before the object overloads, which also take three arguments.
|
|
211
|
+
export function typedSwitch<
|
|
212
|
+
T extends Discriminant,
|
|
213
|
+
R,
|
|
214
|
+
Cases extends {
|
|
215
|
+
[V in T as `${V}`]?: (value: V) => R
|
|
216
|
+
} & NoUnknownCases<Cases, `${T}`>,
|
|
217
|
+
DefaultCase extends
|
|
218
|
+
| ((value: Exclude<T, ValuesForKeys<T, HandledCaseKeys<Cases>>>) => R)
|
|
219
|
+
| ObjectCasesContext<R>
|
|
220
|
+
>(
|
|
221
|
+
value: T,
|
|
222
|
+
cases: Cases,
|
|
223
|
+
defaultCase: DefaultCase
|
|
224
|
+
): Annotated<R, RawCasesReturnType<Cases> | RawHandlerReturnType<DefaultCase>>
|
|
225
|
+
|
|
226
|
+
// Object input with key - all cases required, typed by the annotation
|
|
227
|
+
export function typedSwitch<
|
|
228
|
+
T extends object,
|
|
229
|
+
K extends keyof T,
|
|
230
|
+
R,
|
|
231
|
+
Cases extends ObjectCases<T, K, R> &
|
|
232
|
+
NoUnknownCases<Cases, `${Extract<T[K], Discriminant>}`>
|
|
233
|
+
>(value: T, key: K, cases: Cases): Annotated<R, RawCasesReturnType<Cases>>
|
|
234
|
+
|
|
235
|
+
// Object input with key - partial cases with default, typed by the annotation
|
|
236
|
+
export function typedSwitch<
|
|
237
|
+
T extends object,
|
|
238
|
+
K extends keyof T,
|
|
239
|
+
R,
|
|
240
|
+
Cases extends PartialObjectCases<T, K, R> &
|
|
241
|
+
NoUnknownCases<Cases, `${Extract<T[K], Discriminant>}`>,
|
|
242
|
+
DefaultCase extends (
|
|
243
|
+
value: Exclude<T, Record<K, ValuesForKeys<T[K], HandledCaseKeys<Cases>>>>
|
|
244
|
+
) => R
|
|
245
|
+
>(
|
|
246
|
+
value: T,
|
|
247
|
+
key: K,
|
|
248
|
+
cases: Cases,
|
|
249
|
+
defaultCase: DefaultCase
|
|
250
|
+
): Annotated<R, RawCasesReturnType<Cases> | RawHandlerReturnType<DefaultCase>>
|
|
251
|
+
|
|
176
252
|
// Plain value input - all cases required (no default)
|
|
177
253
|
export function typedSwitch<
|
|
178
254
|
T extends Discriminant,
|
|
@@ -182,6 +258,14 @@ export function typedSwitch<
|
|
|
182
258
|
>
|
|
183
259
|
>(value: T, cases: Cases): RawCasesReturnType<Cases>
|
|
184
260
|
|
|
261
|
+
// Object input with key - all cases required (no default)
|
|
262
|
+
export function typedSwitch<
|
|
263
|
+
T extends object,
|
|
264
|
+
K extends keyof T,
|
|
265
|
+
Cases extends ObjectCases<T, K, unknown> &
|
|
266
|
+
NoUnknownCases<Cases, `${Extract<T[K], Discriminant>}`>
|
|
267
|
+
>(value: T, key: K, cases: Cases): RawCasesReturnType<Cases>
|
|
268
|
+
|
|
185
269
|
// Plain value input - partial cases allowed (with default)
|
|
186
270
|
// The default handler only receives the cases not covered by `cases`.
|
|
187
271
|
export function typedSwitch<
|
|
@@ -198,14 +282,6 @@ export function typedSwitch<
|
|
|
198
282
|
defaultCase: DefaultCase
|
|
199
283
|
): RawCasesReturnType<Cases> | RawHandlerReturnType<DefaultCase>
|
|
200
284
|
|
|
201
|
-
// Object input with key - all cases required (no default)
|
|
202
|
-
export function typedSwitch<
|
|
203
|
-
T extends object,
|
|
204
|
-
K extends keyof T,
|
|
205
|
-
Cases extends ObjectCases<T, K, unknown> &
|
|
206
|
-
NoUnknownCases<Cases, `${Extract<T[K], Discriminant>}`>
|
|
207
|
-
>(value: T, key: K, cases: Cases): RawCasesReturnType<Cases>
|
|
208
|
-
|
|
209
285
|
// Object input with key - partial cases allowed (with default)
|
|
210
286
|
// The default handler only receives the union members not covered by `cases`.
|
|
211
287
|
export function typedSwitch<
|