vintasend 0.1.13 → 0.1.14

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/dist/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  export { Notification } from "./types/notification";
2
- export { NotificationService } from "./services/notification-service";
2
+ export { VintaSendFactory } from "./services/notification-service";
3
+ export type { VintaSend } from "./services/notification-service";
3
4
  export type { ContextGenerator } from "./types/notification-context-generators";
4
5
  export type { BaseNotificationTypeConfig } from "./types/notification-type-config";
5
6
  export type { BaseNotificationQueueService } from "./services/notification-queue-service/base-notification-queue-service";
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.NotificationService = void 0;
3
+ exports.VintaSendFactory = void 0;
4
4
  var notification_service_1 = require("./services/notification-service");
5
- Object.defineProperty(exports, "NotificationService", { enumerable: true, get: function () { return notification_service_1.NotificationService; } });
5
+ Object.defineProperty(exports, "VintaSendFactory", { enumerable: true, get: function () { return notification_service_1.VintaSendFactory; } });
@@ -0,0 +1,8 @@
1
+ import type { ContextGenerator } from "../types/notification-context-generators";
2
+ export declare class NotificationContextGeneratorsMap<ContextMapType extends {
3
+ [key: string]: ContextGenerator;
4
+ }> {
5
+ private contextGenerators;
6
+ constructor(contextGenerators: ContextMapType);
7
+ getContextGenerator<ContextName extends string & keyof ContextMapType>(contextName: ContextName): ContextMapType[ContextName];
8
+ }
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.NotificationContextGeneratorsMap = void 0;
4
+ class NotificationContextGeneratorsMap {
5
+ constructor(contextGenerators) {
6
+ this.contextGenerators = contextGenerators;
7
+ }
8
+ getContextGenerator(contextName) {
9
+ return this.contextGenerators[contextName];
10
+ }
11
+ }
12
+ exports.NotificationContextGeneratorsMap = NotificationContextGeneratorsMap;
@@ -1,24 +1,26 @@
1
1
  import type { DatabaseNotification, Notification } from '../types/notification';
2
- import type { ContextGenerator } from '../types/notification-context-generators';
3
- import type { JsonObject, JsonPrimitive } from '../types/json-values';
2
+ import type { JsonObject } from '../types/json-values';
4
3
  import type { BaseNotificationTypeConfig } from '../types/notification-type-config';
5
4
  import type { BaseNotificationAdapter } from './notification-adapters/base-notification-adapter';
6
5
  import type { BaseNotificationTemplateRenderer } from './notification-template-renderers/base-notification-template-renderer';
7
6
  import type { BaseNotificationBackend } from './notification-backends/base-notification-backend';
8
7
  import type { BaseLogger } from './loggers/base-logger';
9
8
  import type { BaseNotificationQueueService } from './notification-queue-service/base-notification-queue-service';
