chore: добавить frontend правила и шаблоны SLM
- добавлены frontend инструкции AGENTS и локальный style guide - актуализированы SLM templates под Vite React и слой infra - добавлены шаблоны component, infra и factory-based business - нормализованы примеры документации под alias infra
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
/**
|
||||
* Ошибка API {{name.pascalCase}}.
|
||||
*/
|
||||
export class {{name.pascalCase}}Error extends Error {
|
||||
constructor(
|
||||
public readonly status: number,
|
||||
message: string,
|
||||
) {
|
||||
super(message)
|
||||
this.name = '{{name.pascalCase}}Error'
|
||||
}
|
||||
}
|
||||
3
.templates/infra/{{name.kebabCase}}/index.ts
Normal file
3
.templates/infra/{{name.kebabCase}}/index.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export { {{name.pascalCase}}Client, create{{name.pascalCase}}Client } from './{{name.kebabCase}}.client'
|
||||
export { {{name.pascalCase}}Error } from './errors/{{name.kebabCase}}.error'
|
||||
export type { QueryParams } from './types/{{name.kebabCase}}-client.type'
|
||||
@@ -0,0 +1,4 @@
|
||||
/**
|
||||
* Query-параметры API {{name.pascalCase}}.
|
||||
*/
|
||||
export type QueryParams = Record<string, boolean | number | string | null | undefined>
|
||||
@@ -0,0 +1,44 @@
|
||||
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)
|
||||
Reference in New Issue
Block a user