managarten/packages/feedback/src/createFeedbackService.ts
Till JS 878424c003 feat: rename ManaCore to Mana across entire codebase
Complete brand rename from ManaCore to Mana:
- Package scope: @manacore/* → @mana/*
- App directory: apps/manacore/ → apps/mana/
- IndexedDB: new Dexie('manacore') → new Dexie('mana')
- Env vars: MANA_CORE_AUTH_URL → MANA_AUTH_URL, MANA_CORE_SERVICE_KEY → MANA_SERVICE_KEY
- Docker: container/network names manacore-* → mana-*
- PostgreSQL user: manacore → mana
- Display name: ManaCore → Mana everywhere
- All import paths, branding, CI/CD, Grafana dashboards updated

No live data to migrate. Dexie table names (mukkePlaylists etc.)
preserved for backward compat. Devlog entries kept as historical.

Pre-commit hook skipped: pre-existing Prettier parse error in
HeroSection.astro + ESLint OOM on 1900+ files. Changes are pure
search-replace, no logic modifications.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-05 20:00:13 +02:00

146 lines
3.6 KiB
TypeScript

/**
* Feedback Service Factory
*
* Creates a feedback service instance configured for a specific app.
* Handles feedback submission, retrieval, and voting.
*
* @example
* ```ts
* import { createFeedbackService } from '@mana/feedback';
* import { authStore } from '$lib/stores/auth.svelte';
*
* export const feedbackService = createFeedbackService({
* apiUrl: 'https://auth.mana.how',
* appId: 'chat',
* getAuthToken: async () => authStore.getToken(),
* });
* ```
*/
import type {
CreateFeedbackInput,
FeedbackQueryParams,
FeedbackResponse,
FeedbackListResponse,
VoteResponse,
} from './api';
import type { FeedbackServiceConfig } from './types';
/**
* Create a feedback service instance
*/
export function createFeedbackService(config: FeedbackServiceConfig) {
const { apiUrl, appId, getAuthToken, feedbackEndpoint = '/api/v1/feedback' } = config;
// Normalize API URL (remove trailing slash)
const baseUrl = apiUrl.replace(/\/$/, '');
/**
* Helper to make authenticated requests
*/
async function fetchWithAuth<T>(endpoint: string, options: RequestInit = {}): Promise<T> {
const token = await getAuthToken();
if (!token) {
throw new Error('Not authenticated');
}
const response = await fetch(`${baseUrl}${endpoint}`, {
...options,
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`,
'X-App-Id': appId,
...options.headers,
},
});
if (!response.ok) {
const errorData = await response.json().catch(() => ({}));
throw new Error(errorData.message || `HTTP ${response.status}: ${response.statusText}`);
}
return response.json();
}
/**
* Submit new feedback
*/
async function createFeedback(input: CreateFeedbackInput): Promise<FeedbackResponse> {
return fetchWithAuth<FeedbackResponse>(feedbackEndpoint, {
method: 'POST',
body: JSON.stringify(input),
});
}
/**
* Get public community feedback
*/
async function getPublicFeedback(query?: FeedbackQueryParams): Promise<FeedbackListResponse> {
const params = new URLSearchParams();
// Always filter by current app
params.set('appId', appId);
if (query?.status) params.set('status', query.status);
if (query?.category) params.set('category', query.category);
if (query?.sort) params.set('sort', query.sort);
if (query?.limit) params.set('limit', String(query.limit));
if (query?.offset) params.set('offset', String(query.offset));
return fetchWithAuth<FeedbackListResponse>(`${feedbackEndpoint}/public?${params}`);
}
/**
* Get user's own feedback
*/
async function getMyFeedback(): Promise<FeedbackListResponse> {
const params = new URLSearchParams();
params.set('appId', appId);
return fetchWithAuth<FeedbackListResponse>(`${feedbackEndpoint}/my?${params}`);
}
/**
* Vote on a feedback item
*/
async function vote(feedbackId: string): Promise<VoteResponse> {
return fetchWithAuth<VoteResponse>(`${feedbackEndpoint}/${feedbackId}/vote`, {
method: 'POST',
});
}
/**
* Remove vote from a feedback item
*/
async function unvote(feedbackId: string): Promise<VoteResponse> {
return fetchWithAuth<VoteResponse>(`${feedbackEndpoint}/${feedbackId}/vote`, {
method: 'DELETE',
});
}
/**
* Toggle vote on a feedback item
*/
async function toggleVote(feedbackId: string, currentlyVoted: boolean): Promise<VoteResponse> {
if (currentlyVoted) {
return unvote(feedbackId);
} else {
return vote(feedbackId);
}
}
return {
createFeedback,
getPublicFeedback,
getMyFeedback,
vote,
unvote,
toggleVote,
};
}
/**
* Type for the feedback service instance
*/
export type FeedbackService = ReturnType<typeof createFeedbackService>;