@frigear-nu/notify 0.0.0-stage → 0.0.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.
- package/README.md +105 -2
- package/dist/module.d.mts +94 -0
- package/dist/module.json +9 -0
- package/dist/module.mjs +98 -0
- package/dist/runtime/app/composables/useNotify.d.ts +11 -0
- package/dist/runtime/app/composables/useNotify.js +16 -0
- package/dist/runtime/drivers/log.d.ts +14 -0
- package/dist/runtime/drivers/log.js +5 -0
- package/dist/runtime/drivers/ui/nuxt.ui.d.ts +2 -0
- package/dist/runtime/drivers/ui/nuxt.ui.js +20 -0
- package/dist/runtime/server/utils/definers.d.ts +9 -0
- package/dist/runtime/server/utils/definers.js +8 -0
- package/dist/runtime/server/utils/dispatch.d.ts +22 -0
- package/dist/runtime/server/utils/dispatch.js +11 -0
- package/dist/runtime/stores/memory.d.ts +2 -0
- package/dist/runtime/stores/memory.js +25 -0
- package/dist/types.d.mts +3 -0
- package/package.json +54 -3
package/README.md
CHANGED
|
@@ -1,3 +1,106 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @frigear-nu/notify
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Generic notification driver/dispatch Nuxt module. Lets feature modules (e.g. `@frigear-nu/users`)
|
|
4
|
+
dispatch email/SMS/simple notifications, and push UI toasts, without ever deciding how they're
|
|
5
|
+
delivered - that choice (and configuration) belongs to the app/consuming layer.
|
|
6
|
+
|
|
7
|
+
## Quick Setup
|
|
8
|
+
|
|
9
|
+
Other modules declare `@frigear-nu/notify` as a `moduleDependency`, so it's installed
|
|
10
|
+
automatically wherever they're used - you don't need to add it to `modules` yourself unless you
|
|
11
|
+
want to configure it.
|
|
12
|
+
|
|
13
|
+
## Configuration
|
|
14
|
+
|
|
15
|
+
Configure the module via the `notify` key in `nuxt.config.ts`. Every notification type (`email`,
|
|
16
|
+
`sms`, `simple`) defaults to the built-in `log` driver (logs instead of sending) - only the
|
|
17
|
+
`ui` driver has no default, since there's no sensible UI to push to unless you configure one:
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
export default defineNuxtConfig({
|
|
21
|
+
notify: {
|
|
22
|
+
email: {
|
|
23
|
+
// Name of a built-in driver (`'log'`, the default - logs instead of sending) or a path to
|
|
24
|
+
// a function (default export) matching the `EmailDriver` signature, for a real provider
|
|
25
|
+
// (Resend, SMTP, ...).
|
|
26
|
+
driver: '~/server/utils/resend-email-driver',
|
|
27
|
+
},
|
|
28
|
+
sms: {
|
|
29
|
+
// Same idea, matching the `SMSDriver` signature, for a real provider (Twilio, ...).
|
|
30
|
+
driver: '~/server/utils/twilio-sms-driver',
|
|
31
|
+
},
|
|
32
|
+
simple: {
|
|
33
|
+
// Same idea, matching the `SimpleDriver` signature, for generic push notifications.
|
|
34
|
+
driver: 'log',
|
|
35
|
+
},
|
|
36
|
+
ui: {
|
|
37
|
+
// Built-in `'@nuxt/ui'` driver (forwards to `@nuxt/ui`'s `useToast`), or a path to a
|
|
38
|
+
// function (default export) matching the `UIDriver` signature - a composable factory
|
|
39
|
+
// invoked once, synchronously, during `useNotify()`'s own setup-time call.
|
|
40
|
+
driver: '@nuxt/ui',
|
|
41
|
+
},
|
|
42
|
+
},
|
|
43
|
+
})
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
Only the configured driver for each notification type is ever resolved/loaded - unconfigured
|
|
47
|
+
drivers (e.g. the `ui` driver when `notify.ui` isn't set) are never bundled, to keep the client
|
|
48
|
+
bundle small.
|
|
49
|
+
|
|
50
|
+
#### Example Custom Driver
|
|
51
|
+
|
|
52
|
+
```ts
|
|
53
|
+
// server/utils/resend-email-driver.ts
|
|
54
|
+
import type { EmailDriver } from '@frigear-nu/notify/types'
|
|
55
|
+
|
|
56
|
+
const sendWithResend: EmailDriver = async (notification) => {
|
|
57
|
+
// ... call your provider's API with notification.to/subject/body/text
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export default sendWithResend
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## Usage
|
|
64
|
+
|
|
65
|
+
### Server-side dispatch
|
|
66
|
+
|
|
67
|
+
`dispatchEmailNotification`, `dispatchSMSNotification`, and `dispatchSimpleNotification` are
|
|
68
|
+
auto-imported server-side wherever this module is installed:
|
|
69
|
+
|
|
70
|
+
```ts
|
|
71
|
+
await dispatchEmailNotification({
|
|
72
|
+
to: user.email,
|
|
73
|
+
subject: 'Welcome!',
|
|
74
|
+
body: '<p>Thanks for signing up.</p>',
|
|
75
|
+
text: 'Thanks for signing up.',
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
await dispatchSMSNotification({
|
|
79
|
+
to: user.phone,
|
|
80
|
+
body: 'Your verification code is 123456.',
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
await dispatchSimpleNotification({
|
|
84
|
+
title: 'New signup',
|
|
85
|
+
text: `${user.email} just signed up.`,
|
|
86
|
+
})
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
### UI notifications
|
|
90
|
+
|
|
91
|
+
When `notify.ui.driver` is configured, `useNotify` is auto-imported client-side to push
|
|
92
|
+
`UINotification`s through the configured driver (e.g. `@nuxt/ui`'s `useToast`):
|
|
93
|
+
|
|
94
|
+
```vue
|
|
95
|
+
<script setup lang="ts">
|
|
96
|
+
const notify = useNotify()
|
|
97
|
+
|
|
98
|
+
function onSuccess() {
|
|
99
|
+
notify.addNotification(defineUINotification({
|
|
100
|
+
title: 'Saved',
|
|
101
|
+
text: 'Your changes have been saved.',
|
|
102
|
+
type: 'success',
|
|
103
|
+
}))
|
|
104
|
+
}
|
|
105
|
+
</script>
|
|
106
|
+
```
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import * as _nuxt_schema from '@nuxt/schema';
|
|
2
|
+
|
|
3
|
+
type NotificationMeta<AdditionalMeta = Record<string, unknown>> = {
|
|
4
|
+
driver?: string;
|
|
5
|
+
} & AdditionalMeta;
|
|
6
|
+
/**
|
|
7
|
+
* Generic, transport-agnostic push notification. Concrete notifications (e.g. a password reset)
|
|
8
|
+
* build one of these and hand it to `dispatchPushNotification`, similar in spirit to how Laravel
|
|
9
|
+
* notifications render themselves to a `toMail()` payload before a channel sends it.
|
|
10
|
+
*/
|
|
11
|
+
interface SimpleNotification<M = Record<string, unknown>> {
|
|
12
|
+
title: string;
|
|
13
|
+
text: string;
|
|
14
|
+
/** Optional URL to open when the notification is clicked. */
|
|
15
|
+
url?: string;
|
|
16
|
+
meta?: M;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* A notification that is intended to be displayed in the UI, e.g. via a toast or snackbar.
|
|
20
|
+
* The `type` field can be used to determine the styling of the notification.
|
|
21
|
+
*/
|
|
22
|
+
interface UINotification<A = unknown, M = NotificationMeta> extends SimpleNotification<M> {
|
|
23
|
+
type?: 'success' | 'error' | 'info' | 'warning';
|
|
24
|
+
actions?: A[];
|
|
25
|
+
meta?: M;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Generic, transport-agnostic SMS notification. Concrete notifications (e.g. a password reset)
|
|
29
|
+
* build one of these and hand it to `dispatchSMSNotification`, similar in spirit to how Laravel
|
|
30
|
+
* notifications render themselves to a `toMail()` payload before a channel sends it.
|
|
31
|
+
*/
|
|
32
|
+
interface SMSNotification<M = NotificationMeta> {
|
|
33
|
+
to: string | string[];
|
|
34
|
+
body: string;
|
|
35
|
+
meta?: M;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Generic, transport-agnostic email notification. Concrete notifications (e.g. a password reset)
|
|
39
|
+
* build one of these and hand it to `dispatchEmailNotification`, similar in spirit to how Laravel
|
|
40
|
+
* notifications render themselves to a `toMail()` payload before a channel sends it.
|
|
41
|
+
*/
|
|
42
|
+
interface EmailNotification<M = NotificationMeta> {
|
|
43
|
+
to: string | string[];
|
|
44
|
+
subject: string;
|
|
45
|
+
/** HTML body. */
|
|
46
|
+
body: string;
|
|
47
|
+
/** Optional plain-text body. */
|
|
48
|
+
text?: string;
|
|
49
|
+
meta?: M;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* A driver is anything that knows how to actually deliver an `EmailNotification` - log it,
|
|
53
|
+
* hand it to Resend, talk to an SMTP server, etc. Keeping the signature this small makes it
|
|
54
|
+
* trivial to plug in new drivers without this module depending on any of them.
|
|
55
|
+
*/
|
|
56
|
+
type SimpleDriver = (notification: SimpleNotification) => Promise<void> | void;
|
|
57
|
+
type SMSDriver = (notification: SMSNotification) => Promise<void> | void;
|
|
58
|
+
type EmailDriver = (notification: EmailNotification) => Promise<void> | void;
|
|
59
|
+
/**
|
|
60
|
+
* Unlike the server-side drivers above, a UI driver is a *composable factory*: `useNotify()` calls
|
|
61
|
+
* it once, synchronously, during its own (setup-time) invocation, and uses the function it returns
|
|
62
|
+
* to actually dispatch notifications later. This is required because driver implementations that
|
|
63
|
+
* need Vue's `inject()` (e.g. the built-in `@nuxt/ui` driver's `useToast()`) only work when called
|
|
64
|
+
* during a component's `setup()` - resolving them lazily, when a notification is dispatched (e.g.
|
|
65
|
+
* from a `setInterval` callback), runs outside any component and breaks `inject()`.
|
|
66
|
+
*/
|
|
67
|
+
type UIDriver = () => (notification: UINotification) => Promise<void> | void;
|
|
68
|
+
type ModuleOptions = {
|
|
69
|
+
email?: {
|
|
70
|
+
driver?: string | 'log';
|
|
71
|
+
};
|
|
72
|
+
sms?: {
|
|
73
|
+
driver?: string | 'log';
|
|
74
|
+
};
|
|
75
|
+
simple?: {
|
|
76
|
+
driver?: string | 'log';
|
|
77
|
+
};
|
|
78
|
+
push?: {
|
|
79
|
+
driver?: string;
|
|
80
|
+
};
|
|
81
|
+
ui?: {
|
|
82
|
+
driver?: string | '@nuxt/ui';
|
|
83
|
+
};
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
declare module '@nuxt/schema' {
|
|
87
|
+
interface NuxtConfig {
|
|
88
|
+
notify?: ModuleOptions;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
declare const _default: _nuxt_schema.NuxtModule<ModuleOptions, ModuleOptions, false>;
|
|
92
|
+
|
|
93
|
+
export { _default as default };
|
|
94
|
+
export type { EmailDriver, EmailNotification, ModuleOptions, SMSDriver, SMSNotification, SimpleDriver, SimpleNotification, UIDriver, UINotification };
|
package/dist/module.json
ADDED
package/dist/module.mjs
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { defineNuxtModule, createResolver, hasNuxtModule, addImports, addTemplate, addTypeTemplate, addServerImportsDir, addServerTemplate } from '@nuxt/kit';
|
|
2
|
+
import { joinURL } from 'ufo';
|
|
3
|
+
|
|
4
|
+
const BUILTIN_UI_DRIVERS = {
|
|
5
|
+
"log": { path: "./runtime/drivers/log", exportName: "logUIDriver" },
|
|
6
|
+
"@nuxt/ui": { path: "./runtime/drivers/ui/nuxt.ui", exportName: "default" }
|
|
7
|
+
};
|
|
8
|
+
const BUILTIN_SERVER_DRIVERS = {
|
|
9
|
+
log: { path: "./runtime/drivers/log", exportName: "logDriver" }
|
|
10
|
+
};
|
|
11
|
+
const module$1 = defineNuxtModule({
|
|
12
|
+
meta: { name: "@frigear-nu/notify", configKey: "notify" },
|
|
13
|
+
defaults: {
|
|
14
|
+
email: { driver: "log" },
|
|
15
|
+
sms: { driver: "log" },
|
|
16
|
+
simple: { driver: "log" }
|
|
17
|
+
},
|
|
18
|
+
moduleDependencies: {
|
|
19
|
+
"@nuxt/ui": {
|
|
20
|
+
optional: true,
|
|
21
|
+
version: ">=4.10"
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
async setup(options, nuxt) {
|
|
25
|
+
const resolver = createResolver(import.meta.url);
|
|
26
|
+
if (options.ui?.driver) {
|
|
27
|
+
if (options.ui.driver === "@nuxt/ui" && !hasNuxtModule("@nuxt/ui")) {
|
|
28
|
+
throw new Error(`@nuxt/ui driver is configured but @nuxt/ui module is not installed. Please install @nuxt/ui or configure a different UI driver.`);
|
|
29
|
+
}
|
|
30
|
+
addImports({
|
|
31
|
+
name: "useNotify",
|
|
32
|
+
as: "useNotify",
|
|
33
|
+
from: resolver.resolve("./runtime/app/composables/useNotify")
|
|
34
|
+
});
|
|
35
|
+
addImports({
|
|
36
|
+
name: "defineUINotification",
|
|
37
|
+
as: "defineUINotification",
|
|
38
|
+
from: resolver.resolve("./runtime/server/utils/definers")
|
|
39
|
+
});
|
|
40
|
+
const uiDriverTemplate = addTemplate({
|
|
41
|
+
filename: "#notify/ui-driver.ts",
|
|
42
|
+
write: true,
|
|
43
|
+
getContents: async () => {
|
|
44
|
+
const builtin = BUILTIN_UI_DRIVERS[options.ui?.driver];
|
|
45
|
+
if (builtin) {
|
|
46
|
+
return `export { ${builtin.exportName} as default } from ${JSON.stringify(await resolver.resolvePath(builtin.path))}`;
|
|
47
|
+
}
|
|
48
|
+
if (!options.ui?.driver) {
|
|
49
|
+
throw new Error(`Invalid UI driver: ${options.ui?.driver || "undefined"}`);
|
|
50
|
+
}
|
|
51
|
+
const driverPath = await resolver.resolvePath(options.ui.driver, { cwd: nuxt.options.rootDir });
|
|
52
|
+
return `export { default } from ${JSON.stringify(driverPath)}`;
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
nuxt.options.alias["#notify/ui-driver"] = uiDriverTemplate.dst;
|
|
56
|
+
addTypeTemplate({
|
|
57
|
+
filename: "types/notify-ui-driver.d.ts",
|
|
58
|
+
getContents: () => `declare module '#notify/ui-driver' {
|
|
59
|
+
import type { UIDriver } from ${JSON.stringify(resolver.resolve("./types"))}
|
|
60
|
+
const driver: UIDriver
|
|
61
|
+
export default driver
|
|
62
|
+
}
|
|
63
|
+
`
|
|
64
|
+
});
|
|
65
|
+
const registerServerDriver = (name, driverType, driver) => {
|
|
66
|
+
const filename = `#notify/${name}`;
|
|
67
|
+
const template = addServerTemplate({
|
|
68
|
+
filename,
|
|
69
|
+
getContents: async () => {
|
|
70
|
+
const builtin = BUILTIN_SERVER_DRIVERS[driver];
|
|
71
|
+
if (builtin) {
|
|
72
|
+
return `export { ${builtin.exportName} as default } from ${JSON.stringify(await resolver.resolvePath(builtin.path))}`;
|
|
73
|
+
}
|
|
74
|
+
const driverPath = await resolver.resolvePath(driver, { cwd: nuxt.options.rootDir });
|
|
75
|
+
return `export { default } from ${JSON.stringify(driverPath)}`;
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
nuxt.options.nitro.alias ||= {};
|
|
79
|
+
nuxt.options.nitro.alias[`#notify/${name}`] = joinURL(template.filename, ".ts");
|
|
80
|
+
addTypeTemplate({
|
|
81
|
+
filename: `types/notify-${name}.d.ts`,
|
|
82
|
+
getContents: () => `declare module '#notify/${name}' {
|
|
83
|
+
import type { ${driverType} } from ${JSON.stringify(resolver.resolve("./types"))}
|
|
84
|
+
const driver: ${driverType}
|
|
85
|
+
export default driver
|
|
86
|
+
}
|
|
87
|
+
`
|
|
88
|
+
}, { nitro: true, nuxt: true });
|
|
89
|
+
};
|
|
90
|
+
registerServerDriver("email-driver", "EmailDriver", options.email?.driver ?? "log");
|
|
91
|
+
registerServerDriver("sms-driver", "SMSDriver", options.sms?.driver ?? "log");
|
|
92
|
+
registerServerDriver("simple-driver", "SimpleDriver", options.simple?.driver ?? "log");
|
|
93
|
+
addServerImportsDir(resolver.resolve("./runtime/server/utils"));
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
export { module$1 as default };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { UINotification } from '../../../types.js';
|
|
2
|
+
/**
|
|
3
|
+
* App-side composable for pushing `UINotification`s through the configured UI driver (e.g. the
|
|
4
|
+
* `@nuxt/ui` driver, which forwards them to `useToast`). Only registered when `notify.ui.driver`
|
|
5
|
+
* is configured - see `setup()` in the module.
|
|
6
|
+
*/
|
|
7
|
+
export declare const useNotify: () => {
|
|
8
|
+
notifications: import("vue").Ref<UINotification<unknown, import("../../../types.js").NotificationMeta<Record<string, unknown>>>[], UINotification<unknown, import("../../../types.js").NotificationMeta<Record<string, unknown>>>[]>;
|
|
9
|
+
addNotification: (notification: UINotification) => any;
|
|
10
|
+
add: (notification: UINotification) => any;
|
|
11
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { useState } from "#imports";
|
|
2
|
+
import uiDriver from "#notify/ui-driver";
|
|
3
|
+
export const useNotify = () => {
|
|
4
|
+
const notifications = useState("notify-notifications", () => []);
|
|
5
|
+
const dispatch = uiDriver();
|
|
6
|
+
const add = (notification) => {
|
|
7
|
+
notifications.value.push(notification);
|
|
8
|
+
return dispatch(notification);
|
|
9
|
+
};
|
|
10
|
+
return {
|
|
11
|
+
notifications,
|
|
12
|
+
// `addNotification` is an alias for `add` to make the API more explicit and self-documenting
|
|
13
|
+
addNotification: add,
|
|
14
|
+
add
|
|
15
|
+
};
|
|
16
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { SimpleNotification, UINotification, SMSNotification, EmailNotification, UIDriver } from '../../types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Logs the notification instead of sending it. Safe default for development/unconfigured setups,
|
|
4
|
+
* and a reference implementation for the `EmailDriver` signature.
|
|
5
|
+
*/
|
|
6
|
+
export declare const logDriver: (notification: SimpleNotification | UINotification | SMSNotification | EmailNotification) => void;
|
|
7
|
+
/**
|
|
8
|
+
* `UIDriver`-shaped version of `logDriver` - unlike the server-side drivers, UI drivers are
|
|
9
|
+
* composable factories (see `UIDriver`'s doc comment), so this just wraps `logDriver` in one to
|
|
10
|
+
* use as the built-in `'log'` UI driver. Doesn't call any Vue composables itself, so there's
|
|
11
|
+
* nothing `inject()`-sensitive here - it works the same called eagerly or lazily.
|
|
12
|
+
*/
|
|
13
|
+
export declare const logUIDriver: UIDriver;
|
|
14
|
+
export default logDriver;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { useToast } from "#imports";
|
|
2
|
+
import { defineUIDriver } from "../../server/utils/definers.js";
|
|
3
|
+
const COLOR_BY_TYPE = {
|
|
4
|
+
success: "success",
|
|
5
|
+
error: "error",
|
|
6
|
+
warning: "warning",
|
|
7
|
+
info: "info"
|
|
8
|
+
};
|
|
9
|
+
const nuxtUiDriver = defineUIDriver(() => {
|
|
10
|
+
const toast = useToast();
|
|
11
|
+
return (notification) => {
|
|
12
|
+
toast.add({
|
|
13
|
+
title: notification.title,
|
|
14
|
+
description: notification.text,
|
|
15
|
+
color: COLOR_BY_TYPE[notification?.type ?? "info"],
|
|
16
|
+
actions: notification?.actions
|
|
17
|
+
});
|
|
18
|
+
};
|
|
19
|
+
});
|
|
20
|
+
export default nuxtUiDriver;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { SimpleDriver, UIDriver, SMSDriver, EmailDriver, SimpleNotification, UINotification, SMSNotification, EmailNotification } from '../../../types.js';
|
|
2
|
+
export declare const defineSimpleNotification: <T extends SimpleNotification>(notification: T) => T;
|
|
3
|
+
export declare const defineUINotification: <T extends UINotification>(notification: T) => T;
|
|
4
|
+
export declare const defineEmailNotification: <T extends EmailNotification>(notification: T) => T;
|
|
5
|
+
export declare const defineSMSNotification: <T extends SMSNotification>(notification: T) => T;
|
|
6
|
+
export declare const defineSimpleDriver: (driver: SimpleDriver) => SimpleDriver;
|
|
7
|
+
export declare const defineUIDriver: (driver: UIDriver) => UIDriver;
|
|
8
|
+
export declare const defineSMSDriver: (driver: SMSDriver) => SMSDriver;
|
|
9
|
+
export declare const defineEmailDriver: (driver: EmailDriver) => EmailDriver;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export const defineSimpleNotification = (notification) => notification;
|
|
2
|
+
export const defineUINotification = (notification) => notification;
|
|
3
|
+
export const defineEmailNotification = (notification) => notification;
|
|
4
|
+
export const defineSMSNotification = (notification) => notification;
|
|
5
|
+
export const defineSimpleDriver = (driver) => driver;
|
|
6
|
+
export const defineUIDriver = (driver) => driver;
|
|
7
|
+
export const defineSMSDriver = (driver) => driver;
|
|
8
|
+
export const defineEmailDriver = (driver) => driver;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { EmailNotification, SMSNotification, SimpleNotification } from '../../../types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Dispatches an email notification through the driver configured on the `@frigear-nu/notify`
|
|
4
|
+
* module (`notify.email.driver`, see `#notify/email-driver`). Auto-imported wherever this module
|
|
5
|
+
* is installed, so consumers (e.g. `@frigear-nu/users`) only ever build an `EmailNotification`
|
|
6
|
+
* and call this - driver selection stays entirely with the app/consuming layer.
|
|
7
|
+
*/
|
|
8
|
+
export declare function dispatchEmailNotification(notification: EmailNotification): Promise<any>;
|
|
9
|
+
/**
|
|
10
|
+
* Dispatches an SMS notification through the driver configured on the `@frigear-nu/notify`
|
|
11
|
+
* module (`notify.sms.driver`, see `#notify/sms-driver`). Auto-imported wherever this module is
|
|
12
|
+
* installed, so consumers only ever build an `SMSNotification` and call this - driver selection
|
|
13
|
+
* stays entirely with the app/consuming layer.
|
|
14
|
+
*/
|
|
15
|
+
export declare function dispatchSMSNotification(notification: SMSNotification): Promise<void>;
|
|
16
|
+
/**
|
|
17
|
+
* Dispatches a generic, transport-agnostic notification through the driver configured on the
|
|
18
|
+
* `@frigear-nu/notify` module (`notify.simple.driver`, see `#notify/simple-driver`). Auto-imported
|
|
19
|
+
* wherever this module is installed, so consumers only ever build a `SimpleNotification` and call
|
|
20
|
+
* this - driver selection stays entirely with the app/consuming layer.
|
|
21
|
+
*/
|
|
22
|
+
export declare function dispatchSimpleNotification(notification: SimpleNotification): Promise<void>;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import smsDriver from "#notify/sms-driver";
|
|
2
|
+
import simpleDriver from "#notify/simple-driver";
|
|
3
|
+
export async function dispatchEmailNotification(notification) {
|
|
4
|
+
return import("#notify/email-driver").then(({ default: emailDriver }) => emailDriver(notification));
|
|
5
|
+
}
|
|
6
|
+
export async function dispatchSMSNotification(notification) {
|
|
7
|
+
await smsDriver(notification);
|
|
8
|
+
}
|
|
9
|
+
export async function dispatchSimpleNotification(notification) {
|
|
10
|
+
await simpleDriver(notification);
|
|
11
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export const memoryStore = () => {
|
|
2
|
+
const notifications = [];
|
|
3
|
+
return {
|
|
4
|
+
async put(notification) {
|
|
5
|
+
const existingIndex = notifications.findIndex((n) => n.id === notification.id);
|
|
6
|
+
if (existingIndex !== -1) {
|
|
7
|
+
notifications[existingIndex] = notification;
|
|
8
|
+
} else {
|
|
9
|
+
notifications.push(notification);
|
|
10
|
+
}
|
|
11
|
+
},
|
|
12
|
+
async get(id) {
|
|
13
|
+
return notifications.find((n) => n.id === id) || null;
|
|
14
|
+
},
|
|
15
|
+
async list() {
|
|
16
|
+
return [...notifications];
|
|
17
|
+
},
|
|
18
|
+
async delete(id) {
|
|
19
|
+
const index = notifications.findIndex((n) => n.id === id);
|
|
20
|
+
if (index !== -1) {
|
|
21
|
+
notifications.splice(index, 1);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
};
|
package/dist/types.d.mts
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,57 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@frigear-nu/notify",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"
|
|
5
|
-
"description": "
|
|
3
|
+
"version": "0.0.2",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "Framework-agnostic email notification driver/dispatch Nuxt module, shared across Frigear modules.",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/frigear-nu/frigear-nu.git",
|
|
10
|
+
"directory": "packages/notify"
|
|
11
|
+
},
|
|
12
|
+
"main": "./dist/module.mjs",
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"types": "./dist/types.d.mts",
|
|
16
|
+
"import": "./dist/module.mjs"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"dist"
|
|
21
|
+
],
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"@nuxt/kit": "^4.6.0",
|
|
24
|
+
"ufo": "^1.6.4"
|
|
25
|
+
},
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"@nuxt/devtools": "^3.4.2",
|
|
28
|
+
"@nuxt/eslint-config": "^1.17.0",
|
|
29
|
+
"@nuxt/module-builder": "^1.0.3",
|
|
30
|
+
"@nuxt/schema": "^4.6.0",
|
|
31
|
+
"@nuxt/test-utils": "^4.3.2",
|
|
32
|
+
"eslint": "^10.12.0",
|
|
33
|
+
"typescript": "^6.0.3",
|
|
34
|
+
"nuxt": "^4.6.0",
|
|
35
|
+
"vitest": "^5.0.1",
|
|
36
|
+
"vue-tsc": "^3.3.11"
|
|
37
|
+
},
|
|
38
|
+
"optionalDependencies": {
|
|
39
|
+
"@nuxt/ui": ">=4.10.0"
|
|
40
|
+
},
|
|
41
|
+
"scripts": {
|
|
42
|
+
"postinstall": "nuxt-module-build build --stub && nuxt prepare",
|
|
43
|
+
"build": "nuxt-module-build build",
|
|
44
|
+
"dev": "npm run dev:prepare && nuxt dev playground",
|
|
45
|
+
"dev:build": "nuxt build playground",
|
|
46
|
+
"dev:prepare": "nuxt-module-build build --stub && nuxt-module-build prepare && nuxt prepare playground",
|
|
47
|
+
"lint": "eslint .",
|
|
48
|
+
"lint:fix": "eslint . --fix"
|
|
49
|
+
},
|
|
50
|
+
"typesVersions": {
|
|
51
|
+
"*": {
|
|
52
|
+
".": [
|
|
53
|
+
"./dist/types.d.mts"
|
|
54
|
+
]
|
|
55
|
+
}
|
|
56
|
+
}
|
|
6
57
|
}
|