@stonecrop/graphql-middleware 0.14.0 → 0.16.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 +11 -6
- package/dist/plugin/postgraphile.js +0 -583
- package/dist/preset.js +0 -45
- package/dist/registry/actions.js +0 -163
- package/dist/registry/doctypes.js +0 -161
- package/dist/src/registry/actions.d.ts +0 -36
- package/dist/src/registry/actions.d.ts.map +0 -1
- package/dist/src/registry/actions.js +0 -169
- package/dist/src/types/context.d.ts +0 -20
- package/dist/src/types/context.d.ts.map +0 -1
- package/dist/src/types/index.d.ts +0 -46
- package/dist/src/types/index.d.ts.map +0 -1
- package/dist/src/types/index.js +0 -3
- package/dist/src/types/preset.d.ts +0 -18
- package/dist/src/types/preset.d.ts.map +0 -1
- package/dist/src/types/query.d.ts +0 -132
- package/dist/src/types/query.d.ts.map +0 -1
- package/dist/src/types/query.js +0 -3
- package/dist/typeDefs.js +0 -79
- package/dist/types/context.js +0 -0
- package/dist/types/index.js +0 -3
- package/dist/types/preset.js +0 -0
- package/dist/types/query.js +0 -3
package/dist/registry/actions.js
DELETED
|
@@ -1,163 +0,0 @@
|
|
|
1
|
-
const handlerRegistry = new Map();
|
|
2
|
-
/**
|
|
3
|
-
* Register an action handler
|
|
4
|
-
* @param name - Unique name for the action handler
|
|
5
|
-
* @param handler - Action handler function to register
|
|
6
|
-
* @public
|
|
7
|
-
*/
|
|
8
|
-
export function registerHandler(name, handler) {
|
|
9
|
-
handlerRegistry.set(name, handler);
|
|
10
|
-
}
|
|
11
|
-
/**
|
|
12
|
-
* Get a registered handler by name
|
|
13
|
-
* @param name - Name of the action handler to retrieve
|
|
14
|
-
* @public
|
|
15
|
-
*/
|
|
16
|
-
export function getHandler(name) {
|
|
17
|
-
return handlerRegistry.get(name);
|
|
18
|
-
}
|
|
19
|
-
/**
|
|
20
|
-
* Check if a handler is registered
|
|
21
|
-
* @param name - Name of the action handler to check
|
|
22
|
-
* @public
|
|
23
|
-
*/
|
|
24
|
-
export function hasHandler(name) {
|
|
25
|
-
return handlerRegistry.has(name);
|
|
26
|
-
}
|
|
27
|
-
/**
|
|
28
|
-
* Clear all registered handlers
|
|
29
|
-
* @public
|
|
30
|
-
*/
|
|
31
|
-
export function clearHandlers() {
|
|
32
|
-
handlerRegistry.clear();
|
|
33
|
-
}
|
|
34
|
-
// =============================================================================
|
|
35
|
-
// Built-in Handlers
|
|
36
|
-
// =============================================================================
|
|
37
|
-
/**
|
|
38
|
-
* Validate that all required fields are present in a record
|
|
39
|
-
*/
|
|
40
|
-
const validateRequiredFields = async (args, context) => {
|
|
41
|
-
const [record] = args;
|
|
42
|
-
const { doctype } = context;
|
|
43
|
-
const missing = [];
|
|
44
|
-
for (const field of doctype.fields) {
|
|
45
|
-
if (field.required && (record[field.fieldname] === undefined || record[field.fieldname] === null)) {
|
|
46
|
-
missing.push(field.label ?? field.fieldname);
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
if (missing.length > 0) {
|
|
50
|
-
throw new Error(`Missing required fields: ${missing.join(', ')}`);
|
|
51
|
-
}
|
|
52
|
-
return { valid: true };
|
|
53
|
-
};
|
|
54
|
-
/**
|
|
55
|
-
* Validate field types match expected types
|
|
56
|
-
*/
|
|
57
|
-
const validateFieldTypes = async (args, context) => {
|
|
58
|
-
const [record] = args;
|
|
59
|
-
const { doctype } = context;
|
|
60
|
-
const errors = [];
|
|
61
|
-
for (const field of doctype.fields) {
|
|
62
|
-
const value = record[field.fieldname];
|
|
63
|
-
if (value === undefined || value === null)
|
|
64
|
-
continue;
|
|
65
|
-
const error = validateFieldValue(field, value);
|
|
66
|
-
if (error)
|
|
67
|
-
errors.push(error);
|
|
68
|
-
}
|
|
69
|
-
// Validate link fields
|
|
70
|
-
if (doctype.links) {
|
|
71
|
-
for (const [fieldname, link] of Object.entries(doctype.links)) {
|
|
72
|
-
const value = record[fieldname];
|
|
73
|
-
if (value === undefined || value === null)
|
|
74
|
-
continue;
|
|
75
|
-
const isMany = link.cardinality === 'noneOrMany' || link.cardinality === 'atLeastOne';
|
|
76
|
-
if (isMany) {
|
|
77
|
-
if (!Array.isArray(value)) {
|
|
78
|
-
errors.push(`${fieldname}: expected array, got ${typeof value}`);
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
else {
|
|
82
|
-
if (typeof value !== 'object' || Array.isArray(value)) {
|
|
83
|
-
errors.push(`${fieldname}: expected object, got ${typeof value}`);
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
if (errors.length > 0) {
|
|
89
|
-
throw new Error(`Field type validation failed:\n${errors.join('\n')}`);
|
|
90
|
-
}
|
|
91
|
-
return { valid: true };
|
|
92
|
-
};
|
|
93
|
-
/**
|
|
94
|
-
* Validate a single field value against its expected type
|
|
95
|
-
*/
|
|
96
|
-
function validateFieldValue(field, value) {
|
|
97
|
-
const { fieldname, fieldtype, cardinality } = field;
|
|
98
|
-
switch (fieldtype) {
|
|
99
|
-
case 'Int':
|
|
100
|
-
if (typeof value !== 'number' || !Number.isInteger(value)) {
|
|
101
|
-
return `${fieldname}: expected integer, got ${typeof value}`;
|
|
102
|
-
}
|
|
103
|
-
break;
|
|
104
|
-
case 'Float':
|
|
105
|
-
case 'Decimal':
|
|
106
|
-
case 'Currency':
|
|
107
|
-
case 'Quantity':
|
|
108
|
-
if (typeof value !== 'number' && typeof value !== 'string') {
|
|
109
|
-
return `${fieldname}: expected number, got ${typeof value}`;
|
|
110
|
-
}
|
|
111
|
-
break;
|
|
112
|
-
case 'Check':
|
|
113
|
-
if (typeof value !== 'boolean') {
|
|
114
|
-
return `${fieldname}: expected boolean, got ${typeof value}`;
|
|
115
|
-
}
|
|
116
|
-
break;
|
|
117
|
-
case 'Data':
|
|
118
|
-
case 'Text':
|
|
119
|
-
case 'Code':
|
|
120
|
-
case 'Link':
|
|
121
|
-
if (typeof value !== 'string') {
|
|
122
|
-
return `${fieldname}: expected string, got ${typeof value}`;
|
|
123
|
-
}
|
|
124
|
-
break;
|
|
125
|
-
case 'Date':
|
|
126
|
-
case 'Time':
|
|
127
|
-
case 'Datetime':
|
|
128
|
-
if (typeof value !== 'string' && !(value instanceof Date)) {
|
|
129
|
-
return `${fieldname}: expected date string or Date, got ${typeof value}`;
|
|
130
|
-
}
|
|
131
|
-
break;
|
|
132
|
-
case 'JSON':
|
|
133
|
-
if (typeof value !== 'object') {
|
|
134
|
-
return `${fieldname}: expected object, got ${typeof value}`;
|
|
135
|
-
}
|
|
136
|
-
break;
|
|
137
|
-
}
|
|
138
|
-
return null;
|
|
139
|
-
}
|
|
140
|
-
/**
|
|
141
|
-
* No-op handler for testing or placeholder actions
|
|
142
|
-
*/
|
|
143
|
-
const noop = async () => {
|
|
144
|
-
return { ok: true };
|
|
145
|
-
};
|
|
146
|
-
/**
|
|
147
|
-
* Built-in handlers available for registration
|
|
148
|
-
* @public
|
|
149
|
-
*/
|
|
150
|
-
export const builtinHandlers = {
|
|
151
|
-
validateRequiredFields,
|
|
152
|
-
validateFieldTypes,
|
|
153
|
-
noop,
|
|
154
|
-
};
|
|
155
|
-
/**
|
|
156
|
-
* Register all built-in handlers
|
|
157
|
-
* @public
|
|
158
|
-
*/
|
|
159
|
-
export function registerBuiltinHandlers() {
|
|
160
|
-
for (const [name, handler] of Object.entries(builtinHandlers)) {
|
|
161
|
-
registerHandler(name, handler);
|
|
162
|
-
}
|
|
163
|
-
}
|
|
@@ -1,161 +0,0 @@
|
|
|
1
|
-
import { readFileSync, readdirSync, statSync } from 'node:fs';
|
|
2
|
-
import { join } from 'node:path';
|
|
3
|
-
import { parseDoctype, validateDoctype } from '@stonecrop/schema';
|
|
4
|
-
const doctypeRegistry = new Map();
|
|
5
|
-
/**
|
|
6
|
-
* Error thrown when a doctype definition fails validation
|
|
7
|
-
* @public
|
|
8
|
-
*/
|
|
9
|
-
export class DoctypeValidationError extends Error {
|
|
10
|
-
file;
|
|
11
|
-
errors;
|
|
12
|
-
constructor(
|
|
13
|
-
/** File path or name where the validation error occurred */
|
|
14
|
-
file,
|
|
15
|
-
/** List of validation errors found */
|
|
16
|
-
errors) {
|
|
17
|
-
const errorMessages = errors.map(e => ` ${e.path.join('.')}: ${e.message}`).join('\n');
|
|
18
|
-
super(`Invalid doctype definition in ${file}:\n${errorMessages}`);
|
|
19
|
-
this.file = file;
|
|
20
|
-
this.errors = errors;
|
|
21
|
-
this.name = 'DoctypeValidationError';
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
/**
|
|
25
|
-
* Load doctype definitions from a directory of JSON files
|
|
26
|
-
* @param dir - Directory path containing doctype JSON files
|
|
27
|
-
* @param options - Options for loading doctypes (continueOnError, onError callback)
|
|
28
|
-
* @public
|
|
29
|
-
*/
|
|
30
|
-
export function loadDoctypes(dir, options = {}) {
|
|
31
|
-
const entries = readdirSync(dir);
|
|
32
|
-
for (const entry of entries) {
|
|
33
|
-
const fullPath = join(dir, entry);
|
|
34
|
-
const stat = statSync(fullPath);
|
|
35
|
-
if (stat.isDirectory()) {
|
|
36
|
-
loadDoctypes(fullPath, options);
|
|
37
|
-
}
|
|
38
|
-
else if (entry.endsWith('.json')) {
|
|
39
|
-
loadDoctypeFile(fullPath, options);
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
/**
|
|
44
|
-
* Load and validate a single doctype file
|
|
45
|
-
*/
|
|
46
|
-
function loadDoctypeFile(filePath, options) {
|
|
47
|
-
const content = readFileSync(filePath, 'utf-8');
|
|
48
|
-
let data;
|
|
49
|
-
try {
|
|
50
|
-
data = JSON.parse(content);
|
|
51
|
-
}
|
|
52
|
-
catch (err) {
|
|
53
|
-
const parseError = {
|
|
54
|
-
path: [],
|
|
55
|
-
message: `Invalid JSON: ${err instanceof Error ? err.message : String(err)}`,
|
|
56
|
-
};
|
|
57
|
-
if (options.continueOnError) {
|
|
58
|
-
options.onError?.(filePath, [parseError]);
|
|
59
|
-
return;
|
|
60
|
-
}
|
|
61
|
-
throw new DoctypeValidationError(filePath, [parseError]);
|
|
62
|
-
}
|
|
63
|
-
const result = validateDoctype(data);
|
|
64
|
-
if (!result.success) {
|
|
65
|
-
if (options.continueOnError) {
|
|
66
|
-
options.onError?.(filePath, result.errors);
|
|
67
|
-
return;
|
|
68
|
-
}
|
|
69
|
-
throw new DoctypeValidationError(filePath, result.errors);
|
|
70
|
-
}
|
|
71
|
-
const doctype = parseDoctype(data);
|
|
72
|
-
doctypeRegistry.set(doctype.name, doctype);
|
|
73
|
-
}
|
|
74
|
-
/**
|
|
75
|
-
* Load doctypes from an object (for programmatic use)
|
|
76
|
-
* @param doctypes - Object mapping doctype names to doctype definitions
|
|
77
|
-
* @param options - Options for loading doctypes (continueOnError, onError callback)
|
|
78
|
-
* @public
|
|
79
|
-
*/
|
|
80
|
-
export function loadDoctypesFromObject(doctypes, options = {}) {
|
|
81
|
-
for (const [name, data] of Object.entries(doctypes)) {
|
|
82
|
-
const withName = { ...data, name };
|
|
83
|
-
const result = validateDoctype(withName);
|
|
84
|
-
if (!result.success) {
|
|
85
|
-
if (options.continueOnError) {
|
|
86
|
-
options.onError?.(name, result.errors);
|
|
87
|
-
continue;
|
|
88
|
-
}
|
|
89
|
-
throw new DoctypeValidationError(name, result.errors);
|
|
90
|
-
}
|
|
91
|
-
const doctype = parseDoctype(withName);
|
|
92
|
-
doctypeRegistry.set(doctype.name, doctype);
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
/**
|
|
96
|
-
* Get a doctype by name
|
|
97
|
-
* @param name - Name of the doctype to retrieve
|
|
98
|
-
* @public
|
|
99
|
-
*/
|
|
100
|
-
export function getMeta(name) {
|
|
101
|
-
const direct = doctypeRegistry.get(name);
|
|
102
|
-
if (direct)
|
|
103
|
-
return direct;
|
|
104
|
-
// Fallback: find by slug (links reference doctypes by slug, not name)
|
|
105
|
-
for (const doctype of doctypeRegistry.values()) {
|
|
106
|
-
if (doctype.slug === name)
|
|
107
|
-
return doctype;
|
|
108
|
-
}
|
|
109
|
-
return undefined;
|
|
110
|
-
}
|
|
111
|
-
/**
|
|
112
|
-
* Get all loaded doctypes
|
|
113
|
-
* @public
|
|
114
|
-
*/
|
|
115
|
-
export function getAllMeta() {
|
|
116
|
-
return Array.from(doctypeRegistry.values());
|
|
117
|
-
}
|
|
118
|
-
/**
|
|
119
|
-
* Check if a doctype is registered
|
|
120
|
-
* @param name - Name of the doctype to check
|
|
121
|
-
* @public
|
|
122
|
-
*/
|
|
123
|
-
export function hasMeta(name) {
|
|
124
|
-
return doctypeRegistry.has(name);
|
|
125
|
-
}
|
|
126
|
-
/**
|
|
127
|
-
* Clear all registered doctypes
|
|
128
|
-
* @public
|
|
129
|
-
*/
|
|
130
|
-
export function clearRegistry() {
|
|
131
|
-
doctypeRegistry.clear();
|
|
132
|
-
}
|
|
133
|
-
/**
|
|
134
|
-
* Validate cross-doctype references (Link fields, inherits, etc.)
|
|
135
|
-
* Call after all doctypes are loaded.
|
|
136
|
-
* @public
|
|
137
|
-
*/
|
|
138
|
-
export function validateReferences() {
|
|
139
|
-
const errors = [];
|
|
140
|
-
for (const doctype of doctypeRegistry.values()) {
|
|
141
|
-
// Check inherits reference
|
|
142
|
-
if (doctype.inherits && !doctypeRegistry.has(doctype.inherits)) {
|
|
143
|
-
errors.push({
|
|
144
|
-
path: [doctype.name, 'inherits'],
|
|
145
|
-
message: `References unknown doctype: ${doctype.inherits}`,
|
|
146
|
-
});
|
|
147
|
-
}
|
|
148
|
-
// Check Link field targets
|
|
149
|
-
for (const field of doctype.fields) {
|
|
150
|
-
if (field.fieldtype === 'Link' && typeof field.options === 'string') {
|
|
151
|
-
if (!doctypeRegistry.has(field.options)) {
|
|
152
|
-
errors.push({
|
|
153
|
-
path: [doctype.name, 'fields', field.fieldname, 'options'],
|
|
154
|
-
message: `Link references unknown doctype: ${field.options}`,
|
|
155
|
-
});
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
return errors;
|
|
161
|
-
}
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import type { ActionHandler } from '../types';
|
|
2
|
-
/**
|
|
3
|
-
* Register an action handler
|
|
4
|
-
* @param name - Unique name for the action handler
|
|
5
|
-
* @param handler - Action handler function to register
|
|
6
|
-
* @public
|
|
7
|
-
*/
|
|
8
|
-
export declare function registerHandler(name: string, handler: ActionHandler): void;
|
|
9
|
-
/**
|
|
10
|
-
* Get a registered handler by name
|
|
11
|
-
* @param name - Name of the action handler to retrieve
|
|
12
|
-
* @public
|
|
13
|
-
*/
|
|
14
|
-
export declare function getHandler(name: string): ActionHandler | undefined;
|
|
15
|
-
/**
|
|
16
|
-
* Check if a handler is registered
|
|
17
|
-
* @param name - Name of the action handler to check
|
|
18
|
-
* @public
|
|
19
|
-
*/
|
|
20
|
-
export declare function hasHandler(name: string): boolean;
|
|
21
|
-
/**
|
|
22
|
-
* Clear all registered handlers
|
|
23
|
-
* @public
|
|
24
|
-
*/
|
|
25
|
-
export declare function clearHandlers(): void;
|
|
26
|
-
/**
|
|
27
|
-
* Built-in handlers available for registration
|
|
28
|
-
* @public
|
|
29
|
-
*/
|
|
30
|
-
export declare const builtinHandlers: Record<string, ActionHandler>;
|
|
31
|
-
/**
|
|
32
|
-
* Register all built-in handlers
|
|
33
|
-
* @public
|
|
34
|
-
*/
|
|
35
|
-
export declare function registerBuiltinHandlers(): void;
|
|
36
|
-
//# sourceMappingURL=actions.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"actions.d.ts","sourceRoot":"","sources":["../../../src/registry/actions.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,UAAU,CAAA;AAI7C;;;;;GAKG;AACH,wBAAgB,eAAe,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,aAAa,GAAG,IAAI,CAE1E;AAED;;;;GAIG;AACH,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,aAAa,GAAG,SAAS,CAElE;AAED;;;;GAIG;AACH,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAEhD;AAED;;;GAGG;AACH,wBAAgB,aAAa,IAAI,IAAI,CAEpC;AA2ID;;;GAGG;AACH,eAAO,MAAM,eAAe,EAAE,MAAM,CAAC,MAAM,EAAE,aAAa,CAIzD,CAAA;AAED;;;GAGG;AACH,wBAAgB,uBAAuB,IAAI,IAAI,CAI9C"}
|
|
@@ -1,169 +0,0 @@
|
|
|
1
|
-
const handlerRegistry = new Map();
|
|
2
|
-
/**
|
|
3
|
-
* Register an action handler
|
|
4
|
-
* @param name - Unique name for the action handler
|
|
5
|
-
* @param handler - Action handler function to register
|
|
6
|
-
* @public
|
|
7
|
-
*/
|
|
8
|
-
export function registerHandler(name, handler) {
|
|
9
|
-
handlerRegistry.set(name, handler);
|
|
10
|
-
}
|
|
11
|
-
/**
|
|
12
|
-
* Get a registered handler by name
|
|
13
|
-
* @param name - Name of the action handler to retrieve
|
|
14
|
-
* @public
|
|
15
|
-
*/
|
|
16
|
-
export function getHandler(name) {
|
|
17
|
-
return handlerRegistry.get(name);
|
|
18
|
-
}
|
|
19
|
-
/**
|
|
20
|
-
* Check if a handler is registered
|
|
21
|
-
* @param name - Name of the action handler to check
|
|
22
|
-
* @public
|
|
23
|
-
*/
|
|
24
|
-
export function hasHandler(name) {
|
|
25
|
-
return handlerRegistry.has(name);
|
|
26
|
-
}
|
|
27
|
-
/**
|
|
28
|
-
* Clear all registered handlers
|
|
29
|
-
* @public
|
|
30
|
-
*/
|
|
31
|
-
export function clearHandlers() {
|
|
32
|
-
handlerRegistry.clear();
|
|
33
|
-
}
|
|
34
|
-
// =============================================================================
|
|
35
|
-
// Built-in Handlers
|
|
36
|
-
// =============================================================================
|
|
37
|
-
/**
|
|
38
|
-
* Validate that all required fields are present in a record
|
|
39
|
-
*/
|
|
40
|
-
const validateRequiredFields = async (args, context) => {
|
|
41
|
-
// oxlint-disable-next-line typescript/no-unsafe-type-assertion -- ActionHandler args are runtime-typed by GraphQL; first arg is always the record
|
|
42
|
-
const [record] = args;
|
|
43
|
-
const { doctype } = context;
|
|
44
|
-
const missing = [];
|
|
45
|
-
for (const field of doctype.fields) {
|
|
46
|
-
if (field.kind === 'field' &&
|
|
47
|
-
field.required &&
|
|
48
|
-
(record[field.fieldname] === undefined || record[field.fieldname] === null)) {
|
|
49
|
-
missing.push(field.label ?? field.fieldname);
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
if (missing.length > 0) {
|
|
53
|
-
throw new Error(`Missing required fields: ${missing.join(', ')}`);
|
|
54
|
-
}
|
|
55
|
-
return { valid: true };
|
|
56
|
-
};
|
|
57
|
-
/**
|
|
58
|
-
* Validate field types match expected types
|
|
59
|
-
*/
|
|
60
|
-
const validateFieldTypes = async (args, context) => {
|
|
61
|
-
// oxlint-disable-next-line typescript/no-unsafe-type-assertion -- ActionHandler args are runtime-typed by GraphQL; first arg is always the record
|
|
62
|
-
const [record] = args;
|
|
63
|
-
const { doctype } = context;
|
|
64
|
-
const errors = [];
|
|
65
|
-
for (const field of doctype.fields) {
|
|
66
|
-
if (field.kind !== 'field')
|
|
67
|
-
continue;
|
|
68
|
-
const value = record[field.fieldname];
|
|
69
|
-
if (value === undefined || value === null)
|
|
70
|
-
continue;
|
|
71
|
-
const error = validateFieldValue(field, value);
|
|
72
|
-
if (error)
|
|
73
|
-
errors.push(error);
|
|
74
|
-
}
|
|
75
|
-
// Validate link fields
|
|
76
|
-
if (doctype.links) {
|
|
77
|
-
for (const [fieldname, link] of Object.entries(doctype.links)) {
|
|
78
|
-
const value = record[fieldname];
|
|
79
|
-
if (value === undefined || value === null)
|
|
80
|
-
continue;
|
|
81
|
-
const isMany = link.cardinality === 'noneOrMany' || link.cardinality === 'atLeastOne';
|
|
82
|
-
if (isMany) {
|
|
83
|
-
if (!Array.isArray(value)) {
|
|
84
|
-
errors.push(`${fieldname}: expected array, got ${typeof value}`);
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
else {
|
|
88
|
-
if (typeof value !== 'object' || Array.isArray(value)) {
|
|
89
|
-
errors.push(`${fieldname}: expected object, got ${typeof value}`);
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
if (errors.length > 0) {
|
|
95
|
-
throw new Error(`Field type validation failed:\n${errors.join('\n')}`);
|
|
96
|
-
}
|
|
97
|
-
return { valid: true };
|
|
98
|
-
};
|
|
99
|
-
/**
|
|
100
|
-
* Validate a single field value against its expected type
|
|
101
|
-
*/
|
|
102
|
-
function validateFieldValue(field, value) {
|
|
103
|
-
const { fieldname, fieldtype } = field;
|
|
104
|
-
switch (fieldtype) {
|
|
105
|
-
case 'Int':
|
|
106
|
-
if (typeof value !== 'number' || !Number.isInteger(value)) {
|
|
107
|
-
return `${fieldname}: expected integer, got ${typeof value}`;
|
|
108
|
-
}
|
|
109
|
-
break;
|
|
110
|
-
case 'Float':
|
|
111
|
-
case 'Decimal':
|
|
112
|
-
case 'Currency':
|
|
113
|
-
case 'Quantity':
|
|
114
|
-
if (typeof value !== 'number' && typeof value !== 'string') {
|
|
115
|
-
return `${fieldname}: expected number, got ${typeof value}`;
|
|
116
|
-
}
|
|
117
|
-
break;
|
|
118
|
-
case 'Check':
|
|
119
|
-
if (typeof value !== 'boolean') {
|
|
120
|
-
return `${fieldname}: expected boolean, got ${typeof value}`;
|
|
121
|
-
}
|
|
122
|
-
break;
|
|
123
|
-
case 'Data':
|
|
124
|
-
case 'Text':
|
|
125
|
-
case 'Code':
|
|
126
|
-
case 'Link':
|
|
127
|
-
if (typeof value !== 'string') {
|
|
128
|
-
return `${fieldname}: expected string, got ${typeof value}`;
|
|
129
|
-
}
|
|
130
|
-
break;
|
|
131
|
-
case 'Date':
|
|
132
|
-
case 'Time':
|
|
133
|
-
case 'Datetime':
|
|
134
|
-
if (typeof value !== 'string' && !(value instanceof Date)) {
|
|
135
|
-
return `${fieldname}: expected date string or Date, got ${typeof value}`;
|
|
136
|
-
}
|
|
137
|
-
break;
|
|
138
|
-
case 'JSON':
|
|
139
|
-
if (typeof value !== 'object') {
|
|
140
|
-
return `${fieldname}: expected object, got ${typeof value}`;
|
|
141
|
-
}
|
|
142
|
-
break;
|
|
143
|
-
}
|
|
144
|
-
return null;
|
|
145
|
-
}
|
|
146
|
-
/**
|
|
147
|
-
* No-op handler for testing or placeholder actions
|
|
148
|
-
*/
|
|
149
|
-
const noop = async () => {
|
|
150
|
-
return { ok: true };
|
|
151
|
-
};
|
|
152
|
-
/**
|
|
153
|
-
* Built-in handlers available for registration
|
|
154
|
-
* @public
|
|
155
|
-
*/
|
|
156
|
-
export const builtinHandlers = {
|
|
157
|
-
validateRequiredFields,
|
|
158
|
-
validateFieldTypes,
|
|
159
|
-
noop,
|
|
160
|
-
};
|
|
161
|
-
/**
|
|
162
|
-
* Register all built-in handlers
|
|
163
|
-
* @public
|
|
164
|
-
*/
|
|
165
|
-
export function registerBuiltinHandlers() {
|
|
166
|
-
for (const [name, handler] of Object.entries(builtinHandlers)) {
|
|
167
|
-
registerHandler(name, handler);
|
|
168
|
-
}
|
|
169
|
-
}
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import type { DoctypeMeta } from '@stonecrop/schema';
|
|
2
|
-
import type { WithPgClient } from 'postgraphile/@dataplan/pg';
|
|
3
|
-
/**
|
|
4
|
-
* Context passed to action handlers
|
|
5
|
-
* @public
|
|
6
|
-
*/
|
|
7
|
-
export interface ActionContext {
|
|
8
|
-
/** Doctype metadata for the action being executed */
|
|
9
|
-
doctype: DoctypeMeta;
|
|
10
|
-
/** Database access function; available when handler is called from a GraphQL mutation */
|
|
11
|
-
withPgClient?: WithPgClient;
|
|
12
|
-
/** Additional context properties */
|
|
13
|
-
[key: string]: unknown;
|
|
14
|
-
}
|
|
15
|
-
/**
|
|
16
|
-
* Action handler function signature
|
|
17
|
-
* @public
|
|
18
|
-
*/
|
|
19
|
-
export type ActionHandler = (args: unknown[], context: ActionContext) => Promise<unknown>;
|
|
20
|
-
//# sourceMappingURL=context.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../../../src/types/context.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAA;AACpD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAA;AAE7D;;;GAGG;AACH,MAAM,WAAW,aAAa;IAC7B,qDAAqD;IACrD,OAAO,EAAE,WAAW,CAAA;IACpB,yFAAyF;IACzF,YAAY,CAAC,EAAE,YAAY,CAAA;IAC3B,oCAAoC;IACpC,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAA;CACtB;AAED;;;GAGG;AACH,MAAM,MAAM,aAAa,GAAG,CAAC,IAAI,EAAE,OAAO,EAAE,EAAE,OAAO,EAAE,aAAa,KAAK,OAAO,CAAC,OAAO,CAAC,CAAA"}
|
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
import type { PgClient } from '@dataplan/pg';
|
|
2
|
-
import type { DoctypeMeta } from '@stonecrop/schema';
|
|
3
|
-
/**
|
|
4
|
-
* Context passed to action handlers.
|
|
5
|
-
*
|
|
6
|
-
* The open index signature is the extension point for injecting an
|
|
7
|
-
* application-owned data access layer (see ADR 0003) — in a PostGraphile
|
|
8
|
-
* setup that slot is {@link ActionContext.pgClient}; custom backends inject
|
|
9
|
-
* their own (e.g. an in-memory executor).
|
|
10
|
-
*
|
|
11
|
-
* @public
|
|
12
|
-
*/
|
|
13
|
-
export interface ActionContext {
|
|
14
|
-
/** Doctype metadata for the action being executed */
|
|
15
|
-
doctype: DoctypeMeta;
|
|
16
|
-
/**
|
|
17
|
-
* Active database client — available when the action is dispatched via stonecropAction.
|
|
18
|
-
*
|
|
19
|
-
* Rows returned by raw `pgClient.query()` carry **snake_case column names**,
|
|
20
|
-
* not the camelCase fieldnames the client expects (see ADR 0004 — the
|
|
21
|
-
* middleware's own read paths alias columns at the SQL layer, e.g.
|
|
22
|
-
* `"display_name" AS "displayName"`). Handlers querying with `pgClient`
|
|
23
|
-
* own the same conversion for their return values — see {@link ActionHandler}.
|
|
24
|
-
*/
|
|
25
|
-
pgClient?: PgClient;
|
|
26
|
-
/** Additional context properties */
|
|
27
|
-
[key: string]: unknown;
|
|
28
|
-
}
|
|
29
|
-
/**
|
|
30
|
-
* Action handler function signature.
|
|
31
|
-
*
|
|
32
|
-
* The resolved value is passed through verbatim as `ActionResult.data` —
|
|
33
|
-
* the middleware applies no transformation. Return **API-layer data**: plain
|
|
34
|
-
* objects keyed by camelCase fieldnames, matching what `stonecropRecord`
|
|
35
|
-
* returns (see ADR 0004).
|
|
36
|
-
*
|
|
37
|
-
* When reading from the database via `context.pgClient`, either alias columns
|
|
38
|
-
* in the SQL (`SELECT display_name AS "displayName" ...`, as the middleware's
|
|
39
|
-
* own queries do) or convert row keys with `snakeToCamel` from
|
|
40
|
-
* `@stonecrop/schema` before returning. Returning raw query rows leaks
|
|
41
|
-
* snake_case column names to the client.
|
|
42
|
-
*
|
|
43
|
-
* @public
|
|
44
|
-
*/
|
|
45
|
-
export type ActionHandler = (args: unknown[], context: ActionContext) => Promise<unknown>;
|
|
46
|
-
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/types/index.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAA;AAC5C,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAA;AAEpD;;;;;;;;;GASG;AACH,MAAM,WAAW,aAAa;IAC7B,qDAAqD;IACrD,OAAO,EAAE,WAAW,CAAA;IACpB;;;;;;;;OAQG;IACH,QAAQ,CAAC,EAAE,QAAQ,CAAA;IACnB,oCAAoC;IACpC,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAA;CACtB;AAED;;;;;;;;;;;;;;;GAeG;AACH,MAAM,MAAM,aAAa,GAAG,CAAC,IAAI,EAAE,OAAO,EAAE,EAAE,OAAO,EAAE,aAAa,KAAK,OAAO,CAAC,OAAO,CAAC,CAAA"}
|
package/dist/src/types/index.js
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Field name casing convention for generated GraphQL field names.
|
|
3
|
-
* @public
|
|
4
|
-
*/
|
|
5
|
-
export type FieldCasing = 'camel' | 'pascal';
|
|
6
|
-
/**
|
|
7
|
-
* Options for configuring a StonecropPreset.
|
|
8
|
-
* @public
|
|
9
|
-
*/
|
|
10
|
-
export interface StonecropPresetOptions {
|
|
11
|
-
/**
|
|
12
|
-
* Field name casing convention for generated GraphQL field names.
|
|
13
|
-
* `'camel'` (default) produces names like `createdAt`, `taskTitle`.
|
|
14
|
-
* `'pascal'` produces names like `CreatedAt`, `TaskTitle`.
|
|
15
|
-
*/
|
|
16
|
-
fieldCasing?: FieldCasing;
|
|
17
|
-
}
|
|
18
|
-
//# sourceMappingURL=preset.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"preset.d.ts","sourceRoot":"","sources":["../../../src/types/preset.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,MAAM,WAAW,GAAG,OAAO,GAAG,QAAQ,CAAA;AAE5C;;;GAGG;AACH,MAAM,WAAW,sBAAsB;IACtC;;;;OAIG;IACH,WAAW,CAAC,EAAE,WAAW,CAAA;CACzB"}
|