项目作者: RobinBuschmann

项目描述 :
SOAP decorators for creating wsdl's and annotating services to provide metadata for node-soap
高级语言: TypeScript
项目地址: git://github.com/RobinBuschmann/soap-typescript.git
创建时间: 2016-12-06T09:03:53Z
项目社区:https://github.com/RobinBuschmann/soap-typescript

开源协议:

下载


Build Status

soap-decorators

SOAP decorators for creating wsdl’s and annotating services to provide metadata for node-soap.

Installation

  1. npm install soap-decorators --save

Usage

Input and output messages

Define input and output message interfaces for a soap service.

  1. import {XSDComplexType, XSDElement} from 'soap-decorators';
  2. @XSDComplexType
  3. export class CalculatorInput {
  4. @XSDElement
  5. a: number;
  6. @XSDElement
  7. b: number;
  8. }
  9. @XSDComplexType
  10. export class CalculatorResult {
  11. @XSDElement
  12. value: number;
  13. }

For a more advanced usage of creating xsd schemas with decorators
see xsd-decorators.

Soap service and operations

Define soap service, its operations and specify input and output
messages via the previously defined classes.

  1. import {SoapService, SoapOperation} from 'soap-decorators';
  2. @SoapService({
  3. portName: 'CalculatorPort',
  4. serviceName: 'CalculatorService'
  5. })
  6. export class CalculatorController {
  7. @SoapOperation(CalculatorResult)
  8. add(data: CalculatorInput) {
  9. return {
  10. value: data.a + data.b
  11. };
  12. }
  13. @SoapOperation(CalculatorResult)
  14. subtract(data: CalculatorInput) {
  15. return Promise.resolve({
  16. value: data.a - data.b
  17. });
  18. }
  19. }

Use soap service with express.js

soap-decorators provides a middleware for express, which does
all the magic for you. The wsdl will be resolved and the location
address and tns will be set automatically.

  1. import {soap} from 'soap-decorators';
  2. const app = express();
  3. const calculatorController = new CalculatorController();
  4. // resolves wsdl for you and sets location address and tns to current requested url
  5. app.use('/soap/calculation', soap(calculatorController));

Requesting WSDL

Now you can ask for the wsdl by requesting against the defined
endpoint.

  1. GET /soap/calculation?wsdl

Response

  1. <?xml version='1.0' encoding='UTF-8'?>
  2. <wsdl:definitions xmlns:soap='http://schemas.xmlsoap.org/wsdl/soap/'
  3. xmlns:xsd='http://www.w3.org/2001/XMLSchema'
  4. xmlns:wsdl='http://schemas.xmlsoap.org/wsdl/'
  5. name='CalculatorService'
  6. targetNamespace='https://calculation.example.com'
  7. xmlns:tns='https://calculation.example.com'>
  8. <wsdl:types>
  9. <xsd:schema attributeFormDefault='unqualified' elementFormDefault='unqualified' xmlns:xsd='http://www.w3.org/2001/XMLSchema' targetNamespace='https://calculation.example.com'>
  10. <xsd:element name='add' type='tns:CalculatorInput'></xsd:element>
  11. <xsd:element name='CalculatorResult' type='tns:CalculatorResult'></xsd:element>
  12. <xsd:element name='subtract' type='tns:CalculatorInput'></xsd:element>
  13. <xsd:complexType name='CalculatorInput'>
  14. <xsd:sequence>
  15. <xsd:element name='a' type='xsd:int'></xsd:element>
  16. <xsd:element name='b' type='xsd:int'></xsd:element>
  17. </xsd:sequence>
  18. </xsd:complexType>
  19. <xsd:complexType name='CalculatorResult'>
  20. <xsd:sequence>
  21. <xsd:element name='value' type='xsd:int'></xsd:element>
  22. </xsd:sequence>
  23. </xsd:complexType>
  24. </xsd:schema>
  25. </wsdl:types>
  26. <wsdl:message name='addMessage'>
  27. <wsdl:part name='add' element='tns:add'></wsdl:part>
  28. </wsdl:message>
  29. <wsdl:message name='CalculatorResultMessage'>
  30. <wsdl:part name='CalculatorResult' element='tns:CalculatorResult'></wsdl:part>
  31. </wsdl:message>
  32. <wsdl:message name='subtractMessage'>
  33. <wsdl:part name='subtract' element='tns:subtract'></wsdl:part>
  34. </wsdl:message>
  35. <wsdl:portType name='CalculatorPortType'>
  36. <wsdl:operation name='add'>
  37. <wsdl:input message='tns:addMessage'></wsdl:input>
  38. <wsdl:output message='tns:CalculatorResultMessage'></wsdl:output>
  39. </wsdl:operation>
  40. <wsdl:operation name='subtract'>
  41. <wsdl:input message='tns:subtractMessage'></wsdl:input>
  42. <wsdl:output message='tns:CalculatorResultMessage'></wsdl:output>
  43. </wsdl:operation>
  44. </wsdl:portType>
  45. <wsdl:binding name='CalculatorServiceBinding' type='tns:CalculatorPortType'>
  46. <soap:binding style='document' transport='http://schemas.xmlsoap.org/soap/http'></soap:binding>
  47. <wsdl:operation name='add'>
  48. <wsdl:input>
  49. <soap:body use='literal' parts='add'></soap:body>
  50. </wsdl:input>
  51. <wsdl:output>
  52. <soap:body use='literal' parts='CalculatorResult'></soap:body>
  53. </wsdl:output>
  54. <soap:operation soapAction='add'></soap:operation>
  55. </wsdl:operation>
  56. <wsdl:operation name='subtract'>
  57. <wsdl:input>
  58. <soap:body use='literal' parts='subtract'></soap:body>
  59. </wsdl:input>
  60. <wsdl:output>
  61. <soap:body use='literal' parts='CalculatorResult'></soap:body>
  62. </wsdl:output>
  63. <soap:operation soapAction='subtract'></soap:operation>
  64. </wsdl:operation>
  65. </wsdl:binding>
  66. <wsdl:service name='CalculatorService'>
  67. <wsdl:port name='CalculatorPort' binding='tns:CalculatorServiceBinding'>
  68. <soap:address location='https://calculation.example.com/soap/v1'></soap:address>
  69. </wsdl:port>
  70. </wsdl:service>
  71. </wsdl:definitions>

Using operations

  1. POST /soap/calculation
  2. <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:cal="https://calculation.example.com">
  3. <soapenv:Header></soapenv:Header>
  4. <soapenv:Body>
  5. <cal:add>
  6. <a>3</a>
  7. <b>1</b>
  8. </cal:add>
  9. </soapenv:Body>
  10. </soapenv:Envelope>
  1. POST /soap/calculation
  2. <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:cal="https://calculation.example.com">
  3. <soapenv:Header></soapenv:Header>
  4. <soapenv:Body>
  5. <cal:subtract>
  6. <a>8</a>
  7. <b>4</b>
  8. </cal:subtract>
  9. </soapenv:Body>
  10. </soapenv:Envelope>

Response

  1. <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tns="http://localhost:3000/calculation">
  2. <soap:Body>
  3. <CalculatorResult>
  4. <value>4</value>
  5. </CalculatorResult>
  6. </soap:Body>
  7. </soap:Envelope>

Retrieving WSDL from class or instance

  1. import {createWsdl} from 'soap-decorators';
  2. const instance = new CalculatorController();
  3. createWsdl(instance) === createWsdl(CalculatorController);