Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import * as Sentry from '@sentry/node';
- import { AppwriteException, Client } from 'node-appwrite';
- import { Context, Request, Response } from './types';
- export function cloudWrapper(callback: (req: Request, res: Response, data: any, client?: Client) => any) {
- return async ({ req, res, log, error }: Context) => {
- Sentry.init({
- dsn: '',
- tracesSampleRate: 0.2,
- });
- try {
- Sentry.setTag('function.id', process.env.APPWRITE_FUNCTION_ID);
- Sentry.setTag('function.name', process.env.APPWRITE_FUNCTION_NAME);
- Sentry.setUser({ id: process.env.APPWRITE_FUNCTION_USER_ID || null });
- const DEBUG = process.env.DEBUG?.toLowerCase() === 'true';
- let data;
- try {
- data = req.body;
- if (DEBUG) log({ preJSON: data });
- data = JSON.parse(data);
- } catch (e) {}
- Sentry.setExtras({
- event: process.env.APPWRITE_FUNCTION_EVENT,
- trigger: process.env.APPWRITE_FUNCTION_TRIGGER,
- data: data,
- });
- if (DEBUG) log({ postJSON: data });
- let client: Client;
- if (process.env.APPWRITE_ENDPOINT && process.env.APPWRITE_FUNCTION_PROJECT_ID && process.env.APPWRITE_API_KEY) {
- client = new Client();
- client
- .setEndpoint(process.env.APPWRITE_ENDPOINT)
- .setProject(process.env.APPWRITE_FUNCTION_PROJECT_ID)
- .setKey(process.env.APPWRITE_API_KEY);
- }
- const response = await callback(req, res, data, client);
- const responseObject = { data: response || null, message: null, status: 200 };
- if (DEBUG) log(responseObject);
- return res.json(responseObject);
- } catch (e) {
- Sentry.captureException(e);
- const responseObject = { data: null, message: null, status: 500 };
- if (e instanceof AppwriteException) {
- responseObject.message = e.message;
- responseObject.status = e.code;
- } else if (e instanceof Error) {
- responseObject.message = e.message;
- } else if (typeof e === 'string') {
- responseObject.message = e;
- } else {
- responseObject.message = 'An unknown error occurred';
- }
- error(e);
- return res.json(responseObject, responseObject.status);
- }
- };
- }
- import { User } from '@appwrite/common';
- import { cloudWrapper, Request, Response } from '@mono/cloud/wrapper';
- import { Client } from 'node-appwrite';
- export default cloudWrapper(async (req: Request, res: Response, data: User, client: Client) => {
- console.log(req, res, data, client);
- console.log(process.env);
- console.log(req.body, req.body?.test, data);
- return { test: 'test' };
- });
Advertisement
Add Comment
Please, Sign In to add comment