UNPKG

@vizertech/nextjs-capi

Version:

NextJS 13 Wrapper for fb pixel and capi for graph v17.0 API

33 lines (32 loc) 1.53 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); function capiAPI({ endpoint = '', body = null }) { var _a; return __awaiter(this, void 0, void 0, function* () { const pixelId = (_a = process.env.NEXT_PUBLIC_PIXEL_ID) !== null && _a !== void 0 ? _a : ''; try { const raw = yield fetch(`https://graph.facebook.com/${process.env.NEXT_PUBLIC_GRAPH_VERSION}/${pixelId}/${endpoint}?access_token=${process.env.FB_ACCESS_TOKEN}`, { headers: { 'Content-Type': 'application/json' }, method: 'POST', body: JSON.stringify(body) }); let res = yield raw.json(); return res; } catch (error) { throw error; } }); } exports.default = capiAPI;