EverydayTech Platform - Developer Reference
Complete Source Code Documentation - All Applications
Loading...
Searching...
No Matches
ApiTest.tsx
Go to the documentation of this file.
1"use client";
2import { useState } from "react";
3import { fieldpine } from "@/lib/fieldpine";
4
5export default function ApiTest() {
6 const [testResults, setTestResults] = useState<string>("");
7 const [loading, setLoading] = useState(false);
8
9 const testEnvironmentVariables = () => {
10 setLoading(true);
11 setTestResults("=== ENVIRONMENT VARIABLES TEST ===\n");
12
13 try {
14 const results = [];
15
16 // Test environment variables
17 results.push("Environment Variables:");
18 results.push(`NEXT_PUBLIC_FIELDPINE_BASE_URL: ${process.env.NEXT_PUBLIC_FIELDPINE_BASE_URL || 'Not set'}`);
19 results.push(`NEXT_PUBLIC_FIELDPINE_RETAILER_ID: ${process.env.NEXT_PUBLIC_FIELDPINE_RETAILER_ID || 'Not set'}`);
20 results.push(`NEXT_PUBLIC_APP_ENV: ${process.env.NEXT_PUBLIC_APP_ENV || 'Not set'}`);
21 results.push(`NEXT_PUBLIC_DEMO_MODE: ${process.env.NEXT_PUBLIC_DEMO_MODE || 'Not set'}`);
22
23 // Test API URL construction
24 const baseUrl = process.env.NEXT_PUBLIC_FIELDPINE_BASE_URL || "https://www.fieldpine.com";
25 const retailerId = process.env.NEXT_PUBLIC_FIELDPINE_RETAILER_ID || "1_2_3_4";
26 const apiUrl = `${baseUrl}/RetailAPI_${retailerId}`;
27
28 results.push("\n=== API CONFIGURATION ===");
29 results.push(`Base URL: ${baseUrl}`);
30 results.push(`Retailer ID: ${retailerId}`);
31 results.push(`Full API URL: ${apiUrl}`);
32
33 // Test fieldpine instance
34 results.push("\n=== FIELDPINE INSTANCE TEST ===");
35 try {
36 const testUrl = fieldpine.buildUrl('/products', { limit: 1 });
37 results.push(`Built URL: ${testUrl}`);
38 results.push("✅ Fieldpine instance working");
39 } catch (error) {
40 results.push(`❌ Fieldpine instance error: ${error}`);
41 }
42
43 setTestResults(results.join('\n'));
44 } catch (error) {
45 setTestResults(`❌ Error: ${error}`);
46 }
47
48 setLoading(false);
49 };
50
51 const testApiCall = async () => {
52 setLoading(true);
53 setTestResults("=== API CALL TEST ===\n");
54
55 try {
56 // Set demo authentication
57 fieldpine.setAuth("demo-api-key", "1_2_3_4");
58
59 const results = [];
60 results.push("Testing API call to /products endpoint...");
61
62 const url = fieldpine.buildUrl('/products', { limit: 2 });
63 results.push(`URL: ${url}`);
64
65 // Test the API call
66 try {
67 const response = await fieldpine.getProducts({ limit: 2 });
68 results.push("✅ API call successful");
69 results.push(`Response type: ${typeof response}`);
70 results.push(`Response: ${JSON.stringify(response, null, 2)}`);
71 } catch (apiError: any) {
72 results.push(`❌ API call failed: ${apiError.message}`);
73 results.push(`Error details: ${JSON.stringify(apiError, null, 2)}`);
74
75 // Try a direct fetch to see the raw response
76 try {
77 const directResponse = await fetch(url);
78 results.push(`\nDirect fetch status: ${directResponse.status}`);
79 results.push(`Direct fetch ok: ${directResponse.ok}`);
80 const text = await directResponse.text();
81 results.push(`Direct fetch response: ${text.substring(0, 500)}`);
82 } catch (fetchError: any) {
83 results.push(`Direct fetch also failed: ${fetchError.message}`);
84 }
85 }
86
87 setTestResults(results.join('\n'));
88 } catch (error: any) {
89 setTestResults(`❌ Error: ${error.message}`);
90 }
91
92 setLoading(false);
93 };
94
95 const clearResults = () => {
96 setTestResults("");
97 };
98
99 return (
100 <div className="p-6 bg-white rounded-lg shadow">
101 <h2 className="text-2xl font-bold mb-4">API Test Console</h2>
102
103 <div className="space-y-4">
104 <div className="flex flex-wrap gap-2">
105 <button
106 onClick={testEnvironmentVariables}
107 disabled={loading}
108 className="px-4 py-2 bg-brand text-white rounded hover:opacity-90 disabled:opacity-50"
109 >
110 Test Environment Variables
111 </button>
112
113 <button
114 onClick={testApiCall}
115 disabled={loading}
116 className="px-4 py-2 bg-success text-white rounded hover:opacity-90 disabled:opacity-50"
117 >
118 Test API Call
119 </button>
120
121 <button
122 onClick={clearResults}
123 disabled={loading}
124 className="px-4 py-2 bg-surface border-2 border-border text-text rounded hover:bg-surface-2 disabled:opacity-50"
125 >
126 Clear
127 </button>
128 </div>
129
130 {loading && (
131 <div className="text-blue-600 font-medium">
132 Running test...
133 </div>
134 )}
135
136 {testResults && (
137 <div className="mt-4">
138 <h3 className="font-semibold mb-2">Test Results:</h3>
139 <pre className="bg-gray-100 p-4 rounded text-sm overflow-auto max-h-96 whitespace-pre-wrap">
140 {testResults}
141 </pre>
142 </div>
143 )}
144 </div>
145 </div>
146 );
147}