@kinevolution/appwrite-functions-shared-utils 0.1.5 → 0.1.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/dist/appwrite.d.ts +79 -0
- package/dist/utils.d.ts +39 -6
- package/dist/utils.js +4 -6
- package/package.json +3 -3
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import type { Models } from 'appwrite';
|
|
2
|
+
|
|
3
|
+
// This file is auto-generated by the Appwrite CLI.
|
|
4
|
+
// You can regenerate it by running `appwrite types -l ts types`.
|
|
5
|
+
|
|
6
|
+
export enum Role {
|
|
7
|
+
REQUESTER = 'requester',
|
|
8
|
+
COMPANION = 'companion',
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export enum Activities {
|
|
12
|
+
CHAT = 'chat',
|
|
13
|
+
WALK = 'walk',
|
|
14
|
+
GAMES = 'games',
|
|
15
|
+
HOME_HELP = 'home_help',
|
|
16
|
+
READING = 'reading',
|
|
17
|
+
SHOPPING = 'shopping',
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export enum Sex {
|
|
21
|
+
FEMALE = 'female',
|
|
22
|
+
MALE = 'male',
|
|
23
|
+
PREFER_NOT_SAY = 'prefer_not_say',
|
|
24
|
+
OTHER = 'other',
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export type ProfilesRequester = Models.Row & {
|
|
28
|
+
phone: string;
|
|
29
|
+
contactByPhone: boolean;
|
|
30
|
+
approxLocationLabel: string;
|
|
31
|
+
homeLocation: string;
|
|
32
|
+
notesForCompanions: string | null;
|
|
33
|
+
emergencyContactName: string | null;
|
|
34
|
+
emergencyContactPhone: string | null;
|
|
35
|
+
user: Roles;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
export type ProfilesCompanion = Models.Row & {
|
|
39
|
+
hasVehicle: boolean;
|
|
40
|
+
ratingCount: number;
|
|
41
|
+
ratingAvg: number;
|
|
42
|
+
user: Roles;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
export type CompanionZones = Models.Row & {
|
|
46
|
+
label: string;
|
|
47
|
+
priority: number;
|
|
48
|
+
active: boolean;
|
|
49
|
+
notes: string | null;
|
|
50
|
+
zone: string | null;
|
|
51
|
+
profilesCompanion: ProfilesCompanion;
|
|
52
|
+
center: string | null;
|
|
53
|
+
radius: number | null;
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
export type Roles = Models.Row & {
|
|
57
|
+
userId: string;
|
|
58
|
+
role: Role;
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
export type ProfilesExtras = Models.Row & {
|
|
62
|
+
user: Roles;
|
|
63
|
+
bio: string | null;
|
|
64
|
+
photoIds: string[] | null;
|
|
65
|
+
activities: Activities[] | null;
|
|
66
|
+
locale: string | null;
|
|
67
|
+
timezone: string | null;
|
|
68
|
+
marketingOptIn: boolean;
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
export type ProfilesCore = Models.Row & {
|
|
72
|
+
pushTokens: string[] | null;
|
|
73
|
+
termsAcceptedAt: string;
|
|
74
|
+
birthDate: string;
|
|
75
|
+
firstName: string;
|
|
76
|
+
lastName: string;
|
|
77
|
+
sex: Sex;
|
|
78
|
+
user: Roles;
|
|
79
|
+
};
|
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
|
-
|
|
13
|
+
type FieldDefinition = {
|
|
14
14
|
key: string;
|
|
15
15
|
type: 'string' | 'number' | 'boolean' | 'object' | 'array';
|
|
16
|
-
required:
|
|
17
|
-
}
|
|
18
|
-
|
|
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?:
|
|
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,
|
|
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.
|
|
6
|
+
"version": "0.1.7",
|
|
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": "
|
|
23
|
+
"build": "tsc -p tsconfig.json && powershell -Command \"Copy-Item appwrite.d.ts dist/appwrite.d.ts\"",
|
|
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": "
|
|
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",
|