@ofauth/onlyfans-sdk 2.2.3 → 3.0.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/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
- let errorBody: any;
104
- try { errorBody = await response.json(); } catch {}
105
- throw new OFAuthAPIError(response, errorBody);
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.text() as unknown as T;
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
- let errorBody: any;
172
- try { errorBody = await response.json(); } catch {}
173
- throw new OFAuthAPIError(response, errorBody);
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.text() as unknown as T;
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
+ });