franchising 0.6.0 → 0.8.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.
Files changed (110) hide show
  1. package/README.md +13 -72
  2. package/dist/{accounts-TyMPSHM0.cjs → accounts-BscmRiXc.cjs} +26 -1
  3. package/dist/{accounts-TyMPSHM0.cjs.map → accounts-BscmRiXc.cjs.map} +1 -1
  4. package/dist/{accounts-B4bnTXUd.js → accounts-DJRg4gXj.js} +26 -1
  5. package/dist/{accounts-B4bnTXUd.js.map → accounts-DJRg4gXj.js.map} +1 -1
  6. package/dist/cli/template-assets/api/src/main.ts +13 -11
  7. package/dist/cli/template-assets/api/src/modules/health/controller.ts +25 -4
  8. package/dist/cli/template-assets/api/src/shared/http/errors.ts +17 -0
  9. package/dist/cli/template-assets/cloudflare/cache.service.ts +1 -0
  10. package/dist/cli/template-assets/cloudflare/queue.service.ts +1 -0
  11. package/dist/cli/template-assets/cloudflare/worker.ts +4 -0
  12. package/dist/cli/template-assets/github/PULL_REQUEST_TEMPLATE.md +37 -0
  13. package/dist/cli/template-assets/observability/adapters.ts +37 -0
  14. package/dist/cli/template-assets/observability/bootstrap.ts +48 -0
  15. package/dist/cli/template-assets/observability/env.ts +21 -0
  16. package/dist/cli/template-assets/observability/index.ts +10 -0
  17. package/dist/cli/template-assets/observability/lifecycle.ts +97 -0
  18. package/dist/cli/template-assets/observability/local-index.ts +9 -0
  19. package/dist/cli/template-assets/observability/register.ts +12 -0
  20. package/dist/cli/template-assets/packages/jobs/src/index.ts +4 -40
  21. package/dist/cli/template-assets/packages/jobs/src/shared/queue/job-id.ts +9 -0
  22. package/dist/cli/template-assets/packages/jobs/src/shared/queue/options.ts +7 -0
  23. package/dist/cli/template-assets/packages/jobs/src/shared/queue/prefix.ts +18 -0
  24. package/dist/cli/template-assets/packages/queue/src/index.ts +4 -0
  25. package/dist/cli/template-assets/packages/queue/src/shared/errors/failure-reason.ts +7 -0
  26. package/dist/cli/template-assets/packages/{queue-runtime/src/index.ts → queue/src/shared/redis/connection.ts} +2 -15
  27. package/dist/cli/template-assets/packages/queue/src/types/redis/connection.ts +6 -0
  28. package/dist/cli/template-assets/packages/webhooks/src/index.ts +11 -0
  29. package/dist/cli/template-assets/packages/webhooks/src/shared/crypto/secret.ts +81 -0
  30. package/dist/cli/template-assets/packages/webhooks/src/shared/signing/headers.ts +43 -0
  31. package/dist/cli/template-assets/packages/webhooks/src/shared/url/deliverable.ts +58 -0
  32. package/dist/cli/template-assets/packages/webhooks/src/types/webhooks/job.ts +18 -0
  33. package/dist/cli/template-assets/packages/webhooks/test/webhooks.test.ts.tpl +69 -0
  34. package/dist/cli/template-assets/root/dot-dockerignore +88 -8
  35. package/dist/cli/template-assets/root/dot-oxlintrc.json +22 -0
  36. package/dist/cli/template-assets/root/lefthook.yml +32 -8
  37. package/dist/cli/template-assets/web/src/app/(app)/layout.tsx +2 -3
  38. package/dist/cli/template-assets/web/src/app/(auth)/login/page.tsx +1 -1
  39. package/dist/cli/template-assets/web/src/app/page.tsx +1 -0
  40. package/dist/cli/template-assets/web/src/{lib/api-client.ts → shared/api/client.ts} +11 -8
  41. package/dist/cli/template-assets/web/src/{lib/server-auth.ts → shared/auth/server.ts} +1 -2
  42. package/dist/cli/template-assets/web/src/types/api.ts +0 -15
  43. package/dist/cli/template-assets/webhooks/api/src/modules/webhooks/controller.ts +81 -0
  44. package/dist/cli/template-assets/webhooks/api/src/modules/webhooks/dto/create-endpoint.dto.ts +25 -0
  45. package/dist/cli/template-assets/webhooks/api/src/modules/webhooks/dto/endpoint.dto.ts +28 -0
  46. package/dist/cli/template-assets/webhooks/api/src/modules/webhooks/module.ts +27 -0
  47. package/dist/cli/template-assets/webhooks/api/src/modules/webhooks/repository.ts +147 -0
  48. package/dist/cli/template-assets/webhooks/api/src/modules/webhooks/service.ts +119 -0
  49. package/dist/cli/template-assets/webhooks/api/src/types/webhooks/endpoint.ts +26 -0
  50. package/dist/cli/template-assets/webhooks/worker/src/env.ts +18 -0
  51. package/dist/cli/template-assets/webhooks/worker/src/modules/queue/dispatcher.ts +119 -0
  52. package/dist/cli/template-assets/webhooks/worker/src/modules/queue/module.ts +16 -0
  53. package/dist/cli/template-assets/webhooks/worker/src/modules/queue/processor.ts +176 -0
  54. package/dist/cli/template-assets/webhooks/worker/src/modules/queue/repository.ts +187 -0
  55. package/dist/cli/template-assets/webhooks/worker/src/shared/http/deliver.ts +41 -0
  56. package/dist/cli/template-assets/webhooks/worker/src/types/webhooks/dispatch.ts +26 -0
  57. package/dist/cli/template-assets/worker/src/app.module.ts +14 -0
  58. package/dist/cli/template-assets/worker/src/main.ts +24 -0
  59. package/dist/cli/template-assets/worker/src/modules/queue/processor.ts +26 -3
  60. package/dist/cli/template-assets/worker/src/modules/queue/retry.ts +22 -7
  61. package/dist/cli/template-assets/worker/test/retry.test.ts.tpl +13 -1
  62. package/dist/cli/template-assets/worker-messaging/messaging-api-service.ts +6 -6
  63. package/dist/cli/template-assets/worker-messaging/src/env.ts +1 -1
  64. package/dist/cli/template-assets/worker-messaging/src/modules/messaging/service.ts +2 -4
  65. package/dist/cli/template-assets/worker-messaging/src/modules/queue/module.ts +1 -1
  66. package/dist/cli/template-assets/worker-messaging/src/modules/queue/processor.ts +32 -10
  67. package/dist/cli.js +631 -314
  68. package/dist/{index-CNlb6WNr.d.ts → index-Bl4Ka1Wr.d.ts} +2 -2
  69. package/dist/{index-I_Nx5SsQ.d.cts → index-CBue43h0.d.cts} +2 -2
  70. package/dist/{index-B_byXBRW.d.ts → index-cKCKFKLi.d.cts} +181 -5
  71. package/dist/{index-B_byXBRW.d.cts → index-cKCKFKLi.d.ts} +181 -5
  72. package/dist/index.cjs +1 -1
  73. package/dist/index.d.cts +1 -1
  74. package/dist/index.d.ts +1 -1
  75. package/dist/index.js +1 -1
  76. package/dist/messaging/index.cjs +1 -1
  77. package/dist/messaging/index.d.cts +1 -1
  78. package/dist/messaging/index.d.ts +1 -1
  79. package/dist/messaging/index.js +1 -1
  80. package/dist/sso/client.d.cts +2 -2
  81. package/dist/sso/client.d.ts +2 -2
  82. package/dist/sso/index.cjs +72 -1
  83. package/dist/sso/index.cjs.map +1 -1
  84. package/dist/sso/index.d.cts +2 -2
  85. package/dist/sso/index.d.ts +2 -2
  86. package/dist/sso/index.js +72 -1
  87. package/dist/sso/index.js.map +1 -1
  88. package/dist/sso/nestjs.cjs +2 -2
  89. package/dist/sso/nestjs.d.cts +2 -2
  90. package/dist/sso/nestjs.d.ts +2 -2
  91. package/dist/sso/nestjs.js +2 -2
  92. package/dist/sso/resource.cjs +2 -2
  93. package/dist/sso/resource.d.cts +3 -3
  94. package/dist/sso/resource.d.ts +3 -3
  95. package/dist/sso/resource.js +2 -2
  96. package/dist/{verifier-BIBtPCVi.cjs → verifier-BYrFb8tg.cjs} +2 -2
  97. package/dist/{verifier-BIBtPCVi.cjs.map → verifier-BYrFb8tg.cjs.map} +1 -1
  98. package/dist/{verifier-DNdy5Ynw.js → verifier-DSvtdWuY.js} +2 -2
  99. package/dist/{verifier-DNdy5Ynw.js.map → verifier-DSvtdWuY.js.map} +1 -1
  100. package/dist/version-8Zc_uUvG.js +1 -0
  101. package/dist/{version-DDLRsahz.cjs → version-BMXokpna.cjs} +1 -1
  102. package/package.json +1 -1
  103. package/dist/cli/template-assets/api/src/errors/codes.ts +0 -17
  104. package/dist/cli/template-assets/api/src/errors/index.ts +0 -16
  105. package/dist/cli/template-assets/api/src/filters/exception.ts +0 -53
  106. package/dist/cli/template-assets/api/src/shared/exceptions.ts +0 -40
  107. package/dist/cli/template-assets/api/src/shared/response.ts +0 -31
  108. package/dist/cli/template-assets/api/src/telemetry.ts +0 -59
  109. package/dist/version-DHlIo_M1.js +0 -1
  110. /package/dist/cli/template-assets/web/src/{lib/auth-client.ts → shared/auth/client.ts} +0 -0
