@t-0/provider-sdk 1.1.27 → 1.1.29

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. package/README.md +33 -3
  2. package/lib/cjs/crypto/hash.d.ts +2 -0
  3. package/lib/cjs/crypto/hash.js +17 -0
  4. package/lib/cjs/crypto/index.d.ts +5 -0
  5. package/lib/cjs/crypto/index.js +14 -0
  6. package/lib/cjs/crypto/keys.d.ts +2 -0
  7. package/lib/cjs/crypto/keys.js +19 -0
  8. package/lib/cjs/crypto/request.d.ts +20 -0
  9. package/lib/cjs/crypto/request.js +49 -0
  10. package/lib/cjs/crypto/verify.d.ts +1 -0
  11. package/lib/cjs/crypto/verify.js +19 -0
  12. package/lib/cjs/index.d.ts +3 -0
  13. package/lib/cjs/index.js +8 -1
  14. package/lib/cjs/service/health.d.ts +1 -1
  15. package/lib/cjs/service/health.js +1 -5
  16. package/lib/cjs/service/health_pb.d.ts +157 -0
  17. package/lib/cjs/service/health_pb.js +90 -0
  18. package/lib/cjs/service/service.d.ts +12 -1
  19. package/lib/cjs/service/service.js +7 -17
  20. package/lib/cjs/service/validate.d.ts +25 -0
  21. package/lib/cjs/service/validate.js +42 -0
  22. package/lib/cjs/service/validate_response.d.ts +15 -1
  23. package/lib/cjs/service/validate_response.js +29 -2
  24. package/lib/cjs/version.d.ts +1 -1
  25. package/lib/cjs/version.js +1 -1
  26. package/lib/esm/crypto/hash.d.ts +2 -0
  27. package/lib/esm/crypto/hash.js +13 -0
  28. package/lib/esm/crypto/index.d.ts +5 -0
  29. package/lib/esm/crypto/index.js +4 -0
  30. package/lib/esm/crypto/keys.d.ts +2 -0
  31. package/lib/esm/crypto/keys.js +15 -0
  32. package/lib/esm/crypto/request.d.ts +20 -0
  33. package/lib/esm/crypto/request.js +45 -0
  34. package/lib/esm/crypto/verify.d.ts +1 -0
  35. package/lib/esm/crypto/verify.js +16 -0
  36. package/lib/esm/index.d.ts +3 -0
  37. package/lib/esm/index.js +3 -0
  38. package/lib/esm/service/health.d.ts +1 -1
  39. package/lib/esm/service/health.js +1 -5
  40. package/lib/esm/service/health_pb.d.ts +157 -0
  41. package/lib/esm/service/health_pb.js +87 -0
  42. package/lib/esm/service/service.d.ts +12 -1
  43. package/lib/esm/service/service.js +7 -17
  44. package/lib/esm/service/validate.d.ts +25 -0
  45. package/lib/esm/service/validate.js +38 -0
  46. package/lib/esm/service/validate_response.d.ts +15 -1
  47. package/lib/esm/service/validate_response.js +29 -2
  48. package/lib/esm/version.d.ts +1 -1
  49. package/lib/esm/version.js +1 -1
  50. package/lib/tsconfig.cjs.tsbuildinfo +1 -1
  51. package/lib/tsconfig.esm.tsbuildinfo +1 -1
  52. package/package.json +15 -6
@@ -0,0 +1,25 @@
1
+ import type { DescMessage, MessageShape } from "@bufbuild/protobuf";
2
+ /**
3
+ * Shared protovalidate validator instance for the public {@link validate} helper.
4
+ * Construction is cheap, but reusing one instance avoids repeated compilation
5
+ * of the same rules.
6
+ */
7
+ export declare const validator: import("@bufbuild/protovalidate").Validator;
8
+ /**
9
+ * Validates a response message against its buf.validate proto annotations.
10
+ *
11
+ * On success, returns the message unchanged (typed). On failure, throws a
12
+ * {@link ConnectError} with {@link Code.Internal} and a message matching the
13
+ * shape emitted by the SDK's response-validation interceptor — so propagating
14
+ * the error from a handler produces the same wire response as not calling
15
+ * `validate` at all.
16
+ *
17
+ * Intended use:
18
+ * ```ts
19
+ * return validate(PayOutResponseSchema, { result: { case: "accepted", value: {} } });
20
+ * ```
21
+ *
22
+ * Catch the error to convert it into a domain-level failure (e.g. the `Failed`
23
+ * arm of a `oneof result`) instead of an opaque `Code.Internal`.
24
+ */
25
+ export declare function validate<Desc extends DescMessage>(schema: Desc, msg: MessageShape<Desc>): MessageShape<Desc>;
@@ -0,0 +1,38 @@
1
+ import { Code, ConnectError } from "@connectrpc/connect";
2
+ import { createValidator } from "@bufbuild/protovalidate";
3
+ /**
4
+ * Shared protovalidate validator instance for the public {@link validate} helper.
5
+ * Construction is cheap, but reusing one instance avoids repeated compilation
6
+ * of the same rules.
7
+ */
8
+ export const validator = createValidator();
9
+ /**
10
+ * Validates a response message against its buf.validate proto annotations.
11
+ *
12
+ * On success, returns the message unchanged (typed). On failure, throws a
13
+ * {@link ConnectError} with {@link Code.Internal} and a message matching the
14
+ * shape emitted by the SDK's response-validation interceptor — so propagating
15
+ * the error from a handler produces the same wire response as not calling
16
+ * `validate` at all.
17
+ *
18
+ * Intended use:
19
+ * ```ts
20
+ * return validate(PayOutResponseSchema, { result: { case: "accepted", value: {} } });
21
+ * ```
22
+ *
23
+ * Catch the error to convert it into a domain-level failure (e.g. the `Failed`
24
+ * arm of a `oneof result`) instead of an opaque `Code.Internal`.
25
+ */
26
+ export function validate(schema, msg) {
27
+ const result = validator.validate(schema, msg);
28
+ if (result.kind === "invalid") {
29
+ const details = result.violations
30
+ .map((v) => `${v.field?.toString() ?? ""}: ${v.message}`)
31
+ .join("; ");
32
+ throw new ConnectError(`response validation failed: ${details}`, Code.Internal);
33
+ }
34
+ if (result.kind === "error") {
35
+ throw new ConnectError(`response validation error: ${result.error.message}`, Code.Internal);
36
+ }
37
+ return msg;
38
+ }
@@ -1,12 +1,26 @@
1
1
  import type { Interceptor } from "@connectrpc/connect";
