@ecomiq/storefront 2.1.3 → 2.1.4

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,59 +0,0 @@
1
- import { useRouterState } from '@tanstack/react-router'
2
- import {
3
- StorefrontDefinitionSchema,
4
- resolveStorefrontRoute,
5
- type StorefrontDefinition,
6
- type StorefrontPage,
7
- } from '@ecomiq/storefront-definition'
8
-
9
- export type StorefrontDefinitionRoute = {
10
- definition: StorefrontDefinition
11
- page: StorefrontPage
12
- }
13
-
14
- export function definitionRouteFor(
15
- pathname: string,
16
- loaderData: unknown,
17
- ): StorefrontDefinitionRoute | null {
18
- if (loaderData === null || typeof loaderData !== 'object') {
19
- return null
20
- }
21
- if (!('kind' in loaderData) || loaderData.kind !== 'definition') {
22
- return null
23
- }
24
- if (!('definition' in loaderData)) {
25
- return null
26
- }
27
-
28
- const parsed = StorefrontDefinitionSchema.safeParse(loaderData.definition)
29
- if (!parsed.success) return null
30
-
31
- const resolution = resolveStorefrontRoute(pathname, parsed.data.pages)
32
- if (resolution.kind !== 'page') return null
33
- const page = parsed.data.pages.find(
34
- (candidate) => candidate.id === resolution.pageId,
35
- )
36
- if (!page) return null
37
- return { definition: parsed.data, page }
38
- }
39
-
40
- export function definitionForRoute(
41
- pathname: string,
42
- loaderData: unknown,
43
- ): StorefrontDefinition | null {
44
- return definitionRouteFor(pathname, loaderData)?.definition ?? null
45
- }
46
-
47
- export function useDefinitionRoute(): StorefrontDefinitionRoute | null {
48
- return useRouterState({
49
- select: (state) =>
50
- definitionRouteFor(
51
- state.location.pathname,
52
- state.matches.at(-1)?.loaderData,
53
- ),
54
- })
55
- }
56
-
57
- export function useDefinition(): StorefrontDefinition | null {
58
- return useDefinitionRoute()?.definition ?? null
59
- }
@@ -1,173 +0,0 @@
1
- import {
2
- CloudRuntimeStoreSlugSchema,
3
- getCloudRuntimeClient,
4
- type ActiveStorefrontRuntime,
5
- } from './cloud-runtime-client'
6
- import { getStorefrontClient } from './client'
7
- import {
8
- getServerConfig,
9
- type ServerEcomiqConfig,
10
- } from './server-config-store'
11
- import {
12
- resolveStorefrontRoute,
13
- type StorefrontDefinition,
14
- type StorefrontPage,
15
- } from '@ecomiq/storefront-definition'
16
- import { storefrontPageFor } from './storefront-definition/renderer'
17
- import {
18
- emptyRendererData,
19
- resolveStorefrontDefinitionData,
20
- type ResolvedStorefrontDefinitionData,
21
- type StorefrontRuntimeCatalogClient,
22
- } from './storefront-definition/data'
23
-
24
- export {
25
- resolveStorefrontDefinitionData,
26
- type ResolvedStorefrontDefinitionData,
27
- type StorefrontRuntimeCatalogClient,
28
- } from './storefront-definition/data'
29
-
30
- export interface StorefrontRuntimeCloudClient {
31
- resolveActiveStorefront(
32
- storeSlug: string,
33
- ): Promise<ActiveStorefrontRuntime | null>
34
- }
35
-
36
- export type ActiveStorefrontHome =
37
- | {
38
- kind: 'legacy'
39
- }
40
- | {
41
- kind: 'definition'
42
- definition: StorefrontDefinition
43
- page: StorefrontPage
44
- publication: ActiveStorefrontRuntime['publication']
45
- data: ResolvedStorefrontDefinitionData
46
- }
47
-
48
- export type ActiveStorefrontPage =
49
- | {
50
- kind: 'unpublished'
51
- }
52
- | {
53
- kind: 'not-found'
54
- }
55
- | {
56
- kind: 'definition'
57
- definition: StorefrontDefinition
58
- page: StorefrontPage
59
- publication: ActiveStorefrontRuntime['publication']
60
- data: ResolvedStorefrontDefinitionData
61
- }
62
-
63
- export function deriveCloudStoreSlug(domain: string): string | null {
64
- const normalized = domain.trim().toLowerCase()
65
- if (!normalized) return null
66
-
67
- let hostname: string
68
- try {
69
- hostname = new URL(
70
- normalized.includes('://') ? normalized : `https://${normalized}`,
71
- ).hostname
72
- } catch {
73
- return null
74
- }
75
-
76
- const candidate = hostname.split('.')[0] ?? ''
77
- const parsed = CloudRuntimeStoreSlugSchema.safeParse(candidate)
78
- return parsed.success ? parsed.data : null
79
- }
80
-
81
- export interface LoadActiveStorefrontHomeDependencies {
82
- config?: ServerEcomiqConfig
83
- cloudClient?: StorefrontRuntimeCloudClient
84
- catalogClient?: StorefrontRuntimeCatalogClient
85
- }
86
-
87
- async function resolveActiveRuntime(
88
- dependencies: LoadActiveStorefrontHomeDependencies,
89
- ): Promise<ActiveStorefrontRuntime | null> {
90
- const config = dependencies.config ?? getServerConfig()
91
- const cloudConfig = config.cloud
92
- if (!cloudConfig?.baseUrl) return null
93
-
94
- const storeSlug =
95
- cloudConfig.storeSlug ?? deriveCloudStoreSlug(config.domain)
96
- if (!storeSlug) return null
97
-
98
- const cloudClient =
99
- dependencies.cloudClient ??
100
- getCloudRuntimeClient({
101
- baseUrl: cloudConfig.baseUrl,
102
- artifactBaseUrl: cloudConfig.artifactBaseUrl,
103
- timeoutMs: cloudConfig.timeoutMs,
104
- cacheTtlMs: cloudConfig.cacheTtlMs,
105
- })
106
-
107
- try {
108
- return await cloudClient.resolveActiveStorefront(storeSlug)
109
- } catch {
110
- return null
111
- }
112
- }
113
-
114
- export async function loadActiveStorefrontHome(
115
- dependencies: LoadActiveStorefrontHomeDependencies = {},
116
- ): Promise<ActiveStorefrontHome> {
117
- const runtime = await resolveActiveRuntime(dependencies)
118
- if (!runtime) return { kind: 'legacy' }
119
-
120
- const page = storefrontPageFor(runtime.definition)
121
- if (!page) return { kind: 'legacy' }
122
-
123
- const catalogClient =
124
- dependencies.catalogClient ?? getStorefrontClient()
125
- const data = await resolveStorefrontDefinitionData(
126
- runtime.definition,
127
- catalogClient,
128
- page.id,
129
- ).catch(emptyRendererData)
130
-
131
- return {
132
- kind: 'definition',
133
- definition: runtime.definition,
134
- page,
135
- publication: runtime.publication,
136
- data,
137
- }
138
- }
139
-
140
- export async function loadActiveStorefrontPage(
141
- pathname: string,
142
- dependencies: LoadActiveStorefrontHomeDependencies = {},
143
- ): Promise<ActiveStorefrontPage> {
144
- const runtime = await resolveActiveRuntime(dependencies)
145
- if (!runtime) return { kind: 'unpublished' }
146
-
147
- const resolution = resolveStorefrontRoute(
148
- pathname,
149
- runtime.definition.pages,
150
- )
151
- if (resolution.kind !== 'page') return { kind: 'not-found' }
152
-
153
- const page = runtime.definition.pages.find(
154
- (candidate) => candidate.id === resolution.pageId,
155
- )
156
- if (!page) return { kind: 'not-found' }
157
-
158
- const catalogClient =
159
- dependencies.catalogClient ?? getStorefrontClient()
160
- const data = await resolveStorefrontDefinitionData(
161
- runtime.definition,
162
- catalogClient,
163
- page.id,
164
- ).catch(emptyRendererData)
165
-
166
- return {
167
- kind: 'definition',
168
- definition: runtime.definition,
169
- page,
170
- publication: runtime.publication,
171
- data,
172
- }
173
- }