notefeed 0.3.0 → 0.5.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/README.md +9 -6
- package/dist/bin.js +6 -0
- package/dist/cli.js +42 -15
- package/dist/client.d.ts +52 -17
- package/dist/client.js +127 -53
- package/dist/generated/client/client.gen.d.ts +2 -0
- package/dist/generated/client/client.gen.js +216 -0
- package/dist/generated/client/index.d.ts +10 -0
- package/dist/generated/client/index.js +6 -0
- package/dist/generated/client/types.gen.d.ts +120 -0
- package/dist/generated/client/types.gen.js +2 -0
- package/dist/generated/client/utils.gen.d.ts +37 -0
- package/dist/generated/client/utils.gen.js +228 -0
- package/dist/generated/client.gen.d.ts +12 -0
- package/dist/generated/client.gen.js +3 -0
- package/dist/generated/core/auth.gen.d.ts +25 -0
- package/dist/generated/core/auth.gen.js +14 -0
- package/dist/generated/core/bodySerializer.gen.d.ts +25 -0
- package/dist/generated/core/bodySerializer.gen.js +57 -0
- package/dist/generated/core/params.gen.d.ts +43 -0
- package/dist/generated/core/params.gen.js +109 -0
- package/dist/generated/core/pathSerializer.gen.d.ts +33 -0
- package/dist/generated/core/pathSerializer.gen.js +106 -0
- package/dist/generated/core/queryKeySerializer.gen.d.ts +18 -0
- package/dist/generated/core/queryKeySerializer.gen.js +92 -0
- package/dist/generated/core/serverSentEvents.gen.d.ts +71 -0
- package/dist/generated/core/serverSentEvents.gen.js +132 -0
- package/dist/generated/core/types.gen.d.ts +83 -0
- package/dist/generated/core/types.gen.js +2 -0
- package/dist/generated/core/utils.gen.d.ts +19 -0
- package/dist/generated/core/utils.gen.js +87 -0
- package/dist/generated/index.d.ts +2 -0
- package/dist/generated/index.js +2 -0
- package/dist/generated/sdk.gen.d.ts +49 -0
- package/dist/generated/sdk.gen.js +52 -0
- package/dist/generated/types.gen.d.ts +276 -0
- package/dist/generated/types.gen.js +2 -0
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1 -1
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -1,21 +1,24 @@
|
|
|
1
1
|
# notefeed
|
|
2
2
|
|
|
3
|
-
Post markdown notes
|
|
3
|
+
Post and read markdown notes on a [notefeed](https://github.com/Marceltov/notefeed) server from Node or the command line. No dependencies; Node 20 or newer.
|
|
4
4
|
|
|
5
5
|
```sh
|
|
6
6
|
npm install notefeed
|
|
7
|
-
export NOTEFEED_URL=https://notes.example.com
|
|
7
|
+
export NOTEFEED_URL=https://notes.example.com NOTEFEED_FEED=homelab-7f3k2q9x4m8wz
|
|
8
8
|
npx notefeed post "# Backup finished"
|
|
9
9
|
backup.sh 2>&1 | npx notefeed post -
|
|
10
|
+
npx notefeed notes --limit 5
|
|
10
11
|
```
|
|
11
12
|
|
|
12
13
|
```js
|
|
13
14
|
import { Client } from "notefeed";
|
|
14
15
|
|
|
15
|
-
const client = new Client({ url: "https://notes.example.com",
|
|
16
|
-
|
|
16
|
+
const client = new Client({ url: "https://notes.example.com", feed: "homelab-7f3k2q9x4m8wz" }); // password: "..." if the instance has one
|
|
17
|
+
const created = await client.post("# Deploy done\nversion 1.4.2");
|
|
18
|
+
console.log(created.url, created.read_url);
|
|
19
|
+
for await (const note of client.notes()) console.log(note.created_at, note.title); // newest first, page by page
|
|
17
20
|
```
|
|
18
21
|
|
|
19
|
-
|
|
22
|
+
`Client.fromEnv()` reads `NOTEFEED_URL` / `NOTEFEED_FEED` / `NOTEFEED_PASSWORD`; so does the `notefeed` command, unless given `--url` / `--feed` / `--password`.
|
|
20
23
|
|
|
21
|
-
Full documentation: https://notefeed.me/clients/
|
|
24
|
+
Full documentation: https://docs.notefeed.me/clients/
|
package/dist/bin.js
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import { main } from "./cli.js";
|
|
3
|
+
// The reader went away (`notefeed notes | head -1`): stop quietly instead of crashing on EPIPE.
|
|
4
|
+
process.stdout.on("error", (e) => {
|
|
5
|
+
if (e.code === "EPIPE")
|
|
6
|
+
process.exit(0);
|
|
7
|
+
throw e;
|
|
8
|
+
});
|
|
3
9
|
process.exitCode = await main(process.argv.slice(2));
|
package/dist/cli.js
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
|
-
/** `notefeed post ...`
|
|
1
|
+
/** `notefeed post ...` and `notefeed notes ...`: post and read notes from the command line. */
|
|
2
2
|
import { readFileSync } from "node:fs";
|
|
3
3
|
import { readFile } from "node:fs/promises";
|
|
4
4
|
import { parseArgs } from "node:util";
|
|
5
5
|
import { Client, ConfigError, NotefeedError } from "./client.js";
|
|
6
6
|
class UsageError extends Error {
|
|
7
7
|
}
|
|
8
|
-
const USAGE =
|
|
8
|
+
const USAGE = [
|
|
9
|
+
"usage: notefeed post <text | - | --file PATH> [--url URL] [--feed FEED] [--password PASSWORD]",
|
|
10
|
+
" notefeed notes [--limit N] [--json] [--url URL] [--feed FEED] [--password PASSWORD]",
|
|
11
|
+
].join("\n");
|
|
9
12
|
/** Returns the exit code: 0 ok, 1 server/network error, 2 usage/config error. */
|
|
10
13
|
export async function main(argv, io = process) {
|
|
11
14
|
try {
|
|
@@ -14,8 +17,11 @@ export async function main(argv, io = process) {
|
|
|
14
17
|
allowPositionals: true,
|
|
15
18
|
options: {
|
|
16
19
|
url: { type: "string" },
|
|
17
|
-
|
|
20
|
+
feed: { type: "string" },
|
|
21
|
+
password: { type: "string" },
|
|
18
22
|
file: { type: "string" },
|
|
23
|
+
limit: { type: "string" },
|
|
24
|
+
json: { type: "boolean" },
|
|
19
25
|
version: { type: "boolean" },
|
|
20
26
|
help: { type: "boolean", short: "h" },
|
|
21
27
|
},
|
|
@@ -30,18 +36,29 @@ export async function main(argv, io = process) {
|
|
|
30
36
|
return 0;
|
|
31
37
|
}
|
|
32
38
|
const [command, text, ...rest] = positionals;
|
|
33
|
-
if (command
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
39
|
+
if (command === "post" && !rest.length) {
|
|
40
|
+
const markdown = await read(text, values.file, io);
|
|
41
|
+
const note = await client(values).post(markdown);
|
|
42
|
+
io.stdout.write(`${note.url}\n`);
|
|
43
|
+
return 0;
|
|
44
|
+
}
|
|
45
|
+
if (command === "notes" && text === undefined) {
|
|
46
|
+
const limit = Number(values.limit ?? 20);
|
|
47
|
+
if (!Number.isInteger(limit) || limit < 1)
|
|
48
|
+
throw new UsageError("--limit must be a whole number, 1 or more");
|
|
49
|
+
let left = limit;
|
|
50
|
+
for await (const n of client(values).notes({ pageSize: Math.min(limit, 100) })) {
|
|
51
|
+
// To the second, UTC: the same form as the Python CLI, so scripts read either the same way.
|
|
52
|
+
const when = new Date(n.created_at).toISOString().replace(/\.\d+Z$/, "Z");
|
|
53
|
+
// --json: exactly the documented fields, like the Python CLI, even if the server adds more.
|
|
54
|
+
const fields = { id: n.id, title: n.title, markdown: n.markdown, created_at: when, url: n.url };
|
|
55
|
+
io.stdout.write(values.json ? `${JSON.stringify(fields)}\n` : `${when} ${n.title || n.id} ${n.url}\n`);
|
|
56
|
+
if (--left === 0)
|
|
57
|
+
break;
|
|
58
|
+
}
|
|
59
|
+
return 0;
|
|
60
|
+
}
|
|
61
|
+
throw new UsageError(USAGE);
|
|
45
62
|
}
|
|
46
63
|
catch (e) {
|
|
47
64
|
io.stderr.write(`notefeed: ${e.message.split("\n")[0]}\n`);
|
|
@@ -52,6 +69,16 @@ export async function main(argv, io = process) {
|
|
|
52
69
|
return 2; // parseArgs rejects unknown options with a TypeError
|
|
53
70
|
}
|
|
54
71
|
}
|
|
72
|
+
// Flags win over NOTEFEED_URL, NOTEFEED_FEED and NOTEFEED_PASSWORD.
|
|
73
|
+
function client(values) {
|
|
74
|
+
const url = values.url || process.env.NOTEFEED_URL;
|
|
75
|
+
const feed = values.feed || process.env.NOTEFEED_FEED;
|
|
76
|
+
if (!url)
|
|
77
|
+
throw new UsageError("no URL given; pass --url or set NOTEFEED_URL");
|
|
78
|
+
if (!feed)
|
|
79
|
+
throw new UsageError("no feed given; pass --feed or set NOTEFEED_FEED");
|
|
80
|
+
return new Client({ url, feed, password: values.password || process.env.NOTEFEED_PASSWORD });
|
|
81
|
+
}
|
|
55
82
|
async function read(text, file, io) {
|
|
56
83
|
if (file) {
|
|
57
84
|
try {
|
package/dist/client.d.ts
CHANGED
|
@@ -1,35 +1,70 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
import type { Created, Error as ApiError, Note } from "./generated/types.gen.js";
|
|
2
|
+
/** The stable error codes the API answers with. */
|
|
3
|
+
export type ErrorCode = NonNullable<ApiError["code"]>;
|
|
4
|
+
export type { Created, Note };
|
|
5
|
+
/** `timeoutMs` (default 10000) limits each whole request, including reading the answer. */
|
|
6
6
|
export type ClientOptions = {
|
|
7
7
|
url: string;
|
|
8
|
-
|
|
8
|
+
feed?: string;
|
|
9
|
+
password?: string;
|
|
9
10
|
timeoutMs?: number;
|
|
10
11
|
};
|
|
11
|
-
/** Any failure talking to notefeed. `status`
|
|
12
|
+
/** Any failure talking to notefeed. `status` and `code` are null when there was no API answer. */
|
|
12
13
|
export declare class NotefeedError extends Error {
|
|
13
14
|
readonly status: number | null;
|
|
14
|
-
|
|
15
|
+
readonly code: ErrorCode | null;
|
|
16
|
+
constructor(message: string, status?: number | null, code?: ErrorCode | null);
|
|
15
17
|
}
|
|
16
|
-
/** URL or
|
|
18
|
+
/** Raised before sending: no URL, no feed, an invalid feed name or password. */
|
|
17
19
|
export declare class ConfigError extends NotefeedError {
|
|
18
20
|
}
|
|
19
|
-
/**
|
|
20
|
-
export declare class InvalidNoteError extends NotefeedError {
|
|
21
|
-
}
|
|
22
|
-
/** Missing or wrong token (401). */
|
|
21
|
+
/** Missing or wrong password on a locked instance. */
|
|
23
22
|
export declare class AuthError extends NotefeedError {
|
|
24
23
|
}
|
|
25
|
-
/**
|
|
24
|
+
/** Too many posts or wrong passwords. `retryAfter` is the wait in seconds, or null if none was given. */
|
|
25
|
+
export declare class RateLimitedError extends NotefeedError {
|
|
26
|
+
readonly retryAfter: number | null;
|
|
27
|
+
constructor(message: string, status: number | null, code: ErrorCode | null, retryAfter: number | null);
|
|
28
|
+
}
|
|
29
|
+
/** No such note, or a malformed read id. */
|
|
30
|
+
export declare class NotFoundError extends NotefeedError {
|
|
31
|
+
}
|
|
32
|
+
/** The instance's feed or note limit is reached. */
|
|
33
|
+
export declare class LimitReachedError extends NotefeedError {
|
|
34
|
+
}
|
|
35
|
+
/** The note is over the server's size limit. */
|
|
26
36
|
export declare class NoteTooLargeError extends NotefeedError {
|
|
27
37
|
}
|
|
38
|
+
/** The server refused the request itself: invalid or reserved feed, empty note, bad body or parameter. */
|
|
39
|
+
export declare class InvalidRequestError extends NotefeedError {
|
|
40
|
+
}
|
|
28
41
|
export declare class Client {
|
|
29
42
|
readonly url: string;
|
|
30
|
-
|
|
43
|
+
readonly feed: string | null;
|
|
44
|
+
private readonly api;
|
|
31
45
|
private readonly timeoutMs;
|
|
32
|
-
/** A notefeed server.
|
|
46
|
+
/** A notefeed server. The feed set here is the default for every call; each call can override it. */
|
|
33
47
|
constructor(options: ClientOptions);
|
|
34
|
-
|
|
48
|
+
/** NOTEFEED_URL, NOTEFEED_FEED and NOTEFEED_PASSWORD. */
|
|
49
|
+
static fromEnv(env?: Record<string, string | undefined>): Client;
|
|
50
|
+
post(markdown: string, options?: {
|
|
51
|
+
feed?: string;
|
|
52
|
+
}): Promise<Created>;
|
|
53
|
+
/** Every note in the feed, newest first, fetched a page at a time; stop iterating whenever you like. */
|
|
54
|
+
notes(options?: {
|
|
55
|
+
feed?: string;
|
|
56
|
+
pageSize?: number;
|
|
57
|
+
}): AsyncGenerator<Note>;
|
|
58
|
+
note(id: string, options?: {
|
|
59
|
+
feed?: string;
|
|
60
|
+
}): Promise<Note>;
|
|
61
|
+
/** Like notes(), by the feed's read id: public, read-only, needs no password. */
|
|
62
|
+
readNotes(readId: string, options?: {
|
|
63
|
+
pageSize?: number;
|
|
64
|
+
}): AsyncGenerator<Note>;
|
|
65
|
+
readNote(readId: string, id: string): Promise<Note>;
|
|
66
|
+
private feedFor;
|
|
67
|
+
private opts;
|
|
68
|
+
private pages;
|
|
69
|
+
private call;
|
|
35
70
|
}
|
package/dist/client.js
CHANGED
|
@@ -1,83 +1,157 @@
|
|
|
1
|
-
/**
|
|
2
|
-
|
|
1
|
+
/**
|
|
2
|
+
* The notefeed client: a small convenience layer over the API client generated from the server's
|
|
3
|
+
* OpenAPI description (./generated, `npm run generate` in the repo). No runtime dependencies.
|
|
4
|
+
*/
|
|
5
|
+
import { createClient, createConfig } from "./generated/client/index.js";
|
|
6
|
+
import { getNote, getReadNote, listNotes, listReadNotes, postNote } from "./generated/sdk.gen.js";
|
|
7
|
+
/** Any failure talking to notefeed. `status` and `code` are null when there was no API answer. */
|
|
3
8
|
export class NotefeedError extends Error {
|
|
4
9
|
status;
|
|
5
|
-
|
|
10
|
+
code;
|
|
11
|
+
constructor(message, status = null, code = null) {
|
|
6
12
|
super(message);
|
|
7
13
|
this.status = status;
|
|
14
|
+
this.code = code;
|
|
8
15
|
this.name = new.target.name;
|
|
9
16
|
}
|
|
10
17
|
}
|
|
11
|
-
/** URL or
|
|
18
|
+
/** Raised before sending: no URL, no feed, an invalid feed name or password. */
|
|
12
19
|
export class ConfigError extends NotefeedError {
|
|
13
20
|
}
|
|
14
|
-
/**
|
|
15
|
-
export class InvalidNoteError extends NotefeedError {
|
|
16
|
-
}
|
|
17
|
-
/** Missing or wrong token (401). */
|
|
21
|
+
/** Missing or wrong password on a locked instance. */
|
|
18
22
|
export class AuthError extends NotefeedError {
|
|
19
23
|
}
|
|
20
|
-
/**
|
|
24
|
+
/** Too many posts or wrong passwords. `retryAfter` is the wait in seconds, or null if none was given. */
|
|
25
|
+
export class RateLimitedError extends NotefeedError {
|
|
26
|
+
retryAfter;
|
|
27
|
+
constructor(message, status, code, retryAfter) {
|
|
28
|
+
super(message, status, code);
|
|
29
|
+
this.retryAfter = retryAfter;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
/** No such note, or a malformed read id. */
|
|
33
|
+
export class NotFoundError extends NotefeedError {
|
|
34
|
+
}
|
|
35
|
+
/** The instance's feed or note limit is reached. */
|
|
36
|
+
export class LimitReachedError extends NotefeedError {
|
|
37
|
+
}
|
|
38
|
+
/** The note is over the server's size limit. */
|
|
21
39
|
export class NoteTooLargeError extends NotefeedError {
|
|
22
40
|
}
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
41
|
+
/** The server refused the request itself: invalid or reserved feed, empty note, bad body or parameter. */
|
|
42
|
+
export class InvalidRequestError extends NotefeedError {
|
|
43
|
+
}
|
|
44
|
+
const BY_CODE = {
|
|
45
|
+
auth: AuthError,
|
|
46
|
+
not_found: NotFoundError,
|
|
47
|
+
feed_limit: LimitReachedError,
|
|
48
|
+
note_limit: LimitReachedError,
|
|
49
|
+
too_large: NoteTooLargeError,
|
|
50
|
+
invalid_feed: InvalidRequestError,
|
|
51
|
+
reserved_feed: InvalidRequestError,
|
|
52
|
+
empty_note: InvalidRequestError,
|
|
53
|
+
invalid_body: InvalidRequestError,
|
|
54
|
+
invalid_request: InvalidRequestError,
|
|
55
|
+
unsupported_type: InvalidRequestError,
|
|
28
56
|
};
|
|
57
|
+
const FEED_RE = /^[a-z0-9_-]{1,64}$/; // same rule as the server; reserved names still come back as a 400
|
|
29
58
|
export class Client {
|
|
30
59
|
url;
|
|
31
|
-
|
|
60
|
+
feed;
|
|
61
|
+
api;
|
|
32
62
|
timeoutMs;
|
|
33
|
-
/** A notefeed server.
|
|
63
|
+
/** A notefeed server. The feed set here is the default for every call; each call can override it. */
|
|
34
64
|
constructor(options) {
|
|
35
|
-
const { url,
|
|
65
|
+
const { url, feed, password, timeoutMs } = options ?? {};
|
|
36
66
|
if (!url)
|
|
37
67
|
throw new ConfigError("no url given");
|
|
38
|
-
if (!token?.trim())
|
|
39
|
-
throw new ConfigError("no token given");
|
|
40
68
|
this.url = url.replace(/\/+$/, "");
|
|
41
|
-
this.
|
|
69
|
+
this.feed = feed ? checkFeed(feed) : null;
|
|
70
|
+
const pw = password?.trim() || null;
|
|
42
71
|
// Never echo the value: it would end up in terminals and CI logs.
|
|
43
|
-
if (/[\x00-\x1f\x7f]/.test(
|
|
44
|
-
throw new ConfigError("
|
|
72
|
+
if (pw && /[\x00-\x1f\x7f]/.test(pw))
|
|
73
|
+
throw new ConfigError("password contains invalid characters");
|
|
45
74
|
this.timeoutMs = timeoutMs ?? 10_000;
|
|
75
|
+
this.api = createClient(createConfig({ baseUrl: this.url, headers: pw ? { Authorization: `Bearer ${pw}` } : {} }));
|
|
46
76
|
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
77
|
+
/** NOTEFEED_URL, NOTEFEED_FEED and NOTEFEED_PASSWORD. */
|
|
78
|
+
static fromEnv(env = process.env) {
|
|
79
|
+
return new Client({ url: env.NOTEFEED_URL ?? "", feed: env.NOTEFEED_FEED || undefined, password: env.NOTEFEED_PASSWORD });
|
|
80
|
+
}
|
|
81
|
+
async post(markdown, options = {}) {
|
|
82
|
+
const feed = this.feedFor(options.feed);
|
|
83
|
+
return this.call(postNote({ client: this.api, path: { feed }, body: { markdown }, ...this.opts() }));
|
|
84
|
+
}
|
|
85
|
+
/** Every note in the feed, newest first, fetched a page at a time; stop iterating whenever you like. */
|
|
86
|
+
notes(options = {}) {
|
|
87
|
+
const feed = this.feedFor(options.feed);
|
|
88
|
+
return this.pages((before) => listNotes({ client: this.api, path: { feed }, query: { limit: options.pageSize, before }, ...this.opts() }));
|
|
89
|
+
}
|
|
90
|
+
async note(id, options = {}) {
|
|
91
|
+
const feed = this.feedFor(options.feed);
|
|
92
|
+
return this.call(getNote({ client: this.api, path: { feed, id }, ...this.opts() }));
|
|
93
|
+
}
|
|
94
|
+
/** Like notes(), by the feed's read id: public, read-only, needs no password. */
|
|
95
|
+
readNotes(readId, options = {}) {
|
|
96
|
+
return this.pages((before) => listReadNotes({ client: this.api, path: { readId }, query: { limit: options.pageSize, before }, ...this.opts() }));
|
|
97
|
+
}
|
|
98
|
+
async readNote(readId, id) {
|
|
99
|
+
return this.call(getReadNote({ client: this.api, path: { readId, id }, ...this.opts() }));
|
|
100
|
+
}
|
|
101
|
+
feedFor(feed) {
|
|
102
|
+
const f = feed || this.feed;
|
|
103
|
+
if (!f)
|
|
104
|
+
throw new ConfigError("no feed given");
|
|
105
|
+
return checkFeed(f);
|
|
106
|
+
}
|
|
107
|
+
opts() {
|
|
108
|
+
return { signal: AbortSignal.timeout(this.timeoutMs) };
|
|
109
|
+
}
|
|
110
|
+
// `before` is the previous page's `next`: older notes only, so notes posted meanwhile never repeat.
|
|
111
|
+
async *pages(fetchPage) {
|
|
112
|
+
let before;
|
|
113
|
+
do {
|
|
114
|
+
const page = await this.call(fetchPage(before));
|
|
115
|
+
yield* page.notes;
|
|
116
|
+
before = page.next ?? undefined;
|
|
117
|
+
} while (before);
|
|
118
|
+
}
|
|
119
|
+
// Anything but a JSON object from the API itself is an error, never a success: a POST that was
|
|
120
|
+
// redirected (http → https) came back as a GET of the list, so the note was never stored.
|
|
121
|
+
async call(request) {
|
|
122
|
+
const { data, error, response } = await request;
|
|
123
|
+
if (!response) {
|
|
124
|
+
const cause = error?.cause;
|
|
125
|
+
const reason = cause?.code ?? cause?.message ?? error?.message ?? "unknown error";
|
|
60
126
|
throw new NotefeedError(`could not reach ${this.url}: ${reason}`);
|
|
61
127
|
}
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
}
|
|
70
|
-
catch {
|
|
71
|
-
message = `HTTP ${res.status}: ${text.slice(0, 200).replace(/\s+/g, " ").trim()}`;
|
|
72
|
-
}
|
|
73
|
-
throw new (ERRORS[res.status] ?? NotefeedError)(message, res.status);
|
|
128
|
+
if (response.redirected) {
|
|
129
|
+
throw new NotefeedError(`${this.url} redirected to ${new URL(response.url).origin}: use that address as the URL`, response.status);
|
|
130
|
+
}
|
|
131
|
+
if (response.ok) {
|
|
132
|
+
if (error === undefined && isObject(data))
|
|
133
|
+
return data;
|
|
134
|
+
throw new NotefeedError(`unexpected response from ${this.url} (not a notefeed server?)`, response.status);
|
|
74
135
|
}
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
136
|
+
const body = isObject(error) ? error : null;
|
|
137
|
+
const code = typeof body?.code === "string" ? body.code : null;
|
|
138
|
+
let message = typeof body?.error === "string" ? body.error : "";
|
|
139
|
+
if (!message) {
|
|
140
|
+
const raw = typeof error === "string" ? error : isObject(error) && Object.keys(error).length ? JSON.stringify(error) : "";
|
|
141
|
+
message = `HTTP ${response.status}: ${raw.slice(0, 200).replace(/\s+/g, " ").trim()}`.replace(/: $/, "");
|
|
79
142
|
}
|
|
80
|
-
|
|
81
|
-
|
|
143
|
+
if (code === "rate_limited" || code === "too_many_attempts" || response.status === 429) {
|
|
144
|
+
const retry = response.headers.get("retry-after")?.trim() ?? "";
|
|
145
|
+
throw new RateLimitedError(message, response.status, code, /^\d+$/.test(retry) ? Number(retry) : null);
|
|
146
|
+
}
|
|
147
|
+
const Cls = (code && BY_CODE[code]) || NotefeedError;
|
|
148
|
+
throw new Cls(message, response.status, code);
|
|
82
149
|
}
|
|
83
150
|
}
|
|
151
|
+
const isObject = (v) => typeof v === "object" && v !== null && !Array.isArray(v);
|
|
152
|
+
function checkFeed(feed) {
|
|
153
|
+
// Never echo the name: it is the write key.
|
|
154
|
+
if (!FEED_RE.test(feed))
|
|
155
|
+
throw new ConfigError("invalid feed name: use 1-64 of a-z, 0-9, _ and -");
|
|
156
|
+
return feed;
|
|
157
|
+
}
|
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
// This file is auto-generated by @hey-api/openapi-ts
|
|
2
|
+
import { createSseClient } from '../core/serverSentEvents.gen.js';
|
|
3
|
+
import { getValidRequestBody } from '../core/utils.gen.js';
|
|
4
|
+
import { buildUrl, createConfig, createInterceptors, getParseAs, mergeConfigs, mergeHeaders, setAuthParams, } from './utils.gen.js';
|
|
5
|
+
export const createClient = (config = {}) => {
|
|
6
|
+
let _config = mergeConfigs(createConfig(), config);
|
|
7
|
+
const getConfig = () => ({ ..._config });
|
|
8
|
+
const setConfig = (config) => {
|
|
9
|
+
_config = mergeConfigs(_config, config);
|
|
10
|
+
return getConfig();
|
|
11
|
+
};
|
|
12
|
+
const interceptors = createInterceptors();
|
|
13
|
+
const beforeRequest = async (options) => {
|
|
14
|
+
const opts = {
|
|
15
|
+
..._config,
|
|
16
|
+
...options,
|
|
17
|
+
fetch: options.fetch ?? _config.fetch ?? globalThis.fetch,
|
|
18
|
+
headers: mergeHeaders(_config.headers, options.headers),
|
|
19
|
+
serializedBody: undefined,
|
|
20
|
+
};
|
|
21
|
+
if (opts.security) {
|
|
22
|
+
await setAuthParams(opts);
|
|
23
|
+
}
|
|
24
|
+
if (opts.requestValidator) {
|
|
25
|
+
await opts.requestValidator(opts);
|
|
26
|
+
}
|
|
27
|
+
if (opts.body !== undefined && opts.bodySerializer) {
|
|
28
|
+
opts.serializedBody = opts.bodySerializer(opts.body);
|
|
29
|
+
}
|
|
30
|
+
// remove Content-Type header if body is empty to avoid sending invalid requests
|
|
31
|
+
if (opts.body === undefined || opts.serializedBody === '') {
|
|
32
|
+
opts.headers.delete('Content-Type');
|
|
33
|
+
}
|
|
34
|
+
const resolvedOpts = opts;
|
|
35
|
+
const url = buildUrl(resolvedOpts);
|
|
36
|
+
return { opts: resolvedOpts, url };
|
|
37
|
+
};
|
|
38
|
+
const request = async (options) => {
|
|
39
|
+
const throwOnError = options.throwOnError ?? _config.throwOnError;
|
|
40
|
+
const responseStyle = options.responseStyle ?? _config.responseStyle;
|
|
41
|
+
let request;
|
|
42
|
+
let response;
|
|
43
|
+
try {
|
|
44
|
+
const { opts, url } = await beforeRequest(options);
|
|
45
|
+
const requestInit = {
|
|
46
|
+
redirect: 'follow',
|
|
47
|
+
...opts,
|
|
48
|
+
body: getValidRequestBody(opts),
|
|
49
|
+
};
|
|
50
|
+
request = new Request(url, requestInit);
|
|
51
|
+
for (const fn of interceptors.request.fns) {
|
|
52
|
+
if (fn) {
|
|
53
|
+
request = await fn(request, opts);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
// fetch must be assigned here, otherwise it would throw the error:
|
|
57
|
+
// TypeError: Failed to execute 'fetch' on 'Window': Illegal invocation
|
|
58
|
+
const _fetch = opts.fetch;
|
|
59
|
+
response = await _fetch(request);
|
|
60
|
+
for (const fn of interceptors.response.fns) {
|
|
61
|
+
if (fn) {
|
|
62
|
+
response = await fn(response, request, opts);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
const result = {
|
|
66
|
+
request,
|
|
67
|
+
response,
|
|
68
|
+
};
|
|
69
|
+
if (response.ok) {
|
|
70
|
+
const parseAs = (opts.parseAs === 'auto'
|
|
71
|
+
? getParseAs(response.headers.get('Content-Type'))
|
|
72
|
+
: opts.parseAs) ?? 'json';
|
|
73
|
+
if (response.status === 204 || response.headers.get('Content-Length') === '0') {
|
|
74
|
+
let emptyData;
|
|
75
|
+
switch (parseAs) {
|
|
76
|
+
case 'arrayBuffer':
|
|
77
|
+
case 'blob':
|
|
78
|
+
case 'text':
|
|
79
|
+
emptyData = await response[parseAs]();
|
|
80
|
+
break;
|
|
81
|
+
case 'formData':
|
|
82
|
+
emptyData = new FormData();
|
|
83
|
+
break;
|
|
84
|
+
case 'stream':
|
|
85
|
+
emptyData = response.body;
|
|
86
|
+
break;
|
|
87
|
+
case 'json':
|
|
88
|
+
default:
|
|
89
|
+
emptyData = {};
|
|
90
|
+
break;
|
|
91
|
+
}
|
|
92
|
+
return opts.responseStyle === 'data'
|
|
93
|
+
? emptyData
|
|
94
|
+
: {
|
|
95
|
+
data: emptyData,
|
|
96
|
+
...result,
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
let data;
|
|
100
|
+
switch (parseAs) {
|
|
101
|
+
case 'arrayBuffer':
|
|
102
|
+
case 'blob':
|
|
103
|
+
case 'formData':
|
|
104
|
+
case 'text':
|
|
105
|
+
data = await response[parseAs]();
|
|
106
|
+
break;
|
|
107
|
+
case 'json': {
|
|
108
|
+
// Some servers return 200 with no Content-Length and empty body.
|
|
109
|
+
// response.json() would throw; read as text and parse if non-empty.
|
|
110
|
+
const text = await response.text();
|
|
111
|
+
data = text ? JSON.parse(text) : {};
|
|
112
|
+
break;
|
|
113
|
+
}
|
|
114
|
+
case 'stream':
|
|
115
|
+
return opts.responseStyle === 'data'
|
|
116
|
+
? response.body
|
|
117
|
+
: {
|
|
118
|
+
data: response.body,
|
|
119
|
+
...result,
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
if (parseAs === 'json') {
|
|
123
|
+
if (opts.responseValidator) {
|
|
124
|
+
await opts.responseValidator(data);
|
|
125
|
+
}
|
|
126
|
+
if (opts.responseTransformer) {
|
|
127
|
+
data = await opts.responseTransformer(data);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
return opts.responseStyle === 'data'
|
|
131
|
+
? data
|
|
132
|
+
: {
|
|
133
|
+
data,
|
|
134
|
+
...result,
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
const textError = await response.text();
|
|
138
|
+
let jsonError;
|
|
139
|
+
try {
|
|
140
|
+
jsonError = JSON.parse(textError);
|
|
141
|
+
}
|
|
142
|
+
catch {
|
|
143
|
+
// noop
|
|
144
|
+
}
|
|
145
|
+
throw jsonError ?? textError;
|
|
146
|
+
}
|
|
147
|
+
catch (error) {
|
|
148
|
+
let finalError = error;
|
|
149
|
+
for (const fn of interceptors.error.fns) {
|
|
150
|
+
if (fn) {
|
|
151
|
+
finalError = await fn(finalError, response, request, options);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
finalError = finalError || {};
|
|
155
|
+
if (throwOnError) {
|
|
156
|
+
throw finalError;
|
|
157
|
+
}
|
|
158
|
+
// TODO: we probably want to return error and improve types
|
|
159
|
+
return responseStyle === 'data'
|
|
160
|
+
? undefined
|
|
161
|
+
: {
|
|
162
|
+
error: finalError,
|
|
163
|
+
request,
|
|
164
|
+
response,
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
};
|
|
168
|
+
const makeMethodFn = (method) => (options) => request({ ...options, method });
|
|
169
|
+
const makeSseFn = (method) => async (options) => {
|
|
170
|
+
const { opts, url } = await beforeRequest(options);
|
|
171
|
+
return createSseClient({
|
|
172
|
+
...opts,
|
|
173
|
+
body: opts.body,
|
|
174
|
+
method,
|
|
175
|
+
onRequest: async (url, init) => {
|
|
176
|
+
let request = new Request(url, init);
|
|
177
|
+
for (const fn of interceptors.request.fns) {
|
|
178
|
+
if (fn) {
|
|
179
|
+
request = await fn(request, opts);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
return request;
|
|
183
|
+
},
|
|
184
|
+
serializedBody: getValidRequestBody(opts),
|
|
185
|
+
url,
|
|
186
|
+
});
|
|
187
|
+
};
|
|
188
|
+
const _buildUrl = (options) => buildUrl({ ..._config, ...options });
|
|
189
|
+
return {
|
|
190
|
+
buildUrl: _buildUrl,
|
|
191
|
+
connect: makeMethodFn('CONNECT'),
|
|
192
|
+
delete: makeMethodFn('DELETE'),
|
|
193
|
+
get: makeMethodFn('GET'),
|
|
194
|
+
getConfig,
|
|
195
|
+
head: makeMethodFn('HEAD'),
|
|
196
|
+
interceptors,
|
|
197
|
+
options: makeMethodFn('OPTIONS'),
|
|
198
|
+
patch: makeMethodFn('PATCH'),
|
|
199
|
+
post: makeMethodFn('POST'),
|
|
200
|
+
put: makeMethodFn('PUT'),
|
|
201
|
+
request,
|
|
202
|
+
setConfig,
|
|
203
|
+
sse: {
|
|
204
|
+
connect: makeSseFn('CONNECT'),
|
|
205
|
+
delete: makeSseFn('DELETE'),
|
|
206
|
+
get: makeSseFn('GET'),
|
|
207
|
+
head: makeSseFn('HEAD'),
|
|
208
|
+
options: makeSseFn('OPTIONS'),
|
|
209
|
+
patch: makeSseFn('PATCH'),
|
|
210
|
+
post: makeSseFn('POST'),
|
|
211
|
+
put: makeSseFn('PUT'),
|
|
212
|
+
trace: makeSseFn('TRACE'),
|
|
213
|
+
},
|
|
214
|
+
trace: makeMethodFn('TRACE'),
|
|
215
|
+
};
|
|
216
|
+
};
|