lazypock 0.8.0 → 0.8.2

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
@@ -103,10 +103,9 @@ var HttpClient = class {
103
103
  }
104
104
  const data = await res.json();
105
105
  if (data && typeof data.token === "string") {
106
- this.authStore.set(
107
- data.token,
108
- data.record ?? null
109
- );
106
+ const record = data["record"];
107
+ const model = record && typeof record === "object" ? record : null;
108
+ this.authStore.set(data.token, model);
110
109
  return data;
111
110
  }
112
111
  return null;
@@ -272,7 +271,15 @@ var HttpClient = class {
272
271
  if (bodyText) {
273
272
  data = JSON.parse(bodyText);
274
273
  }
275
- } catch {
274
+ } catch (err) {
275
+ if (isAbortError(err)) {
276
+ throw new ApiError(
277
+ "The request was aborted (most likely auto-cancelled by a newer request with the same requestKey)",
278
+ {},
279
+ 0,
280
+ true
281
+ );
282
+ }
276
283
  }
277
284
  if (!res.ok) {
278
285
  throw new ApiError(
@@ -1483,6 +1490,12 @@ function generateTypes(collections, options = {}) {
1483
1490
  body
1484
1491
  })}`
1485
1492
  );
1493
+ const createLines = fields.map((f) => createDataMemberLine(f)).filter((l) => l !== "");
1494
+ sections.push(
1495
+ `export interface ${typeName}CreateData${renderInterface({
1496
+ body: createLines.join("\n")
1497
+ })}`
1498
+ );
1486
1499
  }
1487
1500
  sections.push(`export interface AuthRecord extends BaseRecord {
1488
1501
  email: string;
@@ -1493,6 +1506,10 @@ function generateTypes(collections, options = {}) {
1493
1506
  ).join("\n");
1494
1507
  sections.push(`export interface LazypockCollections {
1495
1508
  ${mapEntries}
1509
+ }`);
1510
+ const createMapEntries = filtered.map((c) => ` "${c.name}": ${collectionTypeName(c.name)}CreateData;`).join("\n");
1511
+ sections.push(`export interface LazypockCreateData {
1512
+ ${createMapEntries}
1496
1513
  }`);
1497
1514
  const schemaEntries = collections.map(
1498
1515
  (c) => ` {
@@ -1516,6 +1533,10 @@ ${schemaEntries}
1516
1533
  * Collection access is fully type-checked:
1517
1534
  * client.collection("posts").create({ title: "x" }) // title must exist
1518
1535
  *
1536
+ * For auth collections the generated *CreateData type includes the
1537
+ * write-only password field, so creating a user is type-safe:
1538
+ * client.collection("users").create({ email, password }) // \u2713
1539
+ *
1519
1540
  * The schema snapshot is wired into the client automatically, so hidden
1520
1541
  * fields are excluded from responses and select/expand are validated.
1521
1542
  */
@@ -1535,8 +1556,22 @@ export class TypedClient extends LazypockClient {
1535
1556
  // are rejected at compile time:
1536
1557
  // client.collection("posts") // suggested + typed
1537
1558
  // client.collection("nope") // TS error
1538
- override collection<K extends keyof LazypockCollections>(name: K): CollectionService<LazypockCollections[K]> {
1539
- return super.collection(name) as CollectionService<LazypockCollections[K]>;
1559
+ //
1560
+ // create()/update() take the collection's *CreateData \u2014 the read model
1561
+ // omits password/hidden fields, but the write model carries them.
1562
+ // T extends string (rather than keyof LazypockCollections) with a conditional
1563
+ // return type, so the IDE suggests collection names AND unknown/dynamic names
1564
+ // still resolve to the untyped service \u2014 a studio that manages
1565
+ // user-created collections must be able to call collection(someString).
1566
+ //
1567
+ // create()/update() take the collection's *CreateData \u2014 the read model
1568
+ // omits password/hidden fields, but the write model carries them.
1569
+ override collection<T extends string>(name: T): T extends keyof LazypockCollections
1570
+ ? CollectionService<LazypockCollections[T], LazypockCreateData[T]>
1571
+ : CollectionService<unknown> {
1572
+ return super.collection(name) as T extends keyof LazypockCollections
1573
+ ? CollectionService<LazypockCollections[T], LazypockCreateData[T]>
1574
+ : CollectionService<unknown>;
1540
1575
  }
1541
1576
  }
1542
1577
  `);
@@ -1557,6 +1592,15 @@ function memberLine(f) {
1557
1592
  if (type === "never") return "";
1558
1593
  return ` ${JSON.stringify(key)}${req}: ${type};`;
1559
1594
  }
1595
+ function createDataMemberLine(f) {
1596
+ if (f.type === "autodate") return "";
1597
+ const key = fieldKey(f.name);
1598
+ const serverDefaulted = f.options?.defaultValue !== void 0 || f.system && (f.name === "verified" || f.name === "emailVisibility");
1599
+ const req = f.required && !serverDefaulted ? "" : "?";
1600
+ const type = f.type === "password" ? "string" : fieldTypeScriptType(f);
1601
+ if (type === "never") return "";
1602
+ return ` ${JSON.stringify(key)}${req}: ${type};`;
1603
+ }
1560
1604
 
1561
1605
  // src/lazypock.ts
1562
1606
  var LazypockClient = class {