@vicacha-devs/medusa-plugin-stories 0.0.0-stage → 0.1.0

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 (143) hide show
  1. package/.medusa/server/src/__tests__/stories-plugin.spec.js +358 -0
  2. package/.medusa/server/src/admin/i18n/index.js +35 -0
  3. package/.medusa/server/src/admin/i18n/json/ar.json +196 -0
  4. package/.medusa/server/src/admin/i18n/json/de.json +196 -0
  5. package/.medusa/server/src/admin/i18n/json/en.json +196 -0
  6. package/.medusa/server/src/admin/i18n/json/es.json +196 -0
  7. package/.medusa/server/src/admin/i18n/json/fr.json +196 -0
  8. package/.medusa/server/src/admin/i18n/json/it.json +196 -0
  9. package/.medusa/server/src/admin/i18n/json/nl.json +196 -0
  10. package/.medusa/server/src/admin/i18n/json/pl.json +196 -0
  11. package/.medusa/server/src/admin/i18n/json/pt.json +196 -0
  12. package/.medusa/server/src/admin/i18n/json/tr.json +196 -0
  13. package/.medusa/server/src/admin/index.cjs +4699 -0
  14. package/.medusa/server/src/admin/index.js +4699 -0
  15. package/.medusa/server/src/api/admin/plugin/route.js +4 -0
  16. package/.medusa/server/src/api/admin/stories/[id]/hotspots/route.js +14 -0
  17. package/.medusa/server/src/api/admin/stories/[id]/route.js +27 -0
  18. package/.medusa/server/src/api/admin/stories/[id]/stickers/route.js +14 -0
  19. package/.medusa/server/src/api/admin/stories/analytics/route.js +7 -0
  20. package/.medusa/server/src/api/admin/stories/groups/[id]/route.js +23 -0
  21. package/.medusa/server/src/api/admin/stories/groups/route.js +15 -0
  22. package/.medusa/server/src/api/admin/stories/route.js +21 -0
  23. package/.medusa/server/src/api/middlewares.js +58 -0
  24. package/.medusa/server/src/api/store/plugin/route.js +4 -0
  25. package/.medusa/server/src/api/store/stories/[id]/events/route.js +12 -0
  26. package/.medusa/server/src/api/store/stories/[id]/interact/route.js +14 -0
  27. package/.medusa/server/src/api/store/stories/route.js +33 -0
  28. package/.medusa/server/src/api/validators.js +136 -0
  29. package/.medusa/server/src/hooks/index.js +213 -0
  30. package/.medusa/server/src/hooks/use-sdk-primitives.js +93 -0
  31. package/.medusa/server/src/index.js +6 -0
  32. package/.medusa/server/src/jobs/expire-ephemeral-stories.js +10 -0
  33. package/.medusa/server/src/modules/index.js +7 -0
  34. package/.medusa/server/src/modules/stories/index.js +3 -0
  35. package/.medusa/server/src/modules/story/index.js +23 -0
  36. package/.medusa/server/src/modules/story/migrations/Migration20260514000000.js +181 -0
  37. package/.medusa/server/src/modules/story/models/index.js +141 -0
  38. package/.medusa/server/src/modules/story/service.js +673 -0
  39. package/.medusa/server/src/modules/story/types.js +3 -0
  40. package/.medusa/server/src/modules/story/utils/security.js +76 -0
  41. package/.medusa/server/src/rbac.js +75 -0
  42. package/.medusa/server/src/sdk/index.js +148 -0
  43. package/.medusa/server/src/subscribers/product-deleted.js +11 -0
  44. package/.medusa/server/src/types/index.js +2 -0
  45. package/.medusa/server/src/ui/index.js +5 -0
  46. package/.medusa/server/src/ui/story-hotspot-pin.js +43 -0
  47. package/.medusa/server/src/ui/story-player-modal.js +104 -0
  48. package/.medusa/server/src/ui/story-product-sheet.js +7 -0
  49. package/.medusa/server/src/ui/story-rings-bar.js +42 -0
  50. package/.medusa/server/src/workflows/index.js +50 -0
  51. package/LICENSE +21 -0
  52. package/README.md +246 -2
  53. package/package.json +132 -3
  54. package/src/__tests__/medusa-shim.mjs +533 -0
  55. package/src/__tests__/register-ts-resolver.mjs +141 -0
  56. package/src/__tests__/stories-plugin.spec.ts +526 -0
  57. package/src/admin/README.md +94 -0
  58. package/src/admin/components/analytics-tab.tsx +225 -0
  59. package/src/admin/components/canvas-editor-tab.tsx +77 -0
  60. package/src/admin/components/hotspot-form-section.tsx +155 -0
  61. package/src/admin/components/index.ts +15 -0
  62. package/src/admin/components/overview-cards.tsx +96 -0
  63. package/src/admin/components/product-stories-card.tsx +123 -0
  64. package/src/admin/components/rbac-guard.tsx +153 -0
  65. package/src/admin/components/slide-form-section.tsx +187 -0
  66. package/src/admin/components/slide-preview-canvas.tsx +166 -0
  67. package/src/admin/components/slide-timeline-section.tsx +126 -0
  68. package/src/admin/components/sticker-form-section.tsx +201 -0
  69. package/src/admin/components/sticker-list-section.tsx +113 -0
  70. package/src/admin/components/stickers-tab.tsx +34 -0
  71. package/src/admin/components/story-ring-form.tsx +241 -0
  72. package/src/admin/components/story-rings-tab.tsx +204 -0
  73. package/src/admin/components/story-rings-table.tsx +222 -0
  74. package/src/admin/components/studio-header.tsx +67 -0
  75. package/src/admin/hooks/use-product-stories.ts +71 -0
  76. package/src/admin/hooks/use-stories-studio.ts +439 -0
  77. package/src/admin/i18n/README.md +84 -0
  78. package/src/admin/i18n/index.ts +36 -0
  79. package/src/admin/i18n/json/ar.json +196 -0
  80. package/src/admin/i18n/json/de.json +196 -0
  81. package/src/admin/i18n/json/en.json +196 -0
  82. package/src/admin/i18n/json/es.json +196 -0
  83. package/src/admin/i18n/json/fr.json +196 -0
  84. package/src/admin/i18n/json/it.json +196 -0
  85. package/src/admin/i18n/json/nl.json +196 -0
  86. package/src/admin/i18n/json/pl.json +196 -0
  87. package/src/admin/i18n/json/pt.json +196 -0
  88. package/src/admin/i18n/json/tr.json +196 -0
  89. package/src/admin/routes/stories/analytics/page.tsx +66 -0
  90. package/src/admin/routes/stories/canvas/page.tsx +101 -0
  91. package/src/admin/routes/stories/page.tsx +164 -0
  92. package/src/admin/routes/stories/rings/@create/page.tsx +61 -0
  93. package/src/admin/routes/stories/rings/page.tsx +88 -0
  94. package/src/admin/routes/stories/stickers/page.tsx +74 -0
  95. package/src/admin/tsconfig.json +23 -0
  96. package/src/admin/utils/theme-presets.ts +94 -0
  97. package/src/admin/vite-env.d.ts +1 -0
  98. package/src/admin/widgets/product-stories-widget.tsx +32 -0
  99. package/src/api/README.md +61 -0
  100. package/src/api/admin/plugin/route.ts +5 -0
  101. package/src/api/admin/stories/[id]/hotspots/route.ts +17 -0
  102. package/src/api/admin/stories/[id]/route.ts +40 -0
  103. package/src/api/admin/stories/[id]/stickers/route.ts +20 -0
  104. package/src/api/admin/stories/analytics/route.ts +8 -0
  105. package/src/api/admin/stories/groups/[id]/route.ts +30 -0
  106. package/src/api/admin/stories/groups/route.ts +18 -0
  107. package/src/api/admin/stories/route.ts +31 -0
  108. package/src/api/middlewares.ts +73 -0
  109. package/src/api/store/plugin/route.ts +5 -0
  110. package/src/api/store/stories/[id]/events/route.ts +14 -0
  111. package/src/api/store/stories/[id]/interact/route.ts +18 -0
  112. package/src/api/store/stories/route.ts +44 -0
  113. package/src/api/validators.ts +166 -0
  114. package/src/hooks/index.ts +419 -0
  115. package/src/hooks/use-sdk-primitives.ts +114 -0
  116. package/src/index.ts +14 -0
  117. package/src/jobs/README.md +16 -0
  118. package/src/jobs/expire-ephemeral-stories.ts +14 -0
  119. package/src/links/README.md +18 -0
  120. package/src/modules/README.md +46 -0
  121. package/src/modules/index.ts +15 -0
  122. package/src/modules/stories/index.ts +11 -0
  123. package/src/modules/story/index.ts +34 -0
  124. package/src/modules/story/migrations/Migration20260514000000.ts +196 -0
  125. package/src/modules/story/models/index.ts +146 -0
  126. package/src/modules/story/service.ts +1052 -0
  127. package/src/modules/story/types.ts +190 -0
  128. package/src/modules/story/utils/security.ts +100 -0
  129. package/src/providers/README.md +14 -0
  130. package/src/rbac.ts +89 -0
  131. package/src/sdk/index.ts +233 -0
  132. package/src/subscribers/README.md +16 -0
  133. package/src/subscribers/product-deleted.ts +15 -0
  134. package/src/types/index.ts +1 -0
  135. package/src/types/medusa-ui.d.ts +451 -0
  136. package/src/ui/index.ts +4 -0
  137. package/src/ui/story-hotspot-pin.tsx +122 -0
  138. package/src/ui/story-player-modal.tsx +361 -0
  139. package/src/ui/story-product-sheet.tsx +85 -0
  140. package/src/ui/story-rings-bar.tsx +113 -0
  141. package/src/workflows/README.md +19 -0
  142. package/src/workflows/index.ts +217 -0
  143. package/tsconfig.json +39 -0
