moleculer-web icon indicating copy to clipboard operation
moleculer-web copied to clipboard

What is the benefit of using ctx.meta.$location & ctx.meta.$responseType vs just setting via ctx.meta.$responseHeaders?

Open valeeum opened this issue 10 months ago • 4 comments

I'm just curious why ctx.meta.$location & ctx.meta.$responseType options are offered as opposed to setting "Location" and "Content-Type" header via ctx.meta.$responseHeaders?

valeeum avatar Apr 10 '24 21:04 valeeum

Nothing special just you can write the same shortly.

icebob avatar Apr 21 '24 11:04 icebob

But it's not just there for convenience since it can cause error if not set:

// Redirect
			if (res.statusCode==201 || (res.statusCode >= 300 && res.statusCode < 400 && res.statusCode !== 304)) {
				const location = ctx.meta.$location;
				/* istanbul ignore next */
				if (!location) {
					this.logger.warn(`The 'ctx.meta.$location' is missing for status code '${res.statusCode}'!`);
				} else {
					res.setHeader("Location", location);
				}
			}

valeeum avatar Apr 21 '24 15:04 valeeum

I suggest we use the following code:

const location = ctx.meta.$location ?? ctx.meta.$responseHeaders?.['Location'];

valeeum avatar Apr 21 '24 15:04 valeeum

Good catch

icebob avatar Apr 21 '24 17:04 icebob