diff --git a/src/libs/http.ts b/src/libs/http.ts index 8da24fc6..b6c7bdc1 100644 --- a/src/libs/http.ts +++ b/src/libs/http.ts @@ -1,20 +1,30 @@ import fetch from 'cross-fetch' -export interface ApiResponse { - data: T; +export async function fetchData(url: string, adapter: (source: any) => T): Promise { + const response = await fetch(url); + if (!response.ok) { + throw new Error(`HTTP error: ${response.status}`); + } + const data = await response.json(); + return adapter(data); } // Usage: -// const usersResponse = await fetchData("https://somewhere/") -export async function fetchData(url: string): Promise> { - const response = await fetch(url); - const data = await response.json(); - - const result : ApiResponse = { - data - } - return result +/* +const userAdapter = (source: any): User => { + return { + id: source.id, + name: source.name, + email: source.email, + }; +}; +try { + const userData = await fetchData("https://jsonplaceholder.typicode.com/users/1", userAdapter); + console.log(userData); // Output: { id: 1, name: "Leanne Graham", email: "Sincere@april.biz" } +} catch (error) { + console.error(error.message); } +// */ export async function get(url: string) { return (await fetch(url)).json() diff --git a/src/libs/registry.ts b/src/libs/registry.ts index 6b86729d..ef676d93 100644 --- a/src/libs/registry.ts +++ b/src/libs/registry.ts @@ -1,46 +1,61 @@ import semver from "semver"; -interface Registry { - [key: string]: string; - } - - const registry: Registry = { - v1: "https://example.com/api/v1/endpoint", - v2: "https://example.com/api/v2/endpoint", - v3: "https://example.com/api/v3/endpoint", - alpha: "https://example.com/api/alpha/endpoint", - beta: "https://example.com/api/beta/endpoint", - production: "https://example.com/api/production/endpoint", - }; - -export function findConfigByName(name: string): string { - const base = "https://example.com/api/"; - - const url = registry[name]; - if (!url) { - throw new Error(`Unsupported version or name: ${name}`); - } - - return url; + +export interface Request { + url: string, + adapter: (source: any) => T +} + +interface User { + +} + +interface Post { + +} + +export interface RequestRegistry { + users: Request; + posts: Request; +} + +export function convert(source: any): T { + return source +} + +export interface Registry { + [key: string]: RequestRegistry; +} + +export function withCustomAdapter(target: T, source: Partial): T { + return Object.assign({}, target, source); +} + +export function findConfigByName(name: string, registry: Registry): RequestRegistry { + const url = registry[name]; + if (!url) { + throw new Error(`Unsupported version or name: ${name}`); } -export function findConfigByVersion(version: string, registry: Registry): string { - let closestVersion: string | null = null; - - for (const key in registry) { - if (semver.satisfies(key, version)) { - if (!closestVersion || semver.gt(key, closestVersion)) { - closestVersion = key; - } + return url; +} + +export function findConfigByVersion(version: string, registry: Registry): RequestRegistry { + let closestVersion: string | null = null; + + for (const key in registry) { + if (semver.satisfies(key, version)) { + if (!closestVersion || semver.gt(key, closestVersion)) { + closestVersion = key; } } - - if (!closestVersion) { - throw new Error(`Unsupported version: ${version}`); - } - - console.log(`Closest version to ${version}: ${closestVersion}`); - - return registry[closestVersion]; } - \ No newline at end of file + + if (!closestVersion) { + throw new Error(`Unsupported version: ${version}`); + } + + console.log(`Closest version to ${version}: ${closestVersion}`); + + return registry[closestVersion]; +}