ellmo-ai-react 0.0.22 → 0.0.24
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/dist/EllmoApi.d.ts.map +1 -1
- package/dist/EllmoApiStatic.d.ts +4 -12
- package/dist/EllmoApiStatic.d.ts.map +1 -1
- package/dist/index.d.ts +4 -12
- package/dist/index.js +77 -73
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +77 -73
- package/dist/index.mjs.map +1 -1
- package/dist/shared.d.ts +4 -0
- package/dist/shared.d.ts.map +1 -1
- package/package.json +1 -1
package/dist/EllmoApi.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"EllmoApi.d.ts","sourceRoot":"","sources":["../src/EllmoApi.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAC;AAC1B,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;
|
|
1
|
+
{"version":3,"file":"EllmoApi.d.ts","sourceRoot":"","sources":["../src/EllmoApi.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAC;AAC1B,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AAG7C,YAAY,EAAE,YAAY,EAAE,CAAC;AAE7B,MAAM,WAAW,aAAa;IAC5B;;;;;;;;;;;;;;;;OAgBG;IACH,GAAG,EAAE,MAAM,CAAC;IACZ;;OAEG;IACH,QAAQ,EAAE,MAAM,CAAC;IACjB;;OAEG;IACH,IAAI,CAAC,EAAE,YAAY,CAAC;IACpB;;OAEG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB;;OAEG;IACH,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB;;OAEG;IACH,QAAQ,CAAC,EAAE,CAAC,IAAI,EAAE,GAAG,EAAE,KAAK,EAAE,KAAK,GAAG,IAAI,KAAK,KAAK,CAAC,SAAS,CAAC;CAChE;AAED;;;;;;;;;;;;GAYG;AACH,QAAA,MAAM,QAAQ,GAAU,0DAOrB,aAAa,KAAG,OAAO,CAAC,KAAK,CAAC,YAAY,GAAG,IAAI,CAwEnD,CAAC;AAEF,eAAe,QAAQ,CAAC"}
|
package/dist/EllmoApiStatic.d.ts
CHANGED
|
@@ -5,14 +5,6 @@ type AppContext = {
|
|
|
5
5
|
ctx: any;
|
|
6
6
|
router: any;
|
|
7
7
|
};
|
|
8
|
-
type GetStaticPropsContext = {
|
|
9
|
-
params?: Record<string, string | string[]>;
|
|
10
|
-
preview?: boolean;
|
|
11
|
-
previewData?: any;
|
|
12
|
-
locale?: string;
|
|
13
|
-
locales?: string[];
|
|
14
|
-
defaultLocale?: string;
|
|
15
|
-
};
|
|
16
8
|
/**
|
|
17
9
|
* Helper for _app.tsx to fetch global Ellmo data and merge with page props
|
|
18
10
|
* Automatically calls the page's getInitialProps and merges everything
|
|
@@ -55,7 +47,7 @@ type GetStaticPropsContext = {
|
|
|
55
47
|
*/
|
|
56
48
|
export declare function withEllmoApp({ clientId, url, type, debugging }: {
|
|
57
49
|
clientId: string;
|
|
58
|
-
url: (
|
|
50
|
+
url: () => string;
|
|
59
51
|
type?: EllmoApiType;
|
|
60
52
|
debugging?: boolean;
|
|
61
53
|
}): (appContext: AppContext) => Promise<{
|
|
@@ -109,10 +101,10 @@ export declare function withEllmoApp({ clientId, url, type, debugging }: {
|
|
|
109
101
|
*/
|
|
110
102
|
export declare function withEllmoStatic<P extends Record<string, any> = Record<string, any>>({ clientId, url, type, debugging }: {
|
|
111
103
|
clientId: string;
|
|
112
|
-
url: (
|
|
104
|
+
url: () => string;
|
|
113
105
|
type?: EllmoApiType;
|
|
114
106
|
debugging?: boolean;
|
|
115
|
-
}): (
|
|
107
|
+
}): () => Promise<{
|
|
116
108
|
props: P & {
|
|
117
109
|
ellmoData: string | null;
|
|
118
110
|
ellmoError?: string | null;
|
|
@@ -132,7 +124,7 @@ export declare function withEllmoStatic<P extends Record<string, any> = Record<s
|
|
|
132
124
|
*/
|
|
133
125
|
export declare function getEllmoStaticProps({ clientId, url, type, debugging }: {
|
|
134
126
|
clientId: string;
|
|
135
|
-
url: string;
|
|
127
|
+
url: () => string;
|
|
136
128
|
type?: EllmoApiType;
|
|
137
129
|
debugging?: boolean;
|
|
138
130
|
}): Promise<{
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"EllmoApiStatic.d.ts","sourceRoot":"","sources":["../src/EllmoApiStatic.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAkB,KAAK,YAAY,EAAE,MAAM,UAAU,CAAC;AAE7D,YAAY,EAAE,YAAY,EAAE,CAAC;AAG7B,KAAK,UAAU,GAAG;IAChB,SAAS,EAAE,GAAG,CAAC;IACf,GAAG,EAAE,GAAG,CAAC;IACT,MAAM,EAAE,GAAG,CAAC;CACb,CAAC;
|
|
1
|
+
{"version":3,"file":"EllmoApiStatic.d.ts","sourceRoot":"","sources":["../src/EllmoApiStatic.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAkB,KAAK,YAAY,EAAE,MAAM,UAAU,CAAC;AAE7D,YAAY,EAAE,YAAY,EAAE,CAAC;AAG7B,KAAK,UAAU,GAAG;IAChB,SAAS,EAAE,GAAG,CAAC;IACf,GAAG,EAAE,GAAG,CAAC;IACT,MAAM,EAAE,GAAG,CAAC;CACb,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuCG;AACH,wBAAgB,YAAY,CAAC,EAC3B,QAAQ,EACR,GAAG,EACH,IAAiB,EACjB,SAAiB,EAClB,EAAE;IACD,QAAQ,EAAE,MAAM,CAAC;IACjB,GAAG,EAAE,MAAM,MAAM,CAAC;IAClB,IAAI,CAAC,EAAE,YAAY,CAAC;IACpB,SAAS,CAAC,EAAE,OAAO,CAAC;CACrB,IACe,YAAY,UAAU;;;;;GAuBrC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0CG;AACH,wBAAg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etStaticPropsContext = {
|
|
165
|
-
params?: Record<string, string | string[]>;
|
|
166
|
-
preview?: boolean;
|
|
167
|
-
previewData?: any;
|
|
168
|
-
locale?: string;
|
|
169
|
-
locales?: string[];
|
|
170
|
-
defaultLocale?: string;
|
|
171
|
-
};
|
|
172
164
|
/**
|
|
173
165
|
* Helper for _app.tsx to fetch global Ellmo data and merge with page props
|
|
174
166
|
* Automatically calls the page's getInitialProps and merges everything
|
|
@@ -211,7 +203,7 @@ type GetStaticPropsContext = {
|
|
|
211
203
|
*/
|
|
212
204
|
declare function withEllmoApp({ clientId, url, type, debugging }: {
|
|
213
205
|
clientId: string;
|
|
214
|
-
url: (
|
|
206
|
+
url: () => string;
|
|
215
207
|
type?: EllmoApiType;
|
|
216
208
|
debugging?: boolean;
|
|
217
209
|
}): (appContext: AppContext) => Promise<{
|
|
@@ -265,10 +257,10 @@ declare function withEllmoApp({ clientId, url, type, debugging }: {
|
|
|
265
257
|
*/
|
|
266
258
|
declare function withEllmoStatic<P extends Record<string, any> = Record<string, any>>({ clientId, url, type, debugging }: {
|
|
267
259
|
clientId: string;
|
|
268
|
-
url: (
|
|
260
|
+
url: () => string;
|
|
269
261
|
type?: EllmoApiType;
|
|
270
262
|
debugging?: boolean;
|
|
271
|
-
}): (
|
|
263
|
+
}): () => Promise<{
|
|
272
264
|
props: P & {
|
|
273
265
|
ellmoData: string | null;
|
|
274
266
|
ellmoError?: string | null;
|
|
@@ -288,7 +280,7 @@ declare function withEllmoStatic<P extends Record<string, any> = Record<string,
|
|
|
288
280
|
*/
|
|
289
281
|
declare function getEllmoStaticProps({ clientId, url, type, debugging }: {
|
|
290
282
|
clientId: string;
|
|
291
|
-
url: string;
|
|
283
|
+
url: () => string;
|
|
292
284
|
type?: EllmoApiType;
|
|
293
285
|
debugging?: boolean;
|
|
294
286
|
}): Promise<{
|
package/dist/index.js
CHANGED
|
@@ -2,6 +2,75 @@
|
|
|
2
2
|
|
|
3
3
|
var React = require('react');
|
|
4
4
|
|
|
5
|
+
/** Base URL for the embedded-html API (Supabase Edge Function). */
|
|
6
|
+
const ELLMO_EMBEDDED_HTML_BASE = 'https://api-s.tryellmo.ai/functions/v1/embedded-html';
|
|
7
|
+
/** Cache revalidation time in seconds (30 minutes). Used with Next.js fetch. */
|
|
8
|
+
const CACHE_REVALIDATE_SECONDS = 1800;
|
|
9
|
+
/**
|
|
10
|
+
* Fetch data from Ellmo API
|
|
11
|
+
* Use this in getServerSideProps, getStaticProps, or getInitialProps
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```tsx
|
|
15
|
+
* // In getServerSideProps (Next.js Pages Router)
|
|
16
|
+
* export async function getServerSideProps(context) {
|
|
17
|
+
* const { data, error } = await fetchEllmoData({
|
|
18
|
+
* clientId: process.env.ELLMO_CLIENT_ID!,
|
|
19
|
+
* url: context.resolvedUrl,
|
|
20
|
+
* type: 'semantic'
|
|
21
|
+
* });
|
|
22
|
+
*
|
|
23
|
+
* return {
|
|
24
|
+
* props: { ellmoData: data, ellmoError: error }
|
|
25
|
+
* };
|
|
26
|
+
* }
|
|
27
|
+
*
|
|
28
|
+
* // In getInitialProps (older Next.js API)
|
|
29
|
+
* MyPage.getInitialProps = async (ctx) => {
|
|
30
|
+
* const { data, error } = await fetchEllmoData({
|
|
31
|
+
* clientId: process.env.NEXT_PUBLIC_ELLMO_CLIENT_ID!,
|
|
32
|
+
* url: ctx.asPath || '/',
|
|
33
|
+
* type: 'semantic'
|
|
34
|
+
* });
|
|
35
|
+
*
|
|
36
|
+
* return { ellmoData: data, ellmoError: error };
|
|
37
|
+
* };
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
async function fetchEllmoData({ clientId, url, type = 'semantic' }) {
|
|
41
|
+
try {
|
|
42
|
+
// Validate required props
|
|
43
|
+
if (!clientId) {
|
|
44
|
+
throw new Error('clientId is required. Please provide your Ellmo API client identifier.');
|
|
45
|
+
}
|
|
46
|
+
if (!url) {
|
|
47
|
+
throw new Error('url is required. Please provide the page pathname.');
|
|
48
|
+
}
|
|
49
|
+
if (url.includes('.')) {
|
|
50
|
+
// Sometimes urls contain a dot if they're images or other assets which are served by SSG (static generation)
|
|
51
|
+
return { data: null, error: null };
|
|
52
|
+
}
|
|
53
|
+
// Encode the URL path for the API call
|
|
54
|
+
const encodedUrl = encodeURIComponent(url);
|
|
55
|
+
const translatedType = type === 'structured' ? 'structured' : 'semantic';
|
|
56
|
+
// Build the API URL (embedded-html Supabase Edge Function)
|
|
57
|
+
const apiUrl = `${ELLMO_EMBEDDED_HTML_BASE}?c=${clientId}&t=${translatedType}&u=${encodedUrl}`;
|
|
58
|
+
// Fetch data with 30-min cache (Next.js data cache; other runtimes ignore next option)
|
|
59
|
+
const response = await fetch(apiUrl, {
|
|
60
|
+
next: { revalidate: CACHE_REVALIDATE_SECONDS },
|
|
61
|
+
});
|
|
62
|
+
if (!response.ok) {
|
|
63
|
+
throw new Error(`API request failed with status ${response.status}`);
|
|
64
|
+
}
|
|
65
|
+
const data = await response.text();
|
|
66
|
+
return { data, error: null };
|
|
67
|
+
}
|
|
68
|
+
catch (err) {
|
|
69
|
+
const errorMessage = err instanceof Error ? err.message : 'Unknown error occurred';
|
|
70
|
+
return { data: null, error: errorMessage };
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
5
74
|
/**
|
|
6
75
|
* Server-only Ellmo API component (React Server Component)
|
|
7
76
|
* This component fetches data during server rendering.
|
|
@@ -29,11 +98,11 @@ const EllmoApi = async ({ url, clientId, type, className = '', showErrors = fals
|
|
|
29
98
|
}
|
|
30
99
|
// Encode the URL path for the API call
|
|
31
100
|
const encodedUrl = encodeURIComponent(url);
|
|
32
|
-
// Build the API URL
|
|
33
|
-
const apiUrl =
|
|
34
|
-
// Fetch data
|
|
101
|
+
// Build the API URL (embedded-html Supabase Edge Function)
|
|
102
|
+
const apiUrl = `${ELLMO_EMBEDDED_HTML_BASE}?c=${clientId}&t=${translatedType}&u=${encodedUrl}`;
|
|
103
|
+
// Fetch data with 30-min cache (Next.js data cache)
|
|
35
104
|
const response = await fetch(apiUrl, {
|
|
36
|
-
|
|
105
|
+
next: { revalidate: CACHE_REVALIDATE_SECONDS },
|
|
37
106
|
});
|
|
38
107
|
if (!response.ok) {
|
|
39
108
|
throw new Error(`API request failed with status ${response.status}`);
|
|
@@ -134,71 +203,6 @@ const EllmoApiSSR = ({ data = null, error = null, className = '', showErrors = f
|
|
|
134
203
|
return null;
|
|
135
204
|
};
|
|
136
205
|
|
|
137
|
-
/**
|
|
138
|
-
* Fetch data from Ellmo API
|
|
139
|
-
* Use this in getServerSideProps, getStaticProps, or getInitialProps
|
|
140
|
-
*
|
|
141
|
-
* @example
|
|
142
|
-
* ```tsx
|
|
143
|
-
* // In getServerSideProps (Next.js Pages Router)
|
|
144
|
-
* export async function getServerSideProps(context) {
|
|
145
|
-
* const { data, error } = await fetchEllmoData({
|
|
146
|
-
* clientId: process.env.ELLMO_CLIENT_ID!,
|
|
147
|
-
* url: context.resolvedUrl,
|
|
148
|
-
* type: 'semantic'
|
|
149
|
-
* });
|
|
150
|
-
*
|
|
151
|
-
* return {
|
|
152
|
-
* props: { ellmoData: data, ellmoError: error }
|
|
153
|
-
* };
|
|
154
|
-
* }
|
|
155
|
-
*
|
|
156
|
-
* // In getInitialProps (older Next.js API)
|
|
157
|
-
* MyPage.getInitialProps = async (ctx) => {
|
|
158
|
-
* const { data, error } = await fetchEllmoData({
|
|
159
|
-
* clientId: process.env.NEXT_PUBLIC_ELLMO_CLIENT_ID!,
|
|
160
|
-
* url: ctx.asPath || '/',
|
|
161
|
-
* type: 'semantic'
|
|
162
|
-
* });
|
|
163
|
-
*
|
|
164
|
-
* return { ellmoData: data, ellmoError: error };
|
|
165
|
-
* };
|
|
166
|
-
* ```
|
|
167
|
-
*/
|
|
168
|
-
async function fetchEllmoData({ clientId, url, type = 'semantic' }) {
|
|
169
|
-
try {
|
|
170
|
-
// Validate required props
|
|
171
|
-
if (!clientId) {
|
|
172
|
-
throw new Error('clientId is required. Please provide your Ellmo API client identifier.');
|
|
173
|
-
}
|
|
174
|
-
if (!url) {
|
|
175
|
-
throw new Error('url is required. Please provide the page pathname.');
|
|
176
|
-
}
|
|
177
|
-
if (url.includes('.')) {
|
|
178
|
-
// Sometimes urls contain a dot if they're images or other assets which are served by SSG (static generation)
|
|
179
|
-
return { data: null, error: null };
|
|
180
|
-
}
|
|
181
|
-
// Encode the URL path for the API call
|
|
182
|
-
const encodedUrl = encodeURIComponent(url);
|
|
183
|
-
const translatedType = type === 'structured' ? 'structured' : 'semantic';
|
|
184
|
-
// Build the API URL
|
|
185
|
-
const apiUrl = `https://www.tryellmo.ai/public-api/wordpress?c=${clientId}&t=${translatedType}&u=${encodedUrl}`;
|
|
186
|
-
// Fetch data
|
|
187
|
-
const response = await fetch(apiUrl, {
|
|
188
|
-
cache: 'no-store',
|
|
189
|
-
});
|
|
190
|
-
if (!response.ok) {
|
|
191
|
-
throw new Error(`API request failed with status ${response.status}`);
|
|
192
|
-
}
|
|
193
|
-
const data = await response.text();
|
|
194
|
-
return { data, error: null };
|
|
195
|
-
}
|
|
196
|
-
catch (err) {
|
|
197
|
-
const errorMessage = err instanceof Error ? err.message : 'Unknown error occurred';
|
|
198
|
-
return { data: null, error: errorMessage };
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
|
|
202
206
|
/**
|
|
203
207
|
* Helper for _app.tsx to fetch global Ellmo data and merge with page props
|
|
204
208
|
* Automatically calls the page's getInitialProps and merges everything
|
|
@@ -244,7 +248,7 @@ function withEllmoApp({ clientId, url, type = 'semantic', debugging = false }) {
|
|
|
244
248
|
// Fetch global Ellmo data
|
|
245
249
|
const { data, error } = await fetchEllmoData({
|
|
246
250
|
clientId,
|
|
247
|
-
url: url(
|
|
251
|
+
url: url(), // Use the function to get the URL
|
|
248
252
|
type
|
|
249
253
|
});
|
|
250
254
|
// Call the page's getInitialProps if it exists
|
|
@@ -306,10 +310,10 @@ function withEllmoApp({ clientId, url, type = 'semantic', debugging = false }) {
|
|
|
306
310
|
* ```
|
|
307
311
|
*/
|
|
308
312
|
function withEllmoStatic({ clientId, url, type = 'semantic', debugging = false }) {
|
|
309
|
-
return async (
|
|
313
|
+
return async () => {
|
|
310
314
|
return getEllmoStaticProps({
|
|
311
315
|
clientId,
|
|
312
|
-
url
|
|
316
|
+
url,
|
|
313
317
|
type,
|
|
314
318
|
debugging
|
|
315
319
|
});
|
|
@@ -325,7 +329,7 @@ function withEllmoStatic({ clientId, url, type = 'semantic', debugging = false }
|
|
|
325
329
|
* Accepts a plain URL string and returns the same shape as buildEllmoProps.
|
|
326
330
|
*/
|
|
327
331
|
async function getEllmoStaticProps({ clientId, url, type = 'semantic', debugging = false }) {
|
|
328
|
-
const { data, error } = await fetchEllmoData({ clientId, url, type });
|
|
332
|
+
const { data, error } = await fetchEllmoData({ clientId, url: url(), type });
|
|
329
333
|
const props = {
|
|
330
334
|
ellmoData: data,
|
|
331
335
|
pageProps: {
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../src/EllmoApi.tsx","../src/EllmoApiSSR.tsx","../src/shared.ts","../src/EllmoApiStatic.tsx","../src/middleware.ts"],"sourcesContent":["import React from 'react';\nimport type { EllmoApiType } from './shared';\n\nexport type { EllmoApiType };\n\nexport interface EllmoApiProps {\n /**\n * The URL path to fetch data for (required).\n * \n * With middleware (recommended):\n * ```tsx\n * import { headers } from 'next/headers';\n * import { getPathnameFromHeaders } from 'ellmo-ai-react/middleware';\n * \n * const pathname = getPathnameFromHeaders(headers());\n * <EllmoApi clientId=\"...\" url={pathname || '/'} />\n * ```\n * \n * Without middleware:\n * ```tsx\n * <EllmoApi clientId=\"...\" url=\"/about\" />\n * ```\n */\n url: string;\n /**\n * The API key/client identifier (required)\n */\n clientId: string;\n /**\n * Type of data to fetch\n */\n type?: EllmoApiType;\n /**\n * Custom className for styling\n */\n className?: string;\n /**\n * Whether to show errors\n */\n showErrors?: boolean;\n /**\n * Children to render when data is available\n */\n children?: (data: any, error: Error | null) => React.ReactNode;\n}\n\n/**\n * Server-only Ellmo API component (React Server Component)\n * This component fetches data during server rendering.\n * It requires Next.js 13+ App Router or other RSC-compatible frameworks.\n * \n * @example\n * ```tsx\n * // In a server component (app/page.tsx)\n * export default async function Page() {\n * return <EllmoApi clientId=\"your-id\" url=\"/about\" />;\n * }\n * ```\n */\nconst EllmoApi = async ({\n url,\n clientId,\n type,\n className = '',\n showErrors = false,\n children\n}: EllmoApiProps): Promise<React.ReactElement | null> => {\n let data: any = null;\n let error: Error | null = null;\n\n let translatedType = type === 'structured' ? 'structured' : 'semantic';\n\n try {\n // Validate required props\n if (!clientId) {\n throw new Error('clientId is required. Please provide your Ellmo API client identifier.');\n }\n\n if (!url) {\n throw new Error('url prop is required. Please provide the page pathname.');\n }\n\n // Encode the URL path for the API call\n const encodedUrl = encodeURIComponent(url);\n\n // Build the API URL\n const apiUrl = `https://www.tryellmo.ai/public-api/wordpress?c=${clientId}&t=${translatedType}&u=${encodedUrl}`;\n\n // Fetch data during server render\n const response = await fetch(apiUrl, {\n cache: 'no-store', // Always fetch fresh data\n });\n\n if (!response.ok) {\n throw new Error(`API request failed with status ${response.status}`);\n }\n\n if (type === 'structured') {\n data = await response.text();\n } else if (type === 'semantic') {\n data = await response.text();\n } else {\n throw new Error(`Invalid type: ${type}`);\n }\n } catch (err) {\n error = err instanceof Error ? err : new Error('Unknown error occurred');\n }\n\n // If children prop is provided, use render prop pattern\n if (children) {\n return <>{children(data, error)}</>;\n }\n\n // Show errors if enabled\n if (error && showErrors) {\n return (\n <div className={`ellmo-api-error ${className}`}>\n <div>Error: {error.message}</div>\n </div>\n );\n }\n\n // Show data if showErrors is enabled (for debugging)\n if (data && showErrors) {\n return (\n <div className={`ellmo-api-data ${className}`}>\n <pre>{JSON.stringify(data, null, 2)}</pre>\n </div>\n );\n }\n\n // Invisible component by default - just fetches data during server render\n if (type === 'structured' && data) {\n return <div dangerouslySetInnerHTML={{ __html: data }} />;\n } else if (type === 'semantic' && data) {\n return <div dangerouslySetInnerHTML={{ __html: data }} />;\n }\n return null;\n};\n\nexport default EllmoApi;\n","import React from 'react';\nimport type { EllmoApiType } from './shared';\n\nexport type { EllmoApiType };\n\nexport interface EllmoApiSSRProps {\n /**\n * The pre-fetched data from the Ellmo API\n */\n data?: string | null;\n /**\n * Error from fetching data\n */\n error?: string | null;\n /**\n * Type of data to render\n */\n type?: EllmoApiType;\n /**\n * Custom className for styling\n */\n className?: string;\n /**\n * Whether to show errors\n */\n showErrors?: boolean;\n /**\n * Children render prop with data and error\n */\n children?: (data: string | null, error: string | null) => React.ReactNode;\n}\n\n/**\n * Ellmo API component for traditional SSR environments (Next.js Pages Router)\n * This component receives pre-fetched data and renders it.\n * Data fetching should be done in getServerSideProps or getStaticProps.\n * \n * @example\n * ```tsx\n * // In your page file (pages/about.tsx)\n * import { EllmoApiSSR, fetchEllmoData } from 'ellmo-ai-react';\n * \n * export async function getServerSideProps(context) {\n * const { data, error } = await fetchEllmoData({\n * clientId: 'your-client-id',\n * url: context.resolvedUrl,\n * type: 'semantic'\n * });\n * \n * return {\n * props: { ellmoData: data, ellmoError: error }\n * };\n * }\n * \n * export default function AboutPage({ ellmoData, ellmoError }) {\n * return (\n * <div>\n * <h1>About Page</h1>\n * <EllmoApiSSR data={ellmoData} error={ellmoError} />\n * </div>\n * );\n * }\n * ```\n */\nconst EllmoApiSSR: React.FC<EllmoApiSSRProps> = ({\n data = null,\n error = null,\n className = '',\n showErrors = false,\n children\n}) => {\n // If children prop is provided, use render prop pattern\n if (children) {\n return <>{children(data, error)}</>;\n }\n\n // Show errors if enabled\n if (error && showErrors) {\n return (\n <div className={`ellmo-api-error ${className}`}>\n <div>Error: {error}</div>\n </div>\n );\n }\n\n // Show data if showErrors is enabled (for debugging)\n if (data && showErrors) {\n return (\n <div className={`ellmo-api-data ${className}`}>\n <pre>{data}</pre>\n </div>\n );\n }\n\n // Render HTML content if available\n if (data && !error) {\n return <div className={className} dangerouslySetInnerHTML={{ __html: data }} />;\n }\n\n // Return null if no data\n return null;\n};\n\nexport default EllmoApiSSR;\n \n","export type EllmoApiType = 'semantic' | 'structured';\n\n/**\n * Fetch data from Ellmo API\n * Use this in getServerSideProps, getStaticProps, or getInitialProps\n * \n * @example\n * ```tsx\n * // In getServerSideProps (Next.js Pages Router)\n * export async function getServerSideProps(context) {\n * const { data, error } = await fetchEllmoData({\n * clientId: process.env.ELLMO_CLIENT_ID!,\n * url: context.resolvedUrl,\n * type: 'semantic'\n * });\n * \n * return {\n * props: { ellmoData: data, ellmoError: error }\n * };\n * }\n * \n * // In getInitialProps (older Next.js API)\n * MyPage.getInitialProps = async (ctx) => {\n * const { data, error } = await fetchEllmoData({\n * clientId: process.env.NEXT_PUBLIC_ELLMO_CLIENT_ID!,\n * url: ctx.asPath || '/',\n * type: 'semantic'\n * });\n * \n * return { ellmoData: data, ellmoError: error };\n * };\n * ```\n */\nexport async function fetchEllmoData({\n clientId,\n url,\n type = 'semantic'\n}: {\n clientId: string;\n url: string;\n type?: EllmoApiType;\n}): Promise<{ data: string | null; error: string | null }> {\n try {\n // Validate required props\n if (!clientId) {\n throw new Error('clientId is required. Please provide your Ellmo API client identifier.');\n }\n\n if (!url) {\n throw new Error('url is required. Please provide the page pathname.');\n }\n\n if (url.includes('.')) {\n // Sometimes urls contain a dot if they're images or other assets which are served by SSG (static generation)\n return { data: null, error: null };\n }\n\n // Encode the URL path for the API call\n const encodedUrl = encodeURIComponent(url);\n const translatedType = type === 'structured' ? 'structured' : 'semantic';\n\n // Build the API URL\n const apiUrl = `https://www.tryellmo.ai/public-api/wordpress?c=${clientId}&t=${translatedType}&u=${encodedUrl}`;\n\n // Fetch data\n const response = await fetch(apiUrl, {\n cache: 'no-store',\n });\n\n if (!response.ok) {\n throw new Error(`API request failed with status ${response.status}`);\n }\n\n const data = await response.text();\n return { data, error: null };\n } catch (err) {\n const errorMessage = err instanceof Error ? err.message : 'Unknown error occurred';\n return { data: null, error: errorMessage };\n }\n}\n\n","import { fetchEllmoData, type EllmoApiType } from './shared';\n\nexport type { EllmoApiType };\n\n// Type for Next.js App context\ntype AppContext = {\n Component: any;\n ctx: any;\n router: any;\n};\n\n// Type for Next.js GetStaticProps context (minimal definition to avoid next.js dependency)\ntype GetStaticPropsContext = {\n params?: Record<string, string | string[]>;\n preview?: boolean;\n previewData?: any;\n locale?: string;\n locales?: string[];\n defaultLocale?: string;\n};\n\n\n/**\n * Helper for _app.tsx to fetch global Ellmo data and merge with page props\n * Automatically calls the page's getInitialProps and merges everything\n * \n * @param clientId - Your Ellmo client ID\n * @param url - Function that returns the URL path from app context\n * @param type - Content type: 'semantic' or 'structured' (default: 'semantic')\n * @param debugging - Include pageEllmoError in props for debugging (default: false)\n * \n * Returns pageEllmoData in pageProps. If debugging is enabled, also includes pageEllmoError.\n * \n * @example\n * ```tsx\n * import { withEllmoApp } from 'ellmo-ai-react';\n * \n * function MyApp({ Component, pageProps }) {\n * return (\n * <>\n * <header>\n * <EllmoApiSSR \n * data={pageProps.pageEllmoData} \n * error={pageProps.pageEllmoError} \n * showErrors={true} // Show errors in development\n * />\n * </header>\n * <Component {...pageProps} />\n * </>\n * );\n * }\n * \n * MyApp.getInitialProps = withEllmoApp({\n * clientId: process.env.NEXT_PUBLIC_ELLMO_CLIENT_ID!,\n * url: (appContext) => '/global-header',\n * type: 'structured',\n * debugging: process.env.NODE_ENV === 'development' // Enable error prop in development\n * });\n * \n * export default MyApp;\n * ```\n */\nexport function withEllmoApp({\n clientId,\n url,\n type = 'semantic',\n debugging = false\n}: {\n clientId: string;\n url: (appContext: AppContext) => string;\n type?: EllmoApiType;\n debugging?: boolean;\n}) {\n return async (appContext: AppContext) => {\n // Fetch global Ellmo data\n const { data, error } = await fetchEllmoData({\n clientId,\n url: url(appContext), // Use the function to get the URL\n type\n });\n\n // Call the page's getInitialProps if it exists\n let pageProps = {};\n if (appContext.Component.getInitialProps) {\n pageProps = await appContext.Component.getInitialProps(appContext.ctx);\n }\n\n // Return merged props with ellmo data inside pageProps\n return {\n pageProps: {\n ...pageProps,\n pageEllmoData: data,\n ...(debugging ? { pageEllmoError: error } : {}), // Only add error if debugging is enabled\n }\n };\n };\n}\n\n/**\n * Higher-order function to wrap getStaticProps with Ellmo data fetching\n * Automatically calls the page's existing getStaticProps and merges Ellmo data\n *\n * @param clientId - Your Ellmo client ID\n * @param url - Function that returns the URL path from GetStaticPropsContext\n * @param type - Content type: 'semantic' or 'structured' (default: 'semantic')\n * @param debugging - Include ellmoError in props for debugging (default: false)\n *\n * Returns ellmoData and ellmoError in props at both root level and inside pageProps for flexibility.\n *\n * @example\n * ```tsx\n * import { withEllmoStatic } from 'ellmo-ai-react';\n *\n * export const getStaticProps = withEllmoStatic({\n * clientId: process.env.ELLMO_CLIENT_ID!,\n * url: (context) => context.params?.slug ? `/blog/${context.params.slug}` : '/',\n * type: 'semantic',\n * debugging: process.env.NODE_ENV === 'development'\n * });\n *\n * // In page component - access directly\n * export default function BlogPost({ ellmoData, ellmoError }) {\n * return (\n * <div>\n * <EllmoApiSSR data={ellmoData} error={ellmoError} />\n * <h1>Blog Post</h1>\n * </div>\n * );\n * }\n *\n * // In _app.tsx - access via pageProps\n * function MyApp({ Component, pageProps }) {\n * return (\n * <div>\n * <EllmoApiSSR data={pageProps.pageEllmoData} error={pageProps.pageEllmoError} />\n * <Component {...pageProps} />\n * </div>\n * );\n * }\n * ```\n */\nexport function withEllmoStatic<P extends Record<string, any> = Record<string, any>>({\n clientId,\n url,\n type = 'semantic',\n debugging = false\n}: {\n clientId: string;\n url: (context: GetStaticPropsContext) => string;\n type?: EllmoApiType;\n debugging?: boolean;\n}) {\n return async (context: GetStaticPropsContext) => {\n return getEllmoStaticProps({\n clientId,\n url: url(context),\n type,\n debugging\n }) as Promise<{ props: P & { ellmoData: string | null; ellmoError?: string | null; pageProps?: { pageEllmoData: string | null; pageEllmoError?: string | null } } }>;\n };\n}\n\n/**\n * Build standardized Ellmo props synchronously from provided data and error.\n * Returns ellmoData/ellmoError at root and pageEllmoData/pageEllmoError inside pageProps.\n */\n// (removed buildEllmoProps to reduce surface area)\n\n/**\n * Fetch Ellmo data and return standardized props (no Next.js dependency).\n * Accepts a plain URL string and returns the same shape as buildEllmoProps.\n */\nexport async function getEllmoStaticProps({\n clientId,\n url,\n type = 'semantic',\n debugging = false\n}: {\n clientId: string;\n url: string;\n type?: EllmoApiType;\n debugging?: boolean;\n}): Promise<{ props: { ellmoData: string | null; ellmoError?: string | null; pageProps: { pageEllmoData: string | null; pageEllmoError?: string | null } } }> {\n const { data, error } = await fetchEllmoData({ clientId, url, type });\n const props: { ellmoData: string | null; ellmoError?: string | null; pageProps: { pageEllmoData: string | null; pageEllmoError?: string | null } } = {\n ellmoData: data,\n pageProps: {\n pageEllmoData: data\n }\n };\n\n if (debugging) {\n props.ellmoError = error;\n props.pageProps.pageEllmoError = error;\n }\n\n return { props };\n}\n\n","/**\n * Custom header name for Ellmo AI to pass pathname information\n * Using 'x-ellmo-pathname' to avoid conflicts with other libraries\n */\nexport const ELLMO_PATHNAME_HEADER = 'x-ellmo-pathname';\n\n/**\n * Type definitions for Next.js middleware (to avoid requiring Next.js as a dependency)\n */\nexport interface EllmoRequest {\n nextUrl: {\n pathname: string;\n };\n}\n\nexport interface EllmoResponse {\n headers: {\n set(name: string, value: string): void;\n };\n}\n\n/**\n * Ellmo AI middleware helper for Next.js\n * \n * This is a vanilla helper that adds pathname to request headers.\n * It doesn't depend on Next.js - you provide the request/response objects.\n * \n * Usage in Next.js:\n * \n * ```ts\n * // middleware.ts\n * import { ellmoMiddleware } from 'ellmo-ai-react/middleware';\n * import { NextResponse } from 'next/server';\n * import type { NextRequest } from 'next/server';\n * \n * export function middleware(request: NextRequest) {\n * const response = NextResponse.next();\n * return ellmoMiddleware(request, response);\n * }\n * \n * export const config = {\n * matcher: [\n * '/((?!_next/static|_next/image|favicon.ico).*)',\n * ],\n * };\n * ```\n * \n * Composing with existing middleware:\n * \n * ```ts\n * export function middleware(request: NextRequest) {\n * let response = NextResponse.next();\n * \n * // Your logic here\n * if (someCondition) {\n * response = NextResponse.redirect(new URL('/login', request.url));\n * }\n * \n * // Apply Ellmo middleware\n * return ellmoMiddleware(request, response);\n * }\n * ```\n */\nexport function ellmoMiddleware<\n Req extends EllmoRequest,\n Res extends EllmoResponse\n>(\n request: Req,\n response: Res\n): Res {\n // Extract pathname from the request URL\n const pathname = request.nextUrl.pathname;\n \n // Add the pathname to a custom header\n response.headers.set(ELLMO_PATHNAME_HEADER, pathname);\n \n return response;\n}\n\n/**\n * Helper to extract pathname from headers object\n * \n * Usage in Next.js Server Components:\n * ```tsx\n * import { headers } from 'next/headers';\n * import { getPathnameFromHeaders } from 'ellmo-ai-react/middleware';\n * \n * export default function Page() {\n * const headersList = headers();\n * const pathname = getPathnameFromHeaders(headersList);\n * \n * return <EllmoApi clientId=\"your-id\" url={pathname} />;\n * }\n * ```\n */\nexport function getPathnameFromHeaders(headers: { get(name: string): string | null }): string | null {\n return headers.get(ELLMO_PATHNAME_HEADER);\n}\n\nexport default ellmoMiddleware;\n"],"names":[],"mappings":";;;;AA8CA;;;;;;;;;;;;AAYG;AACG,MAAA,QAAQ,GAAG,OAAO,EACtB,GAAG,EACH,QAAQ,EACR,IAAI,EACJ,SAAS,GAAG,EAAE,EACd,UAAU,GAAG,KAAK,EAClB,QAAQ,EACM,KAAwC;IACtD,IAAI,IAAI,GAAQ,IAAI,CAAC;IACrB,IAAI,KAAK,GAAiB,IAAI,CAAC;AAE/B,IAAA,IAAI,cAAc,GAAG,IAAI,KAAK,YAAY,GAAG,YAAY,GAAG,UAAU,CAAC;AAEvE,IAAA,IAAI;;QAEF,IAAI,CAAC,QAAQ,EAAE;AACb,YAAA,MAAM,IAAI,KAAK,CAAC,wEAAwE,CAAC,CAAC;SAC3F;QAED,IAAI,CAAC,GAAG,EAAE;AACR,YAAA,MAAM,IAAI,KAAK,CAAC,yDAAyD,CAAC,CAAC;SAC5E;;AAG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;;;AAGG;AACI,MAAM,qBAAqB,GAAG,mBAAmB;AAiBxD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAyCG;AACa,SAAA,eAAe,CAI7B,OAAY,EACZ,QAAa,EAAA;;AAGb,IAAA,MAAM,QAAQ,GAAG,OAAO,CAAC,OAAO,CAAC,QAAQ,CAAC;;IAG1C,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,qBAAqB,EAAE,QAAQ,CAAC,CAAC;AAEtD,IAAA,OAAO,QAAQ,CAAC;AAClB,CAAC;AAED;;;;;;;;;;;;;;;AAeG;AACG,SAAU,sBAAsB,CAAC,OAA6C,EAAA;AAClF,IAAA,OAAO,OAAO,CAAC,GAAG,CAAC,qBAAqB,CAAC,CAAC;AAC5C;;;;;;;;;;;;;"}
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../src/shared.ts","../src/EllmoApi.tsx","../src/EllmoApiSSR.tsx","../src/EllmoApiStatic.tsx","../src/middleware.ts"],"sourcesContent":["export type EllmoApiType = 'semantic' | 'structured';\n\n/** Base URL for the embedded-html API (Supabase Edge Function). */\nexport const ELLMO_EMBEDDED_HTML_BASE =\n 'https://api-s.tryellmo.ai/functions/v1/embedded-html';\n\n/** Cache revalidation time in seconds (30 minutes). Used with Next.js fetch. */\nexport const CACHE_REVALIDATE_SECONDS = 1800;\n\n/**\n * Fetch data from Ellmo API\n * Use this in getServerSideProps, getStaticProps, or getInitialProps\n * \n * @example\n * ```tsx\n * // In getServerSideProps (Next.js Pages Router)\n * export async function getServerSideProps(context) {\n * const { data, error } = await fetchEllmoData({\n * clientId: process.env.ELLMO_CLIENT_ID!,\n * url: context.resolvedUrl,\n * type: 'semantic'\n * });\n * \n * return {\n * props: { ellmoData: data, ellmoError: error }\n * };\n * }\n * \n * // In getInitialProps (older Next.js API)\n * MyPage.getInitialProps = async (ctx) => {\n * const { data, error } = await fetchEllmoData({\n * clientId: process.env.NEXT_PUBLIC_ELLMO_CLIENT_ID!,\n * url: ctx.asPath || '/',\n * type: 'semantic'\n * });\n * \n * return { ellmoData: data, ellmoError: error };\n * };\n * ```\n */\nexport async function fetchEllmoData({\n clientId,\n url,\n type = 'semantic'\n}: {\n clientId: string;\n url: string;\n type?: EllmoApiType;\n}): Promise<{ data: string | null; error: string | null }> {\n try {\n // Validate required props\n if (!clientId) {\n throw new Error('clientId is required. Please provide your Ellmo API client identifier.');\n }\n\n if (!url) {\n throw new Error('url is required. Please provide the page pathname.');\n }\n\n if (url.includes('.')) {\n // Sometimes urls contain a dot if they're images or other assets which are served by SSG (static generation)\n return { data: null, error: null };\n }\n\n // Encode the URL path for the API call\n const encodedUrl = encodeURIComponent(url);\n const translatedType = type === 'structured' ? 'structured' : 'semantic';\n\n // Build the API URL (embedded-html Supabase Edge Function)\n const apiUrl = `${ELLMO_EMBEDDED_HTML_BASE}?c=${clientId}&t=${translatedType}&u=${encodedUrl}`;\n\n // Fetch data with 30-min cache (Next.js data cache; other runtimes ignore next option)\n const response = await fetch(apiUrl, {\n next: { revalidate: CACHE_REVALIDATE_SECONDS },\n } as RequestInit);\n\n if (!response.ok) {\n throw new Error(`API request failed with status ${response.status}`);\n }\n\n const data = await response.text();\n return { data, error: null };\n } catch (err) {\n const errorMessage = err instanceof Error ? err.message : 'Unknown error occurred';\n return { data: null, error: errorMessage };\n }\n}\n\n","import React from 'react';\nimport type { EllmoApiType } from './shared';\nimport { ELLMO_EMBEDDED_HTML_BASE, CACHE_REVALIDATE_SECONDS } from './shared';\n\nexport type { EllmoApiType };\n\nexport interface EllmoApiProps {\n /**\n * The URL path to fetch data for (required).\n * \n * With middleware (recommended):\n * ```tsx\n * import { headers } from 'next/headers';\n * import { getPathnameFromHeaders } from 'ellmo-ai-react/middleware';\n * \n * const pathname = getPathnameFromHeaders(headers());\n * <EllmoApi clientId=\"...\" url={pathname || '/'} />\n * ```\n * \n * Without middleware:\n * ```tsx\n * <EllmoApi clientId=\"...\" url=\"/about\" />\n * ```\n */\n url: string;\n /**\n * The API key/client identifier (required)\n */\n clientId: string;\n /**\n * Type of data to fetch\n */\n type?: EllmoApiType;\n /**\n * Custom className for styling\n */\n className?: string;\n /**\n * Whether to show errors\n */\n showErrors?: boolean;\n /**\n * Children to render when data is available\n */\n children?: (data: any, error: Error | null) => React.ReactNode;\n}\n\n/**\n * Server-only Ellmo API component (React Server Component)\n * This component fetches data during server rendering.\n * It requires Next.js 13+ App Router or other RSC-compatible frameworks.\n * \n * @example\n * ```tsx\n * // In a server component (app/page.tsx)\n * export default async function Page() {\n * return <EllmoApi clientId=\"your-id\" url=\"/about\" />;\n * }\n * ```\n */\nconst EllmoApi = async ({\n url,\n clientId,\n type,\n className = '',\n showErrors = false,\n children\n}: EllmoApiProps): Promise<React.ReactElement | null> => {\n let data: any = null;\n let error: Error | null = null;\n\n let translatedType = type === 'structured' ? 'structured' : 'semantic';\n\n try {\n // Validate required props\n if (!clientId) {\n throw new Error('clientId is required. Please provide your Ellmo API client identifier.');\n }\n\n if (!url) {\n throw new Error('url prop is required. Please provide the page pathname.');\n }\n\n // Encode the URL path for the API call\n const encodedUrl = encodeURIComponent(url);\n\n // Build the API URL (embedded-html Supabase Edge Function)\n const apiUrl = `${ELLMO_EMBEDDED_HTML_BASE}?c=${clientId}&t=${translatedType}&u=${encodedUrl}`;\n\n // Fetch data with 30-min cache (Next.js data cache)\n const response = await fetch(apiUrl, {\n next: { revalidate: CACHE_REVALIDATE_SECONDS },\n } as RequestInit);\n\n if (!response.ok) {\n throw new Error(`API request failed with status ${response.status}`);\n }\n\n if (type === 'structured') {\n data = await response.text();\n } else if (type === 'semantic') {\n data = await response.text();\n } else {\n throw new Error(`Invalid type: ${type}`);\n }\n } catch (err) {\n error = err instanceof Error ? err : new Error('Unknown error occurred');\n }\n\n // If children prop is provided, use render prop pattern\n if (children) {\n return <>{children(data, error)}</>;\n }\n\n // Show errors if enabled\n if (error && showErrors) {\n return (\n <div className={`ellmo-api-error ${className}`}>\n <div>Error: {error.message}</div>\n </div>\n );\n }\n\n // Show data if showErrors is enabled (for debugging)\n if (data && showErrors) {\n return (\n <div className={`ellmo-api-data ${className}`}>\n <pre>{JSON.stringify(data, null, 2)}</pre>\n </div>\n );\n }\n\n // Invisible component by default - just fetches data during server render\n if (type === 'structured' && data) {\n return <div dangerouslySetInnerHTML={{ __html: data }} />;\n } else if (type === 'semantic' && data) {\n return <div dangerouslySetInnerHTML={{ __html: data }} />;\n }\n return null;\n};\n\nexport default EllmoApi;\n","import React from 'react';\nimport type { EllmoApiType } from './shared';\n\nexport type { EllmoApiType };\n\nexport interface EllmoApiSSRProps {\n /**\n * The pre-fetched data from the Ellmo API\n */\n data?: string | null;\n /**\n * Error from fetching data\n */\n error?: string | null;\n /**\n * Type of data to render\n */\n type?: EllmoApiType;\n /**\n * Custom className for styling\n */\n className?: string;\n /**\n * Whether to show errors\n */\n showErrors?: boolean;\n /**\n * Children render prop with data and error\n */\n children?: (data: string | null, error: string | null) => React.ReactNode;\n}\n\n/**\n * Ellmo API component for traditional SSR environments (Next.js Pages Router)\n * This component receives pre-fetched data and renders it.\n * Data fetching should be done in getServerSideProps or getStaticProps.\n * \n * @example\n * ```tsx\n * // In your page file (pages/about.tsx)\n * import { EllmoApiSSR, fetchEllmoData } from 'ellmo-ai-react';\n * \n * export async function getServerSideProps(context) {\n * const { data, error } = await fetchEllmoData({\n * clientId: 'your-client-id',\n * url: context.resolvedUrl,\n * type: 'semantic'\n * });\n * \n * return {\n * props: { ellmoData: data, ellmoError: error }\n * };\n * }\n * \n * export default function AboutPage({ ellmoData, ellmoError }) {\n * return (\n * <div>\n * <h1>About Page</h1>\n * <EllmoApiSSR data={ellmoData} error={ellmoError} />\n * </div>\n * );\n * }\n * ```\n */\nconst EllmoApiSSR: React.FC<EllmoApiSSRProps> = ({\n data = null,\n error = null,\n className = '',\n showErrors = false,\n children\n}) => {\n // If children prop is provided, use render prop pattern\n if (children) {\n return <>{children(data, error)}</>;\n }\n\n // Show errors if enabled\n if (error && showErrors) {\n return (\n <div className={`ellmo-api-error ${className}`}>\n <div>Error: {error}</div>\n </div>\n );\n }\n\n // Show data if showErrors is enabled (for debugging)\n if (data && showErrors) {\n return (\n <div className={`ellmo-api-data ${className}`}>\n <pre>{data}</pre>\n </div>\n );\n }\n\n // Render HTML content if available\n if (data && !error) {\n return <div className={className} dangerouslySetInnerHTML={{ __html: data }} />;\n }\n\n // Return null if no data\n return null;\n};\n\nexport default EllmoApiSSR;\n \n","import { fetchEllmoData, type EllmoApiType } from './shared';\n\nexport type { EllmoApiType };\n\n// Type for Next.js App context\ntype AppContext = {\n Component: any;\n ctx: any;\n router: any;\n};\n\n/**\n * Helper for _app.tsx to fetch global Ellmo data and merge with page props\n * Automatically calls the page's getInitialProps and merges everything\n * \n * @param clientId - Your Ellmo client ID\n * @param url - Function that returns the URL path from app context\n * @param type - Content type: 'semantic' or 'structured' (default: 'semantic')\n * @param debugging - Include pageEllmoError in props for debugging (default: false)\n * \n * Returns pageEllmoData in pageProps. If debugging is enabled, also includes pageEllmoError.\n * \n * @example\n * ```tsx\n * import { withEllmoApp } from 'ellmo-ai-react';\n * \n * function MyApp({ Component, pageProps }) {\n * return (\n * <>\n * <header>\n * <EllmoApiSSR \n * data={pageProps.pageEllmoData} \n * error={pageProps.pageEllmoError} \n * showErrors={true} // Show errors in development\n * />\n * </header>\n * <Component {...pageProps} />\n * </>\n * );\n * }\n * \n * MyApp.getInitialProps = withEllmoApp({\n * clientId: process.env.NEXT_PUBLIC_ELLMO_CLIENT_ID!,\n * url: (appContext) => '/global-header',\n * type: 'structured',\n * debugging: process.env.NODE_ENV === 'development' // Enable error prop in development\n * });\n * \n * export default MyApp;\n * ```\n */\nexport function withEllmoApp({\n clientId,\n url,\n type = 'semantic',\n debugging = false\n}: {\n clientId: string;\n url: () => string;\n type?: EllmoApiType;\n debugging?: boolean;\n}) {\n return async (appContext: AppContext) => {\n // Fetch global Ellmo data\n const { data, error } = await fetchEllmoData({\n clientId,\n url: url(), // Use the function to get the URL\n type\n });\n\n // Call the page's getInitialProps if it exists\n let pageProps = {};\n if (appContext.Component.getInitialProps) {\n pageProps = await appContext.Component.getInitialProps(appContext.ctx);\n }\n\n // Return merged props with ellmo data inside pageProps\n return {\n pageProps: {\n ...pageProps,\n pageEllmoData: data,\n ...(debugging ? { pageEllmoError: error } : {}), // Only add error if debugging is enabled\n }\n };\n };\n}\n\n/**\n * Higher-order function to wrap getStaticProps with Ellmo data fetching\n * Automatically calls the page's existing getStaticProps and merges Ellmo data\n *\n * @param clientId - Your Ellmo client ID\n * @param url - Function that returns the URL path from GetStaticPropsContext\n * @param type - Content type: 'semantic' or 'structured' (default: 'semantic')\n * @param debugging - Include ellmoError in props for debugging (default: false)\n *\n * Returns ellmoData and ellmoError in props at both root level and inside pageProps for flexibility.\n *\n * @example\n * ```tsx\n * import { withEllmoStatic } from 'ellmo-ai-react';\n *\n * export const getStaticProps = withEllmoStatic({\n * clientId: process.env.ELLMO_CLIENT_ID!,\n * url: (context) => context.params?.slug ? `/blog/${context.params.slug}` : '/',\n * type: 'semantic',\n * debugging: process.env.NODE_ENV === 'development'\n * });\n *\n * // In page component - access directly\n * export default function BlogPost({ ellmoData, ellmoError }) {\n * return (\n * <div>\n * <EllmoApiSSR data={ellmoData} error={ellmoError} />\n * <h1>Blog Post</h1>\n * </div>\n * );\n * }\n *\n * // In _app.tsx - access via pageProps\n * function MyApp({ Component, pageProps }) {\n * return (\n * <div>\n * <EllmoApiSSR data={pageProps.pageEllmoData} error={pageProps.pageEllmoError} />\n * <Component {...pageProps} />\n * </div>\n * );\n * }\n * ```\n */\nexport function withEllmoStatic<P extends Record<string, any> = Record<string, any>>({\n clientId,\n url,\n type = 'semantic',\n debugging = false\n}: {\n clientId: string;\n url: () => string;\n type?: EllmoApiType;\n debugging?: boolean;\n}) {\n return async () => {\n return getEllmoStaticProps({\n clientId,\n url,\n type,\n debugging\n }) as Promise<{ props: P & { ellmoData: string | null; ellmoError?: string | null; pageProps?: { pageEllmoData: string | null; pageEllmoError?: string | null } } }>;\n };\n}\n\n/**\n * Build standardized Ellmo props synchronously from provided data and error.\n * Returns ellmoData/ellmoError at root and pageEllmoData/pageEllmoError inside pageProps.\n */\n// (removed buildEllmoProps to reduce surface area)\n\n/**\n * Fetch Ellmo data and return standardized props (no Next.js dependency).\n * Accepts a plain URL string and returns the same shape as buildEllmoProps.\n */\nexport async function getEllmoStaticProps({\n clientId,\n url,\n type = 'semantic',\n debugging = false\n}: {\n clientId: string;\n url: () => string;\n type?: EllmoApiType;\n debugging?: boolean;\n}): Promise<{ props: { ellmoData: string | null; ellmoError?: string | null; pageProps: { pageEllmoData: string | null; pageEllmoError?: string | null } } }> {\n const { data, error } = await fetchEllmoData({ clientId, url: url(), type });\n const props: { ellmoData: string | null; ellmoError?: string | null; pageProps: { pageEllmoData: string | null; pageEllmoError?: string | null } } = {\n ellmoData: data,\n pageProps: {\n pageEllmoData: data\n }\n };\n\n if (debugging) {\n props.ellmoError = error;\n props.pageProps.pageEllmoError = error;\n }\n\n return { props };\n}\n\n","/**\n * Custom header name for Ellmo AI to pass pathname information\n * Using 'x-ellmo-pathname' to avoid conflicts with other libraries\n */\nexport const ELLMO_PATHNAME_HEADER = 'x-ellmo-pathname';\n\n/**\n * Type definitions for Next.js middleware (to avoid requiring Next.js as a dependency)\n */\nexport interface EllmoRequest {\n nextUrl: {\n pathname: string;\n };\n}\n\nexport interface EllmoResponse {\n headers: {\n set(name: string, value: string): void;\n };\n}\n\n/**\n * Ellmo AI middleware helper for Next.js\n * \n * This is a vanilla helper that adds pathname to request headers.\n * It doesn't depend on Next.js - you provide the request/response objects.\n * \n * Usage in Next.js:\n * \n * ```ts\n * // middleware.ts\n * import { ellmoMiddleware } from 'ellmo-ai-react/middleware';\n * import { NextResponse } from 'next/server';\n * import type { NextRequest } from 'next/server';\n * \n * export function middleware(request: NextRequest) {\n * const response = NextResponse.next();\n * return ellmoMiddleware(request, response);\n * }\n * \n * export const config = {\n * matcher: [\n * '/((?!_next/static|_next/image|favicon.ico).*)',\n * ],\n * };\n * ```\n * \n * Composing with existing middleware:\n * \n * ```ts\n * export function middleware(request: NextRequest) {\n * let response = NextResponse.next();\n * \n * // Your logic here\n * if (someCondition) {\n * response = NextResponse.redirect(new URL('/login', request.url));\n * }\n * \n * // Apply Ellmo middleware\n * return ellmoMiddleware(request, response);\n * }\n * ```\n */\nexport function ellmoMiddleware<\n Req extends EllmoRequest,\n Res extends EllmoResponse\n>(\n request: Req,\n response: Res\n): Res {\n // Extract pathname from the request URL\n const pathname = request.nextUrl.pathname;\n \n // Add the pathname to a custom header\n response.headers.set(ELLMO_PATHNAME_HEADER, pathname);\n \n return response;\n}\n\n/**\n * Helper to extract pathname from headers object\n * \n * Usage in Next.js Server Components:\n * ```tsx\n * import { headers } from 'next/headers';\n * import { getPathnameFromHeaders } from 'ellmo-ai-react/middleware';\n * \n * export default function Page() {\n * const headersList = headers();\n * const pathname = getPathnameFromHeaders(headersList);\n * \n * return <EllmoApi clientId=\"your-id\" url={pathname} />;\n * }\n * ```\n */\nexport function getPathnameFromHeaders(headers: { get(name: string): string | null }): string | null {\n return headers.get(ELLMO_PATHNAME_HEADER);\n}\n\nexport default ellmoMiddleware;\n"],"names":[],"mappings":";;;;AAEA;AACO,MAAM,wBAAwB,GACnC,sDAAsD,CAAC;AAEzD;AACO,MAAM,wBAAwB,GAAG,IAAI,CAAC;AAE7C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA8BG;AACI,eAAe,cAAc,CAAC,EACnC,QAAQ,EACR,GAAG,EACH,IAAI,GAAG,UAAU,EAKlB,EAAA;AACC,IAAA,IAAI;;QAEF,IAAI,CAAC,QAAQ,EAAE;AACb,YAAA,MAAM,IAAI,KAAK,CAAC,wEAAwE,CAAC,CAAC;SAC3F;QAED,IAAI,CAAC,GAAG,EAAE;AACR,YAAA,MAAM,IAAI,KAAK,CAAC,oDAAoD,CAAC,CAAC;SACvE;AAED,QAAA,IAAI,GAAG,CAAC,QAAQ,CAAC,GAAG,CAAC,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AAA;KACF,CAAC;IAEF,IAAI,SAAS,EAAE;AACb,QAAA,KAAK,CAAC,UAAU,GAAG,KAAK,CAAC;AACzB,QAAA,KAAK,CAAC,SAAS,CAAC,cAAc,GAAG,KAAK,CAAC;KACxC;IAED,OAAO,EAAE,KAAK,EAAE,CAAC;AACnB;;AC1LA;;;AAGG;AACI,MAAM,qBAAqB,GAAG,mBAAmB;AAiBxD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAyCG;AACa,SAAA,eAAe,CAI7B,OAAY,EACZ,QAAa,EAAA;;AAGb,IAAA,MAAM,QAAQ,GAAG,OAAO,CAAC,OAAO,CAAC,QAAQ,CAAC;;IAG1C,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,qBAAqB,EAAE,QAAQ,CAAC,CAAC;AAEtD,IAAA,OAAO,QAAQ,CAAC;AAClB,CAAC;AAED;;;;;;;;;;;;;;;AAeG;AACG,SAAU,sBAAsB,CAAC,OAA6C,EAAA;AAClF,IAAA,OAAO,OAAO,CAAC,GAAG,CAAC,qBAAqB,CAAC,CAAC;AAC5C;;;;;;;;;;;;;"}
|
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,74 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
2
|
|
|
3
|
+
/** Base URL for the embedded-html API (Supabase Edge Function). */
|
|
4
|
+
const ELLMO_EMBEDDED_HTML_BASE = 'https://api-s.tryellmo.ai/functions/v1/embedded-html';
|
|
5
|
+
/** Cache revalidation time in seconds (30 minutes). Used with Next.js fetch. */
|
|
6
|
+
const CACHE_REVALIDATE_SECONDS = 1800;
|
|
7
|
+
/**
|
|
8
|
+
* Fetch data from Ellmo API
|
|
9
|
+
* Use this in getServerSideProps, getStaticProps, or getInitialProps
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```tsx
|
|
13
|
+
* // In getServerSideProps (Next.js Pages Router)
|
|
14
|
+
* export async function getServerSideProps(context) {
|
|
15
|
+
* const { data, error } = await fetchEllmoData({
|
|
16
|
+
* clientId: process.env.ELLMO_CLIENT_ID!,
|
|
17
|
+
* url: context.resolvedUrl,
|
|
18
|
+
* type: 'semantic'
|
|
19
|
+
* });
|
|
20
|
+
*
|
|
21
|
+
* return {
|
|
22
|
+
* props: { ellmoData: data, ellmoError: error }
|
|
23
|
+
* };
|
|
24
|
+
* }
|
|
25
|
+
*
|
|
26
|
+
* // In getInitialProps (older Next.js API)
|
|
27
|
+
* MyPage.getInitialProps = async (ctx) => {
|
|
28
|
+
* const { data, error } = await fetchEllmoData({
|
|
29
|
+
* clientId: process.env.NEXT_PUBLIC_ELLMO_CLIENT_ID!,
|
|
30
|
+
* url: ctx.asPath || '/',
|
|
31
|
+
* type: 'semantic'
|
|
32
|
+
* });
|
|
33
|
+
*
|
|
34
|
+
* return { ellmoData: data, ellmoError: error };
|
|
35
|
+
* };
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
async function fetchEllmoData({ clientId, url, type = 'semantic' }) {
|
|
39
|
+
try {
|
|
40
|
+
// Validate required props
|
|
41
|
+
if (!clientId) {
|
|
42
|
+
throw new Error('clientId is required. Please provide your Ellmo API client identifier.');
|
|
43
|
+
}
|
|
44
|
+
if (!url) {
|
|
45
|
+
throw new Error('url is required. Please provide the page pathname.');
|
|
46
|
+
}
|
|
47
|
+
if (url.includes('.')) {
|
|
48
|
+
// Sometimes urls contain a dot if they're images or other assets which are served by SSG (static generation)
|
|
49
|
+
return { data: null, error: null };
|
|
50
|
+
}
|
|
51
|
+
// Encode the URL path for the API call
|
|
52
|
+
const encodedUrl = encodeURIComponent(url);
|
|
53
|
+
const translatedType = type === 'structured' ? 'structured' : 'semantic';
|
|
54
|
+
// Build the API URL (embedded-html Supabase Edge Function)
|
|
55
|
+
const apiUrl = `${ELLMO_EMBEDDED_HTML_BASE}?c=${clientId}&t=${translatedType}&u=${encodedUrl}`;
|
|
56
|
+
// Fetch data with 30-min cache (Next.js data cache; other runtimes ignore next option)
|
|
57
|
+
const response = await fetch(apiUrl, {
|
|
58
|
+
next: { revalidate: CACHE_REVALIDATE_SECONDS },
|
|
59
|
+
});
|
|
60
|
+
if (!response.ok) {
|
|
61
|
+
throw new Error(`API request failed with status ${response.status}`);
|
|
62
|
+
}
|
|
63
|
+
const data = await response.text();
|
|
64
|
+
return { data, error: null };
|
|
65
|
+
}
|
|
66
|
+
catch (err) {
|
|
67
|
+
const errorMessage = err instanceof Error ? err.message : 'Unknown error occurred';
|
|
68
|
+
return { data: null, error: errorMessage };
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
3
72
|
/**
|
|
4
73
|
* Server-only Ellmo API component (React Server Component)
|
|
5
74
|
* This component fetches data during server rendering.
|
|
@@ -27,11 +96,11 @@ const EllmoApi = async ({ url, clientId, type, className = '', showErrors = fals
|
|
|
27
96
|
}
|
|
28
97
|
// Encode the URL path for the API call
|
|
29
98
|
const encodedUrl = encodeURIComponent(url);
|
|
30
|
-
// Build the API URL
|
|
31
|
-
const apiUrl =
|
|
32
|
-
// Fetch data
|
|
99
|
+
// Build the API URL (embedded-html Supabase Edge Function)
|
|
100
|
+
const apiUrl = `${ELLMO_EMBEDDED_HTML_BASE}?c=${clientId}&t=${translatedType}&u=${encodedUrl}`;
|
|
101
|
+
// Fetch data with 30-min cache (Next.js data cache)
|
|
33
102
|
const response = await fetch(apiUrl, {
|
|
34
|
-
|
|
103
|
+
next: { revalidate: CACHE_REVALIDATE_SECONDS },
|
|
35
104
|
});
|
|
36
105
|
if (!response.ok) {
|
|
37
106
|
throw new Error(`API request failed with status ${response.status}`);
|
|
@@ -132,71 +201,6 @@ const EllmoApiSSR = ({ data = null, error = null, className = '', showErrors = f
|
|
|
132
201
|
return null;
|
|
133
202
|
};
|
|
134
203
|
|
|
135
|
-
/**
|
|
136
|
-
* Fetch data from Ellmo API
|
|
137
|
-
* Use this in getServerSideProps, getStaticProps, or getInitialProps
|
|
138
|
-
*
|
|
139
|
-
* @example
|
|
140
|
-
* ```tsx
|
|
141
|
-
* // In getServerSideProps (Next.js Pages Router)
|
|
142
|
-
* export async function getServerSideProps(context) {
|
|
143
|
-
* const { data, error } = await fetchEllmoData({
|
|
144
|
-
* clientId: process.env.ELLMO_CLIENT_ID!,
|
|
145
|
-
* url: context.resolvedUrl,
|
|
146
|
-
* type: 'semantic'
|
|
147
|
-
* });
|
|
148
|
-
*
|
|
149
|
-
* return {
|
|
150
|
-
* props: { ellmoData: data, ellmoError: error }
|
|
151
|
-
* };
|
|
152
|
-
* }
|
|
153
|
-
*
|
|
154
|
-
* // In getInitialProps (older Next.js API)
|
|
155
|
-
* MyPage.getInitialProps = async (ctx) => {
|
|
156
|
-
* const { data, error } = await fetchEllmoData({
|
|
157
|
-
* clientId: process.env.NEXT_PUBLIC_ELLMO_CLIENT_ID!,
|
|
158
|
-
* url: ctx.asPath || '/',
|
|
159
|
-
* type: 'semantic'
|
|
160
|
-
* });
|
|
161
|
-
*
|
|
162
|
-
* return { ellmoData: data, ellmoError: error };
|
|
163
|
-
* };
|
|
164
|
-
* ```
|
|
165
|
-
*/
|
|
166
|
-
async function fetchEllmoData({ clientId, url, type = 'semantic' }) {
|
|
167
|
-
try {
|
|
168
|
-
// Validate required props
|
|
169
|
-
if (!clientId) {
|
|
170
|
-
throw new Error('clientId is required. Please provide your Ellmo API client identifier.');
|
|
171
|
-
}
|
|
172
|
-
if (!url) {
|
|
173
|
-
throw new Error('url is required. Please provide the page pathname.');
|
|
174
|
-
}
|
|
175
|
-
if (url.includes('.')) {
|
|
176
|
-
// Sometimes urls contain a dot if they're images or other assets which are served by SSG (static generation)
|
|
177
|
-
return { data: null, error: null };
|
|
178
|
-
}
|
|
179
|
-
// Encode the URL path for the API call
|
|
180
|
-
const encodedUrl = encodeURIComponent(url);
|
|
181
|
-
const translatedType = type === 'structured' ? 'structured' : 'semantic';
|
|
182
|
-
// Build the API URL
|
|
183
|
-
const apiUrl = `https://www.tryellmo.ai/public-api/wordpress?c=${clientId}&t=${translatedType}&u=${encodedUrl}`;
|
|
184
|
-
// Fetch data
|
|
185
|
-
const response = await fetch(apiUrl, {
|
|
186
|
-
cache: 'no-store',
|
|
187
|
-
});
|
|
188
|
-
if (!response.ok) {
|
|
189
|
-
throw new Error(`API request failed with status ${response.status}`);
|
|
190
|
-
}
|
|
191
|
-
const data = await response.text();
|
|
192
|
-
return { data, error: null };
|
|
193
|
-
}
|
|
194
|
-
catch (err) {
|
|
195
|
-
const errorMessage = err instanceof Error ? err.message : 'Unknown error occurred';
|
|
196
|
-
return { data: null, error: errorMessage };
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
|
|
200
204
|
/**
|
|
201
205
|
* Helper for _app.tsx to fetch global Ellmo data and merge with page props
|
|
202
206
|
* Automatically calls the page's getInitialProps and merges everything
|
|
@@ -242,7 +246,7 @@ function withEllmoApp({ clientId, url, type = 'semantic', debugging = false }) {
|
|
|
242
246
|
// Fetch global Ellmo data
|
|
243
247
|
const { data, error } = await fetchEllmoData({
|
|
244
248
|
clientId,
|
|
245
|
-
url: url(
|
|
249
|
+
url: url(), // Use the function to get the URL
|
|
246
250
|
type
|
|
247
251
|
});
|
|
248
252
|
// Call the page's getInitialProps if it exists
|
|
@@ -304,10 +308,10 @@ function withEllmoApp({ clientId, url, type = 'semantic', debugging = false }) {
|
|
|
304
308
|
* ```
|
|
305
309
|
*/
|
|
306
310
|
function withEllmoStatic({ clientId, url, type = 'semantic', debugging = false }) {
|
|
307
|
-
return async (
|
|
311
|
+
return async () => {
|
|
308
312
|
return getEllmoStaticProps({
|
|
309
313
|
clientId,
|
|
310
|
-
url
|
|
314
|
+
url,
|
|
311
315
|
type,
|
|
312
316
|
debugging
|
|
313
317
|
});
|
|
@@ -323,7 +327,7 @@ function withEllmoStatic({ clientId, url, type = 'semantic', debugging = false }
|
|
|
323
327
|
* Accepts a plain URL string and returns the same shape as buildEllmoProps.
|
|
324
328
|
*/
|
|
325
329
|
async function getEllmoStaticProps({ clientId, url, type = 'semantic', debugging = false }) {
|
|
326
|
-
const { data, error } = await fetchEllmoData({ clientId, url, type });
|
|
330
|
+
const { data, error } = await fetchEllmoData({ clientId, url: url(), type });
|
|
327
331
|
const props = {
|
|
328
332
|
ellmoData: data,
|
|
329
333
|
pageProps: {
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","sources":["../src/EllmoApi.tsx","../src/EllmoApiSSR.tsx","../src/shared.ts","../src/EllmoApiStatic.tsx","../src/middleware.ts"],"sourcesContent":["import React from 'react';\nimport type { EllmoApiType } from './shared';\n\nexport type { EllmoApiType };\n\nexport interface EllmoApiProps {\n /**\n * The URL path to fetch data for (required).\n * \n * With middleware (recommended):\n * ```tsx\n * import { headers } from 'next/headers';\n * import { getPathnameFromHeaders } from 'ellmo-ai-react/middleware';\n * \n * const pathname = getPathnameFromHeaders(headers());\n * <EllmoApi clientId=\"...\" url={pathname || '/'} />\n * ```\n * \n * Without middleware:\n * ```tsx\n * <EllmoApi clientId=\"...\" url=\"/about\" />\n * ```\n */\n url: string;\n /**\n * The API key/client identifier (required)\n */\n clientId: string;\n /**\n * Type of data to fetch\n */\n type?: EllmoApiType;\n /**\n * Custom className for styling\n */\n className?: string;\n /**\n * Whether to show errors\n */\n showErrors?: boolean;\n /**\n * Children to render when data is available\n */\n children?: (data: any, error: Error | null) => React.ReactNode;\n}\n\n/**\n * Server-only Ellmo API component (React Server Component)\n * This component fetches data during server rendering.\n * It requires Next.js 13+ App Router or other RSC-compatible frameworks.\n * \n * @example\n * ```tsx\n * // In a server component (app/page.tsx)\n * export default async function Page() {\n * return <EllmoApi clientId=\"your-id\" url=\"/about\" />;\n * }\n * ```\n */\nconst EllmoApi = async ({\n url,\n clientId,\n type,\n className = '',\n showErrors = false,\n children\n}: EllmoApiProps): Promise<React.ReactElement | null> => {\n let data: any = null;\n let error: Error | null = null;\n\n let translatedType = type === 'structured' ? 'structured' : 'semantic';\n\n try {\n // Validate required props\n if (!clientId) {\n throw new Error('clientId is required. Please provide your Ellmo API client identifier.');\n }\n\n if (!url) {\n throw new Error('url prop is required. Please provide the page pathname.');\n }\n\n // Encode the URL path for the API call\n const encodedUrl = encodeURIComponent(url);\n\n // Build the API URL\n const apiUrl = `https://www.tryellmo.ai/public-api/wordpress?c=${clientId}&t=${translatedType}&u=${encodedUrl}`;\n\n // Fetch data during server render\n const response = await fetch(apiUrl, {\n cache: 'no-store', // Always fetch fresh data\n });\n\n if (!response.ok) {\n throw new Error(`API request failed with status ${response.status}`);\n }\n\n if (type === 'structured') {\n data = await response.text();\n } else if (type === 'semantic') {\n data = await response.text();\n } else {\n throw new Error(`Invalid type: ${type}`);\n }\n } catch (err) {\n error = err instanceof Error ? err : new Error('Unknown error occurred');\n }\n\n // If children prop is provided, use render prop pattern\n if (children) {\n return <>{children(data, error)}</>;\n }\n\n // Show errors if enabled\n if (error && showErrors) {\n return (\n <div className={`ellmo-api-error ${className}`}>\n <div>Error: {error.message}</div>\n </div>\n );\n }\n\n // Show data if showErrors is enabled (for debugging)\n if (data && showErrors) {\n return (\n <div className={`ellmo-api-data ${className}`}>\n <pre>{JSON.stringify(data, null, 2)}</pre>\n </div>\n );\n }\n\n // Invisible component by default - just fetches data during server render\n if (type === 'structured' && data) {\n return <div dangerouslySetInnerHTML={{ __html: data }} />;\n } else if (type === 'semantic' && data) {\n return <div dangerouslySetInnerHTML={{ __html: data }} />;\n }\n return null;\n};\n\nexport default EllmoApi;\n","import React from 'react';\nimport type { EllmoApiType } from './shared';\n\nexport type { EllmoApiType };\n\nexport interface EllmoApiSSRProps {\n /**\n * The pre-fetched data from the Ellmo API\n */\n data?: string | null;\n /**\n * Error from fetching data\n */\n error?: string | null;\n /**\n * Type of data to render\n */\n type?: EllmoApiType;\n /**\n * Custom className for styling\n */\n className?: string;\n /**\n * Whether to show errors\n */\n showErrors?: boolean;\n /**\n * Children render prop with data and error\n */\n children?: (data: string | null, error: string | null) => React.ReactNode;\n}\n\n/**\n * Ellmo API component for traditional SSR environments (Next.js Pages Router)\n * This component receives pre-fetched data and renders it.\n * Data fetching should be done in getServerSideProps or getStaticProps.\n * \n * @example\n * ```tsx\n * // In your page file (pages/about.tsx)\n * import { EllmoApiSSR, fetchEllmoData } from 'ellmo-ai-react';\n * \n * export async function getServerSideProps(context) {\n * const { data, error } = await fetchEllmoData({\n * clientId: 'your-client-id',\n * url: context.resolvedUrl,\n * type: 'semantic'\n * });\n * \n * return {\n * props: { ellmoData: data, ellmoError: error }\n * };\n * }\n * \n * export default function AboutPage({ ellmoData, ellmoError }) {\n * return (\n * <div>\n * <h1>About Page</h1>\n * <EllmoApiSSR data={ellmoData} error={ellmoError} />\n * </div>\n * );\n * }\n * ```\n */\nconst EllmoApiSSR: React.FC<EllmoApiSSRProps> = ({\n data = null,\n error = null,\n className = '',\n showErrors = false,\n children\n}) => {\n // If children prop is provided, use render prop pattern\n if (children) {\n return <>{children(data, error)}</>;\n }\n\n // Show errors if enabled\n if (error && showErrors) {\n return (\n <div className={`ellmo-api-error ${className}`}>\n <div>Error: {error}</div>\n </div>\n );\n }\n\n // Show data if showErrors is enabled (for debugging)\n if (data && showErrors) {\n return (\n <div className={`ellmo-api-data ${className}`}>\n <pre>{data}</pre>\n </div>\n );\n }\n\n // Render HTML content if available\n if (data && !error) {\n return <div className={className} dangerouslySetInnerHTML={{ __html: data }} />;\n }\n\n // Return null if no data\n return null;\n};\n\nexport default EllmoApiSSR;\n \n","export type EllmoApiType = 'semantic' | 'structured';\n\n/**\n * Fetch data from Ellmo API\n * Use this in getServerSideProps, getStaticProps, or getInitialProps\n * \n * @example\n * ```tsx\n * // In getServerSideProps (Next.js Pages Router)\n * export async function getServerSideProps(context) {\n * const { data, error } = await fetchEllmoData({\n * clientId: process.env.ELLMO_CLIENT_ID!,\n * url: context.resolvedUrl,\n * type: 'semantic'\n * });\n * \n * return {\n * props: { ellmoData: data, ellmoError: error }\n * };\n * }\n * \n * // In getInitialProps (older Next.js API)\n * MyPage.getInitialProps = async (ctx) => {\n * const { data, error } = await fetchEllmoData({\n * clientId: process.env.NEXT_PUBLIC_ELLMO_CLIENT_ID!,\n * url: ctx.asPath || '/',\n * type: 'semantic'\n * });\n * \n * return { ellmoData: data, ellmoError: error };\n * };\n * ```\n */\nexport async function fetchEllmoData({\n clientId,\n url,\n type = 'semantic'\n}: {\n clientId: string;\n url: string;\n type?: EllmoApiType;\n}): Promise<{ data: string | null; error: string | null }> {\n try {\n // Validate required props\n if (!clientId) {\n throw new Error('clientId is required. Please provide your Ellmo API client identifier.');\n }\n\n if (!url) {\n throw new Error('url is required. Please provide the page pathname.');\n }\n\n if (url.includes('.')) {\n // Sometimes urls contain a dot if they're images or other assets which are served by SSG (static generation)\n return { data: null, error: null };\n }\n\n // Encode the URL path for the API call\n const encodedUrl = encodeURIComponent(url);\n const translatedType = type === 'structured' ? 'structured' : 'semantic';\n\n // Build the API URL\n const apiUrl = `https://www.tryellmo.ai/public-api/wordpress?c=${clientId}&t=${translatedType}&u=${encodedUrl}`;\n\n // Fetch data\n const response = await fetch(apiUrl, {\n cache: 'no-store',\n });\n\n if (!response.ok) {\n throw new Error(`API request failed with status ${response.status}`);\n }\n\n const data = await response.text();\n return { data, error: null };\n } catch (err) {\n const errorMessage = err instanceof Error ? err.message : 'Unknown error occurred';\n return { data: null, error: errorMessage };\n }\n}\n\n","import { fetchEllmoData, type EllmoApiType } from './shared';\n\nexport type { EllmoApiType };\n\n// Type for Next.js App context\ntype AppContext = {\n Component: any;\n ctx: any;\n router: any;\n};\n\n// Type for Next.js GetStaticProps context (minimal definition to avoid next.js dependency)\ntype GetStaticPropsContext = {\n params?: Record<string, string | string[]>;\n preview?: boolean;\n previewData?: any;\n locale?: string;\n locales?: string[];\n defaultLocale?: string;\n};\n\n\n/**\n * Helper for _app.tsx to fetch global Ellmo data and merge with page props\n * Automatically calls the page's getInitialProps and merges everything\n * \n * @param clientId - Your Ellmo client ID\n * @param url - Function that returns the URL path from app context\n * @param type - Content type: 'semantic' or 'structured' (default: 'semantic')\n * @param debugging - Include pageEllmoError in props for debugging (default: false)\n * \n * Returns pageEllmoData in pageProps. If debugging is enabled, also includes pageEllmoError.\n * \n * @example\n * ```tsx\n * import { withEllmoApp } from 'ellmo-ai-react';\n * \n * function MyApp({ Component, pageProps }) {\n * return (\n * <>\n * <header>\n * <EllmoApiSSR \n * data={pageProps.pageEllmoData} \n * error={pageProps.pageEllmoError} \n * showErrors={true} // Show errors in development\n * />\n * </header>\n * <Component {...pageProps} />\n * </>\n * );\n * }\n * \n * MyApp.getInitialProps = withEllmoApp({\n * clientId: process.env.NEXT_PUBLIC_ELLMO_CLIENT_ID!,\n * url: (appContext) => '/global-header',\n * type: 'structured',\n * debugging: process.env.NODE_ENV === 'development' // Enable error prop in development\n * });\n * \n * export default MyApp;\n * ```\n */\nexport function withEllmoApp({\n clientId,\n url,\n type = 'semantic',\n debugging = false\n}: {\n clientId: string;\n url: (appContext: AppContext) => string;\n type?: EllmoApiType;\n debugging?: boolean;\n}) {\n return async (appContext: AppContext) => {\n // Fetch global Ellmo data\n const { data, error } = await fetchEllmoData({\n clientId,\n url: url(appContext), // Use the function to get the URL\n type\n });\n\n // Call the page's getInitialProps if it exists\n let pageProps = {};\n if (appContext.Component.getInitialProps) {\n pageProps = await appContext.Component.getInitialProps(appContext.ctx);\n }\n\n // Return merged props with ellmo data inside pageProps\n return {\n pageProps: {\n ...pageProps,\n pageEllmoData: data,\n ...(debugging ? { pageEllmoError: error } : {}), // Only add error if debugging is enabled\n }\n };\n };\n}\n\n/**\n * Higher-order function to wrap getStaticProps with Ellmo data fetching\n * Automatically calls the page's existing getStaticProps and merges Ellmo data\n *\n * @param clientId - Your Ellmo client ID\n * @param url - Function that returns the URL path from GetStaticPropsContext\n * @param type - Content type: 'semantic' or 'structured' (default: 'semantic')\n * @param debugging - Include ellmoError in props for debugging (default: false)\n *\n * Returns ellmoData and ellmoError in props at both root level and inside pageProps for flexibility.\n *\n * @example\n * ```tsx\n * import { withEllmoStatic } from 'ellmo-ai-react';\n *\n * export const getStaticProps = withEllmoStatic({\n * clientId: process.env.ELLMO_CLIENT_ID!,\n * url: (context) => context.params?.slug ? `/blog/${context.params.slug}` : '/',\n * type: 'semantic',\n * debugging: process.env.NODE_ENV === 'development'\n * });\n *\n * // In page component - access directly\n * export default function BlogPost({ ellmoData, ellmoError }) {\n * return (\n * <div>\n * <EllmoApiSSR data={ellmoData} error={ellmoError} />\n * <h1>Blog Post</h1>\n * </div>\n * );\n * }\n *\n * // In _app.tsx - access via pageProps\n * function MyApp({ Component, pageProps }) {\n * return (\n * <div>\n * <EllmoApiSSR data={pageProps.pageEllmoData} error={pageProps.pageEllmoError} />\n * <Component {...pageProps} />\n * </div>\n * );\n * }\n * ```\n */\nexport function withEllmoStatic<P extends Record<string, any> = Record<string, any>>({\n clientId,\n url,\n type = 'semantic',\n debugging = false\n}: {\n clientId: string;\n url: (context: GetStaticPropsContext) => string;\n type?: EllmoApiType;\n debugging?: boolean;\n}) {\n return async (context: GetStaticPropsContext) => {\n return getEllmoStaticProps({\n clientId,\n url: url(context),\n type,\n debugging\n }) as Promise<{ props: P & { ellmoData: string | null; ellmoError?: string | null; pageProps?: { pageEllmoData: string | null; pageEllmoError?: string | null } } }>;\n };\n}\n\n/**\n * Build standardized Ellmo props synchronously from provided data and error.\n * Returns ellmoData/ellmoError at root and pageEllmoData/pageEllmoError inside pageProps.\n */\n// (removed buildEllmoProps to reduce surface area)\n\n/**\n * Fetch Ellmo data and return standardized props (no Next.js dependency).\n * Accepts a plain URL string and returns the same shape as buildEllmoProps.\n */\nexport async function getEllmoStaticProps({\n clientId,\n url,\n type = 'semantic',\n debugging = false\n}: {\n clientId: string;\n url: string;\n type?: EllmoApiType;\n debugging?: boolean;\n}): Promise<{ props: { ellmoData: string | null; ellmoError?: string | null; pageProps: { pageEllmoData: string | null; pageEllmoError?: string | null } } }> {\n const { data, error } = await fetchEllmoData({ clientId, url, type });\n const props: { ellmoData: string | null; ellmoError?: string | null; pageProps: { pageEllmoData: string | null; pageEllmoError?: string | null } } = {\n ellmoData: data,\n pageProps: {\n pageEllmoData: data\n }\n };\n\n if (debugging) {\n props.ellmoError = error;\n props.pageProps.pageEllmoError = error;\n }\n\n return { props };\n}\n\n","/**\n * Custom header name for Ellmo AI to pass pathname information\n * Using 'x-ellmo-pathname' to avoid conflicts with other libraries\n */\nexport const ELLMO_PATHNAME_HEADER = 'x-ellmo-pathname';\n\n/**\n * Type definitions for Next.js middleware (to avoid requiring Next.js as a dependency)\n */\nexport interface EllmoRequest {\n nextUrl: {\n pathname: string;\n };\n}\n\nexport interface EllmoResponse {\n headers: {\n set(name: string, value: string): void;\n };\n}\n\n/**\n * Ellmo AI middleware helper for Next.js\n * \n * This is a vanilla helper that adds pathname to request headers.\n * It doesn't depend on Next.js - you provide the request/response objects.\n * \n * Usage in Next.js:\n * \n * ```ts\n * // middleware.ts\n * import { ellmoMiddleware } from 'ellmo-ai-react/middleware';\n * import { NextResponse } from 'next/server';\n * import type { NextRequest } from 'next/server';\n * \n * export function middleware(request: NextRequest) {\n * const response = NextResponse.next();\n * return ellmoMiddleware(request, response);\n * }\n * \n * export const config = {\n * matcher: [\n * '/((?!_next/static|_next/image|favicon.ico).*)',\n * ],\n * };\n * ```\n * \n * Composing with existing middleware:\n * \n * ```ts\n * export function middleware(request: NextRequest) {\n * let response = NextResponse.next();\n * \n * // Your logic here\n * if (someCondition) {\n * response = NextResponse.redirect(new URL('/login', request.url));\n * }\n * \n * // Apply Ellmo middleware\n * return ellmoMiddleware(request, response);\n * }\n * ```\n */\nexport function ellmoMiddleware<\n Req extends EllmoRequest,\n Res extends EllmoResponse\n>(\n request: Req,\n response: Res\n): Res {\n // Extract pathname from the request URL\n const pathname = request.nextUrl.pathname;\n \n // Add the pathname to a custom header\n response.headers.set(ELLMO_PATHNAME_HEADER, pathname);\n \n return response;\n}\n\n/**\n * Helper to extract pathname from headers object\n * \n * Usage in Next.js Server Components:\n * ```tsx\n * import { headers } from 'next/headers';\n * import { getPathnameFromHeaders } from 'ellmo-ai-react/middleware';\n * \n * export default function Page() {\n * const headersList = headers();\n * const pathname = getPathnameFromHeaders(headersList);\n * \n * return <EllmoApi clientId=\"your-id\" url={pathname} />;\n * }\n * ```\n */\nexport function getPathnameFromHeaders(headers: { get(name: string): string | null }): string | null {\n return headers.get(ELLMO_PATHNAME_HEADER);\n}\n\nexport default ellmoMiddleware;\n"],"names":[],"mappings":";;AA8CA;;;;;;;;;;;;AAYG;AACG,MAAA,QAAQ,GAAG,OAAO,EACtB,GAAG,EACH,QAAQ,EACR,IAAI,EACJ,SAAS,GAAG,EAAE,EACd,UAAU,GAAG,KAAK,EAClB,QAAQ,EACM,KAAwC;IACtD,IAAI,IAAI,GAAQ,IAAI,CAAC;IACrB,IAAI,KAAK,GAAiB,IAAI,CAAC;AAE/B,IAAA,IAAI,cAAc,GAAG,IAAI,KAAK,YAAY,GAAG,YAAY,GAAG,UAAU,CAAC;AAEvE,IAAA,IAAI;;QAEF,IAAI,CAAC,QAAQ,EAAE;AACb,YAAA,MAAM,IAAI,KAAK,CAAC,wEAAwE,CAAC,CAAC;SAC3F;QAED,IAAI,CAAC,GAAG,EAAE;AACR,YAAA,MAAM,IAAI,KAAK,CAAC,yDAAyD,CAAC,CAAC;SAC5E;;AAGD,QAAA,MAAM,UAAU,GAAG,kBAAkB,CAAC,GAAG,CAAC,CAAC;;QAG3C,MAAM,MAAM,GAAG,CAAkD,+CAAA,EAAA,QAAQ,MAAM,cAAc,CAAA,GAAA,EAAM,UAAU,CAAA,CAAE,CAAC;;AAGhH,QAAA,MAAM,QAAQ,GAAG,MAAM,KAAK,CAAC,MAAM,EAAE;YACnC,KAAK,EAAE,UAAU;AAClB,SAAA,CAAC,CAAC;AAEH,QAAA,IAAI,CAAC,QAAQ,CAAC,EAAE,EAAE;YAChB,MAAM,IAAI,KAAK,CAAC,CAAA,+BAAA,EAAkC,QAAQ,CAAC,MAAM,CAAE,CAAA,CAAC,CAAC;SACtE;AAED,QAAA,IAAI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,GAAG,CAAC,qBAAqB,CAAC,CAAC;AAC5C;;;;"}
|
|
1
|
+
{"version":3,"file":"index.mjs","sources":["../src/shared.ts","../src/EllmoApi.tsx","../src/EllmoApiSSR.tsx","../src/EllmoApiStatic.tsx","../src/middleware.ts"],"sourcesContent":["export type EllmoApiType = 'semantic' | 'structured';\n\n/** Base URL for the embedded-html API (Supabase Edge Function). */\nexport const ELLMO_EMBEDDED_HTML_BASE =\n 'https://api-s.tryellmo.ai/functions/v1/embedded-html';\n\n/** Cache revalidation time in seconds (30 minutes). Used with Next.js fetch. */\nexport const CACHE_REVALIDATE_SECONDS = 1800;\n\n/**\n * Fetch data from Ellmo API\n * Use this in getServerSideProps, getStaticProps, or getInitialProps\n * \n * @example\n * ```tsx\n * // In getServerSideProps (Next.js Pages Router)\n * export async function getServerSideProps(context) {\n * const { data, error } = await fetchEllmoData({\n * clientId: process.env.ELLMO_CLIENT_ID!,\n * url: context.resolvedUrl,\n * type: 'semantic'\n * });\n * \n * return {\n * props: { ellmoData: data, ellmoError: error }\n * };\n * }\n * \n * // In getInitialProps (older Next.js API)\n * MyPage.getInitialProps = async (ctx) => {\n * const { data, error } = await fetchEllmoData({\n * clientId: process.env.NEXT_PUBLIC_ELLMO_CLIENT_ID!,\n * url: ctx.asPath || '/',\n * type: 'semantic'\n * });\n * \n * return { ellmoData: data, ellmoError: error };\n * };\n * ```\n */\nexport async function fetchEllmoData({\n clientId,\n url,\n type = 'semantic'\n}: {\n clientId: string;\n url: string;\n type?: EllmoApiType;\n}): Promise<{ data: string | null; error: string | null }> {\n try {\n // Validate required props\n if (!clientId) {\n throw new Error('clientId is required. Please provide your Ellmo API client identifier.');\n }\n\n if (!url) {\n throw new Error('url is required. Please provide the page pathname.');\n }\n\n if (url.includes('.')) {\n // Sometimes urls contain a dot if they're images or other assets which are served by SSG (static generation)\n return { data: null, error: null };\n }\n\n // Encode the URL path for the API call\n const encodedUrl = encodeURIComponent(url);\n const translatedType = type === 'structured' ? 'structured' : 'semantic';\n\n // Build the API URL (embedded-html Supabase Edge Function)\n const apiUrl = `${ELLMO_EMBEDDED_HTML_BASE}?c=${clientId}&t=${translatedType}&u=${encodedUrl}`;\n\n // Fetch data with 30-min cache (Next.js data cache; other runtimes ignore next option)\n const response = await fetch(apiUrl, {\n next: { revalidate: CACHE_REVALIDATE_SECONDS },\n } as RequestInit);\n\n if (!response.ok) {\n throw new Error(`API request failed with status ${response.status}`);\n }\n\n const data = await response.text();\n return { data, error: null };\n } catch (err) {\n const errorMessage = err instanceof Error ? err.message : 'Unknown error occurred';\n return { data: null, error: errorMessage };\n }\n}\n\n","import React from 'react';\nimport type { EllmoApiType } from './shared';\nimport { ELLMO_EMBEDDED_HTML_BASE, CACHE_REVALIDATE_SECONDS } from './shared';\n\nexport type { EllmoApiType };\n\nexport interface EllmoApiProps {\n /**\n * The URL path to fetch data for (required).\n * \n * With middleware (recommended):\n * ```tsx\n * import { headers } from 'next/headers';\n * import { getPathnameFromHeaders } from 'ellmo-ai-react/middleware';\n * \n * const pathname = getPathnameFromHeaders(headers());\n * <EllmoApi clientId=\"...\" url={pathname || '/'} />\n * ```\n * \n * Without middleware:\n * ```tsx\n * <EllmoApi clientId=\"...\" url=\"/about\" />\n * ```\n */\n url: string;\n /**\n * The API key/client identifier (required)\n */\n clientId: string;\n /**\n * Type of data to fetch\n */\n type?: EllmoApiType;\n /**\n * Custom className for styling\n */\n className?: string;\n /**\n * Whether to show errors\n */\n showErrors?: boolean;\n /**\n * Children to render when data is available\n */\n children?: (data: any, error: Error | null) => React.ReactNode;\n}\n\n/**\n * Server-only Ellmo API component (React Server Component)\n * This component fetches data during server rendering.\n * It requires Next.js 13+ App Router or other RSC-compatible frameworks.\n * \n * @example\n * ```tsx\n * // In a server component (app/page.tsx)\n * export default async function Page() {\n * return <EllmoApi clientId=\"your-id\" url=\"/about\" />;\n * }\n * ```\n */\nconst EllmoApi = async ({\n url,\n clientId,\n type,\n className = '',\n showErrors = false,\n children\n}: EllmoApiProps): Promise<React.ReactElement | null> => {\n let data: any = null;\n let error: Error | null = null;\n\n let translatedType = type === 'structured' ? 'structured' : 'semantic';\n\n try {\n // Validate required props\n if (!clientId) {\n throw new Error('clientId is required. Please provide your Ellmo API client identifier.');\n }\n\n if (!url) {\n throw new Error('url prop is required. Please provide the page pathname.');\n }\n\n // Encode the URL path for the API call\n const encodedUrl = encodeURIComponent(url);\n\n // Build the API URL (embedded-html Supabase Edge Function)\n const apiUrl = `${ELLMO_EMBEDDED_HTML_BASE}?c=${clientId}&t=${translatedType}&u=${encodedUrl}`;\n\n // Fetch data with 30-min cache (Next.js data cache)\n const response = await fetch(apiUrl, {\n next: { revalidate: CACHE_REVALIDATE_SECONDS },\n } as RequestInit);\n\n if (!response.ok) {\n throw new Error(`API request failed with status ${response.status}`);\n }\n\n if (type === 'structured') {\n data = await response.text();\n } else if (type === 'semantic') {\n data = await response.text();\n } else {\n throw new Error(`Invalid type: ${type}`);\n }\n } catch (err) {\n error = err instanceof Error ? err : new Error('Unknown error occurred');\n }\n\n // If children prop is provided, use render prop pattern\n if (children) {\n return <>{children(data, error)}</>;\n }\n\n // Show errors if enabled\n if (error && showErrors) {\n return (\n <div className={`ellmo-api-error ${className}`}>\n <div>Error: {error.message}</div>\n </div>\n );\n }\n\n // Show data if showErrors is enabled (for debugging)\n if (data && showErrors) {\n return (\n <div className={`ellmo-api-data ${className}`}>\n <pre>{JSON.stringify(data, null, 2)}</pre>\n </div>\n );\n }\n\n // Invisible component by default - just fetches data during server render\n if (type === 'structured' && data) {\n return <div dangerouslySetInnerHTML={{ __html: data }} />;\n } else if (type === 'semantic' && data) {\n return <div dangerouslySetInnerHTML={{ __html: data }} />;\n }\n return null;\n};\n\nexport default EllmoApi;\n","import React from 'react';\nimport type { EllmoApiType } from './shared';\n\nexport type { EllmoApiType };\n\nexport interface EllmoApiSSRProps {\n /**\n * The pre-fetched data from the Ellmo API\n */\n data?: string | null;\n /**\n * Error from fetching data\n */\n error?: string | null;\n /**\n * Type of data to render\n */\n type?: EllmoApiType;\n /**\n * Custom className for styling\n */\n className?: string;\n /**\n * Whether to show errors\n */\n showErrors?: boolean;\n /**\n * Children render prop with data and error\n */\n children?: (data: string | null, error: string | null) => React.ReactNode;\n}\n\n/**\n * Ellmo API component for traditional SSR environments (Next.js Pages Router)\n * This component receives pre-fetched data and renders it.\n * Data fetching should be done in getServerSideProps or getStaticProps.\n * \n * @example\n * ```tsx\n * // In your page file (pages/about.tsx)\n * import { EllmoApiSSR, fetchEllmoData } from 'ellmo-ai-react';\n * \n * export async function getServerSideProps(context) {\n * const { data, error } = await fetchEllmoData({\n * clientId: 'your-client-id',\n * url: context.resolvedUrl,\n * type: 'semantic'\n * });\n * \n * return {\n * props: { ellmoData: data, ellmoError: error }\n * };\n * }\n * \n * export default function AboutPage({ ellmoData, ellmoError }) {\n * return (\n * <div>\n * <h1>About Page</h1>\n * <EllmoApiSSR data={ellmoData} error={ellmoError} />\n * </div>\n * );\n * }\n * ```\n */\nconst EllmoApiSSR: React.FC<EllmoApiSSRProps> = ({\n data = null,\n error = null,\n className = '',\n showErrors = false,\n children\n}) => {\n // If children prop is provided, use render prop pattern\n if (children) {\n return <>{children(data, error)}</>;\n }\n\n // Show errors if enabled\n if (error && showErrors) {\n return (\n <div className={`ellmo-api-error ${className}`}>\n <div>Error: {error}</div>\n </div>\n );\n }\n\n // Show data if showErrors is enabled (for debugging)\n if (data && showErrors) {\n return (\n <div className={`ellmo-api-data ${className}`}>\n <pre>{data}</pre>\n </div>\n );\n }\n\n // Render HTML content if available\n if (data && !error) {\n return <div className={className} dangerouslySetInnerHTML={{ __html: data }} />;\n }\n\n // Return null if no data\n return null;\n};\n\nexport default EllmoApiSSR;\n \n","import { fetchEllmoData, type EllmoApiType } from './shared';\n\nexport type { EllmoApiType };\n\n// Type for Next.js App context\ntype AppContext = {\n Component: any;\n ctx: any;\n router: any;\n};\n\n/**\n * Helper for _app.tsx to fetch global Ellmo data and merge with page props\n * Automatically calls the page's getInitialProps and merges everything\n * \n * @param clientId - Your Ellmo client ID\n * @param url - Function that returns the URL path from app context\n * @param type - Content type: 'semantic' or 'structured' (default: 'semantic')\n * @param debugging - Include pageEllmoError in props for debugging (default: false)\n * \n * Returns pageEllmoData in pageProps. If debugging is enabled, also includes pageEllmoError.\n * \n * @example\n * ```tsx\n * import { withEllmoApp } from 'ellmo-ai-react';\n * \n * function MyApp({ Component, pageProps }) {\n * return (\n * <>\n * <header>\n * <EllmoApiSSR \n * data={pageProps.pageEllmoData} \n * error={pageProps.pageEllmoError} \n * showErrors={true} // Show errors in development\n * />\n * </header>\n * <Component {...pageProps} />\n * </>\n * );\n * }\n * \n * MyApp.getInitialProps = withEllmoApp({\n * clientId: process.env.NEXT_PUBLIC_ELLMO_CLIENT_ID!,\n * url: (appContext) => '/global-header',\n * type: 'structured',\n * debugging: process.env.NODE_ENV === 'development' // Enable error prop in development\n * });\n * \n * export default MyApp;\n * ```\n */\nexport function withEllmoApp({\n clientId,\n url,\n type = 'semantic',\n debugging = false\n}: {\n clientId: string;\n url: () => string;\n type?: EllmoApiType;\n debugging?: boolean;\n}) {\n return async (appContext: AppContext) => {\n // Fetch global Ellmo data\n const { data, error } = await fetchEllmoData({\n clientId,\n url: url(), // Use the function to get the URL\n type\n });\n\n // Call the page's getInitialProps if it exists\n let pageProps = {};\n if (appContext.Component.getInitialProps) {\n pageProps = await appContext.Component.getInitialProps(appContext.ctx);\n }\n\n // Return merged props with ellmo data inside pageProps\n return {\n pageProps: {\n ...pageProps,\n pageEllmoData: data,\n ...(debugging ? { pageEllmoError: error } : {}), // Only add error if debugging is enabled\n }\n };\n };\n}\n\n/**\n * Higher-order function to wrap getStaticProps with Ellmo data fetching\n * Automatically calls the page's existing getStaticProps and merges Ellmo data\n *\n * @param clientId - Your Ellmo client ID\n * @param url - Function that returns the URL path from GetStaticPropsContext\n * @param type - Content type: 'semantic' or 'structured' (default: 'semantic')\n * @param debugging - Include ellmoError in props for debugging (default: false)\n *\n * Returns ellmoData and ellmoError in props at both root level and inside pageProps for flexibility.\n *\n * @example\n * ```tsx\n * import { withEllmoStatic } from 'ellmo-ai-react';\n *\n * export const getStaticProps = withEllmoStatic({\n * clientId: process.env.ELLMO_CLIENT_ID!,\n * url: (context) => context.params?.slug ? `/blog/${context.params.slug}` : '/',\n * type: 'semantic',\n * debugging: process.env.NODE_ENV === 'development'\n * });\n *\n * // In page component - access directly\n * export default function BlogPost({ ellmoData, ellmoError }) {\n * return (\n * <div>\n * <EllmoApiSSR data={ellmoData} error={ellmoError} />\n * <h1>Blog Post</h1>\n * </div>\n * );\n * }\n *\n * // In _app.tsx - access via pageProps\n * function MyApp({ Component, pageProps }) {\n * return (\n * <div>\n * <EllmoApiSSR data={pageProps.pageEllmoData} error={pageProps.pageEllmoError} />\n * <Component {...pageProps} />\n * </div>\n * );\n * }\n * ```\n */\nexport function withEllmoStatic<P extends Record<string, any> = Record<string, any>>({\n clientId,\n url,\n type = 'semantic',\n debugging = false\n}: {\n clientId: string;\n url: () => string;\n type?: EllmoApiType;\n debugging?: boolean;\n}) {\n return async () => {\n return getEllmoStaticProps({\n clientId,\n url,\n type,\n debugging\n }) as Promise<{ props: P & { ellmoData: string | null; ellmoError?: string | null; pageProps?: { pageEllmoData: string | null; pageEllmoError?: string | null } } }>;\n };\n}\n\n/**\n * Build standardized Ellmo props synchronously from provided data and error.\n * Returns ellmoData/ellmoError at root and pageEllmoData/pageEllmoError inside pageProps.\n */\n// (removed buildEllmoProps to reduce surface area)\n\n/**\n * Fetch Ellmo data and return standardized props (no Next.js dependency).\n * Accepts a plain URL string and returns the same shape as buildEllmoProps.\n */\nexport async function getEllmoStaticProps({\n clientId,\n url,\n type = 'semantic',\n debugging = false\n}: {\n clientId: string;\n url: () => string;\n type?: EllmoApiType;\n debugging?: boolean;\n}): Promise<{ props: { ellmoData: string | null; ellmoError?: string | null; pageProps: { pageEllmoData: string | null; pageEllmoError?: string | null } } }> {\n const { data, error } = await fetchEllmoData({ clientId, url: url(), type });\n const props: { ellmoData: string | null; ellmoError?: string | null; pageProps: { pageEllmoData: string | null; pageEllmoError?: string | null } } = {\n ellmoData: data,\n pageProps: {\n pageEllmoData: data\n }\n };\n\n if (debugging) {\n props.ellmoError = error;\n props.pageProps.pageEllmoError = error;\n }\n\n return { props };\n}\n\n","/**\n * Custom header name for Ellmo AI to pass pathname information\n * Using 'x-ellmo-pathname' to avoid conflicts with other libraries\n */\nexport const ELLMO_PATHNAME_HEADER = 'x-ellmo-pathname';\n\n/**\n * Type definitions for Next.js middleware (to avoid requiring Next.js as a dependency)\n */\nexport interface EllmoRequest {\n nextUrl: {\n pathname: string;\n };\n}\n\nexport interface EllmoResponse {\n headers: {\n set(name: string, value: string): void;\n };\n}\n\n/**\n * Ellmo AI middleware helper for Next.js\n * \n * This is a vanilla helper that adds pathname to request headers.\n * It doesn't depend on Next.js - you provide the request/response objects.\n * \n * Usage in Next.js:\n * \n * ```ts\n * // middleware.ts\n * import { ellmoMiddleware } from 'ellmo-ai-react/middleware';\n * import { NextResponse } from 'next/server';\n * import type { NextRequest } from 'next/server';\n * \n * export function middleware(request: NextRequest) {\n * const response = NextResponse.next();\n * return ellmoMiddleware(request, response);\n * }\n * \n * export const config = {\n * matcher: [\n * '/((?!_next/static|_next/image|favicon.ico).*)',\n * ],\n * };\n * ```\n * \n * Composing with existing middleware:\n * \n * ```ts\n * export function middleware(request: NextRequest) {\n * let response = NextResponse.next();\n * \n * // Your logic here\n * if (someCondition) {\n * response = NextResponse.redirect(new URL('/login', request.url));\n * }\n * \n * // Apply Ellmo middleware\n * return ellmoMiddleware(request, response);\n * }\n * ```\n */\nexport function ellmoMiddleware<\n Req extends EllmoRequest,\n Res extends EllmoResponse\n>(\n request: Req,\n response: Res\n): Res {\n // Extract pathname from the request URL\n const pathname = request.nextUrl.pathname;\n \n // Add the pathname to a custom header\n response.headers.set(ELLMO_PATHNAME_HEADER, pathname);\n \n return response;\n}\n\n/**\n * Helper to extract pathname from headers object\n * \n * Usage in Next.js Server Components:\n * ```tsx\n * import { headers } from 'next/headers';\n * import { getPathnameFromHeaders } from 'ellmo-ai-react/middleware';\n * \n * export default function Page() {\n * const headersList = headers();\n * const pathname = getPathnameFromHeaders(headersList);\n * \n * return <EllmoApi clientId=\"your-id\" url={pathname} />;\n * }\n * ```\n */\nexport function getPathnameFromHeaders(headers: { get(name: string): string | null }): string | null {\n return headers.get(ELLMO_PATHNAME_HEADER);\n}\n\nexport default ellmoMiddleware;\n"],"names":[],"mappings":";;AAEA;AACO,MAAM,wBAAwB,GACnC,sDAAsD,CAAC;AAEzD;AACO,MAAM,wBAAwB,GAAG,IAAI,CAAC;AAE7C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA8BG;AACI,eAAe,cAAc,CAAC,EACnC,QAAQ,EACR,GAAG,EACH,IAAI,GAAG,UAAU,EAKlB,EAAA;AACC,IAAA,IAAI;;QAEF,IAAI,CAAC,QAAQ,EAAE;AACb,YAAA,MAAM,IAAI,KAAK,CAAC,wEAAwE,CAAC,CAAC;SAC3F;QAED,IAAI,CAAC,GAAG,EAAE;AACR,YAAA,MAAM,IAAI,KAAK,CAAC,oDAAoD,CAAC,CAAC;SACvE;AAED,QAAA,IAAI,GAAG,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE;;YAErB,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC;SACpC;;AAGD,QAAA,MAAM,UAAU,GAAG,kBAAkB,CAAC,GAAG,CAAC,CAAC;AAC3C,QAAA,MAAM,cAAc,GAAG,IAAI,KAAK,YAAY,GAAG,YAAY,GAAG,UAAU,CAAC;;QAGzE,MAAM,MAAM,GAAG,CAAA,EAAG,wBAAwB,CAAA,GAAA,EAAM,QAAQ,CAAA,GAAA,EAAM,cAAc,CAAA,GAAA,EAAM,UAAU,CAAA,CAAE,CAAC;;AAG/F,QAAA,MAAM,QAAQ,GAAG,MAAM,KAAK,CAAC,MAAM,EAAE;AACnC,YAAA,IAAI,EAAE,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AAC,GAAG,CAAC,qBAAqB,EAAE,QAAQ,CAAC,CAAC;AAEtD,IAAA,OAAO,QAAQ,CAAC;AAClB,CAAC;AAED;;;;;;;;;;;;;;;AAeG;AACG,SAAU,sBAAsB,CAAC,OAA6C,EAAA;AAClF,IAAA,OAAO,OAAO,CAAC,GAAG,CAAC,qBAAqB,CAAC,CAAC;AAC5C;;;;"}
|
package/dist/shared.d.ts
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
1
|
export type EllmoApiType = 'semantic' | 'structured';
|
|
2
|
+
/** Base URL for the embedded-html API (Supabase Edge Function). */
|
|
3
|
+
export declare const ELLMO_EMBEDDED_HTML_BASE = "https://api-s.tryellmo.ai/functions/v1/embedded-html";
|
|
4
|
+
/** Cache revalidation time in seconds (30 minutes). Used with Next.js fetch. */
|
|
5
|
+
export declare const CACHE_REVALIDATE_SECONDS = 1800;
|
|
2
6
|
/**
|
|
3
7
|
* Fetch data from Ellmo API
|
|
4
8
|
* Use this in getServerSideProps, getStaticProps, or getInitialProps
|
package/dist/shared.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"shared.d.ts","sourceRoot":"","sources":["../src/shared.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,YAAY,GAAG,UAAU,GAAG,YAAY,CAAC;AAErD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,wBAAsB,cAAc,CAAC,EACnC,QAAQ,EACR,GAAG,EACH,IAAiB,EAClB,EAAE;IACD,QAAQ,EAAE,MAAM,CAAC;IACjB,GAAG,EAAE,MAAM,CAAC;IACZ,IAAI,CAAC,EAAE,YAAY,CAAC;CACrB,GAAG,OAAO,CAAC;IAAE,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;IAAC,KAAK,EAAE,MAAM,GAAG,IAAI,CAAA;CAAE,CAAC,CAsCzD"}
|
|
1
|
+
{"version":3,"file":"shared.d.ts","sourceRoot":"","sources":["../src/shared.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,YAAY,GAAG,UAAU,GAAG,YAAY,CAAC;AAErD,mEAAmE;AACnE,eAAO,MAAM,wBAAwB,yDACmB,CAAC;AAEzD,gFAAgF;AAChF,eAAO,MAAM,wBAAwB,OAAO,CAAC;AAE7C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,wBAAsB,cAAc,CAAC,EACnC,QAAQ,EACR,GAAG,EACH,IAAiB,EAClB,EAAE;IACD,QAAQ,EAAE,MAAM,CAAC;IACjB,GAAG,EAAE,MAAM,CAAC;IACZ,IAAI,CAAC,EAAE,YAAY,CAAC;CACrB,GAAG,OAAO,CAAC;IAAE,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;IAAC,KAAK,EAAE,MAAM,GAAG,IAAI,CAAA;CAAE,CAAC,CAsCzD"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ellmo-ai-react",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.24",
|
|
4
4
|
"website": "https://www.tryellmo.ai?utm_source=npm&utm_medium=package&utm_campaign=ellmo-ai-react",
|
|
5
5
|
"description": "React component for integration with Ellmo AI Platform",
|
|
6
6
|
"main": "dist/index.js",
|