TimLuq / wsdl-to-ts

Generate TypeScript typings for WSDL services
ISC License
61 stars 48 forks source link

Support *Async methods for soap client #11

Open pauldraper opened 6 years ago

pauldraper commented 6 years ago

The soap library adds methods that return a promise. By default this are suffixed with Async, though that is customizable (in case it conflicts with a method).

E.g.

await output = client.LaunchTheMisslesAsync(input);
ReeganExE commented 4 years ago

I made an example at https://github.com/ReeganExE/node-soap-example/blob/master/src/index.ts

Generate types

npm run wsdl -- 'http://ws.cdyne.com/ip2geo/ip2geo.asmx?wsdl'
Output at src/IP2Geo/IP2GeoSoap.d.ts

will look like this:

export interface IIP2GeoSoapSoap extends Client {
    ResolveIP: (input: Partial<IResolveIPInput>, cb: (err: any | null, result: IResolveIPOutput, rawResult: string,  soapHeader: {[k: string]: any; },  rawRequest: string) => any, options?: any, extraHeaders?: any) => void;
    ResolveIPAsync: (input: Partial<IResolveIPInput>, options?: any, extraHeaders?: any) => Promise<[IResolveIPOutput, string, {[k: string]: any; }, string]>;
}

Test the output

import { createClientAsync, Client } from 'soap';
// import types from the generated source
import { IIP2GeoSoapSoap } from './IP2Geo/IP2GeoSoap';

function createSoapClientAs<T extends Client>(url: string): Promise<T> {
  return createClientAsync(url) as Promise<T>;
}

async function main() {
  const url = 'http://ws.cdyne.com/ip2geo/ip2geo.asmx?wsdl';
  const client = await createSoapClientAs<IIP2GeoSoapSoap>(url);

  try {
    const [result] = await client.ResolveIPAsync({ ipAddress: '1.1.1.1', licenseKey: '' });
    console.log(result.ResolveIPResult);
  } catch (error) {
    console.log(error);
  }
}

main();