@silenteer/natsu
Advanced tools
Comparing version 1.0.33 to 1.0.34
@@ -58,5 +58,12 @@ "use strict"; | ||
handlerLogService.error('Incoming message has no data'); | ||
respond({ | ||
yield respond({ | ||
message, | ||
data: responseCodec.encode(Object.assign(Object.assign({}, data), { body: undefined, code: 400 })), | ||
request: data, | ||
response: { | ||
headers: data === null || data === void 0 ? void 0 : data.headers, | ||
body: undefined, | ||
code: 400, | ||
}, | ||
injection, | ||
onResponse: handler.response, | ||
}); | ||
@@ -115,6 +122,13 @@ handlerLogService.info('End'); | ||
//#endregion | ||
respond({ | ||
yield respond({ | ||
message, | ||
data: responseCodec.encode(Object.assign(Object.assign({}, data), { headers: (handleResult === null || handleResult === void 0 ? void 0 : handleResult.headers) | ||
? Object.assign(Object.assign({}, data === null || data === void 0 ? void 0 : data.headers), handleResult === null || handleResult === void 0 ? void 0 : handleResult.headers) : data === null || data === void 0 ? void 0 : data.headers, code: (handleResult === null || handleResult === void 0 ? void 0 : handleResult.code) === 'OK' ? 200 : handleResult === null || handleResult === void 0 ? void 0 : handleResult.code, body: handleResult === null || handleResult === void 0 ? void 0 : handleResult.body })), | ||
request: data, | ||
response: { | ||
headers: (handleResult === null || handleResult === void 0 ? void 0 : handleResult.headers) | ||
? Object.assign(Object.assign({}, data === null || data === void 0 ? void 0 : data.headers), handleResult === null || handleResult === void 0 ? void 0 : handleResult.headers) : data === null || data === void 0 ? void 0 : data.headers, | ||
code: (handleResult === null || handleResult === void 0 ? void 0 : handleResult.code) === 'OK' ? 200 : handleResult === null || handleResult === void 0 ? void 0 : handleResult.code, | ||
body: handleResult === null || handleResult === void 0 ? void 0 : handleResult.body, | ||
}, | ||
injection, | ||
onResponse: handler.response, | ||
}); | ||
@@ -379,5 +393,11 @@ handlerLogService.info('End'); | ||
if (beforeResult.code !== 'OK') { | ||
respond({ | ||
yield respond({ | ||
message, | ||
data: responseCodec.encode(Object.assign(Object.assign({}, beforeResult.data), { code: beforeResult.code, body: beforeResult.errors })), | ||
request: beforeResult.data, | ||
response: { | ||
headers: beforeResult.data.headers, | ||
body: beforeResult.errors, | ||
code: beforeResult.code, | ||
}, | ||
injection, | ||
}); | ||
@@ -410,5 +430,11 @@ break; | ||
} | ||
respond({ | ||
yield respond({ | ||
message, | ||
data: responseCodec.encode(Object.assign(Object.assign({}, data), { code: handleResult.code, body: handleResult.errors })), | ||
request: data, | ||
response: { | ||
headers: data.headers, | ||
body: handleResult.errors, | ||
code: handleResult.code, | ||
}, | ||
injection, | ||
}); | ||
@@ -437,5 +463,11 @@ } | ||
if (afterResult.code !== 'OK') { | ||
respond({ | ||
yield respond({ | ||
message, | ||
data: responseCodec.encode(Object.assign(Object.assign({}, afterResult.data), { code: afterResult.code, body: afterResult.errors })), | ||
request: afterResult.data, | ||
response: { | ||
headers: afterResult.data.headers, | ||
body: afterResult.errors, | ||
code: afterResult.code, | ||
}, | ||
injection, | ||
}); | ||
@@ -466,5 +498,12 @@ break; | ||
} | ||
respond({ | ||
yield respond({ | ||
message, | ||
data: responseCodec.encode(Object.assign(Object.assign({}, data), { body: data === null || data === void 0 ? void 0 : data.body, code: 500 })), | ||
request: data, | ||
response: { | ||
headers: data === null || data === void 0 ? void 0 : data.headers, | ||
body: data === null || data === void 0 ? void 0 : data.body, | ||
code: 500, | ||
}, | ||
injection, | ||
onResponse: handler.response, | ||
}); | ||
@@ -474,6 +513,25 @@ }); | ||
function respond(params) { | ||
const { message, data } = params; | ||
if (message.reply) { | ||
return tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
const { message, request, response, injection, onResponse } = params; | ||
if (!message.reply) { | ||
return; | ||
} | ||
let data; | ||
if (onResponse && request) { | ||
data = yield onResponse({ | ||
request, | ||
response, | ||
codec: responseCodec, | ||
injection, | ||
}); | ||
} | ||
else { | ||
data = responseCodec.encode({ | ||
headers: response.headers, | ||
body: response.body, | ||
code: response.code, | ||
}); | ||
} | ||
message.respond(data); | ||
} | ||
}); | ||
} | ||
@@ -480,0 +538,0 @@ exports.default = { |
@@ -1,2 +0,2 @@ | ||
import type { Msg, NatsConnection, PublishOptions, RequestOptions } from 'nats'; | ||
import type { Msg, NatsConnection, PublishOptions, RequestOptions, Codec } from 'nats'; | ||
import type { NatsService, NatsRequest, NatsResponse } from '@silenteer/natsu-type'; | ||
@@ -134,3 +134,12 @@ type NatsInjection<TService extends NatsService<string, unknown, unknown>, TInjection extends Record<string, unknown> = Record<string, unknown>> = TInjection & { | ||
respondUnhandledError?: (data: NatsRequest<TService['request']>, error: Error, injection: NatsInjection<TService, TInjection>) => Promise<void>; | ||
response?: (params: { | ||
request: NatsRequest<TService['request']>; | ||
response: Pick<NatsResponse, 'headers' | 'code'> & { | ||
code: number; | ||
body: TService['response']; | ||
}; | ||
codec: Codec<NatsResponse>; | ||
injection: NatsInjection<TService, TInjection>; | ||
}) => Promise<Uint8Array>; | ||
}; | ||
export type { NatsInjection, NatsValidationInjection, NatsAuthorizationInjection, NatsHandleInjection, NatsMiddlewareBeforeInjection, NatsMiddlewareAfterInjection, NatsValidationResult, NatsAuthorizationResult, NatsHandleResult, NatsMiddlewareBeforeResult, NatsMiddlewareAfterResult, NatsBefore, NatsAfter, NatsValidate, NatsAuthorize, NatsHandle, NatsMiddleware, NatsHandler, }; |
{ | ||
"name": "@silenteer/natsu", | ||
"version": "1.0.33", | ||
"version": "1.0.34", | ||
"license": "MIT", | ||
@@ -5,0 +5,0 @@ "private": false, |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
56375
820