Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow requesting basic object/array response instead of typed objects #657

Open
skylord123 opened this issue Nov 18, 2024 · 0 comments

Comments

@skylord123
Copy link

What is this feature about?
I manage the module node-red-contrib-gamedig for using GameDig with Node-RED. I noticed on the latest version of my module responses are not being passed through Node-RED correctly.

More specifically it looks like because GameDig now returns Results, Players, and Player objects it causes Node-RED to throw an error because NR doesn't know about these classes.

TypeError: Class constructor Players cannot be invoked without 'new'

This is because Node-RED takes data you are sending to nodes further down the chain and duplicates the data to prevent multiple flows from changing the same reference. When it tries to replicate this object it fails.

Because of this I now have to convert these classes to their Object / Array counterparts before sending it along. Example:

	GameDig.query(options)
		.then(function(state) {
			msg.payload = 'online';
			// GameDig returns Results, Players, and Player objects that we need to convert
			// to standard Array/Object instances so that Node-RED doesn't error
			msg.data = {
				...state,
				players: [ ...state.players.map(player => ({ ...player })) ],
				bots: [ ...state.bots.map(player => ({ ...player })) ],
				raw: {
					...state.raw,
					vanilla: {
						...state.raw.vanilla,
						players: [ ...state.raw.vanilla.players.map(player => ({ ...player })) ],
						bots: [ ...state.raw.vanilla.bots.map(player => ({ ...player })) ],
					}
				}
			};

			if (msg.payload === node.halt_if) {
				return null;
			}
			node.status({ fill: "green", shape: "dot", text: `Online ${state.players.length} players` });
			node.send(msg);
		})
		.catch(function(error) {
			msg.payload = 'offline';
			msg.data = {
				error,
				stack: error.stack,
			};
			if (msg.payload === node.halt_if) {
				return null;
			}
			node.status({ fill: "red", shape: "dot", text: "Offline" });
			node.send(msg);
			node.error(`GameDig Error: \n${error.stack}`);
			console.error(error);
		});

It would be nice if GameDig had a built in way to supply the state object using standard Array / Object classes instead of the Results, Players, and Player objects it currently uses. A flag that could be set or a function directly on GameDig would both seem like good solution to this.

Also I noticed the README.md page states that the players and bots fields in the query response is an array of objects when in reality it's a Players object that extends an array that contains Player objects.

Additional context/references
There may be an easier way to fix this. I tried using Object.assign({}, state); and it gives me an error as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant