@molecule/api-resource 1.0.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/LICENSE +115 -0
- package/dist/browser-guard.d.ts +2 -0
- package/dist/browser-guard.d.ts.map +1 -0
- package/dist/browser-guard.js +19 -0
- package/dist/browser-guard.js.map +1 -0
- package/dist/create.d.ts +58 -0
- package/dist/create.d.ts.map +1 -0
- package/dist/create.js +87 -0
- package/dist/create.js.map +1 -0
- package/dist/createRequestHandler.d.ts +48 -0
- package/dist/createRequestHandler.d.ts.map +1 -0
- package/dist/createRequestHandler.js +42 -0
- package/dist/createRequestHandler.js.map +1 -0
- package/dist/del.d.ts +55 -0
- package/dist/del.d.ts.map +1 -0
- package/dist/del.js +63 -0
- package/dist/del.js.map +1 -0
- package/dist/http-types.d.ts +58 -0
- package/dist/http-types.d.ts.map +1 -0
- package/dist/http-types.js +11 -0
- package/dist/http-types.js.map +1 -0
- package/dist/i18n.d.ts +11 -0
- package/dist/i18n.d.ts.map +1 -0
- package/dist/i18n.js +19 -0
- package/dist/i18n.js.map +1 -0
- package/dist/index.d.ts +100 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +100 -0
- package/dist/index.js.map +1 -0
- package/dist/query.d.ts +102 -0
- package/dist/query.d.ts.map +1 -0
- package/dist/query.js +100 -0
- package/dist/query.js.map +1 -0
- package/dist/read.d.ts +57 -0
- package/dist/read.d.ts.map +1 -0
- package/dist/read.js +56 -0
- package/dist/read.js.map +1 -0
- package/dist/respond-error.d.ts +44 -0
- package/dist/respond-error.d.ts.map +1 -0
- package/dist/respond-error.js +46 -0
- package/dist/respond-error.js.map +1 -0
- package/dist/schema.d.ts +81 -0
- package/dist/schema.d.ts.map +1 -0
- package/dist/schema.js +112 -0
- package/dist/schema.js.map +1 -0
- package/dist/types.d.ts +88 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +10 -0
- package/dist/types.js.map +1 -0
- package/dist/update.d.ts +59 -0
- package/dist/update.d.ts.map +1 -0
- package/dist/update.js +82 -0
- package/dist/update.js.map +1 -0
- package/dist/utilities/index.d.ts +9 -0
- package/dist/utilities/index.d.ts.map +1 -0
- package/dist/utilities/index.js +9 -0
- package/dist/utilities/index.js.map +1 -0
- package/package.json +66 -0
package/dist/schema.d.ts
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Zod schemas and validation utilities for resource operations.
|
|
3
|
+
*
|
|
4
|
+
* Every resource extends `basePropsSchema` which defines the `id`,
|
|
5
|
+
* `createdAt`, and `updatedAt` fields common to all database records.
|
|
6
|
+
*
|
|
7
|
+
* @module
|
|
8
|
+
*/
|
|
9
|
+
import { z } from 'zod';
|
|
10
|
+
/**
|
|
11
|
+
* Base Zod schema with the three fields every resource row must have:
|
|
12
|
+
* `id` (UUID), `createdAt` (ISO datetime), and `updatedAt` (ISO datetime).
|
|
13
|
+
*/
|
|
14
|
+
export declare const basePropsSchema: z.ZodObject<{
|
|
15
|
+
id: z.ZodString;
|
|
16
|
+
createdAt: z.ZodString;
|
|
17
|
+
updatedAt: z.ZodString;
|
|
18
|
+
}, z.core.$strip>;
|
|
19
|
+
/**
|
|
20
|
+
* TypeScript type inferred from `basePropsSchema`, containing `id`, `createdAt`, and `updatedAt`.
|
|
21
|
+
*/
|
|
22
|
+
export type BaseProps = z.infer<typeof basePropsSchema>;
|
|
23
|
+
/**
|
|
24
|
+
* Creates a Zod schema for the standard `{ statusCode, body }` response
|
|
25
|
+
* returned by resource CRUD operations.
|
|
26
|
+
*
|
|
27
|
+
* @param propsSchema - The Zod schema for the resource's props (e.g. `userPropsSchema`).
|
|
28
|
+
* @returns A Zod schema that validates `{ statusCode: number, body: { props? } | T[] }`.
|
|
29
|
+
*/
|
|
30
|
+
export declare const responseSchema: <T extends z.ZodTypeAny>(propsSchema: T) => z.ZodTypeAny;
|
|
31
|
+
/**
|
|
32
|
+
* Creates a Zod schema that validates a resource descriptor object
|
|
33
|
+
* (`{ name, tableName, schema }`).
|
|
34
|
+
*
|
|
35
|
+
* @param _propsSchema - The Zod schema for the resource's props (used for type inference only).
|
|
36
|
+
* @returns A Zod schema that validates the resource descriptor shape.
|
|
37
|
+
*/
|
|
38
|
+
export declare const resourceSchema: <T extends z.ZodTypeAny>(_propsSchema: T) => z.ZodTypeAny;
|
|
39
|
+
/**
|
|
40
|
+
* Validates data against a Zod schema, throwing an `Error` with a
|
|
41
|
+
* human-readable message listing all validation failures if invalid.
|
|
42
|
+
*
|
|
43
|
+
* @param schema - The Zod schema to validate against.
|
|
44
|
+
* @param data - The data to validate.
|
|
45
|
+
* @returns The parsed and typed data if validation succeeds.
|
|
46
|
+
* @throws {Error} Error with comma-separated validation issue descriptions.
|
|
47
|
+
*/
|
|
48
|
+
export declare const validate: <T>(schema: z.ZodSchema<T>, data: unknown) => T;
|
|
49
|
+
/**
|
|
50
|
+
* Validates data against a Zod schema without throwing. Returns a discriminated
|
|
51
|
+
* union: `{ success: true, data }` on success or `{ success: false, error }` on failure.
|
|
52
|
+
*
|
|
53
|
+
* @param schema - The Zod schema to validate against.
|
|
54
|
+
* @param data - The data to validate.
|
|
55
|
+
* @returns A discriminated union indicating success with parsed data, or failure with a `ZodError`.
|
|
56
|
+
*/
|
|
57
|
+
export declare const safeParse: <T>(schema: z.ZodSchema<T>, data: unknown) => {
|
|
58
|
+
success: true;
|
|
59
|
+
data: T;
|
|
60
|
+
} | {
|
|
61
|
+
success: false;
|
|
62
|
+
error: z.ZodError;
|
|
63
|
+
};
|
|
64
|
+
/**
|
|
65
|
+
* Creates a schema where all fields are optional, suitable for validating
|
|
66
|
+
* partial update payloads where only changed fields are sent.
|
|
67
|
+
*
|
|
68
|
+
* @param schema - The Zod object schema to make partial.
|
|
69
|
+
* @returns A new Zod schema with all fields marked optional.
|
|
70
|
+
*/
|
|
71
|
+
export declare const partial: <T extends z.ZodRawShape>(schema: z.ZodObject<T>) => z.ZodObject<{ [K in keyof T]: z.ZodOptional<T[K]>; }>;
|
|
72
|
+
/**
|
|
73
|
+
* Creates a schema with only the specified fields, useful for create payloads
|
|
74
|
+
* where only a subset of the full props should be provided by the client.
|
|
75
|
+
*
|
|
76
|
+
* @param schema - The Zod object schema to pick from.
|
|
77
|
+
* @param keys - The field names to include in the new schema.
|
|
78
|
+
* @returns A new Zod schema containing only the picked fields.
|
|
79
|
+
*/
|
|
80
|
+
export declare const pick: <T extends z.ZodRawShape, K extends keyof T>(schema: z.ZodObject<T>, keys: readonly K[]) => z.ZodObject<Pick<T, K>>;
|
|
81
|
+
//# sourceMappingURL=schema.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../src/schema.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AAEvB;;;GAGG;AACH,eAAO,MAAM,eAAe;;;;iBAe1B,CAAA;AAEF;;GAEG;AACH,MAAM,MAAM,SAAS,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,eAAe,CAAC,CAAA;AAEvD;;;;;;GAMG;AACH,eAAO,MAAM,cAAc,GAAI,CAAC,SAAS,CAAC,CAAC,UAAU,EAAE,aAAa,CAAC,KAAG,CAAC,CAAC,UAUtE,CAAA;AAEJ;;;;;;GAMG;AACH,eAAO,MAAM,cAAc,GAAI,CAAC,SAAS,CAAC,CAAC,UAAU,EAAE,cAAc,CAAC,KAAG,CAAC,CAAC,UAKvE,CAAA;AAEJ;;;;;;;;GAQG;AACH,eAAO,MAAM,QAAQ,GAAI,CAAC,EAAE,QAAQ,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,MAAM,OAAO,KAAG,CAOnE,CAAA;AAED;;;;;;;GAOG;AACH,eAAO,MAAM,SAAS,GAAI,CAAC,EACzB,QAAQ,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,EACtB,MAAM,OAAO,KACZ;IAAE,OAAO,EAAE,IAAI,CAAC;IAAC,IAAI,EAAE,CAAC,CAAA;CAAE,GAAG;IAAE,OAAO,EAAE,KAAK,CAAC;IAAC,KAAK,EAAE,CAAC,CAAC,QAAQ,CAAA;CAElE,CAAA;AAED;;;;;;GAMG;AACH,eAAO,MAAM,OAAO,GAAI,CAAC,SAAS,CAAC,CAAC,WAAW,EAC7C,QAAQ,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,KACrB,CAAC,CAAC,SAAS,CAAC,GAAG,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAAE,CAErD,CAAA;AAED;;;;;;;GAOG;AACH,eAAO,MAAM,IAAI,GAAI,CAAC,SAAS,CAAC,CAAC,WAAW,EAAE,CAAC,SAAS,MAAM,CAAC,EAC7D,QAAQ,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,EACtB,MAAM,SAAS,CAAC,EAAE,KACjB,CAAC,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC,CAMxB,CAAA"}
|
package/dist/schema.js
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Zod schemas and validation utilities for resource operations.
|
|
3
|
+
*
|
|
4
|
+
* Every resource extends `basePropsSchema` which defines the `id`,
|
|
5
|
+
* `createdAt`, and `updatedAt` fields common to all database records.
|
|
6
|
+
*
|
|
7
|
+
* @module
|
|
8
|
+
*/
|
|
9
|
+
import { z } from 'zod';
|
|
10
|
+
/**
|
|
11
|
+
* Base Zod schema with the three fields every resource row must have:
|
|
12
|
+
* `id` (UUID), `createdAt` (ISO datetime), and `updatedAt` (ISO datetime).
|
|
13
|
+
*/
|
|
14
|
+
export const basePropsSchema = z.object({
|
|
15
|
+
/**
|
|
16
|
+
* Usually a UUID.
|
|
17
|
+
*/
|
|
18
|
+
id: z.string().uuid(),
|
|
19
|
+
/**
|
|
20
|
+
* When the resource was created.
|
|
21
|
+
* Usually an ISO 8601 timestamp.
|
|
22
|
+
*/
|
|
23
|
+
createdAt: z.string().datetime(),
|
|
24
|
+
/**
|
|
25
|
+
* When the resource was last updated.
|
|
26
|
+
* Usually an ISO 8601 timestamp.
|
|
27
|
+
*/
|
|
28
|
+
updatedAt: z.string().datetime(),
|
|
29
|
+
});
|
|
30
|
+
/**
|
|
31
|
+
* Creates a Zod schema for the standard `{ statusCode, body }` response
|
|
32
|
+
* returned by resource CRUD operations.
|
|
33
|
+
*
|
|
34
|
+
* @param propsSchema - The Zod schema for the resource's props (e.g. `userPropsSchema`).
|
|
35
|
+
* @returns A Zod schema that validates `{ statusCode: number, body: { props? } | T[] }`.
|
|
36
|
+
*/
|
|
37
|
+
export const responseSchema = (propsSchema) => z.object({
|
|
38
|
+
statusCode: z.number(),
|
|
39
|
+
body: z.union([
|
|
40
|
+
z.object({
|
|
41
|
+
props: propsSchema.optional(),
|
|
42
|
+
error: z.string().optional(),
|
|
43
|
+
}),
|
|
44
|
+
z.array(propsSchema),
|
|
45
|
+
]),
|
|
46
|
+
});
|
|
47
|
+
/**
|
|
48
|
+
* Creates a Zod schema that validates a resource descriptor object
|
|
49
|
+
* (`{ name, tableName, schema }`).
|
|
50
|
+
*
|
|
51
|
+
* @param _propsSchema - The Zod schema for the resource's props (used for type inference only).
|
|
52
|
+
* @returns A Zod schema that validates the resource descriptor shape.
|
|
53
|
+
*/
|
|
54
|
+
export const resourceSchema = (_propsSchema) => z.object({
|
|
55
|
+
name: z.string(),
|
|
56
|
+
tableName: z.string(),
|
|
57
|
+
schema: z.custom(() => true),
|
|
58
|
+
});
|
|
59
|
+
/**
|
|
60
|
+
* Validates data against a Zod schema, throwing an `Error` with a
|
|
61
|
+
* human-readable message listing all validation failures if invalid.
|
|
62
|
+
*
|
|
63
|
+
* @param schema - The Zod schema to validate against.
|
|
64
|
+
* @param data - The data to validate.
|
|
65
|
+
* @returns The parsed and typed data if validation succeeds.
|
|
66
|
+
* @throws {Error} Error with comma-separated validation issue descriptions.
|
|
67
|
+
*/
|
|
68
|
+
export const validate = (schema, data) => {
|
|
69
|
+
const result = schema.safeParse(data);
|
|
70
|
+
if (!result.success) {
|
|
71
|
+
const errors = result.error.issues.map((e) => `${e.path.join('.')}: ${e.message}`).join(', ');
|
|
72
|
+
throw new Error(`Validation failed: ${errors}`);
|
|
73
|
+
}
|
|
74
|
+
return result.data;
|
|
75
|
+
};
|
|
76
|
+
/**
|
|
77
|
+
* Validates data against a Zod schema without throwing. Returns a discriminated
|
|
78
|
+
* union: `{ success: true, data }` on success or `{ success: false, error }` on failure.
|
|
79
|
+
*
|
|
80
|
+
* @param schema - The Zod schema to validate against.
|
|
81
|
+
* @param data - The data to validate.
|
|
82
|
+
* @returns A discriminated union indicating success with parsed data, or failure with a `ZodError`.
|
|
83
|
+
*/
|
|
84
|
+
export const safeParse = (schema, data) => {
|
|
85
|
+
return schema.safeParse(data);
|
|
86
|
+
};
|
|
87
|
+
/**
|
|
88
|
+
* Creates a schema where all fields are optional, suitable for validating
|
|
89
|
+
* partial update payloads where only changed fields are sent.
|
|
90
|
+
*
|
|
91
|
+
* @param schema - The Zod object schema to make partial.
|
|
92
|
+
* @returns A new Zod schema with all fields marked optional.
|
|
93
|
+
*/
|
|
94
|
+
export const partial = (schema) => {
|
|
95
|
+
return schema.partial();
|
|
96
|
+
};
|
|
97
|
+
/**
|
|
98
|
+
* Creates a schema with only the specified fields, useful for create payloads
|
|
99
|
+
* where only a subset of the full props should be provided by the client.
|
|
100
|
+
*
|
|
101
|
+
* @param schema - The Zod object schema to pick from.
|
|
102
|
+
* @param keys - The field names to include in the new schema.
|
|
103
|
+
* @returns A new Zod schema containing only the picked fields.
|
|
104
|
+
*/
|
|
105
|
+
export const pick = (schema, keys) => {
|
|
106
|
+
const shape = {};
|
|
107
|
+
for (const key of keys) {
|
|
108
|
+
shape[key] = schema.shape[key];
|
|
109
|
+
}
|
|
110
|
+
return z.object(shape);
|
|
111
|
+
};
|
|
112
|
+
//# sourceMapping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
|
+
* Core type definitions for the resource CRUD framework.
|
|
3
|
+
*
|
|
4
|
+
* Defines the `Props`, `Resource`, and `Response` interfaces used by
|
|
5
|
+
* the `create`, `read`, `update`, `del`, and `query` handler factories.
|
|
6
|
+
*
|
|
7
|
+
* @module
|
|
8
|
+
*/
|
|
9
|
+
import type { z } from 'zod';
|
|
10
|
+
import type { BaseProps } from './schema.js';
|
|
11
|
+
export type { BaseProps };
|
|
12
|
+
/**
|
|
13
|
+
* The resource's properties to be stored in the database.
|
|
14
|
+
*
|
|
15
|
+
* Used by CRUD operations for type inference.
|
|
16
|
+
*/
|
|
17
|
+
export interface Props {
|
|
18
|
+
/**
|
|
19
|
+
* Usually a UUID.
|
|
20
|
+
*/
|
|
21
|
+
id: string;
|
|
22
|
+
/**
|
|
23
|
+
* When the resource was created.
|
|
24
|
+
*
|
|
25
|
+
* Usually an ISO 8601 timestamp.
|
|
26
|
+
*/
|
|
27
|
+
createdAt: string;
|
|
28
|
+
/**
|
|
29
|
+
* When the resource was last updated.
|
|
30
|
+
*
|
|
31
|
+
* Usually an ISO 8601 timestamp.
|
|
32
|
+
*/
|
|
33
|
+
updatedAt: string;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* An object describing the resource.
|
|
37
|
+
*
|
|
38
|
+
* The schema can be a Zod schema (z.ZodTypeAny) for new code,
|
|
39
|
+
* or any other schema type for backward compatibility.
|
|
40
|
+
*/
|
|
41
|
+
export interface Resource<T = unknown> {
|
|
42
|
+
/**
|
|
43
|
+
* The name of the resource.
|
|
44
|
+
*
|
|
45
|
+
* @example User
|
|
46
|
+
*/
|
|
47
|
+
name: string;
|
|
48
|
+
/**
|
|
49
|
+
* The database table name for the resource.
|
|
50
|
+
*
|
|
51
|
+
* @example users
|
|
52
|
+
*/
|
|
53
|
+
tableName: string;
|
|
54
|
+
/**
|
|
55
|
+
* The schema used when validating props.
|
|
56
|
+
*
|
|
57
|
+
* Can be a Zod schema (z.ZodTypeAny) or other schema formats.
|
|
58
|
+
*/
|
|
59
|
+
schema: T;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* An object describing the resource with a Zod schema.
|
|
63
|
+
*
|
|
64
|
+
* Use this type when you want to enforce Zod schema usage.
|
|
65
|
+
*/
|
|
66
|
+
export interface ZodResource<T extends z.ZodTypeAny = z.ZodTypeAny> {
|
|
67
|
+
name: string;
|
|
68
|
+
tableName: string;
|
|
69
|
+
schema: T;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Standard response from resource operations.
|
|
73
|
+
*/
|
|
74
|
+
export interface Response<T = unknown> {
|
|
75
|
+
/**
|
|
76
|
+
* HTTP status code to return.
|
|
77
|
+
*/
|
|
78
|
+
statusCode: number;
|
|
79
|
+
/**
|
|
80
|
+
* The response payload.
|
|
81
|
+
*/
|
|
82
|
+
body: {
|
|
83
|
+
props?: T;
|
|
84
|
+
error?: string;
|
|
85
|
+
errorKey?: string;
|
|
86
|
+
} | T[];
|
|
87
|
+
}
|
|
88
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AAE5B,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,aAAa,CAAA;AAG5C,YAAY,EAAE,SAAS,EAAE,CAAA;AAEzB;;;;GAIG;AACH,MAAM,WAAW,KAAK;IACpB;;OAEG;IACH,EAAE,EAAE,MAAM,CAAA;IACV;;;;OAIG;IACH,SAAS,EAAE,MAAM,CAAA;IACjB;;;;OAIG;IACH,SAAS,EAAE,MAAM,CAAA;CAClB;AAED;;;;;GAKG;AACH,MAAM,WAAW,QAAQ,CAAC,CAAC,GAAG,OAAO;IACnC;;;;OAIG;IACH,IAAI,EAAE,MAAM,CAAA;IACZ;;;;OAIG;IACH,SAAS,EAAE,MAAM,CAAA;IACjB;;;;OAIG;IACH,MAAM,EAAE,CAAC,CAAA;CACV;AAED;;;;GAIG;AACH,MAAM,WAAW,WAAW,CAAC,CAAC,SAAS,CAAC,CAAC,UAAU,GAAG,CAAC,CAAC,UAAU;IAChE,IAAI,EAAE,MAAM,CAAA;IACZ,SAAS,EAAE,MAAM,CAAA;IACjB,MAAM,EAAE,CAAC,CAAA;CACV;AAED;;GAEG;AACH,MAAM,WAAW,QAAQ,CAAC,CAAC,GAAG,OAAO;IACnC;;OAEG;IACH,UAAU,EAAE,MAAM,CAAA;IAClB;;OAEG;IACH,IAAI,EACA;QACE,KAAK,CAAC,EAAE,CAAC,CAAA;QACT,KAAK,CAAC,EAAE,MAAM,CAAA;QACd,QAAQ,CAAC,EAAE,MAAM,CAAA;KAClB,GACD,CAAC,EAAE,CAAA;CACR"}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Core type definitions for the resource CRUD framework.
|
|
3
|
+
*
|
|
4
|
+
* Defines the `Props`, `Resource`, and `Response` interfaces used by
|
|
5
|
+
* the `create`, `read`, `update`, `del`, and `query` handler factories.
|
|
6
|
+
*
|
|
7
|
+
* @module
|
|
8
|
+
*/
|
|
9
|
+
export {};
|
|
10
|
+
//# sourceMappingURL=types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG"}
|
package/dist/update.d.ts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Resource update operation.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import type * as types from './types.js';
|
|
7
|
+
/**
|
|
8
|
+
* Creates a handler to update a resource by its `id` and some `props`.
|
|
9
|
+
*
|
|
10
|
+
* Uses the bonded DataStore from `@molecule/api-database` for database-agnostic operations.
|
|
11
|
+
*
|
|
12
|
+
* Example usage:
|
|
13
|
+
* ```ts
|
|
14
|
+
* import { update as resourceUpdate } from '@molecule/api-resource'
|
|
15
|
+
* import type * as types from '../../types.js'
|
|
16
|
+
*
|
|
17
|
+
* export const update = ({ name, tableName, schema }: types.Resource) => {
|
|
18
|
+
* const update = resourceUpdate({ name, tableName, schema })
|
|
19
|
+
*
|
|
20
|
+
* return async (req: MoleculeRequest) => {
|
|
21
|
+
* const id = req.params.id
|
|
22
|
+
* const props = req.body
|
|
23
|
+
* return await update({ id, props })
|
|
24
|
+
* }
|
|
25
|
+
* }
|
|
26
|
+
* ```
|
|
27
|
+
* @param resource - The resource descriptor.
|
|
28
|
+
* @param resource.name - Human-readable resource name used in error messages (e.g. `'User'`).
|
|
29
|
+
* @param resource.tableName - Database table to update the row in.
|
|
30
|
+
* @param resource.schema - Zod schema to validate the incoming props against.
|
|
31
|
+
* @returns A curried async function that accepts `{ id, props }` and returns a `{ statusCode, body }` response.
|
|
32
|
+
*/
|
|
33
|
+
export declare const update: <UpdateProps extends Record<string, unknown> = Record<string, unknown>, UpdatedProps extends Pick<types.Props, `updatedAt`> & UpdateProps = Pick<types.Props, `updatedAt`> & UpdateProps>({ name, tableName, schema, }: types.Resource) => ({ id, props: updateProps, }: {
|
|
34
|
+
/**
|
|
35
|
+
* The `id` of the resource to be updated.
|
|
36
|
+
*/
|
|
37
|
+
id: UpdatedProps["id"] | types.Props["id"];
|
|
38
|
+
/**
|
|
39
|
+
* The properties to be updated.
|
|
40
|
+
*
|
|
41
|
+
* Usually from `req.body`.
|
|
42
|
+
*/
|
|
43
|
+
props: UpdateProps;
|
|
44
|
+
}) => Promise<{
|
|
45
|
+
statusCode: number;
|
|
46
|
+
body: {
|
|
47
|
+
error: string;
|
|
48
|
+
errorKey: string;
|
|
49
|
+
props?: undefined;
|
|
50
|
+
};
|
|
51
|
+
} | {
|
|
52
|
+
statusCode: number;
|
|
53
|
+
body: {
|
|
54
|
+
props: UpdatedProps;
|
|
55
|
+
error?: undefined;
|
|
56
|
+
errorKey?: undefined;
|
|
57
|
+
};
|
|
58
|
+
}>;
|
|
59
|
+
//# sourceMappingURL=update.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"update.d.ts","sourceRoot":"","sources":["../src/update.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AASH,OAAO,KAAK,KAAK,KAAK,MAAM,YAAY,CAAA;AAGxC;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,eAAO,MAAM,MAAM,GAEf,WAAW,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EACrE,YAAY,SAAS,IAAI,CAAC,KAAK,CAAC,KAAK,EAAE,WAAW,CAAC,GAAG,WAAW,GAAG,IAAI,CACtE,KAAK,CAAC,KAAK,EACX,WAAW,CACZ,GACC,WAAW,EACb,8BAIC,KAAK,CAAC,QAAQ,MACV,6BAGJ;IACD;;OAEG;IACH,EAAE,EAAE,YAAY,CAAC,IAAI,CAAC,GAAG,KAAK,CAAC,KAAK,CAAC,IAAI,CAAC,CAAA;IAC1C;;;;OAIG;IACH,KAAK,EAAE,WAAW,CAAA;CACnB;;;;;;;;;;;;;;EAuDA,CAAA"}
|
package/dist/update.js
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Resource update operation.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import { getLogger } from '@molecule/api-bond';
|
|
7
|
+
const logger = getLogger();
|
|
8
|
+
import { updateById } from '@molecule/api-database';
|
|
9
|
+
import { t } from '@molecule/api-i18n';
|
|
10
|
+
import { getValidProps } from './utilities/index.js';
|
|
11
|
+
/**
|
|
12
|
+
* Creates a handler to update a resource by its `id` and some `props`.
|
|
13
|
+
*
|
|
14
|
+
* Uses the bonded DataStore from `@molecule/api-database` for database-agnostic operations.
|
|
15
|
+
*
|
|
16
|
+
* Example usage:
|
|
17
|
+
* ```ts
|
|
18
|
+
* import { update as resourceUpdate } from '@molecule/api-resource'
|
|
19
|
+
* import type * as types from '../../types.js'
|
|
20
|
+
*
|
|
21
|
+
* export const update = ({ name, tableName, schema }: types.Resource) => {
|
|
22
|
+
* const update = resourceUpdate({ name, tableName, schema })
|
|
23
|
+
*
|
|
24
|
+
* return async (req: MoleculeRequest) => {
|
|
25
|
+
* const id = req.params.id
|
|
26
|
+
* const props = req.body
|
|
27
|
+
* return await update({ id, props })
|
|
28
|
+
* }
|
|
29
|
+
* }
|
|
30
|
+
* ```
|
|
31
|
+
* @param resource - The resource descriptor.
|
|
32
|
+
* @param resource.name - Human-readable resource name used in error messages (e.g. `'User'`).
|
|
33
|
+
* @param resource.tableName - Database table to update the row in.
|
|
34
|
+
* @param resource.schema - Zod schema to validate the incoming props against.
|
|
35
|
+
* @returns A curried async function that accepts `{ id, props }` and returns a `{ statusCode, body }` response.
|
|
36
|
+
*/
|
|
37
|
+
export const update = ({ name, tableName, schema, }) => async ({ id, props: updateProps, }) => {
|
|
38
|
+
// TypeScript 5+ requires explicit unknown cast for generic type conversions
|
|
39
|
+
let props = updateProps;
|
|
40
|
+
try {
|
|
41
|
+
// Describe when the resource was updated.
|
|
42
|
+
props.updatedAt = new Date().toISOString();
|
|
43
|
+
// Validate the new `props`.
|
|
44
|
+
props = getValidProps({
|
|
45
|
+
name,
|
|
46
|
+
schema: schema,
|
|
47
|
+
props,
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
catch (error) {
|
|
51
|
+
// The provided `props` failed validation.
|
|
52
|
+
return {
|
|
53
|
+
statusCode: 400,
|
|
54
|
+
body: {
|
|
55
|
+
error: error
|
|
56
|
+
? String(error)
|
|
57
|
+
: t('resource.error.unknownError', undefined, { defaultValue: 'Unknown error.' }),
|
|
58
|
+
errorKey: 'resource.error.unknownError',
|
|
59
|
+
},
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
try {
|
|
63
|
+
// Update the resource in the database via the bonded DataStore.
|
|
64
|
+
const result = await updateById(tableName, id, props);
|
|
65
|
+
if (!result.affected) {
|
|
66
|
+
throw new Error(`Unable to update ${tableName} by id: ${id}`);
|
|
67
|
+
}
|
|
68
|
+
// If we made it here, updates to the database were successful.
|
|
69
|
+
return { statusCode: 200, body: { props } };
|
|
70
|
+
}
|
|
71
|
+
catch (error) {
|
|
72
|
+
logger.error(error);
|
|
73
|
+
return {
|
|
74
|
+
statusCode: error.statusCode || 400,
|
|
75
|
+
body: {
|
|
76
|
+
error: t('resource.error.unableToUpdate', { name }, { defaultValue: `Unable to update ${name}.` }),
|
|
77
|
+
errorKey: 'resource.error.unableToUpdate',
|
|
78
|
+
},
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
//# sourceMappingURL=update.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"update.js","sourceRoot":"","sources":["../src/update.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAIH,OAAO,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAA;AAC9C,MAAM,MAAM,GAAG,SAAS,EAAE,CAAA;AAC1B,OAAO,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAA;AACnD,OAAO,EAAE,CAAC,EAAE,MAAM,oBAAoB,CAAA;AAGtC,OAAO,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAA;AAEpD;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,MAAM,CAAC,MAAM,MAAM,GACjB,CAOE,EACA,IAAI,EACJ,SAAS,EACT,MAAM,GACS,EAAE,EAAE,CACrB,KAAK,EAAE,EACL,EAAE,EACF,KAAK,EAAE,WAAW,GAYnB,EAAE,EAAE;IACH,4EAA4E;IAC5E,IAAI,KAAK,GAAG,WAAsC,CAAA;IAElD,IAAI,CAAC;QACH,0CAA0C;QAC1C,KAAK,CAAC,SAAS,GAAG,IAAI,IAAI,EAAE,CAAC,WAAW,E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"","sources":["../../src/utilities/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,cAAc,oCAAoC,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/utilities/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,cAAc,oCAAoC,CAAA"}
|
package/package.json
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@molecule/api-resource",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Resource CRUD operations and utilities for molecule.dev.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"build": "tsc",
|
|
10
|
+
"test": "vitest run",
|
|
11
|
+
"test:watch": "vitest"
|
|
12
|
+
},
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"import": "./dist/index.js"
|
|
17
|
+
},
|
|
18
|
+
"./types": {
|
|
19
|
+
"types": "./dist/types.d.ts",
|
|
20
|
+
"import": "./dist/types.js"
|
|
21
|
+
},
|
|
22
|
+
"./schema": {
|
|
23
|
+
"types": "./dist/schema.d.ts",
|
|
24
|
+
"import": "./dist/schema.js"
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
"files": [
|
|
28
|
+
"dist"
|
|
29
|
+
],
|
|
30
|
+
"keywords": [
|
|
31
|
+
"molecule",
|
|
32
|
+
"resource",
|
|
33
|
+
"crud"
|
|
34
|
+
],
|
|
35
|
+
"license": "Apache-2.0",
|
|
36
|
+
"dependencies": {
|
|
37
|
+
"uuid": "14.0.1",
|
|
38
|
+
"zod": "4.4.3"
|
|
39
|
+
},
|
|
40
|
+
"devDependencies": {
|
|
41
|
+
"@molecule/api-bond": "1.0.0",
|
|
42
|
+
"@molecule/api-database": "1.0.0",
|
|
43
|
+
"@molecule/api-locales-resource": "1.0.0",
|
|
44
|
+
"@molecule/api-utilities-validation": "1.0.0",
|
|
45
|
+
"@types/node": "26.1.2",
|
|
46
|
+
"typescript": "6.0.3",
|
|
47
|
+
"vitest": "4.1.10"
|
|
48
|
+
},
|
|
49
|
+
"peerDependencies": {
|
|
50
|
+
"@molecule/api-bond": "^1.0.0",
|
|
51
|
+
"@molecule/api-database": "^1.0.0",
|
|
52
|
+
"@molecule/api-i18n": "^1.0.0",
|
|
53
|
+
"@molecule/api-locales-resource": "^1.0.0",
|
|
54
|
+
"@molecule/api-utilities-validation": "^1.0.0"
|
|
55
|
+
},
|
|
56
|
+
"repository": {
|
|
57
|
+
"type": "git",
|
|
58
|
+
"url": "https://github.com/molecule-dev/molecule.git",
|
|
59
|
+
"directory": "packages/api/infrastructure/resource"
|
|
60
|
+
},
|
|
61
|
+
"homepage": "https://github.com/molecule-dev/molecule/tree/main/packages/api/infrastructure/resource",
|
|
62
|
+
"bugs": "https://github.com/molecule-dev/molecule/issues",
|
|
63
|
+
"publishConfig": {
|
|
64
|
+
"access": "public"
|
|
65
|
+
}
|
|
66
|
+
}
|