@joytreesite/joytree 1.0.2 → 1.0.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/lib/api.js +22 -22
  2. package/package.json +1 -1
package/lib/api.js CHANGED
@@ -1,15 +1,10 @@
1
1
  'use strict';
2
2
 
3
- const https = require('https');
4
- const http = require('http');
3
+ const https = require('https');
4
+ const http = require('http');
5
5
  const { URL } = require('url');
6
- const config = require('./config');
6
+ const config = require('./config');
7
7
 
8
- /**
9
- * Make an authenticated request to the Joytree API.
10
- * Auth is done via ?api_key=jtk_... (same pattern as /api/v1/transfer).
11
- * For session-based endpoints we pass token as Bearer header.
12
- */
13
8
  async function request(method, endpoint, body = null, opts = {}) {
14
9
  const apiKey = config.getApiKey();
15
10
  const baseUrl = config.getBaseUrl();
@@ -18,9 +13,7 @@ async function request(method, endpoint, body = null, opts = {}) {
18
13
  throw new Error('Not authenticated. Run: joytree login');
19
14
  }
20
15
 
21
- const separator = endpoint.includes('?') ? '&' : '?';
22
- const url = new URL(`${baseUrl}${endpoint}${apiKey ? `${separator}api_key=${encodeURIComponent(apiKey)}` : ''}`);
23
-
16
+ const url = new URL(`${baseUrl}${endpoint}`);
24
17
  const payload = body ? JSON.stringify(body) : null;
25
18
 
26
19
  return new Promise((resolve, reject) => {
@@ -33,6 +26,8 @@ async function request(method, endpoint, body = null, opts = {}) {
33
26
  headers: {
34
27
  'Content-Type': 'application/json',
35
28
  'User-Agent': `joytree-cli/${require('../package.json').version}`,
29
+ // Send API key as Bearer token — this is what requireAuth on the server reads
30
+ ...(apiKey ? { 'Authorization': `Bearer ${apiKey}` } : {}),
36
31
  ...(payload ? { 'Content-Length': Buffer.byteLength(payload) } : {}),
37
32
  ...(opts.headers || {}),
38
33
  },
@@ -63,7 +58,6 @@ async function request(method, endpoint, body = null, opts = {}) {
63
58
  });
64
59
  }
65
60
 
66
- // Convenience wrappers
67
61
  const api = {
68
62
  get: (path, opts) => request('GET', path, null, opts),
69
63
  post: (path, body, opts) => request('POST', path, body, opts),
@@ -72,19 +66,24 @@ const api = {
72
66
  delete: (path, opts) => request('DELETE', path, null, opts),
73
67
  };
74
68
 
75
- /**
76
- * Validate an API key by calling /api/v1/transfer and reading the response.
77
- * Returns { ok, email, projectCount } or throws.
78
- */
69
+ // Validate API key via /api/v1/transfer
79
70
  async function validateApiKey(apiKey, baseUrl) {
80
- const url = `${baseUrl}/api/v1/transfer?api_key=${encodeURIComponent(apiKey)}`;
81
- const parsed = new URL(url);
82
- const mod = parsed.protocol === 'https:' ? require('https') : require('http');
71
+ const url = new URL(`${baseUrl}/api/v1/transfer`);
72
+ const mod = url.protocol === 'https:' ? require('https') : require('http');
83
73
 
84
74
  return new Promise((resolve, reject) => {
85
- const req = mod.get(url, {
86
- headers: { 'User-Agent': `joytree-cli/${require('../package.json').version}` }
87
- }, (res) => {
75
+ const reqOpts = {
76
+ hostname: url.hostname,
77
+ port: url.port || (url.protocol === 'https:' ? 443 : 80),
78
+ path: url.pathname,
79
+ method: 'GET',
80
+ headers: {
81
+ 'Authorization': `Bearer ${apiKey}`,
82
+ 'User-Agent': `joytree-cli/${require('../package.json').version}`,
83
+ },
84
+ };
85
+
86
+ const req = mod.request(reqOpts, (res) => {
88
87
  let raw = '';
89
88
  res.on('data', c => raw += c);
90
89
  res.on('end', () => {
@@ -98,6 +97,7 @@ async function validateApiKey(apiKey, baseUrl) {
98
97
  });
99
98
  });
100
99
  req.on('error', reject);
100
+ req.end();
101
101
  });
102
102
  }
103
103
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@joytreesite/joytree",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "description": "Joytree CLI — deploy and manage your sites from the terminal",
5
5
  "main": "bin/joytree.js",
6
6
  "bin": {