@kinevolution/appwrite-functions-shared-utils 0.1.14 → 0.1.16
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.js +2 -0
- package/dist/utils.d.ts +8 -0
- package/dist/utils.js +22 -25
- package/package.json +1 -1
package/dist/appwrite.js
CHANGED
package/dist/utils.d.ts
CHANGED
|
@@ -68,4 +68,12 @@ export type FieldsToType<Fs extends readonly FieldDefinition[]> = {
|
|
|
68
68
|
type: FieldDefinition['type'];
|
|
69
69
|
} ? K['type'] : never>;
|
|
70
70
|
};
|
|
71
|
+
export type Response<T> = {
|
|
72
|
+
success: true;
|
|
73
|
+
data: T;
|
|
74
|
+
} | {
|
|
75
|
+
success: false;
|
|
76
|
+
error: string;
|
|
77
|
+
};
|
|
78
|
+
export declare const formatResponse: <T>(res: any, response: Response<T>) => any;
|
|
71
79
|
export {};
|
package/dist/utils.js
CHANGED
|
@@ -21,32 +21,26 @@ export function validateInput(config, req) {
|
|
|
21
21
|
error: `Invalid request method: expected ${config.method}, received ${req.method}`,
|
|
22
22
|
};
|
|
23
23
|
}
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
error: `Validation error: ${validation.error}`,
|
|
44
|
-
};
|
|
45
|
-
}
|
|
24
|
+
if (!req.bodyJson) {
|
|
25
|
+
return {
|
|
26
|
+
valid: false,
|
|
27
|
+
error: 'No body provided in the request',
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
// Validate the request body
|
|
31
|
+
const validation = validateRequestBody(req.bodyJson, config.fields);
|
|
32
|
+
if (validation.valid) {
|
|
33
|
+
return {
|
|
34
|
+
valid: true,
|
|
35
|
+
data: validation.data,
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
return {
|
|
40
|
+
valid: false,
|
|
41
|
+
error: `Validation error: ${validation.error}`,
|
|
42
|
+
};
|
|
46
43
|
}
|
|
47
|
-
return {
|
|
48
|
-
valid: true,
|
|
49
|
-
};
|
|
50
44
|
}
|
|
51
45
|
/**
|
|
52
46
|
* Validates and parses the JSON body of the request
|
|
@@ -122,3 +116,6 @@ function validateRequestBody(bodyJson, fields) {
|
|
|
122
116
|
data: validatedData,
|
|
123
117
|
};
|
|
124
118
|
}
|
|
119
|
+
export const formatResponse = (res, response) => {
|
|
120
|
+
return res.json(response, response.success ? 200 : 500);
|
|
121
|
+
};
|