@nvwa-app/sdk-functions 6.0.51 → 6.0.53
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.d.mts +21 -2
- package/dist/index.d.ts +21 -2
- package/dist/index.js +91 -0
- package/dist/index.mjs +90 -0
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -19,7 +19,7 @@ interface AuthUser {
|
|
|
19
19
|
declare function getRequestUser(req: Request, baseUrlOverride?: string): Promise<AuthUser | null>;
|
|
20
20
|
|
|
21
21
|
/** 支付端类型:与 integrations/payment 中的 supportedPlatforms 一致,用于按端过滤可用 provider。 */
|
|
22
|
-
type PaymentPlatform = "web" | "uniapp" | "wechat-miniprogram";
|
|
22
|
+
type PaymentPlatform = "web" | "uniapp" | "wechat-miniprogram" | "alipay-miniprogram";
|
|
23
23
|
interface PaymentGatewayClientOptions {
|
|
24
24
|
baseUrl: string;
|
|
25
25
|
/** Project key (PAYMENT_PROJECT_KEY) for project-side auth; gateway validates via X-Payment-Project-Key */
|
|
@@ -73,4 +73,23 @@ declare function getAvailableProviders(options?: GetAvailableProvidersOptions):
|
|
|
73
73
|
/** 单例:Functions 内 getPayment() 使用 env PAYMENT_GATEWAY_URL、PAYMENT_PROJECT_KEY。 */
|
|
74
74
|
declare function getPayment(options?: PaymentGatewayClientOptions): PaymentGatewayClient;
|
|
75
75
|
|
|
76
|
-
|
|
76
|
+
interface ExecuteCapabilityOptions {
|
|
77
|
+
/** 覆盖 NVWA_SERVER_BASE_URL(Workspace/Server 根地址,与 nvwa.json server.baseUrl 一致) */
|
|
78
|
+
serverBaseUrl?: string;
|
|
79
|
+
/** 覆盖 NVWA_INTERNAL_SECRET(与 nvwa.json server.internalSecret / 项目 runtimeInternalKey 一致) */
|
|
80
|
+
internalSecret?: string;
|
|
81
|
+
/** 覆盖 NVWA_PROJECT_CODE */
|
|
82
|
+
projectCode?: string;
|
|
83
|
+
}
|
|
84
|
+
interface CapabilityExecuteResponse<Data = unknown> {
|
|
85
|
+
status: number;
|
|
86
|
+
data?: Data;
|
|
87
|
+
message?: string;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* 在 Edge Function 中调用已开通的 Nvwa capability(经 Server internal HMAC 路由)。
|
|
91
|
+
* 需在环境中配置 NVWA_SERVER_BASE_URL、NVWA_INTERNAL_SECRET、NVWA_PROJECT_CODE(与 Runtime nvwa.json server 块一致)。
|
|
92
|
+
*/
|
|
93
|
+
declare function executeCapability<Result = unknown>(capabilityName: string, param?: unknown, options?: ExecuteCapabilityOptions): Promise<Result | undefined>;
|
|
94
|
+
|
|
95
|
+
export { type AuthUser, type CapabilityExecuteResponse, type CreateOrderParams, type CreateOrderResult, type ExecuteCapabilityOptions, type GetAvailableProvidersOptions, type GetOrderResult, type PaymentGatewayClient, type PaymentGatewayClientOptions, type PaymentPlatform, createPaymentClient, executeCapability, filterProvidersByPlatform, getAvailableProviders, getConfiguredProviders, getDb, getPayment, getRequestUser, providerSupportedPlatforms };
|
package/dist/index.d.ts
CHANGED
|
@@ -19,7 +19,7 @@ interface AuthUser {
|
|
|
19
19
|
declare function getRequestUser(req: Request, baseUrlOverride?: string): Promise<AuthUser | null>;
|
|
20
20
|
|
|
21
21
|
/** 支付端类型:与 integrations/payment 中的 supportedPlatforms 一致,用于按端过滤可用 provider。 */
|
|
22
|
-
type PaymentPlatform = "web" | "uniapp" | "wechat-miniprogram";
|
|
22
|
+
type PaymentPlatform = "web" | "uniapp" | "wechat-miniprogram" | "alipay-miniprogram";
|
|
23
23
|
interface PaymentGatewayClientOptions {
|
|
24
24
|
baseUrl: string;
|
|
25
25
|
/** Project key (PAYMENT_PROJECT_KEY) for project-side auth; gateway validates via X-Payment-Project-Key */
|
|
@@ -73,4 +73,23 @@ declare function getAvailableProviders(options?: GetAvailableProvidersOptions):
|
|
|
73
73
|
/** 单例:Functions 内 getPayment() 使用 env PAYMENT_GATEWAY_URL、PAYMENT_PROJECT_KEY。 */
|
|
74
74
|
declare function getPayment(options?: PaymentGatewayClientOptions): PaymentGatewayClient;
|
|
75
75
|
|
|
76
|
-
|
|
76
|
+
interface ExecuteCapabilityOptions {
|
|
77
|
+
/** 覆盖 NVWA_SERVER_BASE_URL(Workspace/Server 根地址,与 nvwa.json server.baseUrl 一致) */
|
|
78
|
+
serverBaseUrl?: string;
|
|
79
|
+
/** 覆盖 NVWA_INTERNAL_SECRET(与 nvwa.json server.internalSecret / 项目 runtimeInternalKey 一致) */
|
|
80
|
+
internalSecret?: string;
|
|
81
|
+
/** 覆盖 NVWA_PROJECT_CODE */
|
|
82
|
+
projectCode?: string;
|
|
83
|
+
}
|
|
84
|
+
interface CapabilityExecuteResponse<Data = unknown> {
|
|
85
|
+
status: number;
|
|
86
|
+
data?: Data;
|
|
87
|
+
message?: string;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* 在 Edge Function 中调用已开通的 Nvwa capability(经 Server internal HMAC 路由)。
|
|
91
|
+
* 需在环境中配置 NVWA_SERVER_BASE_URL、NVWA_INTERNAL_SECRET、NVWA_PROJECT_CODE(与 Runtime nvwa.json server 块一致)。
|
|
92
|
+
*/
|
|
93
|
+
declare function executeCapability<Result = unknown>(capabilityName: string, param?: unknown, options?: ExecuteCapabilityOptions): Promise<Result | undefined>;
|
|
94
|
+
|
|
95
|
+
export { type AuthUser, type CapabilityExecuteResponse, type CreateOrderParams, type CreateOrderResult, type ExecuteCapabilityOptions, type GetAvailableProvidersOptions, type GetOrderResult, type PaymentGatewayClient, type PaymentGatewayClientOptions, type PaymentPlatform, createPaymentClient, executeCapability, filterProvidersByPlatform, getAvailableProviders, getConfiguredProviders, getDb, getPayment, getRequestUser, providerSupportedPlatforms };
|
package/dist/index.js
CHANGED
|
@@ -31,6 +31,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
31
31
|
var index_exports = {};
|
|
32
32
|
__export(index_exports, {
|
|
33
33
|
createPaymentClient: () => createPaymentClient,
|
|
34
|
+
executeCapability: () => executeCapability,
|
|
34
35
|
filterProvidersByPlatform: () => filterProvidersByPlatform,
|
|
35
36
|
getAvailableProviders: () => getAvailableProviders,
|
|
36
37
|
getConfiguredProviders: () => getConfiguredProviders,
|
|
@@ -99,6 +100,7 @@ async function getRequestUser(req, baseUrlOverride) {
|
|
|
99
100
|
var PROVIDER_SUPPORTED_PLATFORMS = {
|
|
100
101
|
"wechat-pay": ["web", "uniapp", "wechat-miniprogram"],
|
|
101
102
|
"alipay": ["web", "uniapp"],
|
|
103
|
+
"alipay-miniprogram": ["alipay-miniprogram"],
|
|
102
104
|
"stripe": ["web"]
|
|
103
105
|
};
|
|
104
106
|
function resolveBaseUrl(explicit) {
|
|
@@ -211,9 +213,98 @@ function getPayment(options) {
|
|
|
211
213
|
if (!_payment) _payment = createPaymentClient();
|
|
212
214
|
return _payment;
|
|
213
215
|
}
|
|
216
|
+
|
|
217
|
+
// src/capability.ts
|
|
218
|
+
function bytesToHex(bytes) {
|
|
219
|
+
return Array.from(bytes).map((byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
220
|
+
}
|
|
221
|
+
async function sha256Hex(content) {
|
|
222
|
+
const digest = await crypto.subtle.digest(
|
|
223
|
+
"SHA-256",
|
|
224
|
+
new TextEncoder().encode(content)
|
|
225
|
+
);
|
|
226
|
+
return bytesToHex(new Uint8Array(digest));
|
|
227
|
+
}
|
|
228
|
+
async function hmacSha256Hex(secret, content) {
|
|
229
|
+
const key = await crypto.subtle.importKey(
|
|
230
|
+
"raw",
|
|
231
|
+
new TextEncoder().encode(secret),
|
|
232
|
+
{ name: "HMAC", hash: "SHA-256" },
|
|
233
|
+
false,
|
|
234
|
+
["sign"]
|
|
235
|
+
);
|
|
236
|
+
const signature = await crypto.subtle.sign(
|
|
237
|
+
"HMAC",
|
|
238
|
+
key,
|
|
239
|
+
new TextEncoder().encode(content)
|
|
240
|
+
);
|
|
241
|
+
return bytesToHex(new Uint8Array(signature));
|
|
242
|
+
}
|
|
243
|
+
function resolveServerBaseUrl(explicit) {
|
|
244
|
+
const url = explicit ?? getEnv("NVWA_SERVER_BASE_URL");
|
|
245
|
+
if (!url) {
|
|
246
|
+
throw new Error(
|
|
247
|
+
"NVWA_SERVER_BASE_URL is not set; executeCapability requires Workspace server base URL (same as nvwa.json server.baseUrl)"
|
|
248
|
+
);
|
|
249
|
+
}
|
|
250
|
+
return url.replace(/\/$/, "");
|
|
251
|
+
}
|
|
252
|
+
function resolveInternalSecret(explicit) {
|
|
253
|
+
const s = explicit ?? getEnv("NVWA_INTERNAL_SECRET");
|
|
254
|
+
if (!s) {
|
|
255
|
+
throw new Error(
|
|
256
|
+
"NVWA_INTERNAL_SECRET is not set; must match project runtime internal HMAC secret"
|
|
257
|
+
);
|
|
258
|
+
}
|
|
259
|
+
return s;
|
|
260
|
+
}
|
|
261
|
+
function resolveProjectCode(explicit) {
|
|
262
|
+
const c = explicit ?? getEnv("NVWA_PROJECT_CODE");
|
|
263
|
+
if (!c) {
|
|
264
|
+
throw new Error("NVWA_PROJECT_CODE is not set");
|
|
265
|
+
}
|
|
266
|
+
return c;
|
|
267
|
+
}
|
|
268
|
+
async function executeCapability(capabilityName, param, options) {
|
|
269
|
+
const projectCode = resolveProjectCode(options?.projectCode);
|
|
270
|
+
const baseUrl = resolveServerBaseUrl(options?.serverBaseUrl);
|
|
271
|
+
const internalSecret = resolveInternalSecret(options?.internalSecret);
|
|
272
|
+
const path = `/internal/project/${projectCode}/capability/${capabilityName}/execute`;
|
|
273
|
+
const bodyText = param != null ? JSON.stringify(param) : "";
|
|
274
|
+
const timestamp = Date.now().toString();
|
|
275
|
+
const nonce = crypto.randomUUID();
|
|
276
|
+
const bodySha256 = await sha256Hex(bodyText);
|
|
277
|
+
const signingPayload = `POST
|
|
278
|
+
${path}
|
|
279
|
+
${timestamp}
|
|
280
|
+
${nonce}
|
|
281
|
+
${bodySha256}
|
|
282
|
+
${projectCode}`;
|
|
283
|
+
const signature = await hmacSha256Hex(internalSecret, signingPayload);
|
|
284
|
+
const response = await fetch(`${baseUrl}${path}`, {
|
|
285
|
+
method: "POST",
|
|
286
|
+
headers: {
|
|
287
|
+
"Content-Type": "application/json",
|
|
288
|
+
"X-Internal-Timestamp": timestamp,
|
|
289
|
+
"X-Internal-Nonce": nonce,
|
|
290
|
+
"X-Internal-Body-Sha256": bodySha256,
|
|
291
|
+
"X-Internal-Signature": signature,
|
|
292
|
+
"X-Internal-Project-Code": projectCode
|
|
293
|
+
},
|
|
294
|
+
body: bodyText
|
|
295
|
+
});
|
|
296
|
+
const responseBody = await response.json();
|
|
297
|
+
if (responseBody.status !== 200) {
|
|
298
|
+
throw new Error(
|
|
299
|
+
responseBody.message ?? `Capability execute failed with status ${responseBody.status}`
|
|
300
|
+
);
|
|
301
|
+
}
|
|
302
|
+
return responseBody.data;
|
|
303
|
+
}
|
|
214
304
|
// Annotate the CommonJS export names for ESM import in node:
|
|
215
305
|
0 && (module.exports = {
|
|
216
306
|
createPaymentClient,
|
|
307
|
+
executeCapability,
|
|
217
308
|
filterProvidersByPlatform,
|
|
218
309
|
getAvailableProviders,
|
|
219
310
|
getConfiguredProviders,
|
package/dist/index.mjs
CHANGED
|
@@ -56,6 +56,7 @@ async function getRequestUser(req, baseUrlOverride) {
|
|
|
56
56
|
var PROVIDER_SUPPORTED_PLATFORMS = {
|
|
57
57
|
"wechat-pay": ["web", "uniapp", "wechat-miniprogram"],
|
|
58
58
|
"alipay": ["web", "uniapp"],
|
|
59
|
+
"alipay-miniprogram": ["alipay-miniprogram"],
|
|
59
60
|
"stripe": ["web"]
|
|
60
61
|
};
|
|
61
62
|
function resolveBaseUrl(explicit) {
|
|
@@ -168,8 +169,97 @@ function getPayment(options) {
|
|
|
168
169
|
if (!_payment) _payment = createPaymentClient();
|
|
169
170
|
return _payment;
|
|
170
171
|
}
|
|
172
|
+
|
|
173
|
+
// src/capability.ts
|
|
174
|
+
function bytesToHex(bytes) {
|
|
175
|
+
return Array.from(bytes).map((byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
176
|
+
}
|
|
177
|
+
async function sha256Hex(content) {
|
|
178
|
+
const digest = await crypto.subtle.digest(
|
|
179
|
+
"SHA-256",
|
|
180
|
+
new TextEncoder().encode(content)
|
|
181
|
+
);
|
|
182
|
+
return bytesToHex(new Uint8Array(digest));
|
|
183
|
+
}
|
|
184
|
+
async function hmacSha256Hex(secret, content) {
|
|
185
|
+
const key = await crypto.subtle.importKey(
|
|
186
|
+
"raw",
|
|
187
|
+
new TextEncoder().encode(secret),
|
|
188
|
+
{ name: "HMAC", hash: "SHA-256" },
|
|
189
|
+
false,
|
|
190
|
+
["sign"]
|
|
191
|
+
);
|
|
192
|
+
const signature = await crypto.subtle.sign(
|
|
193
|
+
"HMAC",
|
|
194
|
+
key,
|
|
195
|
+
new TextEncoder().encode(content)
|
|
196
|
+
);
|
|
197
|
+
return bytesToHex(new Uint8Array(signature));
|
|
198
|
+
}
|
|
199
|
+
function resolveServerBaseUrl(explicit) {
|
|
200
|
+
const url = explicit ?? getEnv("NVWA_SERVER_BASE_URL");
|
|
201
|
+
if (!url) {
|
|
202
|
+
throw new Error(
|
|
203
|
+
"NVWA_SERVER_BASE_URL is not set; executeCapability requires Workspace server base URL (same as nvwa.json server.baseUrl)"
|
|
204
|
+
);
|
|
205
|
+
}
|
|
206
|
+
return url.replace(/\/$/, "");
|
|
207
|
+
}
|
|
208
|
+
function resolveInternalSecret(explicit) {
|
|
209
|
+
const s = explicit ?? getEnv("NVWA_INTERNAL_SECRET");
|
|
210
|
+
if (!s) {
|
|
211
|
+
throw new Error(
|
|
212
|
+
"NVWA_INTERNAL_SECRET is not set; must match project runtime internal HMAC secret"
|
|
213
|
+
);
|
|
214
|
+
}
|
|
215
|
+
return s;
|
|
216
|
+
}
|
|
217
|
+
function resolveProjectCode(explicit) {
|
|
218
|
+
const c = explicit ?? getEnv("NVWA_PROJECT_CODE");
|
|
219
|
+
if (!c) {
|
|
220
|
+
throw new Error("NVWA_PROJECT_CODE is not set");
|
|
221
|
+
}
|
|
222
|
+
return c;
|
|
223
|
+
}
|
|
224
|
+
async function executeCapability(capabilityName, param, options) {
|
|
225
|
+
const projectCode = resolveProjectCode(options?.projectCode);
|
|
226
|
+
const baseUrl = resolveServerBaseUrl(options?.serverBaseUrl);
|
|
227
|
+
const internalSecret = resolveInternalSecret(options?.internalSecret);
|
|
228
|
+
const path = `/internal/project/${projectCode}/capability/${capabilityName}/execute`;
|
|
229
|
+
const bodyText = param != null ? JSON.stringify(param) : "";
|
|
230
|
+
const timestamp = Date.now().toString();
|
|
231
|
+
const nonce = crypto.randomUUID();
|
|
232
|
+
const bodySha256 = await sha256Hex(bodyText);
|
|
233
|
+
const signingPayload = `POST
|
|
234
|
+
${path}
|
|
235
|
+
${timestamp}
|
|
236
|
+
${nonce}
|
|
237
|
+
${bodySha256}
|
|
238
|
+
${projectCode}`;
|
|
239
|
+
const signature = await hmacSha256Hex(internalSecret, signingPayload);
|
|
240
|
+
const response = await fetch(`${baseUrl}${path}`, {
|
|
241
|
+
method: "POST",
|
|
242
|
+
headers: {
|
|
243
|
+
"Content-Type": "application/json",
|
|
244
|
+
"X-Internal-Timestamp": timestamp,
|
|
245
|
+
"X-Internal-Nonce": nonce,
|
|
246
|
+
"X-Internal-Body-Sha256": bodySha256,
|
|
247
|
+
"X-Internal-Signature": signature,
|
|
248
|
+
"X-Internal-Project-Code": projectCode
|
|
249
|
+
},
|
|
250
|
+
body: bodyText
|
|
251
|
+
});
|
|
252
|
+
const responseBody = await response.json();
|
|
253
|
+
if (responseBody.status !== 200) {
|
|
254
|
+
throw new Error(
|
|
255
|
+
responseBody.message ?? `Capability execute failed with status ${responseBody.status}`
|
|
256
|
+
);
|
|
257
|
+
}
|
|
258
|
+
return responseBody.data;
|
|
259
|
+
}
|
|
171
260
|
export {
|
|
172
261
|
createPaymentClient,
|
|
262
|
+
executeCapability,
|
|
173
263
|
filterProvidersByPlatform,
|
|
174
264
|
getAvailableProviders,
|
|
175
265
|
getConfiguredProviders,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nvwa-app/sdk-functions",
|
|
3
|
-
"version": "6.0.
|
|
3
|
+
"version": "6.0.53",
|
|
4
4
|
"description": "NVWA Edge Functions SDK: db, payment gateway, auth, provider list. Use in Deno: import from 'npm:@nvwa-app/sdk-functions'.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.mjs",
|