@orpc/experimental-pino 0.0.0

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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 oRPC
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,81 @@
1
+ <div align="center">
2
+ <image align="center" src="https://orpc.unnoq.com/logo.webp" width=280 alt="oRPC logo" />
3
+ </div>
4
+
5
+ <h1></h1>
6
+
7
+ <div align="center">
8
+ <a href="https://codecov.io/gh/unnoq/orpc">
9
+ <img alt="codecov" src="https://codecov.io/gh/unnoq/orpc/branch/main/graph/badge.svg">
10
+ </a>
11
+ <a href="https://www.npmjs.com/package/@orpc/experimental-pino">
12
+ <img alt="weekly downloads" src="https://img.shields.io/npm/dw/%40orpc%2Fexperimental-pino?logo=npm" />
13
+ </a>
14
+ <a href="https://github.com/unnoq/orpc/blob/main/LICENSE">
15
+ <img alt="MIT License" src="https://img.shields.io/github/license/unnoq/orpc?logo=open-source-initiative" />
16
+ </a>
17
+ <a href="https://discord.gg/TXEbwRBvQn">
18
+ <img alt="Discord" src="https://img.shields.io/discord/1308966753044398161?color=7389D8&label&logo=discord&logoColor=ffffff" />
19
+ </a>
20
+ <a href="https://deepwiki.com/unnoq/orpc">
21
+ <img src="https://deepwiki.com/badge.svg" alt="Ask DeepWiki">
22
+ </a>
23
+ </div>
24
+
25
+ <h3 align="center">Typesafe APIs Made Simple 🪄</h3>
26
+
27
+ **oRPC is a powerful combination of RPC and OpenAPI**, makes it easy to build APIs that are end-to-end type-safe and adhere to OpenAPI standards
28
+
29
+ ---
30
+
31
+ ## Highlights
32
+
33
+ - **🔗 End-to-End Type Safety**: Ensure type-safe inputs, outputs, and errors from client to server.
34
+ - **📘 First-Class OpenAPI**: Built-in support that fully adheres to the OpenAPI standard.
35
+ - **📝 Contract-First Development**: Optionally define your API contract before implementation.
36
+ - **🔍 First-Class OpenTelemetry**: Seamlessly integrate with OpenTelemetry for observability.
37
+ - **⚙️ Framework Integrations**: Seamlessly integrate with TanStack Query (React, Vue, Solid, Svelte, Angular), SWR, Pinia Colada, and more.
38
+ - **🚀 Server Actions**: Fully compatible with React Server Actions on Next.js, TanStack Start, and other platforms.
39
+ - **🔠 Standard Schema Support**: Works out of the box with Zod, Valibot, ArkType, and other schema validators.
40
+ - **🗃️ Native Types**: Supports native types like Date, File, Blob, BigInt, URL, and more.
41
+ - **⏱️ Lazy Router**: Enhance cold start times with our lazy routing feature.
42
+ - **📡 SSE & Streaming**: Enjoy full type-safe support for SSE and streaming.
43
+ - **🌍 Multi-Runtime Support**: Fast and lightweight on Cloudflare, Deno, Bun, Node.js, and beyond.
44
+ - **🔌 Extendability**: Easily extend functionality with plugins, middleware, and interceptors.
45
+
46
+ ## Documentation
47
+
48
+ You can find the full documentation [here](https://orpc.unnoq.com).
49
+
50
+ ## Packages
51
+
52
+ - [@orpc/contract](https://www.npmjs.com/package/@orpc/contract): Build your API contract.
53
+ - [@orpc/server](https://www.npmjs.com/package/@orpc/server): Build your API or implement API contract.
54
+ - [@orpc/client](https://www.npmjs.com/package/@orpc/client): Consume your API on the client with type-safety.
55
+ - [@orpc/openapi](https://www.npmjs.com/package/@orpc/openapi): Generate OpenAPI specs and handle OpenAPI requests.
56
+ - [@orpc/otel](https://www.npmjs.com/package/@orpc/otel): [OpenTelemetry](https://opentelemetry.io/) integration for observability.
57
+ - [@orpc/nest](https://www.npmjs.com/package/@orpc/nest): Deeply integrate oRPC with [NestJS](https://nestjs.com/).
58
+ - [@orpc/react](https://www.npmjs.com/package/@orpc/react): Utilities for integrating oRPC with React and React Server Actions.
59
+ - [@orpc/tanstack-query](https://www.npmjs.com/package/@orpc/tanstack-query): [TanStack Query](https://tanstack.com/query/latest) integration.
60
+ - [@orpc/experimental-react-swr](https://www.npmjs.com/package/@orpc/experimental-react-swr): [SWR](https://swr.vercel.app/) integration.
61
+ - [@orpc/vue-colada](https://www.npmjs.com/package/@orpc/vue-colada): Integration with [Pinia Colada](https://pinia-colada.esm.dev/).
62
+ - [@orpc/hey-api](https://www.npmjs.com/package/@orpc/hey-api): [Hey API](https://heyapi.dev/) integration.
63
+ - [@orpc/zod](https://www.npmjs.com/package/@orpc/zod): More schemas that [Zod](https://zod.dev/) doesn't support yet.
64
+ - [@orpc/valibot](https://www.npmjs.com/package/@orpc/valibot): OpenAPI spec generation from [Valibot](https://valibot.dev/).
65
+ - [@orpc/arktype](https://www.npmjs.com/package/@orpc/arktype): OpenAPI spec generation from [ArkType](https://arktype.io/).
66
+
67
+ ## `@orpc/experimental-pino`
68
+
69
+ Integration with [Pino](https://getpino.io/) for structured logging.
70
+
71
+ ## Sponsors
72
+
73
+ <p align="center">
74
+ <a href="https://cdn.jsdelivr.net/gh/unnoq/unnoq/sponsors.svg">
75
+ <img src='https://cdn.jsdelivr.net/gh/unnoq/unnoq/sponsors.svg'/>
76
+ </a>
77
+ </p>
78
+
79
+ ## License
80
+
81
+ Distributed under the MIT License. See [LICENSE](https://github.com/unnoq/orpc/blob/main/LICENSE) for more information.
@@ -0,0 +1,56 @@
1
+ import { Logger } from 'pino';
2
+ import { Context } from '@orpc/server';
3
+ import { StandardHandlerInterceptorOptions, StandardHandlerPlugin, StandardHandlerOptions } from '@orpc/server/standard';
4
+
5
+ declare const CONTEXT_LOGGER_SYMBOL: unique symbol;
6
+ interface LoggerContext {
7
+ [CONTEXT_LOGGER_SYMBOL]?: Logger;
8
+ }
9
+ declare function getLogger(context: object): Logger | undefined;
10
+
11
+ interface LoggingHandlerPluginOptions<T extends Context> {
12
+ /**
13
+ * Logger instance to use for logging.
14
+ *
15
+ * @default pino()
16
+ */
17
+ logger?: Logger;
18
+ /**
19
+ * Function to generate a unique ID for each request.
20
+ *
21
+ * @default () => crypto.randomUUID()
22
+ */
23
+ generateId?: (logger: Logger, options: StandardHandlerInterceptorOptions<T>) => string;
24
+ /**
25
+ * Enables info-level lifecycle logging for each request.
26
+ *
27
+ * @example
28
+ * - request received
29
+ * - request handled
30
+ *
31
+ * @default undefined (disabled)
32
+ */
33
+ logRequestLifecycle?: boolean;
34
+ /**
35
+ * Enables logging when requests are aborted.
36
+ *
37
+ * @example
38
+ * - request is aborted (reason)
39
+ *
40
+ * @remarks If a signal is used for multiple requests, this may lead to un-efficient memory usage (listeners never removed).
41
+ *
42
+ * @default undefined (disabled)
43
+ */
44
+ logAbort?: boolean;
45
+ }
46
+ declare class LoggingHandlerPlugin<T extends Context> implements StandardHandlerPlugin<T> {
47
+ private readonly logger;
48
+ private readonly generateId;
49
+ private readonly logRequestLifecycle;
50
+ private readonly logAbort;
51
+ constructor(options?: LoggingHandlerPluginOptions<T>);
52
+ init(options: StandardHandlerOptions<T>): void;
53
+ }
54
+
55
+ export { CONTEXT_LOGGER_SYMBOL, LoggingHandlerPlugin, getLogger };
56
+ export type { LoggerContext, LoggingHandlerPluginOptions };
@@ -0,0 +1,56 @@
1
+ import { Logger } from 'pino';
2
+ import { Context } from '@orpc/server';
3
+ import { StandardHandlerInterceptorOptions, StandardHandlerPlugin, StandardHandlerOptions } from '@orpc/server/standard';
4
+
5
+ declare const CONTEXT_LOGGER_SYMBOL: unique symbol;
6
+ interface LoggerContext {
7
+ [CONTEXT_LOGGER_SYMBOL]?: Logger;
8
+ }
9
+ declare function getLogger(context: object): Logger | undefined;
10
+
11
+ interface LoggingHandlerPluginOptions<T extends Context> {
12
+ /**
13
+ * Logger instance to use for logging.
14
+ *
15
+ * @default pino()
16
+ */
17
+ logger?: Logger;
18
+ /**
19
+ * Function to generate a unique ID for each request.
20
+ *
21
+ * @default () => crypto.randomUUID()
22
+ */
23
+ generateId?: (logger: Logger, options: StandardHandlerInterceptorOptions<T>) => string;
24
+ /**
25
+ * Enables info-level lifecycle logging for each request.
26
+ *
27
+ * @example
28
+ * - request received
29
+ * - request handled
30
+ *
31
+ * @default undefined (disabled)
32
+ */
33
+ logRequestLifecycle?: boolean;
34
+ /**
35
+ * Enables logging when requests are aborted.
36
+ *
37
+ * @example
38
+ * - request is aborted (reason)
39
+ *
40
+ * @remarks If a signal is used for multiple requests, this may lead to un-efficient memory usage (listeners never removed).
41
+ *
42
+ * @default undefined (disabled)
43
+ */
44
+ logAbort?: boolean;
45
+ }
46
+ declare class LoggingHandlerPlugin<T extends Context> implements StandardHandlerPlugin<T> {
47
+ private readonly logger;
48
+ private readonly generateId;
49
+ private readonly logRequestLifecycle;
50
+ private readonly logAbort;
51
+ constructor(options?: LoggingHandlerPluginOptions<T>);
52
+ init(options: StandardHandlerOptions<T>): void;
53
+ }
54
+
55
+ export { CONTEXT_LOGGER_SYMBOL, LoggingHandlerPlugin, getLogger };
56
+ export type { LoggerContext, LoggingHandlerPluginOptions };
package/dist/index.mjs ADDED
@@ -0,0 +1,98 @@
1
+ import { get, isAsyncIteratorObject, overlayProxy } from '@orpc/shared';
2
+ import { mapEventIterator } from '@orpc/client';
3
+ import pino from 'pino';
4
+
5
+ const CONTEXT_LOGGER_SYMBOL = Symbol("ORPC_PINO_CONTEXT_LOGGER_SYMBOL");
6
+ function getLogger(context) {
7
+ return get(context, [CONTEXT_LOGGER_SYMBOL]);
8
+ }
9
+
10
+ class LoggingHandlerPlugin {
11
+ logger;
12
+ generateId;
13
+ logRequestLifecycle;
14
+ logAbort;
15
+ constructor(options = {}) {
16
+ this.logger = options.logger ?? pino();
17
+ this.generateId = options.generateId ?? (() => crypto.randomUUID());
18
+ this.logRequestLifecycle = options.logRequestLifecycle;
19
+ this.logAbort = options.logAbort;
20
+ }
21
+ init(options) {
22
+ options.rootInterceptors ??= [];
23
+ options.interceptors ??= [];
24
+ options.clientInterceptors ??= [];
25
+ options.rootInterceptors.unshift(async (interceptorOptions) => {
26
+ const logger = (interceptorOptions.context[CONTEXT_LOGGER_SYMBOL] ?? this.logger).child({});
27
+ const currentBindings = logger.bindings();
28
+ const extraBindings = {
29
+ orpc: { ...currentBindings.orpc, id: this.generateId(logger, interceptorOptions) }
30
+ };
31
+ if (!currentBindings.req) {
32
+ extraBindings.req = { url: interceptorOptions.request.url, method: interceptorOptions.request.method };
33
+ }
34
+ logger.setBindings(extraBindings);
35
+ try {
36
+ return await interceptorOptions.next({
37
+ ...interceptorOptions,
38
+ context: {
39
+ ...interceptorOptions.context,
40
+ [CONTEXT_LOGGER_SYMBOL]: logger
41
+ }
42
+ });
43
+ } catch (error) {
44
+ this.logger.error(error);
45
+ throw error;
46
+ }
47
+ });
48
+ options.interceptors.unshift(async ({ next, context, request }) => {
49
+ const logger = getLogger(context);
50
+ if (this.logAbort) {
51
+ request.signal?.addEventListener("abort", () => {
52
+ logger?.info(`request is aborted (${String(request.signal?.reason)})`);
53
+ }, { once: true });
54
+ }
55
+ try {
56
+ if (this.logRequestLifecycle) {
57
+ logger?.info("request received");
58
+ }
59
+ const result = await next();
60
+ if (this.logRequestLifecycle) {
61
+ logger?.info(result.matched ? "request handled" : "no matching procedure found");
62
+ }
63
+ return result;
64
+ } catch (error) {
65
+ if (request.signal?.aborted && request.signal.reason === error) {
66
+ logger?.info(error);
67
+ } else {
68
+ logger?.error(error);
69
+ }
70
+ throw error;
71
+ }
72
+ });
73
+ options.clientInterceptors.unshift(async ({ next, path, context, signal }) => {
74
+ const logger = getLogger(context);
75
+ logger?.setBindings({ orpc: { ...logger.bindings().orpc, path } });
76
+ const output = await next();
77
+ if (isAsyncIteratorObject(output)) {
78
+ return overlayProxy(output, mapEventIterator(
79
+ output,
80
+ {
81
+ value: (v) => v,
82
+ error: async (error) => {
83
+ if (signal?.aborted && signal.reason === error) {
84
+ logger?.info(error);
85
+ } else {
86
+ logger?.error(error);
87
+ }
88
+ return error;
89
+ }
90
+ }
91
+ ));
92
+ }
93
+ return output;
94
+ });
95
+ }
96
+ }
97
+
98
+ export { CONTEXT_LOGGER_SYMBOL, LoggingHandlerPlugin, getLogger };
package/package.json ADDED
@@ -0,0 +1,42 @@
1
+ {
2
+ "name": "@orpc/experimental-pino",
3
+ "type": "module",
4
+ "version": "0.0.0",
5
+ "license": "MIT",
6
+ "homepage": "https://orpc.unnoq.com",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/unnoq/orpc.git",
10
+ "directory": "packages/pino"
11
+ },
12
+ "keywords": [
13
+ "orpc",
14
+ "pino"
15
+ ],
16
+ "exports": {
17
+ ".": {
18
+ "types": "./dist/index.d.mts",
19
+ "import": "./dist/index.mjs",
20
+ "default": "./dist/index.mjs"
21
+ }
22
+ },
23
+ "files": [
24
+ "dist"
25
+ ],
26
+ "peerDependencies": {
27
+ "pino": ">=10.1.0"
28
+ },
29
+ "dependencies": {
30
+ "@orpc/client": "1.10.4",
31
+ "@orpc/shared": "1.10.4",
32
+ "@orpc/server": "1.10.4"
33
+ },
34
+ "devDependencies": {
35
+ "pino": "^10.1.0"
36
+ },
37
+ "scripts": {
38
+ "build": "unbuild",
39
+ "build:watch": "pnpm run build --watch",
40
+ "type:check": "tsc -b"
41
+ }
42
+ }