forked from CalloraOrg/Callora-Backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapiRepository.ts
More file actions
84 lines (69 loc) · 2.1 KB
/
Copy pathapiRepository.ts
File metadata and controls
84 lines (69 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import { eq } from 'drizzle-orm';
import { db, schema } from '../db/index.js';
import type { Api, ApiStatus } from '../db/schema.js';
export interface ApiListFilters {
status?: ApiStatus;
limit?: number;
offset?: number;
}
export interface ApiRepository {
listByDeveloper(developerId: number, filters?: ApiListFilters): Promise<Api[]>;
}
export const defaultApiRepository: ApiRepository = {
async listByDeveloper(developerId, filters = {}) {
let query = db.select().from(schema.apis).where(eq(schema.apis.developer_id, developerId));
if (filters.status) {
query = query.where(eq(schema.apis.status, filters.status));
}
if (typeof filters.limit === 'number') {
query = query.limit(filters.limit);
}
if (typeof filters.offset === 'number') {
query = query.offset(filters.offset);
}
return query;
},
};
export interface ApiDeveloperInfo {
name: string | null;
website: string | null;
description: string | null;
}
export interface ApiDetails {
id: number;
name: string;
description: string | null;
base_url: string;
logo_url: string | null;
category: string | null;
status: string;
developer: ApiDeveloperInfo;
}
export interface ApiEndpointInfo {
path: string;
method: string;
price_per_call_usdc: string;
description: string | null;
}
export interface ApiRepository {
findById(id: number): Promise<ApiDetails | null>;
getEndpoints(apiId: number): Promise<ApiEndpointInfo[]>;
}
// --- In-Memory implementation (for testing) ---
export class InMemoryApiRepository implements ApiRepository {
private readonly apis: ApiDetails[];
private readonly endpointsByApiId: Map<number, ApiEndpointInfo[]>;
constructor(
apis: ApiDetails[] = [],
endpointsByApiId: Map<number, ApiEndpointInfo[]> = new Map()
) {
this.apis = [...apis];
this.endpointsByApiId = new Map(endpointsByApiId);
}
async findById(id: number): Promise<ApiDetails | null> {
return this.apis.find((a) => a.id === id) ?? null;
}
async getEndpoints(apiId: number): Promise<ApiEndpointInfo[]> {
return this.endpointsByApiId.get(apiId) ?? [];
}
}