2
+ /**
3
+ * Minimal logger contract accepted by the SDK. Providers may pass `console`
4
+ * directly, or adapt their preferred logger (e.g. pino) with:
5
+ *
6
+ * { error: (msg, fields) => pinoInstance.error(fields, msg) }
7
+ */
8
+ export interface Logger {
9
+ error(msg: string, fields?: Record<string, unknown>): void;
10
+ }
2
11
  /**
3
12
  * Creates a ConnectRPC interceptor that validates provider responses against
4
13
  * buf.validate proto annotations before they are serialized and sent.
5
14
  * Also validates incoming requests using the official @connectrpc/validate interceptor.
6
15
  *
7
16
  * Invalid requests return Code.InvalidArgument; invalid responses return Code.Internal.
17
+ *
18
+ * On invalid responses, a single structured `error`-level line is emitted to
19
+ * the supplied {@link Logger} (default: `console.error` with JSON-encoded
20
+ * fields) before the `Code.Internal` error is thrown. This is the safety net
21
+ * for handler code paths that skipped the public `validate()` helper.
8
22
  */
9
- export declare function createValidationInterceptor(): Interceptor;
23
+ export declare function createValidationInterceptor(logger?: Logger): Interceptor;
10
24
  /**
11
25
  * @deprecated Use createValidationInterceptor instead.
12
26
  */
@@ -1,15 +1,26 @@
1
1
  import { Code, ConnectError } from "@connectrpc/connect";
2
2
  import { createValidator } from "@bufbuild/protovalidate";
3
3
  import { createValidateInterceptor } from "@connectrpc/validate";
4
+ import { SDK_VERSION } from "../version.js";
4
5
  const validator = createValidator();
6
+ const defaultLogger = {
7
+ error: (msg, fields) =>
8
+ // eslint-disable-next-line no-console
9
+ console.error(JSON.stringify({ msg, ...(fields ?? {}) })),
10
+ };
5
11
  /**
6
12
  * Creates a ConnectRPC interceptor that validates provider responses against
7
13
  * buf.validate proto annotations before they are serialized and sent.
8
14
  * Also validates incoming requests using the official @connectrpc/validate interceptor.
9
15
  *
10
16
  * Invalid requests return Code.InvalidArgument; invalid responses return Code.Internal.
17
+ *
18
+ * On invalid responses, a single structured `error`-level line is emitted to
19
+ * the supplied {@link Logger} (default: `console.error` with JSON-encoded
20
+ * fields) before the `Code.Internal` error is thrown. This is the safety net
21
+ * for handler code paths that skipped the public `validate()` helper.
11
22
  */
12
- export function createValidationInterceptor() {
23
+ export function createValidationInterceptor(logger = defaultLogger) {
13
24
  const requestInterceptor = createValidateInterceptor();
14
25
  return (next) => async (req) => {
15
26
  // Validate request (delegates to official interceptor which throws on invalid)
@@ -19,10 +30,26 @@ export function createValidationInterceptor() {
19
30
  const msg = resp.message;
20
31
  const result = validator.validate(schema, msg);
21
32
  if (result.kind === "invalid") {
22
- const details = result.violations.map(v => `${v.field?.toString() ?? ""}: ${v.message}`).join("; ");
33
+ const violations = result.violations.map((v) => ({
34
+ field: v.field?.toString() ?? "",
35
+ message: v.message,
36
+ }));
37
+ const details = violations.map((v) => `${v.field}: ${v.message}`).join("; ");
38
+ logger.error("response validation failed", {
39
+ rpc_method: `${req.service.typeName}/${req.method.name}`,
40
+ response_type: schema.typeName,
41
+ violations,
42
+ sdk_version: SDK_VERSION,
43
+ });
23
44
  throw new ConnectError(`response validation failed: ${details}`, Code.Internal);
24
45
  }
25
46
  if (result.kind === "error") {
47
+ logger.error("response validation error", {
48
+ rpc_method: `${req.service.typeName}/${req.method.name}`,
49
+ response_type: schema.typeName,
50
+ error: result.error.message,
51
+ sdk_version: SDK_VERSION,
52
+ });
26
53
  throw new ConnectError(`response validation error: ${result.error.message}`, Code.Internal);
27
54
  }
28
55
  return resp;
@@ -1 +1 @@
1
- export declare const SDK_VERSION = "1.1.27";
1
+ export declare const SDK_VERSION = "1.1.29";
@@ -1,3 +1,3 @@
1
1
  // SDK semantic version. Bumped in lockstep with all other SDKs by the
2
2
  // release.yaml workflow.
3
- export const SDK_VERSION = "1.1.27";
3
+ export const SDK_VERSION = "1.1.29";