tsp-scale-sdk 1.2.2 → 1.2.3

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.
Files changed (2) hide show
  1. package/dist/index.js +1 -1
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -1,2 +1,2 @@
1
- var d=require("crypto"),m="1.2.2",y={prod:"https://api.tspscale.in/api/v1",staging:"https://staging-api.tspscale.in/api/v1",local:"http://localhost:4000/api/v1"};function g(l){if(l.baseUrl)return l.baseUrl;let t=(l.env||"prod").toLowerCase();return y[t]||y.prod}var f=class{constructor(t){let s={};typeof t=="string"?s={apiKey:t}:s=t||{};let e=require("fs"),o=require("path"),a=require("os"),n=this._loadLocalConfig();if(this.apiKey=s.apiKey||process.env.TSP_API_KEY||n.apiKey||this._loadGlobalKey(),this.projectId=s.projectId||process.env.TSP_PROJECT_ID||n.projectId,this.apiKey||console.warn("\u26A0\uFE0F TSP Scale: No API Key found. Run 'npx tsp-scale init'."),this.baseUrl=g(s).replace(/\/+$/,""),this.timeoutMs=s.timeoutMs||15e3,this.fetchImpl=s.fetchImpl||globalThis.fetch,typeof this.fetchImpl!="function")throw new Error("Fetch implementation is not available. Use Node 18+ or pass fetchImpl.")}_loadLocalConfig(){try{let t=require("fs"),e=require("path").join(process.cwd(),".tspscale");if(t.existsSync(e))return JSON.parse(t.readFileSync(e,"utf8"))}catch{}return{}}_loadGlobalKey(){try{let t=require("fs"),s=require("path"),e=require("os"),o=s.join(e.homedir(),".tsp-scale","config.json");if(t.existsSync(o))return JSON.parse(t.readFileSync(o,"utf8")).apiKey}catch{}return null}async sendEmail(t){let s="/emails/send",e=`${this.baseUrl}${s}`,o=JSON.stringify(t||{}),a=Date.now().toString(),n=d.randomBytes(16).toString("hex"),h=this._sign({timestamp:a,nonce:n,method:"POST",path:s,body:o}),p=new AbortController,u=setTimeout(()=>p.abort(),this.timeoutMs);try{let i=await this.fetchImpl(e,{method:"POST",headers:{"Content-Type":"application/json","x-api-key":this.apiKey,"x-tsp-client":`tsp-scale-sdk/${m}`,"x-tsp-timestamp":a,"x-tsp-nonce":n,"x-tsp-signature":h,...this.projectId?{"x-tsp-project-id":this.projectId}:{}},body:o,signal:p.signal}),r=await i.json().catch(()=>({}));if(!i.ok){let c=new Error(r?.error||`Request failed with status ${i.status}`);throw c.status=i.status,c.response=r,c}return r}finally{clearTimeout(u)}}async sendWhatsApp(t){let s="/whatsapp/messages/send",e=`${this.baseUrl}${s}`,o=JSON.stringify(t||{}),a=Date.now().toString(),n=d.randomBytes(16).toString("hex"),h=this._sign({timestamp:a,nonce:n,method:"POST",path:s,body:o}),p=new AbortController,u=setTimeout(()=>p.abort(),this.timeoutMs);try{let i=await this.fetchImpl(e,{method:"POST",headers:{"Content-Type":"application/json","x-api-key":this.apiKey,"x-tsp-client":`tsp-scale-sdk/${m}`,"x-tsp-timestamp":a,"x-tsp-nonce":n,"x-tsp-signature":h},body:o,signal:p.signal}),r=await i.json().catch(()=>({}));if(!i.ok){let c=new Error(r?.error||`Request failed with status ${i.status}`);throw c.status=i.status,c.response=r,c}return r}finally{clearTimeout(u)}}_sign({timestamp:t,nonce:s,method:e,path:o,body:a}){let n=`${t}.${s}.${e}.${o}.${a}`;return d.createHmac("sha256",this.apiKey).update(n).digest("hex")}};module.exports={TspScaleClient:f};
1
+ var p=typeof window>"u"&&typeof require=="function"?require("crypto"):null,y="1.2.3",m={prod:"https://api.tspscale.in/api/v1",staging:"https://staging-api.tspscale.in/api/v1",local:"http://localhost:4000/api/v1"};function g(u){if(u.baseUrl)return u.baseUrl;let t=(u.env||"prod").toLowerCase();return m[t]||m.prod}var f=class{constructor(t){let e={};typeof t=="string"?e={apiKey:t}:e=t||{};let s=typeof window>"u"&&typeof process<"u"&&process.versions&&process.versions.node,o=s?this._loadLocalConfig():{};if(this.apiKey=e.apiKey||(s?process.env.TSP_API_KEY:void 0)||o.apiKey||(s?this._loadGlobalKey():void 0),this.projectId=e.projectId||(s?process.env.TSP_PROJECT_ID:void 0)||o.projectId,!this.apiKey&&s&&console.warn("\u26A0\uFE0F TSP Scale: No API Key found. Run 'npx tsp-scale init'."),this.baseUrl=g(e).replace(/\/+$/,""),this.timeoutMs=e.timeoutMs||15e3,this.fetchImpl=e.fetchImpl||globalThis.fetch,typeof this.fetchImpl!="function")throw new Error("Fetch implementation is not available. Use Node 18+ or pass fetchImpl.")}_loadLocalConfig(){try{let t=require("fs"),s=require("path").join(process.cwd(),".tspscale");if(t.existsSync(s))return JSON.parse(t.readFileSync(s,"utf8"))}catch{}return{}}_loadGlobalKey(){try{let t=require("fs"),e=require("path"),s=require("os"),o=e.join(s.homedir(),".tsp-scale","config.json");if(t.existsSync(o))return JSON.parse(t.readFileSync(o,"utf8")).apiKey}catch{}return null}async sendEmail(t){if(!p)throw new Error("\u26A0\uFE0F TSP Scale SDK: Client-side signing is not permitted. API requests must be executed on the server to keep your API key secure.");let e="/emails/send",s=`${this.baseUrl}${e}`,o=JSON.stringify(t||{}),i=Date.now().toString(),r=p.randomBytes(16).toString("hex"),h=this._sign({timestamp:i,nonce:r,method:"POST",path:e,body:o}),l=new AbortController,d=setTimeout(()=>l.abort(),this.timeoutMs);try{let n=await this.fetchImpl(s,{method:"POST",headers:{"Content-Type":"application/json","x-api-key":this.apiKey,"x-tsp-client":`tsp-scale-sdk/${y}`,"x-tsp-timestamp":i,"x-tsp-nonce":r,"x-tsp-signature":h,...this.projectId?{"x-tsp-project-id":this.projectId}:{}},body:o,signal:l.signal}),a=await n.json().catch(()=>({}));if(!n.ok){let c=new Error(a?.error||`Request failed with status ${n.status}`);throw c.status=n.status,c.response=a,c}return a}finally{clearTimeout(d)}}async sendWhatsApp(t){if(!p)throw new Error("\u26A0\uFE0F TSP Scale SDK: Client-side signing is not permitted. API requests must be executed on the server to keep your API key secure.");let e="/whatsapp/messages/send",s=`${this.baseUrl}${e}`,o=JSON.stringify(t||{}),i=Date.now().toString(),r=p.randomBytes(16).toString("hex"),h=this._sign({timestamp:i,nonce:r,method:"POST",path:e,body:o}),l=new AbortController,d=setTimeout(()=>l.abort(),this.timeoutMs);try{let n=await this.fetchImpl(s,{method:"POST",headers:{"Content-Type":"application/json","x-api-key":this.apiKey,"x-tsp-client":`tsp-scale-sdk/${y}`,"x-tsp-timestamp":i,"x-tsp-nonce":r,"x-tsp-signature":h},body:o,signal:l.signal}),a=await n.json().catch(()=>({}));if(!n.ok){let c=new Error(a?.error||`Request failed with status ${n.status}`);throw c.status=n.status,c.response=a,c}return a}finally{clearTimeout(d)}}_sign({timestamp:t,nonce:e,method:s,path:o,body:i}){if(!p)throw new Error("\u26A0\uFE0F TSP Scale SDK: Client-side signing is not permitted. API requests must be executed on the server to keep your API key secure.");let r=`${t}.${e}.${s}.${o}.${i}`;return p.createHmac("sha256",this.apiKey).update(r).digest("hex")}};module.exports={TspScaleClient:f};
2
2
  //# sourceMappingURL=index.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tsp-scale-sdk",
3
- "version": "1.2.2",
3
+ "version": "1.2.3",
4
4
  "description": "Official Node.js client for TSP Scale Email and WhatsApp API",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",