UNPKG

@htdangkhoa/google-ads

Version:
73 lines (72 loc) 2.74 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.GoogleAds = void 0; const util_1 = require("util"); const grpc_js_1 = require("@grpc/grpc-js"); const deepmerge_1 = __importDefault(require("deepmerge")); const Service_js_1 = require("./Service.js"); class GoogleAds extends Service_js_1.Service { constructor(options, customer = {}) { super(options); this.customerOptions = customer; } setCustomerId(customerId) { this.customerOptions.customer_id = customerId; return this; } setLoginCustomerId(loginCustomerId) { this.customerOptions.login_customer_id = loginCustomerId; return this; } setLinkedCustomerId(linkedCustomerId) { this.customerOptions.linked_customer_id = linkedCustomerId; return this; } get callMetadata() { const meta = new grpc_js_1.Metadata(); meta.set('developer-token', this.options.developer_token); if (!this.customerOptions.customer_id) throw new Error('Missing customer ID'); if (this.customerOptions.login_customer_id) { meta.set('login-customer-id', this.customerOptions.login_customer_id); } if (this.customerOptions.linked_customer_id) { meta.set('linked-customer-id', this.customerOptions.linked_customer_id); } return meta; } transformRequest(request, metadata) { const req = (deepmerge_1.default.all([ { customer_id: this.customerOptions.customer_id }, request, ])); const meta = this.callMetadata; if (metadata) { meta.merge(metadata); } return [req, meta]; } search(request, metadata) { const client = this.loadService('GoogleAdsServiceClient'); const [req, meta] = this.transformRequest(request, metadata); const fn = client.search.bind(client); const caller = (0, util_1.promisify)(fn); return caller(req, meta); } searchStream(request, metadata) { const client = this.loadService('GoogleAdsServiceClient'); const [req, meta] = this.transformRequest(request, metadata); return client.searchStream(req, meta); } mutate(request, metadata) { const client = this.loadService('GoogleAdsServiceClient'); const [req, meta] = this.transformRequest(request, metadata); const fn = client.mutate.bind(client); const caller = (0, util_1.promisify)(fn); return caller(req, meta); } } exports.GoogleAds = GoogleAds;