payload-subscribers-plugin 0.0.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.
Files changed (133) hide show
  1. package/README.md +305 -0
  2. package/dist/collections/OptInChannels.d.ts +3 -0
  3. package/dist/collections/OptInChannels.js +44 -0
  4. package/dist/collections/OptInChannels.js.map +1 -0
  5. package/dist/collections/Subscribers.d.ts +8 -0
  6. package/dist/collections/Subscribers.js +88 -0
  7. package/dist/collections/Subscribers.js.map +1 -0
  8. package/dist/collections/fields/OptedInChannels.d.ts +2 -0
  9. package/dist/collections/fields/OptedInChannels.js +12 -0
  10. package/dist/collections/fields/OptedInChannels.js.map +1 -0
  11. package/dist/components/BeforeDashboardClient.d.ts +1 -0
  12. package/dist/components/BeforeDashboardClient.js +40 -0
  13. package/dist/components/BeforeDashboardClient.js.map +1 -0
  14. package/dist/components/BeforeDashboardServer.d.ts +2 -0
  15. package/dist/components/BeforeDashboardServer.js +22 -0
  16. package/dist/components/BeforeDashboardServer.js.map +1 -0
  17. package/dist/components/BeforeDashboardServer.module.css +5 -0
  18. package/dist/components/app/RequestMagicLink.d.ts +16 -0
  19. package/dist/components/app/RequestMagicLink.js +114 -0
  20. package/dist/components/app/RequestMagicLink.js.map +1 -0
  21. package/dist/components/app/RequestMagicLink.module.css +5 -0
  22. package/dist/components/app/RequestOrSubscribe.d.ts +17 -0
  23. package/dist/components/app/RequestOrSubscribe.js +28 -0
  24. package/dist/components/app/RequestOrSubscribe.js.map +1 -0
  25. package/dist/components/app/SelectOptInChannels.d.ts +20 -0
  26. package/dist/components/app/SelectOptInChannels.js +120 -0
  27. package/dist/components/app/SelectOptInChannels.js.map +1 -0
  28. package/dist/components/app/SelectOptInChannels.module.css +5 -0
  29. package/dist/components/app/Subscribe.d.ts +18 -0
  30. package/dist/components/app/Subscribe.js +169 -0
  31. package/dist/components/app/Subscribe.js.map +1 -0
  32. package/dist/components/app/Subscribe.module.css +5 -0
  33. package/dist/components/app/SubscriberMenu.d.ts +7 -0
  34. package/dist/components/app/SubscriberMenu.js +44 -0
  35. package/dist/components/app/SubscriberMenu.js.map +1 -0
  36. package/dist/components/app/VerifyMagicLink.d.ts +23 -0
  37. package/dist/components/app/VerifyMagicLink.js +169 -0
  38. package/dist/components/app/VerifyMagicLink.js.map +1 -0
  39. package/dist/components/app/VerifyMagicLink.module.css +5 -0
  40. package/dist/components/app/helpers.d.ts +1 -0
  41. package/dist/components/app/helpers.js +5 -0
  42. package/dist/components/app/helpers.js.map +1 -0
  43. package/dist/components/app/shared.module.css +14 -0
  44. package/dist/contexts/SubscriberProvider.d.ts +15 -0
  45. package/dist/contexts/SubscriberProvider.js +105 -0
  46. package/dist/contexts/SubscriberProvider.js.map +1 -0
  47. package/dist/copied/payload-types.d.ts +395 -0
  48. package/dist/copied/payload-types.js +15 -0
  49. package/dist/copied/payload-types.js.map +1 -0
  50. package/dist/copied/payload.config.d.ts +2 -0
  51. package/dist/endpoints/customEndpointHandler.d.ts +2 -0
  52. package/dist/endpoints/customEndpointHandler.js +7 -0
  53. package/dist/endpoints/customEndpointHandler.js.map +1 -0
  54. package/dist/endpoints/getOptInChannels.d.ts +19 -0
  55. package/dist/endpoints/getOptInChannels.js +42 -0
  56. package/dist/endpoints/getOptInChannels.js.map +1 -0
  57. package/dist/endpoints/logout.d.ts +20 -0
  58. package/dist/endpoints/logout.js +60 -0
  59. package/dist/endpoints/logout.js.map +1 -0
  60. package/dist/endpoints/requestMagicLink.d.ts +20 -0
  61. package/dist/endpoints/requestMagicLink.js +122 -0
  62. package/dist/endpoints/requestMagicLink.js.map +1 -0
  63. package/dist/endpoints/subscribe.d.ts +24 -0
  64. package/dist/endpoints/subscribe.js +343 -0
  65. package/dist/endpoints/subscribe.js.map +1 -0
  66. package/dist/endpoints/subscriberAuth.d.ts +22 -0
  67. package/dist/endpoints/subscriberAuth.js +69 -0
  68. package/dist/endpoints/subscriberAuth.js.map +1 -0
  69. package/dist/endpoints/verifyMagicLink.d.ts +20 -0
  70. package/dist/endpoints/verifyMagicLink.js +142 -0
  71. package/dist/endpoints/verifyMagicLink.js.map +1 -0
  72. package/dist/exports/client.d.ts +1 -0
  73. package/dist/exports/client.js +3 -0
  74. package/dist/exports/client.js.map +1 -0
  75. package/dist/exports/index.d.ts +1 -0
  76. package/dist/exports/index.js +3 -0
  77. package/dist/exports/index.js.map +1 -0
  78. package/dist/exports/rsc.d.ts +1 -0
  79. package/dist/exports/rsc.js +3 -0
  80. package/dist/exports/rsc.js.map +1 -0
  81. package/dist/exports/ui.d.ts +11 -0
  82. package/dist/exports/ui.js +9 -0
  83. package/dist/exports/ui.js.map +1 -0
  84. package/dist/helpers/serverConfig.d.ts +4 -0
  85. package/dist/helpers/serverConfig.js +22 -0
  86. package/dist/helpers/serverConfig.js.map +1 -0
  87. package/dist/helpers/testData.d.ts +2 -0
  88. package/dist/helpers/testData.js +4 -0
  89. package/dist/helpers/testData.js.map +1 -0
  90. package/dist/helpers/token.d.ts +9 -0
  91. package/dist/helpers/token.js +20 -0
  92. package/dist/helpers/token.js.map +1 -0
  93. package/dist/helpers/verifyOptIns.d.ts +5 -0
  94. package/dist/helpers/verifyOptIns.js +33 -0
  95. package/dist/helpers/verifyOptIns.js.map +1 -0
  96. package/dist/index.d.ts +26 -0
  97. package/dist/index.js +147 -0
  98. package/dist/index.js.map +1 -0
  99. package/dist/react-hooks/useServerUrl.d.ts +3 -0
  100. package/dist/react-hooks/useServerUrl.js +19 -0
  101. package/dist/react-hooks/useServerUrl.js.map +1 -0
  102. package/dist/server-functions/serverUrl.d.ts +3 -0
  103. package/dist/server-functions/serverUrl.js +31 -0
  104. package/dist/server-functions/serverUrl.js.map +1 -0
  105. package/dist/server-functions/subscriberAuth.d.ts +11 -0
  106. package/package.json +94 -0
  107. package/src/collections/OptInChannels.ts +45 -0
  108. package/src/collections/Subscribers.ts +99 -0
  109. package/src/collections/fields/OptedInChannels.ts +12 -0
  110. package/src/components/app/RequestMagicLink.tsx +129 -0
  111. package/src/components/app/RequestOrSubscribe.tsx +58 -0
  112. package/src/components/app/SelectOptInChannels.tsx +147 -0
  113. package/src/components/app/Subscribe.tsx +190 -0
  114. package/src/components/app/SubscriberMenu.tsx +46 -0
  115. package/src/components/app/VerifyMagicLink.tsx +197 -0
  116. package/src/components/app/helpers.ts +6 -0
  117. package/src/components/app/shared.module.css +14 -0
  118. package/src/contexts/SubscriberProvider.tsx +122 -0
  119. package/src/copied/payload-types.ts +478 -0
  120. package/src/endpoints/getOptInChannels.ts +56 -0
  121. package/src/endpoints/logout.ts +104 -0
  122. package/src/endpoints/requestMagicLink.ts +139 -0
  123. package/src/endpoints/subscribe.ts +435 -0
  124. package/src/endpoints/subscriberAuth.ts +100 -0
  125. package/src/endpoints/verifyMagicLink.ts +164 -0
  126. package/src/exports/index.ts +1 -0
  127. package/src/exports/ui.ts +17 -0
  128. package/src/helpers/testData.ts +2 -0
  129. package/src/helpers/token.ts +14 -0
  130. package/src/helpers/verifyOptIns.ts +39 -0
  131. package/src/index.ts +207 -0
  132. package/src/react-hooks/useServerUrl.tsx +18 -0
  133. package/src/server-functions/serverUrl.ts +38 -0
