@djangocfg/nextjs 2.1.138 → 2.1.140

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,138 +0,0 @@
1
- /**
2
- * Ready-to-use Push Notification Route Handlers
3
- *
4
- * Import these in your app/api/push/ routes
5
- */
6
-
7
- import { NextRequest, NextResponse } from 'next/server';
8
-
9
- import { configureVapid, sendPushNotification, validateSubscription } from './push';
10
-
11
- // In-memory storage для demo (в production используй БД)
12
- const subscriptions = new Set<string>();
13
-
14
- /**
15
- * POST /api/push/subscribe
16
- * Save push subscription
17
- *
18
- * @example
19
- * ```typescript
20
- * // app/api/push/subscribe/route.ts
21
- * export { POST } from '@djangocfg/nextjs/pwa/server/routes';
22
- * ```
23
- */
24
- export async function handleSubscribe(request: NextRequest) {
25
- try {
26
- const subscription = await request.json();
27
-
28
- if (!validateSubscription(subscription)) {
29
- return NextResponse.json(
30
- { error: 'Invalid subscription format' },
31
- { status: 400 }
32
- );
33
- }
34
-
35
- // Сохраняем subscription (в demo просто в памяти)
36
- subscriptions.add(JSON.stringify(subscription));
37
-
38
- console.log('✅ Push subscription saved:', {
39
- endpoint: subscription.endpoint.substring(0, 50) + '...',
40
- total: subscriptions.size,
41
- });
42
-
43
- return NextResponse.json({
44
- success: true,
45
- message: 'Subscription saved',
46
- totalSubscriptions: subscriptions.size,
47
- });
48
- } catch (error: any) {
49
- console.error('Subscription error:', error);
50
-
51
- return NextResponse.json(
52
- {
53
- error: 'Failed to save subscription',
54
- details: error.message,
55
- },
56
- { status: 500 }
57
- );
58
- }
59
- }
60
-
61
- /**
62
- * GET /api/push/subscribe
63
- * Get all subscriptions (for testing)
64
- */
65
- export async function handleGetSubscriptions() {
66
- return NextResponse.json({
67
- totalSubscriptions: subscriptions.size,
68
- subscriptions: Array.from(subscriptions).map((sub) => {
69
- const parsed = JSON.parse(sub);
70
- return {
71
- endpoint: parsed.endpoint.substring(0, 50) + '...',
72
- };
73
- }),
74
- });
75
- }
76
-
77
- /**
78
- * POST /api/push/send
79
- * Send push notification
80
- *
81
- * @example
82
- * ```typescript
83
- * // app/api/push/send/route.ts
84
- * export { POST as handleSend as POST } from '@djangocfg/nextjs/pwa/server/routes';
85
- * ```
86
- */
87
- export async function handleSend(request: NextRequest) {
88
- try {
89
- const { subscription, notification } = await request.json();
90
-
91
- if (!subscription) {
92
- return NextResponse.json(
93
- { error: 'Subscription is required' },
94
- { status: 400 }
95
- );
96
- }
97
-
98
- if (!validateSubscription(subscription)) {
99
- return NextResponse.json(
100
- { error: 'Invalid subscription format' },
101
- { status: 400 }
102
- );
103
- }
104
-
105
- // Configure VAPID if not already configured
106
- configureVapid();
107
-
108
- await sendPushNotification(subscription, {
109
- title: notification?.title || 'Test Notification',
110
- body: notification?.body || 'This is a test push notification',
111
- icon: notification?.icon,
112
- badge: notification?.badge,
113
- data: notification?.data,
114
- });
115
-
116
- return NextResponse.json({
117
- success: true,
118
- message: 'Push notification sent',
119
- });
120
- } catch (error: any) {
121
- console.error('Push notification error:', error);
122
-
123
- return NextResponse.json(
124
- {
125
- error: 'Failed to send push notification',
126
- details: error.message,
127
- },
128
- { status: 500 }
129
- );
130
- }
131
- }
132
-
133
- /**
134
- * Combined route handlers
135
- * Use like: export { POST, GET } from '@djangocfg/nextjs/pwa/server/routes'
136
- */
137
- export const POST = handleSubscribe;
138
- export const GET = handleGetSubscriptions;