@simtlix/simfinity-js 2.4.6 → 2.5.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.
Files changed (26) hide show
  1. package/.claude/worktrees/agitated-kepler/.claude/settings.local.json +23 -0
  2. package/.claude/worktrees/agitated-kepler/AGGREGATION_CHANGES_SUMMARY.md +235 -0
  3. package/.claude/worktrees/agitated-kepler/AGGREGATION_EXAMPLE.md +567 -0
  4. package/.claude/worktrees/agitated-kepler/LICENSE +201 -0
  5. package/.claude/worktrees/agitated-kepler/README.md +3941 -0
  6. package/.claude/worktrees/agitated-kepler/eslint.config.mjs +71 -0
  7. package/.claude/worktrees/agitated-kepler/package-lock.json +4740 -0
  8. package/.claude/worktrees/agitated-kepler/package.json +41 -0
  9. package/.claude/worktrees/agitated-kepler/src/auth/errors.js +44 -0
  10. package/.claude/worktrees/agitated-kepler/src/auth/expressions.js +273 -0
  11. package/.claude/worktrees/agitated-kepler/src/auth/index.js +391 -0
  12. package/.claude/worktrees/agitated-kepler/src/auth/rules.js +274 -0
  13. package/.claude/worktrees/agitated-kepler/src/const/QLOperator.js +39 -0
  14. package/.claude/worktrees/agitated-kepler/src/const/QLSort.js +28 -0
  15. package/.claude/worktrees/agitated-kepler/src/const/QLValue.js +39 -0
  16. package/.claude/worktrees/agitated-kepler/src/errors/internal-server.error.js +11 -0
  17. package/.claude/worktrees/agitated-kepler/src/errors/simfinity.error.js +15 -0
  18. package/.claude/worktrees/agitated-kepler/src/index.js +2412 -0
  19. package/.claude/worktrees/agitated-kepler/src/plugins.js +53 -0
  20. package/.claude/worktrees/agitated-kepler/src/scalars.js +188 -0
  21. package/.claude/worktrees/agitated-kepler/src/validators.js +250 -0
  22. package/.claude/worktrees/agitated-kepler/yarn.lock +1154 -0
  23. package/.cursor/rules/simfinity-core-functions.mdc +3 -1
  24. package/README.md +202 -0
  25. package/package.json +1 -1
  26. package/src/index.js +235 -21
