@onyx.dev/onyx-database 0.2.0 → 0.3.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/index.cjs CHANGED
@@ -342,7 +342,8 @@ var HttpClient = class {
342
342
  ...method === "DELETE" ? { Prefer: "return=representation" } : {},
343
343
  ...extraHeaders ?? {}
344
344
  });
345
- if (body == null) delete headers["Content-Type"];
345
+ const hasExplicitContentType = extraHeaders && "Content-Type" in extraHeaders || Object.prototype.hasOwnProperty.call(this.defaults, "Content-Type");
346
+ if (body == null && !hasExplicitContentType) delete headers["Content-Type"];
346
347
  if (this.requestLoggingEnabled) {
347
348
  console.log(`${method} ${url}`);
348
349
  if (body != null) {
@@ -959,6 +960,33 @@ function serializeDates(value) {
959
960
  }
960
961
  return value;
961
962
  }
963
+ function normalizeSecretMetadata(input) {
964
+ return { ...input, updatedAt: new Date(input.updatedAt) };
965
+ }
966
+ function normalizeSecretRecord(input) {
967
+ return { ...input, updatedAt: new Date(input.updatedAt) };
968
+ }
969
+ function normalizeDate(value) {
970
+ if (value == null) return void 0;
971
+ if (value instanceof Date) return value;
972
+ const ts = new Date(value);
973
+ return Number.isNaN(ts.getTime()) ? void 0 : ts;
974
+ }
975
+ function normalizeSchemaRevision(input, fallbackDatabaseId) {
976
+ const { meta, createdAt, publishedAt, revisionId, ...rest } = input;
977
+ const mergedMeta = {
978
+ revisionId: meta?.revisionId ?? revisionId,
979
+ createdAt: normalizeDate(meta?.createdAt ?? createdAt),
980
+ publishedAt: normalizeDate(meta?.publishedAt ?? publishedAt)
981
+ };
982
+ const cleanedMeta = mergedMeta.revisionId || mergedMeta.createdAt || mergedMeta.publishedAt ? mergedMeta : void 0;
983
+ return {
984
+ ...rest,
985
+ databaseId: input.databaseId ?? fallbackDatabaseId,
986
+ meta: cleanedMeta,
987
+ entities: input.entities ?? []
988
+ };
989
+ }
962
990
  var OnyxDatabaseImpl = class {
963
991
  cfgPromise;
964
992
  resolved = null;
@@ -1092,6 +1120,86 @@ var OnyxDatabaseImpl = class {
1092
1120
  )}`;
1093
1121
  return http.request("DELETE", path);
1094
1122
  }
1123
+ async getSchema(options) {
1124
+ const { http, databaseId } = await this.ensureClient();
1125
+ const params = new URLSearchParams();
1126
+ const tables = options?.tables;
1127
+ const tableList = Array.isArray(tables) ? tables : typeof tables === "string" ? tables.split(",") : [];
1128
+ const normalizedTables = tableList.map((t) => t.trim()).filter(Boolean);
1129
+ if (normalizedTables.length) {
1130
+ params.append("tables", normalizedTables.map(encodeURIComponent).join(","));
1131
+ }
1132
+ const path = `/schemas/${encodeURIComponent(databaseId)}${params.size ? `?${params.toString()}` : ""}`;
1133
+ const res = await http.request("GET", path);
1134
+ return normalizeSchemaRevision(res, databaseId);
1135
+ }
1136
+ async getSchemaHistory() {
1137
+ const { http, databaseId } = await this.ensureClient();
1138
+ const path = `/schemas/history/${encodeURIComponent(databaseId)}`;
1139
+ const res = await http.request("GET", path);
1140
+ return Array.isArray(res) ? res.map((entry) => normalizeSchemaRevision(entry, databaseId)) : [];
1141
+ }
1142
+ async updateSchema(schema, options) {
1143
+ const { http, databaseId } = await this.ensureClient();
1144
+ const params = new URLSearchParams();
1145
+ if (options?.publish) params.append("publish", "true");
1146
+ const path = `/schemas/${encodeURIComponent(databaseId)}${params.size ? `?${params.toString()}` : ""}`;
1147
+ const body = { ...schema, databaseId: schema.databaseId ?? databaseId };
1148
+ const res = await http.request("PUT", path, serializeDates(body));
1149
+ return normalizeSchemaRevision(res, databaseId);
1150
+ }
1151
+ async validateSchema(schema) {
1152
+ const { http, databaseId } = await this.ensureClient();
1153
+ const path = `/schemas/${encodeURIComponent(databaseId)}/validate`;
1154
+ const body = { ...schema, databaseId: schema.databaseId ?? databaseId };
1155
+ const res = await http.request("POST", path, serializeDates(body));
1156
+ const normalizedSchema = res.schema ? normalizeSchemaRevision(res.schema, databaseId) : void 0;
1157
+ return {
1158
+ ...res,
1159
+ valid: res.valid ?? true,
1160
+ schema: normalizedSchema
1161
+ };
1162
+ }
1163
+ async listSecrets() {
1164
+ const { http, databaseId } = await this.ensureClient();
1165
+ const path = `/database/${encodeURIComponent(databaseId)}/secret`;
1166
+ const response = await http.request(
1167
+ "GET",
1168
+ path,
1169
+ void 0,
1170
+ { "Content-Type": "application/json" }
1171
+ );
1172
+ const records = (response.records ?? []).map(normalizeSecretMetadata);
1173
+ return {
1174
+ ...response,
1175
+ records,
1176
+ meta: response.meta ?? { totalRecords: records.length }
1177
+ };
1178
+ }
1179
+ async getSecret(key) {
1180
+ const { http, databaseId } = await this.ensureClient();
1181
+ const path = `/database/${encodeURIComponent(databaseId)}/secret/${encodeURIComponent(key)}`;
1182
+ const record = await http.request("GET", path, void 0, {
1183
+ "Content-Type": "application/json"
1184
+ });
1185
+ return normalizeSecretRecord(record);
1186
+ }
1187
+ async putSecret(key, input) {
1188
+ const { http, databaseId } = await this.ensureClient();
1189
+ const path = `/database/${encodeURIComponent(databaseId)}/secret/${encodeURIComponent(key)}`;
1190
+ const response = await http.request("PUT", path, serializeDates(input));
1191
+ return normalizeSecretMetadata(response);
1192
+ }
1193
+ async deleteSecret(key) {
1194
+ const { http, databaseId } = await this.ensureClient();
1195
+ const path = `/database/${encodeURIComponent(databaseId)}/secret/${encodeURIComponent(key)}`;
1196
+ const response = await http.request(
1197
+ "DELETE",
1198
+ path
1199
+ );
1200
+ const deletedKey = response && typeof response === "object" && "key" in response ? response.key : void 0;
1201
+ return { key: deletedKey ?? key };
1202
+ }
1095
1203
  close() {
1096
1204
  for (const h of Array.from(this.streams)) {
1097
1205
  try {