-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjavascript_client_example.js
More file actions
196 lines (172 loc) · 6.01 KB
/
Copy pathjavascript_client_example.js
File metadata and controls
196 lines (172 loc) · 6.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
JavaScript Client Example (browser-compatible)
- Uses Fetch API for HTTP requests
- Demonstrates CRUD operations against Applications API
- Shows async/await and Promise-based error handling
- Includes examples for Tracking endpoints
How to use (in browser or with bundler):
- Set API_BASE_URL to your API endpoint (e.g., https://your-service-name.onrender.com)
- If your API requires an API key, set API_KEY and include the header
*/
const API_BASE_URL = (typeof window !== 'undefined' && window.API_BASE_URL)
|| (typeof process !== 'undefined' && process.env.API_BASE_URL)
|| 'https://your-service-name.onrender.com';
const API_KEY = (typeof window !== 'undefined' && window.API_KEY)
|| (typeof process !== 'undefined' && process.env.API_KEY)
|| null;
function buildHeaders(extra = {}) {
const headers = {
'Content-Type': 'application/json',
...extra,
};
if (API_KEY) headers['X-API-Key'] = API_KEY;
return headers;
}
async function handleResponse(resp) {
const contentType = resp.headers.get('content-type') || '';
let data = null;
if (contentType.includes('application/json')) {
data = await resp.json().catch(() => null);
} else {
data = await resp.text().catch(() => null);
}
if (!resp.ok) {
const error = new Error((data && (data.message || data.error)) || `HTTP ${resp.status}`);
error.status = resp.status;
error.data = data;
throw error;
}
return data;
}
// CRUD Operations using async/await
export async function listApplications(params = {}) {
const url = new URL(`${API_BASE_URL}/api/applications/`);
Object.entries(params).forEach(([k, v]) => {
if (Array.isArray(v)) v.forEach((val) => url.searchParams.append(k, val));
else if (v !== undefined && v !== null) url.searchParams.set(k, v);
});
const resp = await fetch(url, { headers: buildHeaders() });
return handleResponse(resp);
}
export async function createApplication(app) {
const resp = await fetch(`${API_BASE_URL}/api/applications/`, {
method: 'POST',
headers: buildHeaders(),
body: JSON.stringify(app),
});
return handleResponse(resp);
}
export async function getApplication(id) {
const resp = await fetch(`${API_BASE_URL}/api/applications/${id}`, {
headers: buildHeaders(),
});
return handleResponse(resp);
}
export async function updateApplication(id, patch) {
const resp = await fetch(`${API_BASE_URL}/api/applications/${id}`, {
method: 'PUT',
headers: buildHeaders(),
body: JSON.stringify(patch),
});
return handleResponse(resp);
}
export async function updateApplicationStatus(id, status) {
const url = new URL(`${API_BASE_URL}/api/applications/${id}/status`);
url.searchParams.set('status', status);
const resp = await fetch(url, {
method: 'PATCH',
headers: buildHeaders(),
});
return handleResponse(resp);
}
export async function deleteApplication(id) {
const resp = await fetch(`${API_BASE_URL}/api/applications/${id}`, {
method: 'DELETE',
headers: buildHeaders(),
});
return handleResponse(resp);
}
// Tracking endpoints
export async function quickTrack(payload) {
const resp = await fetch(`${API_BASE_URL}/api/tracking/track`, {
method: 'POST',
headers: buildHeaders(),
body: JSON.stringify(payload),
});
return handleResponse(resp);
}
export async function getAnalyticsStats() {
const resp = await fetch(`${API_BASE_URL}/api/tracking/stats`, {
headers: buildHeaders(),
});
return handleResponse(resp);
}
export async function getApplicationHistory(appId) {
const resp = await fetch(`${API_BASE_URL}/api/tracking/application-history/${appId}`, {
headers: buildHeaders(),
});
return handleResponse(resp);
}
// Demo runner using async/await with try/catch
export async function runAsyncDemo() {
try {
// Create
const created = await createApplication({
company_name: 'Example Corp',
job_title: 'Frontend Engineer',
status: 'applied',
});
console.log('Created:', created);
const id = (created && created.data && created.data.id) || created.id;
// Read
const fetched = await getApplication(id);
console.log('Fetched:', fetched);
// Update
const updated = await updateApplication(id, {
status: 'reviewing',
notes: 'Screening scheduled',
});
console.log('Updated:', updated);
// Patch status
const patched = await updateApplicationStatus(id, 'phone_screen');
console.log('Patched status:', patched);
// List
const list = await listApplications({ limit: 5 });
console.log('List:', list);
// Tracking
const tracked = await quickTrack({ company: 'TrackCo', title: 'Dev' });
console.log('Quick track:', tracked);
const stats = await getAnalyticsStats();
console.log('Analytics:', stats);
// Delete
const del = await deleteApplication(id);
console.log('Deleted:', del);
} catch (err) {
console.error('Async demo error:', err && err.message ? err.message : err);
if (err && err.data) console.error('Error data:', err.data);
}
}
// Promise-based usage example (no async/await)
export function runPromiseDemo() {
createApplication({ company_name: 'Promise Inc', job_title: 'SWE I' })
.then((created) => {
console.log('Created:', created);
const id = (created && created.data && created.data.id) || created.id;
return getApplication(id).then((fetched) => ({ id, fetched }));
})
.then(({ id }) => updateApplicationStatus(id, 'reviewing').then(() => id))
.then((id) => deleteApplication(id))
.then((res) => console.log('Cleanup result:', res))
.catch((err) => {
console.error('Promise demo error:', err && err.message ? err.message : err);
if (err && err.data) console.error('Error data:', err.data);
});
}
// If executed directly in Node with experimental ESM or bundlers, you can run demos:
if (typeof window === 'undefined') {
// Attempt to detect direct execution
const isDirect = process.argv && process.argv[1] && process.argv[1].includes('javascript_client_example');
if (isDirect) {
runAsyncDemo().then(() => runPromiseDemo());
}
}