@@ -0,0 +1,53 @@
1
+ import { createAuthPlugin } from './auth/index.js';
2
+
3
+ export { createAuthPlugin } from './auth/index.js';
4
+
5
+ /**
6
+ * Apollo Server plugin to add count to GraphQL response extensions
7
+ * @returns {Object} Apollo Server plugin
8
+ */
9
+ export const apolloCountPlugin = () => {
10
+ return {
11
+ async requestDidStart() {
12
+ return {
13
+ async willSendResponse({ contextValue, response }) {
14
+ if (response.body.kind === 'single' && contextValue?.count) {
15
+ response.body.singleResult.extensions = {
16
+ ...(response.body.singleResult.extensions || {}),
17
+ count: contextValue.count,
18
+ };
19
+ }
20
+ },
21
+ };
22
+ },
23
+ };
24
+ };
25
+
26
+ /**
27
+ * Envelop plugin to add count to GraphQL response extensions
28
+ * @returns {Object} Envelop plugin
29
+ */
30
+ export const envelopCountPlugin = () => {
31
+ return {
32
+ onExecute() {
33
+ return {
34
+ onExecuteDone({ result, args }) {
35
+ if (args.contextValue?.count) {
36
+ result.extensions = {
37
+ ...result.extensions,
38
+ count: args.contextValue.count,
39
+ };
40
+ }
41
+ },
42
+ };
43
+ },
44
+ };
45
+ };
46
+
47
+ const plugins = {
48
+ createAuthPlugin,
49
+ apolloCountPlugin,
50
+ envelopCountPlugin,
51
+ };
52
+
53
+ export default plugins;
@@ -0,0 +1,188 @@
1
+ import {
2
+ GraphQLString, GraphQLInt, GraphQLFloat,
3
+ } from 'graphql';
4
+ import { createValidatedScalar } from './index.js';
5
+
6
+ /**
7
+ * Email scalar - validates email format
8
+ * Type name: Email_String
9
+ */
10
+ export const EmailScalar = createValidatedScalar(
11
+ 'Email',
12
+ 'A valid email address',
13
+ GraphQLString,
14
+ (value) => {
15
+ const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
16
+ if (!emailRegex.test(value)) {
17
+ throw new Error('Invalid email format');
18
+ }
19
+ },
20
+ );
21
+
22
+ /**
23
+ * URL scalar - validates URL format
24
+ * Type name: URL_String
25
+ */
26
+ export const URLScalar = createValidatedScalar(
27
+ 'URL',
28
+ 'A valid URL',
29
+ GraphQLString,
30
+ (value) => {
31
+ try {
32
+ new URL(value);
33
+ } catch {
34
+ throw new Error('Invalid URL format');
35
+ }
36
+ },
37
+ );
38
+
39
+ /**
40
+ * PositiveInt scalar - validates positive integers
41
+ * Type name: PositiveInt_Int
42
+ */
43
+ export const PositiveIntScalar = createValidatedScalar(
44
+ 'PositiveInt',
45
+ 'A positive integer',
46
+ GraphQLInt,
47
+ (value) => {
48
+ if (value <= 0) {
49
+ throw new Error('Value must be positive');
50
+ }
51
+ },
52
+ );
53
+
54
+ /**
55
+ * PositiveFloat scalar - validates positive floats
56
+ * Type name: PositiveFloat_Float
57
+ */
58
+ export const PositiveFloatScalar = createValidatedScalar(
59
+ 'PositiveFloat',
60
+ 'A positive float',
61
+ GraphQLFloat,
62
+ (value) => {
63
+ if (value <= 0) {
64
+ throw new Error('Value must be positive');
65
+ }
66
+ },
67
+ );
68
+
69
+ /**
70
+ * Factory function to create a bounded string scalar
71
+ * @param {string} name - Name for the scalar
72
+ * @param {number} min - Minimum length
73
+ * @param {number} max - Maximum length
74
+ * @returns {GraphQLScalarType} A scalar type with length validation
75
+ */
76
+ export const createBoundedStringScalar = (name, min, max) => {
77
+ return createValidatedScalar(
78
+ name,
79
+ `A string with length between ${min} and ${max} characters`,
80
+ GraphQLString,
81
+ (value) => {
82
+ if (typeof value !== 'string') {
83
+ throw new Error('Value must be a string');
84
+ }
85
+ if (min !== undefined && value.length < min) {
86
+ throw new Error(`String must be at least ${min} characters`);
87
+ }
88
+ if (max !== undefined && value.length > max) {
89
+ throw new Error(`String must be at most ${max} characters`);
90
+ }
91
+ },
92
+ );
93
+ };
94
+
95
+ /**
96
+ * Factory function to create a bounded integer scalar
97
+ * @param {string} name - Name for the scalar
98
+ * @param {number} min - Minimum value
99
+ * @param {number} max - Maximum value
100
+ * @returns {GraphQLScalarType} A scalar type with range validation
101
+ */
102
+ export const createBoundedIntScalar = (name, min, max) => {
103
+ return createValidatedScalar(
104
+ name,
105
+ `An integer between ${min} and ${max}`,
106
+ GraphQLInt,
107
+ (value) => {
108
+ if (typeof value !== 'number' || isNaN(value)) {
109
+ throw new Error('Value must be a number');
110
+ }
111
+ if (min !== undefined && value < min) {
112
+ throw new Error(`Value must be at least ${min}`);
113
+ }
114
+ if (max !== undefined && value > max) {
115
+ throw new Error(`Value must be at most ${max}`);
116
+ }
117
+ },
118
+ );
119
+ };
120
+
121
+ /**
122
+ * Factory function to create a bounded float scalar
123
+ * @param {string} name - Name for the scalar
124
+ * @param {number} min - Minimum value
125
+ * @param {number} max - Maximum value
126
+ * @returns {GraphQLScalarType} A scalar type with range validation
127
+ */
128
+ export const createBoundedFloatScalar = (name, min, max) => {
129
+ return createValidatedScalar(
130
+ name,
131
+ `A float between ${min} and ${max}`,
132
+ GraphQLFloat,
133
+ (value) => {
134
+ if (typeof value !== 'number' || isNaN(value)) {
135
+ throw new Error('Value must be a number');
136
+ }
137
+ if (min !== undefined && value < min) {
138
+ throw new Error(`Value must be at least ${min}`);
139
+ }
140
+ if (max !== undefined && value > max) {
141
+ throw new Error(`Value must be at most ${max}`);
142
+ }
143
+ },
144
+ );
145
+ };
146
+
147
+ /**
148
+ * Factory function to create a regex pattern string scalar
149
+ * @param {string} name - Name for the scalar
150
+ * @param {RegExp|string} pattern - Regex pattern to validate against
151
+ * @param {string} message - Error message if validation fails
152
+ * @returns {GraphQLScalarType} A scalar type with pattern validation
153
+ */
154
+ export const createPatternStringScalar = (name, pattern, message) => {
155
+ const regex = typeof pattern === 'string' ? new RegExp(pattern) : pattern;
156
+ const errorMessage = message || 'Value does not match required pattern';
157
+
158
+ return createValidatedScalar(
159
+ name,
160
+ `A string matching the pattern: ${pattern}`,
161
+ GraphQLString,
162
+ (value) => {
163
+ if (typeof value !== 'string') {
164
+ throw new Error('Value must be a string');
165
+ }
166
+ if (!regex.test(value)) {
167
+ throw new Error(errorMessage);
168
+ }
169
+ },
170
+ );
171
+ };
172
+
173
+ // Export all scalars as an object for convenience
174
+ const scalars = {
175
+ // Pre-built scalars
176
+ EmailScalar,
177
+ URLScalar,
178
+ PositiveIntScalar,
179
+ PositiveFloatScalar,
180
+ // Factory functions
181
+ createBoundedStringScalar,
182
+ createBoundedIntScalar,
183
+ createBoundedFloatScalar,
184
+ createPatternStringScalar,
185
+ };
186
+
187
+ export default scalars;
188
+
@@ -0,0 +1,250 @@
1
+ import SimfinityError from './errors/simfinity.error.js';
2
+
3
+ /**
4
+ * Creates a validation object that works for both 'save' (CREATE) and 'update' (UPDATE) operations.
5
+ * The validators will be applied to both operations.
6
+ * For CREATE operations, the value must be provided and valid.
7
+ * For UPDATE operations, undefined/null values are allowed (field might not be updated),
8
+ * but if a value is provided, it must be valid.
9
+ */
10
+ const createValidator = (validatorFn, required = false) => {
11
+ // Validator for CREATE operations - value is required if required=true
12
+ const validateCreate = async (typeName, fieldName, value, session) => {
13
+ if (required && (value === null || value === undefined)) {
14
+ throw new SimfinityError(`${fieldName} is required`, 'VALIDATION_ERROR', 400);
15
+ }
16
+ if (value !== null && value !== undefined) {
17
+ await validatorFn(typeName, fieldName, value, session);
18
+ }
19
+ };
20
+
21
+ // Validator for UPDATE operations - value is optional
22
+ const validateUpdate = async (typeName, fieldName, value, session) => {
23
+ // Skip validation if value is not provided (field is not being updated)
24
+ if (value === null || value === undefined) {
25
+ return;
26
+ }
27
+ // If value is provided, validate it
28
+ await validatorFn(typeName, fieldName, value, session);
29
+ };
30
+
31
+ const validatorCreate = { validate: validateCreate };
32
+ const validatorUpdate = { validate: validateUpdate };
33
+
34
+ // Return validations for both CREATE and UPDATE operations
35
+ // Also support 'save'/'update' for backward compatibility (though code uses CREATE/UPDATE)
36
+ return {
37
+ CREATE: [validatorCreate],
38
+ UPDATE: [validatorUpdate],
39
+ save: [validatorCreate], // For backward compatibility
40
+ update: [validatorUpdate], // For backward compatibility
41
+ };
42
+ };
43
+
44
+ /**
45
+ * String validators
46
+ */
47
+ export const stringLength = (name, min, max) => {
48
+ return createValidator(async (typeName, fieldName, value) => {
49
+ if (typeof value !== 'string') {
50
+ throw new SimfinityError(`${name} must be a string`, 'VALIDATION_ERROR', 400);
51
+ }
52
+
53
+ if (min !== undefined && value.length < min) {
54
+ throw new SimfinityError(`${name} must be at least ${min} characters`, 'VALIDATION_ERROR', 400);
55
+ }
56
+
57
+ if (max !== undefined && value.length > max) {
58
+ throw new SimfinityError(`${name} must be at most ${max} characters`, 'VALIDATION_ERROR', 400);
59
+ }
60
+ }, true); // Required for CREATE operations
61
+ };
62
+
63
+ export const maxLength = (name, max) => {
64
+ return createValidator(async (typeName, fieldName, value) => {
65
+ if (typeof value !== 'string') {
66
+ throw new SimfinityError(`${name} must be a string`, 'VALIDATION_ERROR', 400);
67
+ }
68
+
69
+ if (value.length > max) {
70
+ throw new SimfinityError(`${name} must be at most ${max} characters`, 'VALIDATION_ERROR', 400);
71
+ }
72
+ }, false); // Optional
73
+ };
74
+
75
+ export const pattern = (name, regex, message) => {
76
+ const regexObj = typeof regex === 'string' ? new RegExp(regex) : regex;
77
+ const errorMessage = message || `${name} format is invalid`;
78
+
79
+ return createValidator(async (typeName, fieldName, value) => {
80
+ if (typeof value !== 'string') {
81
+ throw new SimfinityError(`${name} must be a string`, 'VALIDATION_ERROR', 400);
82
+ }
83
+
84
+ if (!regexObj.test(value)) {
85
+ throw new SimfinityError(errorMessage, 'VALIDATION_ERROR', 400);
86
+ }
87
+ }, false); // Optional
88
+ };
89
+
90
+ export const email = () => {
91
+ const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
92
+
93
+ return createValidator(async (typeName, fieldName, value) => {
94
+ if (typeof value !== 'string') {
95
+ throw new SimfinityError('Email must be a string', 'VALIDATION_ERROR', 400);
96
+ }
97
+
98
+ if (!emailRegex.test(value)) {
99
+ throw new SimfinityError('Invalid email format', 'VALIDATION_ERROR', 400);
100
+ }
101
+ }, false); // Optional
102
+ };
103
+
104
+ export const url = () => {
105
+ return createValidator(async (typeName, fieldName, value) => {
106
+ if (typeof value !== 'string') {
107
+ throw new SimfinityError('URL must be a string', 'VALIDATION_ERROR', 400);
108
+ }
109
+
110
+ try {
111
+ // Use URL constructor for better validation
112
+ new URL(value);
113
+ } catch (e) {
114
+ console.log('Invalid URL format', e);
115
+ throw new SimfinityError('Invalid URL format', 'VALIDATION_ERROR', 400);
116
+ }
117
+ }, false); // Optional
118
+ };
119
+
120
+ /**
121
+ * Number validators
122
+ */
123
+ export const numberRange = (name, min, max) => {
124
+ return createValidator(async (typeName, fieldName, value) => {
125
+ if (typeof value !== 'number' || isNaN(value)) {
126
+ throw new SimfinityError(`${name} must be a number`, 'VALIDATION_ERROR', 400);
127
+ }
128
+
129
+ if (min !== undefined && value < min) {
130
+ throw new SimfinityError(`${name} must be at least ${min}`, 'VALIDATION_ERROR', 400);
131
+ }
132
+
133
+ if (max !== undefined && value > max) {
134
+ throw new SimfinityError(`${name} must be at most ${max}`, 'VALIDATION_ERROR', 400);
135
+ }
136
+ }, false); // Optional
137
+ };
138
+
139
+ export const positive = (name) => {
140
+ return createValidator(async (typeName, fieldName, value) => {
141
+ if (typeof value !== 'number' || isNaN(value)) {
142
+ throw new SimfinityError(`${name} must be a number`, 'VALIDATION_ERROR', 400);
143
+ }
144
+
145
+ if (value <= 0) {
146
+ throw new SimfinityError(`${name} must be positive`, 'VALIDATION_ERROR', 400);
147
+ }
148
+ }, false); // Optional
149
+ };
150
+
151
+ /**
152
+ * Array validators
153
+ */
154
+ export const arrayLength = (name, maxItems, itemValidator) => {
155
+ return createValidator(async (typeName, fieldName, value, session) => {
156
+ if (!Array.isArray(value)) {
157
+ throw new SimfinityError(`${name} must be an array`, 'VALIDATION_ERROR', 400);
158
+ }
159
+
160
+ if (maxItems !== undefined && value.length > maxItems) {
161
+ throw new SimfinityError(`${name} must have at most ${maxItems} items`, 'VALIDATION_ERROR', 400);
162
+ }
163
+
164
+ // If itemValidator is provided, validate each item
165
+ if (itemValidator && Array.isArray(itemValidator)) {
166
+ for (let i = 0; i < value.length; i++) {
167
+ for (const validator of itemValidator) {
168
+ await validator.validate(typeName, fieldName, value[i], session);
169
+ }
170
+ }
171
+ }
172
+ }, false); // Optional
173
+ };
174
+
175
+ /**
176
+ * Date validators
177
+ */
178
+ export const dateFormat = (name, format) => {
179
+ return createValidator(async (typeName, fieldName, value) => {
180
+ // Handle Date objects, ISO strings, and timestamps
181
+ let date;
182
+ if (value instanceof Date) {
183
+ date = value;
184
+ } else if (typeof value === 'string') {
185
+ date = new Date(value);
186
+ } else if (typeof value === 'number') {
187
+ date = new Date(value);
188
+ } else {
189
+ throw new SimfinityError(`${name} must be a valid date`, 'VALIDATION_ERROR', 400);
190
+ }
191
+
192
+ if (isNaN(date.getTime())) {
193
+ throw new SimfinityError(`${name} must be a valid date`, 'VALIDATION_ERROR', 400);
194
+ }
195
+
196
+ // If format is provided, validate format
197
+ if (format && typeof value === 'string') {
198
+ // Simple format validation - can be enhanced
199
+ const formatRegex = /^\d{4}-\d{2}-\d{2}$/; // YYYY-MM-DD
200
+ if (format === 'YYYY-MM-DD' && !formatRegex.test(value)) {
201
+ throw new SimfinityError(`${name} must be in format ${format}`, 'VALIDATION_ERROR', 400);
202
+ }
203
+ // Add more format patterns as needed
204
+ }
205
+ }, false); // Optional
206
+ };
207
+
208
+ export const futureDate = (name) => {
209
+ return createValidator(async (typeName, fieldName, value) => {
210
+ let date;
211
+ if (value instanceof Date) {
212
+ date = value;
213
+ } else if (typeof value === 'string') {
214
+ date = new Date(value);
215
+ } else if (typeof value === 'number') {
216
+ date = new Date(value);
217
+ } else {
218
+ throw new SimfinityError(`${name} must be a valid date`, 'VALIDATION_ERROR', 400);
219
+ }
220
+
221
+ if (isNaN(date.getTime())) {
222
+ throw new SimfinityError(`${name} must be a valid date`, 'VALIDATION_ERROR', 400);
223
+ }
224
+
225
+ if (date <= new Date()) {
226
+ throw new SimfinityError(`${name} must be a future date`, 'VALIDATION_ERROR', 400);
227
+ }
228
+ }, false); // Optional
229
+ };
230
+
231
+ // Export all validators as an object
232
+ const validators = {
233
+ // String validators
234
+ stringLength,
235
+ maxLength,
236
+ pattern,
237
+ email,
238
+ url,
239
+ // Number validators
240
+ numberRange,
241
+ positive,
242
+ // Array validators
243
+ arrayLength,
244
+ // Date validators
245
+ dateFormat,
246
+ futureDate,
247
+ };
248
+
249
+ export default validators;
250
+