Skip to content

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

@skylord123

Description

@skylord123

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions