UNPKG

@bonniernews/bn-google-ad-manager-api

Version:

This repo is a fork of https://github.com/thewizarodofoz/google-ad-manager-api to update dependencies, functionality and add typing for the different gam services.

68 lines 2.96 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.GAMClient = void 0; const soap_1 = require("soap"); const utils_1 = require("./utils"); class GAMClient { apiVersion = undefined; networkCode = ""; accessToken = ""; async getLatestApiVersion() { const response = await fetch("https://ads.google.com/apis/ads/publisher"); const html = await response.text(); return html.match(/v\d+/g).at(-1); } async authorize({ networkCode, apiVersion, accessToken }) { // TODO: check array of supported versions if (apiVersion && !apiVersion.match(/v\d+/)) throw "Api Version not supported"; this.networkCode = networkCode; this.apiVersion = apiVersion || await this.getLatestApiVersion(); this.accessToken = accessToken; } async getService(serviceName, token) { if (!this.networkCode) throw "Client not authorized"; const client = await (0, soap_1.createClientAsync)(`https://ads.google.com/apis/ads/publisher/${this.apiVersion}/${serviceName}?wsdl`); client.addSoapHeader(this.getSoapHeaders()); client.setSecurity(new soap_1.BearerSecurity(token ?? this.accessToken)); return new Proxy(client, { get: (target, propertyKey) => { const method = propertyKey.toString(); if (target.hasOwnProperty(method) && !['setToken'].includes(method)) { return async function run(dto = {}) { let res; if (client[method + "Async"]) { res = await client[method + "Async"](dto); } else { res = await (0, utils_1.promiseFromCallback)((cb) => client[method](dto, cb)); } return Array.isArray(res) ? !res[0] ? new Error("Got no rval from GAM API, check if values sent are correct") : res[0].rval : res.rval; }; } else { return target[method]; } } }); } getSoapHeaders() { return { RequestHeader: { attributes: { 'soapenv:actor': "http://schemas.xmlsoap.org/soap/actor/next", 'soapenv:mustUnderstand': 0, 'xsi:type': "ns1:SoapRequestHeader", 'xmlns:ns1': "https://www.google.com/apis/ads/publisher/" + this.apiVersion, 'xmlns:xsi': "http://www.w3.org/2001/XMLSchema-instance", 'xmlns:soapenv': "http://schemas.xmlsoap.org/soap/envelope/" }, 'ns1:networkCode': this.networkCode, 'ns1:applicationName': 'content-api' } }; } } exports.GAMClient = GAMClient; //# sourceMappingURL=GAMClient.js.map