@run402/sdk 3.5.4 → 3.5.6
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 +30 -1
- package/core-dist/config.d.ts +19 -0
- package/core-dist/config.js +103 -4
- package/dist/namespaces/deploy.d.ts.map +1 -1
- package/dist/namespaces/deploy.js +281 -21
- package/dist/namespaces/deploy.js.map +1 -1
- package/dist/namespaces/deploy.types.d.ts +16 -1
- package/dist/namespaces/deploy.types.d.ts.map +1 -1
- package/dist/namespaces/deploy.types.js.map +1 -1
- package/dist/namespaces/projects.d.ts.map +1 -1
- package/dist/namespaces/projects.js +1 -0
- package/dist/namespaces/projects.js.map +1 -1
- package/dist/namespaces/projects.types.d.ts +7 -0
- package/dist/namespaces/projects.types.d.ts.map +1 -1
- package/dist/node/deploy-manifest.d.ts +5 -3
- package/dist/node/deploy-manifest.d.ts.map +1 -1
- package/dist/node/deploy-manifest.js +16 -2
- package/dist/node/deploy-manifest.js.map +1 -1
- package/dist/node/index.d.ts +2 -0
- package/dist/node/index.d.ts.map +1 -1
- package/dist/node/index.js +1 -0
- package/dist/node/index.js.map +1 -1
- package/dist/node/target-profile.d.ts +40 -0
- package/dist/node/target-profile.d.ts.map +1 -0
- package/dist/node/target-profile.js +98 -0
- package/dist/node/target-profile.js.map +1 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -10,7 +10,7 @@ npm install @run402/sdk
|
|
|
10
10
|
|
|
11
11
|
| Import | Use when |
|
|
12
12
|
|---|---|
|
|
13
|
-
| `@run402/sdk/node` | Running in Node 22 with the local keystore + allowance. Auto-loads `~/.config/run402/projects.json
|
|
13
|
+
| `@run402/sdk/node` | Running in Node 22 with the local keystore + allowance. Auto-loads the configured API base, `~/.config/run402/projects.json`, and signs x402 payments from `~/.config/run402/allowance.json`. Includes `r.sites.deployDir(dir)`, `fileSetFromDir(dir)`, `loadDeployManifest(path)`, `normalizeDeployManifest(input)`, and `resolveRun402TargetProfile()`. |
|
|
14
14
|
| `@run402/sdk` | Isomorphic — works in Node, Deno, Bun, V8 isolates. No filesystem access. Bring your own `CredentialsProvider` (a session-token shim, a remote vault, anything that resolves project keys + auth headers). |
|
|
15
15
|
|
|
16
16
|
## Quick start (Node)
|
|
@@ -25,6 +25,35 @@ await (await r.project(project.project_id)).assets.put("hello.txt", { content: "
|
|
|
25
25
|
|
|
26
26
|
That's it — credentials are read, x402 payments are signed, results are typed.
|
|
27
27
|
|
|
28
|
+
For a self-hosted Run402 Core Gateway, run `run402 init --api-base=http://my-core:4020` once. The Node SDK then targets that API base by default; explicit `run402({ apiBase })` still wins.
|
|
29
|
+
|
|
30
|
+
App build scripts should use the same target/profile store instead of parsing `target.json` or `projects.json`:
|
|
31
|
+
|
|
32
|
+
```ts
|
|
33
|
+
import { resolveRun402TargetProfile } from "@run402/sdk/node";
|
|
34
|
+
|
|
35
|
+
const target = resolveRun402TargetProfile({
|
|
36
|
+
requiredTarget: "core",
|
|
37
|
+
requireProject: true,
|
|
38
|
+
requireAnonKey: true,
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
console.log(target.apiBase, target.projectId, target.anonKey);
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
For app-specific legacy env names, pass aliases:
|
|
45
|
+
|
|
46
|
+
```ts
|
|
47
|
+
import { resolveRun402TargetProfile } from "@run402/sdk/node";
|
|
48
|
+
|
|
49
|
+
resolveRun402TargetProfile({
|
|
50
|
+
envAliases: {
|
|
51
|
+
projectId: ["MY_APP_PROJECT_ID"],
|
|
52
|
+
anonKey: ["MY_APP_ANON_KEY"],
|
|
53
|
+
},
|
|
54
|
+
});
|
|
55
|
+
```
|
|
56
|
+
|
|
28
57
|
### Project-scoped sub-client
|
|
29
58
|
|
|
30
59
|
Most operations are project-scoped. Bind once and skip the id arg on every call:
|
package/core-dist/config.d.ts
CHANGED
|
@@ -1,4 +1,14 @@
|
|
|
1
|
+
export declare const DEFAULT_API_BASE = "https://api.run402.com";
|
|
2
|
+
export type ApiTargetKind = "cloud" | "core" | "unknown";
|
|
3
|
+
export interface ApiTargetConfig {
|
|
4
|
+
api_base?: string;
|
|
5
|
+
target_kind?: ApiTargetKind;
|
|
6
|
+
updated_at?: string;
|
|
7
|
+
health_status?: string;
|
|
8
|
+
health_error?: string;
|
|
9
|
+
}
|
|
1
10
|
export declare function getApiBase(): string;
|
|
11
|
+
export declare function getApiBaseSource(): "env" | "profile" | "default";
|
|
2
12
|
/**
|
|
3
13
|
* API base for the deploy-v2 routes. Defaults to the same value as
|
|
4
14
|
* `getApiBase()`. Set `RUN402_DEPLOY_API_BASE` to point only deploy traffic
|
|
@@ -35,5 +45,14 @@ export declare function getProfilesDir(): string;
|
|
|
35
45
|
*/
|
|
36
46
|
export declare function getConfigDir(): string;
|
|
37
47
|
export declare function getKeystorePath(): string;
|
|
48
|
+
export declare function getApiTargetConfigPath(): string;
|
|
49
|
+
export declare function readApiTargetConfig(path?: string): ApiTargetConfig | null;
|
|
50
|
+
export declare function saveApiTargetConfig(config: ApiTargetConfig, path?: string): void;
|
|
51
|
+
export declare function configureApiBase(apiBase: string, options?: Omit<ApiTargetConfig, "api_base" | "updated_at"> & {
|
|
52
|
+
updated_at?: string;
|
|
53
|
+
}): ApiTargetConfig;
|
|
54
|
+
export declare function getConfiguredApiBase(): string | null;
|
|
55
|
+
export declare function getApiTargetKind(): ApiTargetKind;
|
|
56
|
+
export declare function isCoreApiTarget(): boolean;
|
|
38
57
|
export declare function getAllowancePath(): string;
|
|
39
58
|
//# sourceMappingURL=config.d.ts.map
|
package/core-dist/config.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { homedir } from "node:os";
|
|
2
|
-
import { join } from "node:path";
|
|
3
|
-
import { existsSync, renameSync, mkdirSync, chmodSync } from "node:fs";
|
|
4
|
-
|
|
2
|
+
import { dirname, join } from "node:path";
|
|
3
|
+
import { existsSync, renameSync, mkdirSync, chmodSync, readFileSync, writeFileSync } from "node:fs";
|
|
4
|
+
import { randomBytes } from "node:crypto";
|
|
5
|
+
export const DEFAULT_API_BASE = "https://api.run402.com";
|
|
5
6
|
/**
|
|
6
7
|
* Validate a user-supplied API base URL. Throws a clear error message that
|
|
7
8
|
* names the env var when the URL is malformed or uses a scheme other than
|
|
@@ -33,7 +34,12 @@ function validateApiBase(envVar, raw, fallback) {
|
|
|
33
34
|
}
|
|
34
35
|
export function getApiBase() {
|
|
35
36
|
const validated = validateApiBase("RUN402_API_BASE", process.env.RUN402_API_BASE, DEFAULT_API_BASE);
|
|
36
|
-
return validated ?? DEFAULT_API_BASE;
|
|
37
|
+
return validated ?? getConfiguredApiBase() ?? DEFAULT_API_BASE;
|
|
38
|
+
}
|
|
39
|
+
export function getApiBaseSource() {
|
|
40
|
+
if (process.env.RUN402_API_BASE !== undefined)
|
|
41
|
+
return "env";
|
|
42
|
+
return getConfiguredApiBase() ? "profile" : "default";
|
|
37
43
|
}
|
|
38
44
|
/**
|
|
39
45
|
* API base for the deploy-v2 routes. Defaults to the same value as
|
|
@@ -115,6 +121,99 @@ export function getConfigDir() {
|
|
|
115
121
|
export function getKeystorePath() {
|
|
116
122
|
return join(getConfigDir(), "projects.json");
|
|
117
123
|
}
|
|
124
|
+
export function getApiTargetConfigPath() {
|
|
125
|
+
return join(getConfigDir(), "target.json");
|
|
126
|
+
}
|
|
127
|
+
export function readApiTargetConfig(path) {
|
|
128
|
+
const p = path ?? getApiTargetConfigPath();
|
|
129
|
+
try {
|
|
130
|
+
const parsed = JSON.parse(readFileSync(p, "utf-8"));
|
|
131
|
+
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed))
|
|
132
|
+
return null;
|
|
133
|
+
const cfg = parsed;
|
|
134
|
+
if (cfg.api_base !== undefined && typeof cfg.api_base !== "string")
|
|
135
|
+
return null;
|
|
136
|
+
if (cfg.target_kind !== undefined &&
|
|
137
|
+
cfg.target_kind !== "cloud" &&
|
|
138
|
+
cfg.target_kind !== "core" &&
|
|
139
|
+
cfg.target_kind !== "unknown") {
|
|
140
|
+
return null;
|
|
141
|
+
}
|
|
142
|
+
return cfg;
|
|
143
|
+
}
|
|
144
|
+
catch {
|
|
145
|
+
return null;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
function atomicWrite(path, content, mode) {
|
|
149
|
+
const dir = dirname(path);
|
|
150
|
+
mkdirSync(dir, { recursive: true });
|
|
151
|
+
const tmp = join(dir, `.target.${randomBytes(4).toString("hex")}.tmp`);
|
|
152
|
+
writeFileSync(tmp, content, { mode });
|
|
153
|
+
renameSync(tmp, path);
|
|
154
|
+
try {
|
|
155
|
+
chmodSync(path, mode);
|
|
156
|
+
}
|
|
157
|
+
catch {
|
|
158
|
+
/* best-effort on non-POSIX */
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
export function saveApiTargetConfig(config, path) {
|
|
162
|
+
const p = path ?? getApiTargetConfigPath();
|
|
163
|
+
atomicWrite(p, JSON.stringify(config, null, 2), 0o600);
|
|
164
|
+
}
|
|
165
|
+
export function configureApiBase(apiBase, options = {}) {
|
|
166
|
+
if (apiBase === "") {
|
|
167
|
+
throw new Error("api_base must be a non-empty http(s) URL.");
|
|
168
|
+
}
|
|
169
|
+
const validated = validateApiBase("api_base", apiBase, DEFAULT_API_BASE);
|
|
170
|
+
if (!validated) {
|
|
171
|
+
throw new Error("api_base must be a non-empty http(s) URL.");
|
|
172
|
+
}
|
|
173
|
+
const config = {
|
|
174
|
+
api_base: validated.replace(/\/+$/, ""),
|
|
175
|
+
target_kind: options.target_kind ?? "unknown",
|
|
176
|
+
updated_at: options.updated_at ?? new Date().toISOString(),
|
|
177
|
+
...(options.health_status ? { health_status: options.health_status } : {}),
|
|
178
|
+
...(options.health_error ? { health_error: options.health_error } : {}),
|
|
179
|
+
};
|
|
180
|
+
saveApiTargetConfig(config);
|
|
181
|
+
return config;
|
|
182
|
+
}
|
|
183
|
+
export function getConfiguredApiBase() {
|
|
184
|
+
const cfg = readApiTargetConfig();
|
|
185
|
+
if (!cfg?.api_base)
|
|
186
|
+
return null;
|
|
187
|
+
const validated = validateApiBase("api_base", cfg.api_base, DEFAULT_API_BASE);
|
|
188
|
+
return validated ? validated.replace(/\/+$/, "") : null;
|
|
189
|
+
}
|
|
190
|
+
export function getApiTargetKind() {
|
|
191
|
+
const cfg = readApiTargetConfig();
|
|
192
|
+
return cfg?.target_kind ?? "unknown";
|
|
193
|
+
}
|
|
194
|
+
function stripTrailingSlashes(value) {
|
|
195
|
+
return value.replace(/\/+$/, "");
|
|
196
|
+
}
|
|
197
|
+
function isExplicitHttpCoreTarget(apiBase) {
|
|
198
|
+
if (stripTrailingSlashes(apiBase) === stripTrailingSlashes(DEFAULT_API_BASE))
|
|
199
|
+
return false;
|
|
200
|
+
try {
|
|
201
|
+
return new URL(apiBase).protocol === "http:";
|
|
202
|
+
}
|
|
203
|
+
catch {
|
|
204
|
+
return false;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
export function isCoreApiTarget() {
|
|
208
|
+
const apiBase = getApiBase();
|
|
209
|
+
const cfg = readApiTargetConfig();
|
|
210
|
+
if (cfg?.target_kind === "core" && cfg.api_base) {
|
|
211
|
+
return stripTrailingSlashes(apiBase) === stripTrailingSlashes(cfg.api_base);
|
|
212
|
+
}
|
|
213
|
+
if (process.env.RUN402_API_BASE !== undefined && isExplicitHttpCoreTarget(apiBase))
|
|
214
|
+
return true;
|
|
215
|
+
return false;
|
|
216
|
+
}
|
|
118
217
|
export function getAllowancePath() {
|
|
119
218
|
if (process.env.RUN402_ALLOWANCE_PATH)
|
|
120
219
|
return process.env.RUN402_ALLOWANCE_PATH;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"deploy.d.ts","sourceRoot":"","sources":["../../src/namespaces/deploy.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AAEH,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AAkB3C,OAAO,KAAK,EACV,YAAY,EACZ,sBA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