@phuzle/relay 0.1.0 → 0.1.2
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 +26 -0
- package/cli.js +741 -32
- package/index.cjs +579 -25
- package/index.d.cts +67 -0
- package/index.d.ts +67 -0
- package/index.js +577 -25
- package/package.json +1 -1
package/index.d.cts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { type SubscribeOptions, Subscription } from "./stream.ts";
|
|
2
|
+
export { type StreamCondition, StreamError, type StreamEvent, type StreamFilter, type StreamGap, type StreamState, type SubscribeOptions, Subscription, } from "./stream.ts";
|
|
1
3
|
/** Thrown when the plaintext message exceeds 2 KiB. */
|
|
2
4
|
export declare class PayloadTooLargeError extends Error {
|
|
3
5
|
constructor(size: number);
|
|
@@ -106,6 +108,47 @@ export type RelayOptions = {
|
|
|
106
108
|
fetch?: typeof fetch;
|
|
107
109
|
maxRetries?: number;
|
|
108
110
|
};
|
|
111
|
+
/** A topic this API key can send to. `topic` is what you pass to `send()`. */
|
|
112
|
+
export type Topic = {
|
|
113
|
+
id: string;
|
|
114
|
+
slug: string;
|
|
115
|
+
/** `<org>-<slug>`: the value for `send({ topic })` */
|
|
116
|
+
fullName: string;
|
|
117
|
+
name: string;
|
|
118
|
+
description: string | null;
|
|
119
|
+
/** Phones receiving it now */
|
|
120
|
+
devices: number;
|
|
121
|
+
createdAt: string;
|
|
122
|
+
};
|
|
123
|
+
export type TopicList = {
|
|
124
|
+
topics: Topic[];
|
|
125
|
+
/** The key may create topics: it has “Can create topics” (and access to all topics), set in the dashboard. */
|
|
126
|
+
canCreate: boolean;
|
|
127
|
+
};
|
|
128
|
+
export type CreateTopic = {
|
|
129
|
+
/** 2–32 lowercase letters, numbers and single hyphens; becomes `<org>-<slug>` */
|
|
130
|
+
slug: string;
|
|
131
|
+
name: string;
|
|
132
|
+
description?: string;
|
|
133
|
+
};
|
|
134
|
+
/** A phone receiving a topic. Metadata only; never keys or tokens. */
|
|
135
|
+
export type TopicDevice = {
|
|
136
|
+
id: string;
|
|
137
|
+
nickname: string | null;
|
|
138
|
+
name: string | null;
|
|
139
|
+
model: string | null;
|
|
140
|
+
platform: string;
|
|
141
|
+
appVersion: string | null;
|
|
142
|
+
lastSeenAt: string | null;
|
|
143
|
+
subscribedAt: string;
|
|
144
|
+
};
|
|
145
|
+
/** A topic with its name filled in (see `Relay.topic()`). */
|
|
146
|
+
export type TopicHandle = {
|
|
147
|
+
name: string;
|
|
148
|
+
send(m: Omit<Message, "topic">): Promise<SendResult>;
|
|
149
|
+
devices(): Promise<TopicDevice[]>;
|
|
150
|
+
subscribe(o: SubscribeOptions): Subscription;
|
|
151
|
+
};
|
|
109
152
|
export declare class Relay {
|
|
110
153
|
private readonly opts;
|
|
111
154
|
private readonly base;
|
|
@@ -117,6 +160,30 @@ export declare class Relay {
|
|
|
117
160
|
constructor(opts: RelayOptions);
|
|
118
161
|
/** Reads `RELAY_API_KEY` and, optionally, `RELAY_API_URL`; `overrides` win over the environment. */
|
|
119
162
|
static fromEnv(env?: Record<string, string | undefined>, overrides?: Partial<RelayOptions>): Relay;
|
|
163
|
+
/** A new consumer key for `subscribe()`: 43 characters, shown once. Store it as `RELAY_CONSUMER_KEY`; Relay never sees it and cannot recover it. */
|
|
164
|
+
static generateConsumerKey(): string;
|
|
165
|
+
/** One topic by its full name (`<org>-<slug>`): send to it, list its phones, or subscribe to its events from a server. */
|
|
166
|
+
topic(name: string): TopicHandle;
|
|
167
|
+
/**
|
|
168
|
+
* Receive a topic's events on a server (Relay Streams): webhooks Relay received for this topic, delivered over one long-lived
|
|
169
|
+
* connection, sealed to your consumer key so only this process can open them. Needs an API key with “Can receive events” for the
|
|
170
|
+
* topic. Reconnects by itself, and **at-least-once**: events wait for you (for your plan's retention) until acknowledged, so a
|
|
171
|
+
* restart loses nothing; repeats are possible, so use `ev.id` to ignore ones you have handled.
|
|
172
|
+
*
|
|
173
|
+
* const sub = relay.topic("acme-github-api").subscribe({ name: "deploy-bot" }); // key from RELAY_CONSUMER_KEY
|
|
174
|
+
* for await (const ev of sub) { if (ev.type === "push") deploy(ev.json()); }
|
|
175
|
+
*/
|
|
176
|
+
subscribe(topic: string, o: SubscribeOptions): Subscription;
|
|
177
|
+
/** The topics this key may send to (its `trigger:*` / `trigger:<slug>` permissions), and whether it may create more. */
|
|
178
|
+
topics(): Promise<TopicList>;
|
|
179
|
+
/**
|
|
180
|
+
* Create a topic. Needs the **Can create topics** permission on the key (`scope_denied` otherwise) and counts against the plan's
|
|
181
|
+
* topic limit (`topic_limit`). Only keys that can send to all topics can have this permission, so the new topic is usable straight away. Not retried automatically:
|
|
182
|
+
* a second attempt would only answer `slug_taken`.
|
|
183
|
+
*/
|
|
184
|
+
createTopic(t: CreateTopic): Promise<Topic>;
|
|
185
|
+
/** The phones receiving `topic` (the full `<org>-<slug>` name). The key must be allowed to send to it. */
|
|
186
|
+
devices(topic: string): Promise<TopicDevice[]>;
|
|
120
187
|
send(m: Message): Promise<SendResult>;
|
|
121
188
|
private sendCore;
|
|
122
189
|
private sendOnce;
|
package/index.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { type SubscribeOptions, Subscription } from "./stream.ts";
|
|
2
|
+
export { type StreamCondition, StreamError, type StreamEvent, type StreamFilter, type StreamGap, type StreamState, type SubscribeOptions, Subscription, } from "./stream.ts";
|
|
1
3
|
/** Thrown when the plaintext message exceeds 2 KiB. */
|
|
2
4
|
export declare class PayloadTooLargeError extends Error {
|
|
3
5
|
constructor(size: number);
|
|
@@ -106,6 +108,47 @@ export type RelayOptions = {
|
|
|
106
108
|
fetch?: typeof fetch;
|
|
107
109
|
maxRetries?: number;
|
|
108
110
|
};
|
|
111
|
+
/** A topic this API key can send to. `topic` is what you pass to `send()`. */
|
|
112
|
+
export type Topic = {
|
|
113
|
+
id: string;
|
|
114
|
+
slug: string;
|
|
115
|
+
/** `<org>-<slug>`: the value for `send({ topic })` */
|
|
116
|
+
fullName: string;
|
|
117
|
+
name: string;
|
|
118
|
+
description: string | null;
|
|
119
|
+
/** Phones receiving it now */
|
|
120
|
+
devices: number;
|
|
121
|
+
createdAt: string;
|
|
122
|
+
};
|
|
123
|
+
export type TopicList = {
|
|
124
|
+
topics: Topic[];
|
|
125
|
+
/** The key may create topics: it has “Can create topics” (and access to all topics), set in the dashboard. */
|
|
126
|
+
canCreate: boolean;
|
|
127
|
+
};
|
|
128
|
+
export type CreateTopic = {
|
|
129
|
+
/** 2–32 lowercase letters, numbers and single hyphens; becomes `<org>-<slug>` */
|
|
130
|
+
slug: string;
|
|
131
|
+
name: string;
|
|
132
|
+
description?: string;
|
|
133
|
+
};
|
|
134
|
+
/** A phone receiving a topic. Metadata only; never keys or tokens. */
|
|
135
|
+
export type TopicDevice = {
|
|
136
|
+
id: string;
|
|
137
|
+
nickname: string | null;
|
|
138
|
+
name: string | null;
|
|
139
|
+
model: string | null;
|
|
140
|
+
platform: string;
|
|
141
|
+
appVersion: string | null;
|
|
142
|
+
lastSeenAt: string | null;
|
|
143
|
+
subscribedAt: string;
|
|
144
|
+
};
|
|
145
|
+
/** A topic with its name filled in (see `Relay.topic()`). */
|
|
146
|
+
export type TopicHandle = {
|
|
147
|
+
name: string;
|
|
148
|
+
send(m: Omit<Message, "topic">): Promise<SendResult>;
|
|
149
|
+
devices(): Promise<TopicDevice[]>;
|
|
150
|
+
subscribe(o: SubscribeOptions): Subscription;
|
|
151
|
+
};
|
|
109
152
|
export declare class Relay {
|
|
110
153
|
private readonly opts;
|
|
111
154
|
private readonly base;
|
|
@@ -117,6 +160,30 @@ export declare class Relay {
|
|
|
117
160
|
constructor(opts: RelayOptions);
|
|
118
161
|
/** Reads `RELAY_API_KEY` and, optionally, `RELAY_API_URL`; `overrides` win over the environment. */
|
|
119
162
|
static fromEnv(env?: Record<string, string | undefined>, overrides?: Partial<RelayOptions>): Relay;
|
|
163
|
+
/** A new consumer key for `subscribe()`: 43 characters, shown once. Store it as `RELAY_CONSUMER_KEY`; Relay never sees it and cannot recover it. */
|
|
164
|
+
static generateConsumerKey(): string;
|
|
165
|
+
/** One topic by its full name (`<org>-<slug>`): send to it, list its phones, or subscribe to its events from a server. */
|
|
166
|
+
topic(name: string): TopicHandle;
|
|
167
|
+
/**
|
|
168
|
+
* Receive a topic's events on a server (Relay Streams): webhooks Relay received for this topic, delivered over one long-lived
|
|
169
|
+
* connection, sealed to your consumer key so only this process can open them. Needs an API key with “Can receive events” for the
|
|
170
|
+
* topic. Reconnects by itself, and **at-least-once**: events wait for you (for your plan's retention) until acknowledged, so a
|
|
171
|
+
* restart loses nothing; repeats are possible, so use `ev.id` to ignore ones you have handled.
|
|
172
|
+
*
|
|
173
|
+
* const sub = relay.topic("acme-github-api").subscribe({ name: "deploy-bot" }); // key from RELAY_CONSUMER_KEY
|
|
174
|
+
* for await (const ev of sub) { if (ev.type === "push") deploy(ev.json()); }
|
|
175
|
+
*/
|
|
176
|
+
subscribe(topic: string, o: SubscribeOptions): Subscription;
|
|
177
|
+
/** The topics this key may send to (its `trigger:*` / `trigger:<slug>` permissions), and whether it may create more. */
|
|
178
|
+
topics(): Promise<TopicList>;
|
|
179
|
+
/**
|
|
180
|
+
* Create a topic. Needs the **Can create topics** permission on the key (`scope_denied` otherwise) and counts against the plan's
|
|
181
|
+
* topic limit (`topic_limit`). Only keys that can send to all topics can have this permission, so the new topic is usable straight away. Not retried automatically:
|
|
182
|
+
* a second attempt would only answer `slug_taken`.
|
|
183
|
+
*/
|
|
184
|
+
createTopic(t: CreateTopic): Promise<Topic>;
|
|
185
|
+
/** The phones receiving `topic` (the full `<org>-<slug>` name). The key must be allowed to send to it. */
|
|
186
|
+
devices(topic: string): Promise<TopicDevice[]>;
|
|
120
187
|
send(m: Message): Promise<SendResult>;
|
|
121
188
|
private sendCore;
|
|
122
189
|
private sendOnce;
|