@ofauth/onlyfans-sdk 2.2.3 → 2.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +6151 -6009
- package/dist/index.d.ts +6151 -6009
- package/dist/index.js +1064 -948
- package/dist/index.mjs +1064 -948
- package/package.json +1 -1
- package/src/client.ts +6928 -6598
- package/src/runtime.ts +48 -19
- package/src/runtime.unit.test.ts +39 -0
package/src/runtime.ts
CHANGED
|
@@ -31,6 +31,7 @@ export interface PaginatedResponse<T> {
|
|
|
31
31
|
export interface MarkerPaginatedResponse<T> {
|
|
32
32
|
list: T[];
|
|
33
33
|
hasMore: boolean;
|
|
34
|
+
nextMarker?: string;
|
|
34
35
|
marker?: string;
|
|
35
36
|
}
|
|
36
37
|
|
|
@@ -55,6 +56,12 @@ export class OFAuthAPIError extends Error {
|
|
|
55
56
|
}
|
|
56
57
|
}
|
|
57
58
|
|
|
59
|
+
function isJsonContentType(contentType: string | null): boolean {
|
|
60
|
+
if (!contentType) return false;
|
|
61
|
+
const lower = contentType.toLowerCase();
|
|
62
|
+
return lower.includes("application/json") || lower.includes("+json");
|
|
63
|
+
}
|
|
64
|
+
|
|
58
65
|
function buildQueryString(params: Record<string, any>): string {
|
|
59
66
|
const entries: string[] = [];
|
|
60
67
|
for (const [key, value] of Object.entries(params)) {
|
|
@@ -70,6 +77,30 @@ function buildQueryString(params: Record<string, any>): string {
|
|
|
70
77
|
return entries.length > 0 ? `?${entries.join('&')}` : '';
|
|
71
78
|
}
|
|
72
79
|
|
|
80
|
+
async function readErrorBody(response: Response): Promise<{ message?: string; code?: string; details?: unknown } | string> {
|
|
81
|
+
const contentType = response.headers.get("content-type");
|
|
82
|
+
if (isJsonContentType(contentType)) {
|
|
83
|
+
// If the server claims JSON, propagate JSON parsing failures (contract tests should catch bad JSON).
|
|
84
|
+
const json = (await response.json()) as unknown;
|
|
85
|
+
const obj = typeof json === "object" && json !== null ? (json as Record<string, unknown>) : null;
|
|
86
|
+
return {
|
|
87
|
+
message: typeof obj?.message === "string" ? obj.message : undefined,
|
|
88
|
+
code: typeof obj?.code === "string" ? obj.code : undefined,
|
|
89
|
+
details: obj?.details,
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
return await response.text();
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
async function readSuccessBody<T>(response: Response): Promise<T> {
|
|
96
|
+
const contentType = response.headers.get("content-type");
|
|
97
|
+
if (isJsonContentType(contentType)) {
|
|
98
|
+
return (await response.json()) as T;
|
|
99
|
+
}
|
|
100
|
+
// Non-JSON responses are returned as text (e.g. some download endpoints).
|
|
101
|
+
return (await response.text()) as unknown as T;
|
|
102
|
+
}
|
|
103
|
+
|
|
73
104
|
export async function request<T>(config: OFAuthConfig, reqConfig: RequestConfig): Promise<T> {
|
|
74
105
|
const fetchFn = config.fetchApi || fetch;
|
|
75
106
|
const basePath = config.basePath || BASE_PATH;
|
|
@@ -100,18 +131,17 @@ export async function request<T>(config: OFAuthConfig, reqConfig: RequestConfig)
|
|
|
100
131
|
});
|
|
101
132
|
|
|
102
133
|
if (!response.ok) {
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
throw new OFAuthAPIError(response,
|
|
134
|
+
const body = await readErrorBody(response);
|
|
135
|
+
const obj = typeof body === "object" && body !== null ? (body as Record<string, unknown>) : undefined;
|
|
136
|
+
throw new OFAuthAPIError(response, {
|
|
137
|
+
message: (typeof obj?.message === "string" ? obj.message : undefined) ?? (typeof body === "string" ? body : undefined),
|
|
138
|
+
code: typeof obj?.code === "string" ? obj.code : undefined,
|
|
139
|
+
details: obj?.details,
|
|
140
|
+
});
|
|
106
141
|
}
|
|
107
142
|
|
|
108
|
-
const contentType = response.headers.get('content-type');
|
|
109
|
-
if (contentType?.includes('application/json')) {
|
|
110
|
-
return response.json();
|
|
111
|
-
}
|
|
112
|
-
|
|
113
143
|
if (response.status === 204) return {} as T;
|
|
114
|
-
return response
|
|
144
|
+
return await readSuccessBody<T>(response);
|
|
115
145
|
}
|
|
116
146
|
|
|
117
147
|
export type ItemType<T> = T extends { list: Array<infer U> } ? U : never;
|
|
@@ -168,16 +198,15 @@ export async function proxy<T = any>(config: OFAuthConfig, opts: ProxyRequestOpt
|
|
|
168
198
|
});
|
|
169
199
|
|
|
170
200
|
if (!response.ok) {
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
throw new OFAuthAPIError(response,
|
|
201
|
+
const body = await readErrorBody(response);
|
|
202
|
+
const obj = typeof body === "object" && body !== null ? (body as Record<string, unknown>) : undefined;
|
|
203
|
+
throw new OFAuthAPIError(response, {
|
|
204
|
+
message: (typeof obj?.message === "string" ? obj.message : undefined) ?? (typeof body === "string" ? body : undefined),
|
|
205
|
+
code: typeof obj?.code === "string" ? obj.code : undefined,
|
|
206
|
+
details: obj?.details,
|
|
207
|
+
});
|
|
174
208
|
}
|
|
175
|
-
|
|
176
|
-
const contentType = response.headers.get('content-type');
|
|
177
|
-
if (contentType?.includes('application/json')) {
|
|
178
|
-
return response.json();
|
|
179
|
-
}
|
|
180
|
-
|
|
209
|
+
|
|
181
210
|
if (response.status === 204) return {} as T;
|
|
182
|
-
return response
|
|
211
|
+
return await readSuccessBody<T>(response);
|
|
183
212
|
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { request, type OFAuthConfig } from "./runtime";
|
|
3
|
+
|
|
4
|
+
describe("runtime.request JSON parsing", () => {
|
|
5
|
+
it("returns text for non-JSON content-type", async () => {
|
|
6
|
+
const cfg: OFAuthConfig = {
|
|
7
|
+
apiKey: "k",
|
|
8
|
+
basePath: "http://example.test",
|
|
9
|
+
fetchApi: (async () =>
|
|
10
|
+
new Response(`{"hasMore":false,"items":[{"url":"""""}]}`, {
|
|
11
|
+
status: 200,
|
|
12
|
+
headers: { "content-type": "text/plain" },
|
|
13
|
+
})) as unknown as typeof fetch,
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
const res = await request<unknown>(cfg, {
|
|
17
|
+
method: "GET",
|
|
18
|
+
path: "/v2/access/analytics/posts/top",
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
expect(typeof res).toBe("string");
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it("throws on invalid JSON when content-type is JSON", async () => {
|
|
25
|
+
const cfg: OFAuthConfig = {
|
|
26
|
+
apiKey: "k",
|
|
27
|
+
basePath: "http://example.test",
|
|
28
|
+
fetchApi: (async () =>
|
|
29
|
+
new Response(`{"items":[{"url": """""}]}`, {
|
|
30
|
+
status: 200,
|
|
31
|
+
headers: { "content-type": "application/json; charset=utf-8" },
|
|
32
|
+
})) as unknown as typeof fetch,
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
await expect(
|
|
36
|
+
request<unknown>(cfg, { method: "GET", path: "/v2/access/posts" }),
|
|
37
|
+
).rejects.toThrow();
|
|
38
|
+
});
|
|
39
|
+
});
|