@attunedev/mighty-core-api-client
Version:
JavaScript SDK for the MIGHTy Core API
187 lines (167 loc) • 7.3 kB
JavaScript
class MightyClient {
constructor({ baseUrl, apiKey, userId }) {
if (!baseUrl || !apiKey || !userId) {
throw new Error('baseUrl, apiKey, and userId are required');
}
this.baseUrl = baseUrl.replace(/\/$/, '');
this.apiKey = apiKey;
this.userId = userId;
}
async request(method, path, { query, body, headers = {} } = {}) {
const url = new URL(`${this.baseUrl.replace(/\/+$/, '')}/${path.replace(/^\/+/, '')}`);
console.log('➡️ Fetching:', url.toString());
if (query) {
Object.entries(query).forEach(([key, value]) => {
if (value !== undefined && value !== null) {
url.searchParams.append(key, value);
}
});
}
const response = await fetch(url.toString(), {
method,
headers: {
'Authorization': `ApiKey ${this.apiKey}`,
'x-user-id': this.userId,
'Content-Type': 'application/json',
...headers
},
...(body && { body: JSON.stringify(body) })
});
const text = await response.text();
if (!response.ok) {
throw new Error(`API error ${response.status}: ${text}`);
}
// Return null for 204 No Content responses
if (response.status === 204) {
return null;
}
return text ? JSON.parse(text) : null;
}
// Context API
context = {
getContext: (contextType, query) => this.request('GET', `/context/${contextType}`, { query }),
getRegistry: () => this.request('GET', '/context/registry')
};
// Habits API
habits = {
getAll: (query) => this.request('GET', '/habits', { query }),
getById: (id) => this.request('GET', `/habits/${id}`),
create: (data) => this.request('POST', '/habits', { body: data }),
update: (id, data) => this.request('PATCH', `/habits/${id}`, { body: data }),
delete: (id) => this.request('DELETE', `/habits/${id}`),
// Helper methods for common operations
addReflection: (id, note) => this.request('POST', `/habits/${id}/reflections`, {
body: { note }
}),
toggleActive: (id, isActive) => this.request('PATCH', `/habits/${id}`, {
body: { isActive }
}),
logs: {
getAll: (query) => this.request('GET', '/habit-logs', { query }),
create: (data) => this.request('POST', '/habit-logs', { body: data }),
update: (id, data) => this.request('PUT', `/habit-logs/${id}`, { body: data })
},
evaluations: {
getAll: (query) => this.request('GET', '/habit-evaluations', { query }),
create: (data) => this.request('POST', '/habit-evaluations', { body: data }),
update: (id, data) => this.request('PUT', `/habit-evaluations/${id}`, { body: data })
}
};
// Goals API
goals = {
getAll: (query) => this.request('GET', '/goals', { query }),
getById: (id) => this.request('GET', `/goals/${id}`),
create: (data) => this.request('POST', '/goals', { body: data }),
update: (id, data) => this.request('PUT', `/goals/${id}`, { body: data }),
patch: (id, data) => this.request('PATCH', `/goals/${id}`, { body: data }),
delete: (id) => this.request('DELETE', `/goals/${id}`),
// Helper methods for common operations
addTask: (id, task) => this.request('POST', `/goals/${id}/tasks`, { body: task }),
addReflection: (id, note) => this.request('POST', `/goals/${id}/reflections`, {
body: { note }
}),
updateTaskStatus: (id, taskId, done) => this.request('PATCH', `/goals/${id}/tasks/${taskId}/status`, {
body: { done }
}),
updateSmartScore: (id, score) => this.request('PATCH', `/goals/${id}`, {
body: { updateSmartScore: { score } }
}),
updateStatus: (id, status) => this.request('PATCH', `/goals/${id}`, {
body: { status }
}),
updateTags: (id, tags) => this.request('PATCH', `/goals/${id}`, {
body: { tags }
})
};
// Ideas API
ideas = {
getAll: (query) => this.request('GET', '/ideas', { query }),
getById: (id) => this.request('GET', `/ideas/${id}`),
create: (data) => this.request('POST', '/ideas', { body: data }),
update: (id, data) => this.request('PUT', `/ideas/${id}`, { body: data }),
patch: (id, data) => this.request('PATCH', `/ideas/${id}`, { body: data }),
delete: (id) => this.request('DELETE', `/ideas/${id}`),
// Helper methods for common operations
addReflection: (id, note) => this.request('POST', `/ideas/${id}/reflections`, {
body: { note }
}),
addLetMe: (id, action) => this.request('POST', `/ideas/${id}/letMes`, {
body: { action }
}),
updateLetMeStatus: (id, letMeId, done) => this.request('PATCH', `/ideas/${id}/letMes/${letMeId}`, {
body: { done }
})
};
// Pulse API
pulse = {
getAll: (query) => this.request('GET', '/pulse', { query }),
getById: (id) => this.request('GET', `/pulse/${id}`),
create: (data) => this.request('POST', '/pulse', { body: data }),
update: (id, data) => this.request('PUT', `/pulse/${id}`, { body: data }),
delete: (id) => this.request('DELETE', `/pulse/${id}`)
};
// Segments API
segments = {
getAll: (query) => this.request('GET', '/segments', { query }),
getById: (id) => this.request('GET', `/segments/${id}`),
create: (data) => this.request('POST', '/segments', { body: data }),
update: (id, data) => this.request('PATCH', `/segments/${id}`, { body: data }),
delete: (id) => this.request('DELETE', `/segments/${id}`)
};
// Thoughts API
thoughts = {
getAll: (query) => this.request('GET', '/thoughts', { query }),
getById: (id) => this.request('GET', `/thoughts/${id}`),
create: (data) => this.request('POST', '/thoughts', { body: data }),
update: (id, data) => this.request('PATCH', `/thoughts/${id}`, { body: data }),
delete: (id) => this.request('DELETE', `/thoughts/${id}`)
};
// Chat History API
chatHistory = {
getAll: (query) => this.request('GET', '/chat-history', { query }),
getById: (id) => this.request('GET', `/chat-history/${id}`),
create: (data) => this.request('POST', '/chat-history', { body: data }),
update: (id, data) => this.request('PUT', `/chat-history/${id}`, { body: data }),
delete: (id) => this.request('DELETE', `/chat-history/${id}`)
};
// Messages API
messages = {
handleIncomingMessage: (data) => this.request('POST', '/messages/incoming', { body: data }),
sendMessage: (data) => this.request('POST', '/messages/send', { body: data })
};
// Users API
users = {
getAll: (query) => this.request('GET', '/users', { query }),
getById: (id) => this.request('GET', `/users/${id}`),
create: (data) => this.request('POST', '/users', { body: data }),
update: (id, data) => this.request('PUT', `/users/${id}`, { body: data }),
patch: (id, data) => this.request('PATCH', `/users/${id}`, { body: data }),
delete: (id) => this.request('DELETE', `/users/${id}`)
};
// Auth API
auth = {
login: (data) => this.request('POST', '/auth/login', { body: data }),
refreshToken: (data) => this.request('POST', '/auth/refresh', { body: data })
};
}
export default MightyClient;