Guest User

Untitled

a guest
Oct 3rd, 2023
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import * as Sentry from '@sentry/node';
  2. import { AppwriteException, Client } from 'node-appwrite';
  3. import { Context, Request, Response } from './types';
  4.  
  5. export function cloudWrapper(callback: (req: Request, res: Response, data: any, client?: Client) => any) {
  6.   return async ({ req, res, log, error }: Context) => {
  7.     Sentry.init({
  8.       dsn: '',
  9.       tracesSampleRate: 0.2,
  10.     });
  11.     try {
  12.       Sentry.setTag('function.id', process.env.APPWRITE_FUNCTION_ID);
  13.       Sentry.setTag('function.name', process.env.APPWRITE_FUNCTION_NAME);
  14.       Sentry.setUser({ id: process.env.APPWRITE_FUNCTION_USER_ID || null });
  15.  
  16.       const DEBUG = process.env.DEBUG?.toLowerCase() === 'true';
  17.  
  18.       let data;
  19.       try {
  20.         data = req.body;
  21.         if (DEBUG) log({ preJSON: data });
  22.         data = JSON.parse(data);
  23.       } catch (e) {}
  24.  
  25.       Sentry.setExtras({
  26.         event: process.env.APPWRITE_FUNCTION_EVENT,
  27.         trigger: process.env.APPWRITE_FUNCTION_TRIGGER,
  28.         data: data,
  29.       });
  30.  
  31.       if (DEBUG) log({ postJSON: data });
  32.  
  33.       let client: Client;
  34.       if (process.env.APPWRITE_ENDPOINT && process.env.APPWRITE_FUNCTION_PROJECT_ID && process.env.APPWRITE_API_KEY) {
  35.         client = new Client();
  36.         client
  37.           .setEndpoint(process.env.APPWRITE_ENDPOINT)
  38.           .setProject(process.env.APPWRITE_FUNCTION_PROJECT_ID)
  39.           .setKey(process.env.APPWRITE_API_KEY);
  40.       }
  41.  
  42.       const response = await callback(req, res, data, client);
  43.       const responseObject = { data: response || null, message: null, status: 200 };
  44.       if (DEBUG) log(responseObject);
  45.       return res.json(responseObject);
  46.     } catch (e) {
  47.       Sentry.captureException(e);
  48.       const responseObject = { data: null, message: null, status: 500 };
  49.  
  50.       if (e instanceof AppwriteException) {
  51.         responseObject.message = e.message;
  52.         responseObject.status = e.code;
  53.       } else if (e instanceof Error) {
  54.         responseObject.message = e.message;
  55.       } else if (typeof e === 'string') {
  56.         responseObject.message = e;
  57.       } else {
  58.         responseObject.message = 'An unknown error occurred';
  59.       }
  60.       error(e);
  61.       return res.json(responseObject, responseObject.status);
  62.     }
  63.   };
  64. }
  65.  
  66.  
  67.  
  68.  
  69.  
  70. import { User } from '@appwrite/common';
  71. import { cloudWrapper, Request, Response } from '@mono/cloud/wrapper';
  72. import { Client } from 'node-appwrite';
  73.  
  74. export default cloudWrapper(async (req: Request, res: Response, data: User, client: Client) => {
  75.   console.log(req, res, data, client);
  76.   console.log(process.env);
  77.   console.log(req.body, req.body?.test, data);
  78.  
  79.   return { test: 'test' };
  80. });
  81.  
Advertisement
Add Comment
Please, Sign In to add comment