@molecule/api-resource 1.0.6 → 1.0.7
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 +59 -27
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -3,7 +3,7 @@ AUTO-GENERATED — DO NOT EDIT THIS FILE.
|
|
|
3
3
|
Generated by `mlcl sync-docs` from the package's src/index.ts JSDoc + mlcl/registry.json.
|
|
4
4
|
Edits here are overwritten on the next commit (molecule's pre-commit hook regenerates).
|
|
5
5
|
To change this document, edit the module-level JSDoc in src/index.ts.
|
|
6
|
-
Generated: 2026-10-
|
|
6
|
+
Generated: 2026-10-26T04:23:42.740Z
|
|
7
7
|
-->
|
|
8
8
|
|
|
9
9
|
# @molecule/api-resource
|
|
@@ -307,11 +307,14 @@ export const create = ({ name, tableName, schema }: types.Resource) => {
|
|
|
307
307
|
```
|
|
308
308
|
|
|
309
309
|
```typescript
|
|
310
|
-
function create
|
|
310
|
+
function create<
|
|
311
|
+
CreateProps extends Record<string, unknown> = Record<string, unknown>,
|
|
312
|
+
CreatedProps extends types.Props & CreateProps = types.Props & CreateProps,
|
|
313
|
+
>({
|
|
311
314
|
name,
|
|
312
315
|
tableName,
|
|
313
316
|
schema,
|
|
314
|
-
}: types.Resource
|
|
317
|
+
}: types.Resource): ({
|
|
315
318
|
props: createProps,
|
|
316
319
|
id,
|
|
317
320
|
}: {
|
|
@@ -372,7 +375,13 @@ export const del = ({ name, tableName, schema }: types.Resource) => {
|
|
|
372
375
|
```
|
|
373
376
|
|
|
374
377
|
```typescript
|
|
375
|
-
function del
|
|
378
|
+
function del<
|
|
379
|
+
Props extends types.Props = types.Props,
|
|
380
|
+
DeletedResource extends types.Resource<Props> = types.Resource<Props>,
|
|
381
|
+
>({
|
|
382
|
+
name,
|
|
383
|
+
tableName,
|
|
384
|
+
}: DeletedResource): ({ id }: { id: Props['id'] }) => Promise<
|
|
376
385
|
| { statusCode: number; body: { error: string; errorKey: string; props?: undefined } }
|
|
377
386
|
| {
|
|
378
387
|
statusCode: number
|
|
@@ -394,7 +403,10 @@ Creates a request handler to be used to query resources.
|
|
|
394
403
|
Uses the bonded DataStore from `@molecule/api-database` for database-agnostic operations.
|
|
395
404
|
|
|
396
405
|
```typescript
|
|
397
|
-
function query
|
|
406
|
+
function query<
|
|
407
|
+
Props extends types.Props = types.Props,
|
|
408
|
+
QueriedResource extends types.Resource = types.Resource,
|
|
409
|
+
>({
|
|
398
410
|
tableName,
|
|
399
411
|
}: QueriedResource): (
|
|
400
412
|
req: MoleculeRequest,
|
|
@@ -432,7 +444,10 @@ export const read = ({ name, tableName, schema }: types.Resource) => {
|
|
|
432
444
|
```
|
|
433
445
|
|
|
434
446
|
```typescript
|
|
435
|
-
function read
|
|
447
|
+
function read<
|
|
448
|
+
ReadProps extends types.Props = types.Props,
|
|
449
|
+
ReadResource extends types.Resource<ReadProps> = types.Resource<ReadProps>,
|
|
450
|
+
>({
|
|
436
451
|
tableName,
|
|
437
452
|
}: ReadResource): ({
|
|
438
453
|
id,
|
|
@@ -504,11 +519,18 @@ export const update = ({ name, tableName, schema }: types.Resource) => {
|
|
|
504
519
|
```
|
|
505
520
|
|
|
506
521
|
```typescript
|
|
507
|
-
function update
|
|
522
|
+
function update<
|
|
523
|
+
UpdateProps extends Record<string, unknown> = Record<string, unknown>,
|
|
524
|
+
UpdatedProps extends Pick<types.Props, `updatedAt`> & UpdateProps = Pick<
|
|
525
|
+
types.Props,
|
|
526
|
+
`updatedAt`
|
|
527
|
+
> &
|
|
528
|
+
UpdateProps,
|
|
529
|
+
>({
|
|
508
530
|
name,
|
|
509
531
|
tableName,
|
|
510
532
|
schema,
|
|
511
|
-
}: types.Resource
|
|
533
|
+
}: types.Resource): ({
|
|
512
534
|
id,
|
|
513
535
|
props: updateProps,
|
|
514
536
|
}: {
|
|
@@ -597,9 +619,7 @@ Creates a Zod schema for the standard `{ statusCode, body }` response
|
|
|
597
619
|
returned by resource CRUD operations.
|
|
598
620
|
|
|
599
621
|
```typescript
|
|
600
|
-
function responseSchema(
|
|
601
|
-
propsSchema: T,
|
|
602
|
-
): z.ZodTypeAny<unknown, unknown, z.core.$ZodTypeInternals<unknown, unknown>>
|
|
622
|
+
function responseSchema<T extends z.ZodTypeAny>(propsSchema: T): z.ZodTypeAny
|
|
603
623
|
```
|
|
604
624
|
|
|
605
625
|
- `propsSchema` — The Zod schema for the resource's props (e.g. `userPropsSchema`).
|
|
@@ -612,9 +632,7 @@ Creates a Zod schema that validates a resource descriptor object
|
|
|
612
632
|
(`{ name, tableName, schema }`).
|
|
613
633
|
|
|
614
634
|
```typescript
|
|
615
|
-
function resourceSchema(
|
|
616
|
-
_propsSchema: T,
|
|
617
|
-
): z.ZodTypeAny<unknown, unknown, z.core.$ZodTypeInternals<unknown, unknown>>
|
|
635
|
+
function resourceSchema<T extends z.ZodTypeAny>(_propsSchema: T): z.ZodTypeAny
|
|
618
636
|
```
|
|
619
637
|
|
|
620
638
|
- `_propsSchema` — The Zod schema for the resource's props (used for type inference only).
|
|
@@ -627,10 +645,7 @@ Validates data against a Zod schema, throwing an `Error` with a
|
|
|
627
645
|
human-readable message listing all validation failures if invalid.
|
|
628
646
|
|
|
629
647
|
```typescript
|
|
630
|
-
function validate(
|
|
631
|
-
schema: z.ZodSchema<T, unknown, z.core.$ZodTypeInternals<T, unknown>>,
|
|
632
|
-
data: unknown,
|
|
633
|
-
): T
|
|
648
|
+
function validate<T>(schema: z.ZodSchema<T>, data: unknown): T
|
|
634
649
|
```
|
|
635
650
|
|
|
636
651
|
- `schema` — The Zod schema to validate against.
|
|
@@ -644,8 +659,8 @@ Validates data against a Zod schema without throwing. Returns a discriminated
|
|
|
644
659
|
union: `{ success: true, data }` on success or `{ success: false, error }` on failure.
|
|
645
660
|
|
|
646
661
|
```typescript
|
|
647
|
-
function safeParse(
|
|
648
|
-
schema: z.ZodSchema<T
|
|
662
|
+
function safeParse<T>(
|
|
663
|
+
schema: z.ZodSchema<T>,
|
|
649
664
|
data: unknown,
|
|
650
665
|
): { success: true; data: T } | { success: false; error: z.ZodError }
|
|
651
666
|
```
|
|
@@ -661,9 +676,9 @@ Creates a schema where all fields are optional, suitable for validating
|
|
|
661
676
|
partial update payloads where only changed fields are sent.
|
|
662
677
|
|
|
663
678
|
```typescript
|
|
664
|
-
function partial(
|
|
665
|
-
schema: z.ZodObject<T
|
|
666
|
-
): z.ZodObject<{ [K in keyof T]: z.ZodOptional<T[K]> }
|
|
679
|
+
function partial<T extends z.ZodRawShape>(
|
|
680
|
+
schema: z.ZodObject<T>,
|
|
681
|
+
): z.ZodObject<{ [K in keyof T]: z.ZodOptional<T[K]> }>
|
|
667
682
|
```
|
|
668
683
|
|
|
669
684
|
- `schema` — The Zod object schema to make partial.
|
|
@@ -676,10 +691,10 @@ Creates a schema with only the specified fields, useful for create payloads
|
|
|
676
691
|
where only a subset of the full props should be provided by the client.
|
|
677
692
|
|
|
678
693
|
```typescript
|
|
679
|
-
function pick(
|
|
680
|
-
schema: z.ZodObject<T
|
|
694
|
+
function pick<T extends z.ZodRawShape, K extends keyof T>(
|
|
695
|
+
schema: z.ZodObject<T>,
|
|
681
696
|
keys: readonly K[],
|
|
682
|
-
): z.ZodObject<Pick<T, K
|
|
697
|
+
): z.ZodObject<Pick<T, K>>
|
|
683
698
|
```
|
|
684
699
|
|
|
685
700
|
- `schema` — The Zod object schema to pick from.
|
|
@@ -839,8 +854,17 @@ const getValidProps: <Props>({
|
|
|
839
854
|
schema,
|
|
840
855
|
props,
|
|
841
856
|
}: {
|
|
857
|
+
/**
|
|
858
|
+
* The name of the resource (used in error messages).
|
|
859
|
+
*/
|
|
842
860
|
name: string
|
|
861
|
+
/**
|
|
862
|
+
* The Zod schema used when validating props.
|
|
863
|
+
*/
|
|
843
864
|
schema: ZodSchema<Props>
|
|
865
|
+
/**
|
|
866
|
+
* The `props` to be validated.
|
|
867
|
+
*/
|
|
844
868
|
props: unknown
|
|
845
869
|
}) => Props
|
|
846
870
|
```
|
|
@@ -860,7 +884,15 @@ Validates data against a Zod schema without throwing, returning a discriminated
|
|
|
860
884
|
const safeParse: <T>(
|
|
861
885
|
schema: ZodSchema<T>,
|
|
862
886
|
data: unknown,
|
|
863
|
-
) =>
|
|
887
|
+
) =>
|
|
888
|
+
| {
|
|
889
|
+
success: true
|
|
890
|
+
data: T
|
|
891
|
+
}
|
|
892
|
+
| {
|
|
893
|
+
success: false
|
|
894
|
+
error: ZodError
|
|
895
|
+
}
|
|
864
896
|
```
|
|
865
897
|
|
|
866
898
|
- `schema` — The Zod schema to validate against.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@molecule/api-resource",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.7",
|
|
4
4
|
"description": "Resource CRUD operations and utilities for molecule.dev.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -39,10 +39,10 @@
|
|
|
39
39
|
"zod": "4.6.7"
|
|
40
40
|
},
|
|
41
41
|
"devDependencies": {
|
|
42
|
-
"@molecule/api-bond": "1.0.
|
|
43
|
-
"@molecule/api-database": "1.1
|
|
42
|
+
"@molecule/api-bond": "1.0.4",
|
|
43
|
+
"@molecule/api-database": "1.2.1",
|
|
44
44
|
"@molecule/api-locales-resource": "1.0.2",
|
|
45
|
-
"@molecule/api-utilities-validation": "1.0.
|
|
45
|
+
"@molecule/api-utilities-validation": "1.0.4",
|
|
46
46
|
"@types/node": "26.6.5",
|
|
47
47
|
"typescript": "6.0.3",
|
|
48
48
|
"vitest": "4.1.12"
|