@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 CHANGED
@@ -1,3 +1,5 @@
1
+ // This file is auto-generated by the Appwrite CLI.
2
+ // You can regenerate it by running `appwrite types -l ts appwrite.ts`.
1
3
  export var Role;
2
4
  (function (Role) {
3
5
  Role["REQUESTER"] = "requester";
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
- // Check that the body exists (if this is a POST, PATCH or PUT request)
25
- if (config.method === 'POST' || config.method === 'PUT' || config.method === 'PATCH') {
26
- if (!req.bodyJson) {
27
- return {
28
- valid: false,
29
- error: 'No body provided in the request',
30
- };
31
- }
32
- // Validate the request body
33
- const validation = validateRequestBody(req.bodyJson, config.fields);
34
- if (validation.valid) {
35
- return {
36
- valid: true,
37
- data: validation.data,
38
- };
39
- }
40
- else {
41
- return {
42
- valid: false,
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
+ };
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "0.1.14",
6
+ "version": "0.1.16",
7
7
  "license": "ISC",
8
8
  "author": "Nicolas Forêt <nicolas4@gmail.com>",
9
9
  "description": "Shared utilities for Appwrite functions",