@sealevel-dev/sdk
Version:
TypeScript SDK for Sealevel.
310 lines (298 loc) • 9.71 kB
JavaScript
"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var src_exports = {};
__export(src_exports, {
RealtimeV1Client: () => RealtimeV1Client,
RealtimeV1CloseCode: () => RealtimeV1CloseCode,
RealtimeV2Client: () => RealtimeV2Client,
RealtimeV2CloseCode: () => RealtimeV2CloseCode,
createRealtimeV1Client: () => createRealtimeV1Client,
createRealtimeV2Client: () => createRealtimeV2Client,
createV1ApiClient: () => createV1ApiClient
});
module.exports = __toCommonJS(src_exports);
// src/api/v1/client.ts
var import_openapi_fetch = __toESM(require("openapi-fetch"), 1);
// src/api/const.ts
var API_DEFAULT_BASE_URL = "https://api.sealevel.dev";
// src/api/v1/client.ts
function createV1ApiClient(options) {
const baseUrl = `${options?.baseUrl ?? API_DEFAULT_BASE_URL}/v1`;
const client = (0, import_openapi_fetch.default)({
baseUrl,
headers: {
Authorization: `Bearer ${options.accessToken}`
}
});
return client;
}
// src/realtime/v1/client.ts
var import_eventemitter3 = __toESM(require("eventemitter3"), 1);
var import_ws = require("ws");
var import_zod = require("zod");
// src/realtime/v1/const.ts
var REALTIME_V1_DEFAULT_URL = "wss://realtime.sealevel.dev/v1";
// src/realtime/v1/client.ts
var realtimeClientOptionsSchema = import_zod.z.object({
url: import_zod.z.string().default(REALTIME_V1_DEFAULT_URL),
accessToken: import_zod.z.string(),
reconnect: import_zod.z.boolean().default(true),
reconnectInterval: import_zod.z.number().default(1e3)
});
var RealtimeV1Client = class extends import_eventemitter3.default {
#options;
#userDisconnected = false;
#ws = null;
constructor(options) {
super();
this.#options = realtimeClientOptionsSchema.parse(options ?? {});
}
connect = () => {
if (this.#ws) {
throw new Error("connect: websocket already intialized");
}
const url = new URL(this.#options.url);
url.searchParams.set("access_token", this.#options.accessToken);
this.#userDisconnected = false;
this.#ws = new import_ws.WebSocket(url);
this.#ws.addEventListener("message", this.#handleMessage);
this.#ws.addEventListener("error", this.#handleError);
this.#ws.addEventListener("close", this.#handleClose);
this.#ws.addEventListener("open", this.#handleOpen);
};
send = (message) => {
if (!this.#ws) {
throw new Error(
"send: websocket not initialized, please connect before trying to send a message"
);
}
this.#ws.send(JSON.stringify(message));
};
subscribe = (topics) => {
this.send({ type: "subscribe", topics });
};
unsubscribe = (topics) => {
this.send({ type: "unsubscribe", topics });
};
disconnect = () => {
if (!this.#ws) {
throw new Error(
"disconnect: websocket not initialized, please connect before trying to disconnect"
);
}
this.#userDisconnected = true;
this.#removeListeners();
this.#ws.close();
this.#ws = null;
this.emit("disconnect");
};
#reconnect = () => {
if (!this.#userDisconnected) {
this.connect();
}
};
#removeListeners = () => {
if (!this.#ws) {
throw new Error("disconnect: websocket not initialized");
}
this.#ws.removeEventListener("message", this.#handleMessage);
this.#ws.removeEventListener("error", this.#handleError);
this.#ws.removeEventListener("close", this.#handleClose);
this.#ws.removeEventListener("open", this.#handleOpen);
};
#handleOpen = () => {
this.emit("connect");
};
#handleClose = (event) => {
this.#removeListeners();
this.#ws = null;
this.emit("disconnect", {
...event,
code: event.code
});
if (this.#options.reconnect) {
setTimeout(this.#reconnect, this.#options.reconnectInterval);
}
};
#handleError = (event) => {
this.emit("error", new Error(event.message, { cause: event }));
};
#handleMessage = (event) => {
if (typeof event.data !== "string") {
this.emit("error", new Error("Invalid message format"));
return;
}
let data;
try {
data = JSON.parse(event.data);
} catch (error) {
this.emit("error", new Error("Invalid message format", { cause: error }));
return;
}
this.emit("message", data);
};
};
function createRealtimeV1Client(options) {
return new RealtimeV1Client(options);
}
// src/realtime/v1/close-code.ts
var RealtimeV1CloseCode = {
InvalidMessage: 4e3,
UnknownOperation: 4001,
InvalidParams: 4002,
InternalError: 5e3
};
// src/realtime/v2/client.ts
var import_eventemitter32 = __toESM(require("eventemitter3"), 1);
var import_ws2 = require("ws");
var import_zod2 = require("zod");
// src/realtime/v2/const.ts
var REALTIME_V2_DEFAULT_URL = "wss://realtime.sealevel.dev/v2";
// src/realtime/v2/client.ts
var realtimeClientOptionsSchema2 = import_zod2.z.object({
url: import_zod2.z.string().default(REALTIME_V2_DEFAULT_URL),
accessToken: import_zod2.z.string(),
reconnect: import_zod2.z.boolean().default(true),
reconnectInterval: import_zod2.z.number().default(1e3)
});
var RealtimeV2Client = class extends import_eventemitter32.default {
#options;
#userDisconnected = false;
#ws = null;
constructor(options) {
super();
this.#options = realtimeClientOptionsSchema2.parse(options ?? {});
}
connect = () => {
if (this.#ws) {
throw new Error("connect: websocket already intialized");
}
const url = new URL(this.#options.url);
url.searchParams.set("access_token", this.#options.accessToken);
this.#userDisconnected = false;
this.#ws = new import_ws2.WebSocket(url);
this.#ws.addEventListener("message", this.#handleMessage);
this.#ws.addEventListener("error", this.#handleError);
this.#ws.addEventListener("close", this.#handleClose);
this.#ws.addEventListener("open", this.#handleOpen);
};
send = (message) => {
if (!this.#ws) {
throw new Error(
"send: websocket not initialized, please connect before trying to send a message"
);
}
this.#ws.send(JSON.stringify(message));
};
subscribe = (topics) => {
this.send({ type: "subscribe", topics });
};
unsubscribe = (topics) => {
this.send({ type: "unsubscribe", topics });
};
disconnect = () => {
if (!this.#ws) {
throw new Error(
"disconnect: websocket not initialized, please connect before trying to disconnect"
);
}
this.#userDisconnected = true;
this.#removeListeners();
this.#ws.close();
this.#ws = null;
this.emit("disconnect");
};
#reconnect = () => {
if (!this.#userDisconnected) {
this.connect();
}
};
#removeListeners = () => {
if (!this.#ws) {
throw new Error("disconnect: websocket not initialized");
}
this.#ws.removeEventListener("message", this.#handleMessage);
this.#ws.removeEventListener("error", this.#handleError);
this.#ws.removeEventListener("close", this.#handleClose);
this.#ws.removeEventListener("open", this.#handleOpen);
};
#handleOpen = () => {
this.emit("connect");
};
#handleClose = (event) => {
this.#removeListeners();
this.#ws = null;
this.emit("disconnect", {
...event,
code: event.code
});
if (this.#options.reconnect) {
setTimeout(this.#reconnect, this.#options.reconnectInterval);
}
};
#handleError = (event) => {
this.emit("error", new Error(event.message, { cause: event }));
};
#handleMessage = (event) => {
if (typeof event.data !== "string") {
this.emit("error", new Error("Invalid message format"));
return;
}
let data;
try {
data = JSON.parse(event.data);
} catch (error) {
this.emit("error", new Error("Invalid message format", { cause: error }));
return;
}
this.emit("message", data);
};
};
function createRealtimeV2Client(options) {
return new RealtimeV2Client(options);
}
// src/realtime/v2/close-code.ts
var RealtimeV2CloseCode = {
InvalidMessage: 4e3,
UnknownOperation: 4001,
InvalidParams: 4002,
InternalError: 5e3
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
RealtimeV1Client,
RealtimeV1CloseCode,
RealtimeV2Client,
RealtimeV2CloseCode,
createRealtimeV1Client,
createRealtimeV2Client,
createV1ApiClient
});
//# sourceMappingURL=index.cjs.map