UNPKG

@dmgt/google-ad-manager-api

Version:
112 lines (102 loc) 3.89 kB
"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 { #applicationName: string #authorize: () => Promise<Credentials> #credentialsPromise!: Promise<Credentials> #networkCode: string #version = '${version}' get version() { return this.#version } constructor(options: GoogleAdManagerOptions) { this.#applicationName = options.applicationName this.#authorize = options.authorize this.#networkCode = options.networkCode.toString() } get credentials() { if (!this.#credentialsPromise) this.authorize() return this.#credentialsPromise } authorize() { this.#credentialsPromise = this.#authorize() return this.#credentialsPromise } ${mapJoin(services, (service) => `create${service}Client = this.#wrapClientCreator(create${service}Client, '${wsdl_path_1.baseURL}/'+this.#version+'/${service}?wsdl')`)} get #soapHeaders() { 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.#networkCode, 'ns1:applicationName': this.#applicationName, }, } } #wrapClientCreator<C extends Client>( 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.#soapHeaders) 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