@graphql-hive/logger 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.
@@ -0,0 +1,20 @@
1
+ 'use strict';
2
+
3
+ const requestIdByRequest = /* @__PURE__ */ new WeakMap();
4
+ function loggerForRequest(log, request, getId) {
5
+ let requestId = requestIdByRequest.get(request);
6
+ if (!requestId) {
7
+ if (getId === void 0) {
8
+ return log;
9
+ }
10
+ requestId = getId(request);
11
+ requestIdByRequest.set(request, requestId);
12
+ }
13
+ if (log.attrs && "requestId" in log.attrs && log.attrs["requestId"] === requestId) {
14
+ return log;
15
+ }
16
+ return log.child({ requestId });
17
+ }
18
+
19
+ exports.loggerForRequest = loggerForRequest;
20
+ exports.requestIdByRequest = requestIdByRequest;
@@ -0,0 +1,26 @@
1
+ import { L as Logger } from './Logger-BMj0djMW.js';
2
+
3
+ declare const requestIdByRequest: WeakMap<Request, string>;
4
+ /** The getter function that extracts the requestID from the {@link request} or creates a new one if none-exist. */
5
+ type GetRequestID = (request: Request) => string;
6
+ /**
7
+ * Creates a child {@link Logger} under the {@link log given logger} for the {@link request}.
8
+ *
9
+ * Request's ID will be stored in the {@link requestIdByRequest} weak map; meaning, all
10
+ * subsequent calls to this function with the same {@link request} will return the same ID.
11
+ *
12
+ * The {@link getId} argument will be used to create a new ID if the {@link request} does not
13
+ * have one. The convention is to the `X-Request-ID` header or create a new ID which is an
14
+ * UUID v4.
15
+ *
16
+ * On the other hand, if the {@link getId} argument is omitted, the {@link requestIdByRequest} weak
17
+ * map will be looked up, and if there is no ID stored for the {@link request} - the function
18
+ * will not attempt to create a new ID and will just return the same {@link log logger}.
19
+ *
20
+ * The request ID will be added to the logger attributes under the `requestId` key and
21
+ * will be logged in every subsequent log.
22
+ */
23
+ declare function loggerForRequest(log: Logger, request: Request): Logger;
24
+ declare function loggerForRequest(log: Logger, request: Request, getId: GetRequestID): Logger;
25
+
26
+ export { type GetRequestID, loggerForRequest, requestIdByRequest };
@@ -0,0 +1,26 @@
1
+ import { L as Logger } from './Logger-BMj0djMW.js';
2
+
3
+ declare const requestIdByRequest: WeakMap<Request, string>;
4
+ /** The getter function that extracts the requestID from the {@link request} or creates a new one if none-exist. */
5
+ type GetRequestID = (request: Request) => string;
6
+ /**
7
+ * Creates a child {@link Logger} under the {@link log given logger} for the {@link request}.
8
+ *
9
+ * Request's ID will be stored in the {@link requestIdByRequest} weak map; meaning, all
10
+ * subsequent calls to this function with the same {@link request} will return the same ID.
11
+ *
12
+ * The {@link getId} argument will be used to create a new ID if the {@link request} does not
13
+ * have one. The convention is to the `X-Request-ID` header or create a new ID which is an
14
+ * UUID v4.
15
+ *
16
+ * On the other hand, if the {@link getId} argument is omitted, the {@link requestIdByRequest} weak
17
+ * map will be looked up, and if there is no ID stored for the {@link request} - the function
18
+ * will not attempt to create a new ID and will just return the same {@link log logger}.
19
+ *
20
+ * The request ID will be added to the logger attributes under the `requestId` key and
21
+ * will be logged in every subsequent log.
22
+ */
23
+ declare function loggerForRequest(log: Logger, request: Request): Logger;
24
+ declare function loggerForRequest(log: Logger, request: Request, getId: GetRequestID): Logger;
25
+
26
+ export { type GetRequestID, loggerForRequest, requestIdByRequest };
@@ -0,0 +1,17 @@
1
+ const requestIdByRequest = /* @__PURE__ */ new WeakMap();
2
+ function loggerForRequest(log, request, getId) {
3
+ let requestId = requestIdByRequest.get(request);
4
+ if (!requestId) {
5
+ if (getId === void 0) {
6
+ return log;
7
+ }
8
+ requestId = getId(request);
9
+ requestIdByRequest.set(request, requestId);
10
+ }
11
+ if (log.attrs && "requestId" in log.attrs && log.attrs["requestId"] === requestId) {
12
+ return log;
13
+ }
14
+ return log.child({ requestId });
15
+ }
16
+
17
+ export { loggerForRequest, requestIdByRequest };
package/package.json ADDED
@@ -0,0 +1,58 @@
1
+ {
2
+ "name": "@graphql-hive/logger",
3
+ "version": "0.0.0",
4
+ "type": "module",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "git+https://github.com/graphql-hive/gateway.git",
8
+ "directory": "packages/logger"
9
+ },
10
+ "author": {
11
+ "email": "contact@the-guild.dev",
12
+ "name": "The Guild",
13
+ "url": "https://the-guild.dev"
14
+ },
15
+ "license": "MIT",
16
+ "engines": {
17
+ "node": ">=18.0.0"
18
+ },
19
+ "main": "./dist/index.js",
20
+ "types": "./dist/index.d.ts",
21
+ "exports": {
22
+ ".": {
23
+ "require": {
24
+ "types": "./dist/index.d.cts",
25
+ "default": "./dist/index.cjs"
26
+ },
27
+ "import": {
28
+ "types": "./dist/index.d.ts",
29
+ "default": "./dist/index.js"
30
+ }
31
+ },
32
+ "./request": {
33
+ "require": {
34
+ "types": "./dist/request.d.cts",
35
+ "default": "./dist/request.cjs"
36
+ },
37
+ "import": {
38
+ "types": "./dist/request.d.ts",
39
+ "default": "./dist/request.js"
40
+ }
41
+ },
42
+ "./package.json": "./package.json"
43
+ },
44
+ "files": [
45
+ "dist"
46
+ ],
47
+ "scripts": {
48
+ "build": "pkgroll --clean-dist",
49
+ "prepack": "yarn build"
50
+ },
51
+ "devDependencies": {
52
+ "@types/quick-format-unescaped": "^4.0.3",
53
+ "fast-safe-stringify": "^2.1.1",
54
+ "pkgroll": "2.11.2",
55
+ "quick-format-unescaped": "^4.0.4"
56
+ },
57
+ "sideEffects": false
58
+ }