@@ -1,15 +1,21 @@
1
1
  import {
2
2
  Injectable,
3
+ // #if !telemetry
3
4
  Logger,
5
+ // #endif
4
6
  type OnModuleDestroy,
5
7
  type OnModuleInit,
6
8
  } from '@nestjs/common';
7
9
  import { UnrecoverableError, Worker } from 'bullmq';
10
+ // #if telemetry
11
+ import { Telemetry } from 'franchising/telemetry';
12
+ import { observeWorker, runObservedJob } from '__ROCK_SCOPE__/observability';
13
+ // #endif
8
14
  import {
9
15
  createWorkerRedisConnection,
10
16
  failureReason,
11
17
  type QueueRedisConnection,
12
- } from '__ROCK_SCOPE__/queue-runtime';
18
+ } from '__ROCK_SCOPE__/queue';
13
19
  import { __ROCK_WORKER_QUEUE_CONST__, queuePrefix } from '__ROCK_SCOPE__/jobs';
14
20
  import { env } from '../../env';
15
21
  import {
@@ -33,19 +39,33 @@ import { isRetryable } from './retry';
33
39
  export class __ROCK_WORKER_CLASS__Processor
34
40
  implements OnModuleInit, OnModuleDestroy
35
41
  {
42
+ // #if !telemetry
36
43
  private readonly logger = new Logger(__ROCK_WORKER_CLASS__Processor.name);
44
+ // #endif
37
45
  private worker?: Worker;
38
46
  private connection?: QueueRedisConnection;
39
47
 
48
+ // #if telemetry
40
49
  constructor(
41
50
  private readonly repository: __ROCK_WORKER_CLASS__Repository,
51
+ private readonly telemetry: Telemetry,
42
52
  ) {}
53
+ // #else
54
+ constructor(
55
+ private readonly repository: __ROCK_WORKER_CLASS__Repository,
56
+ ) {}
57
+ // #endif
43
58
 
44
59
  onModuleInit(): void {
45
60
  this.connection = createWorkerRedisConnection(env.QUEUE_REDIS_URL);
61
+
46
62
  this.worker = new Worker(
47
63
  __ROCK_WORKER_QUEUE_CONST__,
64
+ // #if telemetry
65
+ (job) => runObservedJob(this.telemetry, job, () => this.process(job)),
66
+ // #else
48
67
  (job) => this.process(job),
68
+ // #endif
49
69
  {
50
70
  connection: this.connection.client,
51
71
  prefix: queuePrefix(
@@ -60,6 +80,9 @@ export class __ROCK_WORKER_CLASS__Processor
60
80
  },
61
81
  );
62
82
 
83
+ // #if telemetry
84
+ observeWorker(this.worker, this.telemetry);
85
+ // #else
63
86
  this.worker.on('failed', (job, error) => {
64
87
  this.logger.error(
65
88
  JSON.stringify({
@@ -79,6 +102,7 @@ export class __ROCK_WORKER_CLASS__Processor
79
102
  }),
80
103
  );
81
104
  });
105
+ // #endif
82
106
  }
83
107
 
84
108
  async onModuleDestroy(): Promise<void> {
@@ -95,9 +119,8 @@ export class __ROCK_WORKER_CLASS__Processor
95
119
  * `UnrecoverableError` tira o job da fila na hora. Sem isso um
96
120
  * payload invalido ficaria repetindo ate esgotar as tentativas.
97
121
  */
98
- if (!isRetryable(error)) {
122
+ if (!isRetryable(error))
99
123
  throw new UnrecoverableError(failureReason(error));
100
- }
101
124
 
102
125
  throw error;
103
126
  }
@@ -1,17 +1,32 @@
1
+ /** Status HTTP de um erro: `HttpError`/`HttpException` ou `{ status }`. */
2
+ function statusOf(error: unknown): number | undefined {
3
+ const candidate = error as
4
+ | { status?: unknown; getStatus?: () => number }
5
+ | undefined;
6
+
7
+ if (typeof candidate?.getStatus === 'function') return candidate.getStatus();
8
+
9
+ return typeof candidate?.status === 'number' ? candidate.status : void 0;
10
+ }
11
+
1
12
  /**
2
13
  * Decide se vale tentar o job de novo.
3
14
  *
4
15
  * @remarks
5
- * Erro de rede ou indisponibilidade tende a passar sozinho, entao vale
6
- * retentar. Erro de payload invalido nao muda com o tempo: retentar so queima
7
- * as tentativas e atrasa a ida para a fila de falhas.
16
+ * Erro de rede, 5xx, 408 e 429 tendem a passar sozinhos, entao vale retentar.
17
+ * Payload invalido (`TypeError`, o `ValidationFailed` do `franchising/http`) e
18
+ * os outros 4xx nao mudam com o tempo: retentar so queima as tentativas e
19
+ * atrasa a ida para a fila de falhas.
8
20
  */
9
21
  export function isRetryable(error: unknown): boolean {
10
- if (error instanceof TypeError) {
22
+ if (error instanceof TypeError) return false;
23
+
24
+ if ((error as { code?: unknown } | undefined)?.code === 'ValidationFailed')
11
25
  return false;
12
- }
13
26
 
14
- const code = (error as { code?: string } | undefined)?.code;
27
+ const status = statusOf(error);
28
+
29
+ if (status === void 0) return true;
15
30
 
16
- return code !== 'VALIDATION_FAILED';
31
+ return status >= 500 || status === 408 || status === 429;
17
32
  }
@@ -7,6 +7,18 @@ describe('isRetryable', () => {
7
7
  });
8
8
 
9
9
  it('nao retenta payload invalido', () => {
10
- expect(isRetryable(Object.assign(new Error('bad'), { code: 'VALIDATION_FAILED' }))).toBe(false);
10
+ expect(
11
+ isRetryable(Object.assign(new Error('bad'), { code: 'ValidationFailed' })),
12
+ ).toBe(false);
13
+
14
+ expect(isRetryable(new TypeError('INVALID_JOB'))).toBe(false);
15
+ });
16
+
17
+ it('retenta 5xx, 408 e 429, e nao os outros 4xx', () => {
18
+ for (const status of [500, 503, 408, 429])
19
+ expect(isRetryable({ status })).toBe(true);
20
+
21
+ for (const status of [400, 401, 404, 410])
22
+ expect(isRetryable({ status })).toBe(false);
11
23
  });
12
24
  });
@@ -2,29 +2,29 @@ import { Injectable, type OnModuleDestroy } from '@nestjs/common';
2
2
  import { Queue } from 'bullmq';
3
3
  import {
4
4
  DEFAULT_JOB_OPTIONS,
5
- MESSAGING_QUEUE_NAME,
5
+ EMAILS_QUEUE_NAME,
6
6
  queuePrefix,
7
7
  type SendEmailJob,
8
8
  } from '__ROCK_SCOPE__/jobs';
9
- import { createProducerRedisConnection } from '__ROCK_SCOPE__/queue-runtime';
9
+ import { createProducerRedisConnection } from '__ROCK_SCOPE__/queue';
10
10
  import { env } from '../../env';
11
11
 
12
12
  /**
13
- * Publica e-mails na fila do worker `messaging`.
13
+ * Publica e-mails na fila do worker `emails`.
14
14
  *
15
15
  * @remarks
16
16
  * A API nao fala com o provedor: enfileira e responde. O envio, as
17
17
  * retentativas e a credencial da plataforma Messaging ficam em
18
- * `apps/workers/messaging`, entao uma lentidao no provedor nao segura a
18
+ * `apps/workers/emails`, entao uma lentidao no provedor nao segura a
19
19
  * requisicao de quem pediu o e-mail.
20
20
  */
21
21
  @Injectable()
22
22
  export class MessagingService implements OnModuleDestroy {
23
23
  private readonly connection = createProducerRedisConnection(env.REDIS_URL);
24
24
 
25
- private readonly queue = new Queue<SendEmailJob>(MESSAGING_QUEUE_NAME, {
25
+ private readonly queue = new Queue<SendEmailJob>(EMAILS_QUEUE_NAME, {
26
26
  connection: this.connection.client,
27
- prefix: queuePrefix(env.QUEUE_PREFIX, MESSAGING_QUEUE_NAME),
27
+ prefix: queuePrefix(env.QUEUE_PREFIX, EMAILS_QUEUE_NAME),
28
28
  defaultJobOptions: DEFAULT_JOB_OPTIONS,
29
29
  });
30
30
 
@@ -8,7 +8,7 @@ export const env = cleanEnv(process.env, {
8
8
  }),
9
9
  QUEUE_REDIS_URL: str({ default: 'redis://localhost:6379' }),
10
10
  QUEUE_PREFIX: str({ default: '__ROCK_PROJECT_SLUG__' }),
11
- MESSAGING_CONCURRENCY: num({ default: 5 }),
11
+ __ROCK_WORKER_CONCURRENCY_ENV__: num({ default: 5 }),
12
12
  MESSAGING_API_KEY: str({ default: '' }),
13
13
  MESSAGING_FROM: str({ default: 'nao-responda@rockfeller.com.br' }),
14
14
  });
@@ -29,9 +29,8 @@ export class MessagingService {
29
29
  * desenvolvimento o conteudo vai para o console, para testar links
30
30
  * de login e redefinicao sem conta na plataforma.
31
31
  */
32
- if (env.NODE_ENV === 'production') {
32
+ if (env.NODE_ENV === 'production')
33
33
  throw new Error('MESSAGING_API_KEY_REQUIRED');
34
- }
35
34
 
36
35
  this.logger.warn(
37
36
  JSON.stringify({
@@ -52,8 +51,7 @@ export class MessagingService {
52
51
  html: input.html,
53
52
  });
54
53
 
55
- if (error) {
54
+ if (error)
56
55
  throw new Error(`EMAIL_SEND_FAILED: ${error.code}`);
57
- }
58
56
  }
59
57
  }
@@ -5,4 +5,4 @@ import { MessagingProcessor } from './processor';
5
5
  @Module({
6
6
  providers: [MessagingService, MessagingProcessor],
7
7
  })
8
- export class MessagingModule {}
8
+ export class __ROCK_WORKER_CLASS__Module {}
@@ -1,17 +1,23 @@
1
1
  import {
2
2
  Injectable,
3
+ // #if !telemetry
3
4
  Logger,
5
+ // #endif
4
6
  type OnModuleDestroy,
5
7
  type OnModuleInit,
6
8
  } from '@nestjs/common';
7
9
  import { UnrecoverableError, Worker } from 'bullmq';
10
+ // #if telemetry
11
+ import { Telemetry } from 'franchising/telemetry';
12
+ import { observeWorker, runObservedJob } from '__ROCK_SCOPE__/observability';
13
+ // #endif
8
14
  import {
9
15
  createWorkerRedisConnection,
10
16
  failureReason,
11
17
  type QueueRedisConnection,
12
- } from '__ROCK_SCOPE__/queue-runtime';
18
+ } from '__ROCK_SCOPE__/queue';
13
19
  import {
14
- MESSAGING_QUEUE_NAME,
20
+ __ROCK_WORKER_QUEUE_CONST__,
15
21
  queuePrefix,
16
22
  type SendEmailJob,
17
23
  } from '__ROCK_SCOPE__/jobs';
@@ -33,15 +39,14 @@ function parseSendEmailJob(data: unknown): SendEmailJob {
33
39
  typeof job?.to !== 'string' ||
34
40
  typeof job.subject !== 'string' ||
35
41
  typeof job.html !== 'string'
36
- ) {
42
+ )
37
43
  throw new TypeError('INVALID_EMAIL_JOB');
38
- }
39
44
 
40
45
  return { to: job.to, subject: job.subject, html: job.html };
41
46
  }
