45 lines
1.8 KiB
JavaScript
45 lines
1.8 KiB
JavaScript
/**
|
|
* Engine client — единая точка вызовов к zeropost-engine
|
|
*/
|
|
const ENGINE_URL = process.env.ENGINE_URL || 'http://127.0.0.1:3040';
|
|
const ENGINE_SECRET = process.env.ENGINE_SECRET || 'zeropost_internal_2026';
|
|
|
|
async function call(path, options = {}) {
|
|
const { userId, body, method = 'GET' } = options;
|
|
const headers = {
|
|
'Content-Type': 'application/json',
|
|
'x-internal-secret': ENGINE_SECRET,
|
|
};
|
|
if (userId) headers['x-user-id'] = String(userId);
|
|
|
|
const url = `${ENGINE_URL}${path}`;
|
|
const res = await fetch(url, {
|
|
method,
|
|
headers,
|
|
body: body ? JSON.stringify(body) : undefined,
|
|
cache: 'no-store',
|
|
});
|
|
if (!res.ok) {
|
|
const err = await res.json().catch(() => ({ error: res.statusText }));
|
|
throw new Error(err.error || `Engine ${res.status}`);
|
|
}
|
|
return res.json();
|
|
}
|
|
|
|
export const engine = {
|
|
// Channels
|
|
listChannels: (userId) => call('/api/channels/', { userId }),
|
|
getChannel: (userId, id) => call(`/api/channels/${id}`, { userId }),
|
|
createChannel: (userId, data) => call('/api/channels/', { userId, method: 'POST', body: data }),
|
|
updateChannel: (userId, id, data) => call(`/api/channels/${id}`, { userId, method: 'PATCH', body: data }),
|
|
deleteChannel: (userId, id) => call(`/api/channels/${id}`, { userId, method: 'DELETE' }),
|
|
|
|
// Generation
|
|
generate: (userId, data) => call('/api/generate/', { userId, method: 'POST', body: data }),
|
|
getJob: (userId, id) => call(`/api/generate/${id}`, { userId }),
|
|
transformPost: (userId, data) => call('/api/generate/transform', { userId, method: 'POST', body: data }),
|
|
generatePostImage: (userId, data) => call('/api/generate/post-image', { userId, method: 'POST', body: data }),
|
|
topicsIdeas: (userId, data) => call('/api/generate/topics-ideas', { userId, method: 'POST', body: data }),
|
|
getImageStyles: () => call('/api/generate/image-styles'),
|
|
};
|