@slates/profiles 1.0.0-rc.10
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/dist/index.cjs +462 -0
- package/dist/index.d.cts +126 -0
- package/dist/index.d.ts +126 -0
- package/dist/index.module.js +420 -0
- package/package.json +44 -0
- package/src/client.ts +158 -0
- package/src/index.ts +3 -0
- package/src/store.test.ts +533 -0
- package/src/store.ts +442 -0
- package/src/types.ts +67 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import * as _slates_client from '@slates/client';
|
|
2
|
+
import { SlatesJsonObject } from '@slates/client';
|
|
3
|
+
import { SlateAuthenticationMethod, SlatesAction } from '@slates/proto';
|
|
4
|
+
|
|
5
|
+
interface SlatesStoredAuth {
|
|
6
|
+
id: string;
|
|
7
|
+
authMethodId: string;
|
|
8
|
+
authMethodName?: string;
|
|
9
|
+
authType: SlateAuthenticationMethod['type'];
|
|
10
|
+
input: SlatesJsonObject;
|
|
11
|
+
output: SlatesJsonObject;
|
|
12
|
+
scopes: string[];
|
|
13
|
+
oauthCredentialId?: string;
|
|
14
|
+
clientId?: string;
|
|
15
|
+
clientSecret?: string;
|
|
16
|
+
callbackState?: SlatesJsonObject | null;
|
|
17
|
+
profile?: SlatesJsonObject | null;
|
|
18
|
+
createdAt: string;
|
|
19
|
+
updatedAt: string;
|
|
20
|
+
}
|
|
21
|
+
interface SlatesOAuthCredentialRecord {
|
|
22
|
+
id: string;
|
|
23
|
+
name: string;
|
|
24
|
+
authMethodId: string;
|
|
25
|
+
clientId: string;
|
|
26
|
+
clientSecret: string;
|
|
27
|
+
createdAt: string;
|
|
28
|
+
updatedAt: string;
|
|
29
|
+
}
|
|
30
|
+
interface SlatesLocalProfileTarget {
|
|
31
|
+
type: 'local';
|
|
32
|
+
entry: string;
|
|
33
|
+
exportName?: string;
|
|
34
|
+
}
|
|
35
|
+
type SlatesProfileTarget = SlatesLocalProfileTarget;
|
|
36
|
+
interface SlatesCliStoreScope {
|
|
37
|
+
key: string;
|
|
38
|
+
name?: string;
|
|
39
|
+
}
|
|
40
|
+
interface SlatesProfileRecord {
|
|
41
|
+
id: string;
|
|
42
|
+
name: string;
|
|
43
|
+
target: SlatesProfileTarget;
|
|
44
|
+
config: SlatesJsonObject | null;
|
|
45
|
+
auth: Record<string, SlatesStoredAuth>;
|
|
46
|
+
session: {
|
|
47
|
+
id: string;
|
|
48
|
+
state: SlatesJsonObject;
|
|
49
|
+
} | null;
|
|
50
|
+
metadata: {
|
|
51
|
+
provider?: SlatesJsonObject | null;
|
|
52
|
+
actions?: SlatesAction[] | null;
|
|
53
|
+
};
|
|
54
|
+
createdAt: string;
|
|
55
|
+
updatedAt: string;
|
|
56
|
+
}
|
|
57
|
+
interface SlatesCliStoreData {
|
|
58
|
+
version: 3;
|
|
59
|
+
currentProfileId: string | null;
|
|
60
|
+
profiles: Record<string, SlatesProfileRecord>;
|
|
61
|
+
oauthCredentials: Record<string, SlatesOAuthCredentialRecord>;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
declare let resolveSlatesCliRoot: (cwd?: string) => string;
|
|
65
|
+
declare let resolveSlatesCliDir: (cwd?: string) => string;
|
|
66
|
+
declare let resolveSlatesCliProfilesDir: (cwd?: string) => string;
|
|
67
|
+
declare class SlatesCliStore {
|
|
68
|
+
readonly rootDir: string;
|
|
69
|
+
readonly cliDir: string;
|
|
70
|
+
readonly dirPath: string;
|
|
71
|
+
readonly storePath: string;
|
|
72
|
+
readonly scope: SlatesCliStoreScope | null;
|
|
73
|
+
readonly data: SlatesCliStoreData;
|
|
74
|
+
constructor(rootDir: string, cliDir: string, dirPath: string, storePath: string, scope: SlatesCliStoreScope | null, data: SlatesCliStoreData);
|
|
75
|
+
static open(opts?: {
|
|
76
|
+
cwd?: string;
|
|
77
|
+
rootDir?: string;
|
|
78
|
+
scope?: SlatesCliStoreScope;
|
|
79
|
+
storePath?: string;
|
|
80
|
+
}): Promise<SlatesCliStore>;
|
|
81
|
+
save(): Promise<void>;
|
|
82
|
+
listProfiles(): SlatesProfileRecord[];
|
|
83
|
+
listOAuthCredentials(authMethodId?: string): SlatesOAuthCredentialRecord[];
|
|
84
|
+
getOAuthCredential(credentialId?: string | null, authMethodId?: string | null): SlatesOAuthCredentialRecord;
|
|
85
|
+
getProfile(profileId?: string | null): SlatesProfileRecord;
|
|
86
|
+
requireProfile(profileId?: string | null): SlatesProfileRecord;
|
|
87
|
+
upsertProfile(d: {
|
|
88
|
+
profileId?: string;
|
|
89
|
+
name: string;
|
|
90
|
+
target: SlatesProfileTarget;
|
|
91
|
+
config?: SlatesProfileRecord['config'];
|
|
92
|
+
session?: SlatesProfileRecord['session'];
|
|
93
|
+
metadata?: SlatesProfileRecord['metadata'];
|
|
94
|
+
}): SlatesProfileRecord;
|
|
95
|
+
upsertOAuthCredential(d: {
|
|
96
|
+
credentialId?: string;
|
|
97
|
+
name: string;
|
|
98
|
+
authMethodId: string;
|
|
99
|
+
clientId: string;
|
|
100
|
+
clientSecret: string;
|
|
101
|
+
}): SlatesOAuthCredentialRecord;
|
|
102
|
+
setCurrentProfile(profileId: string): void;
|
|
103
|
+
removeProfile(profileId: string): void;
|
|
104
|
+
setProfileConfig(profileId: string, config: SlatesProfileRecord['config']): SlatesProfileRecord;
|
|
105
|
+
setProfileSession(profileId: string, session: SlatesProfileRecord['session']): SlatesProfileRecord;
|
|
106
|
+
setProfileMetadata(profileId: string, metadata: Partial<SlatesProfileRecord['metadata']>): SlatesProfileRecord;
|
|
107
|
+
upsertAuth(profileId: string, auth: Omit<SlatesStoredAuth, 'id' | 'createdAt' | 'updatedAt'> & {
|
|
108
|
+
id?: string;
|
|
109
|
+
}): SlatesStoredAuth;
|
|
110
|
+
getAuth(profileId: string, authMethodId?: string | null): SlatesStoredAuth;
|
|
111
|
+
listAuth(profileId: string): SlatesStoredAuth[];
|
|
112
|
+
}
|
|
113
|
+
declare let openSlatesCliStore: (opts?: {
|
|
114
|
+
cwd?: string;
|
|
115
|
+
rootDir?: string;
|
|
116
|
+
scope?: SlatesCliStoreScope;
|
|
117
|
+
storePath?: string;
|
|
118
|
+
}) => Promise<SlatesCliStore>;
|
|
119
|
+
|
|
120
|
+
declare let createSlatesClientFromProfile: (profile: SlatesProfileRecord, opts?: {
|
|
121
|
+
cwd?: string;
|
|
122
|
+
store?: SlatesCliStore;
|
|
123
|
+
autoRefresh?: boolean;
|
|
124
|
+
}) => Promise<_slates_client.SlatesProtocolClient>;
|
|
125
|
+
|
|
126
|
+
export { SlatesCliStore, type SlatesCliStoreData, type SlatesCliStoreScope, type SlatesLocalProfileTarget, type SlatesOAuthCredentialRecord, type SlatesProfileRecord, type SlatesProfileTarget, type SlatesStoredAuth, createSlatesClientFromProfile, openSlatesCliStore, resolveSlatesCliDir, resolveSlatesCliProfilesDir, resolveSlatesCliRoot };
|
|
@@ -0,0 +1,420 @@
|
|
|
1
|
+
// src/client.ts
|
|
2
|
+
import { createLocalSlateTransport, createSlatesClient } from "@slates/client";
|
|
3
|
+
import path2 from "path";
|
|
4
|
+
import { pathToFileURL } from "url";
|
|
5
|
+
|
|
6
|
+
// src/store.ts
|
|
7
|
+
import { execFileSync } from "child_process";
|
|
8
|
+
import { randomUUID } from "crypto";
|
|
9
|
+
import { mkdir, readFile, writeFile } from "fs/promises";
|
|
10
|
+
import path from "path";
|
|
11
|
+
var STORE_VERSION = 3;
|
|
12
|
+
var CLI_DIR_NAME = ".slates-cli";
|
|
13
|
+
var PROFILES_DIR_NAME = "profiles";
|
|
14
|
+
var STORE_FILE_NAME = "store.json";
|
|
15
|
+
var GITIGNORE_FILE_NAME = ".gitignore";
|
|
16
|
+
var createEmptyStore = () => ({
|
|
17
|
+
version: STORE_VERSION,
|
|
18
|
+
currentProfileId: null,
|
|
19
|
+
profiles: {},
|
|
20
|
+
oauthCredentials: {}
|
|
21
|
+
});
|
|
22
|
+
var now = () => (/* @__PURE__ */ new Date()).toISOString();
|
|
23
|
+
var ensureDir = async (dirPath) => {
|
|
24
|
+
await mkdir(dirPath, { recursive: true });
|
|
25
|
+
};
|
|
26
|
+
var ensureGitIgnore = async (dirPath) => {
|
|
27
|
+
let filePath = path.join(dirPath, GITIGNORE_FILE_NAME);
|
|
28
|
+
await writeFile(filePath, "*\n!.gitignore\n", { encoding: "utf-8" }).catch(async (error) => {
|
|
29
|
+
if (error.code === "EEXIST") return;
|
|
30
|
+
throw error;
|
|
31
|
+
});
|
|
32
|
+
};
|
|
33
|
+
var resolveSlatesCliRoot = (cwd = process.cwd()) => {
|
|
34
|
+
try {
|
|
35
|
+
return execFileSync("git", ["rev-parse", "--show-toplevel"], {
|
|
36
|
+
cwd,
|
|
37
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
38
|
+
encoding: "utf-8"
|
|
39
|
+
}).trim();
|
|
40
|
+
} catch {
|
|
41
|
+
return cwd;
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
var resolveSlatesCliDirFromRoot = (rootDir) => path.join(rootDir, CLI_DIR_NAME);
|
|
45
|
+
var resolveSlatesCliProfilesDirFromRoot = (rootDir) => path.join(resolveSlatesCliDirFromRoot(rootDir), PROFILES_DIR_NAME);
|
|
46
|
+
var resolveSlatesCliDir = (cwd = process.cwd()) => resolveSlatesCliDirFromRoot(resolveSlatesCliRoot(cwd));
|
|
47
|
+
var resolveSlatesCliProfilesDir = (cwd = process.cwd()) => resolveSlatesCliProfilesDirFromRoot(resolveSlatesCliRoot(cwd));
|
|
48
|
+
var normalizeScopeKey = (scopeKey) => path.posix.normalize(scopeKey.replace(/\\/g, "/")).replace(/^\.?\//, "");
|
|
49
|
+
var splitScopeKey = (scopeKey) => normalizeScopeKey(scopeKey).split("/").filter(Boolean).filter((segment) => segment !== "." && segment !== "..");
|
|
50
|
+
var resolveScopedStoreDir = (rootDir, scopeKey) => path.join(resolveSlatesCliProfilesDirFromRoot(rootDir), ...splitScopeKey(scopeKey));
|
|
51
|
+
var resolveScopedStorePath = (rootDir, scopeKey) => path.join(resolveScopedStoreDir(rootDir, scopeKey), STORE_FILE_NAME);
|
|
52
|
+
var inferRootDirFromStorePath = (storePath) => {
|
|
53
|
+
let normalized = path.resolve(storePath);
|
|
54
|
+
let parts = normalized.split(path.sep).filter(Boolean);
|
|
55
|
+
let cliDirIndex = parts.lastIndexOf(CLI_DIR_NAME);
|
|
56
|
+
if (cliDirIndex === -1) {
|
|
57
|
+
throw new Error(`Could not infer Slates CLI root from store path: ${storePath}`);
|
|
58
|
+
}
|
|
59
|
+
let rootParts = parts.slice(0, cliDirIndex);
|
|
60
|
+
return path.join(path.sep, ...rootParts);
|
|
61
|
+
};
|
|
62
|
+
var resolveScopeKeyFromStorePath = (rootDir, storePath) => {
|
|
63
|
+
let profilesDir = resolveSlatesCliProfilesDirFromRoot(rootDir);
|
|
64
|
+
let relativeDir = path.relative(profilesDir, path.dirname(storePath));
|
|
65
|
+
if (!relativeDir || relativeDir.startsWith("..") || path.isAbsolute(relativeDir)) {
|
|
66
|
+
return null;
|
|
67
|
+
}
|
|
68
|
+
return normalizeScopeKey(relativeDir);
|
|
69
|
+
};
|
|
70
|
+
var readStoreData = async (storePath) => {
|
|
71
|
+
try {
|
|
72
|
+
let raw = await readFile(storePath, "utf-8");
|
|
73
|
+
if (!raw.trim()) {
|
|
74
|
+
return null;
|
|
75
|
+
}
|
|
76
|
+
return JSON.parse(raw);
|
|
77
|
+
} catch (error) {
|
|
78
|
+
if (error.code === "ENOENT") {
|
|
79
|
+
return null;
|
|
80
|
+
}
|
|
81
|
+
throw error;
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
var isProfileInScope = (rootDir, scopeKey, profile) => {
|
|
85
|
+
if (profile.target.type !== "local") {
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
let scopeDir = path.resolve(rootDir, ...splitScopeKey(scopeKey));
|
|
89
|
+
let entryPath = path.isAbsolute(profile.target.entry) ? profile.target.entry : path.resolve(rootDir, profile.target.entry);
|
|
90
|
+
let relative = path.relative(scopeDir, entryPath);
|
|
91
|
+
return relative !== "" && !relative.startsWith("..") && !path.isAbsolute(relative);
|
|
92
|
+
};
|
|
93
|
+
var loadMigratedLegacyStore = async (rootDir, scopeKey) => {
|
|
94
|
+
let legacyStorePath = path.join(resolveSlatesCliDirFromRoot(rootDir), STORE_FILE_NAME);
|
|
95
|
+
let legacy = await readStoreData(legacyStorePath);
|
|
96
|
+
if (!legacy) {
|
|
97
|
+
return createEmptyStore();
|
|
98
|
+
}
|
|
99
|
+
let profiles = Object.fromEntries(
|
|
100
|
+
Object.entries(legacy.profiles ?? {}).filter(
|
|
101
|
+
([, profile]) => isProfileInScope(rootDir, scopeKey, profile)
|
|
102
|
+
)
|
|
103
|
+
);
|
|
104
|
+
let currentProfileId = legacy.currentProfileId && profiles[legacy.currentProfileId] ? legacy.currentProfileId : null;
|
|
105
|
+
return {
|
|
106
|
+
version: STORE_VERSION,
|
|
107
|
+
currentProfileId,
|
|
108
|
+
profiles,
|
|
109
|
+
oauthCredentials: {}
|
|
110
|
+
};
|
|
111
|
+
};
|
|
112
|
+
var SlatesCliStore = class _SlatesCliStore {
|
|
113
|
+
constructor(rootDir, cliDir, dirPath, storePath, scope, data) {
|
|
114
|
+
this.rootDir = rootDir;
|
|
115
|
+
this.cliDir = cliDir;
|
|
116
|
+
this.dirPath = dirPath;
|
|
117
|
+
this.storePath = storePath;
|
|
118
|
+
this.scope = scope;
|
|
119
|
+
this.data = data;
|
|
120
|
+
}
|
|
121
|
+
rootDir;
|
|
122
|
+
cliDir;
|
|
123
|
+
dirPath;
|
|
124
|
+
storePath;
|
|
125
|
+
scope;
|
|
126
|
+
data;
|
|
127
|
+
static async open(opts = {}) {
|
|
128
|
+
let storageRootDir = opts.storePath ? inferRootDirFromStorePath(opts.storePath) : resolveSlatesCliRoot(opts.cwd);
|
|
129
|
+
let rootDir = opts.rootDir ?? storageRootDir;
|
|
130
|
+
let cliDir = resolveSlatesCliDirFromRoot(storageRootDir);
|
|
131
|
+
let scopeKey = opts.storePath ? resolveScopeKeyFromStorePath(storageRootDir, opts.storePath) : opts.scope?.key ? normalizeScopeKey(opts.scope.key) : null;
|
|
132
|
+
let dirPath = opts.storePath ? path.dirname(opts.storePath) : scopeKey ? resolveScopedStoreDir(rootDir, scopeKey) : cliDir;
|
|
133
|
+
let storePath = opts.storePath ?? (scopeKey ? resolveScopedStorePath(rootDir, scopeKey) : path.join(cliDir, STORE_FILE_NAME));
|
|
134
|
+
await ensureDir(cliDir);
|
|
135
|
+
await ensureGitIgnore(cliDir);
|
|
136
|
+
await ensureDir(dirPath);
|
|
137
|
+
let parsed = await readStoreData(storePath);
|
|
138
|
+
let data = parsed ? {
|
|
139
|
+
version: STORE_VERSION,
|
|
140
|
+
currentProfileId: parsed.currentProfileId ?? null,
|
|
141
|
+
profiles: parsed.profiles ?? {},
|
|
142
|
+
oauthCredentials: parsed.oauthCredentials ?? {}
|
|
143
|
+
} : opts.storePath ? createEmptyStore() : scopeKey ? await loadMigratedLegacyStore(rootDir, scopeKey) : createEmptyStore();
|
|
144
|
+
if (!parsed) {
|
|
145
|
+
await writeFile(storePath, `${JSON.stringify(data, null, 2)}
|
|
146
|
+
`, "utf-8");
|
|
147
|
+
}
|
|
148
|
+
return new _SlatesCliStore(
|
|
149
|
+
rootDir,
|
|
150
|
+
cliDir,
|
|
151
|
+
dirPath,
|
|
152
|
+
storePath,
|
|
153
|
+
scopeKey ? { key: scopeKey, name: opts.scope?.name } : null,
|
|
154
|
+
data
|
|
155
|
+
);
|
|
156
|
+
}
|
|
157
|
+
async save() {
|
|
158
|
+
await ensureDir(this.dirPath);
|
|
159
|
+
await ensureDir(this.cliDir);
|
|
160
|
+
await ensureGitIgnore(this.cliDir);
|
|
161
|
+
await writeFile(this.storePath, `${JSON.stringify(this.data, null, 2)}
|
|
162
|
+
`, "utf-8");
|
|
163
|
+
}
|
|
164
|
+
listProfiles() {
|
|
165
|
+
return Object.values(this.data.profiles).sort((a, b) => a.name.localeCompare(b.name));
|
|
166
|
+
}
|
|
167
|
+
listOAuthCredentials(authMethodId) {
|
|
168
|
+
return Object.values(this.data.oauthCredentials).filter((credential) => !authMethodId || credential.authMethodId === authMethodId).sort((a, b) => a.name.localeCompare(b.name));
|
|
169
|
+
}
|
|
170
|
+
getOAuthCredential(credentialId, authMethodId) {
|
|
171
|
+
if (credentialId) {
|
|
172
|
+
let direct = this.data.oauthCredentials[credentialId];
|
|
173
|
+
if (direct && (!authMethodId || direct.authMethodId === authMethodId)) {
|
|
174
|
+
return direct;
|
|
175
|
+
}
|
|
176
|
+
let matches = this.listOAuthCredentials(authMethodId ?? void 0).filter(
|
|
177
|
+
(credential) => credential.name === credentialId
|
|
178
|
+
);
|
|
179
|
+
if (matches.length === 1) {
|
|
180
|
+
return matches[0];
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
return this.listOAuthCredentials(authMethodId ?? void 0)[0] ?? null;
|
|
184
|
+
}
|
|
185
|
+
getProfile(profileId) {
|
|
186
|
+
if (profileId) {
|
|
187
|
+
if (this.data.profiles[profileId]) {
|
|
188
|
+
return this.data.profiles[profileId] ?? null;
|
|
189
|
+
}
|
|
190
|
+
let matches = this.listProfiles().filter((profile) => profile.name === profileId);
|
|
191
|
+
if (matches.length === 1) {
|
|
192
|
+
return matches[0];
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
if (this.data.currentProfileId && this.data.profiles[this.data.currentProfileId]) {
|
|
196
|
+
return this.data.profiles[this.data.currentProfileId];
|
|
197
|
+
}
|
|
198
|
+
return this.listProfiles()[0] ?? null;
|
|
199
|
+
}
|
|
200
|
+
requireProfile(profileId) {
|
|
201
|
+
let profile = this.getProfile(profileId);
|
|
202
|
+
if (!profile) {
|
|
203
|
+
throw new Error("No Slates profile found for this integration.");
|
|
204
|
+
}
|
|
205
|
+
return profile;
|
|
206
|
+
}
|
|
207
|
+
upsertProfile(d) {
|
|
208
|
+
let existing = d.profileId ? this.data.profiles[d.profileId] : void 0;
|
|
209
|
+
let id = existing?.id ?? d.profileId ?? randomUUID();
|
|
210
|
+
let createdAt = existing?.createdAt ?? now();
|
|
211
|
+
let profile = {
|
|
212
|
+
id,
|
|
213
|
+
name: d.name,
|
|
214
|
+
target: d.target,
|
|
215
|
+
config: d.config ?? existing?.config ?? null,
|
|
216
|
+
auth: existing?.auth ?? {},
|
|
217
|
+
session: d.session ?? existing?.session ?? null,
|
|
218
|
+
metadata: {
|
|
219
|
+
provider: d.metadata?.provider ?? existing?.metadata?.provider ?? null,
|
|
220
|
+
actions: d.metadata?.actions ?? existing?.metadata?.actions ?? null
|
|
221
|
+
},
|
|
222
|
+
createdAt,
|
|
223
|
+
updatedAt: now()
|
|
224
|
+
};
|
|
225
|
+
this.data.profiles[id] = profile;
|
|
226
|
+
if (!this.data.currentProfileId) {
|
|
227
|
+
this.data.currentProfileId = id;
|
|
228
|
+
}
|
|
229
|
+
return profile;
|
|
230
|
+
}
|
|
231
|
+
upsertOAuthCredential(d) {
|
|
232
|
+
let existing = d.credentialId ? this.data.oauthCredentials[d.credentialId] : void 0;
|
|
233
|
+
let id = existing?.id ?? d.credentialId ?? randomUUID();
|
|
234
|
+
let credential = {
|
|
235
|
+
id,
|
|
236
|
+
name: d.name,
|
|
237
|
+
authMethodId: d.authMethodId,
|
|
238
|
+
clientId: d.clientId,
|
|
239
|
+
clientSecret: d.clientSecret,
|
|
240
|
+
createdAt: existing?.createdAt ?? now(),
|
|
241
|
+
updatedAt: now()
|
|
242
|
+
};
|
|
243
|
+
this.data.oauthCredentials[id] = credential;
|
|
244
|
+
return credential;
|
|
245
|
+
}
|
|
246
|
+
setCurrentProfile(profileId) {
|
|
247
|
+
if (!this.data.profiles[profileId]) {
|
|
248
|
+
throw new Error(`Unknown profile: ${profileId}`);
|
|
249
|
+
}
|
|
250
|
+
this.data.currentProfileId = profileId;
|
|
251
|
+
}
|
|
252
|
+
removeProfile(profileId) {
|
|
253
|
+
delete this.data.profiles[profileId];
|
|
254
|
+
if (this.data.currentProfileId === profileId) {
|
|
255
|
+
this.data.currentProfileId = this.listProfiles()[0]?.id ?? null;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
setProfileConfig(profileId, config) {
|
|
259
|
+
let profile = this.requireProfile(profileId);
|
|
260
|
+
profile.config = config;
|
|
261
|
+
profile.updatedAt = now();
|
|
262
|
+
return profile;
|
|
263
|
+
}
|
|
264
|
+
setProfileSession(profileId, session) {
|
|
265
|
+
let profile = this.requireProfile(profileId);
|
|
266
|
+
profile.session = session;
|
|
267
|
+
profile.updatedAt = now();
|
|
268
|
+
return profile;
|
|
269
|
+
}
|
|
270
|
+
setProfileMetadata(profileId, metadata) {
|
|
271
|
+
let profile = this.requireProfile(profileId);
|
|
272
|
+
profile.metadata = {
|
|
273
|
+
provider: metadata.provider ?? profile.metadata.provider ?? null,
|
|
274
|
+
actions: metadata.actions ?? profile.metadata.actions ?? null
|
|
275
|
+
};
|
|
276
|
+
profile.updatedAt = now();
|
|
277
|
+
return profile;
|
|
278
|
+
}
|
|
279
|
+
upsertAuth(profileId, auth) {
|
|
280
|
+
let profile = this.requireProfile(profileId);
|
|
281
|
+
let existing = profile.auth[auth.authMethodId];
|
|
282
|
+
profile.auth[auth.authMethodId] = {
|
|
283
|
+
...auth,
|
|
284
|
+
id: auth.id ?? existing?.id ?? randomUUID(),
|
|
285
|
+
createdAt: existing?.createdAt ?? now(),
|
|
286
|
+
updatedAt: now()
|
|
287
|
+
};
|
|
288
|
+
profile.updatedAt = now();
|
|
289
|
+
return profile.auth[auth.authMethodId];
|
|
290
|
+
}
|
|
291
|
+
getAuth(profileId, authMethodId) {
|
|
292
|
+
let profile = this.requireProfile(profileId);
|
|
293
|
+
if (authMethodId) return profile.auth[authMethodId] ?? null;
|
|
294
|
+
return Object.values(profile.auth)[0] ?? null;
|
|
295
|
+
}
|
|
296
|
+
listAuth(profileId) {
|
|
297
|
+
let profile = this.requireProfile(profileId);
|
|
298
|
+
return Object.values(profile.auth).sort(
|
|
299
|
+
(a, b) => a.authMethodId.localeCompare(b.authMethodId)
|
|
300
|
+
);
|
|
301
|
+
}
|
|
302
|
+
};
|
|
303
|
+
var openSlatesCliStore = async (opts = {}) => SlatesCliStore.open(opts);
|
|
304
|
+
|
|
305
|
+
// src/client.ts
|
|
306
|
+
var resolveEntryPath = (entry, opts = {}) => {
|
|
307
|
+
if (path2.isAbsolute(entry)) return entry;
|
|
308
|
+
return path2.resolve(opts.rootDir ?? resolveSlatesCliRoot(opts.cwd), entry);
|
|
309
|
+
};
|
|
310
|
+
var loadSlateFromProfile = async (profile, opts = {}) => {
|
|
311
|
+
if (profile.target.type !== "local") {
|
|
312
|
+
throw new Error(`Unsupported profile target type: ${profile.target.type}`);
|
|
313
|
+
}
|
|
314
|
+
let modulePath = resolveEntryPath(profile.target.entry, {
|
|
315
|
+
cwd: opts.cwd,
|
|
316
|
+
rootDir: opts.store?.rootDir
|
|
317
|
+
});
|
|
318
|
+
let loaded = await import(pathToFileURL(modulePath).href);
|
|
319
|
+
let exportName = profile.target.exportName;
|
|
320
|
+
let slate = (exportName ? loaded[exportName] : void 0) ?? loaded.provider ?? loaded.slate ?? loaded.default;
|
|
321
|
+
if (!slate) {
|
|
322
|
+
throw new Error(
|
|
323
|
+
`Could not find a slate export in ${profile.target.entry}. Tried ${exportName ? `\`${exportName}\`, ` : ""}\`provider\`, \`slate\`, and \`default\`.`
|
|
324
|
+
);
|
|
325
|
+
}
|
|
326
|
+
return slate;
|
|
327
|
+
};
|
|
328
|
+
var createSlatesClientFromProfile = async (profile, opts = {}) => {
|
|
329
|
+
let firstAuth = Object.values(profile.auth)[0];
|
|
330
|
+
let slate = await loadSlateFromProfile(profile, opts);
|
|
331
|
+
let client = createSlatesClient({
|
|
332
|
+
transport: createLocalSlateTransport({ slate }),
|
|
333
|
+
state: {
|
|
334
|
+
config: profile.config,
|
|
335
|
+
session: profile.session,
|
|
336
|
+
auth: firstAuth ? {
|
|
337
|
+
authenticationMethodId: firstAuth.authMethodId,
|
|
338
|
+
output: firstAuth.output
|
|
339
|
+
} : null
|
|
340
|
+
}
|
|
341
|
+
});
|
|
342
|
+
if (firstAuth && opts.autoRefresh !== false) {
|
|
343
|
+
attachAutoRefresh(client, profile, firstAuth, opts.store);
|
|
344
|
+
}
|
|
345
|
+
return client;
|
|
346
|
+
};
|
|
347
|
+
var EXPIRY_BUFFER_MS = 60 * 1e3;
|
|
348
|
+
var hasExpiredOrExpiringOutput = (auth) => {
|
|
349
|
+
if (typeof auth.output?.expiresAt !== "string" || !auth.output.expiresAt) return false;
|
|
350
|
+
let expiresAt = Date.parse(auth.output.expiresAt);
|
|
351
|
+
return Number.isFinite(expiresAt) && expiresAt <= Date.now() + EXPIRY_BUFFER_MS;
|
|
352
|
+
};
|
|
353
|
+
var shouldRefreshAuth = (auth) => {
|
|
354
|
+
if (!hasExpiredOrExpiringOutput(auth)) return false;
|
|
355
|
+
if (auth.authType === "auth.oauth") {
|
|
356
|
+
if (!auth.clientId || !auth.clientSecret) return false;
|
|
357
|
+
if (typeof auth.output?.refreshToken !== "string" || !auth.output.refreshToken)
|
|
358
|
+
return false;
|
|
359
|
+
}
|
|
360
|
+
return true;
|
|
361
|
+
};
|
|
362
|
+
var attachAutoRefresh = (client, profile, storedAuth, store) => {
|
|
363
|
+
let baseRequest = client.request.bind(client);
|
|
364
|
+
let inflightRefresh = null;
|
|
365
|
+
let refreshIfNeeded = async () => {
|
|
366
|
+
if (inflightRefresh) {
|
|
367
|
+
await inflightRefresh;
|
|
368
|
+
return;
|
|
369
|
+
}
|
|
370
|
+
if (!shouldRefreshAuth(storedAuth)) {
|
|
371
|
+
return;
|
|
372
|
+
}
|
|
373
|
+
inflightRefresh = (async () => {
|
|
374
|
+
let authMethod = await baseRequest("slates/auth.method.get", {
|
|
375
|
+
authenticationMethodId: storedAuth.authMethodId
|
|
376
|
+
});
|
|
377
|
+
if (!authMethod.authenticationMethod.capabilities.handleTokenRefresh?.enabled) {
|
|
378
|
+
return;
|
|
379
|
+
}
|
|
380
|
+
let refreshed = await baseRequest("slates/auth.token_refresh.handle", {
|
|
381
|
+
authenticationMethodId: storedAuth.authMethodId,
|
|
382
|
+
output: storedAuth.output,
|
|
383
|
+
input: storedAuth.input,
|
|
384
|
+
clientId: storedAuth.clientId ?? "",
|
|
385
|
+
clientSecret: storedAuth.clientSecret ?? "",
|
|
386
|
+
scopes: storedAuth.scopes
|
|
387
|
+
});
|
|
388
|
+
storedAuth.input = refreshed.input ?? storedAuth.input;
|
|
389
|
+
storedAuth.output = refreshed.output;
|
|
390
|
+
storedAuth.updatedAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
391
|
+
client.setAuth({
|
|
392
|
+
authenticationMethodId: storedAuth.authMethodId,
|
|
393
|
+
output: storedAuth.output
|
|
394
|
+
});
|
|
395
|
+
if (store) {
|
|
396
|
+
store.upsertAuth(profile.id, storedAuth);
|
|
397
|
+
await store.save();
|
|
398
|
+
}
|
|
399
|
+
})();
|
|
400
|
+
try {
|
|
401
|
+
await inflightRefresh;
|
|
402
|
+
} finally {
|
|
403
|
+
inflightRefresh = null;
|
|
404
|
+
}
|
|
405
|
+
};
|
|
406
|
+
client.request = (async (method, params) => {
|
|
407
|
+
if (method !== "slates/auth.token_refresh.handle") {
|
|
408
|
+
await refreshIfNeeded();
|
|
409
|
+
}
|
|
410
|
+
return baseRequest(method, params);
|
|
411
|
+
});
|
|
412
|
+
};
|
|
413
|
+
export {
|
|
414
|
+
SlatesCliStore,
|
|
415
|
+
createSlatesClientFromProfile,
|
|
416
|
+
openSlatesCliStore,
|
|
417
|
+
resolveSlatesCliDir,
|
|
418
|
+
resolveSlatesCliProfilesDir,
|
|
419
|
+
resolveSlatesCliRoot
|
|
420
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@slates/profiles",
|
|
3
|
+
"version": "1.0.0-rc.10",
|
|
4
|
+
"publishConfig": {
|
|
5
|
+
"access": "public"
|
|
6
|
+
},
|
|
7
|
+
"files": [
|
|
8
|
+
"src/**",
|
|
9
|
+
"dist/**",
|
|
10
|
+
"README.md",
|
|
11
|
+
"package.json"
|
|
12
|
+
],
|
|
13
|
+
"author": "Tobias Herber",
|
|
14
|
+
"license": "FSL 1.1",
|
|
15
|
+
"type": "module",
|
|
16
|
+
"source": "src/index.ts",
|
|
17
|
+
"exports": {
|
|
18
|
+
"types": "./dist/index.d.ts",
|
|
19
|
+
"require": "./dist/index.cjs",
|
|
20
|
+
"import": "./dist/index.module.js",
|
|
21
|
+
"default": "./dist/index.module.js"
|
|
22
|
+
},
|
|
23
|
+
"main": "./dist/index.cjs",
|
|
24
|
+
"module": "./dist/index.module.js",
|
|
25
|
+
"types": "dist/index.d.ts",
|
|
26
|
+
"unpkg": "./dist/index.module.js",
|
|
27
|
+
"scripts": {
|
|
28
|
+
"test": "vitest run --passWithNoTests",
|
|
29
|
+
"build": "tsup --config ../../tsup.packages.config.ts --external async_hooks",
|
|
30
|
+
"typecheck": "tsc --noEmit",
|
|
31
|
+
"biome:check": "biome check --write src"
|
|
32
|
+
},
|
|
33
|
+
"dependencies": {
|
|
34
|
+
"@slates/client": "1.0.0-rc.12",
|
|
35
|
+
"@slates/proto": "1.0.0-rc.11"
|
|
36
|
+
},
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"@slates/provider": "1.0.0-rc.15",
|
|
39
|
+
"@slates/tsconfig": "1.0.0-rc.1",
|
|
40
|
+
"typescript": "5.8.2",
|
|
41
|
+
"vitest": "^3.1.2",
|
|
42
|
+
"zod": "^4.2.1"
|
|
43
|
+
}
|
|
44
|
+
}
|