@dmgt/google-ad-manager-api
Version:
Typed Google Ad Manager API
112 lines (102 loc) • 3.89 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const mkdirp_1 = require("mkdirp");
const promises_1 = require("node:fs/promises");
const wsdl_path_1 = require("./wsdl-path");
const node_path_1 = require("node:path");
generateAPIs().then(generateIndex).catch(console.error);
async function generateAPIs() {
const versions = await (0, promises_1.readdir)('src/wsdl');
for (const version of versions) {
const services = (await (0, promises_1.readdir)(`src/wsdl/${version}`)).map((wsdl) => wsdl.replace(/\.wsdl$/, ''));
const template = /* ts */ `
import { Credentials } from 'google-auth-library'
import { BearerSecurity, Client, createClientAsync } from 'soap'
${mapJoin(services, (service) => `import { createClientAsync as create${service}Client } from '../service/${version}/${service.toLowerCase()}'`)}
${mapJoin(services, (service) => `export * as ${service} from '../service/${version}/${service.toLowerCase()}'`)}
export interface GoogleAdManagerOptions {
applicationName: string
authorize(): Promise<Credentials>
networkCode: number | string
}
export class GoogleAdManager {
get version() { return this.
constructor(options: GoogleAdManagerOptions) {
this.
this.
this.
}
get credentials() {
if (!this.
return this.
}
authorize() {
this.
return this.
}
${mapJoin(services, (service) => `create${service}Client = this.
get
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.#version}\`,
'xmlns:xsi': 'http://www.w3.org/2001/XMLSchema-instance',
'xmlns:soapenv': 'http://schemas.xmlsoap.org/soap/envelope/',
},
'ns1:networkCode': this.
'ns1:applicationName': this.
},
}
}
createClient: (
...args: Parameters<typeof createClientAsync>
) => Promise<C>,
wsdlPath: string
) {
return async (): Promise<C> => {
const [token, client] = await Promise.all([
this.credentials,
createClient(wsdlPath, {
ignoredNamespaces: {
namespaces: ['tns'],
},
}),
])
if (!token.access_token) {
console.error(token)
throw new Error('Failed to authenticate with Google')
}
client.addSoapHeader(this.
client.setSecurity(new BearerSecurity(token.access_token))
return client
}
}
}
`;
await (0, mkdirp_1.mkdirp)('src/api');
await (0, promises_1.writeFile)(`src/api/${version}.ts`, template);
}
}
async function generateIndex() {
const apis = await (0, promises_1.readdir)('src/api');
const latestAPI = apis[apis.length - 1];
const filePath = 'src/index.ts';
await (0, promises_1.writeFile)(filePath,
/* ts */ `export * from './query'
export * from './report/runReport'
export * from './api/${(0, node_path_1.basename)(latestAPI, (0, node_path_1.extname)(latestAPI))}'
`);
}
function mapJoin(arr, map) {
return arr.reduce((output, item) => output + map(item) + '\n', '');
}
//# sourceMappingURL=api.js.map