appstash 0.3.0 → 0.4.0

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 CHANGED
@@ -288,8 +288,7 @@ pnpm test:watch
288
288
 
289
289
  ## Credits
290
290
 
291
- Built for developers, with developers.
292
- 👉 https://launchql.com | https://hyperweb.io
291
+ **🛠 Built by the [Constructive](https://constructive.io) team — creators of modular Postgres tooling for secure, composable backends. If you like our work, contribute on [GitHub](https://github.com/constructive-io).**
293
292
 
294
293
  ## Disclaimer
295
294
 
@@ -0,0 +1,37 @@
1
+ export interface ContextConfig {
2
+ name: string;
3
+ endpoint: string;
4
+ createdAt: string;
5
+ updatedAt: string;
6
+ }
7
+ export interface ContextCredentials {
8
+ token: string;
9
+ expiresAt?: string;
10
+ refreshToken?: string;
11
+ }
12
+ export interface Credentials {
13
+ tokens: Record<string, ContextCredentials>;
14
+ }
15
+ export interface GlobalSettings {
16
+ currentContext?: string;
17
+ }
18
+ export interface ConfigStoreOptions {
19
+ baseDir?: string;
20
+ }
21
+ export interface ConfigStore {
22
+ loadSettings(): GlobalSettings;
23
+ saveSettings(settings: GlobalSettings): void;
24
+ createContext(name: string, options: {
25
+ endpoint: string;
26
+ }): ContextConfig;
27
+ loadContext(name: string): ContextConfig | null;
28
+ listContexts(): ContextConfig[];
29
+ deleteContext(name: string): boolean;
30
+ getCurrentContext(): ContextConfig | null;
31
+ setCurrentContext(name: string): boolean;
32
+ setCredentials(contextName: string, creds: ContextCredentials): void;
33
+ getCredentials(contextName: string): ContextCredentials | null;
34
+ removeCredentials(contextName: string): boolean;
35
+ hasValidCredentials(contextName: string): boolean;
36
+ }
37
+ export declare function createConfigStore(toolName: string, options?: ConfigStoreOptions): ConfigStore;
@@ -0,0 +1,194 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.createConfigStore = createConfigStore;
37
+ const fs = __importStar(require("fs"));
38
+ const path = __importStar(require("path"));
39
+ const index_1 = require("./index");
40
+ const DEFAULT_SETTINGS = {};
41
+ function readJson(filePath, fallback) {
42
+ if (fs.existsSync(filePath)) {
43
+ try {
44
+ return JSON.parse(fs.readFileSync(filePath, 'utf8'));
45
+ }
46
+ catch {
47
+ return fallback;
48
+ }
49
+ }
50
+ return fallback;
51
+ }
52
+ function writeJson(filePath, data, mode) {
53
+ const dir = path.dirname(filePath);
54
+ if (!fs.existsSync(dir)) {
55
+ fs.mkdirSync(dir, { recursive: true });
56
+ }
57
+ const options = mode ? { mode } : {};
58
+ fs.writeFileSync(filePath, JSON.stringify(data, null, 2), options);
59
+ }
60
+ function createConfigStore(toolName, options) {
61
+ const dirs = (0, index_1.appstash)(toolName, { ensure: true, baseDir: options?.baseDir });
62
+ function settingsPath() {
63
+ return (0, index_1.resolve)(dirs, 'config', 'settings.json');
64
+ }
65
+ function credentialsPath() {
66
+ return (0, index_1.resolve)(dirs, 'config', 'credentials.json');
67
+ }
68
+ function contextPath(name) {
69
+ const contextsDir = (0, index_1.resolve)(dirs, 'config', 'contexts');
70
+ if (!fs.existsSync(contextsDir)) {
71
+ fs.mkdirSync(contextsDir, { recursive: true });
72
+ }
73
+ return path.join(contextsDir, `${name}.json`);
74
+ }
75
+ function loadSettings() {
76
+ return readJson(settingsPath(), DEFAULT_SETTINGS);
77
+ }
78
+ function saveSettings(settings) {
79
+ writeJson(settingsPath(), settings);
80
+ }
81
+ function loadContext(name) {
82
+ return readJson(contextPath(name), null);
83
+ }
84
+ function createContext(name, options) {
85
+ const now = new Date().toISOString();
86
+ const context = {
87
+ name,
88
+ endpoint: options.endpoint,
89
+ createdAt: now,
90
+ updatedAt: now,
91
+ };
92
+ writeJson(contextPath(name), context);
93
+ return context;
94
+ }
95
+ function listContexts() {
96
+ const contextsDir = (0, index_1.resolve)(dirs, 'config', 'contexts');
97
+ if (!fs.existsSync(contextsDir)) {
98
+ return [];
99
+ }
100
+ const files = fs.readdirSync(contextsDir).filter(f => f.endsWith('.json'));
101
+ const contexts = [];
102
+ for (const file of files) {
103
+ try {
104
+ const content = fs.readFileSync(path.join(contextsDir, file), 'utf8');
105
+ contexts.push(JSON.parse(content));
106
+ }
107
+ catch {
108
+ // skip invalid files
109
+ }
110
+ }
111
+ return contexts;
112
+ }
113
+ function deleteContext(name) {
114
+ const p = contextPath(name);
115
+ if (fs.existsSync(p)) {
116
+ fs.unlinkSync(p);
117
+ const settings = loadSettings();
118
+ if (settings.currentContext === name) {
119
+ settings.currentContext = undefined;
120
+ saveSettings(settings);
121
+ }
122
+ return true;
123
+ }
124
+ return false;
125
+ }
126
+ function getCurrentContext() {
127
+ const settings = loadSettings();
128
+ if (settings.currentContext) {
129
+ return loadContext(settings.currentContext);
130
+ }
131
+ return null;
132
+ }
133
+ function setCurrentContext(name) {
134
+ const context = loadContext(name);
135
+ if (!context) {
136
+ return false;
137
+ }
138
+ const settings = loadSettings();
139
+ settings.currentContext = name;
140
+ saveSettings(settings);
141
+ return true;
142
+ }
143
+ function loadCredentials() {
144
+ return readJson(credentialsPath(), { tokens: {} });
145
+ }
146
+ function saveCredentials(credentials) {
147
+ writeJson(credentialsPath(), credentials, 0o600);
148
+ }
149
+ function setCredentials(contextName, creds) {
150
+ const credentials = loadCredentials();
151
+ credentials.tokens[contextName] = creds;
152
+ saveCredentials(credentials);
153
+ }
154
+ function getCredentials(contextName) {
155
+ const credentials = loadCredentials();
156
+ return credentials.tokens[contextName] || null;
157
+ }
158
+ function removeCredentials(contextName) {
159
+ const credentials = loadCredentials();
160
+ if (credentials.tokens[contextName]) {
161
+ delete credentials.tokens[contextName];
162
+ saveCredentials(credentials);
163
+ return true;
164
+ }
165
+ return false;
166
+ }
167
+ function hasValidCredentials(contextName) {
168
+ const creds = getCredentials(contextName);
169
+ if (!creds || !creds.token) {
170
+ return false;
171
+ }
172
+ if (creds.expiresAt) {
173
+ const expiresAt = new Date(creds.expiresAt);
174
+ if (expiresAt <= new Date()) {
175
+ return false;
176
+ }
177
+ }
178
+ return true;
179
+ }
180
+ return {
181
+ loadSettings,
182
+ saveSettings,
183
+ createContext,
184
+ loadContext,
185
+ listContexts,
186
+ deleteContext,
187
+ getCurrentContext,
188
+ setCurrentContext,
189
+ setCredentials,
190
+ getCredentials,
191
+ removeCredentials,
192
+ hasValidCredentials,
193
+ };
194
+ }
@@ -0,0 +1,37 @@
1
+ export interface ContextConfig {
2
+ name: string;
3
+ endpoint: string;
4
+ createdAt: string;
5
+ updatedAt: string;
6
+ }
7
+ export interface ContextCredentials {
8
+ token: string;
9
+ expiresAt?: string;
10
+ refreshToken?: string;
11
+ }
12
+ export interface Credentials {
13
+ tokens: Record<string, ContextCredentials>;
14
+ }
15
+ export interface GlobalSettings {
16
+ currentContext?: string;
17
+ }
18
+ export interface ConfigStoreOptions {
19
+ baseDir?: string;
20
+ }
21
+ export interface ConfigStore {
22
+ loadSettings(): GlobalSettings;
23
+ saveSettings(settings: GlobalSettings): void;
24
+ createContext(name: string, options: {
25
+ endpoint: string;
26
+ }): ContextConfig;
27
+ loadContext(name: string): ContextConfig | null;
28
+ listContexts(): ContextConfig[];
29
+ deleteContext(name: string): boolean;
30
+ getCurrentContext(): ContextConfig | null;
31
+ setCurrentContext(name: string): boolean;
32
+ setCredentials(contextName: string, creds: ContextCredentials): void;
33
+ getCredentials(contextName: string): ContextCredentials | null;
34
+ removeCredentials(contextName: string): boolean;
35
+ hasValidCredentials(contextName: string): boolean;
36
+ }
37
+ export declare function createConfigStore(toolName: string, options?: ConfigStoreOptions): ConfigStore;
@@ -0,0 +1,158 @@
1
+ import * as fs from 'fs';
2
+ import * as path from 'path';
3
+ import { appstash, resolve } from './index';
4
+ const DEFAULT_SETTINGS = {};
5
+ function readJson(filePath, fallback) {
6
+ if (fs.existsSync(filePath)) {
7
+ try {
8
+ return JSON.parse(fs.readFileSync(filePath, 'utf8'));
9
+ }
10
+ catch {
11
+ return fallback;
12
+ }
13
+ }
14
+ return fallback;
15
+ }
16
+ function writeJson(filePath, data, mode) {
17
+ const dir = path.dirname(filePath);
18
+ if (!fs.existsSync(dir)) {
19
+ fs.mkdirSync(dir, { recursive: true });
20
+ }
21
+ const options = mode ? { mode } : {};
22
+ fs.writeFileSync(filePath, JSON.stringify(data, null, 2), options);
23
+ }
24
+ export function createConfigStore(toolName, options) {
25
+ const dirs = appstash(toolName, { ensure: true, baseDir: options?.baseDir });
26
+ function settingsPath() {
27
+ return resolve(dirs, 'config', 'settings.json');
28
+ }
29
+ function credentialsPath() {
30
+ return resolve(dirs, 'config', 'credentials.json');
31
+ }
32
+ function contextPath(name) {
33
+ const contextsDir = resolve(dirs, 'config', 'contexts');
34
+ if (!fs.existsSync(contextsDir)) {
35
+ fs.mkdirSync(contextsDir, { recursive: true });
36
+ }
37
+ return path.join(contextsDir, `${name}.json`);
38
+ }
39
+ function loadSettings() {
40
+ return readJson(settingsPath(), DEFAULT_SETTINGS);
41
+ }
42
+ function saveSettings(settings) {
43
+ writeJson(settingsPath(), settings);
44
+ }
45
+ function loadContext(name) {
46
+ return readJson(contextPath(name), null);
47
+ }
48
+ function createContext(name, options) {
49
+ const now = new Date().toISOString();
50
+ const context = {
51
+ name,
52
+ endpoint: options.endpoint,
53
+ createdAt: now,
54
+ updatedAt: now,
55
+ };
56
+ writeJson(contextPath(name), context);
57
+ return context;
58
+ }
59
+ function listContexts() {
60
+ const contextsDir = resolve(dirs, 'config', 'contexts');
61
+ if (!fs.existsSync(contextsDir)) {
62
+ return [];
63
+ }
64
+ const files = fs.readdirSync(contextsDir).filter(f => f.endsWith('.json'));
65
+ const contexts = [];
66
+ for (const file of files) {
67
+ try {
68
+ const content = fs.readFileSync(path.join(contextsDir, file), 'utf8');
69
+ contexts.push(JSON.parse(content));
70
+ }
71
+ catch {
72
+ // skip invalid files
73
+ }
74
+ }
75
+ return contexts;
76
+ }
77
+ function deleteContext(name) {
78
+ const p = contextPath(name);
79
+ if (fs.existsSync(p)) {
80
+ fs.unlinkSync(p);
81
+ const settings = loadSettings();
82
+ if (settings.currentContext === name) {
83
+ settings.currentContext = undefined;
84
+ saveSettings(settings);
85
+ }
86
+ return true;
87
+ }
88
+ return false;
89
+ }
90
+ function getCurrentContext() {
91
+ const settings = loadSettings();
92
+ if (settings.currentContext) {
93
+ return loadContext(settings.currentContext);
94
+ }
95
+ return null;
96
+ }
97
+ function setCurrentContext(name) {
98
+ const context = loadContext(name);
99
+ if (!context) {
100
+ return false;
101
+ }
102
+ const settings = loadSettings();
103
+ settings.currentContext = name;
104
+ saveSettings(settings);
105
+ return true;
106
+ }
107
+ function loadCredentials() {
108
+ return readJson(credentialsPath(), { tokens: {} });
109
+ }
110
+ function saveCredentials(credentials) {
111
+ writeJson(credentialsPath(), credentials, 0o600);
112
+ }
113
+ function setCredentials(contextName, creds) {
114
+ const credentials = loadCredentials();
115
+ credentials.tokens[contextName] = creds;
116
+ saveCredentials(credentials);
117
+ }
118
+ function getCredentials(contextName) {
119
+ const credentials = loadCredentials();
120
+ return credentials.tokens[contextName] || null;
121
+ }
122
+ function removeCredentials(contextName) {
123
+ const credentials = loadCredentials();
124
+ if (credentials.tokens[contextName]) {
125
+ delete credentials.tokens[contextName];
126
+ saveCredentials(credentials);
127
+ return true;
128
+ }
129
+ return false;
130
+ }
131
+ function hasValidCredentials(contextName) {
132
+ const creds = getCredentials(contextName);
133
+ if (!creds || !creds.token) {
134
+ return false;
135
+ }
136
+ if (creds.expiresAt) {
137
+ const expiresAt = new Date(creds.expiresAt);
138
+ if (expiresAt <= new Date()) {
139
+ return false;
140
+ }
141
+ }
142
+ return true;
143
+ }
144
+ return {
145
+ loadSettings,
146
+ saveSettings,
147
+ createContext,
148
+ loadContext,
149
+ listContexts,
150
+ deleteContext,
151
+ getCurrentContext,
152
+ setCurrentContext,
153
+ setCredentials,
154
+ getCredentials,
155
+ removeCredentials,
156
+ hasValidCredentials,
157
+ };
158
+ }
package/esm/index.d.ts CHANGED
@@ -97,3 +97,5 @@ export declare function ensure(dirs: AppStashResult): EnsureResult;
97
97
  * ```
98
98
  */
99
99
  export declare function resolve(dirs: AppStashResult, kind: 'config' | 'cache' | 'data' | 'logs' | 'tmp', ...parts: string[]): string;
100
+ export { createConfigStore } from './config-store';
101
+ export type { ConfigStore, ConfigStoreOptions, ContextConfig, ContextCredentials, Credentials, GlobalSettings, } from './config-store';
package/esm/index.js CHANGED
@@ -201,3 +201,4 @@ function ensureDirectories(dirs, useXdgFallback = true, tmpRoot = os.tmpdir(), t
201
201
  export function resolve(dirs, kind, ...parts) {
202
202
  return path.join(dirs[kind], ...parts);
203
203
  }
204
+ export { createConfigStore } from './config-store';
package/index.d.ts CHANGED
@@ -97,3 +97,5 @@ export declare function ensure(dirs: AppStashResult): EnsureResult;
97
97
  * ```
98
98
  */
99
99
  export declare function resolve(dirs: AppStashResult, kind: 'config' | 'cache' | 'data' | 'logs' | 'tmp', ...parts: string[]): string;
100
+ export { createConfigStore } from './config-store';
101
+ export type { ConfigStore, ConfigStoreOptions, ContextConfig, ContextCredentials, Credentials, GlobalSettings, } from './config-store';
package/index.js CHANGED
@@ -33,6 +33,7 @@ var __importStar = (this && this.__importStar) || (function () {
33
33
  };
34
34
  })();
35
35
  Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.createConfigStore = void 0;
36
37
  exports.appstash = appstash;
37
38
  exports.ensure = ensure;
38
39
  exports.resolve = resolve;
@@ -239,3 +240,5 @@ function ensureDirectories(dirs, useXdgFallback = true, tmpRoot = os.tmpdir(), t
239
240
  function resolve(dirs, kind, ...parts) {
240
241
  return path.join(dirs[kind], ...parts);
241
242
  }
243
+ var config_store_1 = require("./config-store");
244
+ Object.defineProperty(exports, "createConfigStore", { enumerable: true, get: function () { return config_store_1.createConfigStore; } });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "appstash",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "author": "Constructive <developers@constructive.io>",
5
5
  "description": "Simple, clean application directory resolution",
6
6
  "main": "index.js",
@@ -40,5 +40,5 @@
40
40
  "xdg",
41
41
  "appdirs"
42
42
  ],
43
- "gitHead": "c6f85c28e227126d86839ba1628748ba7d857da9"
43
+ "gitHead": "de740970766f110cad481ea900880a9b3120f9d5"
44
44
  }