@niur/google-admanager-api
Version:
Google Ad Manager API Client Library for NodeJs
58 lines • 2.52 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.GoogleSoapService = void 0;
const soap_1 = require("soap");
const constants_1 = require("../common/constants");
const utils_1 = require("../common/utils");
class GoogleSoapService {
constructor(service, options) {
this.service = service;
this.applicationName = options.applicationName;
this.networkCode = options.networkCode;
this.token = options.token;
}
async createClient() {
const serviceUrl = `https://ads.google.com/apis/ads/publisher/${constants_1.API_VERSION}/${this.service}?wsdl`;
const client = await (0, soap_1.createClientAsync)(serviceUrl);
client.addSoapHeader(this.getSoapHeaders());
client.setToken = function setToken(token) {
client.setSecurity(new soap_1.BearerSecurity(token));
};
if (this.token)
client.setToken(this.token);
this._client = new Proxy(client, {
get: function get(target, propertyKey) {
const method = propertyKey.toString();
if (target.hasOwnProperty(method) && !['setToken'].includes(method)) {
return async function run(dto = {}) {
const res = await (0, utils_1.promiseFromCallback)((cb) => client[method](dto, cb));
return (res === null || res === void 0 ? void 0 : res.rval) || null;
};
}
else {
return target[method];
}
},
});
const services = constants_1.SERVICE_MAP;
return new services[this.service](this._client);
}
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/' + constants_1.API_VERSION,
'xmlns:xsi': 'http://www.w3.org/2001/XMLSchema-instance',
'xmlns:soapenv': 'http://schemas.xmlsoap.org/soap/envelope/',
},
'ns1:networkCode': this.networkCode,
'ns1:applicationName': this.applicationName,
},
};
}
}
exports.GoogleSoapService = GoogleSoapService;
//# sourceMappingURL=googleSoap.service.js.map