@osrsclans/api 0.0.0-stage → 1.15.1
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/LICENSE +21 -0
- package/README.md +165 -2
- package/dist/answered.d.ts +17 -0
- package/dist/answered.js +52 -0
- package/dist/api-error.d.ts +13 -0
- package/dist/api-error.js +18 -0
- package/dist/chains.d.ts +37 -0
- package/dist/chains.js +30 -0
- package/dist/client.d.ts +24 -0
- package/dist/client.js +25 -0
- package/dist/data-request-error.d.ts +9 -0
- package/dist/data-request-error.js +12 -0
- package/dist/data-requests.d.ts +6 -0
- package/dist/data-requests.js +16 -0
- package/dist/destinations.d.ts +23 -0
- package/dist/destinations.js +74 -0
- package/dist/errors.generated.d.ts +775 -0
- package/dist/errors.generated.js +1034 -0
- package/dist/index.generated.d.ts +7 -0
- package/dist/index.generated.js +4 -0
- package/dist/models.generated.d.ts +351 -0
- package/dist/models.generated.js +1 -0
- package/dist/operation-types.d.ts +23 -0
- package/dist/operation-types.js +1 -0
- package/dist/operations.generated.d.ts +5693 -0
- package/dist/operations.generated.js +362 -0
- package/dist/outcomes.generated.d.ts +21 -0
- package/dist/outcomes.generated.js +26 -0
- package/dist/public.d.ts +7 -0
- package/dist/public.js +7 -0
- package/dist/schema.generated.d.ts +32193 -0
- package/dist/schema.generated.js +1136 -0
- package/dist/streams.d.ts +29 -0
- package/dist/streams.js +95 -0
- package/dist/unexpected-status.d.ts +5 -0
- package/dist/unexpected-status.js +9 -0
- package/dist/walk.d.ts +13 -0
- package/dist/walk.js +24 -0
- package/dist/walkers.generated.d.ts +702 -0
- package/dist/walkers.generated.js +106 -0
- package/package.json +51 -5
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { ClanSocketEvent } from "./schema.generated.js";
|
|
2
|
+
import type { ClanSocketEventFrame } from "./models.generated.js";
|
|
3
|
+
export declare const STREAM_PATH = "/api/v1/clans/{slug}/clansocket/stream";
|
|
4
|
+
/** Opens a WebSocket that sends `headers` with its upgrade request. */
|
|
5
|
+
export type WebSocketFactory = (url: string, headers: Record<string, string>) => WebSocket;
|
|
6
|
+
/** The default socket: the global `WebSocket` with a `headers` option, which Bun reads. */
|
|
7
|
+
export declare const headeredWebSocket: WebSocketFactory;
|
|
8
|
+
/** The service closed the stream. `code` and `reason` are the WebSocket close code and reason. */
|
|
9
|
+
export declare class StreamClosed extends Error {
|
|
10
|
+
readonly code: number;
|
|
11
|
+
readonly reason: string;
|
|
12
|
+
constructor(code: number, reason: string);
|
|
13
|
+
}
|
|
14
|
+
export interface StreamOptions {
|
|
15
|
+
/** The event types to take. Every type when left out. */
|
|
16
|
+
events?: ClanSocketEvent[];
|
|
17
|
+
/** Ends the stream. */
|
|
18
|
+
signal?: AbortSignal;
|
|
19
|
+
}
|
|
20
|
+
/** The address of one clan's stream, as a `ws:` or `wss:` URL. */
|
|
21
|
+
export declare const streamUrl: (baseUrl: string, slug: string) => string;
|
|
22
|
+
/** The event one stream message carries, or `null` for a message that carries no event. */
|
|
23
|
+
export declare const eventFrame: (text: string) => ClanSocketEventFrame | null;
|
|
24
|
+
/**
|
|
25
|
+
* One clan's live stream of ClanSocket events. Each event arrives in the order the service sent it.
|
|
26
|
+
* The iteration ends when the service closes the stream, and a close other than a normal one
|
|
27
|
+
* throws `StreamClosed`. Leaving the loop closes the socket.
|
|
28
|
+
*/
|
|
29
|
+
export declare const streamEvents: (open: () => WebSocket, options?: StreamOptions) => AsyncIterableIterator<ClanSocketEventFrame, null>;
|
package/dist/streams.js
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
export const STREAM_PATH = "/api/v1/clans/{slug}/clansocket/stream";
|
|
2
|
+
const NORMAL_CLOSE = 1000;
|
|
3
|
+
const FINISHED = { done: true, value: null };
|
|
4
|
+
/** The default socket: the global `WebSocket` with a `headers` option, which Bun reads. */
|
|
5
|
+
export const headeredWebSocket = (url, headers) => {
|
|
6
|
+
const socket = Reflect.construct(WebSocket, [url, { headers }]);
|
|
7
|
+
if (!(socket instanceof WebSocket)) {
|
|
8
|
+
throw new TypeError("the global WebSocket made no socket");
|
|
9
|
+
}
|
|
10
|
+
return socket;
|
|
11
|
+
};
|
|
12
|
+
/** The service closed the stream. `code` and `reason` are the WebSocket close code and reason. */
|
|
13
|
+
export class StreamClosed extends Error {
|
|
14
|
+
code;
|
|
15
|
+
reason;
|
|
16
|
+
constructor(code, reason) {
|
|
17
|
+
super(`the stream closed with ${code}: ${reason}`);
|
|
18
|
+
this.name = "StreamClosed";
|
|
19
|
+
this.code = code;
|
|
20
|
+
this.reason = reason;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
/** The address of one clan's stream, as a `ws:` or `wss:` URL. */
|
|
24
|
+
export const streamUrl = function streamUrl(baseUrl, slug) {
|
|
25
|
+
const url = new URL(STREAM_PATH.replace("{slug}", encodeURIComponent(slug)), baseUrl);
|
|
26
|
+
url.protocol = url.protocol === "http:" ? "ws:" : "wss:";
|
|
27
|
+
return url.toString();
|
|
28
|
+
};
|
|
29
|
+
const isEventFrame = function isEventFrame(value) {
|
|
30
|
+
return typeof value["event"] === "string" && typeof value["data"] === "object" && value["data"] !== null;
|
|
31
|
+
};
|
|
32
|
+
/** The event one stream message carries, or `null` for a message that carries no event. */
|
|
33
|
+
export const eventFrame = function eventFrame(text) {
|
|
34
|
+
const parsed = JSON.parse(text);
|
|
35
|
+
if (typeof parsed !== "object" || parsed === null || Reflect.get(parsed, "type") !== "event") {
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
const frame = Object.fromEntries(Object.entries(parsed).filter(([key]) => key !== "type"));
|
|
39
|
+
return isEventFrame(frame) ? frame : null;
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* One clan's live stream of ClanSocket events. Each event arrives in the order the service sent it.
|
|
43
|
+
* The iteration ends when the service closes the stream, and a close other than a normal one
|
|
44
|
+
* throws `StreamClosed`. Leaving the loop closes the socket.
|
|
45
|
+
*/
|
|
46
|
+
export const streamEvents = function streamEvents(open, options = {}) {
|
|
47
|
+
const socket = open();
|
|
48
|
+
const state = { arrived: [], closed: null, waiting: null };
|
|
49
|
+
const wake = () => {
|
|
50
|
+
const resolve = state.waiting;
|
|
51
|
+
state.waiting = null;
|
|
52
|
+
resolve?.();
|
|
53
|
+
};
|
|
54
|
+
socket.addEventListener("message", (message) => {
|
|
55
|
+
state.arrived.push(String(message.data));
|
|
56
|
+
wake();
|
|
57
|
+
});
|
|
58
|
+
socket.addEventListener("close", (close) => {
|
|
59
|
+
state.closed = close;
|
|
60
|
+
wake();
|
|
61
|
+
});
|
|
62
|
+
socket.addEventListener("open", () => {
|
|
63
|
+
if (options.events !== undefined && options.events.length > 0) {
|
|
64
|
+
socket.send(JSON.stringify({ events: options.events, type: "subscribe" }));
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
options.signal?.addEventListener("abort", () => { socket.close(); });
|
|
68
|
+
const ended = (close) => {
|
|
69
|
+
if (close.code !== NORMAL_CLOSE && options.signal?.aborted !== true) {
|
|
70
|
+
throw new StreamClosed(close.code, close.reason);
|
|
71
|
+
}
|
|
72
|
+
return FINISHED;
|
|
73
|
+
};
|
|
74
|
+
const next = async () => {
|
|
75
|
+
const text = state.arrived.shift();
|
|
76
|
+
if (text !== undefined) {
|
|
77
|
+
const frame = eventFrame(text);
|
|
78
|
+
return frame === null ? next() : { done: false, value: frame };
|
|
79
|
+
}
|
|
80
|
+
if (state.closed !== null) {
|
|
81
|
+
return ended(state.closed);
|
|
82
|
+
}
|
|
83
|
+
await new Promise((resolve) => {
|
|
84
|
+
state.waiting = resolve;
|
|
85
|
+
});
|
|
86
|
+
return next();
|
|
87
|
+
};
|
|
88
|
+
const leave = async () => {
|
|
89
|
+
if (state.closed === null) {
|
|
90
|
+
socket.close();
|
|
91
|
+
}
|
|
92
|
+
return Promise.resolve(FINISHED);
|
|
93
|
+
};
|
|
94
|
+
return { next, return: leave, [Symbol.asyncIterator]() { return this; } };
|
|
95
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/** The API answered with a status below 400 that the route does not document, or a success with no body where the route documents one. */
|
|
2
|
+
export class UnexpectedStatus extends Error {
|
|
3
|
+
status;
|
|
4
|
+
constructor(status) {
|
|
5
|
+
super(`Unexpected status code: ${status}`);
|
|
6
|
+
this.name = "UnexpectedStatus";
|
|
7
|
+
this.status = status;
|
|
8
|
+
}
|
|
9
|
+
}
|
package/dist/walk.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/** One page a walker reads: its rows, the route's total, and the fields the next request takes from it. */
|
|
2
|
+
export interface WalkedPage<Row, Carried extends object> {
|
|
3
|
+
rows: Row[];
|
|
4
|
+
total: number;
|
|
5
|
+
carried: Carried;
|
|
6
|
+
}
|
|
7
|
+
/** Reads one page at an offset, with the fields the page before it handed on. */
|
|
8
|
+
export type PageRead<Row, Carried extends object> = (offset: number, carried: Partial<Carried>) => Promise<WalkedPage<Row, Carried>>;
|
|
9
|
+
/**
|
|
10
|
+
* Every row of a paged route, page by page from the first, read as the rows are taken. It reads the
|
|
11
|
+
* next page once the rows of the last are taken, and ends at an empty page or at the route's total.
|
|
12
|
+
*/
|
|
13
|
+
export declare const walk: <Row, Carried extends object>(read: PageRead<Row, Carried>) => AsyncIterableIterator<Row, null>;
|
package/dist/walk.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
const FINISHED = { done: true, value: null };
|
|
2
|
+
/**
|
|
3
|
+
* Every row of a paged route, page by page from the first, read as the rows are taken. It reads the
|
|
4
|
+
* next page once the rows of the last are taken, and ends at an empty page or at the route's total.
|
|
5
|
+
*/
|
|
6
|
+
export const walk = function walk(read) {
|
|
7
|
+
const state = { carried: {}, ended: false, offset: 0, rows: [].values() };
|
|
8
|
+
const next = async () => {
|
|
9
|
+
const taken = state.rows.next();
|
|
10
|
+
if (taken.done !== true) {
|
|
11
|
+
return { done: false, value: taken.value };
|
|
12
|
+
}
|
|
13
|
+
if (state.ended) {
|
|
14
|
+
return FINISHED;
|
|
15
|
+
}
|
|
16
|
+
const page = await read(state.offset, state.carried);
|
|
17
|
+
state.offset += page.rows.length;
|
|
18
|
+
state.ended = page.rows.length === 0 || state.offset >= page.total;
|
|
19
|
+
state.carried = page.carried;
|
|
20
|
+
state.rows = page.rows.values();
|
|
21
|
+
return page.rows.length > 0 ? next() : FINISHED;
|
|
22
|
+
};
|
|
23
|
+
return { next, [Symbol.asyncIterator]() { return this; } };
|
|
24
|
+
};
|