add factory for request
This commit is contained in:
parent
671050f5f2
commit
7f477bcd9f
@ -1,20 +1,30 @@
|
|||||||
import fetch from 'cross-fetch'
|
import fetch from 'cross-fetch'
|
||||||
|
|
||||||
export interface ApiResponse<T> {
|
export async function fetchData<T>(url: string, adapter: (source: any) => T): Promise<T> {
|
||||||
data: 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:
|
// Usage:
|
||||||
// const usersResponse = await fetchData<User[]>("https://somewhere/")
|
/*
|
||||||
export async function fetchData<T>(url: string): Promise<ApiResponse<T>> {
|
const userAdapter = (source: any): User => {
|
||||||
const response = await fetch(url);
|
return {
|
||||||
const data = await response.json();
|
id: source.id,
|
||||||
|
name: source.name,
|
||||||
const result : ApiResponse<T> = {
|
email: source.email,
|
||||||
data
|
};
|
||||||
}
|
};
|
||||||
return result
|
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) {
|
export async function get(url: string) {
|
||||||
return (await fetch(url)).json()
|
return (await fetch(url)).json()
|
||||||
|
@ -1,30 +1,46 @@
|
|||||||
import semver from "semver";
|
import semver from "semver";
|
||||||
|
|
||||||
interface Registry {
|
|
||||||
[key: string]: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
const registry: Registry = {
|
export interface Request<T> {
|
||||||
v1: "https://example.com/api/v1/endpoint",
|
url: string,
|
||||||
v2: "https://example.com/api/v2/endpoint",
|
adapter: (source: any) => T
|
||||||
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 {
|
interface User {
|
||||||
const base = "https://example.com/api/";
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
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];
|
const url = registry[name];
|
||||||
if (!url) {
|
if (!url) {
|
||||||
throw new Error(`Unsupported version or name: ${name}`);
|
throw new Error(`Unsupported version or name: ${name}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
return url;
|
return url;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function findConfigByVersion(version: string, registry: Registry): string {
|
export function findConfigByVersion(version: string, registry: Registry): RequestRegistry {
|
||||||
let closestVersion: string | null = null;
|
let closestVersion: string | null = null;
|
||||||
|
|
||||||
for (const key in registry) {
|
for (const key in registry) {
|
||||||
@ -42,5 +58,4 @@ export function findConfigByVersion(version: string, registry: Registry): string
|
|||||||
console.log(`Closest version to ${version}: ${closestVersion}`);
|
console.log(`Closest version to ${version}: ${closestVersion}`);
|
||||||
|
|
||||||
return registry[closestVersion];
|
return registry[closestVersion];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user