lehh / nestjs-soap

Nestjs module wrapper for soap npm package
MIT License
21 stars 15 forks source link

forRootAsync takes an array but instantiates only the last client in the array #10

Closed Bishal18 closed 3 years ago

Bishal18 commented 3 years ago

@lehh

For the below code snippet: The client-2 is only instantiated

const getOptions = (jsonConfig: any, serviceId: string, subModule?: string): SoapModuleOptions => {
  //config returns a json of format 
  //"SAMPLE": {
  //    "SUB1": {
  //      "uri": "https://sub1.asmx?wsdl"
  //    },
  //    "SUB2": {
  //      "uri": "https://sub2.asmx?wsdl"
  //   }
  // }
  return {
    uri: jsonConfig[serviceId][subModule].uri,
  };
};

export const Soap = SoapModule.forRootAsync([
  { 
    name: 'CLIENT-1',
    inject: [JSON_CONFIG],
    useFactory: (jsonConfig: any) => getOptions(jsonConfig, 'SAMPLE', 'SUB1'),
  },
  {
    name: 'CLIENT-2,
    inject: [JSON_CONFIG],
    useFactory: (jsonConfig: any) => getOptions(jsonConfig, 'SAMPLE', 'SUB2'),
  },
]);

Also, FYI: in the main module the Soap is imported as such:

import { Soap } from '.../soap.config';
@Module({
  imports: [Soap],
  controllers: [],
  providers: []
    })
lehh commented 3 years ago

Hello @Bishal18.

Which version of nestjs-soap are you running on?

I've tried to reproduce it by your examples but I couldn't exactly reproduce it. If possible, could you provide a working repository with your problem?

I've tested forRootAsync with v1.2.2, using useFactory and some injected class and both clients were working fine. Example:

@Injectable()
export class Config {
  getJsonConfig(): Object {
    return {
      SAMPLE: {
        SUB1: {
          uri: 'https://sub1.asmx?wsdl',
        },
        SUB2: {
          uri: 'https://sub2.asmx?wsdl',
        },
      }
    };
  }
}

const getOptions = (jsonConfig: any, serviceId: string, subModule?: string): SoapModuleOptions => {
  return {
    uri: jsonConfig[serviceId][subModule].uri
  };
};

export const Soap: DynamicModule = SoapModule.forRootAsync([
  { 
    name: 'CLIENT-1',
    inject: [Config],
    useFactory: (config: Config) => getOptions(config.getJsonConfig(), 'SAMPLE', 'SUB1'),
  },
  {
    name: 'CLIENT-2',
    inject: [Config],
    useFactory: (config: Config) => getOptions(config.getJsonConfig(), 'SAMPLE', 'SUB2'),
  },
]);

Controller:

export class SoapExampleController {
  constructor(
    @Inject('CLIENT-1') private readonly client: Client,
    @Inject('CLIENT-2') private readonly client2: Client
  ) {}
}
Bishal18 commented 3 years ago

Hello @lehh , there's no problem with the DI and code compiles fine, but if a call is made to a soap function inside the loaded client , it doesn't even recognize it and throws this function is not present in the client. You can check with two valid uris. Only the client-2 function calls will work.

Also, fyi, am using the 1.2.2 ver.

P.S. Thanks for your efforts to support this legacy module in the NestJS world!

lehh commented 3 years ago

Oh, right. I tested with two identical endpoints, that's why it was working fine 🤦🏻 . All right, I'm going to fix it. Thanks for reporting!

Also, I will be soon releasing a v2 with some improvements and, I do hope, less bugs.

You're welcome :)).

Bishal18 commented 3 years ago

Haha, looking forward to it! :)