@@ -0,0 +1,478 @@
1
+ /* tslint:disable */
2
+ /* eslint-disable */
3
+ /**
4
+ * This file was automatically generated by Payload.
5
+ * DO NOT MODIFY IT BY HAND. Instead, modify your source Payload config,
6
+ * and re-run `payload generate:types` to regenerate this file.
7
+ */
8
+
9
+ /**
10
+ * Supported timezones in IANA format.
11
+ *
12
+ * This interface was referenced by `Config`'s JSON-Schema
13
+ * via the `definition` "supportedTimezones".
14
+ */
15
+ export type SupportedTimezones =
16
+ | 'Pacific/Midway'
17
+ | 'Pacific/Niue'
18
+ | 'Pacific/Honolulu'
19
+ | 'Pacific/Rarotonga'
20
+ | 'America/Anchorage'
21
+ | 'Pacific/Gambier'
22
+ | 'America/Los_Angeles'
23
+ | 'America/Tijuana'
24
+ | 'America/Denver'
25
+ | 'America/Phoenix'
26
+ | 'America/Chicago'
27
+ | 'America/Guatemala'
28
+ | 'America/New_York'
29
+ | 'America/Bogota'
30
+ | 'America/Caracas'
31
+ | 'America/Santiago'
32
+ | 'America/Buenos_Aires'
33
+ | 'America/Sao_Paulo'
34
+ | 'Atlantic/South_Georgia'
35
+ | 'Atlantic/Azores'
36
+ | 'Atlantic/Cape_Verde'
37
+ | 'Europe/London'
38
+ | 'Europe/Berlin'
39
+ | 'Africa/Lagos'
40
+ | 'Europe/Athens'
41
+ | 'Africa/Cairo'
42
+ | 'Europe/Moscow'
43
+ | 'Asia/Riyadh'
44
+ | 'Asia/Dubai'
45
+ | 'Asia/Baku'
46
+ | 'Asia/Karachi'
47
+ | 'Asia/Tashkent'
48
+ | 'Asia/Calcutta'
49
+ | 'Asia/Dhaka'
50
+ | 'Asia/Almaty'
51
+ | 'Asia/Jakarta'
52
+ | 'Asia/Bangkok'
53
+ | 'Asia/Shanghai'
54
+ | 'Asia/Singapore'
55
+ | 'Asia/Tokyo'
56
+ | 'Asia/Seoul'
57
+ | 'Australia/Brisbane'
58
+ | 'Australia/Sydney'
59
+ | 'Pacific/Guam'
60
+ | 'Pacific/Noumea'
61
+ | 'Pacific/Auckland'
62
+ | 'Pacific/Fiji';
63
+
64
+ export interface Config {
65
+ auth: {
66
+ users: UserAuthOperations;
67
+ subscribers: SubscriberAuthOperations;
68
+ };
69
+ blocks: {};
70
+ collections: {
71
+ posts: Post;
72
+ media: Media;
73
+ users: User;
74
+ 'opt-in-channels': OptInChannel;
75
+ subscribers: Subscriber;
76
+ 'payload-kv': PayloadKv;
77
+ 'payload-locked-documents': PayloadLockedDocument;
78
+ 'payload-preferences': PayloadPreference;
79
+ 'payload-migrations': PayloadMigration;
80
+ };
81
+ collectionsJoins: {};
82
+ collectionsSelect: {
83
+ posts: PostsSelect<false> | PostsSelect<true>;
84
+ media: MediaSelect<false> | MediaSelect<true>;
85
+ users: UsersSelect<false> | UsersSelect<true>;
86
+ 'opt-in-channels': OptInChannelsSelect<false> | OptInChannelsSelect<true>;
87
+ subscribers: SubscribersSelect<false> | SubscribersSelect<true>;
88
+ 'payload-kv': PayloadKvSelect<false> | PayloadKvSelect<true>;
89
+ 'payload-locked-documents': PayloadLockedDocumentsSelect<false> | PayloadLockedDocumentsSelect<true>;
90
+ 'payload-preferences': PayloadPreferencesSelect<false> | PayloadPreferencesSelect<true>;
91
+ 'payload-migrations': PayloadMigrationsSelect<false> | PayloadMigrationsSelect<true>;
92
+ };
93
+ db: {
94
+ defaultIDType: string;
95
+ };
96
+ fallbackLocale: null;
97
+ globals: {};
98
+ globalsSelect: {};
99
+ locale: null;
100
+ user:
101
+ | (User & {
102
+ collection: 'users';
103
+ })
104
+ | (Subscriber & {
105
+ collection: 'subscribers';
106
+ });
107
+ jobs: {
108
+ tasks: unknown;
109
+ workflows: unknown;
110
+ };
111
+ }
112
+ export interface UserAuthOperations {
113
+ forgotPassword: {
114
+ email: string;
115
+ password: string;
116
+ };
117
+ login: {
118
+ email: string;
119
+ password: string;
120
+ };
121
+ registerFirstUser: {
122
+ email: string;
123
+ password: string;
124
+ };
125
+ unlock: {
126
+ email: string;
127
+ password: string;
128
+ };
129
+ }
130
+ export interface SubscriberAuthOperations {
131
+ forgotPassword: {
132
+ email: string;
133
+ password: string;
134
+ };
135
+ login: {
136
+ email: string;
137
+ password: string;
138
+ };
139
+ registerFirstUser: {
140
+ email: string;
141
+ password: string;
142
+ };
143
+ unlock: {
144
+ email: string;
145
+ password: string;
146
+ };
147
+ }
148
+ /**
149
+ * This interface was referenced by `Config`'s JSON-Schema
150
+ * via the `definition` "posts".
151
+ */
152
+ export interface Post {
153
+ id: string;
154
+ optIns?: (string | OptInChannel)[] | null;
155
+ updatedAt: string;
156
+ createdAt: string;
157
+ }
158
+ /**
159
+ * This interface was referenced by `Config`'s JSON-Schema
160
+ * via the `definition` "opt-in-channels".
161
+ */
162
+ export interface OptInChannel {
163
+ id: string;
164
+ title: string;
165
+ description?: string | null;
166
+ active: boolean;
167
+ slug?: string | null;
168
+ updatedAt: string;
169
+ createdAt: string;
170
+ }
171
+ /**
172
+ * This interface was referenced by `Config`'s JSON-Schema
173
+ * via the `definition` "media".
174
+ */
175
+ export interface Media {
176
+ id: string;
177
+ updatedAt: string;
178
+ createdAt: string;
179
+ url?: string | null;
180
+ thumbnailURL?: string | null;
181
+ filename?: string | null;
182
+ mimeType?: string | null;
183
+ filesize?: number | null;
184
+ width?: number | null;
185
+ height?: number | null;
186
+ focalX?: number | null;
187
+ focalY?: number | null;
188
+ }
189
+ /**
190
+ * This interface was referenced by `Config`'s JSON-Schema
191
+ * via the `definition` "users".
192
+ */
193
+ export interface User {
194
+ id: string;
195
+ updatedAt: string;
196
+ createdAt: string;
197
+ email: string;
198
+ resetPasswordToken?: string | null;
199
+ resetPasswordExpiration?: string | null;
200
+ salt?: string | null;
201
+ hash?: string | null;
202
+ loginAttempts?: number | null;
203
+ lockUntil?: string | null;
204
+ sessions?:
205
+ | {
206
+ id: string;
207
+ createdAt?: string | null;
208
+ expiresAt: string;
209
+ }[]
210
+ | null;
211
+ password?: string | null;
212
+ }
213
+ /**
214
+ * This interface was referenced by `Config`'s JSON-Schema
215
+ * via the `definition` "subscribers".
216
+ */
217
+ export interface Subscriber {
218
+ id: string;
219
+ firstName?: string | null;
220
+ status: 'subscribed' | 'unsubscribed' | 'pending';
221
+ source?: string | null;
222
+ verificationToken?: string | null;
223
+ verificationTokenExpires?: string | null;
224
+ optIns?: (string | OptInChannel)[] | null;
225
+ updatedAt: string;
226
+ createdAt: string;
227
+ email: string;
228
+ resetPasswordToken?: string | null;
229
+ resetPasswordExpiration?: string | null;
230
+ salt?: string | null;
231
+ hash?: string | null;
232
+ loginAttempts?: number | null;
233
+ lockUntil?: string | null;
234
+ sessions?:
235
+ | {
236
+ id: string;
237
+ createdAt?: string | null;
238
+ expiresAt: string;
239
+ }[]
240
+ | null;
241
+ password?: string | null;
242
+ }
243
+ /**
244
+ * This interface was referenced by `Config`'s JSON-Schema
245
+ * via the `definition` "payload-kv".
246
+ */
247
+ export interface PayloadKv {
248
+ id: string;
249
+ key: string;
250
+ data:
251
+ | {
252
+ [k: string]: unknown;
253
+ }
254
+ | unknown[]
255
+ | string
256
+ | number
257
+ | boolean
258
+ | null;
259
+ }
260
+ /**
261
+ * This interface was referenced by `Config`'s JSON-Schema
262
+ * via the `definition` "payload-locked-documents".
263
+ */
264
+ export interface PayloadLockedDocument {
265
+ id: string;
266
+ document?:
267
+ | ({
268
+ relationTo: 'posts';
269
+ value: string | Post;
270
+ } | null)
271
+ | ({
272
+ relationTo: 'media';
273
+ value: string | Media;
274
+ } | null)
275
+ | ({
276
+ relationTo: 'users';
277
+ value: string | User;
278
+ } | null)
279
+ | ({
280
+ relationTo: 'opt-in-channels';
281
+ value: string | OptInChannel;
282
+ } | null)
283
+ | ({
284
+ relationTo: 'subscribers';
285
+ value: string | Subscriber;
286
+ } | null);
287
+ globalSlug?: string | null;
288
+ user:
289
+ | {
290
+ relationTo: 'users';
291
+ value: string | User;
292
+ }
293
+ | {
294
+ relationTo: 'subscribers';
295
+ value: string | Subscriber;
296
+ };
297
+ updatedAt: string;
298
+ createdAt: string;
299
+ }
300
+ /**
301
+ * This interface was referenced by `Config`'s JSON-Schema
302
+ * via the `definition` "payload-preferences".
303
+ */
304
+ export interface PayloadPreference {
305
+ id: string;
306
+ user:
307
+ | {
308
+ relationTo: 'users';
309
+ value: string | User;
310
+ }
311
+ | {
312
+ relationTo: 'subscribers';
313
+ value: string | Subscriber;
314
+ };
315
+ key?: string | null;
316
+ value?:
317
+ | {
318
+ [k: string]: unknown;
319
+ }
320
+ | unknown[]
321
+ | string
322
+ | number
323
+ | boolean
324
+ | null;
325
+ updatedAt: string;
326
+ createdAt: string;
327
+ }
328
+ /**
329
+ * This interface was referenced by `Config`'s JSON-Schema
330
+ * via the `definition` "payload-migrations".
331
+ */
332
+ export interface PayloadMigration {
333
+ id: string;
334
+ name?: string | null;
335
+ batch?: number | null;
336
+ updatedAt: string;
337
+ createdAt: string;
338
+ }
339
+ /**
340
+ * This interface was referenced by `Config`'s JSON-Schema
341
+ * via the `definition` "posts_select".
342
+ */
343
+ export interface PostsSelect<T extends boolean = true> {
344
+ optIns?: T;
345
+ updatedAt?: T;
346
+ createdAt?: T;
347
+ }
348
+ /**
349
+ * This interface was referenced by `Config`'s JSON-Schema
350
+ * via the `definition` "media_select".
351
+ */
352
+ export interface MediaSelect<T extends boolean = true> {
353
+ updatedAt?: T;
354
+ createdAt?: T;
355
+ url?: T;
356
+ thumbnailURL?: T;
357
+ filename?: T;
358
+ mimeType?: T;
359
+ filesize?: T;
360
+ width?: T;
361
+ height?: T;
362
+ focalX?: T;
363
+ focalY?: T;
364
+ }
365
+ /**
366
+ * This interface was referenced by `Config`'s JSON-Schema
367
+ * via the `definition` "users_select".
368
+ */
369
+ export interface UsersSelect<T extends boolean = true> {
370
+ updatedAt?: T;
371
+ createdAt?: T;
372
+ email?: T;
373
+ resetPasswordToken?: T;
374
+ resetPasswordExpiration?: T;
375
+ salt?: T;
376
+ hash?: T;
377
+ loginAttempts?: T;
378
+ lockUntil?: T;
379
+ sessions?:
380
+ | T
381
+ | {
382
+ id?: T;
383
+ createdAt?: T;
384
+ expiresAt?: T;
385
+ };
386
+ }
387
+ /**
388
+ * This interface was referenced by `Config`'s JSON-Schema
389
+ * via the `definition` "opt-in-channels_select".
390
+ */
391
+ export interface OptInChannelsSelect<T extends boolean = true> {
392
+ title?: T;
393
+ description?: T;
394
+ active?: T;
395
+ slug?: T;
396
+ updatedAt?: T;
397
+ createdAt?: T;
398
+ }
399
+ /**
400
+ * This interface was referenced by `Config`'s JSON-Schema
401
+ * via the `definition` "subscribers_select".
402
+ */
403
+ export interface SubscribersSelect<T extends boolean = true> {
404
+ firstName?: T;
405
+ status?: T;
406
+ source?: T;
407
+ verificationToken?: T;
408
+ verificationTokenExpires?: T;
409
+ optIns?: T;
410
+ updatedAt?: T;
411
+ createdAt?: T;
412
+ email?: T;
413
+ resetPasswordToken?: T;
414
+ resetPasswordExpiration?: T;
415
+ salt?: T;
416
+ hash?: T;
417
+ loginAttempts?: T;
418
+ lockUntil?: T;
419
+ sessions?:
420
+ | T
421
+ | {
422
+ id?: T;
423
+ createdAt?: T;
424
+ expiresAt?: T;
425
+ };
426
+ }
427
+ /**
428
+ * This interface was referenced by `Config`'s JSON-Schema
429
+ * via the `definition` "payload-kv_select".
430
+ */
431
+ export interface PayloadKvSelect<T extends boolean = true> {
432
+ key?: T;
433
+ data?: T;
434
+ }
435
+ /**
436
+ * This interface was referenced by `Config`'s JSON-Schema
437
+ * via the `definition` "payload-locked-documents_select".
438
+ */
439
+ export interface PayloadLockedDocumentsSelect<T extends boolean = true> {
440
+ document?: T;
441
+ globalSlug?: T;
442
+ user?: T;
443
+ updatedAt?: T;
444
+ createdAt?: T;
445
+ }
446
+ /**
447
+ * This interface was referenced by `Config`'s JSON-Schema
448
+ * via the `definition` "payload-preferences_select".
449
+ */
450
+ export interface PayloadPreferencesSelect<T extends boolean = true> {
451
+ user?: T;
452
+ key?: T;
453
+ value?: T;
454
+ updatedAt?: T;
455
+ createdAt?: T;
456
+ }
457
+ /**
458
+ * This interface was referenced by `Config`'s JSON-Schema
459
+ * via the `definition` "payload-migrations_select".
460
+ */
461
+ export interface PayloadMigrationsSelect<T extends boolean = true> {
462
+ name?: T;
463
+ batch?: T;
464
+ updatedAt?: T;
465
+ createdAt?: T;
466
+ }
467
+ /**
468
+ * This interface was referenced by `Config`'s JSON-Schema
469
+ * via the `definition` "auth".
470
+ */
471
+ export interface Auth {
472
+ [k: string]: unknown;
473
+ }
474
+
475
+
476
+ declare module 'payload' {
477
+ export interface GeneratedTypes extends Config {}
478
+ }
@@ -0,0 +1,56 @@
1
+ import type { OptInChannel } from '../copied/payload-types.js'
2
+ import type { CollectionSlug, Endpoint, PayloadHandler } from 'payload'
3
+
4
+ import { OptInChannels as OptInChannelCollection } from '../collections/OptInChannels.js'
5
+
6
+ export type GetOptInChannelsResponse =
7
+ | {
8
+ error: string
9
+ now: string
10
+ }
11
+ | {
12
+ now: string
13
+ optInChannels: OptInChannel[]
14
+ }
15
+
16
+ /**
17
+ *
18
+ * @returns
19
+ */
20
+ export const getOptInChannelsHandler: PayloadHandler = async (req) => {
21
+ const findResults = await req.payload.find({
22
+ collection: OptInChannelCollection.slug as CollectionSlug,
23
+ depth: 2,
24
+ where: {
25
+ active: { equals: true },
26
+ },
27
+ })
28
+ // .catch((error) => {
29
+ // return Response.json({ error, now: new Date().toISOString() } as GetOptInChannelsResponse, {
30
+ // status: 400,
31
+ // })
32
+ // })
33
+
34
+ if (!findResults) {
35
+ return Response.json(
36
+ { error: 'Unknown find result', now: new Date().toISOString() } as GetOptInChannelsResponse,
37
+ { status: 400 },
38
+ )
39
+ }
40
+
41
+ return Response.json({
42
+ now: new Date().toISOString(),
43
+ optInChannels: findResults.docs,
44
+ } as GetOptInChannelsResponse)
45
+ }
46
+
47
+ /**
48
+ * getOptInChannels Endpoint Config
49
+ */
50
+ const getOptInChannelsEndpoint: Endpoint = {
51
+ handler: getOptInChannelsHandler,
52
+ method: 'get',
53
+ path: '/optinchannels',
54
+ }
55
+
56
+ export default getOptInChannelsEndpoint
@@ -0,0 +1,104 @@
1
+ import type { CollectionSlug, Endpoint, PayloadHandler } from 'payload'
2
+
3
+ import { headers as nextHeaders } from 'next/headers.js'
4
+
5
+ import { defaultCollectionSlug } from '../collections/Subscribers.js'
6
+
7
+ export type LogoutResponse =
8
+ | {
9
+ error: string
10
+ now: string
11
+ }
12
+ | {
13
+ message: string
14
+ now: string
15
+ }
16
+
17
+ /**
18
+ * createEndpointLogout
19
+ * @param options
20
+ * @returns
21
+ *
22
+ * Factory to generate the endpoint config with handler based on input option for subscribersCollectionSlug
23
+ *
24
+ */
25
+ function createEndpointLogout({
26
+ subscribersCollectionSlug = defaultCollectionSlug,
27
+ }: {
28
+ subscribersCollectionSlug: CollectionSlug
29
+ }): Endpoint {
30
+ const logoutHandler: PayloadHandler = async (req) => {
31
+ const headers = await nextHeaders()
32
+
33
+ try {
34
+ const logoutResult = await fetch(
35
+ `${req.payload.config.serverURL}/api/${subscribersCollectionSlug}/logout`,
36
+ {
37
+ headers,
38
+ method: 'POST',
39
+ },
40
+ )
41
+
42
+ const logoutResultData = await logoutResult.json()
43
+
44
+ if (logoutResult.ok) {
45
+ return Response.json({
46
+ message: logoutResultData.message,
47
+ now: new Date().toISOString(),
48
+ } as LogoutResponse)
49
+ }
50
+
51
+ if (
52
+ logoutResult.status == 400 &&
53
+ logoutResultData.errors?.map((e: { message: string }) => e.message).includes('No User')
54
+ ) {
55
+ return Response.json(
56
+ {
57
+ error: `Logout failed: 'No User'`,
58
+ now: new Date().toISOString(),
59
+ } as LogoutResponse,
60
+ {
61
+ status: 400,
62
+ },
63
+ )
64
+ }
65
+ return Response.json(
66
+ {
67
+ error: `Logout failed: ${
68
+ logoutResultData.errors
69
+ ? logoutResultData.errors?.map((e: { message: string }) => e.message).join(' // ')
70
+ : JSON.stringify(logoutResultData)
71
+ }`,
72
+ now: new Date().toISOString(),
73
+ } as LogoutResponse,
74
+ {
75
+ status: 400,
76
+ },
77
+ )
78
+ } catch (error) {
79
+ // throw new Error(`Logout failed: ${error instanceof Error ? error.message : 'Unknown error'}`)
80
+ return Response.json(
81
+ {
82
+ error: `Logout failed: ${JSON.stringify(error)}`,
83
+ now: new Date().toISOString(),
84
+ } as LogoutResponse,
85
+ {
86
+ status: 400,
87
+ },
88
+ )
89
+ }
90
+ }
91
+
92
+ /**
93
+ * logout Endpoint Config
94
+ */
95
+ const logoutEndpoint: Endpoint = {
96
+ handler: logoutHandler,
97
+ method: 'post',
98
+ path: '/logout',
99
+ }
100
+
101
+ return logoutEndpoint
102
+ }
103
+
104
+ export default createEndpointLogout