42
47
 
43
48
  /**
44
- * Consumidor da fila `messaging`.
49
+ * Consumidor da fila `__ROCK_WORKER_QUEUE__`.
45
50
  *
46
51
  * @remarks
47
52
  * A API so publica o e-mail; o envio, as retentativas e a credencial da
@@ -51,21 +56,35 @@ function parseSendEmailJob(data: unknown): SendEmailJob {
51
56
  */
52
57
  @Injectable()
53
58
  export class MessagingProcessor implements OnModuleInit, OnModuleDestroy {
59
+ // #if !telemetry
54
60
  private readonly logger = new Logger(MessagingProcessor.name);
61
+ // #endif
55
62
  private worker?: Worker;
56
63
  private connection?: QueueRedisConnection;
57
64
 
65
+ // #if telemetry
66
+ constructor(
67
+ private readonly messaging: MessagingService,
68
+ private readonly telemetry: Telemetry,
69
+ ) {}
70
+ // #else
58
71
  constructor(private readonly messaging: MessagingService) {}
72
+ // #endif
59
73
 
60
74
  onModuleInit(): void {
61
75
  this.connection = createWorkerRedisConnection(env.QUEUE_REDIS_URL);
76
+
62
77
  this.worker = new Worker(
63
- MESSAGING_QUEUE_NAME,
78
+ __ROCK_WORKER_QUEUE_CONST__,
79
+ // #if telemetry
80
+ (job) => runObservedJob(this.telemetry, job, () => this.process(job)),
81
+ // #else
64
82
  (job) => this.process(job),
83
+ // #endif
65
84
  {
66
85
  connection: this.connection.client,
67
- prefix: queuePrefix(env.QUEUE_PREFIX, MESSAGING_QUEUE_NAME),
68
- concurrency: env.MESSAGING_CONCURRENCY,
86
+ prefix: queuePrefix(env.QUEUE_PREFIX, __ROCK_WORKER_QUEUE_CONST__),
87
+ concurrency: env.__ROCK_WORKER_CONCURRENCY_ENV__,
69
88
  limiter: { max: RATE_LIMIT_PER_SECOND, duration: 1000 },
70
89
  lockDuration: LOCK_DURATION_MS,
71
90
  stalledInterval: STALLED_INTERVAL_MS,
@@ -73,6 +92,9 @@ export class MessagingProcessor implements OnModuleInit, OnModuleDestroy {
73
92
  },
74
93
  );
75
94
 
95
+ // #if telemetry
96
+ observeWorker(this.worker, this.telemetry);
97
+ // #else
76
98
  this.worker.on('failed', (job, error) => {
77
99
  this.logger.error(
78
100
  JSON.stringify({
@@ -92,6 +114,7 @@ export class MessagingProcessor implements OnModuleInit, OnModuleDestroy {
92
114
  }),
93
115
  );
94
116
  });
117
+ // #endif
95
118
  }
96
119
 
97
120
  async onModuleDestroy(): Promise<void> {
@@ -103,9 +126,8 @@ export class MessagingProcessor implements OnModuleInit, OnModuleDestroy {
103
126
  try {
104
127
  await this.messaging.sendEmail(parseSendEmailJob(job.data));
105
128
  } catch (error) {
106
- if (!isRetryable(error)) {
129
+ if (!isRetryable(error))
107
130
  throw new UnrecoverableError(failureReason(error));
108
- }
109
131
 
110
132
  throw error;
111
133
  }