docs: add porting guide and shared Dataverse helpers
Common rules for moving the remaining stroy-bot-* sections into the monorepo, so every section follows the same structure and keeps 1:1 behaviour with its old web part. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
import { ensureOk, IHttpClient, IHttpRequest } from '../../services/http/IHttpClient';
|
||||
import { DATAVERSE_API_PATH } from '../../config/dataverse';
|
||||
|
||||
// Общие помощники для Dataverse Web API. Заголовки — те же, что в старых web parts
|
||||
// (stroy-bot-works / stroy-bot-report): OData 4.0, JSON.
|
||||
|
||||
export const DATAVERSE_HEADERS: Record<string, string> = {
|
||||
Accept: 'application/json',
|
||||
'Content-Type': 'application/json; charset=utf-8',
|
||||
'OData-Version': '4.0',
|
||||
'OData-MaxVersion': '4.0'
|
||||
};
|
||||
|
||||
/** `{dataverseUrl}/api/data/v9.2/{path}` */
|
||||
export function dataverseUrl(baseUrl: string, path: string): string {
|
||||
return `${baseUrl}${DATAVERSE_API_PATH}${path}`;
|
||||
}
|
||||
|
||||
/** Ответ Dataverse на запрос коллекции. */
|
||||
export interface IDataverseCollection<T> {
|
||||
value: T[];
|
||||
'@odata.nextLink'?: string;
|
||||
}
|
||||
|
||||
async function send(client: IHttpClient, url: string, request: IHttpRequest): Promise<unknown> {
|
||||
const response = await ensureOk(
|
||||
await client.fetch(url, { ...request, headers: { ...DATAVERSE_HEADERS, ...request.headers } })
|
||||
);
|
||||
// 204 No Content (PATCH/DELETE без Prefer: return=representation) — тела нет.
|
||||
if (response.status === 204) {
|
||||
return undefined;
|
||||
}
|
||||
const text = await response.text();
|
||||
return text ? JSON.parse(text) : undefined;
|
||||
}
|
||||
|
||||
export async function dataverseGet<T>(client: IHttpClient, url: string): Promise<T> {
|
||||
return (await send(client, url, { method: 'GET' })) as T;
|
||||
}
|
||||
|
||||
/** POST. С `Prefer: return=representation` Dataverse возвращает созданную запись. */
|
||||
export async function dataversePost<T>(
|
||||
client: IHttpClient,
|
||||
url: string,
|
||||
body: unknown,
|
||||
headers: Record<string, string> = {}
|
||||
): Promise<T | undefined> {
|
||||
return (await send(client, url, { method: 'POST', body: JSON.stringify(body), headers })) as T | undefined;
|
||||
}
|
||||
|
||||
export async function dataversePatch(
|
||||
client: IHttpClient,
|
||||
url: string,
|
||||
body: unknown,
|
||||
headers: Record<string, string> = {}
|
||||
): Promise<void> {
|
||||
await send(client, url, { method: 'PATCH', body: JSON.stringify(body), headers });
|
||||
}
|
||||
|
||||
export async function dataverseDelete(client: IHttpClient, url: string): Promise<void> {
|
||||
await send(client, url, { method: 'DELETE' });
|
||||
}
|
||||
Reference in New Issue
Block a user