@blorp-labs/piefed-api-client 0.0.0-0935c38 → 0.0.0-774e8e8
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/README.md +61 -10
- package/dist/create-client.d.ts +20 -19
- package/dist/create-client.d.ts.map +1 -1
- package/dist/create-client.js +15 -25
- package/dist/create-client.js.map +1 -1
- package/package.json +1 -1
- package/src/create-client.ts +19 -23
package/README.md
CHANGED
|
@@ -10,18 +10,70 @@ Includes:
|
|
|
10
10
|
## Installation
|
|
11
11
|
|
|
12
12
|
```sh
|
|
13
|
-
# .npmrc — point @blorp-labs scope to GitHub Packages
|
|
14
|
-
echo "@blorp-labs:registry=https://npm.pkg.github.com" >> .npmrc
|
|
15
|
-
|
|
16
13
|
pnpm add @blorp-labs/piefed-api-client
|
|
17
14
|
```
|
|
18
15
|
|
|
19
16
|
## Usage
|
|
20
17
|
|
|
18
|
+
### Basic
|
|
19
|
+
|
|
20
|
+
```ts
|
|
21
|
+
import { createClient } from '@blorp-labs/piefed-api-client';
|
|
22
|
+
|
|
23
|
+
const client = createClient('https://piefed.social');
|
|
24
|
+
const site = await client.getApiAlphaSite();
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
### With authentication
|
|
28
|
+
|
|
21
29
|
```ts
|
|
22
|
-
|
|
30
|
+
const client = createClient('https://piefed.social', {
|
|
31
|
+
headers: { Authorization: 'Bearer <token>' },
|
|
32
|
+
});
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
### With default fetch options
|
|
36
|
+
|
|
37
|
+
Any [`RequestInit`](https://developer.mozilla.org/en-US/docs/Web/API/RequestInit) option (except `body` and `method`) can be set as a default:
|
|
38
|
+
|
|
39
|
+
```ts
|
|
40
|
+
const client = createClient('https://piefed.social', {
|
|
41
|
+
cache: 'no-store',
|
|
42
|
+
headers: { Authorization: 'Bearer <token>' },
|
|
43
|
+
});
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
Per-call options override the defaults; headers are merged.
|
|
23
47
|
|
|
24
|
-
|
|
48
|
+
### Multiple instances
|
|
49
|
+
|
|
50
|
+
```ts
|
|
51
|
+
const a = createClient('https://instance-a.com');
|
|
52
|
+
const b = createClient('https://instance-b.com');
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### Error handling
|
|
56
|
+
|
|
57
|
+
Errors throw an `ApiError` with `.status` and `.data`:
|
|
58
|
+
|
|
59
|
+
```ts
|
|
60
|
+
import { createClient, ApiError } from '@blorp-labs/piefed-api-client';
|
|
61
|
+
|
|
62
|
+
try {
|
|
63
|
+
const site = await client.getApiAlphaSite();
|
|
64
|
+
} catch (e) {
|
|
65
|
+
if (e instanceof ApiError) {
|
|
66
|
+
console.error(e.status, e.data);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
### Zod schemas
|
|
72
|
+
|
|
73
|
+
Zod schemas are exported from the `/zod` subpath:
|
|
74
|
+
|
|
75
|
+
```ts
|
|
76
|
+
import { getApiAlphaSiteQueryParams } from '@blorp-labs/piefed-api-client/zod';
|
|
25
77
|
```
|
|
26
78
|
|
|
27
79
|
## Development
|
|
@@ -37,8 +89,7 @@ pnpm build # compile TypeScript → dist/
|
|
|
37
89
|
|
|
38
90
|
## Automation
|
|
39
91
|
|
|
40
|
-
A GitHub Actions workflow
|
|
41
|
-
1. Regenerates `src/`
|
|
42
|
-
2.
|
|
43
|
-
3.
|
|
44
|
-
4. Publishes to GitHub Packages
|
|
92
|
+
A GitHub Actions workflow triggers on every push to `main`. It:
|
|
93
|
+
1. Regenerates `src/` from the live OpenAPI spec
|
|
94
|
+
2. Sets the version to `0.0.0-{commit-sha}`
|
|
95
|
+
3. Builds and publishes to npm
|
package/dist/create-client.d.ts
CHANGED
|
@@ -44,22 +44,7 @@ declare const allFns: {
|
|
|
44
44
|
getPostApiAlphaUploadImageUrl: () => string;
|
|
45
45
|
postApiAlphaUploadImage: (imageUploadRequest: import("./schemas").ImageUploadRequest, options?: RequestInit) => Promise<import("./schemas").ImageUploadResponse>;
|
|
46
46
|
getPostApiAlphaUploadCommunityImageUrl: () => string;
|
|
47
|
-
postApiAlphaUploadCommunityImage: (imageUploadRequest: import("./schemas").ImageUploadRequest
|
|
48
|
-
* Create a client bound to a specific PieFed instance.
|
|
49
|
-
*
|
|
50
|
-
* @example
|
|
51
|
-
* const client = createClient('https://piefed.social');
|
|
52
|
-
* const site = await client.getApiAlphaSite();
|
|
53
|
-
*
|
|
54
|
-
* // With auth:
|
|
55
|
-
* const client = createClient('https://piefed.social', {
|
|
56
|
-
* headers: { Authorization: 'Bearer <token>' },
|
|
57
|
-
* });
|
|
58
|
-
*
|
|
59
|
-
* // Two instances at the same time:
|
|
60
|
-
* const a = createClient('https://instance-a.com');
|
|
61
|
-
* const b = createClient('https://instance-b.com');
|
|
62
|
-
*/, options?: RequestInit) => Promise<import("./schemas").ImageUploadResponse>;
|
|
47
|
+
postApiAlphaUploadCommunityImage: (imageUploadRequest: import("./schemas").ImageUploadRequest, options?: RequestInit) => Promise<import("./schemas").ImageUploadResponse>;
|
|
63
48
|
getPostApiAlphaUploadUserImageUrl: () => string;
|
|
64
49
|
postApiAlphaUploadUserImage: (imageUploadRequest: import("./schemas").ImageUploadRequest, options?: RequestInit) => Promise<import("./schemas").ImageUploadResponse>;
|
|
65
50
|
getPostApiAlphaImageDeleteUrl: () => string;
|
|
@@ -93,7 +78,8 @@ declare const allFns: {
|
|
|
93
78
|
getPostApiAlphaPrivateMessageReportUrl: () => string;
|
|
94
79
|
postApiAlphaPrivateMessageReport: (reportPrivateMessageRequest: import("./schemas").ReportPrivateMessageRequest, options?: RequestInit) => Promise<import("./schemas").PrivateMessageResponse>;
|
|
95
80
|
getGetApiAlphaPostListUrl: (params?: import("./schemas").GetApiAlphaPostListParams) => string;
|
|
96
|
-
getApiAlphaPostList: (
|
|
81
|
+
getApiAlphaPostList: (// Two instances at the same time:
|
|
82
|
+
params?: import("./schemas").GetApiAlphaPostListParams, options?: RequestInit) => Promise<import("./schemas").ListPostsResponse>;
|
|
97
83
|
getGetApiAlphaPostList2Url: (params?: import("./schemas").GetApiAlphaPostList2Params) => string;
|
|
98
84
|
getApiAlphaPostList2: (params?: import("./schemas").GetApiAlphaPostList2Params, options?: RequestInit) => Promise<import("./schemas").ListPostsResponse>;
|
|
99
85
|
getGetApiAlphaPostUrl: (params: import("./schemas").GetApiAlphaPostParams) => string;
|
|
@@ -217,7 +203,22 @@ declare const allFns: {
|
|
|
217
203
|
getGetApiAlphaCommentLikeListUrl: (params: import("./schemas").GetApiAlphaCommentLikeListParams) => string;
|
|
218
204
|
getApiAlphaCommentLikeList: (params: import("./schemas").GetApiAlphaCommentLikeListParams, options?: RequestInit) => Promise<import("./schemas").ListCommentLikesResponse>;
|
|
219
205
|
getGetApiAlphaAdminRegistrationApplicationListUrl: (params?: import("./schemas").GetApiAlphaAdminRegistrationApplicationListParams) => string;
|
|
220
|
-
getApiAlphaAdminRegistrationApplicationList: (params?: import("./schemas").GetApiAlphaAdminRegistrationApplicationListParams, options?: RequestInit
|
|
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>;
|
|
221
222
|
getPutApiAlphaAdminRegistrationApplicationApproveUrl: () => string;
|
|
222
223
|
putApiAlphaAdminRegistrationApplicationApprove: (registrationApproveRequest: import("./schemas").RegistrationApproveRequest, options?: RequestInit) => Promise<void>;
|
|
223
224
|
};
|
|
@@ -225,7 +226,7 @@ type AsyncFn = (...args: any[]) => Promise<any>;
|
|
|
225
226
|
type ApiFunctions = {
|
|
226
227
|
[K in keyof typeof allFns as (typeof allFns)[K] extends AsyncFn ? K : never]: (typeof allFns)[K];
|
|
227
228
|
};
|
|
228
|
-
interface CreateClientOptions {
|
|
229
|
+
interface CreateClientOptions extends Omit<RequestInit, 'body' | 'method'> {
|
|
229
230
|
/** Default headers merged into every request (e.g. Authorization). */
|
|
230
231
|
headers?: Record<string, string>;
|
|
231
232
|
}
|
|
@@ -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;;8BA6B+B,0DACnB,EAAC,qBAAsB;4BAY7C,kDAA4B;sBAYA,kDAA8B,EAAC,qBAC7C;;wBASR,qBAGP;;4BAMwP,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,4D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/dist/create-client.js
CHANGED
|
@@ -79,33 +79,23 @@ function createClient(baseUrl, options = {}) {
|
|
|
79
79
|
for (const [key, fn] of Object.entries(allFns)) {
|
|
80
80
|
if (typeof fn !== 'function')
|
|
81
81
|
continue;
|
|
82
|
+
// fn.length is the total param count; options is always the last param
|
|
83
|
+
const optionsIndex = fn.length - 1;
|
|
82
84
|
boundFns[key] = (...args) => {
|
|
83
|
-
//
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
85
|
+
// Pad args so options index is always reachable
|
|
86
|
+
while (args.length <= optionsIndex)
|
|
87
|
+
args.push(undefined);
|
|
88
|
+
const { headers: defaultHeaders, ...defaultInit } = options;
|
|
89
|
+
const extra = { ...defaultInit, baseUrl };
|
|
90
|
+
const existing = args[optionsIndex];
|
|
91
|
+
args[optionsIndex] = {
|
|
92
|
+
...extra,
|
|
93
|
+
...existing,
|
|
94
|
+
headers: {
|
|
95
|
+
...defaultHeaders,
|
|
96
|
+
...existing?.headers,
|
|
97
|
+
},
|
|
88
98
|
};
|
|
89
|
-
if (last !== null && typeof last === 'object' && !Array.isArray(last)) {
|
|
90
|
-
const lastObj = last;
|
|
91
|
-
args[args.length - 1] = {
|
|
92
|
-
...extra,
|
|
93
|
-
...lastObj,
|
|
94
|
-
// per-call headers win, but default headers fill in any gaps
|
|
95
|
-
headers: { ...options.headers, ...lastObj.headers },
|
|
96
|
-
};
|
|
97
|
-
}
|
|
98
|
-
else if (last === undefined || args.length === 0) {
|
|
99
|
-
if (args.length > 0) {
|
|
100
|
-
args[args.length - 1] = extra;
|
|
101
|
-
}
|
|
102
|
-
else {
|
|
103
|
-
args.push(extra);
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
else {
|
|
107
|
-
args.push(extra);
|
|
108
|
-
}
|
|
109
99
|
return fn(...args);
|
|
110
100
|
};
|
|
111
101
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"create-client.js","sourceRoot":"","sources":["../src/create-client.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAqDA,
|
|
1
|
+
{"version":3,"file":"create-client.js","sourceRoot":"","sources":["../src/create-client.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAqDA,oCA+BC;AApFD,4DAA8C;AAC9C,kEAAoD;AACpD,wEAA0D;AAC1D,yDAA2C;AAC3C,yDAA2C;AAC3C,yDAA2C;AAC3C,yFAA2E;AAC3E,yDAA2C;AAC3C,4DAA8C;AAC9C,+DAAiD;AACjD,yDAA2C;AAE3C,MAAM,MAAM,GAAG;IACb,GAAG,KAAK;IACR,GAAG,OAAO;IACV,GAAG,SAAS;IACZ,GAAG,IAAI;IACP,GAAG,IAAI;IACP,GAAG,IAAI;IACP,GAAG,cAAc;IACjB,GAAG,IAAI;IACP,GAAG,KAAK;IACR,GAAG,MAAM;IACT,GAAG,IAAI;CACR,CAAC;AAaF;;;;;;;;;;;;;;;GAeG;AACH,SAAgB,YAAY,CAAC,OAAe,EAAE,UAA+B,EAAE;IAC7E,MAAM,QAAQ,GAAG,EAA6B,CAAC;IAE/C,KAAK,MAAM,CAAC,GAAG,EAAE,EAAE,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,CAAC;QAC/C,IAAI,OAAO,EAAE,KAAK,UAAU;YAAE,SAAS;QAEvC,uEAAuE;QACvE,MAAM,YAAY,GAAI,EAAc,CAAC,MAAM,GAAG,CAAC,CAAC;QAEhD,QAAQ,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,IAAe,EAAE,EAAE;YACrC,gDAAgD;YAChD,OAAO,IAAI,CAAC,MAAM,IAAI,YAAY;gBAAE,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;YAEzD,MAAM,EAAE,OAAO,EAAE,cAAc,EAAE,GAAG,WAAW,EAAE,GAAG,OAAO,CAAC;YAC5D,MAAM,KAAK,GAAsC,EAAE,GAAG,WAAW,EAAE,OAAO,EAAE,CAAC;YAC7E,MAAM,QAAQ,GAAG,IAAI,CAAC,YAAY,CAAqD,CAAC;YAExF,IAAI,CAAC,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/package.json
CHANGED
package/src/create-client.ts
CHANGED
|
@@ -30,7 +30,7 @@ type ApiFunctions = {
|
|
|
30
30
|
[K in keyof typeof allFns as (typeof allFns)[K] extends AsyncFn ? K : never]: (typeof allFns)[K];
|
|
31
31
|
};
|
|
32
32
|
|
|
33
|
-
interface CreateClientOptions {
|
|
33
|
+
interface CreateClientOptions extends Omit<RequestInit, 'body' | 'method'> {
|
|
34
34
|
/** Default headers merged into every request (e.g. Authorization). */
|
|
35
35
|
headers?: Record<string, string>;
|
|
36
36
|
}
|
|
@@ -57,30 +57,26 @@ export function createClient(baseUrl: string, options: CreateClientOptions = {})
|
|
|
57
57
|
for (const [key, fn] of Object.entries(allFns)) {
|
|
58
58
|
if (typeof fn !== 'function') continue;
|
|
59
59
|
|
|
60
|
+
// fn.length is the total param count; options is always the last param
|
|
61
|
+
const optionsIndex = (fn as AsyncFn).length - 1;
|
|
62
|
+
|
|
60
63
|
boundFns[key] = (...args: unknown[]) => {
|
|
61
|
-
//
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
64
|
+
// Pad args so options index is always reachable
|
|
65
|
+
while (args.length <= optionsIndex) args.push(undefined);
|
|
66
|
+
|
|
67
|
+
const { headers: defaultHeaders, ...defaultInit } = options;
|
|
68
|
+
const extra: RequestInit & { baseUrl: string } = { ...defaultInit, baseUrl };
|
|
69
|
+
const existing = args[optionsIndex] as (RequestInit & { baseUrl?: string }) | undefined;
|
|
70
|
+
|
|
71
|
+
args[optionsIndex] = {
|
|
72
|
+
...extra,
|
|
73
|
+
...existing,
|
|
74
|
+
headers: {
|
|
75
|
+
...defaultHeaders,
|
|
76
|
+
...(existing?.headers as Record<string, string> | undefined),
|
|
77
|
+
},
|
|
66
78
|
};
|
|
67
|
-
|
|
68
|
-
const lastObj = last as RequestInit & { baseUrl?: string };
|
|
69
|
-
args[args.length - 1] = {
|
|
70
|
-
...extra,
|
|
71
|
-
...lastObj,
|
|
72
|
-
// per-call headers win, but default headers fill in any gaps
|
|
73
|
-
headers: { ...options.headers, ...(lastObj.headers as Record<string, string> | undefined) },
|
|
74
|
-
};
|
|
75
|
-
} else if (last === undefined || args.length === 0) {
|
|
76
|
-
if (args.length > 0) {
|
|
77
|
-
args[args.length - 1] = extra;
|
|
78
|
-
} else {
|
|
79
|
-
args.push(extra);
|
|
80
|
-
}
|
|
81
|
-
} else {
|
|
82
|
-
args.push(extra);
|
|
83
|
-
}
|
|
79
|
+
|
|
84
80
|
return (fn as AsyncFn)(...args);
|
|
85
81
|
};
|
|
86
82
|
}
|