mirror of
https://github.com/gromlab-ru/slm-design.git
synced 2026-08-22 07:30:16 +03:00
feat: add example
This commit is contained in:
14
.templates/rest-api/{{name.kebabCase}}-rest-api/client.ts
Normal file
14
.templates/rest-api/{{name.kebabCase}}-rest-api/client.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import { HttpClient } from '@biocad/{{name.kebabCase}}-rest-api-sdk'
|
||||
|
||||
import {
|
||||
{{name.screamingSnakeCase}}_REST_API_BASE_URL,
|
||||
{{name.screamingSnakeCase}}_REST_API_TIMEOUT_MS
|
||||
} from './config/{{name.kebabCase}}-rest-api.config'
|
||||
import { {{name.camelCase}}RestApiFetch } from './lib/{{name.kebabCase}}-rest-api-fetch'
|
||||
|
||||
/** Транспортный HTTP-клиент {{name.pascalCase}} REST API. */
|
||||
export const {{name.camelCase}}HttpClient = new HttpClient({
|
||||
baseUrl: {{name.screamingSnakeCase}}_REST_API_BASE_URL,
|
||||
customFetch: {{name.camelCase}}RestApiFetch,
|
||||
timeout: {{name.screamingSnakeCase}}_REST_API_TIMEOUT_MS
|
||||
})
|
||||
@@ -0,0 +1,5 @@
|
||||
/** Базовый URL {{name.pascalCase}} REST API. */
|
||||
export const {{name.screamingSnakeCase}}_REST_API_BASE_URL = import.meta.env.VITE_{{name.screamingSnakeCase}}_REST_API_BASE_URL ?? ''
|
||||
|
||||
/** Максимальная длительность REST-запроса до автоматической отмены. */
|
||||
export const {{name.screamingSnakeCase}}_REST_API_TIMEOUT_MS = 30_000
|
||||
@@ -0,0 +1,3 @@
|
||||
export { ApiError as {{name.pascalCase}}RestApiError } from '@biocad/{{name.kebabCase}}-rest-api-sdk'
|
||||
export { {{name.pascalCase}}RestApiTransportError } from './{{name.kebabCase}}-rest-api-transport.error'
|
||||
export type { {{name.pascalCase}}RestApiTransportErrorCode } from './{{name.kebabCase}}-rest-api-transport.error'
|
||||
@@ -0,0 +1,20 @@
|
||||
/**
|
||||
* Причина сбоя непосредственно на transport-границе {{name.pascalCase}} REST API.
|
||||
*/
|
||||
export type {{name.pascalCase}}RestApiTransportErrorCode = 'NETWORK_UNAVAILABLE' | 'REQUEST_TIMEOUT'
|
||||
|
||||
/**
|
||||
* Маркирует fetch failure, не смешивая его с TypeError из прикладного кода.
|
||||
*/
|
||||
export class {{name.pascalCase}}RestApiTransportError extends Error {
|
||||
readonly code: {{name.pascalCase}}RestApiTransportErrorCode
|
||||
readonly cause: unknown
|
||||
|
||||
constructor(code: {{name.pascalCase}}RestApiTransportErrorCode, cause: unknown) {
|
||||
super(code)
|
||||
|
||||
this.name = '{{name.pascalCase}}RestApiTransportError'
|
||||
this.code = code
|
||||
this.cause = cause
|
||||
}
|
||||
}
|
||||
3
.templates/rest-api/{{name.kebabCase}}-rest-api/index.ts
Normal file
3
.templates/rest-api/{{name.kebabCase}}-rest-api/index.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export { {{name.camelCase}}HttpClient } from './client'
|
||||
export { {{name.pascalCase}}RestApiError, {{name.pascalCase}}RestApiTransportError } from './errors'
|
||||
export { {{name.camelCase}}RestApi } from './rest-api'
|
||||
@@ -0,0 +1,14 @@
|
||||
import { {{name.pascalCase}}RestApiTransportError } from '../errors'
|
||||
|
||||
/**
|
||||
* Выполняет fetch с явной классификацией transport failure.
|
||||
*/
|
||||
export const {{name.camelCase}}RestApiFetch: typeof fetch = async (...params) => {
|
||||
try {
|
||||
return await fetch(...params)
|
||||
} catch (error) {
|
||||
const code = error instanceof DOMException && error.name === 'AbortError' ? 'REQUEST_TIMEOUT' : 'NETWORK_UNAVAILABLE'
|
||||
|
||||
throw new {{name.pascalCase}}RestApiTransportError(code, error)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
import { createApiClient } from '@biocad/{{name.kebabCase}}-rest-api-sdk/create-api-client'
|
||||
import { operationsTree } from '@biocad/{{name.kebabCase}}-rest-api-sdk/operations-tree'
|
||||
|
||||
import { {{name.camelCase}}HttpClient } from './client'
|
||||
|
||||
/** Полный bound-клиент {{name.pascalCase}} REST API. */
|
||||
export const {{name.camelCase}}RestApi = createApiClient({{name.camelCase}}HttpClient, operationsTree)
|
||||
Reference in New Issue
Block a user