@kinevolution/appwrite-functions-shared-utils 0.1.5 → 0.1.6

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/dist/utils.d.ts CHANGED
@@ -8,16 +8,41 @@ export interface Config {
8
8
  * @param req - The Appwrite function request object
9
9
  * @returns An object containing the validation result
10
10
  */
11
- export declare function validateInput(config: Config, req: any): ValidationResult;
11
+ export declare function validateInput<T>(config: Config, req: any): ValidationResult<T>;
12
12
  type Method = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
13
- interface FieldDefinition {
13
+ type FieldDefinition = {
14
14
  key: string;
15
15
  type: 'string' | 'number' | 'boolean' | 'object' | 'array';
16
- required: boolean;
17
- }
18
- interface ValidationResult {
16
+ required: true;
17
+ } | {
18
+ key: string;
19
+ type: 'string';
20
+ required: false;
21
+ default: string;
22
+ } | {
23
+ key: string;
24
+ type: 'number';
25
+ required: false;
26
+ default: number;
27
+ } | {
28
+ key: string;
29
+ type: 'boolean';
30
+ required: false;
31
+ default: boolean;
32
+ } | {
33
+ key: string;
34
+ type: 'object';
35
+ required: false;
36
+ default: Record<string, any>;
37
+ } | {
38
+ key: string;
39
+ type: 'array';
40
+ required: false;
41
+ default: any[];
42
+ };
43
+ interface ValidationResult<T> {
19
44
  valid: boolean;
20
- data?: any;
45
+ data?: T;
21
46
  error?: string;
22
47
  }
23
48
  export interface AppwriteContext {
@@ -26,4 +51,12 @@ export interface AppwriteContext {
26
51
  log: (message: string) => void;
27
52
  error: (message: string) => void;
28
53
  }
54
+ type TsFromFieldType<T extends FieldDefinition['type']> = T extends 'string' ? string : T extends 'number' ? number : T extends 'boolean' ? boolean : T extends 'object' ? Record<string, unknown> : T extends 'array' ? unknown[] : unknown;
55
+ export type FieldsToType<Fs extends readonly FieldDefinition[]> = {
56
+ [K in Fs[number] as K extends {
57
+ key: string;
58
+ } ? K['key'] : never]: TsFromFieldType<K extends {
59
+ type: FieldDefinition['type'];
60
+ } ? K['type'] : never>;
61
+ };
29
62
  export {};
package/dist/utils.js CHANGED
@@ -13,9 +13,7 @@ export function validateInput(config, req) {
13
13
  };
14
14
  }
15
15
  // Check that the body exists (if this is a POST, PATCH or PUT request)
16
- if (config.method === 'POST' ||
17
- config.method === 'PUT' ||
18
- config.method === 'PATCH') {
16
+ if (config.method === 'POST' || config.method === 'PUT' || config.method === 'PATCH') {
19
17
  if (!req.bodyJson) {
20
18
  return {
21
19
  valid: false,
@@ -73,8 +71,9 @@ function validateRequestBody(bodyJson, fields) {
73
71
  errors.push(`Field "${field.key}" is required`);
74
72
  continue;
75
73
  }
76
- // If field is optional and absent, skip to next
74
+ // If field is optional and absent, use default value
77
75
  if (!field.required && (value === undefined || value === null)) {
76
+ validatedData[field.key] = field.default;
78
77
  continue;
79
78
  }
80
79
  // Check field type
@@ -90,8 +89,7 @@ function validateRequestBody(bodyJson, fields) {
90
89
  isValidType = typeof value === 'boolean';
91
90
  break;
92
91
  case 'object':
93
- isValidType =
94
- typeof value === 'object' && !Array.isArray(value) && value !== null;
92
+ isValidType = typeof value === 'object' && !Array.isArray(value) && value !== null;
95
93
  break;
96
94
  case 'array':
97
95
  isValidType = Array.isArray(value);
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "0.1.5",
6
+ "version": "0.1.6",
7
7
  "license": "ISC",
8
8
  "author": "Nicolas Forêt <nicolas4@gmail.com>",
9
9
  "description": "Shared utilities for Appwrite functions",
@@ -20,13 +20,13 @@
20
20
  ],
21
21
  "scripts": {
22
22
  "bat-update": "cd .. && shared-update.bat",
23
- "build": "rmdir /S /Q dist && tsc -p tsconfig.json",
23
+ "build": "tsc -p tsconfig.json",
24
24
  "bump-version": "npm run bump-version:patch",
25
25
  "bump-version:major": "gulp bump-version --type=major",
26
26
  "bump-version:minor": "gulp bump-version --type=minor",
27
27
  "bump-version:patch": "gulp bump-version --type=patch",
28
28
  "deploy": "npm run build && npm run bump-version && npm publish --access public && npm run bat-update",
29
- "npm-update": "rmdir /S /Q node_modules && del package-lock.json && ncu -u && npm i"
29
+ "npm-update": "npx npkill && del package-lock.json && ncu -u && npm i"
30
30
  },
31
31
  "devDependencies": {
32
32
  "@types/node": "24.10.0",