@htdangkhoa/google-ads
Version:
Google Ads API client for Node.js
66 lines (65 loc) • 2.38 kB
JavaScript
import { promisify } from 'util';
import { Metadata } from '@grpc/grpc-js';
import deepmerge from 'deepmerge';
import { Service } from './Service.js';
export class GoogleAds extends 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 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.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 = 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 = promisify(fn);
return caller(req, meta);
}
}