@@ -5,11 +5,15 @@ export const cookieJarPlugin = {
55 docUrl : 'https://github.com/miller-28/luminara/blob/master/docs/plugins/cookie-jar.md' ,
66 examples : [
77 {
8- id : 'cookie-jar-basic ' ,
9- title : 'Basic Cookie Management ' ,
8+ id : 'cookie-jar-server-side-only ' ,
9+ title : 'Server-Side Only Plugin ' ,
1010 code : `import { createLuminara } from 'luminara';
1111import { cookieJarPlugin } from 'luminara-cookie-jar';
1212
13+ // ⚠️ SERVER-SIDE ONLY (Node.js, SSR, CLI tools)
14+ // Browsers have built-in cookie management
15+ // This plugin is for environments WITHOUT automatic cookies
16+
1317const client = createLuminara({
1418 baseURL: 'https://api.example.com',
1519 plugins: [cookieJarPlugin()]
@@ -24,252 +28,21 @@ await client.post('/login', {
2428// Subsequent requests include cookies automatically
2529const profile = await client.getJson('/profile');
2630console.log('Profile:', profile.data);` ,
27- run : async ( updateOutput , signal , options = { } ) => {
28- try {
29- // Try to import the plugin from CDN
30- const { cookieJarPlugin : plugin } = await import ( 'https://cdn.skypack.dev/luminara-cookie-jar' ) ;
31-
32- const client = createLuminara ( {
33- baseURL : 'https://httpbingo.org' ,
34- plugins : [ plugin ( ) ] ,
35- verbose : options . verbose || false
36- } ) ;
37-
38- updateOutput ( '🔐 Simulating login with cookie...\n' ) ;
39-
40- // Simulate login by manually setting a cookie
41- await client . jar . setCookie (
42- 'session=demo_session_12345; Path=/; HttpOnly' ,
43- 'https://httpbingo.org'
44- ) ;
45-
46- updateOutput ( '✅ Cookie set: session=demo_session_12345\n\n' ) ;
47-
48- // Make request - cookie will be sent automatically
49- updateOutput ( '📤 Making authenticated request...\n' ) ;
50- const response = await client . getJson ( '/headers' , { signal } ) ;
51-
52- const cookieHeader = response . data . headers ?. Cookie || 'No cookies sent' ;
53-
54- return `✅ Cookie automatically sent!\n\nCookie Header: ${ cookieHeader } \n\n🍪 Cookies in jar: ${ ( await client . jar . getCookies ( 'https://httpbingo.org' ) ) . length } ` ;
55- } catch ( err ) {
56- if ( err . code === 'ERR_MODULE_NOT_FOUND' ) {
57- return '⚠️ luminara-cookie-jar plugin not installed\n\nTo try this example:\nnpm install luminara-cookie-jar\n\nLearn more: https://www.npmjs.com/package/luminara-cookie-jar' ;
58- }
59- throw err ;
60- }
61- }
62- } ,
63- {
64- id : 'cookie-jar-manual' ,
65- title : 'Manual Cookie Operations' ,
66- code : `import { createLuminara } from 'luminara';
67- import { cookieJarPlugin } from 'luminara-cookie-jar';
68-
69- const client = createLuminara({
70- baseURL: 'https://api.example.com',
71- plugins: [cookieJarPlugin()]
72- });
73-
74- // Set cookies manually
75- await client.jar.setCookie(
76- 'session=abc123; Path=/; HttpOnly',
77- 'https://api.example.com'
78- );
79-
80- // Get all cookies
81- const cookies = await client.jar.getCookies('https://api.example.com');
82- console.log('Cookies:', cookies);
83-
84- // Get cookie string for request
85- const cookieString = await client.jar.getCookieString('https://api.example.com');
86- console.log('Cookie header:', cookieString);
87-
88- // Remove all cookies
89- await client.jar.removeAllCookies();` ,
90- run : async ( updateOutput , signal , options = { } ) => {
91- try {
92- const { cookieJarPlugin : plugin } = await import ( 'https://cdn.skypack.dev/luminara-cookie-jar' ) ;
93-
94- const client = createLuminara ( {
95- baseURL : 'https://httpbingo.org' ,
96- plugins : [ plugin ( ) ] ,
97- verbose : options . verbose || false
98- } ) ;
99-
100- const url = 'https://httpbingo.org' ;
101-
102- // Set multiple cookies
103- updateOutput ( '📝 Setting cookies manually...\n' ) ;
104- await client . jar . setCookie ( 'session=abc123; Path=/; HttpOnly' , url ) ;
105- await client . jar . setCookie ( 'user_id=12345; Path=/; Max-Age=3600' , url ) ;
106- await client . jar . setCookie ( 'theme=dark; Path=/' , url ) ;
107-
108- updateOutput ( '✅ Set 3 cookies\n\n' ) ;
109-
110- // Get all cookies
111- const allCookies = await client . jar . getCookies ( url ) ;
112- updateOutput ( `🍪 Total cookies: ${ allCookies . length } \n` ) ;
113- allCookies . forEach ( cookie => {
114- updateOutput ( ` - ${ cookie . key } =${ cookie . value } \n` ) ;
115- } ) ;
116-
117- // Get cookie string
118- const cookieString = await client . jar . getCookieString ( url ) ;
119- updateOutput ( `\n📋 Cookie header: ${ cookieString } \n\n` ) ;
120-
121- // Remove all cookies
122- await client . jar . removeAllCookies ( ) ;
123- const remaining = await client . jar . getCookies ( url ) ;
124-
125- return `🗑️ Removed all cookies\n✅ Remaining: ${ remaining . length } ` ;
126- } catch ( err ) {
127- if ( err . code === 'ERR_MODULE_NOT_FOUND' ) {
128- return '⚠️ luminara-cookie-jar plugin not installed\n\nTo try this example:\nnpm install luminara-cookie-jar\n\nLearn more: https://www.npmjs.com/package/luminara-cookie-jar' ;
129- }
130- throw err ;
131- }
132- }
133- } ,
134- {
135- id : 'cookie-jar-shared' ,
136- title : 'Shared Cookie Jar' ,
137- code : `import { createLuminara } from 'luminara';
138- import { cookieJarPlugin } from 'luminara-cookie-jar';
139- import { CookieJar } from 'tough-cookie';
140-
141- // Create shared jar
142- const sharedJar = new CookieJar();
143-
144- // Two clients sharing the same jar
145- const client1 = createLuminara({
146- baseURL: 'https://api.example.com',
147- plugins: [cookieJarPlugin({ jar: sharedJar })]
148- });
149-
150- const client2 = createLuminara({
151- baseURL: 'https://api.example.com',
152- plugins: [cookieJarPlugin({ jar: sharedJar })]
153- });
154-
155- // Client1 logs in
156- await client1.post('/login', credentials);
157-
158- // Client2 automatically has the session!
159- const profile = await client2.getJson('/profile');
160- console.log('Shared session works!');` ,
161- run : async ( updateOutput , signal , options = { } ) => {
162- try {
163- const cookieJarModule = await import ( 'https://cdn.skypack.dev/luminara-cookie-jar' ) ;
164- const { cookieJarPlugin : plugin } = cookieJarModule ;
165- const { CookieJar } = await import ( 'https://cdn.skypack.dev/tough-cookie' ) ;
166-
167- // Create shared jar
168- const sharedJar = new CookieJar ( ) ;
169-
170- const client1 = createLuminara ( {
171- baseURL : 'https://httpbingo.org' ,
172- plugins : [ plugin ( { jar : sharedJar } ) ] ,
173- verbose : options . verbose || false
174- } ) ;
175-
176- const client2 = createLuminara ( {
177- baseURL : 'https://httpbingo.org' ,
178- plugins : [ plugin ( { jar : sharedJar } ) ] ,
179- verbose : options . verbose || false
180- } ) ;
181-
182- // Client1 sets cookie
183- updateOutput ( '🔵 Client 1: Setting authentication cookie...\n' ) ;
184- await client1 . jar . setCookie (
185- 'auth_token=shared_token_789; Path=/; HttpOnly' ,
186- 'https://httpbingo.org'
187- ) ;
188-
189- const client1Cookies = await client1 . jar . getCookies ( 'https://httpbingo.org' ) ;
190- updateOutput ( `✅ Client 1 cookies: ${ client1Cookies . length } \n\n` ) ;
191-
192- // Client2 automatically has the same cookie
193- updateOutput ( '🟢 Client 2: Checking shared cookies...\n' ) ;
194- const client2Cookies = await client2 . jar . getCookies ( 'https://httpbingo.org' ) ;
195- updateOutput ( `✅ Client 2 cookies: ${ client2Cookies . length } \n\n` ) ;
196-
197- // Verify they're the same jar
198- const sameJar = client1 . jar === client2 . jar ;
199-
200- return `🔗 Shared jar: ${ sameJar ? 'YES' : 'NO' } \n\n🍪 Both clients share the same cookies!\n Client 1: ${ client1Cookies . map ( c => c . key ) . join ( ', ' ) } \n Client 2: ${ client2Cookies . map ( c => c . key ) . join ( ', ' ) } ` ;
201- } catch ( err ) {
202- if ( err . code === 'ERR_MODULE_NOT_FOUND' ) {
203- return '⚠️ luminara-cookie-jar plugin not installed\n\nTo try this example:\nnpm install luminara-cookie-jar\n\nLearn more: https://www.npmjs.com/package/luminara-cookie-jar' ;
204- }
205- throw err ;
206- }
207- }
208- } ,
209- {
210- id : 'cookie-jar-ssr' ,
211- title : 'SSR / Server-Side Usage' ,
212- code : `import { createLuminara } from 'luminara';
213- import { cookieJarPlugin } from 'luminara-cookie-jar';
214-
215- // Perfect for Node.js, SSR, CLI tools
216- // Cookies are NOT automatically managed in server environments
217- // This plugin handles Cookie / Set-Cookie headers automatically
218-
219- async function handleRequest(req, res) {
220- // Create per-request client
221- const client = createLuminara({
222- baseURL: 'https://api.backend.com',
223- plugins: [cookieJarPlugin()]
224- });
225-
226- // API sets session cookie
227- await client.post('/auth/login', credentials);
228-
229- // Subsequent API calls include session cookie
230- const userData = await client.getJson('/user/profile');
231-
232- res.json(userData);
233- }` ,
234- run : async ( updateOutput , signal , options = { } ) => {
235- try {
236- const { cookieJarPlugin : plugin } = await import ( 'https://cdn.skypack.dev/luminara-cookie-jar' ) ;
237-
238- updateOutput ( '🖥️ Simulating Server-Side Request\n\n' ) ;
239-
240- // Simulate per-request client (like in SSR)
241- const client = createLuminara ( {
242- baseURL : 'https://httpbingo.org' ,
243- plugins : [ plugin ( ) ] ,
244- verbose : options . verbose || false
245- } ) ;
246-
247- updateOutput ( '1️⃣ Creating per-request client with cookie jar\n' ) ;
248- updateOutput ( '2️⃣ Simulating backend authentication...\n\n' ) ;
249-
250- // Simulate server setting cookies
251- const authResponse = await client . get ( '/cookies/set?session=ssr_demo_session' , { signal } ) ;
252- updateOutput ( `✅ Backend set cookies (status: ${ authResponse . status } )\n\n` ) ;
253-
254- // Check cookies in jar
255- const cookies = await client . jar . getCookies ( 'https://httpbingo.org' ) ;
256- updateOutput ( `3️⃣ Cookies stored in jar: ${ cookies . length } \n` ) ;
257- cookies . forEach ( cookie => {
258- updateOutput ( ` 🍪 ${ cookie . key } =${ cookie . value } \n` ) ;
259- } ) ;
260-
261- updateOutput ( '\n4️⃣ Making follow-up request...\n' ) ;
262- const followUpResponse = await client . getJson ( '/headers' , { signal } ) ;
263-
264- const cookieHeader = followUpResponse . data . headers ?. Cookie || 'none' ;
265-
266- return `✅ Cookies automatically sent!\n\n📤 Cookie header: ${ cookieHeader } \n\n💡 Perfect for SSR where cookies aren't automatic!` ;
267- } catch ( err ) {
268- if ( err . code === 'ERR_MODULE_NOT_FOUND' ) {
269- return '⚠️ luminara-cookie-jar plugin not installed\n\nTo try this example:\nnpm install luminara-cookie-jar\n\nLearn more: https://www.npmjs.com/package/luminara-cookie-jar' ;
270- }
271- throw err ;
272- }
31+ run : async ( updateOutput ) => {
32+ updateOutput ( '⚠️ SERVER-SIDE ONLY PLUGIN\n\n' ) ;
33+ updateOutput ( 'This plugin is designed for server-side environments:\n' ) ;
34+ updateOutput ( ' • Node.js applications\n' ) ;
35+ updateOutput ( ' • Server-Side Rendering (SSR)\n' ) ;
36+ updateOutput ( ' • CLI tools and scripts\n' ) ;
37+ updateOutput ( ' • Backend services\n\n' ) ;
38+ updateOutput ( '🌐 Browsers have BUILT-IN cookie management!\n\n' ) ;
39+ updateOutput ( 'Browsers automatically:\n' ) ;
40+ updateOutput ( ' ✅ Store cookies from Set-Cookie headers\n' ) ;
41+ updateOutput ( ' ✅ Send cookies with matching requests\n' ) ;
42+ updateOutput ( ' ✅ Handle cookie expiration and security\n\n' ) ;
43+ updateOutput ( '💡 You do NOT need this plugin in browser environments.\n\n' ) ;
44+ updateOutput ( '📦 npm: luminara-cookie-jar\n' ) ;
45+ return '📖 See documentation for server-side usage examples' ;
27346 }
27447 }
27548 ]
0 commit comments