ellmo-ai-react 0.0.13 → 0.0.14
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/EllmoApiSSR.d.ts +1 -28
- package/dist/EllmoApiSSR.d.ts.map +1 -1
- package/dist/EllmoApiStatic.d.ts +0 -27
- package/dist/EllmoApiStatic.d.ts.map +1 -1
- package/dist/index.d.ts +40 -55
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +40 -58
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +41 -58
- package/dist/index.mjs.map +1 -1
- package/dist/shared.d.ts +30 -3
- package/dist/shared.d.ts.map +1 -1
- package/package.json +1 -1
package/dist/EllmoApiSSR.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
|
-
import {
|
|
2
|
+
import type { EllmoApiType } from './shared';
|
|
3
3
|
export type { EllmoApiType };
|
|
4
4
|
export interface EllmoApiSSRProps {
|
|
5
5
|
/**
|
|
@@ -60,32 +60,5 @@ export interface EllmoApiSSRProps {
|
|
|
60
60
|
* ```
|
|
61
61
|
*/
|
|
62
62
|
declare const EllmoApiSSR: React.FC<EllmoApiSSRProps>;
|
|
63
|
-
/**
|
|
64
|
-
* Helper function to fetch data from Ellmo API
|
|
65
|
-
* Use this in getServerSideProps or getStaticProps
|
|
66
|
-
*
|
|
67
|
-
* @example
|
|
68
|
-
* ```tsx
|
|
69
|
-
* export async function getServerSideProps(context) {
|
|
70
|
-
* const { data, error } = await fetchEllmoData({
|
|
71
|
-
* clientId: process.env.ELLMO_CLIENT_ID!,
|
|
72
|
-
* url: context.resolvedUrl,
|
|
73
|
-
* type: 'semantic'
|
|
74
|
-
* });
|
|
75
|
-
*
|
|
76
|
-
* return {
|
|
77
|
-
* props: { ellmoData: data, ellmoError: error }
|
|
78
|
-
* };
|
|
79
|
-
* }
|
|
80
|
-
* ```
|
|
81
|
-
*/
|
|
82
|
-
export declare function fetchEllmoData({ clientId, url, type }: {
|
|
83
|
-
clientId: string;
|
|
84
|
-
url: string;
|
|
85
|
-
type?: EllmoApiType;
|
|
86
|
-
}): Promise<{
|
|
87
|
-
data: string | null;
|
|
88
|
-
error: string | null;
|
|
89
|
-
}>;
|
|
90
63
|
export default EllmoApiSSR;
|
|
91
64
|
//# sourceMappingURL=EllmoApiSSR.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"EllmoApiSSR.d.ts","sourceRoot":"","sources":["../src/EllmoApiSSR.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAC;AAC1B,OAAO,
|
|
1
|
+
{"version":3,"file":"EllmoApiSSR.d.ts","sourceRoot":"","sources":["../src/EllmoApiSSR.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAC;AAC1B,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AAE7C,YAAY,EAAE,YAAY,EAAE,CAAC;AAE7B,MAAM,WAAW,gBAAgB;IAC/B;;OAEG;IACH,IAAI,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACrB;;OAEG;IACH,KAAK,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACtB;;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,MAAM,GAAG,IAAI,EAAE,KAAK,EAAE,MAAM,GAAG,IAAI,KAAK,KAAK,CAAC,SAAS,CAAC;CAC3E;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,QAAA,MAAM,WAAW,EAAE,KAAK,CAAC,EAAE,CAAC,gBAAgB,CAqC3C,CAAC;AAEF,eAAe,WAAW,CAAC"}
|
package/dist/EllmoApiStatic.d.ts
CHANGED
|
@@ -5,33 +5,6 @@ type AppContext = {
|
|
|
5
5
|
ctx: any;
|
|
6
6
|
router: any;
|
|
7
7
|
};
|
|
8
|
-
/**
|
|
9
|
-
* Helper function to fetch Ellmo data in getInitialProps
|
|
10
|
-
* Works on both server and client side
|
|
11
|
-
*
|
|
12
|
-
* @example
|
|
13
|
-
* ```tsx
|
|
14
|
-
* import { EllmoApiSSR, fetchEllmoDataStatic } from 'ellmo-ai-react';
|
|
15
|
-
*
|
|
16
|
-
* MyPage.getInitialProps = async (ctx) => {
|
|
17
|
-
* const { data, error } = await fetchEllmoDataStatic({
|
|
18
|
-
* clientId: process.env.NEXT_PUBLIC_ELLMO_CLIENT_ID!,
|
|
19
|
-
* url: ctx.asPath || '/',
|
|
20
|
-
* type: 'semantic'
|
|
21
|
-
* });
|
|
22
|
-
*
|
|
23
|
-
* return { ellmoData: data, ellmoError: error };
|
|
24
|
-
* };
|
|
25
|
-
* ```
|
|
26
|
-
*/
|
|
27
|
-
export declare function fetchEllmoDataStatic({ clientId, url, type }: {
|
|
28
|
-
clientId: string;
|
|
29
|
-
url: string;
|
|
30
|
-
type?: EllmoApiType;
|
|
31
|
-
}): Promise<{
|
|
32
|
-
data: string | null;
|
|
33
|
-
error: string | null;
|
|
34
|
-
}>;
|
|
35
8
|
/**
|
|
36
9
|
* Helper for _app.tsx to fetch global Ellmo data and merge with page props
|
|
37
10
|
* Automatically calls the page's getInitialProps and merges everything
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"EllmoApiStatic.d.ts","sourceRoot":"","sources":["../src/EllmoApiStatic.tsx"],"names":[],"mappings":"AAAA,OAAO,
|
|
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;AAGF;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,wBAAgB,YAAY,CAAC,EAC3B,QAAQ,EACR,GAAG,EACH,IAAiB,EAClB,EAAE;IACD,QAAQ,EAAE,MAAM,CAAC;IACjB,GAAG,EAAE,MAAM,CAAC;IACZ,IAAI,CAAC,EAAE,YAAY,CAAC;CACrB,IACe,YAAY,UAAU;;;;GAqBrC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,45 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
2
|
|
|
3
3
|
type EllmoApiType = 'semantic' | 'structured';
|
|
4
|
+
/**
|
|
5
|
+
* Fetch data from Ellmo API
|
|
6
|
+
* Use this in getServerSideProps, getStaticProps, or getInitialProps
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```tsx
|
|
10
|
+
* // In getServerSideProps (Next.js Pages Router)
|
|
11
|
+
* export async function getServerSideProps(context) {
|
|
12
|
+
* const { data, error } = await fetchEllmoData({
|
|
13
|
+
* clientId: process.env.ELLMO_CLIENT_ID!,
|
|
14
|
+
* url: context.resolvedUrl,
|
|
15
|
+
* type: 'semantic'
|
|
16
|
+
* });
|
|
17
|
+
*
|
|
18
|
+
* return {
|
|
19
|
+
* props: { ellmoData: data, ellmoError: error }
|
|
20
|
+
* };
|
|
21
|
+
* }
|
|
22
|
+
*
|
|
23
|
+
* // In getInitialProps (older Next.js API)
|
|
24
|
+
* MyPage.getInitialProps = async (ctx) => {
|
|
25
|
+
* const { data, error } = await fetchEllmoData({
|
|
26
|
+
* clientId: process.env.NEXT_PUBLIC_ELLMO_CLIENT_ID!,
|
|
27
|
+
* url: ctx.asPath || '/',
|
|
28
|
+
* type: 'semantic'
|
|
29
|
+
* });
|
|
30
|
+
*
|
|
31
|
+
* return { ellmoData: data, ellmoError: error };
|
|
32
|
+
* };
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
declare function fetchEllmoData({ clientId, url, type }: {
|
|
36
|
+
clientId: string;
|
|
37
|
+
url: string;
|
|
38
|
+
type?: EllmoApiType;
|
|
39
|
+
}): Promise<{
|
|
40
|
+
data: string | null;
|
|
41
|
+
error: string | null;
|
|
42
|
+
}>;
|
|
4
43
|
|
|
5
44
|
interface EllmoApiProps {
|
|
6
45
|
/**
|
|
@@ -116,66 +155,12 @@ interface EllmoApiSSRProps {
|
|
|
116
155
|
* ```
|
|
117
156
|
*/
|
|
118
157
|
declare const EllmoApiSSR: React.FC<EllmoApiSSRProps>;
|
|
119
|
-
/**
|
|
120
|
-
* Helper function to fetch data from Ellmo API
|
|
121
|
-
* Use this in getServerSideProps or getStaticProps
|
|
122
|
-
*
|
|
123
|
-
* @example
|
|
124
|
-
* ```tsx
|
|
125
|
-
* export async function getServerSideProps(context) {
|
|
126
|
-
* const { data, error } = await fetchEllmoData({
|
|
127
|
-
* clientId: process.env.ELLMO_CLIENT_ID!,
|
|
128
|
-
* url: context.resolvedUrl,
|
|
129
|
-
* type: 'semantic'
|
|
130
|
-
* });
|
|
131
|
-
*
|
|
132
|
-
* return {
|
|
133
|
-
* props: { ellmoData: data, ellmoError: error }
|
|
134
|
-
* };
|
|
135
|
-
* }
|
|
136
|
-
* ```
|
|
137
|
-
*/
|
|
138
|
-
declare function fetchEllmoData({ clientId, url, type }: {
|
|
139
|
-
clientId: string;
|
|
140
|
-
url: string;
|
|
141
|
-
type?: EllmoApiType;
|
|
142
|
-
}): Promise<{
|
|
143
|
-
data: string | null;
|
|
144
|
-
error: string | null;
|
|
145
|
-
}>;
|
|
146
158
|
|
|
147
159
|
type AppContext = {
|
|
148
160
|
Component: any;
|
|
149
161
|
ctx: any;
|
|
150
162
|
router: any;
|
|
151
163
|
};
|
|
152
|
-
/**
|
|
153
|
-
* Helper function to fetch Ellmo data in getInitialProps
|
|
154
|
-
* Works on both server and client side
|
|
155
|
-
*
|
|
156
|
-
* @example
|
|
157
|
-
* ```tsx
|
|
158
|
-
* import { EllmoApiSSR, fetchEllmoDataStatic } from 'ellmo-ai-react';
|
|
159
|
-
*
|
|
160
|
-
* MyPage.getInitialProps = async (ctx) => {
|
|
161
|
-
* const { data, error } = await fetchEllmoDataStatic({
|
|
162
|
-
* clientId: process.env.NEXT_PUBLIC_ELLMO_CLIENT_ID!,
|
|
163
|
-
* url: ctx.asPath || '/',
|
|
164
|
-
* type: 'semantic'
|
|
165
|
-
* });
|
|
166
|
-
*
|
|
167
|
-
* return { ellmoData: data, ellmoError: error };
|
|
168
|
-
* };
|
|
169
|
-
* ```
|
|
170
|
-
*/
|
|
171
|
-
declare function fetchEllmoDataStatic({ clientId, url, type }: {
|
|
172
|
-
clientId: string;
|
|
173
|
-
url: string;
|
|
174
|
-
type?: EllmoApiType;
|
|
175
|
-
}): Promise<{
|
|
176
|
-
data: string | null;
|
|
177
|
-
error: string | null;
|
|
178
|
-
}>;
|
|
179
164
|
/**
|
|
180
165
|
* Helper for _app.tsx to fetch global Ellmo data and merge with page props
|
|
181
166
|
* Automatically calls the page's getInitialProps and merges everything
|
|
@@ -295,4 +280,4 @@ declare function getPathnameFromHeaders(headers: {
|
|
|
295
280
|
get(name: string): string | null;
|
|
296
281
|
}): string | null;
|
|
297
282
|
|
|
298
|
-
export { ELLMO_PATHNAME_HEADER, EllmoApi, EllmoApiProps, EllmoApiSSR, EllmoApiSSRProps, EllmoRequest, EllmoResponse, ellmoMiddleware, fetchEllmoData,
|
|
283
|
+
export { ELLMO_PATHNAME_HEADER, EllmoApi, EllmoApiProps, EllmoApiSSR, EllmoApiSSRProps, EllmoApiType, EllmoRequest, EllmoResponse, ellmoMiddleware, fetchEllmoData, getPathnameFromHeaders, ellmoMiddleware as middleware, withEllmoApp };
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,IAAI,QAAQ,EAAE,MAAM,YAAY,CAAC;AACjD,YAAY,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAGhD,OAAO,EAAE,OAAO,IAAI,WAAW,EAAE,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,IAAI,QAAQ,EAAE,MAAM,YAAY,CAAC;AACjD,YAAY,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAGhD,OAAO,EAAE,OAAO,IAAI,WAAW,EAAE,MAAM,eAAe,CAAC;AACvD,YAAY,EAAE,gBAAgB,EAAE,MAAM,eAAe,CAAC;AAGtD,OAAO,EAAE,cAAc,EAAE,MAAM,UAAU,CAAC;AAC1C,YAAY,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AAG7C,OAAO,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAC;AAGhD,OAAO,EACL,eAAe,EACf,sBAAsB,EACtB,qBAAqB,EACtB,MAAM,cAAc,CAAC;AACtB,YAAY,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAChE,OAAO,EAAE,OAAO,IAAI,UAAU,EAAE,MAAM,cAAc,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -77,44 +77,6 @@ const EllmoApi = async ({ url, clientId, type, className = '', showErrors = fals
|
|
|
77
77
|
return null;
|
|
78
78
|
};
|
|
79
79
|
|
|
80
|
-
/**
|
|
81
|
-
* Shared helper function to fetch data from Ellmo API
|
|
82
|
-
* Used internally by fetchEllmoData and fetchEllmoDataStatic
|
|
83
|
-
*/
|
|
84
|
-
async function fetchEllmoDataCore({ clientId, url, type = 'semantic' }) {
|
|
85
|
-
try {
|
|
86
|
-
// Validate required props
|
|
87
|
-
if (!clientId) {
|
|
88
|
-
throw new Error('clientId is required. Please provide your Ellmo API client identifier.');
|
|
89
|
-
}
|
|
90
|
-
if (!url) {
|
|
91
|
-
throw new Error('url is required. Please provide the page pathname.');
|
|
92
|
-
}
|
|
93
|
-
if (url.includes('.')) {
|
|
94
|
-
// Sometimes urls contain a dot if they're images or other assets which are served by SSG (static generation)
|
|
95
|
-
return { data: null, error: null };
|
|
96
|
-
}
|
|
97
|
-
// Encode the URL path for the API call
|
|
98
|
-
const encodedUrl = encodeURIComponent(url);
|
|
99
|
-
const translatedType = type === 'structured' ? 'structured' : 'semantic';
|
|
100
|
-
// Build the API URL
|
|
101
|
-
const apiUrl = `https://www.tryellmo.ai/public-api/wordpress?c=${clientId}&t=${translatedType}&u=${encodedUrl}`;
|
|
102
|
-
// Fetch data
|
|
103
|
-
const response = await fetch(apiUrl, {
|
|
104
|
-
cache: 'no-store',
|
|
105
|
-
});
|
|
106
|
-
if (!response.ok) {
|
|
107
|
-
throw new Error(`API request failed with status ${response.status}`);
|
|
108
|
-
}
|
|
109
|
-
const data = await response.text();
|
|
110
|
-
return { data, error: null };
|
|
111
|
-
}
|
|
112
|
-
catch (err) {
|
|
113
|
-
const errorMessage = err instanceof Error ? err.message : 'Unknown error occurred';
|
|
114
|
-
return { data: null, error: errorMessage };
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
|
|
118
80
|
/**
|
|
119
81
|
* Ellmo API component for traditional SSR environments (Next.js Pages Router)
|
|
120
82
|
* This component receives pre-fetched data and renders it.
|
|
@@ -171,12 +133,14 @@ const EllmoApiSSR = ({ data = null, error = null, className = '', showErrors = f
|
|
|
171
133
|
// Return null if no data
|
|
172
134
|
return null;
|
|
173
135
|
};
|
|
136
|
+
|
|
174
137
|
/**
|
|
175
|
-
*
|
|
176
|
-
* Use this in getServerSideProps or
|
|
138
|
+
* Fetch data from Ellmo API
|
|
139
|
+
* Use this in getServerSideProps, getStaticProps, or getInitialProps
|
|
177
140
|
*
|
|
178
141
|
* @example
|
|
179
142
|
* ```tsx
|
|
143
|
+
* // In getServerSideProps (Next.js Pages Router)
|
|
180
144
|
* export async function getServerSideProps(context) {
|
|
181
145
|
* const { data, error } = await fetchEllmoData({
|
|
182
146
|
* clientId: process.env.ELLMO_CLIENT_ID!,
|
|
@@ -188,22 +152,10 @@ const EllmoApiSSR = ({ data = null, error = null, className = '', showErrors = f
|
|
|
188
152
|
* props: { ellmoData: data, ellmoError: error }
|
|
189
153
|
* };
|
|
190
154
|
* }
|
|
191
|
-
* ```
|
|
192
|
-
*/
|
|
193
|
-
async function fetchEllmoData({ clientId, url, type = 'semantic' }) {
|
|
194
|
-
return fetchEllmoDataCore({ clientId, url, type });
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
/**
|
|
198
|
-
* Helper function to fetch Ellmo data in getInitialProps
|
|
199
|
-
* Works on both server and client side
|
|
200
|
-
*
|
|
201
|
-
* @example
|
|
202
|
-
* ```tsx
|
|
203
|
-
* import { EllmoApiSSR, fetchEllmoDataStatic } from 'ellmo-ai-react';
|
|
204
155
|
*
|
|
156
|
+
* // In getInitialProps (older Next.js API)
|
|
205
157
|
* MyPage.getInitialProps = async (ctx) => {
|
|
206
|
-
* const { data, error } = await
|
|
158
|
+
* const { data, error } = await fetchEllmoData({
|
|
207
159
|
* clientId: process.env.NEXT_PUBLIC_ELLMO_CLIENT_ID!,
|
|
208
160
|
* url: ctx.asPath || '/',
|
|
209
161
|
* type: 'semantic'
|
|
@@ -213,9 +165,40 @@ async function fetchEllmoData({ clientId, url, type = 'semantic' }) {
|
|
|
213
165
|
* };
|
|
214
166
|
* ```
|
|
215
167
|
*/
|
|
216
|
-
async function
|
|
217
|
-
|
|
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
|
+
}
|
|
218
200
|
}
|
|
201
|
+
|
|
219
202
|
/**
|
|
220
203
|
* Helper for _app.tsx to fetch global Ellmo data and merge with page props
|
|
221
204
|
* Automatically calls the page's getInitialProps and merges everything
|
|
@@ -247,7 +230,7 @@ async function fetchEllmoDataStatic({ clientId, url, type = 'semantic' }) {
|
|
|
247
230
|
function withEllmoApp({ clientId, url, type = 'semantic' }) {
|
|
248
231
|
return async (appContext) => {
|
|
249
232
|
// Fetch global Ellmo data
|
|
250
|
-
const { data, error } = await
|
|
233
|
+
const { data, error } = await fetchEllmoData({
|
|
251
234
|
clientId,
|
|
252
235
|
url,
|
|
253
236
|
type
|
|
@@ -345,7 +328,6 @@ exports.EllmoApi = EllmoApi;
|
|
|
345
328
|
exports.EllmoApiSSR = EllmoApiSSR;
|
|
346
329
|
exports.ellmoMiddleware = ellmoMiddleware;
|
|
347
330
|
exports.fetchEllmoData = fetchEllmoData;
|
|
348
|
-
exports.fetchEllmoDataStatic = fetchEllmoDataStatic;
|
|
349
331
|
exports.getPathnameFromHeaders = getPathnameFromHeaders;
|
|
350
332
|
exports.middleware = ellmoMiddleware;
|
|
351
333
|
exports.withEllmoApp = withEllmoApp;
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../src/EllmoApi.tsx","../src/shared.ts","../src/EllmoApiSSR.tsx","../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","export type EllmoApiType = 'semantic' | 'structured';\n\n/**\n * Shared helper function to fetch data from Ellmo API\n * Used internally by fetchEllmoData and fetchEllmoDataStatic\n */\nexport async function fetchEllmoDataCore({\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 React from 'react';\nimport { fetchEllmoDataCore, 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\n/**\n * Helper function to fetch data from Ellmo API\n * Use this in getServerSideProps or getStaticProps\n * \n * @example\n * ```tsx\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 */\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 return fetchEllmoDataCore({ clientId, url, type });\n}\n\nexport default EllmoApiSSR;\n \n","import { fetchEllmoDataCore, 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 function to fetch Ellmo data in getInitialProps\n * Works on both server and client side\n * \n * @example\n * ```tsx\n * import { EllmoApiSSR, fetchEllmoDataStatic } from 'ellmo-ai-react';\n * \n * MyPage.getInitialProps = async (ctx) => {\n * const { data, error } = await fetchEllmoDataStatic({\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 fetchEllmoDataStatic({\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 return fetchEllmoDataCore({ clientId, url, type });\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 * @example\n * ```tsx\n * import { withEllmoApp } from 'ellmo-ai-react';\n * \n * function MyApp({ Component, pageProps, globalEllmoData, globalEllmoError }) {\n * return (\n * <>\n * <header>\n * <EllmoApiSSR data={globalEllmoData} error={globalEllmoError} />\n * </header>\n * <Component {...pageProps} />\n * </>\n * );\n * }\n * \n * MyApp.getInitialProps = withEllmoApp({\n * clientId: process.env.NEXT_PUBLIC_ELLMO_CLIENT_ID!,\n * url: '/global-header',\n * type: 'structured'\n * });\n * \n * export default MyApp;\n * ```\n */\nexport function withEllmoApp({\n clientId,\n url,\n type = 'semantic'\n}: {\n clientId: string;\n url: string;\n type?: EllmoApiType;\n}) {\n return async (appContext: AppContext) => {\n // Fetch global Ellmo data\n const { data, error } = await fetchEllmoDataStatic({\n clientId,\n 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\n return {\n pageProps,\n globalEllmoData: data,\n globalEllmoError: error\n };\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,GAA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,GAAG,CAAC,qBAAqB,CAAC,CAAC;AAC5C;;;;;;;;;;;;"}
|
|
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\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 * @example\n * ```tsx\n * import { withEllmoApp } from 'ellmo-ai-react';\n * \n * function MyApp({ Component, pageProps, globalEllmoData, globalEllmoError }) {\n * return (\n * <>\n * <header>\n * <EllmoApiSSR data={globalEllmoData} error={globalEllmoError} />\n * </header>\n * <Component {...pageProps} />\n * </>\n * );\n * }\n * \n * MyApp.getInitialProps = withEllmoApp({\n * clientId: process.env.NEXT_PUBLIC_ELLMO_CLIENT_ID!,\n * url: '/global-header',\n * type: 'structured'\n * });\n * \n * export default MyApp;\n * ```\n */\nexport function withEllmoApp({\n clientId,\n url,\n type = 'semantic'\n}: {\n clientId: string;\n url: string;\n type?: EllmoApiType;\n}) {\n return async (appContext: AppContext) => {\n // Fetch global Ellmo data\n const { data, error } = await fetchEllmoData({\n clientId,\n 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\n return {\n pageProps,\n globalEllmoData: data,\n globalEllmoError: error\n };\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,C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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
|
@@ -75,44 +75,6 @@ const EllmoApi = async ({ url, clientId, type, className = '', showErrors = fals
|
|
|
75
75
|
return null;
|
|
76
76
|
};
|
|
77
77
|
|
|
78
|
-
/**
|
|
79
|
-
* Shared helper function to fetch data from Ellmo API
|
|
80
|
-
* Used internally by fetchEllmoData and fetchEllmoDataStatic
|
|
81
|
-
*/
|
|
82
|
-
async function fetchEllmoDataCore({ clientId, url, type = 'semantic' }) {
|
|
83
|
-
try {
|
|
84
|
-
// Validate required props
|
|
85
|
-
if (!clientId) {
|
|
86
|
-
throw new Error('clientId is required. Please provide your Ellmo API client identifier.');
|
|
87
|
-
}
|
|
88
|
-
if (!url) {
|
|
89
|
-
throw new Error('url is required. Please provide the page pathname.');
|
|
90
|
-
}
|
|
91
|
-
if (url.includes('.')) {
|
|
92
|
-
// Sometimes urls contain a dot if they're images or other assets which are served by SSG (static generation)
|
|
93
|
-
return { data: null, error: null };
|
|
94
|
-
}
|
|
95
|
-
// Encode the URL path for the API call
|
|
96
|
-
const encodedUrl = encodeURIComponent(url);
|
|
97
|
-
const translatedType = type === 'structured' ? 'structured' : 'semantic';
|
|
98
|
-
// Build the API URL
|
|
99
|
-
const apiUrl = `https://www.tryellmo.ai/public-api/wordpress?c=${clientId}&t=${translatedType}&u=${encodedUrl}`;
|
|
100
|
-
// Fetch data
|
|
101
|
-
const response = await fetch(apiUrl, {
|
|
102
|
-
cache: 'no-store',
|
|
103
|
-
});
|
|
104
|
-
if (!response.ok) {
|
|
105
|
-
throw new Error(`API request failed with status ${response.status}`);
|
|
106
|
-
}
|
|
107
|
-
const data = await response.text();
|
|
108
|
-
return { data, error: null };
|
|
109
|
-
}
|
|
110
|
-
catch (err) {
|
|
111
|
-
const errorMessage = err instanceof Error ? err.message : 'Unknown error occurred';
|
|
112
|
-
return { data: null, error: errorMessage };
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
|
|
116
78
|
/**
|
|
117
79
|
* Ellmo API component for traditional SSR environments (Next.js Pages Router)
|
|
118
80
|
* This component receives pre-fetched data and renders it.
|
|
@@ -169,12 +131,14 @@ const EllmoApiSSR = ({ data = null, error = null, className = '', showErrors = f
|
|
|
169
131
|
// Return null if no data
|
|
170
132
|
return null;
|
|
171
133
|
};
|
|
134
|
+
|
|
172
135
|
/**
|
|
173
|
-
*
|
|
174
|
-
* Use this in getServerSideProps or
|
|
136
|
+
* Fetch data from Ellmo API
|
|
137
|
+
* Use this in getServerSideProps, getStaticProps, or getInitialProps
|
|
175
138
|
*
|
|
176
139
|
* @example
|
|
177
140
|
* ```tsx
|
|
141
|
+
* // In getServerSideProps (Next.js Pages Router)
|
|
178
142
|
* export async function getServerSideProps(context) {
|
|
179
143
|
* const { data, error } = await fetchEllmoData({
|
|
180
144
|
* clientId: process.env.ELLMO_CLIENT_ID!,
|
|
@@ -186,22 +150,10 @@ const EllmoApiSSR = ({ data = null, error = null, className = '', showErrors = f
|
|
|
186
150
|
* props: { ellmoData: data, ellmoError: error }
|
|
187
151
|
* };
|
|
188
152
|
* }
|
|
189
|
-
* ```
|
|
190
|
-
*/
|
|
191
|
-
async function fetchEllmoData({ clientId, url, type = 'semantic' }) {
|
|
192
|
-
return fetchEllmoDataCore({ clientId, url, type });
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
/**
|
|
196
|
-
* Helper function to fetch Ellmo data in getInitialProps
|
|
197
|
-
* Works on both server and client side
|
|
198
|
-
*
|
|
199
|
-
* @example
|
|
200
|
-
* ```tsx
|
|
201
|
-
* import { EllmoApiSSR, fetchEllmoDataStatic } from 'ellmo-ai-react';
|
|
202
153
|
*
|
|
154
|
+
* // In getInitialProps (older Next.js API)
|
|
203
155
|
* MyPage.getInitialProps = async (ctx) => {
|
|
204
|
-
* const { data, error } = await
|
|
156
|
+
* const { data, error } = await fetchEllmoData({
|
|
205
157
|
* clientId: process.env.NEXT_PUBLIC_ELLMO_CLIENT_ID!,
|
|
206
158
|
* url: ctx.asPath || '/',
|
|
207
159
|
* type: 'semantic'
|
|
@@ -211,9 +163,40 @@ async function fetchEllmoData({ clientId, url, type = 'semantic' }) {
|
|
|
211
163
|
* };
|
|
212
164
|
* ```
|
|
213
165
|
*/
|
|
214
|
-
async function
|
|
215
|
-
|
|
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
|
+
}
|
|
216
198
|
}
|
|
199
|
+
|
|
217
200
|
/**
|
|
218
201
|
* Helper for _app.tsx to fetch global Ellmo data and merge with page props
|
|
219
202
|
* Automatically calls the page's getInitialProps and merges everything
|
|
@@ -245,7 +228,7 @@ async function fetchEllmoDataStatic({ clientId, url, type = 'semantic' }) {
|
|
|
245
228
|
function withEllmoApp({ clientId, url, type = 'semantic' }) {
|
|
246
229
|
return async (appContext) => {
|
|
247
230
|
// Fetch global Ellmo data
|
|
248
|
-
const { data, error } = await
|
|
231
|
+
const { data, error } = await fetchEllmoData({
|
|
249
232
|
clientId,
|
|
250
233
|
url,
|
|
251
234
|
type
|
|
@@ -338,5 +321,5 @@ function getPathnameFromHeaders(headers) {
|
|
|
338
321
|
return headers.get(ELLMO_PATHNAME_HEADER);
|
|
339
322
|
}
|
|
340
323
|
|
|
341
|
-
export { ELLMO_PATHNAME_HEADER, EllmoApi, EllmoApiSSR, ellmoMiddleware, fetchEllmoData,
|
|
324
|
+
export { ELLMO_PATHNAME_HEADER, EllmoApi, EllmoApiSSR, ellmoMiddleware, fetchEllmoData, getPathnameFromHeaders, ellmoMiddleware as middleware, withEllmoApp };
|
|
342
325
|
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","sources":["../src/EllmoApi.tsx","../src/shared.ts","../src/EllmoApiSSR.tsx","../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","export type EllmoApiType = 'semantic' | 'structured';\n\n/**\n * Shared helper function to fetch data from Ellmo API\n * Used internally by fetchEllmoData and fetchEllmoDataStatic\n */\nexport async function fetchEllmoDataCore({\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 React from 'react';\nimport { fetchEllmoDataCore, 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\n/**\n * Helper function to fetch data from Ellmo API\n * Use this in getServerSideProps or getStaticProps\n * \n * @example\n * ```tsx\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 */\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 return fetchEllmoDataCore({ clientId, url, type });\n}\n\nexport default EllmoApiSSR;\n \n","import { fetchEllmoDataCore, 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 function to fetch Ellmo data in getInitialProps\n * Works on both server and client side\n * \n * @example\n * ```tsx\n * import { EllmoApiSSR, fetchEllmoDataStatic } from 'ellmo-ai-react';\n * \n * MyPage.getInitialProps = async (ctx) => {\n * const { data, error } = await fetchEllmoDataStatic({\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 fetchEllmoDataStatic({\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 return fetchEllmoDataCore({ clientId, url, type });\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 * @example\n * ```tsx\n * import { withEllmoApp } from 'ellmo-ai-react';\n * \n * function MyApp({ Component, pageProps, globalEllmoData, globalEllmoError }) {\n * return (\n * <>\n * <header>\n * <EllmoApiSSR data={globalEllmoData} error={globalEllmoError} />\n * </header>\n * <Component {...pageProps} />\n * </>\n * );\n * }\n * \n * MyApp.getInitialProps = withEllmoApp({\n * clientId: process.env.NEXT_PUBLIC_ELLMO_CLIENT_ID!,\n * url: '/global-header',\n * type: 'structured'\n * });\n * \n * export default MyApp;\n * ```\n */\nexport function withEllmoApp({\n clientId,\n url,\n type = 'semantic'\n}: {\n clientId: string;\n url: string;\n type?: EllmoApiType;\n}) {\n return async (appContext: AppContext) => {\n // Fetch global Ellmo data\n const { data, error } = await fetchEllmoDataStatic({\n clientId,\n 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\n return {\n pageProps,\n globalEllmoData: data,\n globalEllmoError: error\n };\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,QAA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CAAC;AACnB,QAAA,IAAI,UAAU,CAAC,SAAS,CAAC,eAAe,EAAE;AACxC,YAAA,SAAS,GAAG,MAAM,UAAU,CAAC,SAAS,CAAC,eAAe,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC;SACxE;;QAGD,OAAO;YACL,SAAS;AACT,YAAA,eAAe,EAAE,IAAI;AACrB,YAAA,gBAAgB,EAAE,KAAK;SACxB,CAAC;AACJ,KAAC,CAAC;AACJ;;ACpGA;;;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.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\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 * @example\n * ```tsx\n * import { withEllmoApp } from 'ellmo-ai-react';\n * \n * function MyApp({ Component, pageProps, globalEllmoData, globalEllmoError }) {\n * return (\n * <>\n * <header>\n * <EllmoApiSSR data={globalEllmoData} error={globalEllmoError} />\n * </header>\n * <Component {...pageProps} />\n * </>\n * );\n * }\n * \n * MyApp.getInitialProps = withEllmoApp({\n * clientId: process.env.NEXT_PUBLIC_ELLMO_CLIENT_ID!,\n * url: '/global-header',\n * type: 'structured'\n * });\n * \n * export default MyApp;\n * ```\n */\nexport function withEllmoApp({\n clientId,\n url,\n type = 'semantic'\n}: {\n clientId: string;\n url: string;\n type?: EllmoApiType;\n}) {\n return async (appContext: AppContext) => {\n // Fetch global Ellmo data\n const { data, error } = await fetchEllmoData({\n clientId,\n 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\n return {\n pageProps,\n globalEllmoData: data,\n globalEllmoError: error\n };\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,SAA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H,IAAI,GAAG,UAAU,EAKlB,EAAA;AACC,IAAA,OAAO,OAAO,UAAsB,KAAI;;QAEtC,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,GAAG,MAAM,cAAc,CAAC;YAC3C,QAAQ;YACR,GAAG;YACH,IAAI;AACL,SAAA,CAAC,CAAC;;QAGH,IAAI,SAAS,GAAG,EAAE,CAAC;AACnB,QAAA,IAAI,UAAU,CAAC,SAAS,CAAC,eAAe,EAAE;AACxC,YAAA,SAAS,GAAG,MAAM,UAAU,CAAC,SAAS,CAAC,eAAe,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC;SACxE;;QAGD,OAAO;YACL,SAAS;AACT,YAAA,eAAe,EAAE,IAAI;AACrB,YAAA,gBAAgB,EAAE,KAAK;SACxB,CAAC;AACJ,KAAC,CAAC;AACJ;;ACtEA;;;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/shared.d.ts
CHANGED
|
@@ -1,9 +1,36 @@
|
|
|
1
1
|
export type EllmoApiType = 'semantic' | 'structured';
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
4
|
-
*
|
|
3
|
+
* Fetch data from Ellmo API
|
|
4
|
+
* Use this in getServerSideProps, getStaticProps, or getInitialProps
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```tsx
|
|
8
|
+
* // In getServerSideProps (Next.js Pages Router)
|
|
9
|
+
* export async function getServerSideProps(context) {
|
|
10
|
+
* const { data, error } = await fetchEllmoData({
|
|
11
|
+
* clientId: process.env.ELLMO_CLIENT_ID!,
|
|
12
|
+
* url: context.resolvedUrl,
|
|
13
|
+
* type: 'semantic'
|
|
14
|
+
* });
|
|
15
|
+
*
|
|
16
|
+
* return {
|
|
17
|
+
* props: { ellmoData: data, ellmoError: error }
|
|
18
|
+
* };
|
|
19
|
+
* }
|
|
20
|
+
*
|
|
21
|
+
* // In getInitialProps (older Next.js API)
|
|
22
|
+
* MyPage.getInitialProps = async (ctx) => {
|
|
23
|
+
* const { data, error } = await fetchEllmoData({
|
|
24
|
+
* clientId: process.env.NEXT_PUBLIC_ELLMO_CLIENT_ID!,
|
|
25
|
+
* url: ctx.asPath || '/',
|
|
26
|
+
* type: 'semantic'
|
|
27
|
+
* });
|
|
28
|
+
*
|
|
29
|
+
* return { ellmoData: data, ellmoError: error };
|
|
30
|
+
* };
|
|
31
|
+
* ```
|
|
5
32
|
*/
|
|
6
|
-
export declare function
|
|
33
|
+
export declare function fetchEllmoData({ clientId, url, type }: {
|
|
7
34
|
clientId: string;
|
|
8
35
|
url: string;
|
|
9
36
|
type?: EllmoApiType;
|
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
|
|
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"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ellmo-ai-react",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.14",
|
|
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",
|