@@ -0,0 +1,190 @@
1
+ export const STORIES_MODULE = "stories"
2
+ export const LEGACY_STORIES_MODULE = "Story"
3
+
4
+ export type StoryMediaType = "image" | "video"
5
+
6
+ export type StoryRingTheme =
7
+ | "instagram"
8
+ | "tiktok"
9
+ | "sunset"
10
+ | "emerald"
11
+ | "violet"
12
+ | "cyber"
13
+ | "neon"
14
+ | "gold"
15
+ | "minimal"
16
+ | "custom"
17
+
18
+ export type StoryPlacementZone =
19
+ | "home"
20
+ | "collection"
21
+ | "category"
22
+ | "product"
23
+ | "floating_bubble"
24
+ | "global"
25
+
26
+ export type StoryStickerType =
27
+ | "poll"
28
+ | "countdown"
29
+ | "coupon"
30
+ | "question_badge"
31
+
32
+ export type StoryEngagementEventType =
33
+ | "impression"
34
+ | "complete"
35
+ | "tap_forward"
36
+ | "tap_back"
37
+ | "exit"
38
+ | "hotspot_click"
39
+ | "link_click"
40
+ | "add_to_cart"
41
+ | "sticker_interact"
42
+
43
+ export interface StoryHotspotDTO {
44
+ id: string
45
+ story_id: string
46
+ product_id: string
47
+ variant_id?: string | null
48
+ product_title: string
49
+ product_handle?: string | null
50
+ product_thumbnail?: string | null
51
+ price_formatted?: string | null
52
+ price_amount?: number | null
53
+ currency_code?: string | null
54
+ x_percent: number
55
+ y_percent: number
56
+ label?: string | null
57
+ badge_style?: "pill" | "card" | "dot"
58
+ created_at?: string | Date
59
+ }
60
+
61
+ export interface StoryStickerOption {
62
+ id: string
63
+ label: string
64
+ votes: number
65
+ }
66
+
67
+ export interface StoryStickerDTO {
68
+ id: string
69
+ story_id: string
70
+ type: StoryStickerType
71
+ prompt: string
72
+ x_percent: number
73
+ y_percent: number
74
+ options?: StoryStickerOption[] | null
75
+ coupon_code?: string | null
76
+ countdown_until?: string | null
77
+ total_interactions: number
78
+ metadata?: Record<string, unknown> | null
79
+ }
80
+
81
+ /**
82
+ * Backward-compatible with `@bambiste/medusa-stories` fields (`title`, `media_type`,
83
+ * `media_url`, `thumbnail_url`, `link_url`, `link_text`, `is_active`, `display_order`, `expires_at`)
84
+ * while supporting multi-slide Story Groups, X/Y Product Hotspots, Interactive Stickers & Funnel Metrics.
85
+ */
86
+ export interface StorySlideDTO {
87
+ id: string
88
+ group_id?: string | null
89
+ title: string
90
+ caption?: string | null
91
+ media_type: StoryMediaType
92
+ media_url: string
93
+ thumbnail_url?: string | null
94
+ duration_ms: number
95
+ link_url?: string | null
96
+ link_text?: string | null
97
+ is_active: boolean
98
+ display_order: number
99
+ starts_at?: string | Date | null
100
+ expires_at?: string | Date | null
101
+ views_count: number
102
+ completions_count: number
103
+ clicks_count: number
104
+ cart_conversions_count: number
105
+ hotspots?: StoryHotspotDTO[]
106
+ stickers?: StoryStickerDTO[]
107
+ metadata?: Record<string, unknown> | null
108
+ created_at?: string | Date
109
+ updated_at?: string | Date
110
+ }
111
+
112
+ export interface StoryGroupDTO {
113
+ id: string
114
+ title: string
115
+ handle: string
116
+ subtitle?: string | null
117
+ avatar_url?: string | null
118
+ ring_theme: StoryRingTheme
119
+ custom_ring_color?: string | null
120
+ placement: StoryPlacementZone
121
+ target_entity_id?: string | null
122
+ is_pinned: boolean
123
+ is_ephemeral: boolean
124
+ is_active: boolean
125
+ display_order: number
126
+ slides?: StorySlideDTO[]
127
+ metadata?: Record<string, unknown> | null
128
+ created_at?: string | Date
129
+ updated_at?: string | Date
130
+ }
131
+
132
+ export interface StoryGroupAnalyticsSummary {
133
+ group_id: string
134
+ group_title: string
135
+ slides_count: number
136
+ impressions: number
137
+ completions: number
138
+ completion_rate_pct: number
139
+ hotspot_clicks: number
140
+ link_clicks: number
141
+ cart_conversions: number
142
+ conversion_rate_pct: number
143
+ sticker_interactions: number
144
+ }
145
+
146
+ export interface StoriesAnalyticsOverview {
147
+ total_groups: number
148
+ active_groups: number
149
+ total_slides: number
150
+ active_slides: number
151
+ total_impressions: number
152
+ total_completions: number
153
+ overall_completion_rate_pct: number
154
+ total_hotspot_clicks: number
155
+ total_link_clicks: number
156
+ total_cart_conversions: number
157
+ overall_conversion_rate_pct: number
158
+ total_sticker_interactions: number
159
+ by_group: StoryGroupAnalyticsSummary[]
160
+ top_slides: Array<{
161
+ id: string
162
+ title: string
163
+ group_title?: string | null
164
+ impressions: number
165
+ completions: number
166
+ hotspot_clicks: number
167
+ cart_conversions: number
168
+ }>
169
+ }
170
+
171
+ export interface StoriesPluginOptions {
172
+ defaultSlideDurationMs?: number
173
+ default_slide_duration_ms?: number
174
+ maxHotspotsPerSlide?: number
175
+ max_hotspots_per_slide?: number
176
+ defaultEphemeralTtlHours?: number
177
+ default_ephemeral_ttl_hours?: number
178
+ defaultRingTheme?: StoryRingTheme
179
+ default_ring_theme?: StoryRingTheme
180
+ }
181
+
182
+ export interface StoriesQueryHookOptions<TData> {
183
+ enabled?: boolean
184
+ initialData?: TData
185
+ }
186
+
187
+ export interface StoriesMutationHookOptions<TData, TVariables> {
188
+ onSuccess?: (data: TData, variables: TVariables) => void
189
+ onError?: (error: Error, variables: TVariables) => void
190
+ }
@@ -0,0 +1,100 @@
1
+ import { MedusaError } from "@medusajs/framework/utils"
2
+ import type { StoryRingTheme } from "../types"
3
+
4
+ const ALLOWED_PROTOCOLS = new Set(["http:", "https:"])
5
+
6
+ /**
7
+ * Validates and normalizes an external or relative URL for media, thumbnails, avatars, or CTA links.
8
+ * Blocks dangerous schemes (`javascript:`, `data:`, `vbscript:`, `file:`) to prevent XSS.
9
+ */
10
+ export function sanitizeHttpUrl(
11
+ rawUrl: string | undefined | null,
12
+ fieldName = "url",
13
+ allowRelative = true
14
+ ): string | null {
15
+ if (rawUrl === undefined || rawUrl === null) {
16
+ return null
17
+ }
18
+ const trimmed = String(rawUrl).trim()
19
+ if (!trimmed) {
20
+ return null
21
+ }
22
+
23
+ if (allowRelative && trimmed.startsWith("/") && !trimmed.startsWith("//")) {
24
+ return trimmed
25
+ }
26
+
27
+ let parsed: URL
28
+ try {
29
+ parsed = new URL(trimmed)
30
+ } catch {
31
+ throw new MedusaError(
32
+ MedusaError.Types.INVALID_DATA,
33
+ `Invalid ${fieldName}: must be a valid http(s) URL or relative path.`
34
+ )
35
+ }
36
+
37
+ if (!ALLOWED_PROTOCOLS.has(parsed.protocol)) {
38
+ throw new MedusaError(
39
+ MedusaError.Types.INVALID_DATA,
40
+ `Unsupported protocol "${parsed.protocol}" in ${fieldName}. Only http: and https: are allowed.`
41
+ )
42
+ }
43
+
44
+ return parsed.toString()
45
+ }
46
+
47
+ /**
48
+ * Clamps X/Y hotspot or sticker percentage coordinates strictly into [0, 100].
49
+ */
50
+ export function clampCoordinatePercent(value: number): number {
51
+ if (!Number.isFinite(value)) {
52
+ throw new MedusaError(
53
+ MedusaError.Types.INVALID_DATA,
54
+ "Coordinate percentage must be a finite number."
55
+ )
56
+ }
57
+ return Math.min(100, Math.max(0, Math.round(value * 100) / 100))
58
+ }
59
+
60
+ /**
61
+ * Normalizes a story group handle into a URL-safe slug.
62
+ */
63
+ export function slugifyStoryHandle(input: string): string {
64
+ const slug = String(input || "")
65
+ .toLowerCase()
66
+ .trim()
67
+ .replace(/[^a-z0-9]+/g, "-")
68
+ .replace(/^-+|-+$/g, "")
69
+ return slug || "highlight"
70
+ }
71
+
72
+ /**
73
+ * Validates an optional CSS hex color or gradient string so arbitrary CSS injection is prevented.
74
+ */
75
+ export function sanitizeRingColor(color?: string | null): string | null {
76
+ if (!color) return null
77
+ const trimmed = color.trim()
78
+ if (!trimmed) return null
79
+ if (/^#[0-9a-fA-F]{3,8}$/.test(trimmed)) {
80
+ return trimmed
81
+ }
82
+ throw new MedusaError(
83
+ MedusaError.Types.INVALID_DATA,
84
+ `Invalid custom_ring_color "${trimmed}". Must be a hex color (e.g. #FF5500).`
85
+ )
86
+ }
87
+
88
+ export const RING_THEME_GRADIENTS: Record<StoryRingTheme, string> = {
89
+ instagram:
90
+ "linear-gradient(135deg, #feda75 0%, #fa7e1e 25%, #d62976 50%, #962fbf 75%, #4f5bd5 100%)",
91
+ tiktok: "linear-gradient(135deg, #00f2fe 0%, #000000 50%, #ff0050 100%)",
92
+ sunset: "linear-gradient(135deg, #ff512f 0%, #dd2476 100%)",
93
+ emerald: "linear-gradient(135deg, #10b981 0%, #047857 100%)",
94
+ violet: "linear-gradient(135deg, #8b5cf6 0%, #ec4899 100%)",
95
+ cyber: "linear-gradient(135deg, #06b6d4 0%, #3b82f6 50%, #8b5cf6 100%)",
96
+ neon: "linear-gradient(135deg, #a8ff78 0%, #78ffd6 50%, #00f2fe 100%)",
97
+ gold: "linear-gradient(135deg, #f59e0b 0%, #fde68a 50%, #b45309 100%)",
98
+ minimal: "linear-gradient(135deg, #111827 0%, #6b7280 100%)",
99
+ custom: "linear-gradient(135deg, #6366f1 0%, #a855f7 100%)",
100
+ }
@@ -0,0 +1,14 @@
1
+ # Providers
2
+
3
+ Directory reserved for external module providers in `@vicacha-devs/medusa-plugin-stories`.
4
+
5
+ ```
6
+ providers/
7
+ └── README.md
8
+ ```
9
+
10
+ ---
11
+
12
+ ## Overview
13
+
14
+ `@vicacha-devs/medusa-plugin-stories` validates and renders 9:16 image and video URLs directly from Medusa's core File Module or any HTTPS CDN (`sanitizeSafeUrl` in `src/modules/story/utils/security.ts`), so no custom provider registration is required.
package/src/rbac.ts ADDED
@@ -0,0 +1,89 @@
1
+ // Auto-generated RBAC descriptors compatible with @abhi1693/medusa-rbac AccessControlOptions.
2
+ export type RbacPermissionTuple = readonly [
3
+ id: string,
4
+ label: string,
5
+ group: string,
6
+ ]
7
+ export type RbacRoutePolicy = {
8
+ method: string
9
+ path: string
10
+ permissions: string[]
11
+ superuser?: boolean
12
+ }
13
+
14
+ export const RBAC_PERMISSIONS: readonly RbacPermissionTuple[] = [
15
+ ["stories.read", "Read stories, groups & analytics", "Stories"],
16
+ ["stories.write", "Manage stories, groups, hotspots & stickers", "Stories"],
17
+ ] as const
18
+
19
+ export const RBAC_ROUTES: RbacRoutePolicy[] = [
20
+ {
21
+ method: "POST",
22
+ path: "/admin/stories/:id/hotspots",
23
+ permissions: ["stories.write"],
24
+ },
25
+ {
26
+ method: "DELETE",
27
+ path: "/admin/stories/:id/hotspots",
28
+ permissions: ["stories.write"],
29
+ },
30
+ { method: "GET", path: "/admin/stories/:id", permissions: ["stories.read"] },
31
+ {
32
+ method: "PATCH",
33
+ path: "/admin/stories/:id",
34
+ permissions: ["stories.write"],
35
+ },
36
+ { method: "PUT", path: "/admin/stories/:id", permissions: ["stories.write"] },
37
+ {
38
+ method: "DELETE",
39
+ path: "/admin/stories/:id",
40
+ permissions: ["stories.write"],
41
+ },
42
+ {
43
+ method: "POST",
44
+ path: "/admin/stories/:id/stickers",
45
+ permissions: ["stories.write"],
46
+ },
47
+ {
48
+ method: "DELETE",
49
+ path: "/admin/stories/:id/stickers",
50
+ permissions: ["stories.write"],
51
+ },
52
+ {
53
+ method: "GET",
54
+ path: "/admin/stories/analytics",
55
+ permissions: ["stories.read"],
56
+ },
57
+ {
58
+ method: "GET",
59
+ path: "/admin/stories/groups/:id",
60
+ permissions: ["stories.read"],
61
+ },
62
+ {
63
+ method: "PATCH",
64
+ path: "/admin/stories/groups/:id",
65
+ permissions: ["stories.write"],
66
+ },
67
+ {
68
+ method: "DELETE",
69
+ path: "/admin/stories/groups/:id",
70
+ permissions: ["stories.write"],
71
+ },
72
+ {
73
+ method: "GET",
74
+ path: "/admin/stories/groups",
75
+ permissions: ["stories.read"],
76
+ },
77
+ {
78
+ method: "POST",
79
+ path: "/admin/stories/groups",
80
+ permissions: ["stories.write"],
81
+ },
82
+ { method: "GET", path: "/admin/stories", permissions: ["stories.read"] },
83
+ { method: "POST", path: "/admin/stories", permissions: ["stories.write"] },
84
+ ]
85
+
86
+ export const rbacAccess = {
87
+ permissions: [...RBAC_PERMISSIONS],
88
+ routes: RBAC_ROUTES,
89
+ }
@@ -0,0 +1,233 @@
1
+ import { MedusaError } from "@medusajs/framework/utils"
2
+ import type {
3
+ CreateStoryGroupInput,
4
+ CreateStoryHotspotInput,
5
+ CreateStorySlideInput,
6
+ CreateStoryStickerInput,
7
+ InteractWithStoryStickerInput,
8
+ RecordStoryEngagementInput,
9
+ UpdateStoryGroupInput,
10
+ UpdateStorySlideInput,
11
+ } from "../api/validators"
12
+ import type {
13
+ StoriesAnalyticsOverview,
14
+ StoryGroupDTO,
15
+ StoryHotspotDTO,
16
+ StoryPlacementZone,
17
+ StorySlideDTO,
18
+ StoryStickerDTO,
19
+ } from "../modules/story/types"
20
+
21
+ export const storiesQueryKeys = {
22
+ all: ["stories"] as const,
23
+ adminState: (filters?: { product_id?: string }) =>
24
+ filters
25
+ ? (["stories", "admin", filters] as const)
26
+ : (["stories", "admin"] as const),
27
+ groups: () => ["stories", "groups"] as const,
28
+ analytics: () => ["stories", "analytics"] as const,
29
+ storefront: (filters?: {
30
+ placement?: StoryPlacementZone
31
+ target_entity_id?: string
32
+ collection_id?: string
33
+ group_id?: string
34
+ product_id?: string
35
+ }) =>
36
+ filters
37
+ ? (["stories", "store", filters] as const)
38
+ : (["stories", "store"] as const),
39
+ }
40
+
41
+ export function matchesStoriesQueryKeyPrefix(
42
+ candidateKey: readonly unknown[],
43
+ prefixKey: readonly unknown[]
44
+ ): boolean {
45
+ if (candidateKey.length < prefixKey.length) return false
46
+ for (let i = 0; i < prefixKey.length; i++) {
47
+ if (candidateKey[i] !== prefixKey[i]) return false
48
+ }
49
+ return true
50
+ }
51
+
52
+ export interface StoriesSdkOptions {
53
+ baseUrl?: string
54
+ publishableApiKey?: string
55
+ fetchImpl?: typeof fetch
56
+ }
57
+
58
+ export function createStoriesSdk(options?: StoriesSdkOptions) {
59
+ const baseUrl = (options?.baseUrl ?? "").replace(/\/+$/, "")
60
+ const fetchImpl = options?.fetchImpl ?? globalThis.fetch
61
+
62
+ async function request<T>(path: string, init?: RequestInit): Promise<T> {
63
+ const headers: Record<string, string> = {
64
+ "Content-Type": "application/json",
65
+ ...(options?.publishableApiKey
66
+ ? { "x-publishable-api-key": options.publishableApiKey }
67
+ : {}),
68
+ ...(init?.headers as Record<string, string>),
69
+ }
70
+
71
+ const res = await fetchImpl(`${baseUrl}${path}`, {
72
+ credentials: "include",
73
+ ...init,
74
+ headers,
75
+ })
76
+
77
+ const json = (await res.json().catch(() => ({}))) as Record<string, unknown>
78
+ if (!res.ok) {
79
+ throw new MedusaError(
80
+ MedusaError.Types.UNEXPECTED_STATE,
81
+ String(json.message || json.error || `HTTP ${res.status}`)
82
+ )
83
+ }
84
+ return json as T
85
+ }
86
+
87
+ return {
88
+ admin: {
89
+ listStudioState(filters?: { product_id?: string }) {
90
+ const qs = filters?.product_id
91
+ ? `?product_id=${encodeURIComponent(filters.product_id)}`
92
+ : ""
93
+ return request<{ stories: StorySlideDTO[]; groups: StoryGroupDTO[] }>(
94
+ `/admin/stories${qs}`
95
+ )
96
+ },
97
+ createSlide(payload: CreateStorySlideInput) {
98
+ return request<{ story: StorySlideDTO }>("/admin/stories", {
99
+ method: "POST",
100
+ body: JSON.stringify(payload),
101
+ })
102
+ },
103
+ updateSlide(id: string, payload: UpdateStorySlideInput) {
104
+ return request<{ story: StorySlideDTO }>(
105
+ `/admin/stories/${encodeURIComponent(id)}`,
106
+ {
107
+ method: "PATCH",
108
+ body: JSON.stringify(payload),
109
+ }
110
+ )
111
+ },
112
+ deleteSlide(id: string) {
113
+ return request<{ id: string; deleted: boolean }>(
114
+ `/admin/stories/${encodeURIComponent(id)}`,
115
+ {
116
+ method: "DELETE",
117
+ }
118
+ )
119
+ },
120
+ createGroup(payload: CreateStoryGroupInput) {
121
+ return request<{ group: StoryGroupDTO }>("/admin/stories/groups", {
122
+ method: "POST",
123
+ body: JSON.stringify(payload),
124
+ })
125
+ },
126
+ updateGroup(id: string, payload: UpdateStoryGroupInput) {
127
+ return request<{ group: StoryGroupDTO }>(
128
+ `/admin/stories/groups/${encodeURIComponent(id)}`,
129
+ {
130
+ method: "PATCH",
131
+ body: JSON.stringify(payload),
132
+ }
133
+ )
134
+ },
135
+ deleteGroup(id: string) {
136
+ return request<{ id: string; deleted: boolean }>(
137
+ `/admin/stories/groups/${encodeURIComponent(id)}`,
138
+ {
139
+ method: "DELETE",
140
+ }
141
+ )
142
+ },
143
+ addHotspot(storyId: string, payload: CreateStoryHotspotInput) {
144
+ return request<{ hotspot: StoryHotspotDTO }>(
145
+ `/admin/stories/${encodeURIComponent(storyId)}/hotspots`,
146
+ {
147
+ method: "POST",
148
+ body: JSON.stringify(payload),
149
+ }
150
+ )
151
+ },
152
+ removeHotspot(storyId: string, hotspotId: string) {
153
+ return request<{ id: string; deleted: boolean }>(
154
+ `/admin/stories/${encodeURIComponent(storyId)}/hotspots`,
155
+ {
156
+ method: "DELETE",
157
+ body: JSON.stringify({ hotspot_id: hotspotId }),
158
+ }
159
+ )
160
+ },
161
+ addSticker(storyId: string, payload: CreateStoryStickerInput) {
162
+ return request<{ sticker: StoryStickerDTO }>(
163
+ `/admin/stories/${encodeURIComponent(storyId)}/stickers`,
164
+ {
165
+ method: "POST",
166
+ body: JSON.stringify(payload),
167
+ }
168
+ )
169
+ },
170
+ removeSticker(storyId: string, stickerId: string) {
171
+ return request<{ id: string; deleted: boolean }>(
172
+ `/admin/stories/${encodeURIComponent(storyId)}/stickers`,
173
+ {
174
+ method: "DELETE",
175
+ body: JSON.stringify({ sticker_id: stickerId }),
176
+ }
177
+ )
178
+ },
179
+ getAnalytics() {
180
+ return request<{ analytics: StoriesAnalyticsOverview }>(
181
+ "/admin/stories/analytics"
182
+ )
183
+ },
184
+ },
185
+ store: {
186
+ listStories(filters?: {
187
+ placement?: StoryPlacementZone
188
+ target_entity_id?: string
189
+ collection_id?: string
190
+ group_id?: string
191
+ product_id?: string
192
+ }) {
193
+ const params = new URLSearchParams()
194
+ if (filters?.placement) params.set("placement", filters.placement)
195
+ if (filters?.target_entity_id) {
196
+ params.set("target_entity_id", filters.target_entity_id)
197
+ }
198
+ if (filters?.collection_id) {
199
+ params.set("collection_id", filters.collection_id)
200
+ }
201
+ if (filters?.group_id) params.set("group_id", filters.group_id)
202
+ if (filters?.product_id) params.set("product_id", filters.product_id)
203
+ const qs = params.toString() ? `?${params.toString()}` : ""
204
+ return request<{ stories: StorySlideDTO[]; groups: StoryGroupDTO[] }>(
205
+ `/store/stories${qs}`
206
+ )
207
+ },
208
+ recordEvent(storyId: string, payload: RecordStoryEngagementInput) {
209
+ return request<{ ok: boolean; story: StorySlideDTO }>(
210
+ `/store/stories/${encodeURIComponent(storyId)}/events`,
211
+ {
212
+ method: "POST",
213
+ body: JSON.stringify(payload),
214
+ }
215
+ )
216
+ },
217
+ interactWithSticker(
218
+ storyId: string,
219
+ payload: InteractWithStoryStickerInput
220
+ ) {
221
+ return request<{ sticker: StoryStickerDTO }>(
222
+ `/store/stories/${encodeURIComponent(storyId)}/interact`,
223
+ {
224
+ method: "POST",
225
+ body: JSON.stringify(payload),
226
+ }
227
+ )
228
+ },
229
+ },
230
+ }
231
+ }
232
+
233
+ export const defaultStoriesSdk = createStoriesSdk()
@@ -0,0 +1,16 @@
1
+ # Subscribers
2
+
3
+ Event subscribers that keep `StoriesModule` synchronized with Medusa core product lifecycle events.
4
+
5
+ ```
6
+ subscribers/
7
+ └── product-deleted.ts Handles product.deleted — cleans up StoryHotspot records
8
+ ```
9
+
10
+ ---
11
+
12
+ ## Subscriber Index
13
+
14
+ | File | Subscribed Event | Behavior |
15
+ | -------------------- | ----------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
16
+ | `product-deleted.ts` | `product.deleted` | Resolves `StoriesModuleService` (`STORIES_MODULE`) and calls `removeHotspotsByProductId(data.id)` so that any `StoryHotspot` pins referencing the deleted product are automatically removed. |
@@ -0,0 +1,15 @@
1
+ import type { SubscriberArgs, SubscriberConfig } from "@medusajs/framework"
2
+ import { resolveStoriesService } from "../modules/story"
3
+
4
+ export default async function productDeletedSubscriber({
5
+ event: { data },
6
+ container,
7
+ }: SubscriberArgs<{ id: string }>) {
8
+ if (!data?.id) return
9
+ const storiesService = resolveStoriesService(container)
10
+ await storiesService.removeHotspotsByProductId(data.id)
11
+ }
12
+
13
+ export const config: SubscriberConfig = {
14
+ event: "product.deleted",
15
+ }
@@ -0,0 +1 @@
1
+ export * from "../modules/story/types"