vintasend 0.7.0 → 0.7.1

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.
@@ -1,8 +1,9 @@
1
1
  import type { StoredAttachment } from '../../types/attachment';
2
- import type { JsonValue } from '../../types/json-values';
2
+ import type { JsonObject, JsonValue } from '../../types/json-values';
3
3
  import type { AnyDatabaseNotification, DatabaseOneOffNotification } from '../../types/notification';
4
4
  import type { NotificationType } from '../../types/notification-type';
5
5
  import type { BaseNotificationTypeConfig } from '../../types/notification-type-config';
6
+ import type { EmailTemplate, EmailTemplateContent } from '../notification-template-renderers/base-email-template-renderer';
6
7
  import type { BaseLogger } from '../loggers/base-logger';
7
8
  import type { BaseNotificationBackend } from '../notification-backends/base-notification-backend';
8
9
  import type { BaseNotificationTemplateRenderer } from '../notification-template-renderers/base-notification-template-renderer';
@@ -45,4 +46,5 @@ export declare abstract class BaseNotificationAdapter<TemplateRenderer extends B
45
46
  };
46
47
  injectBackend(backend: BaseNotificationBackend<Config>): void;
47
48
  injectLogger(logger: BaseLogger): void;
49
+ renderFromTemplateContent(notification: AnyDatabaseNotification<Config>, templateContent: EmailTemplateContent, context: JsonObject): Promise<EmailTemplate>;
48
50
  }
@@ -90,5 +90,12 @@ class BaseNotificationAdapter {
90
90
  injectLogger(logger) {
91
91
  this.logger = logger;
92
92
  }
93
+ async renderFromTemplateContent(notification, templateContent, context) {
94
+ if (typeof this.templateRenderer.renderFromTemplateContent !== 'function') {
95
+ throw new Error('Template renderer does not support renderFromTemplateContent.');
96
+ }
97
+ const rendered = await this.templateRenderer.renderFromTemplateContent(notification, templateContent, context);
98
+ return rendered;
99
+ }
93
100
  }
94
101
  exports.BaseNotificationAdapter = BaseNotificationAdapter;
@@ -8,10 +8,17 @@ import type { BaseLogger } from './loggers/base-logger';
8
8
  import { type BaseNotificationAdapter } from './notification-adapters/base-notification-adapter';
9
9
  import { type BaseNotificationBackend, type NotificationFilterFields } from './notification-backends/base-notification-backend';
10
10
  import type { BaseNotificationQueueService } from './notification-queue-service/base-notification-queue-service';
11
+ import type { EmailTemplate, EmailTemplateContent } from './notification-template-renderers/base-email-template-renderer';
11
12
  import type { BaseNotificationTemplateRenderer } from './notification-template-renderers/base-notification-template-renderer';
12
13
  type VintaSendOptions = {
13
14
  raiseErrorOnFailedSend: boolean;
14
15
  };
16
+ type RenderEmailTemplateContextInput<Config extends BaseNotificationTypeConfig> = {
17
+ context: JsonObject;
18
+ } | {
19
+ contextName: string & keyof Config['ContextMap'];
20
+ contextParameters: JsonObject;
21
+ };
15
22
  type VintaSendFactoryCreateParams<Config extends BaseNotificationTypeConfig, AdaptersList extends BaseNotificationAdapter<BaseNotificationTemplateRenderer<Config>, Config>[], Backend extends BaseNotificationBackend<Config>, Logger extends BaseLogger, QueueService extends BaseNotificationQueueService<Config>, AttachmentMgr extends BaseAttachmentManager> = {
16
23
  adapters: AdaptersList;
17
24
  backend: Backend;
@@ -101,6 +108,7 @@ export declare class VintaSend<Config extends BaseNotificationTypeConfig, Adapte
101
108
  getFutureNotificationsFromUser(userId: Config['NotificationIdType'], page: number, pageSize: number): Promise<DatabaseNotification<Config>[]>;
102
109
  getFutureNotifications(page: number, pageSize: number): Promise<AnyDatabaseNotification<Config>[]>;
103
110
  getNotificationContext<ContextName extends string & keyof Config['ContextMap']>(contextName: ContextName, parameters: Parameters<ReturnType<typeof this.contextGeneratorsMap.getContextGenerator<ContextName>>['generate']>[0]): Promise<JsonObject>;
111
+ renderEmailTemplateFromContent(notification: AnyDatabaseNotification<Config>, templateContent: EmailTemplateContent, contextInput: RenderEmailTemplateContextInput<Config>): Promise<EmailTemplate>;
104
112
  sendPendingNotifications(): Promise<void>;
105
113
  getPendingNotifications(page: number, pageSize: number): Promise<AnyDatabaseNotification<Config>[]>;
106
114
  getNotifications(page: number, pageSize: number): Promise<AnyDatabaseNotification<Config>[]>;
@@ -269,6 +269,17 @@ class VintaSend {
269
269
  }
270
270
  return Promise.resolve(context);
271
271
  }
272
+ async renderEmailTemplateFromContent(notification, templateContent, contextInput) {
273
+ const adaptersOfType = this.adapters.filter((adapter) => adapter.notificationType === notification.notificationType);
274
+ if (adaptersOfType.length === 0) {
275
+ throw new Error(`No adapter found for notification type ${notification.notificationType}`);
276
+ }
277
+ const adapter = adaptersOfType[0];
278
+ const context = 'context' in contextInput
279
+ ? contextInput.context
280
+ : await this.getNotificationContext(contextInput.contextName, contextInput.contextParameters);
281
+ return adapter.renderFromTemplateContent(notification, templateContent, context);
282
+ }
272
283
  async sendPendingNotifications() {
273
284
  const pendingNotifications = await this.backend.getAllPendingNotifications();
274
285
  await Promise.all(pendingNotifications.map((notification) => this.send(notification)));
@@ -9,8 +9,13 @@ export type EmailTemplate = {
9
9
  subject: string;
10
10
  body: string;
11
11
  };
12
+ export type EmailTemplateContent = {
13
+ subject: string | null;
14
+ body: string;
15
+ };
12
16
  export interface BaseEmailTemplateRenderer<Config extends BaseNotificationTypeConfig> extends BaseNotificationTemplateRenderer<Config, EmailTemplate> {
13
17
  render(notification: AnyNotification<Config>, context: JsonObject): Promise<EmailTemplate>;
18
+ renderFromTemplateContent(notification: AnyNotification<Config>, templateContent: EmailTemplateContent, context: JsonObject): Promise<EmailTemplate>;
14
19
  /**
15
20
  * Inject logger into the template renderer (optional - called by VintaSend when logger exists)
16
21
  */
@@ -3,4 +3,5 @@ import type { AnyNotification } from '../../types/notification';
3
3
  import type { BaseNotificationTypeConfig } from '../../types/notification-type-config';
4
4
  export interface BaseNotificationTemplateRenderer<Config extends BaseNotificationTypeConfig, T = unknown> {
5
5
  render(notification: AnyNotification<Config>, context: JsonObject): Promise<T>;
6
+ renderFromTemplateContent(notification: AnyNotification<Config>, templateContent: unknown, context: JsonObject): Promise<T>;
6
7
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vintasend",
3
- "version": "0.7.0",
3
+ "version": "0.7.1",
4
4
  "main": "dist/index.js",
5
5
  "files": [
6
6
  "dist"