@blorp-labs/piefed-api-client 0.0.0-774e8e8 → 0.0.0-7b3ed49
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/create-client.d.ts +6 -21
- package/dist/create-client.d.ts.map +1 -1
- package/dist/create-client.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -2
- package/dist/index.js.map +1 -1
- package/dist/mutator/custom-fetch.d.ts +0 -3
- package/dist/mutator/custom-fetch.d.ts.map +1 -1
- package/dist/mutator/custom-fetch.js +3 -6
- package/dist/mutator/custom-fetch.js.map +1 -1
- package/package.json +5 -3
- package/src/__tests__/create-client.test.ts +269 -0
- package/src/create-client.ts +2 -3
- package/src/index.ts +1 -1
- package/src/mutator/custom-fetch.ts +2 -7
package/dist/create-client.d.ts
CHANGED
|
@@ -78,8 +78,7 @@ declare const allFns: {
|
|
|
78
78
|
getPostApiAlphaPrivateMessageReportUrl: () => string;
|
|
79
79
|
postApiAlphaPrivateMessageReport: (reportPrivateMessageRequest: import("./schemas").ReportPrivateMessageRequest, options?: RequestInit) => Promise<import("./schemas").PrivateMessageResponse>;
|
|
80
80
|
getGetApiAlphaPostListUrl: (params?: import("./schemas").GetApiAlphaPostListParams) => string;
|
|
81
|
-
getApiAlphaPostList: (
|
|
82
|
-
params?: import("./schemas").GetApiAlphaPostListParams, options?: RequestInit) => Promise<import("./schemas").ListPostsResponse>;
|
|
81
|
+
getApiAlphaPostList: (params?: import("./schemas").GetApiAlphaPostListParams, options?: RequestInit) => Promise<import("./schemas").ListPostsResponse>;
|
|
83
82
|
getGetApiAlphaPostList2Url: (params?: import("./schemas").GetApiAlphaPostList2Params) => string;
|
|
84
83
|
getApiAlphaPostList2: (params?: import("./schemas").GetApiAlphaPostList2Params, options?: RequestInit) => Promise<import("./schemas").ListPostsResponse>;
|
|
85
84
|
getGetApiAlphaPostUrl: (params: import("./schemas").GetApiAlphaPostParams) => string;
|
|
@@ -203,29 +202,15 @@ declare const allFns: {
|
|
|
203
202
|
getGetApiAlphaCommentLikeListUrl: (params: import("./schemas").GetApiAlphaCommentLikeListParams) => string;
|
|
204
203
|
getApiAlphaCommentLikeList: (params: import("./schemas").GetApiAlphaCommentLikeListParams, options?: RequestInit) => Promise<import("./schemas").ListCommentLikesResponse>;
|
|
205
204
|
getGetApiAlphaAdminRegistrationApplicationListUrl: (params?: import("./schemas").GetApiAlphaAdminRegistrationApplicationListParams) => string;
|
|
206
|
-
getApiAlphaAdminRegistrationApplicationList: (params?: import("./schemas").GetApiAlphaAdminRegistrationApplicationListParams, options?: RequestInit
|
|
207
|
-
* Create a client bound to a specific PieFed instance.
|
|
208
|
-
*
|
|
209
|
-
* @example
|
|
210
|
-
* const client = createClient('https://piefed.social');
|
|
211
|
-
* const site = await client.getApiAlphaSite();
|
|
212
|
-
*
|
|
213
|
-
* // With auth:
|
|
214
|
-
* const client = createClient('https://piefed.social', {
|
|
215
|
-
* headers: { Authorization: 'Bearer <token>' },
|
|
216
|
-
* });
|
|
217
|
-
*
|
|
218
|
-
* // Two instances at the same time:
|
|
219
|
-
* const a = createClient('https://instance-a.com');
|
|
220
|
-
* const b = createClient('https://instance-b.com');
|
|
221
|
-
*/) => Promise<import("./schemas").GetRegistrationListResponse>;
|
|
205
|
+
getApiAlphaAdminRegistrationApplicationList: (params?: import("./schemas").GetApiAlphaAdminRegistrationApplicationListParams, options?: RequestInit) => Promise<import("./schemas").GetRegistrationListResponse>;
|
|
222
206
|
getPutApiAlphaAdminRegistrationApplicationApproveUrl: () => string;
|
|
223
207
|
putApiAlphaAdminRegistrationApplicationApprove: (registrationApproveRequest: import("./schemas").RegistrationApproveRequest, options?: RequestInit) => Promise<void>;
|
|
224
208
|
};
|
|
225
209
|
type AsyncFn = (...args: any[]) => Promise<any>;
|
|
226
|
-
type
|
|
227
|
-
[K in keyof typeof allFns
|
|
228
|
-
};
|
|
210
|
+
type AsyncKeys = {
|
|
211
|
+
[K in keyof typeof allFns]: (typeof allFns)[K] extends AsyncFn ? K : never;
|
|
212
|
+
}[keyof typeof allFns];
|
|
213
|
+
type ApiFunctions = Pick<typeof allFns, AsyncKeys>;
|
|
229
214
|
interface CreateClientOptions extends Omit<RequestInit, 'body' | 'method'> {
|
|
230
215
|
/** Default headers merged into every request (e.g. Authorization). */
|
|
231
216
|
headers?: Record<string, string>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"create-client.d.ts","sourceRoot":"","sources":["../src/create-client.ts"],"names":[],"mappings":"AAYA,QAAA,MAAM,MAAM;;
|
|
1
|
+
{"version":3,"file":"create-client.d.ts","sourceRoot":"","sources":["../src/create-client.ts"],"names":[],"mappings":"AAYA,QAAA,MAAM,MAAM;;8BA2BF,0DAC0B,EAAC,qBACnC;4BAWkD,kDAA8B;sBAWhC,kDAEnC,EAAC,qBAAsB;;wBASX,qBAAsB;;4BAU2J,sDAAkC,EAAC,qBAAsB;;iCAAue,qBAAsB;mCAAqR,yDAAqC;6BAAmd,yDAAqC,EAAC,qBAAsB;oCAAwS,0DAAsC;8BAAsd,0DAAsC,EAAC,qBAAsB;iCAAkS,uDAAmC;2BAA6c,uDAAmC,EAAC,qBAAsB;;4BAAyW,sDAAkC,EAAC,qBAAsB;;oCAAsgB,qBAAsB;;+BAA+Z,8DAA0C,EAAC,qBAAsB;;sCAAsgB,oEAAgD,EAAC,qBAAsB;yCAA8c,8DAA0C;mCAAqe,8DAA0C,EAAC,qBAAsB;;uCAAie,8EAA0D,EAAC,qBAAsB;;wCAA6iB,qBAAsB;;8CAAyc,qBAAsB;;wCAA8b,sDAAkC,EAAC,qBAAsB;;+BAA0d,4DAAwC,EAAC,qBAAsB;;2BAA8d,0DAAsC,EAAC,qBAAsB;;0BAAyc,kDAA8B,EAAC,qBAAsB;;4BAAoc,sDAAkC,EAAC,qBAAsB;;8BA3El1U,0DACZ,EAAC,qBAEvB;;uCAmBsD,0DACvC,EAAC,qBAAsB;;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,YAAY,CAAC,GAAG;gBACnB,GAAG,KAAK;gBACR,GAAG,QAAQ;gBACX,OAAO,EAAE;oBACP,GAAG,cAAc;oBACjB,GAAI,QAAQ,EAAE,OAA8C;iBAC7D;aACF,CAAC;YAEF,OAAQ,EAAc,CAAC,GAAG,IAAI,CAAC,CAAC;QAClC,CAAC,CAAC;IACJ,CAAC;IAED,OAAO,QAAwB,CAAC;AAClC,CAAC"}
|
package/dist/index.d.ts
CHANGED
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAE,MAAM,wBAAwB,CAAC;AAClD,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAG/C,cAAc,iBAAiB,CAAC;AAGhC,cAAc,sBAAsB,CAAC;AACrC,cAAc,0BAA0B,CAAC;AACzC,cAAc,8BAA8B,CAAC;AAC7C,cAAc,oBAAoB,CAAC;AACnC,cAAc,oBAAoB,CAAC;AACnC,cAAc,oBAAoB,CAAC;AACnC,cAAc,0CAA0C,CAAC;AACzD,cAAc,oBAAoB,CAAC;AACnC,cAAc,sBAAsB,CAAC;AACrC,cAAc,wBAAwB,CAAC;AACvC,cAAc,oBAAoB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -14,10 +14,9 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
exports.createClient = exports.ApiError =
|
|
17
|
+
exports.createClient = exports.ApiError = void 0;
|
|
18
18
|
// Configuration and client factory
|
|
19
19
|
var custom_fetch_1 = require("./mutator/custom-fetch");
|
|
20
|
-
Object.defineProperty(exports, "configure", { enumerable: true, get: function () { return custom_fetch_1.configure; } });
|
|
21
20
|
Object.defineProperty(exports, "ApiError", { enumerable: true, get: function () { return custom_fetch_1.ApiError; } });
|
|
22
21
|
var create_client_1 = require("./create-client");
|
|
23
22
|
Object.defineProperty(exports, "createClient", { enumerable: true, get: function () { return create_client_1.createClient; } });
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,mCAAmC;AACnC,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,mCAAmC;AACnC,uDAAkD;AAAzC,wGAAA,QAAQ,OAAA;AACjB,iDAA+C;AAAtC,6GAAA,YAAY,OAAA;AAErB,+CAA+C;AAC/C,kDAAgC;AAEhC,0DAA0D;AAC1D,uDAAqC;AACrC,2DAAyC;AACzC,+DAA6C;AAC7C,qDAAmC;AACnC,qDAAmC;AACnC,qDAAmC;AACnC,2EAAyD;AACzD,qDAAmC;AACnC,uDAAqC;AACrC,yDAAuC;AACvC,qDAAmC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"custom-fetch.d.ts","sourceRoot":"","sources":["../../src/mutator/custom-fetch.ts"],"names":[],"mappings":"AAEA,OAAO,CAAC,MAAM,CAAC;IACb,UAAU,WAAW;QACnB,2DAA2D;QAC3D,OAAO,CAAC,EAAE,MAAM,CAAC;KAClB;CACF;
|
|
1
|
+
{"version":3,"file":"custom-fetch.d.ts","sourceRoot":"","sources":["../../src/mutator/custom-fetch.ts"],"names":[],"mappings":"AAEA,OAAO,CAAC,MAAM,CAAC;IACb,UAAU,WAAW;QACnB,2DAA2D;QAC3D,OAAO,CAAC,EAAE,MAAM,CAAC;KAClB;CACF;AAED,qBAAa,QAAS,SAAQ,KAAK;aAEf,MAAM,EAAE,MAAM;aACd,IAAI,EAAE,OAAO;gBADb,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,OAAO;CAKhC;AAED,eAAO,MAAM,WAAW,GAAU,CAAC,EACjC,KAAK,MAAM,EACX,UAAU,WAAW,KACpB,OAAO,CAAC,CAAC,CAcX,CAAC"}
|
|
@@ -1,11 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.customFetch = exports.ApiError = void 0;
|
|
4
|
-
exports.configure = configure;
|
|
5
|
-
let _defaultBaseUrl = 'https://piefed.social';
|
|
6
|
-
function configure(options) {
|
|
7
|
-
_defaultBaseUrl = options.baseUrl.replace(/\/$/, '');
|
|
8
|
-
}
|
|
9
4
|
class ApiError extends Error {
|
|
10
5
|
status;
|
|
11
6
|
data;
|
|
@@ -18,7 +13,9 @@ class ApiError extends Error {
|
|
|
18
13
|
}
|
|
19
14
|
exports.ApiError = ApiError;
|
|
20
15
|
const customFetch = async (url, options) => {
|
|
21
|
-
const { baseUrl
|
|
16
|
+
const { baseUrl, ...fetchOptions } = options ?? {};
|
|
17
|
+
if (!baseUrl)
|
|
18
|
+
throw new Error('baseUrl is required — use createClient() to make requests');
|
|
22
19
|
const base = baseUrl.replace(/\/$/, '');
|
|
23
20
|
const res = await fetch(`${base}${url}`, fetchOptions);
|
|
24
21
|
const body = [204, 205, 304].includes(res.status) ? null : await res.text();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"custom-fetch.js","sourceRoot":"","sources":["../../src/mutator/custom-fetch.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"custom-fetch.js","sourceRoot":"","sources":["../../src/mutator/custom-fetch.ts"],"names":[],"mappings":";;;AASA,MAAa,QAAS,SAAQ,KAAK;IAEf;IACA;IAFlB,YACkB,MAAc,EACd,IAAa;QAE7B,KAAK,CAA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"description": "Typed TypeScript client for the PieFed API, generated from the OpenAPI spec",
|
|
5
5
|
"packageManager": "pnpm@10.6.5",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -22,14 +22,16 @@
|
|
|
22
22
|
"scripts": {
|
|
23
23
|
"generate": "orval --config orval.config.ts",
|
|
24
24
|
"build": "tsc",
|
|
25
|
-
"prepublishOnly": "pnpm build"
|
|
25
|
+
"prepublishOnly": "pnpm build",
|
|
26
|
+
"test": "vitest run"
|
|
26
27
|
},
|
|
27
28
|
"dependencies": {
|
|
28
29
|
"zod": "^3.23.8"
|
|
29
30
|
},
|
|
30
31
|
"devDependencies": {
|
|
31
32
|
"orval": "^7.3.0",
|
|
32
|
-
"typescript": "^5.7.3"
|
|
33
|
+
"typescript": "^5.7.3",
|
|
34
|
+
"vitest": "^2.0.0"
|
|
33
35
|
},
|
|
34
36
|
"repository": {
|
|
35
37
|
"type": "git",
|
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
|
|
2
|
+
import { createClient } from '../create-client';
|
|
3
|
+
import { ApiError } from '../mutator/custom-fetch';
|
|
4
|
+
|
|
5
|
+
const BASE_URL = 'https://test.example.com';
|
|
6
|
+
|
|
7
|
+
function makeResponse(body: unknown, status = 200) {
|
|
8
|
+
return new Response(JSON.stringify(body), {
|
|
9
|
+
status,
|
|
10
|
+
headers: { 'Content-Type': 'application/json' },
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
let fetchSpy: ReturnType<typeof vi.fn>;
|
|
15
|
+
|
|
16
|
+
beforeEach(() => {
|
|
17
|
+
fetchSpy = vi.fn().mockImplementation(() => Promise.resolve(makeResponse({ ok: true })));
|
|
18
|
+
vi.stubGlobal('fetch', fetchSpy);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
afterEach(() => vi.unstubAllGlobals());
|
|
22
|
+
|
|
23
|
+
// ---------------------------------------------------------------------------
|
|
24
|
+
// Helpers
|
|
25
|
+
// ---------------------------------------------------------------------------
|
|
26
|
+
|
|
27
|
+
function lastCall() {
|
|
28
|
+
return fetchSpy.mock.calls[fetchSpy.mock.calls.length - 1] as [string, RequestInit];
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function lastUrl() {
|
|
32
|
+
return lastCall()[0];
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function lastOptions() {
|
|
36
|
+
return lastCall()[1];
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function lastHeaders(): Record<string, string> {
|
|
40
|
+
return (lastOptions().headers ?? {}) as Record<string, string>;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// ---------------------------------------------------------------------------
|
|
44
|
+
// GET no-params (fn.length = 1): getApiAlphaSite(options?)
|
|
45
|
+
// ---------------------------------------------------------------------------
|
|
46
|
+
|
|
47
|
+
describe('getApiAlphaSite — GET, no params', () => {
|
|
48
|
+
it('forwards default headers', async () => {
|
|
49
|
+
const client = createClient(BASE_URL, {
|
|
50
|
+
headers: { Authorization: 'Bearer tok' },
|
|
51
|
+
});
|
|
52
|
+
await client.getApiAlphaSite();
|
|
53
|
+
expect(lastHeaders()['Authorization']).toBe('Bearer tok');
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it('merges per-call headers on top of defaults (per-call wins)', async () => {
|
|
57
|
+
const client = createClient(BASE_URL, {
|
|
58
|
+
headers: { Authorization: 'Bearer default', 'X-Custom': 'base' },
|
|
59
|
+
});
|
|
60
|
+
await client.getApiAlphaSite({ headers: { Authorization: 'Bearer override' } });
|
|
61
|
+
expect(lastHeaders()['Authorization']).toBe('Bearer override');
|
|
62
|
+
expect(lastHeaders()['X-Custom']).toBe('base');
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it('forwards default RequestInit options (e.g. cache)', async () => {
|
|
66
|
+
const client = createClient(BASE_URL, { cache: 'no-store' });
|
|
67
|
+
await client.getApiAlphaSite();
|
|
68
|
+
expect(lastOptions().cache).toBe('no-store');
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it('per-call RequestInit options override defaults', async () => {
|
|
72
|
+
const client = createClient(BASE_URL, { cache: 'no-store' });
|
|
73
|
+
await client.getApiAlphaSite({ cache: 'force-cache' });
|
|
74
|
+
expect(lastOptions().cache).toBe('force-cache');
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it('prepends baseUrl to the path', async () => {
|
|
78
|
+
const client = createClient(BASE_URL);
|
|
79
|
+
await client.getApiAlphaSite();
|
|
80
|
+
expect(lastUrl()).toBe(`${BASE_URL}/api/alpha/site`);
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
// ---------------------------------------------------------------------------
|
|
85
|
+
// GET optional-params (fn.length = 2): getApiAlphaPostList(params?, options?)
|
|
86
|
+
// ---------------------------------------------------------------------------
|
|
87
|
+
|
|
88
|
+
describe('getApiAlphaPostList — GET, optional params', () => {
|
|
89
|
+
it('forwards default headers when params are provided', async () => {
|
|
90
|
+
const client = createClient(BASE_URL, {
|
|
91
|
+
headers: { Authorization: 'Bearer tok' },
|
|
92
|
+
});
|
|
93
|
+
await client.getApiAlphaPostList({ page: 1 });
|
|
94
|
+
expect(lastHeaders()['Authorization']).toBe('Bearer tok');
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it('forwards default headers when params are omitted', async () => {
|
|
98
|
+
const client = createClient(BASE_URL, {
|
|
99
|
+
headers: { Authorization: 'Bearer tok' },
|
|
100
|
+
});
|
|
101
|
+
await client.getApiAlphaPostList();
|
|
102
|
+
expect(lastHeaders()['Authorization']).toBe('Bearer tok');
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it('merges per-call headers (options as second arg)', async () => {
|
|
106
|
+
const client = createClient(BASE_URL, {
|
|
107
|
+
headers: { Authorization: 'Bearer default' },
|
|
108
|
+
});
|
|
109
|
+
await client.getApiAlphaPostList(
|
|
110
|
+
{ page: 1 },
|
|
111
|
+
{ headers: { Authorization: 'Bearer override' } },
|
|
112
|
+
);
|
|
113
|
+
expect(lastHeaders()['Authorization']).toBe('Bearer override');
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it('constructs correct URL with query params', async () => {
|
|
117
|
+
const client = createClient(BASE_URL);
|
|
118
|
+
await client.getApiAlphaPostList({ page: 2 });
|
|
119
|
+
expect(lastUrl()).toContain(`${BASE_URL}/api/alpha/post/list`);
|
|
120
|
+
expect(lastUrl()).toContain('page=2');
|
|
121
|
+
});
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
// ---------------------------------------------------------------------------
|
|
125
|
+
// GET required-params (fn.length = 2): getApiAlphaPost(params, options?)
|
|
126
|
+
// ---------------------------------------------------------------------------
|
|
127
|
+
|
|
128
|
+
describe('getApiAlphaPost — GET, required params', () => {
|
|
129
|
+
const params = { id: 42 };
|
|
130
|
+
|
|
131
|
+
it('forwards default headers', async () => {
|
|
132
|
+
const client = createClient(BASE_URL, {
|
|
133
|
+
headers: { Authorization: 'Bearer tok' },
|
|
134
|
+
});
|
|
135
|
+
await client.getApiAlphaPost(params);
|
|
136
|
+
expect(lastHeaders()['Authorization']).toBe('Bearer tok');
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
it('merges per-call headers', async () => {
|
|
140
|
+
const client = createClient(BASE_URL, {
|
|
141
|
+
headers: { Authorization: 'Bearer default' },
|
|
142
|
+
});
|
|
143
|
+
await client.getApiAlphaPost(params, { headers: { Authorization: 'Bearer override' } });
|
|
144
|
+
expect(lastHeaders()['Authorization']).toBe('Bearer override');
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
it('forwards default cache option', async () => {
|
|
148
|
+
const client = createClient(BASE_URL, { cache: 'no-store' });
|
|
149
|
+
await client.getApiAlphaPost(params);
|
|
150
|
+
expect(lastOptions().cache).toBe('no-store');
|
|
151
|
+
});
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
// ---------------------------------------------------------------------------
|
|
155
|
+
// POST required-body (fn.length = 2): postApiAlphaSiteBlock(body, options?)
|
|
156
|
+
// ---------------------------------------------------------------------------
|
|
157
|
+
|
|
158
|
+
describe('postApiAlphaSiteBlock — POST, required body', () => {
|
|
159
|
+
const body = { instance: 'spam.example.com', block: true };
|
|
160
|
+
|
|
161
|
+
it('sends the body correctly (not corrupted)', async () => {
|
|
162
|
+
const client = createClient(BASE_URL);
|
|
163
|
+
await client.postApiAlphaSiteBlock(body);
|
|
164
|
+
expect(lastOptions().body).toBe(JSON.stringify(body));
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
it('forwards default headers alongside Content-Type', async () => {
|
|
168
|
+
const client = createClient(BASE_URL, {
|
|
169
|
+
headers: { Authorization: 'Bearer tok' },
|
|
170
|
+
});
|
|
171
|
+
await client.postApiAlphaSiteBlock(body);
|
|
172
|
+
expect(lastHeaders()['Authorization']).toBe('Bearer tok');
|
|
173
|
+
expect(lastHeaders()['Content-Type']).toBe('application/json');
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
it('merges per-call headers without losing body', async () => {
|
|
177
|
+
const client = createClient(BASE_URL, {
|
|
178
|
+
headers: { Authorization: 'Bearer default' },
|
|
179
|
+
});
|
|
180
|
+
await client.postApiAlphaSiteBlock(body, {
|
|
181
|
+
headers: { Authorization: 'Bearer override' },
|
|
182
|
+
});
|
|
183
|
+
expect(lastHeaders()['Authorization']).toBe('Bearer override');
|
|
184
|
+
expect(lastOptions().body).toBe(JSON.stringify(body));
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
it('uses POST method', async () => {
|
|
188
|
+
const client = createClient(BASE_URL);
|
|
189
|
+
await client.postApiAlphaSiteBlock(body);
|
|
190
|
+
expect(lastOptions().method).toBe('POST');
|
|
191
|
+
});
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
// ---------------------------------------------------------------------------
|
|
195
|
+
// Error handling
|
|
196
|
+
// ---------------------------------------------------------------------------
|
|
197
|
+
|
|
198
|
+
describe('ApiError', () => {
|
|
199
|
+
it('throws ApiError with correct status and data on 4xx', async () => {
|
|
200
|
+
fetchSpy.mockImplementation(() => Promise.resolve(makeResponse({ error: 'not found' }, 404)));
|
|
201
|
+
const client = createClient(BASE_URL);
|
|
202
|
+
|
|
203
|
+
await expect(client.getApiAlphaSite()).rejects.toMatchObject({
|
|
204
|
+
name: 'ApiError',
|
|
205
|
+
status: 404,
|
|
206
|
+
data: { error: 'not found' },
|
|
207
|
+
});
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
it('throws ApiError on 5xx', async () => {
|
|
211
|
+
fetchSpy.mockImplementation(() => Promise.resolve(makeResponse({ error: 'server error' }, 500)));
|
|
212
|
+
const client = createClient(BASE_URL);
|
|
213
|
+
|
|
214
|
+
const err = await client.getApiAlphaSite().catch((e) => e);
|
|
215
|
+
expect(err).toBeInstanceOf(ApiError);
|
|
216
|
+
expect(err.status).toBe(500);
|
|
217
|
+
});
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
// ---------------------------------------------------------------------------
|
|
221
|
+
// AbortController
|
|
222
|
+
// ---------------------------------------------------------------------------
|
|
223
|
+
|
|
224
|
+
describe('AbortController', () => {
|
|
225
|
+
it('forwards signal to fetch', async () => {
|
|
226
|
+
const controller = new AbortController();
|
|
227
|
+
const client = createClient(BASE_URL);
|
|
228
|
+
await client.getApiAlphaSite({ signal: controller.signal });
|
|
229
|
+
expect(lastOptions().signal).toBe(controller.signal);
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
it('propagates abort error when signal is already aborted', async () => {
|
|
233
|
+
const controller = new AbortController();
|
|
234
|
+
controller.abort();
|
|
235
|
+
fetchSpy.mockImplementation((_url: string, opts: RequestInit) => {
|
|
236
|
+
if (opts.signal?.aborted) return Promise.reject(new DOMException('Aborted', 'AbortError'));
|
|
237
|
+
return Promise.resolve(makeResponse({ ok: true }));
|
|
238
|
+
});
|
|
239
|
+
const client = createClient(BASE_URL);
|
|
240
|
+
const err = await client.getApiAlphaSite({ signal: controller.signal }).catch((e) => e);
|
|
241
|
+
expect(err.name).toBe('AbortError');
|
|
242
|
+
});
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
// ---------------------------------------------------------------------------
|
|
246
|
+
// baseUrl
|
|
247
|
+
// ---------------------------------------------------------------------------
|
|
248
|
+
|
|
249
|
+
describe('baseUrl construction', () => {
|
|
250
|
+
it('trims trailing slash from baseUrl', async () => {
|
|
251
|
+
const client = createClient('https://test.example.com/');
|
|
252
|
+
await client.getApiAlphaSite();
|
|
253
|
+
expect(lastUrl()).toBe('https://test.example.com/api/alpha/site');
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
it('different clients use their own baseUrl', async () => {
|
|
257
|
+
const a = createClient('https://instance-a.com');
|
|
258
|
+
const b = createClient('https://instance-b.com');
|
|
259
|
+
|
|
260
|
+
await a.getApiAlphaSite();
|
|
261
|
+
const urlA = lastUrl();
|
|
262
|
+
|
|
263
|
+
await b.getApiAlphaSite();
|
|
264
|
+
const urlB = lastUrl();
|
|
265
|
+
|
|
266
|
+
expect(urlA).toBe('https://instance-a.com/api/alpha/site');
|
|
267
|
+
expect(urlB).toBe('https://instance-b.com/api/alpha/site');
|
|
268
|
+
});
|
|
269
|
+
});
|
package/src/create-client.ts
CHANGED
|
@@ -26,9 +26,8 @@ const allFns = {
|
|
|
26
26
|
|
|
27
27
|
// Only the async API functions (not URL helpers or type exports)
|
|
28
28
|
type AsyncFn = (...args: any[]) => Promise<any>;
|
|
29
|
-
type
|
|
30
|
-
|
|
31
|
-
};
|
|
29
|
+
type AsyncKeys = { [K in keyof typeof allFns]: (typeof allFns)[K] extends AsyncFn ? K : never }[keyof typeof allFns];
|
|
30
|
+
type ApiFunctions = Pick<typeof allFns, AsyncKeys>;
|
|
32
31
|
|
|
33
32
|
interface CreateClientOptions extends Omit<RequestInit, 'body' | 'method'> {
|
|
34
33
|
/** Default headers merged into every request (e.g. Authorization). */
|
package/src/index.ts
CHANGED
|
@@ -7,12 +7,6 @@ declare global {
|
|
|
7
7
|
}
|
|
8
8
|
}
|
|
9
9
|
|
|
10
|
-
let _defaultBaseUrl = 'https://piefed.social';
|
|
11
|
-
|
|
12
|
-
export function configure(options: { baseUrl: string }) {
|
|
13
|
-
_defaultBaseUrl = options.baseUrl.replace(/\/$/, '');
|
|
14
|
-
}
|
|
15
|
-
|
|
16
10
|
export class ApiError extends Error {
|
|
17
11
|
constructor(
|
|
18
12
|
public readonly status: number,
|
|
@@ -27,7 +21,8 @@ export const customFetch = async <T>(
|
|
|
27
21
|
url: string,
|
|
28
22
|
options?: RequestInit,
|
|
29
23
|
): Promise<T> => {
|
|
30
|
-
const { baseUrl
|
|
24
|
+
const { baseUrl, ...fetchOptions } = options ?? {};
|
|
25
|
+
if (!baseUrl) throw new Error('baseUrl is required — use createClient() to make requests');
|
|
31
26
|
const base = baseUrl.replace(/\/$/, '');
|
|
32
27
|
|
|
33
28
|
const res = await fetch(`${base}${url}`, fetchOptions);
|