tsp-scale-sdk 1.2.0 → 1.2.1

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/bin/tsp-scale.js CHANGED
@@ -2,12 +2,11 @@
2
2
 
3
3
  const { execSync } = require('child_process');
4
4
  const path = require('path');
5
+ const os = require('os');
6
+ const fs = require('fs');
5
7
 
6
8
  try {
7
- // We reuse the curl CLI logic if available, or implement a lightweight version here
8
- // For simplicity, we'll just implement the init logic here for the SDK
9
9
  const readline = require('readline');
10
- const fs = require('fs');
11
10
  const crypto = require('crypto');
12
11
  const https = require('https');
13
12
 
@@ -24,7 +23,13 @@ try {
24
23
  }, res => {
25
24
  let body = '';
26
25
  res.on('data', chunk => body += chunk);
27
- res.on('end', () => resolve({ ok: res.statusCode < 300, json: () => JSON.parse(body) }));
26
+ res.on('end', () => {
27
+ try {
28
+ resolve({ ok: res.statusCode < 300, json: () => JSON.parse(body) });
29
+ } catch (e) {
30
+ resolve({ ok: false, json: () => ({ message: 'Invalid JSON response from server' }) });
31
+ }
32
+ });
28
33
  });
29
34
  req.on('error', reject);
30
35
  req.write(JSON.stringify(data));
@@ -97,16 +102,16 @@ try {
97
102
  const example = await ask("📄 Would you like to generate a sample 'tsp-test.js' script? (y/n): ");
98
103
  if (example.toLowerCase() === 'y') {
99
104
  const sampleCode = `const { TspScaleClient } = require('tsp-scale-sdk');
100
- require('dotenv').config();
101
105
 
102
- const client = new TspScaleClient(process.env.TSP_API_KEY);
106
+ // Smart Auth: Automatically uses .tspscale or environment variables
107
+ const client = new TspScaleClient();
103
108
 
104
109
  async function main() {
105
110
  console.log("Sending test email...");
106
111
  try {
107
112
  const res = await client.sendEmail({
108
- from: "support@yourdomain.com", // Replace with your verified sender
109
- to: "recipient@example.com",
113
+ // 'from' is optional if your key is locked to a sender identity
114
+ to: "recipient@example.com",
110
115
  subject: "TSP Scale Test",
111
116
  html: "<h1>It works!</h1>"
112
117
  });
@@ -129,7 +134,7 @@ main();`;
129
134
  if (command === 'init') {
130
135
  init();
131
136
  } else {
132
- console.log("Usage: npx tsp-scale init");
137
+ console.log("Usage: npx tsp-scale-sdk init");
133
138
  }
134
139
 
135
140
  } catch (err) {
package/dist/index.js CHANGED
@@ -1,2 +1,2 @@
1
- var d=require("crypto"),m="1.2.0",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 d=require("crypto"),m="1.2.1",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};
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.0",
3
+ "version": "1.2.1",
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",