@lark-apaas/db-schema-sync 0.1.9 → 0.1.11-alpha.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/dist/bin.js CHANGED
@@ -6,7 +6,7 @@ import {
6
6
  __toESM,
7
7
  generateSchema,
8
8
  resolveEnvOptions
9
- } from "./chunk-QLAAPH4Z.js";
9
+ } from "./chunk-KZVZNV7Y.js";
10
10
 
11
11
  // node_modules/dotenv/package.json
12
12
  var require_package = __commonJS({
@@ -56,6 +56,7 @@ var require_dist = __commonJS({
56
56
  __export(index_exports, {
57
57
  DEFAULT_CLOCK_TOLERANCE_SEC: () => DEFAULT_CLOCK_TOLERANCE_SEC,
58
58
  DEFAULT_JWT_EXPIRE_TIME_MS: () => DEFAULT_JWT_EXPIRE_TIME_MS,
59
+ FileTokenProvider: () => FileTokenProvider,
59
60
  HttpClient: () => HttpClient2,
60
61
  HttpError: () => HttpError,
61
62
  generateJWTToken: () => generateJWTToken,
@@ -197,7 +198,7 @@ var require_dist = __commonJS({
197
198
  getCacheKey(claims) {
198
199
  const parts = [
199
200
  claims.access_key,
200
- claims.tenant_id?.toString() || "",
201
+ claims.tenant_id || "",
201
202
  claims.user_id || "",
202
203
  claims.app_id || "",
203
204
  claims.app_env || "",
@@ -228,9 +229,95 @@ var require_dist = __commonJS({
228
229
  };
229
230
  }
230
231
  };
232
+ var StaticTokenSource = class {
233
+ constructor(token) {
234
+ this.token = token;
235
+ if (!token) {
236
+ throw new Error("StaticTokenSource: token \u4E0D\u80FD\u4E3A\u7A7A");
237
+ }
238
+ }
239
+ getToken(_claims) {
240
+ return this.token;
241
+ }
242
+ };
243
+ var import_fs = __require("fs");
244
+ var DEFAULT_REFRESH_BEFORE_MS = 5 * 60 * 1e3;
245
+ var DEFAULT_TOKEN_PATHS = [
246
+ "/home/gem/workspace/.force/openclaw/miaoda-provider-key"
247
+ ];
248
+ var FileTokenProvider = class {
249
+ cache = /* @__PURE__ */ new Map();
250
+ paths;
251
+ refreshBeforeMs;
252
+ constructor(config) {
253
+ this.paths = config?.paths ?? DEFAULT_TOKEN_PATHS;
254
+ this.refreshBeforeMs = config?.refreshBeforeMs ?? DEFAULT_REFRESH_BEFORE_MS;
255
+ }
256
+ /**
257
+ * 获取 token,按路径优先级依次尝试
258
+ *
259
+ * 1. 缓存有效且不在刷新窗口 → 直接返回
260
+ * 2. 进入刷新窗口 → 重读文件,更新缓存
261
+ * 3. 文件未更新但缓存未过期 → 仍返回旧缓存
262
+ * 4. 所有路径均无有效 token → 返回 null
263
+ */
264
+ getToken() {
265
+ const now = Date.now();
266
+ for (const filePath of this.paths) {
267
+ const cached = this.cache.get(filePath);
268
+ if (cached && cached.expiresAtMs - now > this.refreshBeforeMs) {
269
+ return { token: cached.token, accessKey: cached.accessKey };
270
+ }
271
+ const result = this.readAndParse(filePath);
272
+ if (result) {
273
+ this.cache.set(filePath, result);
274
+ if (result.expiresAtMs > now) {
275
+ return { token: result.token, accessKey: result.accessKey };
276
+ }
277
+ }
278
+ if (cached && cached.expiresAtMs > now) {
279
+ return { token: cached.token, accessKey: cached.accessKey };
280
+ }
281
+ }
282
+ return null;
283
+ }
284
+ clearCache() {
285
+ this.cache.clear();
286
+ }
287
+ readAndParse(filePath) {
288
+ try {
289
+ const token = (0, import_fs.readFileSync)(filePath, "utf-8").trim();
290
+ if (!token) return null;
291
+ const payload = this.decodePayload(token);
292
+ if (!payload?.exp) return null;
293
+ return {
294
+ token,
295
+ accessKey: payload.access_key ?? "",
296
+ expiresAtMs: payload.exp * 1e3
297
+ };
298
+ } catch {
299
+ return null;
300
+ }
301
+ }
302
+ decodePayload(token) {
303
+ try {
304
+ const segments = token.split(".");
305
+ if (segments.length !== 3) return null;
306
+ const padLength = (4 - (segments[1].length % 4 || 4)) % 4;
307
+ const padded = `${segments[1]}${"=".repeat(padLength)}`.replace(/-/g, "+").replace(/_/g, "/");
308
+ return JSON.parse(Buffer.from(padded, "base64").toString("utf8"));
309
+ } catch {
310
+ return null;
311
+ }
312
+ }
313
+ };
231
314
  var DEFAULT_DOMAIN_ENV = "FORCE_AUTHN_INNERAPI_DOMAIN";
232
315
  var DEFAULT_ACCESS_KEY_ENV = "FORCE_AUTHN_ACCESS_KEY";
233
316
  var DEFAULT_SECRET_KEY_ENV = "FORCE_AUTHN_ACCESS_SECRET";
317
+ var DEFAULT_TOKEN_ENV = "FORCE_AUTHN_TOKEN";
318
+ var DEFAULT_ADMIN_DOMAIN_ENV = "MIAODA_DEV_INNER_DOMAIN_WITH_PREFIX";
319
+ var DEFAULT_CLIENT_TOKEN_ENV = "MIAODA_AUTHN_CODE";
320
+ var MIAODA_CLIENT_TOKEN_HEADER = "X-Miaoda-Client-Token";
234
321
  function resolvePlatformBaseURL(options) {
235
322
  if (!options?.enabled) {
236
323
  return options?.baseURL;
@@ -238,7 +325,7 @@ var require_dist = __commonJS({
238
325
  if (options.baseURL) {
239
326
  return options.baseURL;
240
327
  }
241
- const envName = options.domainEnv || DEFAULT_DOMAIN_ENV;
328
+ const envName = options.adminInnerApi ? options.adminDomainEnv || DEFAULT_ADMIN_DOMAIN_ENV : options.domainEnv || DEFAULT_DOMAIN_ENV;
242
329
  const domain = process.env[envName];
243
330
  if (!domain) {
244
331
  throw new Error(`\u5E73\u53F0\u6A21\u5F0F\u9700\u8981\u57FA\u7840\u57DF\u540D\uFF0C\u8BF7\u8BBE\u7F6E\u73AF\u5883\u53D8\u91CF ${envName}`);
@@ -250,6 +337,13 @@ var require_dist = __commonJS({
250
337
  return void 0;
251
338
  }
252
339
  ensureNoAccessKeyOverride("platform.defaultClaims", options.defaultClaims);
340
+ let fileTokenProvider;
341
+ if (options.tokenProvider?.type === "file") {
342
+ fileTokenProvider = new FileTokenProvider({
343
+ paths: options.tokenProvider.paths,
344
+ refreshBeforeMs: options.refreshBeforeMs
345
+ });
346
+ }
253
347
  const accessKeyEnv = options.accessKeyEnv || DEFAULT_ACCESS_KEY_ENV;
254
348
  const secretKeyEnv = options.secretKeyEnv || DEFAULT_SECRET_KEY_ENV;
255
349
  const accessKey = options.accessKey ?? (process.env[accessKeyEnv] || "");
@@ -261,19 +355,49 @@ var require_dist = __commonJS({
261
355
  expireTimeMs,
262
356
  refreshBeforeMs: options.refreshBeforeMs
263
357
  });
358
+ const tokenEnv = options.tokenEnv || DEFAULT_TOKEN_ENV;
359
+ const platformToken = options.token ?? (process.env[tokenEnv] || "");
360
+ const staticTokenSource = platformToken ? new StaticTokenSource(platformToken) : null;
361
+ const clientTokenEnv = options.clientTokenEnv || DEFAULT_CLIENT_TOKEN_ENV;
264
362
  interceptors.request.use((config) => {
363
+ if (fileTokenProvider) {
364
+ const fileToken = fileTokenProvider.getToken();
365
+ if (fileToken) {
366
+ const headers2 = {
367
+ ...config.headers,
368
+ "Authorization": `Bearer ${fileToken.token}`,
369
+ "x-api-key": fileToken.accessKey
370
+ };
371
+ return { ...config, headers: headers2 };
372
+ }
373
+ }
265
374
  ensureNoAccessKeyOverride("request.platformAuth.customClaims", config.platformAuth?.customClaims);
266
375
  const claims = {
267
376
  ...options.defaultClaims || {},
268
377
  ...config.platformAuth?.customClaims || {},
269
378
  access_key: accessKey
270
379
  };
271
- const token = tokenManager.getToken(claims);
380
+ let token;
381
+ if (staticTokenSource) {
382
+ try {
383
+ token = staticTokenSource.getToken(claims);
384
+ } catch {
385
+ token = tokenManager.getToken(claims);
386
+ }
387
+ } else {
388
+ token = tokenManager.getToken(claims);
389
+ }
272
390
  const headers = {
273
391
  ...config.headers,
274
392
  "Authorization": `Bearer ${token}`,
275
393
  "x-api-key": accessKey
276
394
  };
395
+ if (options.adminInnerApi) {
396
+ const clientToken = process.env[clientTokenEnv];
397
+ if (clientToken) {
398
+ headers[MIAODA_CLIENT_TOKEN_HEADER] = clientToken;
399
+ }
400
+ }
277
401
  return {
278
402
  ...config,
279
403
  headers
@@ -401,7 +525,7 @@ var require_dist = __commonJS({
401
525
  response: new InterceptorManager()
402
526
  };
403
527
  constructor(config) {
404
- const { platform, security, ...restConfig } = config || {};
528
+ const { platform, security, preInterceptors, ...restConfig } = config || {};
405
529
  this.defaultConfig = {
406
530
  timeout: 5e3,
407
531
  ...restConfig
@@ -414,12 +538,14 @@ var require_dist = __commonJS({
414
538
  strictMode,
415
539
  ...security
416
540
  };
541
+ this.registerRequestPreInterceptors(preInterceptors?.request);
417
542
  if (platform?.enabled) {
418
543
  if (!this.defaultConfig.baseURL) {
419
544
  this.defaultConfig.baseURL = resolvePlatformBaseURL(platform);
420
545
  }
421
546
  registerPlatformPlugin(this.interceptors, platform);
422
547
  }
548
+ this.registerResponsePreInterceptors(preInterceptors?.response);
423
549
  }
424
550
  /**
425
551
  * GET 请求
@@ -569,6 +695,34 @@ var require_dist = __commonJS({
569
695
  return this.runResponseInterceptors(Promise.reject(httpError));
570
696
  }
571
697
  }
698
+ /**
699
+ * 注册请求 preInterceptors
700
+ */
701
+ registerRequestPreInterceptors(interceptors) {
702
+ if (!interceptors?.length) {
703
+ return;
704
+ }
705
+ for (const interceptor of interceptors) {
706
+ this.interceptors.request.use(
707
+ interceptor.onFulfilled,
708
+ interceptor.onRejected
709
+ );
710
+ }
711
+ }
712
+ /**
713
+ * 注册响应 preInterceptors
714
+ */
715
+ registerResponsePreInterceptors(interceptors) {
716
+ if (!interceptors?.length) {
717
+ return;
718
+ }
719
+ for (const interceptor of interceptors) {
720
+ this.interceptors.response.use(
721
+ interceptor.onFulfilled,
722
+ interceptor.onRejected
723
+ );
724
+ }
725
+ }
572
726
  /**
573
727
  * 执行请求拦截器链
574
728
  */
@@ -1228,12 +1382,15 @@ function escapeDoubleQuote(s) {
1228
1382
 
1229
1383
  // src/generator/mappers/primitives.ts
1230
1384
  var PG_CORE = "drizzle-orm/pg-core";
1385
+ function withArray(base, f) {
1386
+ return f.isArray ? `${base}.array()` : base;
1387
+ }
1231
1388
  var primitiveMappers = [
1232
1389
  // uuid
1233
1390
  {
1234
1391
  name: "uuid",
1235
1392
  match: (f) => f.type === "uuid",
1236
- generate: (f) => `uuid("${escapeDoubleQuote(f.fieldName)}")`,
1393
+ generate: (f) => withArray(`uuid("${escapeDoubleQuote(f.fieldName)}")`, f),
1237
1394
  imports: () => [{ name: "uuid", from: PG_CORE }]
1238
1395
  },
1239
1396
  // varchar (honors extraInfo.max_length from listTableView API)
@@ -1243,9 +1400,9 @@ var primitiveMappers = [
1243
1400
  generate: (f) => {
1244
1401
  const maxLength = f.extraInfo?.max_length;
1245
1402
  if (typeof maxLength === "number" && Number.isInteger(maxLength) && maxLength > 0) {
1246
- return `varchar("${escapeDoubleQuote(f.fieldName)}", { length: ${maxLength} })`;
1403
+ return withArray(`varchar("${escapeDoubleQuote(f.fieldName)}", { length: ${maxLength} })`, f);
1247
1404
  }
1248
- return `varchar("${escapeDoubleQuote(f.fieldName)}")`;
1405
+ return withArray(`varchar("${escapeDoubleQuote(f.fieldName)}")`, f);
1249
1406
  },
1250
1407
  imports: () => [{ name: "varchar", from: PG_CORE }]
1251
1408
  },
@@ -1253,105 +1410,105 @@ var primitiveMappers = [
1253
1410
  {
1254
1411
  name: "text",
1255
1412
  match: (f) => f.type === "text",
1256
- generate: (f) => `text("${escapeDoubleQuote(f.fieldName)}")`,
1413
+ generate: (f) => withArray(`text("${escapeDoubleQuote(f.fieldName)}")`, f),
1257
1414
  imports: () => [{ name: "text", from: PG_CORE }]
1258
1415
  },
1259
1416
  // smallint (int2)
1260
1417
  {
1261
1418
  name: "smallint",
1262
1419
  match: (f) => f.type === "int2",
1263
- generate: (f) => `smallint("${escapeDoubleQuote(f.fieldName)}")`,
1420
+ generate: (f) => withArray(`smallint("${escapeDoubleQuote(f.fieldName)}")`, f),
1264
1421
  imports: () => [{ name: "smallint", from: PG_CORE }]
1265
1422
  },
1266
1423
  // integer (int4, integer)
1267
1424
  {
1268
1425
  name: "integer",
1269
1426
  match: (f) => ["int4", "integer"].includes(f.type),
1270
- generate: (f) => `integer("${escapeDoubleQuote(f.fieldName)}")`,
1427
+ generate: (f) => withArray(`integer("${escapeDoubleQuote(f.fieldName)}")`, f),
1271
1428
  imports: () => [{ name: "integer", from: PG_CORE }]
1272
1429
  },
1273
1430
  // bigint (int8, bigint)
1274
1431
  {
1275
1432
  name: "bigint",
1276
1433
  match: (f) => ["int8", "bigint"].includes(f.type),
1277
- generate: (f) => `bigint("${escapeDoubleQuote(f.fieldName)}", { mode: 'number' })`,
1434
+ generate: (f) => withArray(`bigint("${escapeDoubleQuote(f.fieldName)}", { mode: 'number' })`, f),
1278
1435
  imports: () => [{ name: "bigint", from: PG_CORE }]
1279
1436
  },
1280
1437
  // real (float4, real)
1281
1438
  {
1282
1439
  name: "real",
1283
1440
  match: (f) => ["float4", "real"].includes(f.type),
1284
- generate: (f) => `real("${escapeDoubleQuote(f.fieldName)}")`,
1441
+ generate: (f) => withArray(`real("${escapeDoubleQuote(f.fieldName)}")`, f),
1285
1442
  imports: () => [{ name: "real", from: PG_CORE }]
1286
1443
  },
1287
1444
  // doublePrecision (float8, double precision)
1288
1445
  {
1289
1446
  name: "doublePrecision",
1290
1447
  match: (f) => ["float8", "double precision"].includes(f.type),
1291
- generate: (f) => `doublePrecision("${escapeDoubleQuote(f.fieldName)}")`,
1448
+ generate: (f) => withArray(`doublePrecision("${escapeDoubleQuote(f.fieldName)}")`, f),
1292
1449
  imports: () => [{ name: "doublePrecision", from: PG_CORE }]
1293
1450
  },
1294
1451
  // numeric (numeric, decimal)
1295
1452
  {
1296
1453
  name: "numeric",
1297
1454
  match: (f) => ["numeric", "decimal"].includes(f.type),
1298
- generate: (f) => `numeric("${escapeDoubleQuote(f.fieldName)}")`,
1455
+ generate: (f) => withArray(`numeric("${escapeDoubleQuote(f.fieldName)}")`, f),
1299
1456
  imports: () => [{ name: "numeric", from: PG_CORE }]
1300
1457
  },
1301
1458
  // boolean (bool, boolean)
1302
1459
  {
1303
1460
  name: "boolean",
1304
1461
  match: (f) => ["bool", "boolean"].includes(f.type),
1305
- generate: (f) => `boolean("${escapeDoubleQuote(f.fieldName)}")`,
1462
+ generate: (f) => withArray(`boolean("${escapeDoubleQuote(f.fieldName)}")`, f),
1306
1463
  imports: () => [{ name: "boolean", from: PG_CORE }]
1307
1464
  },
1308
1465
  // json
1309
1466
  {
1310
1467
  name: "json",
1311
1468
  match: (f) => f.type === "json",
1312
- generate: (f) => `json("${escapeDoubleQuote(f.fieldName)}")`,
1469
+ generate: (f) => withArray(`json("${escapeDoubleQuote(f.fieldName)}")`, f),
1313
1470
  imports: () => [{ name: "json", from: PG_CORE }]
1314
1471
  },
1315
1472
  // jsonb
1316
1473
  {
1317
1474
  name: "jsonb",
1318
1475
  match: (f) => f.type === "jsonb",
1319
- generate: (f) => `jsonb("${escapeDoubleQuote(f.fieldName)}")`,
1476
+ generate: (f) => withArray(`jsonb("${escapeDoubleQuote(f.fieldName)}")`, f),
1320
1477
  imports: () => [{ name: "jsonb", from: PG_CORE }]
1321
1478
  },
1322
1479
  // timestamp (without timezone) — uses drizzle-orm timestamp, not customTimestamptz
1323
1480
  {
1324
1481
  name: "timestamp",
1325
1482
  match: (f) => f.type === "timestamp",
1326
- generate: (f) => `timestamp("${escapeDoubleQuote(f.fieldName)}", { mode: 'string' })`,
1483
+ generate: (f) => withArray(`timestamp("${escapeDoubleQuote(f.fieldName)}", { mode: 'string' })`, f),
1327
1484
  imports: () => [{ name: "timestamp", from: PG_CORE }]
1328
1485
  },
1329
1486
  // date
1330
1487
  {
1331
1488
  name: "date",
1332
1489
  match: (f) => f.type === "date",
1333
- generate: (f) => `date("${escapeDoubleQuote(f.fieldName)}")`,
1490
+ generate: (f) => withArray(`date("${escapeDoubleQuote(f.fieldName)}")`, f),
1334
1491
  imports: () => [{ name: "date", from: PG_CORE }]
1335
1492
  },
1336
1493
  // timetz (time with time zone)
1337
1494
  {
1338
1495
  name: "timetz",
1339
1496
  match: (f) => f.type === "timetz",
1340
- generate: (f) => `time("${escapeDoubleQuote(f.fieldName)}", { withTimezone: true })`,
1497
+ generate: (f) => withArray(`time("${escapeDoubleQuote(f.fieldName)}", { withTimezone: true })`, f),
1341
1498
  imports: () => [{ name: "time", from: PG_CORE }]
1342
1499
  },
1343
1500
  // time
1344
1501
  {
1345
1502
  name: "time",
1346
1503
  match: (f) => f.type === "time",
1347
- generate: (f) => `time("${escapeDoubleQuote(f.fieldName)}")`,
1504
+ generate: (f) => withArray(`time("${escapeDoubleQuote(f.fieldName)}")`, f),
1348
1505
  imports: () => [{ name: "time", from: PG_CORE }]
1349
1506
  },
1350
1507
  // serial
1351
1508
  {
1352
1509
  name: "serial",
1353
1510
  match: (f) => f.type === "serial",
1354
- generate: (f) => `serial("${escapeDoubleQuote(f.fieldName)}")`,
1511
+ generate: (f) => withArray(`serial("${escapeDoubleQuote(f.fieldName)}")`, f),
1355
1512
  imports: () => [{ name: "serial", from: PG_CORE }]
1356
1513
  }
1357
1514
  ];
@@ -1426,7 +1583,10 @@ function createEnumMapper(enumNameToIdentifier) {
1426
1583
  var fallbackMapper = {
1427
1584
  name: "fallback-text",
1428
1585
  match: () => true,
1429
- generate: (field) => `text("${escapeDoubleQuote(field.fieldName)}")`,
1586
+ generate: (field) => {
1587
+ const base = `text("${escapeDoubleQuote(field.fieldName)}")`;
1588
+ return field.isArray ? `${base}.array()` : base;
1589
+ },
1430
1590
  imports: () => [{ name: "text", from: "drizzle-orm/pg-core" }]
1431
1591
  };
1432
1592
 
package/dist/index.js CHANGED
@@ -5,7 +5,7 @@ import {
5
5
  generateSchemaFromData,
6
6
  normalizeApiResponse,
7
7
  resolveEnvOptions
8
- } from "./chunk-QLAAPH4Z.js";
8
+ } from "./chunk-KZVZNV7Y.js";
9
9
  export {
10
10
  generateSchema,
11
11
  generateSchemaCode,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lark-apaas/db-schema-sync",
3
- "version": "0.1.9",
3
+ "version": "0.1.11-alpha.0",
4
4
  "type": "module",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",