Skip to content

Commit

Permalink
feat: replace "Async" suffix hack with promiseFromCallback hack
Browse files Browse the repository at this point in the history
I know I could have used Bluebird's util function... but where's the fun in that?
  • Loading branch information
thewizarodofoz committed Sep 22, 2019
1 parent 889db37 commit 7484129
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/dfp.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { BearerSecurity, Client, createClientAsync } from 'soap';
import { BearerSecurity, Client, createClient } from 'soap';
import { promiseFromCallback } from "./utils";

export type DFPOptions = {
networkCode: string;
Expand All @@ -20,7 +21,7 @@ export class DFP {
public async getService(service: string): Promise<DFPClient> {
const {apiVersion} = this.options;
const serviceUrl = `https://ads.google.com/apis/ads/publisher/${apiVersion}/${service}?wsdl`;
const client = await createClientAsync(serviceUrl);
const client = await promiseFromCallback((cb) => createClient(serviceUrl, cb));

client.addSoapHeader(this.getSoapHeaders());

Expand All @@ -33,8 +34,7 @@ export class DFP {
const method = propertyKey.toString();
if (target.hasOwnProperty(method) && !['setToken'].includes(method)) {
return async function run(dto: any) {
// @ts-ignore
const res = await client[method + 'Async'](dto);
const res = await promiseFromCallback((cb) => client[method](dto, cb));
return DFP.parse(res);
};
} else {
Expand All @@ -44,8 +44,8 @@ export class DFP {
}) as DFPClient;
}

public static parse(res: any[]) {
return res[0].rval;
public static parse(res: any) {
return res.rval;
}

private getSoapHeaders() {
Expand Down
12 changes: 12 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export function promiseFromCallback (fn: (callback: (err: Error, result: any) => void) => void): Promise<any> {
return new Promise((resolve, reject) => {
fn((err, result) => {
if (err) {
reject(err);
return;
}

resolve(result);
});
});
}

0 comments on commit 7484129

Please sign in to comment.