@kispi/chat 0.2.2 → 0.3.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.
@@ -1,6 +1,8 @@
1
1
  import {
2
- errorFrom
3
- } from "../chunk-5QQRM7BH.js";
2
+ ChatEndpoint,
3
+ errorFrom,
4
+ restURLFrom
5
+ } from "../chunk-WSBOTTBM.js";
4
6
 
5
7
  // src/server/api.ts
6
8
  var ServerRest = class {
@@ -178,17 +180,23 @@ function createChatServer(options) {
178
180
  if (!options.secretKey.startsWith("sk_")) {
179
181
  throw new TypeError("secretKey must be an sk_ key; a pk_ cannot sign tokens or reach server routes");
180
182
  }
181
- if (options.keyId === "") {
182
- throw new TypeError("keyId is required: it becomes the token's kid, which tells the server which key signed it");
183
- }
184
- const rest = new ServerRest(options.url, options.secretKey, options.fetch ?? globalThis.fetch.bind(globalThis));
183
+ const keyId = keyIdFrom(options.secretKey);
184
+ const rest = new ServerRest(
185
+ restURLFrom(options.endpoint ?? ChatEndpoint),
186
+ options.secretKey,
187
+ options.fetch ?? globalThis.fetch.bind(globalThis)
188
+ );
185
189
  const guestSecrets = options.guestSecret === void 0 ? [] : typeof options.guestSecret === "string" ? [options.guestSecret] : options.guestSecret;
186
190
  for (const secret of guestSecrets) {
187
191
  if (secret.length < MinGuestSecretLength) {
188
192
  throw new RangeError(`guestSecret must be at least ${MinGuestSecretLength} characters`);
189
193
  }
190
194
  }
191
- const sign = (input) => signHS256({ alg: "HS256", typ: "JWT", kid: options.keyId }, tokenClaims(input, /* @__PURE__ */ new Date()), options.secretKey);
195
+ const sign = (input) => signHS256(
196
+ { alg: "HS256", typ: "JWT", ...keyId === void 0 ? {} : { kid: keyId } },
197
+ tokenClaims(input, /* @__PURE__ */ new Date()),
198
+ options.secretKey
199
+ );
192
200
  return {
193
201
  token: sign,
194
202
  guest(input) {
@@ -263,7 +271,7 @@ function createChatServer(options) {
263
271
  // with 200 and `purgedMessages: 0`, the identity anonymized and
264
272
  // the messages left standing. Nothing failed; the caller was told
265
273
  // it had worked.
266
- delete: (userId, o) => rest.call("DELETE", `/v1/users/${userId}${rest.query({ purge_messages: o?.purgeMessages ? "true" : void 0 })}`),
274
+ withdraw: (userId, o) => rest.call("DELETE", `/v1/users/${userId}${rest.query({ purge_messages: o?.purgeMessages ? "true" : void 0 })}`),
267
275
  purgeMessages: (userId) => rest.call("DELETE", `/v1/users/${userId}/messages`),
268
276
  revokeTokens: async (userId) => {
269
277
  await rest.call("POST", `/v1/users/${userId}/revoke-tokens`);
@@ -295,6 +303,12 @@ function createChatServer(options) {
295
303
  }
296
304
  };
297
305
  }
306
+ function keyIdFrom(secretKey) {
307
+ const parts = secretKey.split("_");
308
+ if (parts.length < 3) return void 0;
309
+ const id = parts[1];
310
+ return id !== void 0 && /^[0-9A-HJKMNP-TV-Z]{13}$/.test(id) ? id : void 0;
311
+ }
298
312
  export {
299
313
  DefaultTokenTtlSeconds,
300
314
  GuestIdPrefix,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kispi/chat",
3
- "version": "0.2.2",
3
+ "version": "0.3.0",
4
4
  "description": "Client SDK for the chat server: one WebSocket, many rooms, ordered history.",
5
5
  "license": "MIT",
6
6
  "type": "module",