@erebus-sh/sdk
Version:
To install dependencies:
9 lines • 4.68 kB
JavaScript
import{ErebusError}from"./error-CVJkstUc.js";import ky,{HTTPError}from"ky";const CLIENT_NAME=`erebus-service-client`,baseClient=({base_url:e})=>ky.create({prefixUrl:e??`https://api.erebus.sh/v1/`,headers:{"content-type":`application/json`},hooks:{beforeRequest:[e=>{e.headers.set(`x-client`,`erebus-service-client`)}]}});var ErebusSession=class{grantRequest;client;constructor(n,r){if(this.grantRequest=n,this.client=r,!this.grantRequest.secret_key)throw new ErebusError([`Secret key is required to create a session. Use the ErebusService class method.`,`example:`,`const service = new ErebusService({ secret_api_key: 'test' });`,`const session = await service.prepareSession({ userId: 'test' });`].join(`
`));if(!this.grantRequest.userId)throw new ErebusError([`User ID is required to create a session. Use the ErebusService class method.`,`example:`,`const service = new ErebusService({ secret_api_key: 'test' });`,`const session = await service.prepareSession({ userId: 'test' });`].join(`
`));this.grantRequest.expiresAt=Math.floor(Date.now()/1e3)+7200}join(n){if(!n)throw new ErebusError(`Channel is required to join.`);if(!/^[A-Za-z0-9_]+$/.test(n))throw new ErebusError(`Channel name ${n} must not contain spaces or special characters. Only letters, numbers, and underscores are allowed.`);if(n.length>64)throw new ErebusError(`Channel name must be less than 64 characters.`);if(this.grantRequest.channel)throw new ErebusError(`Channel already joined. Please call join(channel) only once.`);this.grantRequest.channel=n}allow(n,r){if(!this.grantRequest.channel)throw new ErebusError([`You must set a channel before allowing access to any topics.`,`Please call join(channel) first.`,`example:`,`session.join('channel_name');`].join(`
`));if(!n)throw new ErebusError(`Topic is required to allow access.`);if(n!==`*`&&!/^[A-Za-z0-9_]+$/.test(n))throw new ErebusError(`Topic name must not contain spaces or special characters. Only letters, numbers, and underscores are allowed. Use '*' to allow all topics.`);if(n.length>64)throw new ErebusError(`Topic name must be less than 64 characters.`);if(this.grantRequest.topics.length>64)throw new ErebusError(`Connecting to more than 64 topics at once is inefficient. Please reduce the number of topics in this grant.`);if(!r)throw new ErebusError(`Scope is required to allow access.`);this.grantRequest.topics.push({topic:n,scope:r})}setExpiration(n){let r=Math.floor(Date.now()/1e3),i=r+600,a=r+7200;if(typeof n!=`number`||!Number.isFinite(n))throw new ErebusError(`Expiration must be a valid number (unix timestamp in seconds).`);if(n<i)throw new ErebusError(`Expiration must be at least 10 minutes from now.`);if(n>a)throw new ErebusError(`Expiration cannot be more than 2 hours from now.`);this.grantRequest.expiresAt=n}async authorize(){if(!this.grantRequest.channel)throw new ErebusError(`You must set a channel before allowing access to any topics. Please call join(channel) first.`);if(this.grantRequest.topics.length===0)throw new ErebusError([`At least one topic is required. Please use the allow method to grant access to topics.`,`example:`,`session.allow('topic-1', Access.Read);`].join(`
`));if(this.grantRequest.topics.length>64)throw new ErebusError([`Connecting to more than 64 topics at once is inefficient. Please reduce the number of topics in this grant.`].join(`
`));let n=this.grantRequest;try{let r=await this.client.post(`api/v1/grant-channel`,{json:n});if(!r.ok)throw new ErebusError([`The server returned an error.`,`Please check your API key and ensure you have provided a valid topics array (at least one and less than 64 topics). Please try again.`,`If the problem persists, please contact support: sdk@erebus.sh`].join(`
`));let i=await r.json();if(!i)throw new ErebusError([`The server returned an empty response or invalid data.`,`Please check your API key and try again.`,`If the problem persists, please contact support: sdk@erebus.sh`].join(`
`));return i.grant_jwt}catch(n){throw n instanceof HTTPError?n.response.status===401?new ErebusError(`Invalid API key or token. Please check your API key and try again.`,JSON.stringify(await n.response.json())):n.response.status===400?new ErebusError(`Invalid topics array. Please check your topics array and try again.`,JSON.stringify(await n.response.json())):n.response.status===500?new ErebusError(`Internal server error. Please try again later.`,JSON.stringify(await n.response.json())):new ErebusError(`Failed to get token: ${n.response.statusText}`,JSON.stringify(await n.response.json())):n}}get __debugObject(){return{grant:this.grantRequest,client:this.client}}};export{ErebusSession,baseClient};
//# sourceMappingURL=session-BwF7HMrP.js.map