WSDL TypeScript Client Generator

1.7.1 · active · verified Tue Apr 21

wsdl-tsclient is a utility for generating TypeScript SOAP clients and type definitions from WSDL (Web Services Description Language) files. It serves as a bridge between legacy SOAP services and modern TypeScript applications, offering compile-time safety and improved developer experience when interacting with SOAP APIs. The current stable version is 1.7.1. It maintains a consistent release cadence with minor versions introducing new features and bug fixes, indicated by recent releases like 1.7.0 and 1.7.1. Key differentiators include its reliance on `ts-morph` for robust code generation and `node-soap` for runtime client functionality, drawing inspiration from Java's `wsimport` and `openapi-generator`. This tool is particularly useful for projects needing to consume SOAP services with strong typing, without the complexity of manual type definition. It focuses purely on client and type generation, unlike some alternatives that offer OpenAPI conversion or REST gateway scaffolding.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to programmatically generate a TypeScript SOAP client from a WSDL file and then use the generated client to make a call to a (mocked) SOAP service. It includes setup for a dummy WSDL and output directory.

import { generateClient } from 'wsdl-tsclient';
import { createClientAsync } from './generated/MyService'; // This path will vary based on your WSDL
import * as path from 'path';
import * as fs from 'fs';

async function runGenerationAndClient() {
  const wsdlPath = path.resolve(__dirname, 'resources', 'MyService.wsdl');
  const outputPath = path.resolve(__dirname, 'generated');

  // Ensure output directory exists
  if (!fs.existsSync(outputPath)) {
    fs.mkdirSync(outputPath, { recursive: true });
  }

  // Placeholder WSDL content for demonstration
  // In a real scenario, you'd have a physical WSDL file.
  const dummyWsdlContent = `<?xml version="1.0" encoding="UTF-8"?>
<definitions name="MyService" targetNamespace="http://www.example.org/MyService/" xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/">
  <types>
    <xsd:schema targetNamespace="http://www.example.org/MyService/">
      <xsd:element name="SayHelloRequest">
        <xsd:complexType>
          <xsd:sequence>
            <xsd:element name="name" type="xsd:string"/>
          </xsd:sequence>
        </xsd:complexType>
      </xsd:element>
      <xsd:element name="SayHelloResponse">
        <xsd:complexType>
          <xsd:sequence>
            <xsd:element name="greeting" type="xsd:string"/>
          </xsd:sequence>
        </xsd:complexType>
      </xsd:element>
    </xsd:schema>
  </types>
  <message name="SayHelloRequest">
    <part name="parameters" element="tns:SayHelloRequest"/>
  </message>
  <message name="SayHelloResponse">
    <part name="parameters" element="tns:SayHelloResponse"/>
  </message>
  <portType name="MyService">
    <operation name="SayHello">
      <input message="tns:SayHelloRequest"/>
      <output message="tns:SayHelloResponse"/>
    </operation>
  </portType>
  <binding name="MyServiceSOAP" type="tns:MyService">
    <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
    <operation name="SayHello">
      <soap:operation soapAction="http://www.example.org/MyService/SayHello"/>
      <input>
        <soap:body use="literal"/>
      </input>
      <output>
        <soap:body use="literal"/>
      </output>
    </operation>
  </binding>
  <service name="MyService">
    <port name="MyServiceSOAP" binding="tns:MyServiceSOAP">
      <soap:address location="http://localhost:8000/MyService"/>
    </port>
  </service>
</definitions>`;

  // Save dummy WSDL for generation
  const resourcesDir = path.resolve(__dirname, 'resources');
  if (!fs.existsSync(resourcesDir)) {
    fs.mkdirSync(resourcesDir, { recursive: true });
  }
  fs.writeFileSync(wsdlPath, dummyWsdlContent);

  console.log(`Generating client from ${wsdlPath} to ${outputPath}...`);
  await generateClient(wsdlPath, outputPath);
  console.log('Client generated successfully!');

  // Example of using the generated client (requires 'soap' package to be installed)
  // In a real scenario, the 'soap' client would be configured to hit a live endpoint.
  try {
    const client = await createClientAsync(wsdlPath);
    // Assuming the generated client has a 'MyService' service and 'MyServiceSOAP' port
    const result = await client.MyService.MyServiceSOAP.SayHelloAsync({ name: 'World' });
    console.log('SOAP Call Result:', result[0].greeting); // result[0] typically holds the response body
  } catch (error) {
    console.error('Error using generated client (ensure soap is installed and service is running):', error.message);
  }
}

runGenerationAndClient().catch(console.error);

view raw JSON →