@vrplatform/log 1.0.21 → 2.0.0-alpha.2

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 (71) hide show
  1. package/build/main/index.d.ts +2 -22
  2. package/build/main/index.js +2 -74
  3. package/build/main/index.js.map +1 -1
  4. package/build/main/log/baselog.js.map +1 -0
  5. package/build/main/log/color.js.map +1 -0
  6. package/build/main/log/common.js.map +1 -0
  7. package/build/main/log/index.d.ts +22 -0
  8. package/build/main/log/index.js +91 -0
  9. package/build/main/log/index.js.map +1 -0
  10. package/build/{module → main/log}/type.d.ts +1 -3
  11. package/build/main/log/type.js.map +1 -0
  12. package/build/main/tracking/index.d.ts +53 -0
  13. package/build/main/tracking/index.js +78 -0
  14. package/build/main/tracking/index.js.map +1 -0
  15. package/build/main/tracking/intercom.d.ts +108 -0
  16. package/build/main/tracking/intercom.js +139 -0
  17. package/build/main/tracking/intercom.js.map +1 -0
  18. package/build/module/index.d.ts +2 -22
  19. package/build/module/index.js +2 -73
  20. package/build/module/index.js.map +1 -1
  21. package/build/module/log/baselog.js.map +1 -0
  22. package/build/module/log/color.js.map +1 -0
  23. package/build/module/log/common.js.map +1 -0
  24. package/build/module/log/index.d.ts +22 -0
  25. package/build/module/log/index.js +74 -0
  26. package/build/module/log/index.js.map +1 -0
  27. package/build/{main → module/log}/type.d.ts +1 -3
  28. package/build/module/log/type.js.map +1 -0
  29. package/build/module/tracking/index.d.ts +53 -0
  30. package/build/module/tracking/index.js +74 -0
  31. package/build/module/tracking/index.js.map +1 -0
  32. package/build/module/tracking/intercom.d.ts +108 -0
  33. package/build/module/tracking/intercom.js +136 -0
  34. package/build/module/tracking/intercom.js.map +1 -0
  35. package/package.json +25 -20
  36. package/src/index.ts +2 -127
  37. package/src/log/index.ts +127 -0
  38. package/src/{type.ts → log/type.ts} +1 -5
  39. package/src/tracking/index.ts +124 -0
  40. package/src/tracking/intercom.ts +257 -0
  41. package/build/main/baselog.js.map +0 -1
  42. package/build/main/color.js.map +0 -1
  43. package/build/main/common.js.map +0 -1
  44. package/build/main/index.spec.d.ts +0 -0
  45. package/build/main/index.spec.js +0 -2
  46. package/build/main/index.spec.js.map +0 -1
  47. package/build/main/type.js.map +0 -1
  48. package/build/module/baselog.js.map +0 -1
  49. package/build/module/color.js.map +0 -1
  50. package/build/module/common.js.map +0 -1
  51. package/build/module/index.spec.d.ts +0 -0
  52. package/build/module/index.spec.js +0 -2
  53. package/build/module/index.spec.js.map +0 -1
  54. package/build/module/type.js.map +0 -1
  55. /package/build/main/{baselog.d.ts → log/baselog.d.ts} +0 -0
  56. /package/build/main/{baselog.js → log/baselog.js} +0 -0
  57. /package/build/main/{color.d.ts → log/color.d.ts} +0 -0
  58. /package/build/main/{color.js → log/color.js} +0 -0
  59. /package/build/main/{common.d.ts → log/common.d.ts} +0 -0
  60. /package/build/main/{common.js → log/common.js} +0 -0
  61. /package/build/main/{type.js → log/type.js} +0 -0
  62. /package/build/module/{baselog.d.ts → log/baselog.d.ts} +0 -0
  63. /package/build/module/{baselog.js → log/baselog.js} +0 -0
  64. /package/build/module/{color.d.ts → log/color.d.ts} +0 -0
  65. /package/build/module/{color.js → log/color.js} +0 -0
  66. /package/build/module/{common.d.ts → log/common.d.ts} +0 -0
  67. /package/build/module/{common.js → log/common.js} +0 -0
  68. /package/build/module/{type.js → log/type.js} +0 -0
  69. /package/src/{baselog.ts → log/baselog.ts} +0 -0
  70. /package/src/{color.ts → log/color.ts} +0 -0
  71. /package/src/{common.ts → log/common.ts} +0 -0