10
- type NotificationServiceOptions = {
9
+ type VintaSendOptions = {
11
10
  raiseErrorOnFailedSend: boolean;
12
11
  };
13
- export declare class NotificationService<Config extends BaseNotificationTypeConfig<ContextGeneratorsMap>, ContextGeneratorsMap extends Record<string, ContextGenerator<Parameters<ContextGeneratorsMap[keyof ContextGeneratorsMap]['generate']>[0] extends Record<string, JsonPrimitive> ? Parameters<ContextGeneratorsMap[keyof ContextGeneratorsMap]['generate']>[0] : never, ReturnType<ContextGeneratorsMap[keyof ContextGeneratorsMap]['generate']> extends JsonObject ? ReturnType<ContextGeneratorsMap[keyof ContextGeneratorsMap]['generate']> : Awaited<ReturnType<ContextGeneratorsMap[keyof ContextGeneratorsMap]['generate']>> extends JsonObject ? Awaited<ReturnType<ContextGeneratorsMap[keyof ContextGeneratorsMap]['generate']>> : never>>> {
12
+ export declare class VintaSendFactory<Config extends BaseNotificationTypeConfig> {
13
+ create<AdaptersList extends BaseNotificationAdapter<BaseNotificationTemplateRenderer<Config>, Config>[], Backend extends BaseNotificationBackend<Config>, Logger extends BaseLogger, QueueService extends BaseNotificationQueueService<Config>>(adapters: AdaptersList, backend: Backend, logger: Logger, contextGeneratorsMap: BaseNotificationTypeConfig['ContextMap'], queueService?: QueueService, options?: VintaSendOptions): VintaSend<Config, AdaptersList, Backend, Logger, QueueService>;
14
+ }
15
+ export declare class VintaSend<Config extends BaseNotificationTypeConfig, AdaptersList extends BaseNotificationAdapter<BaseNotificationTemplateRenderer<Config>, Config>[], Backend extends BaseNotificationBackend<Config>, Logger extends BaseLogger, QueueService extends BaseNotificationQueueService<Config>> {
14
16
  private adapters;
15
17
  private backend;
16
18
  private logger;
17
- private contextGeneratorsMap;
18
19
  private queueService?;
19
20
  private options;
20
- constructor(adapters: BaseNotificationAdapter<BaseNotificationTemplateRenderer<Config>, Config>[], backend: BaseNotificationBackend<Config>, logger: BaseLogger, contextGeneratorsMap: ContextGeneratorsMap, queueService?: BaseNotificationQueueService<Config> | undefined, options?: NotificationServiceOptions);
21
- registerQueueService(queueService: BaseNotificationQueueService<Config>): void;
21
+ private contextGeneratorsMap;
22
+ constructor(adapters: AdaptersList, backend: Backend, logger: Logger, contextGeneratorsMap: Config['ContextMap'], queueService?: QueueService | undefined, options?: VintaSendOptions);
23
+ registerQueueService(queueService: QueueService): void;
22
24
  send(notification: DatabaseNotification<Config>): Promise<void>;
23
25
  createNotification(notification: Omit<Notification<Config>, 'id'>): Promise<DatabaseNotification<Config>>;
24
26
  updateNotification(notificationId: Config['NotificationIdType'], notification: Partial<Omit<Notification<Config>, 'id'>>): Promise<DatabaseNotification<Config>>;
@@ -26,7 +28,7 @@ export declare class NotificationService<Config extends BaseNotificationTypeConf
26
28
  getAllFutureNotificationsFromUser(userId: Config['NotificationIdType']): Promise<DatabaseNotification<Config>[]>;
27
29
  getFutureNotificationsFromUser(userId: Config['NotificationIdType']): Promise<DatabaseNotification<Config>[]>;
28
30
  getFutureNotifications(): Promise<DatabaseNotification<Config>[]>;
29
- getNotificationContext<ContextName extends keyof Config['ContextMap']>(contextName: ContextName, parameters: Parameters<ContextGeneratorsMap[ContextName]['generate']>[0]): Promise<ReturnType<ContextGeneratorsMap[keyof ContextGeneratorsMap]["generate"]> extends JsonObject ? ReturnType<ContextGeneratorsMap[keyof ContextGeneratorsMap]["generate"]> : Awaited<ReturnType<ContextGeneratorsMap[keyof ContextGeneratorsMap]["generate"]>> extends JsonObject ? Awaited<ReturnType<ContextGeneratorsMap[keyof ContextGeneratorsMap]["generate"]>> : never>;
31
+ getNotificationContext<ContextName extends string & keyof Config['ContextMap']>(contextName: ContextName, parameters: Parameters<ReturnType<typeof this.contextGeneratorsMap.getContextGenerator<ContextName>>['generate']>[0]): Promise<JsonObject>;
30
32
  sendPendingNotifications(): Promise<void>;
31
33
  getPendingNotifications(): Promise<DatabaseNotification<Config>[]>;
32
34
  getNotification(notificationId: Config['NotificationIdType'], forUpdate?: boolean): Promise<DatabaseNotification<Config> | null>;
@@ -36,8 +38,4 @@ export declare class NotificationService<Config extends BaseNotificationTypeConf
36
38
  resendNotification(notificationId: Config['NotificationIdType'], useStoredContextIfAvailable?: boolean): Promise<DatabaseNotification<Config> | undefined>;
37
39
  delayedSend(notificationId: Config['NotificationIdType']): Promise<void>;
38
40
  }
39
- export declare class NotificationServiceSingleton {
40
- private static instance;
41
- static getInstance<Config extends BaseNotificationTypeConfig<ContextGeneratorsMap>, ContextGeneratorsMap extends Record<string, ContextGenerator<Parameters<ContextGeneratorsMap[keyof ContextGeneratorsMap]['generate']>[0] extends Record<string, JsonPrimitive> ? Parameters<ContextGeneratorsMap[keyof ContextGeneratorsMap]['generate']>[0] : never, ReturnType<ContextGeneratorsMap[keyof ContextGeneratorsMap]['generate']> extends JsonObject ? ReturnType<ContextGeneratorsMap[keyof ContextGeneratorsMap]['generate']> : Awaited<ReturnType<ContextGeneratorsMap[keyof ContextGeneratorsMap]['generate']>> extends JsonObject ? Awaited<ReturnType<ContextGeneratorsMap[keyof ContextGeneratorsMap]['generate']>> : never>>>(...args: ConstructorParameters<typeof NotificationService> | []): NotificationService<Config, ContextGeneratorsMap>;
42
- }
43
41
  export {};
@@ -1,16 +1,25 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.NotificationServiceSingleton = exports.NotificationService = void 0;
4
- class NotificationService {
3
+ exports.VintaSend = exports.VintaSendFactory = void 0;
4
+ const notification_context_generators_map_1 = require("./notification-context-generators-map");
5
+ class VintaSendFactory {
6
+ create(adapters, backend, logger, contextGeneratorsMap, queueService, options = {
7
+ raiseErrorOnFailedSend: false,
8
+ }) {
9
+ return new VintaSend(adapters, backend, logger, contextGeneratorsMap, queueService, options);
10
+ }
11
+ }
12
+ exports.VintaSendFactory = VintaSendFactory;
13
+ class VintaSend {
5
14
  constructor(adapters, backend, logger, contextGeneratorsMap, queueService, options = {
6
15
  raiseErrorOnFailedSend: false,
7
16
  }) {
8
17
  this.adapters = adapters;
9
18
  this.backend = backend;
10
19
  this.logger = logger;
11
- this.contextGeneratorsMap = contextGeneratorsMap;
12
20
  this.queueService = queueService;
13
21
  this.options = options;
22
+ this.contextGeneratorsMap = new notification_context_generators_map_1.NotificationContextGeneratorsMap(contextGeneratorsMap);
14
23
  for (const adapter of adapters) {
15
24
  adapter.injectBackend(backend);
16
25
  }
@@ -123,7 +132,7 @@ class NotificationService {
123
132
  return this.backend.getFutureNotifications();
124
133
  }
125
134
  async getNotificationContext(contextName, parameters) {
126
- const context = this.contextGeneratorsMap[contextName].generate(parameters);
135
+ const context = this.contextGeneratorsMap.getContextGenerator(contextName).generate(parameters);
127
136
  if (context instanceof Promise) {
128
137
  return await context;
129
138
  }
@@ -160,7 +169,7 @@ class NotificationService {
160
169
  }
161
170
  return;
162
171
  }
163
- if ((notification === null || notification === void 0 ? void 0 : notification.sendAfter) && notification.sendAfter > new Date()) {
172
+ if (notification.sendAfter && notification.sendAfter > new Date()) {
164
173
  this.logger.error(`Notification ${notificationId} is scheduled for the future`);
165
174
  if (this.options.raiseErrorOnFailedSend) {
166
175
  throw new Error(`Notification ${notificationId} is scheduled for the future`);
@@ -174,21 +183,32 @@ class NotificationService {
174
183
  }
175
184
  return;
176
185
  }
177
- const notificationResendInput = {
186
+ const notificationResendInputWithoutContext = {
178
187
  userId: notification.userId,
179
188
  notificationType: notification.notificationType,
180
189
  title: notification.title,
181
190
  bodyTemplate: notification.bodyTemplate,
182
191
  contextName: notification.contextName,
183
192
  contextParameters: notification.contextParameters,
184
- contextUsed: useStoredContextIfAvailable && notification.contextUsed
185
- ? notification.contextUsed
186
- : await this.getNotificationContext(notification.contextName, notification.contextParameters),
187
193
  sendAfter: null,
188
194
  subjectTemplate: notification.subjectTemplate,
189
195
  extraParams: notification.extraParams,
190
196
  };
191
- const createdNotification = await this.backend.persistNotification(notificationResendInput);
197
+ let createdNotification;
198
+ if (useStoredContextIfAvailable && notification.contextUsed) {
199
+ const notificationResendInput = {
200
+ ...notificationResendInputWithoutContext,
201
+ contextUsed: notification.contextUsed,
202
+ };
203
+ createdNotification = await this.backend.persistNotification(notificationResendInput);
204
+ }
205
+ else {
206
+ const notificationResendInput = {
207
+ ...notificationResendInputWithoutContext,
208
+ contextUsed: await this.getNotificationContext(notification.contextName, notification.contextParameters),
209
+ };
210
+ createdNotification = await this.backend.persistNotification(notificationResendInput);
211
+ }
192
212
  this.logger.info(`Notification ${createdNotification.id} created for resending notification ${notificationId}`);
193
213
  this.send(createdNotification);
194
214
  return createdNotification;
@@ -239,17 +259,4 @@ class NotificationService {
239
259
  }
240
260
  }
241
261
  }
242
- exports.NotificationService = NotificationService;
243
- // biome-ignore lint/complexity/noStaticOnlyClass: <explanation>
244
- class NotificationServiceSingleton {
245
- static getInstance(...args) {
246
- if (!NotificationServiceSingleton.instance) {
247
- if (!args || args.length === 0) {
248
- throw new Error('NotificationServiceSingleton is not initialized. Please call getInstance with the required arguments');
249
- }
250
- NotificationServiceSingleton.instance = new NotificationService(...args);
251
- }
252
- return NotificationServiceSingleton.instance;
253
- }
254
- }
255
- exports.NotificationServiceSingleton = NotificationServiceSingleton;
262
+ exports.VintaSend = VintaSend;
@@ -1,4 +1,4 @@
1
1
  import type { JsonObject, JsonPrimitive } from './json-values';
2
- export interface ContextGenerator<Params extends Record<string, JsonPrimitive> = Record<string, JsonPrimitive>, Context extends JsonObject = JsonObject> {
3
- generate(params: Params): Context | Promise<Context>;
2
+ export interface ContextGenerator<Params extends Record<string, JsonPrimitive> = Record<string, JsonPrimitive>> {
3
+ generate(params: Params): JsonObject | Promise<JsonObject>;
4
4
  }
@@ -1,7 +1,7 @@
1
1
  import type { ContextGenerator } from "./notification-context-generators";
2
2
  import type { Identifier } from "./identifier";
3
- export type BaseNotificationTypeConfig<ContextMapType extends Record<string, ContextGenerator> = Record<string, ContextGenerator>> = {
4
- ContextMap: ContextMapType;
3
+ export type BaseNotificationTypeConfig = {
4
+ ContextMap: Record<string, ContextGenerator>;
5
5
  NotificationIdType: Identifier;
6
6
  UserIdType: Identifier;
7
7
  };
@@ -1,4 +1,4 @@
1
- import type { InputJsonValue, JsonObject, JsonValue } from './json-values';
1
+ import type { InputJsonValue, JsonValue } from './json-values';
2
2
  import type { NotificationStatus } from './notification-status';
3
3
  import type { NotificationType } from './notification-type';
4
4
  import type { BaseNotificationTypeConfig } from './notification-type-config';
@@ -7,7 +7,7 @@ export type NotificationInput<Config extends BaseNotificationTypeConfig> = {
7
7
  notificationType: NotificationType;
8
8
  title: string | null;
9
9
  bodyTemplate: string;
10
- contextName: keyof Config['ContextMap'];
10
+ contextName: string & keyof Config['ContextMap'];
11
11
  contextParameters: Parameters<Config['ContextMap'][NotificationInput<Config>['contextName']]['generate']>[0];
12
12
  sendAfter: Date | null;
13
13
  subjectTemplate: string | null;
@@ -18,9 +18,9 @@ export type NotificationResendWithContextInput<Config extends BaseNotificationTy
18
18
  notificationType: NotificationType;
19
19
  title: string | null;
20
20
  bodyTemplate: string;
21
- contextName: keyof Config['ContextMap'];
21
+ contextName: string & keyof Config['ContextMap'];
22
22
  contextParameters: Parameters<Config['ContextMap'][NotificationResendWithContextInput<Config>['contextName']]['generate']>[0];
23
- contextUsed: ReturnType<Config['ContextMap'][DatabaseNotification<Config>['contextName']]['generate']> extends JsonObject ? ReturnType<Config['ContextMap'][DatabaseNotification<Config>['contextName']]['generate']> : Awaited<ReturnType<Config['ContextMap'][DatabaseNotification<Config>['contextName']]['generate']>>;
23
+ contextUsed: ReturnType<Config['ContextMap'][NotificationResendWithContextInput<Config>['contextName']]['generate']> extends Promise<infer T> ? T : ReturnType<Config['ContextMap'][NotificationResendWithContextInput<Config>['contextName']]['generate']>;
24
24
  sendAfter: Date | null;
25
25
  subjectTemplate: string | null;
26
26
  extraParams: InputJsonValue | null;
@@ -31,12 +31,12 @@ export type DatabaseNotification<Config extends BaseNotificationTypeConfig> = {
31
31
  notificationType: NotificationType;
32
32
  title: string | null;
33
33
  bodyTemplate: string;
34
- contextName: keyof Config['ContextMap'];
34
+ contextName: string & keyof Config['ContextMap'];
35
35
  contextParameters: Parameters<Config['ContextMap'][DatabaseNotification<Config>['contextName']]['generate']>[0];
36
36
  sendAfter: Date | null;
37
37
  subjectTemplate: string | null;
38
38
  status: NotificationStatus;
39
- contextUsed: ReturnType<Config['ContextMap'][DatabaseNotification<Config>['contextName']]['generate']> extends JsonObject ? ReturnType<Config['ContextMap'][DatabaseNotification<Config>['contextName']]['generate']> : Awaited<ReturnType<Config['ContextMap'][DatabaseNotification<Config>['contextName']]['generate']>>;
39
+ contextUsed: ReturnType<Config['ContextMap'][DatabaseNotification<Config>['contextName']]['generate']> extends Promise<infer T> ? T : ReturnType<Config['ContextMap'][DatabaseNotification<Config>['contextName']]['generate']>;
40
40
  extraParams: JsonValue;
41
41
  adapterUsed: string | null;
42
42
  sentAt: Date | null;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vintasend",
3
- "version": "0.1.13",
3
+ "version": "0.1.14",
4
4
  "main": "dist/index.js",
5
5
  "files": [
6
6
  "dist"