Files
image-platform/.templates/infra/{{name.kebabCase}}/{{name.kebabCase}}.client.ts
S.Gromov 2c88cc3eca chore: добавить frontend правила и шаблоны SLM
- добавлены frontend инструкции AGENTS и локальный style guide
- актуализированы SLM templates под Vite React и слой infra
- добавлены шаблоны component, infra и factory-based business
- нормализованы примеры документации под alias infra
2026-05-05 14:05:43 +03:00

45 lines
1.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { {{name.pascalCase}}Error } from './errors/{{name.kebabCase}}.error'
import type { QueryParams } from './types/{{name.kebabCase}}-client.type'
/**
* REST-клиент {{name.pascalCase}}.
*/
export class {{name.pascalCase}}Client {
constructor(
private readonly baseUrl: string,
private readonly defaultHeaders: Record<string, string> = {},
) {}
/**
* Выполняет GET-запрос к API {{name.pascalCase}}.
*/
async get<T>(path: string, params: QueryParams = {}): Promise<T> {
const base = `${this.baseUrl.replace(/\/+$/, '')}/`
const url = new URL(path.replace(/^\/+/, ''), base)
Object.entries(params).forEach(([key, value]) => {
if (value !== null && value !== undefined) {
url.searchParams.set(key, String(value))
}
})
const response = await fetch(url, {
headers: {
Accept: 'application/json',
...this.defaultHeaders,
},
})
if (!response.ok) {
throw new {{name.pascalCase}}Error(response.status, response.statusText)
}
return response.json() as Promise<T>
}
}
/**
* Создаёт REST-клиент {{name.pascalCase}} с заданным base URL.
*/
export const create{{name.pascalCase}}Client = (baseUrl: string) => new {{name.pascalCase}}Client(baseUrl)