amqp-client.js icon indicating copy to clipboard operation
amqp-client.js copied to clipboard

'TextEncoder' refers to a value, but is being used as a type here

Open michael-rivera-florencehc-com opened this issue 9 months ago • 3 comments

Getting the following error when compiling in Typescript:

node_modules/@cloudamqp/amqp-client/types/amqp-base-client.d.ts:27:27 - error TS2749: 'TextEncoder' refers to a value, but is being used as a type here. Did you mean 'typeof TextEncoder'?

27 readonly textEncoder: TextEncoder;

This is version - 3.1.1

If I comment out line 27 in node_modules/@cloudamqp/amqp-client/types/amqp-base-client.d.ts

everything works.

What version are you using? Can you show the code you are trying to run?

dentarg avatar Oct 25 '23 13:10 dentarg

Typescript - 5.2.2 NodeJS - v20.8.0 This occurs when compiling the code running the tsc command

Here is the tsconfig

{
    "compilerOptions": {
      "lib": ["es2020"],
      "module": "node16",
      "esModuleInterop": true,
      "target": "ES2020",
      "moduleResolution": "node16",
      "resolveJsonModule": true,
      "sourceMap": true,
      "checkJs": false,
      "outDir": "dist",
      "types": ["node"]
    },
    "include": ["./**/*.ts"],
    "exclude": ["node_modules", "dist"]
  }

Example Code

Client Service:

import { AMQPChannel, AMQPClient, AMQPConsumer, AMQPQueue } from '@cloudamqp/amqp-client';


export class AmqpClientService {

  static #Instance: AmqpClientService;
  #client: AMQPClient;
  #channel: AMQPChannel;
  #connection;
  #queue: AMQPQueue;
  #consumer: AMQPConsumer;  

  private constructor(client, connection,  channel, queue){
    this.#client = client;
    this.#channel = channel;
    this.#queue = queue;
    this.#connection = connection;
  }

  public static async init(uri: string) {
    const client = new AMQPClient(uri);
    const conn = await client.connect();
    const ch = await conn.channel();
    const q = await ch.queue();
    return this.#Instance = new AmqpClientService(client, conn, ch, q);
  }

  async subscribe() {
    try {
      this.#consumer = await this.#queue.subscribe({noAck: true}, async (msg) => {
        console.log(msg.bodyToString())
      })
      await this.#consumer.wait();
      
    } catch (e) {
      console.error('ERROR', e)
      e.connection.close()
      setTimeout(this.subscribe, 5000) // will try to reconnect in 1s
    }
  }

  async publish(message) {
    try {
      await this.#queue.publish(message, {deliveryMode: 2});  
    } catch(e) {
      console.error('ERROR', e)
      e.connection.close()
      setTimeout(this.publish, 5000, message)
    }
  }

  async close(){
    await this.#consumer.cancel();
    await this.#client.close();
    await this.#connection.close();
  }
}

bootstrap

import { AmqpClientService } from '../shared/services/amqp-client-service.js'
import { ConfigurationService } from '../shared/services/configuration-service.js';

const location = 'QAUS'
const configuration = new ConfigurationService(
  [
    'AMQP_SERVICE_URI',
  ],
  `.env.${location}`
);

const {
  AMQP_SERVICE_URI,
} = configuration.environmentVariables;

const amqpClientService = await AmqpClientService.init(AMQP_SERVICE_URI);

amqpClientService.subscribe();

while(true){
  amqpClientService.publish(`This is a test ${makeId(10)}`);
  await wait(10000);
}

async function wait(time) {
  return  await new Promise(resolve => setTimeout(resolve, time));
}

function makeId(length) {
  let result = '';
  const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  const charactersLength = characters.length;
  let counter = 0;
  while (counter < length) {
    result += characters.charAt(Math.floor(Math.random() * charactersLength));
    counter += 1;
  }
  return result;
}

Anyone?