tsp-scale-sdk 1.2.1 ā 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.
- package/README.md +1 -1
- package/bin/tsp-scale.js +49 -20
- package/dist/index.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -7,7 +7,7 @@ Official Node.js SDK for TSP Scale.
|
|
|
7
7
|
Initialize your project in seconds. This will link your project, create a secure hidden config, and add it to your `.gitignore` automatically.
|
|
8
8
|
|
|
9
9
|
```bash
|
|
10
|
-
npx tsp-scale init
|
|
10
|
+
npx tsp-scale-sdk init
|
|
11
11
|
```
|
|
12
12
|
|
|
13
13
|
### Manual Installation
|
package/bin/tsp-scale.js
CHANGED
|
@@ -39,17 +39,29 @@ try {
|
|
|
39
39
|
|
|
40
40
|
async function init() {
|
|
41
41
|
console.log("\nš TSP Scale SDK - Initializing Project\n");
|
|
42
|
-
const apiKey = await ask("š Enter your API Key: ");
|
|
43
|
-
if (!apiKey) return console.error("Error: API Key is required.");
|
|
44
42
|
|
|
45
|
-
const projectIdentifier = await ask("š Enter Project Name: ");
|
|
46
43
|
const projectId = crypto.createHash('md5').update(process.cwd()).digest('hex');
|
|
44
|
+
const localConfigPath = path.join(process.cwd(), '.tspscale');
|
|
45
|
+
let existingConfig = null;
|
|
46
|
+
|
|
47
|
+
if (fs.existsSync(localConfigPath)) {
|
|
48
|
+
try {
|
|
49
|
+
existingConfig = JSON.parse(fs.readFileSync(localConfigPath, 'utf8'));
|
|
50
|
+
} catch (e) {}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const apiKey = await ask(`š Enter your API Key${existingConfig ? ` (Current: ${existingConfig.apiKey.substring(0, 12)}...)` : ''}: `);
|
|
54
|
+
const finalKey = apiKey || (existingConfig ? existingConfig.apiKey : null);
|
|
55
|
+
|
|
56
|
+
if (!finalKey) return console.error("ā Error: API Key is required.");
|
|
57
|
+
|
|
58
|
+
const projectIdentifier = await ask("š Enter Project Name: ");
|
|
47
59
|
|
|
48
60
|
console.log("ā³ Verifying with server...");
|
|
49
61
|
|
|
50
62
|
try {
|
|
51
63
|
const res = await post("https://api.tspscale.in/api/v1/apikeys/verify", {
|
|
52
|
-
key:
|
|
64
|
+
key: finalKey,
|
|
53
65
|
projectId,
|
|
54
66
|
projectIdentifier: projectIdentifier || "NodeJS Project"
|
|
55
67
|
});
|
|
@@ -59,10 +71,10 @@ try {
|
|
|
59
71
|
// 1. Save to Global Vault (Fallback)
|
|
60
72
|
const configDir = path.join(os.homedir(), '.tsp-scale');
|
|
61
73
|
if (!fs.existsSync(configDir)) fs.mkdirSync(configDir, { recursive: true });
|
|
62
|
-
fs.writeFileSync(path.join(configDir, 'config.json'), JSON.stringify({ apiKey }, null, 2));
|
|
74
|
+
fs.writeFileSync(path.join(configDir, 'config.json'), JSON.stringify({ apiKey: finalKey }, null, 2));
|
|
63
75
|
|
|
64
76
|
// 2. Save to Local Hidden Config (.tspscale) - Primary
|
|
65
|
-
fs.writeFileSync(
|
|
77
|
+
fs.writeFileSync(localConfigPath, JSON.stringify({ apiKey: finalKey, projectId }, null, 2));
|
|
66
78
|
|
|
67
79
|
// 3. Auto-GitIgnore
|
|
68
80
|
const gitignorePath = path.join(process.cwd(), '.gitignore');
|
|
@@ -84,24 +96,40 @@ try {
|
|
|
84
96
|
console.log("When you deploy to Railway, Vercel, or a VPS,");
|
|
85
97
|
console.log("add these two Environment Variables:");
|
|
86
98
|
console.log("");
|
|
87
|
-
console.log(` TSP_API_KEY = ${
|
|
99
|
+
console.log(` TSP_API_KEY = ${finalKey}`);
|
|
88
100
|
console.log(` TSP_PROJECT_ID = ${projectId}`);
|
|
89
101
|
console.log("-------------------------------------------\n");
|
|
90
102
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
103
|
+
// --- SMART INSTALL CHECK ---
|
|
104
|
+
let isInstalled = false;
|
|
105
|
+
try {
|
|
106
|
+
const pkgJson = JSON.parse(fs.readFileSync(path.join(process.cwd(), 'package.json'), 'utf8'));
|
|
107
|
+
if (pkgJson.dependencies && pkgJson.dependencies['tsp-scale-sdk']) isInstalled = true;
|
|
108
|
+
if (pkgJson.devDependencies && pkgJson.devDependencies['tsp-scale-sdk']) isInstalled = true;
|
|
109
|
+
} catch (e) {}
|
|
110
|
+
|
|
111
|
+
if (isInstalled) {
|
|
112
|
+
console.log("š¦ SDK already detected in package.json. Skipping installation. ā
\n");
|
|
113
|
+
} else {
|
|
114
|
+
const install = await ask("š¦ Would you like to install the 'tsp-scale-sdk' in this project? (y/n): ");
|
|
115
|
+
if (install.toLowerCase() === 'y') {
|
|
116
|
+
console.log("ā³ Installing...");
|
|
117
|
+
try {
|
|
118
|
+
execSync('npm install tsp-scale-sdk', { stdio: 'inherit' });
|
|
119
|
+
console.log("\nā
SDK installed successfully!");
|
|
120
|
+
} catch (e) {
|
|
121
|
+
console.error("\nā Installation failed. Please run 'npm install tsp-scale-sdk' manually.");
|
|
122
|
+
}
|
|
99
123
|
}
|
|
100
124
|
}
|
|
101
125
|
|
|
102
|
-
const
|
|
103
|
-
if (
|
|
104
|
-
|
|
126
|
+
const examplePath = path.join(process.cwd(), "tsp-test.js");
|
|
127
|
+
if (fs.existsSync(examplePath)) {
|
|
128
|
+
console.log("š Sample 'tsp-test.js' already exists. Skipping generator. ā
\n");
|
|
129
|
+
} else {
|
|
130
|
+
const example = await ask("š Would you like to generate a sample 'tsp-test.js' script? (y/n): ");
|
|
131
|
+
if (example.toLowerCase() === 'y') {
|
|
132
|
+
const sampleCode = `const { TspScaleClient } = require('tsp-scale-sdk');
|
|
105
133
|
|
|
106
134
|
// Smart Auth: Automatically uses .tspscale or environment variables
|
|
107
135
|
const client = new TspScaleClient();
|
|
@@ -122,8 +150,9 @@ async function main() {
|
|
|
122
150
|
}
|
|
123
151
|
|
|
124
152
|
main();`;
|
|
125
|
-
|
|
126
|
-
|
|
153
|
+
fs.writeFileSync(examplePath, sampleCode);
|
|
154
|
+
console.log("\nā
'tsp-test.js' created. Run it with: node tsp-test.js");
|
|
155
|
+
}
|
|
127
156
|
}
|
|
128
157
|
} catch (err) {
|
|
129
158
|
console.error(`\nā Error: ${err.message}`);
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var
|
|
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
|