@@ -0,0 +1,108 @@
1
+ import type { Log } from '../log';
2
+ export declare class IntercomAPI {
3
+ constructor(token: string, log?: Log);
4
+ private headers;
5
+ private log?;
6
+ private fetch;
7
+ getUserByUserId(userId: string): Promise<{
8
+ type: "list";
9
+ data: {
10
+ id: string;
11
+ }[] | undefined;
12
+ total_count: 0;
13
+ pages: {
14
+ type: "pages";
15
+ page: 1;
16
+ per_page: 10;
17
+ total_pages: 0;
18
+ };
19
+ } | undefined>;
20
+ createUser({ data, userId, }: {
21
+ userId: string;
22
+ data: {
23
+ email: string;
24
+ name: string;
25
+ custom_attributes: Record<string, any>;
26
+ };
27
+ }): Promise<unknown>;
28
+ updateUser({ data, contactId, }: {
29
+ contactId: string;
30
+ data: {
31
+ email?: string;
32
+ name?: string;
33
+ custom_attributes: Record<string, any>;
34
+ };
35
+ }): Promise<unknown>;
36
+ deleteUser(intercom_contact_id: string): Promise<{
37
+ id: "6657adf56abd0167d9419d1c";
38
+ external_id: "70";
39
+ type: "contact";
40
+ deleted: true;
41
+ } | undefined>;
42
+ createCompanyUser(data: {
43
+ intercom_contact_id: string;
44
+ intercom_company_id: string;
45
+ }): Promise<unknown>;
46
+ deleteCompanyUser({ intercom_company_id, intercom_contact_id, }: {
47
+ intercom_contact_id: string;
48
+ intercom_company_id: string;
49
+ }): Promise<{
50
+ id: "6657adc96abd0167d9419ca7";
51
+ } | undefined>;
52
+ trackEvent(data: {
53
+ user_id: string;
54
+ event_name: string;
55
+ metadata: Record<string, any>;
56
+ }): Promise<unknown>;
57
+ getCompanyByTenantId(tenantId: string): Promise<{
58
+ type: "list";
59
+ data: [{
60
+ type: "company";
61
+ company_id: "remote_companies_scroll_2";
62
+ id: "664df5436abd01f7945e1a78";
63
+ app_id: "this_is_an_id111_that_should_be_at_least_";
64
+ name: "IntercomQATest1";
65
+ remote_created_at: 1716385091;
66
+ created_at: 1716385091;
67
+ updated_at: 1716385091;
68
+ monthly_spend: 0;
69
+ session_count: 0;
70
+ user_count: 4;
71
+ tags: {
72
+ type: "tag.list";
73
+ tags: [];
74
+ };
75
+ segments: {
76
+ type: "segment.list";
77
+ segments: [];
78
+ };
79
+ custom_attributes: Record<string, string>;
80
+ }] | undefined;
81
+ pages: {
82
+ type: "pages";
83
+ next: null;
84
+ page: 1;
85
+ per_page: 15;
86
+ total_pages: 1;
87
+ };
88
+ total_count: 1;
89
+ } | undefined>;
90
+ createCompany({ data, tenantId, }: {
91
+ tenantId: string;
92
+ data: {
93
+ name: string;
94
+ remote_created_at: string;
95
+ custom_attributes: Record<string, any>;
96
+ };
97
+ }): Promise<unknown>;
98
+ updateCompany({ data, companyId, }: {
99
+ companyId: string;
100
+ data: Record<string, any>;
101
+ }): Promise<unknown>;
102
+ deleteCompany(intercom_company_id: string): Promise<{
103
+ id: "6657adc96abd0167d9419ca7";
104
+ object: "company";
105
+ deleted: true;
106
+ } | undefined>;
107
+ }
108
+ export declare function useIntercom(token: string, log?: Log): IntercomAPI;
@@ -0,0 +1,136 @@
1
+ export class IntercomAPI {
2
+ constructor(token, log) {
3
+ this.headers = {
4
+ 'Content-Type': 'application/json',
5
+ Accept: 'application/json',
6
+ 'Intercom-Version': '2.11',
7
+ Authorization: `Bearer ${token}`,
8
+ };
9
+ this.log = log;
10
+ }
11
+ headers;
12
+ log;
13
+ async fetch(path, options) {
14
+ if (options?.data) {
15
+ options.body = JSON.stringify(options.data);
16
+ options.data = undefined;
17
+ }
18
+ try {
19
+ const result = await fetch(`https://api.intercom.io${path}`, {
20
+ method: 'GET',
21
+ headers: this.headers,
22
+ ...options,
23
+ });
24
+ if (!result.ok) {
25
+ this.log?.info('Failed to fetch from Intercom', {
26
+ result: {
27
+ status: result.status,
28
+ statusText: result.statusText,
29
+ body: await result.text(),
30
+ },
31
+ });
32
+ if (result.status === 404)
33
+ return undefined;
34
+ if (result.status === 429) {
35
+ console.log('Intercom rate limit exceeded, waiting for 1 minute');
36
+ // X-RateLimit-Reset: 1487332520 => timestamp from header response when resetting
37
+ const reset = Number(result.headers.get('X-RateLimit-Reset'));
38
+ if (reset) {
39
+ const now = Math.floor(Date.now() / 1000);
40
+ const wait = reset - now + 1;
41
+ console.log(`Waiting for ${wait} seconds`);
42
+ await new Promise((resolve) => setTimeout(resolve, wait * 1000));
43
+ return await this.fetch(path, options);
44
+ }
45
+ }
46
+ }
47
+ return result.status === 202 ? {} : (await result.json());
48
+ }
49
+ catch (error) {
50
+ this.log?.error(error);
51
+ return undefined;
52
+ }
53
+ }
54
+ // User
55
+ async getUserByUserId(userId) {
56
+ return await this.fetch('/contacts/search', {
57
+ method: 'POST',
58
+ data: {
59
+ query: {
60
+ field: 'external_id',
61
+ operator: '=',
62
+ value: userId,
63
+ },
64
+ },
65
+ });
66
+ }
67
+ async createUser({ data, userId, }) {
68
+ return await this.fetch('/contacts', {
69
+ method: 'POST',
70
+ data: { ...data, external_id: userId },
71
+ });
72
+ }
73
+ async updateUser({ data, contactId, }) {
74
+ return await this.fetch(`/contacts/${contactId}`, {
75
+ method: 'PUT',
76
+ data,
77
+ });
78
+ }
79
+ async deleteUser(intercom_contact_id) {
80
+ return await this.fetch(`/contacts/${intercom_contact_id}`, {
81
+ method: 'DELETE',
82
+ });
83
+ }
84
+ async createCompanyUser(data) {
85
+ return await this.fetch(`/contacts/${data.intercom_contact_id}/companies`, {
86
+ method: 'POST',
87
+ data: {
88
+ id: data.intercom_company_id,
89
+ },
90
+ });
91
+ }
92
+ async deleteCompanyUser({ intercom_company_id, intercom_contact_id, }) {
93
+ return await this.fetch(`/contacts/${intercom_contact_id}/companies/${intercom_company_id}`, {
94
+ method: 'DELETE',
95
+ });
96
+ }
97
+ async trackEvent(data) {
98
+ return await this.fetch('/events', {
99
+ method: 'POST',
100
+ data: {
101
+ created_at: Math.floor(new Date().getTime() / 1000), // to seconds
102
+ ...data,
103
+ },
104
+ });
105
+ }
106
+ // Company
107
+ async getCompanyByTenantId(tenantId) {
108
+ return await this.fetch(`/companies?company_id=${tenantId}`, {
109
+ method: 'GET',
110
+ });
111
+ }
112
+ async createCompany({ data, tenantId, }) {
113
+ return await this.fetch('/companies', {
114
+ method: 'POST',
115
+ data: {
116
+ ...data,
117
+ company_id: tenantId,
118
+ },
119
+ });
120
+ }
121
+ async updateCompany({ data, companyId, }) {
122
+ return await this.fetch(`/companies/${companyId}`, {
123
+ method: 'PUT',
124
+ data,
125
+ });
126
+ }
127
+ async deleteCompany(intercom_company_id) {
128
+ return await this.fetch(`/companies/${intercom_company_id}`, {
129
+ method: 'DELETE',
130
+ });
131
+ }
132
+ }
133
+ export function useIntercom(token, log) {
134
+ return new IntercomAPI(token, log);
135
+ }
136
+ //# sourceMappingURL=intercom.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"intercom.js","sourceRoot":"src/","sources":["tracking/intercom.ts"],"names":[],"mappings":"AAEA,MAAM,OAAO,WAAW;IACtB,YAAY,KAAa,EAAE,GAAS;QAClC,IAAI,CAAC,OAAO,GAAG;YACb,cAAc,EAAE,kBAAkB;YAClC,MAAM,EAAE,kBAAkB;YAC1B,kBAAkB,EAAE,MAAM;YAC1B,aAAa,EAAE,UAAU,KAAK,EAAE;SACjC,CAAC;QAEF,IAAI,CAAC,GAAG,GAAG,GAAG,CAAC;IACjB,CAAC;IAEO,OAAO,CAAyB;IAChC,GAAG,CAAO;IAEV,KAAK,CAAC,KAAK,CACjB,IAAY,EACZ,OAAsC;QAEtC,IAAI,OAAO,EAAE,IAAI,EAAE,CAAC;YAClB,OAAO,CAAC,IAAI,GAAG,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;YAC5C,OAAO,CAAC,IAAI,GAAG,SAAS,CAAC;QAC3B,CAAC;QAED,IAAI,CAAC;YACH,MAAM,MAAM,GAAG,MAAM,KAAK,CAAC,0BAA0B,IAAI,EAAE,EAAE;gBAC3D,MAAM,EAAE,KAAK;gBACb,OAAO,EAAE,IAAI,CAAC,OAAO;gBACrB,GAAG,OAAO;aACX,CAAC,CAAC;YAEH,IAAI,CAAC,MAAM,CAAC,EAAE,EAAE,CAAC;gBACf,IAAI,CAAC,GAAG,EAAE,IAAI,CAAC,+BAA+B,EAAE;oBAC9C,MAAM,EAAE;wBACN,MAAM,EAAE,MAAM,CAAC,MAAM;wBACrB,UAAU,EAAE,MAAM,CAAC,UAAU;wBAC7B,IAAI,EAAE,MAAM,MAAM,CAAC,IAAI,EAAE;qBAC1B;iBACF,CAAC,CAAC;gBAEH,IAAI,MAA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AAE;gBACJ,UAAU,EAAE,IAAI,CAAC,KAAK,CAAC,IAAI,IAAI,EAAE,CAAC,OAAO,EAAE,GAAG,IAAI,CAAC,EAAE,aAAa;gBAClE,GAAG,IAAI;aACR;SACF,CAAC,CAAC;IACL,CAAC;IAED,UAAU;IACV,KAAK,CAAC,oBAAoB,CAAC,QAAgB;QACzC,OAAO,MAAM,IAAI,CAAC,KAAK,CAoCpB,yBAAyB,QAAQ,EAAE,EAAE;YACtC,MAAM,EAAE,KAAK;SACd,CAAC,CAAC;IACL,CAAC;IACD,KAAK,CAAC,aAAa,CAAC,EAClB,IAAI,EACJ,QAAQ,GAQT;QACC,OAAO,MAAM,IAAI,CAAC,KAAK,CAAC,YAAY,EAAE;YACpC,MAAM,EAAE,MAAM;YACd,IAAI,EAAE;gBACJ,GAAG,IAAI;gBACP,UAAU,EAAE,QAAQ;aACrB;SACF,CAAC,CAAC;IACL,CAAC;IACD,KAAK,CAAC,aAAa,CAAC,EAClB,IAAI,EACJ,SAAS,GAIV;QACC,OAAO,MAAM,IAAI,CAAC,KAAK,CAAC,cAAc,SAAS,EAAE,EAAE;YACjD,MAAM,EAAE,KAAK;YACb,IAAI;SACL,CAAC,CAAC;IACL,CAAC;IACD,KAAK,CAAC,aAAa,CAAC,mBAA2B;QAC7C,OAAO,MAAM,IAAI,CAAC,KAAK,CAIpB,cAAc,mBAAmB,EAAE,EAAE;YACtC,MAAM,EAAE,QAAQ;SACjB,CAAC,CAAC;IACL,CAAC;CACF;AAED,MAAM,UAAU,WAAW,CAAC,KAAa,EAAE,GAAS;IAClD,OAAO,IAAI,WAAW,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC;AACrC,CAAC"}
package/package.json CHANGED
@@ -1,14 +1,31 @@
1
1
  {
2
2
  "name": "@vrplatform/log",
3
- "version": "1.0.21",
4
- "main": "build/main/index.js",
3
+ "version": "2.0.0-alpha.2",
4
+ "main": "src/index.ts",
5
5
  "publishConfig": {
6
- "access": "public"
6
+ "access": "public",
7
+ "main": "build/main/index.js",
8
+ "typings": "build/main/index.d.ts",
9
+ "module": "build/module/index.js"
7
10
  },
8
11
  "description": "Finalytic cf",
9
12
  "repository": "https://github.com/finalytic/ecosystem",
10
13
  "license": "UNLICENSED",
11
14
  "keywords": [],
15
+ "scripts": {
16
+ "pub": "bun run build && npm version patch --no-commit-hooks --no-git-tag-version && npm publish --no-git-checks",
17
+ "pub:alpha": "bun run build && npm version prerelease --no-commit-hooks --no-git-tag-version && npm publish --no-git-checks --tag alpha",
18
+ "build": "bun build:main & bun build:module",
19
+ "build:main": "tsc -b tsconfig.main.json",
20
+ "build:module": "tsc -b tsconfig.esm.json",
21
+ "typecheck": "tsc --noEmit",
22
+ "lint": "biome check .",
23
+ "lint:fix": "biome check --apply-unsafe --skip-errors .",
24
+ "pre-commit": "echo precommit",
25
+ "pre-push": "bun typecheck && bun lint",
26
+ "post-merge": "bun i",
27
+ "prepare": "husky"
28
+ },
12
29
  "files": [
13
30
  "build/main",
14
31
  "build/module",
@@ -18,10 +35,12 @@
18
35
  ],
19
36
  "dependencies": {
20
37
  "@axiomhq/js": "1.0.0-rc.3",
38
+ "@june-so/analytics-node": "^8.0.0",
39
+ "posthog-node": "^4.0.1",
21
40
  "serialize-error": "11.0.3"
22
41
  },
23
42
  "devDependencies": {
24
- "@biomejs/biome": "^1.8.3",
43
+ "@biomejs/biome": "1.8.3",
25
44
  "@cloudflare/workers-types": "4.20240620.0",
26
45
  "@types/node": "^20.14.9",
27
46
  "husky": "^9.0.11",
@@ -29,19 +48,5 @@
29
48
  },
30
49
  "trustedDependencies": [
31
50
  "@biomejs/biome"
32
- ],
33
- "scripts": {
34
- "pub": "bun run build && pnpm publish --no-git-checks",
35
- "build": "bun build:main & bun build:module",
36
- "build:main": "tsc -b tsconfig.main.json",
37
- "build:module": "tsc -b tsconfig.esm.json",
38
- "typecheck": "tsc --noEmit",
39
- "lint": "biome check .",
40
- "lint:fix": "biome check --apply-unsafe --skip-errors .",
41
- "pre-commit": "echo precommit",
42
- "pre-push": "bun typecheck && bun lint",
43
- "post-merge": "bun i"
44
- },
45
- "typings": "build/main/index.d.ts",
46
- "module": "build/module/index.js"
47
- }
51
+ ]
52
+ }
package/src/index.ts CHANGED
@@ -1,127 +1,2 @@
1
- import { createBaseLog } from './baselog';
2
- import {
3
- AXIOM_AUTH_TOKEN_ENV,
4
- AXIOM_DATASET_ENV,
5
- AXIOM_ORG_ID_ENV,
6
- LOG_ENV,
7
- TASK_QUEUE_HEADER,
8
- WORKFLOW_ID_HEADER,
9
- getCorrelationId,
10
- getEnvironment,
11
- requestToContext,
12
- } from './common';
13
- import type { RequestLike, WorkerLog } from './type';
14
-
15
- export * from './common';
16
- export * from './baselog';
17
- export * from './type';
18
-
19
- export function useLog({
20
- environment: _environment,
21
- name,
22
- request,
23
- body,
24
- env,
25
- correlationId: cid,
26
- version,
27
- executionContext,
28
- logRequest,
29
- dataset: _dataset,
30
- context: initialContext = {},
31
- }: {
32
- environment?: string;
33
- name: string;
34
- request?: RequestLike & { cf?: any };
35
- logRequest?: boolean | ((raw: ReturnType<typeof requestToContext>) => any);
36
- body?: any;
37
- correlationId?: string;
38
- version?: string;
39
- dataset?: string;
40
- env?: Record<string, any>;
41
- context?: Record<string, any>;
42
- executionContext?: {
43
- waitUntil(promise: Promise<any>): void;
44
- };
45
- }): WorkerLog {
46
- const environment = _environment || getEnvironment(env);
47
- const colorLogs = env?.[LOG_ENV] === '*' || environment === 'development';
48
- const correlationId = cid || getCorrelationId(request); //const logger = nodeColorLog;
49
-
50
- const axiomToken = env?.[AXIOM_AUTH_TOKEN_ENV];
51
-
52
- const axiom = createBaseLog(
53
- {
54
- token: axiomToken,
55
- orgId: env?.[AXIOM_ORG_ID_ENV],
56
- dataset: _dataset || env?.[AXIOM_DATASET_ENV] || 'default',
57
- consoleLog: colorLogs ? 'color' : false,
58
- },
59
- {
60
- environment,
61
- type: 'worker',
62
- executionContext,
63
- workerId: env?.MACHINE_NAME || name || 'default',
64
- app: name || 'default',
65
- version: version || 'default',
66
- correlationId,
67
- context: {
68
- ...initialContext,
69
- rootCorrelationId: correlationId,
70
- correlationId,
71
- taskQueue: request?.headers?.get(TASK_QUEUE_HEADER) || undefined,
72
- workflowId: request?.headers?.get(WORKFLOW_ID_HEADER) || undefined,
73
- child: undefined as any as string,
74
- },
75
- }
76
- ) as WorkerLog;
77
-
78
- if (logRequest !== false) {
79
- const { message, ...raw } =
80
- typeof logRequest === 'function'
81
- ? logRequest(requestToContext(request, body))
82
- : requestToContext(request, body);
83
- axiom.info(message || 'Request', raw);
84
- }
85
-
86
- axiom.respond = async (res) => {
87
- const temp = await (typeof res === 'function' ? res() : res);
88
-
89
- const response = (() => {
90
- if (temp instanceof Response) {
91
- axiom?.info(`Response ${temp.status}`, {
92
- response: {
93
- body: temp
94
- .clone()
95
- .json()
96
- .catch(() => undefined),
97
- response: temp,
98
- status: temp.status,
99
- statusText: temp.statusText,
100
- headers: temp.headers,
101
- },
102
- });
103
-
104
- return temp;
105
- } else {
106
- // We have no response status code YET
107
- axiom?.info('Response 200', {
108
- response: temp,
109
- });
110
-
111
- return new Response(
112
- typeof temp.body === 'object' ? JSON.stringify(temp.body) : temp.body,
113
- {
114
- status: temp.status,
115
- statusText: temp.statusText,
116
- headers: temp.headers,
117
- }
118
- );
119
- }
120
- })();
121
-
122
- await axiom?.flush();
123
-
124
- return response!;
125
- };
126
- return axiom;
127
- }
1
+ export * from './log';
2
+ export * from './tracking';
@@ -0,0 +1,127 @@
1
+ import { createBaseLog } from './baselog';
2
+ import {
3
+ AXIOM_AUTH_TOKEN_ENV,
4
+ AXIOM_DATASET_ENV,
5
+ AXIOM_ORG_ID_ENV,
6
+ LOG_ENV,
7
+ TASK_QUEUE_HEADER,
8
+ WORKFLOW_ID_HEADER,
9
+ getCorrelationId,
10
+ getEnvironment,
11
+ requestToContext,
12
+ } from './common';
13
+ import type { RequestLike, WorkerLog } from './type';
14
+
15
+ export * from './common';
16
+ export * from './baselog';
17
+ export * from './type';
18
+
19
+ export function useLog({
20
+ environment: _environment,
21
+ name,
22
+ request,
23
+ body,
24
+ env,
25
+ correlationId: cid,
26
+ version,
27
+ executionContext,
28
+ logRequest,
29
+ dataset: _dataset,
30
+ context: initialContext = {},
31
+ }: {
32
+ environment?: string;
33
+ name: string;
34
+ request?: RequestLike & { cf?: any };
35
+ logRequest?: boolean | ((raw: ReturnType<typeof requestToContext>) => any);
36
+ body?: any;
37
+ correlationId?: string;
38
+ version?: string;
39
+ dataset?: string;
40
+ env?: Record<string, any>;
41
+ context?: Record<string, any>;
42
+ executionContext?: {
43
+ waitUntil(promise: Promise<any>): void;
44
+ };
45
+ }): WorkerLog {
46
+ const environment = _environment || getEnvironment(env);
47
+ const colorLogs = env?.[LOG_ENV] === '*' || environment === 'development';
48
+ const correlationId = cid || getCorrelationId(request); //const logger = nodeColorLog;
49
+
50
+ const axiomToken = env?.[AXIOM_AUTH_TOKEN_ENV];
51
+
52
+ const axiom = createBaseLog(
53
+ {
54
+ token: axiomToken,
55
+ orgId: env?.[AXIOM_ORG_ID_ENV],
56
+ dataset: _dataset || env?.[AXIOM_DATASET_ENV] || 'default',
57
+ consoleLog: colorLogs ? 'color' : false,
58
+ },
59
+ {
60
+ environment,
61
+ type: 'worker',
62
+ executionContext,
63
+ workerId: env?.MACHINE_NAME || name || 'default',
64
+ app: name || 'default',
65
+ version: version || 'default',
66
+ correlationId,
67
+ context: {
68
+ ...initialContext,
69
+ rootCorrelationId: correlationId,
70
+ correlationId,
71
+ taskQueue: request?.headers?.get(TASK_QUEUE_HEADER) || undefined,
72
+ workflowId: request?.headers?.get(WORKFLOW_ID_HEADER) || undefined,
73
+ child: undefined as any as string,
74
+ },
75
+ }
76
+ ) as WorkerLog;
77
+
78
+ if (logRequest !== false) {
79
+ const { message, ...raw } =
80
+ typeof logRequest === 'function'
81
+ ? logRequest(requestToContext(request, body))
82
+ : requestToContext(request, body);
83
+ axiom.info(message || 'Request', raw);
84
+ }
85
+
86
+ axiom.respond = async (res) => {
87
+ const temp = await (typeof res === 'function' ? res() : res);
88
+
89
+ const response = (() => {
90
+ if (temp instanceof Response) {
91
+ axiom?.info(`Response ${temp.status}`, {
92
+ response: {
93
+ body: temp
94
+ .clone()
95
+ .json()
96
+ .catch(() => undefined),
97
+ response: temp,
98
+ status: temp.status,
99
+ statusText: temp.statusText,
100
+ headers: temp.headers,
101
+ },
102
+ });
103
+
104
+ return temp;
105
+ } else {
106
+ // We have no response status code YET
107
+ axiom?.info('Response 200', {
108
+ response: temp,
109
+ });
110
+
111
+ return new Response(
112
+ typeof temp.body === 'object' ? JSON.stringify(temp.body) : temp.body,
113
+ {
114
+ status: temp.status,
115
+ statusText: temp.statusText,
116
+ headers: temp.headers,
117
+ }
118
+ );
119
+ }
120
+ })();
121
+
122
+ await axiom?.flush();
123
+
124
+ return response!;
125
+ };
126
+ return axiom;
127
+ }
@@ -2,15 +2,11 @@ export type LogFn = (
2
2
  message: any,
3
3
  additionalContext?: Record<string, any>
4
4
  ) => void;
5
-
6
- export type LogFns = {
5
+ export type Log = {
7
6
  error: LogFn;
8
7
  info: LogFn;
9
8
  debug: LogFn;
10
9
  warn: LogFn;
11
- };
12
-
13
- export type Log = LogFns & {
14
10
  child: (data: ChildLogOptions) => Log;
15
11
  addContext: (data: Record<string, any>) => void;
16
12
  };