forked from cerc-io/cosmos-explorer
add factory for request
This commit is contained in:
parent
671050f5f2
commit
7f477bcd9f
@ -1,20 +1,30 @@
|
||||
import fetch from 'cross-fetch'
|
||||
|
||||
export interface ApiResponse<T> {
|
||||
data: T;
|
||||
export async function fetchData<T>(url: string, adapter: (source: any) => T): Promise<T> {
|
||||
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<User[]>("https://somewhere/")
|
||||
export async function fetchData<T>(url: string): Promise<ApiResponse<T>> {
|
||||
const response = await fetch(url);
|
||||
const data = await response.json();
|
||||
|
||||
const result : ApiResponse<T> = {
|
||||
data
|
||||
}
|
||||
return result
|
||||
/*
|
||||
const userAdapter = (source: any): User => {
|
||||
return {
|
||||
id: source.id,
|
||||
name: source.name,
|
||||
email: source.email,
|
||||
};
|
||||
};
|
||||
try {
|
||||
const userData = await fetchData<User>("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()
|
||||
|
@ -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<T> {
|
||||
url: string,
|
||||
adapter: (source: any) => T
|
||||
}
|
||||
|
||||
interface User {
|
||||
|
||||
}
|
||||
|
||||
interface Post {
|
||||
|
||||
}
|
||||
|
||||
export interface RequestRegistry {
|
||||
users: Request<User>;
|
||||
posts: Request<Post>;
|
||||
}
|
||||
|
||||
export function convert<T>(source: any): T {
|
||||
return source
|
||||
}
|
||||
|
||||
export interface Registry {
|
||||
[key: string]: RequestRegistry;
|
||||
}
|
||||
|
||||
export function withCustomAdapter<T extends RequestRegistry>(target: T, source: Partial<T>): 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];
|
||||
}
|
||||
|
||||
|
||||
if (!closestVersion) {
|
||||
throw new Error(`Unsupported version: ${version}`);
|
||||
}
|
||||
|
||||
console.log(`Closest version to ${version}: ${closestVersion}`);
|
||||
|
||||
return registry[closestVersion];
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user