-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi-client.ts
More file actions
183 lines (159 loc) · 5.08 KB
/
Copy pathapi-client.ts
File metadata and controls
183 lines (159 loc) · 5.08 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import { DEFAULT_API_URL } from "../types.js";
import type {
InitResponse,
StripeConnectResponse,
ProgramStatus,
TestEventResult,
} from "../types.js";
import * as logger from "./logger.js";
import { resolveApiKey } from "./config.js";
export interface AuthStartResponse {
state: string;
auth_url: string;
poll_url: string;
expires_at: string;
}
export interface AuthPollResponse {
status: "pending" | "complete" | "expired" | "consumed";
token?: string;
email?: string;
advertiser_id?: number;
}
interface RequestOptions {
method?: string;
body?: Record<string, unknown>;
apiKey?: string;
apiUrl?: string;
}
export class AffitorAPI {
private apiUrl: string;
private apiKey?: string;
constructor(opts: { apiUrl?: string; apiKey?: string } = {}) {
this.apiUrl = opts.apiUrl ?? DEFAULT_API_URL;
this.apiKey = opts.apiKey;
}
/**
* Create an API client with auto-resolved credentials.
* Priority: --api-key flag > AFFITOR_API_KEY env > .affitor/.env > legacy config
*/
static fromFlags(flags: { apiKey?: string; apiUrl?: string }, cwd?: string): AffitorAPI {
const apiKey = resolveApiKey(flags, cwd);
return new AffitorAPI({
apiUrl: flags.apiUrl ?? DEFAULT_API_URL,
apiKey: apiKey ?? undefined,
});
}
async initProgram(data: {
name: string;
domain: string;
commission_type: string;
commission_rate: number;
cookie_duration: number;
duration_months?: number;
}): Promise<InitResponse> {
return this.request<InitResponse>("/api/v1/cli/init", {
method: "POST",
body: data,
});
}
async saveStripeConnection(data: {
program_id: string;
stripe_user_id: string;
webhook_endpoint_id: string;
webhook_secret: string;
}): Promise<StripeConnectResponse> {
return this.request<StripeConnectResponse>("/api/v1/cli/stripe-connect", {
method: "POST",
body: data,
});
}
async getStatus(programId: string): Promise<ProgramStatus> {
return this.request<ProgramStatus>(
`/api/v1/cli/status?program_id=${programId}`,
);
}
// ─── Auth endpoints ───────────────────────────────────────────
async authStart(): Promise<AuthStartResponse> {
return this.request<AuthStartResponse>("/api/v1/cli/auth/start", {
method: "POST",
});
}
async authPoll(state: string): Promise<AuthPollResponse> {
return this.request<AuthPollResponse>(
`/api/v1/cli/auth/poll?state=${encodeURIComponent(state)}`,
);
}
// ─── Program endpoints ──────────────────────────────────────────
async sendTestEvent(data: {
program_id: string;
event_type: "click" | "lead" | "sale";
}): Promise<TestEventResult> {
return this.request<TestEventResult>("/api/v1/cli/test-event", {
method: "POST",
body: data,
});
}
private async request<T>(path: string, opts: RequestOptions = {}): Promise<T> {
const url = `${opts.apiUrl ?? this.apiUrl}${path}`;
const key = opts.apiKey ?? this.apiKey;
const headers: Record<string, string> = {
"Content-Type": "application/json",
"User-Agent": "affitor-cli/0.2.0",
};
if (key) {
headers["Authorization"] = `Bearer ${key}`;
}
logger.debug(`${opts.method ?? "GET"} ${url}`);
let lastError: Error | undefined;
for (let attempt = 1; attempt <= 3; attempt++) {
try {
const res = await fetch(url, {
method: opts.method ?? "GET",
headers,
body: opts.body ? JSON.stringify(opts.body) : undefined,
});
if (res.status === 429) {
const retryAfter = res.headers.get("Retry-After");
const wait = retryAfter ? parseInt(retryAfter, 10) * 1000 : 5000;
throw new APIError(
429,
`Rate limited. Wait ${Math.ceil(wait / 1000)} seconds and try again.`,
);
}
if (!res.ok) {
const body = await res.json().catch(() => ({}));
const message =
(body as Record<string, string>).error ??
`API returned ${res.status}`;
throw new APIError(res.status, message);
}
const body = await res.json();
return (body as { data?: T }).data ?? (body as T);
} catch (err) {
if (err instanceof APIError) throw err;
lastError = err as Error;
if (attempt < 3) {
const wait = attempt * 1000;
logger.debug(`Request failed, retrying in ${wait}ms...`);
await new Promise((r) => setTimeout(r, wait));
}
}
}
throw new NetworkError(lastError?.message ?? "Request failed after 3 attempts");
}
}
export class APIError extends Error {
constructor(
public status: number,
message: string,
) {
super(message);
this.name = "APIError";
}
}
export class NetworkError extends Error {
constructor(message: string) {
super(`Network error: ${message}.\nCheck your internet connection and try again.`);
this.name = "NetworkError";
}
}