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/README.md +21 -0
- package/dist/{chunk-TBBHGOSH.js → chunk-V2XSEOWF.js} +40 -3
- package/dist/chunk-V2XSEOWF.js.map +1 -0
- package/dist/cli.cjs +39 -2
- package/dist/cli.cjs.map +1 -1
- package/dist/cli.global.js +39 -2
- package/dist/cli.global.js.map +1 -1
- package/dist/cli.js +1 -1
- package/dist/index.cjs +51 -7
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +5 -5
- package/dist/index.d.ts +5 -5
- package/dist/index.global.js +51 -7
- package/dist/index.global.js.map +1 -1
- package/dist/index.js +13 -6
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/codegen.ts +69 -2
- package/src/collection.ts +15 -7
- package/src/http.ts +21 -7
- package/dist/chunk-TBBHGOSH.js.map +0 -1
package/dist/index.d.cts
CHANGED
|
@@ -458,7 +458,7 @@ type RealtimeCallback = (e: RealtimeMessage) => void;
|
|
|
458
458
|
*
|
|
459
459
|
* @typeParam T — The record shape for this collection. Defaults to {@link ApiRecord}.
|
|
460
460
|
*/
|
|
461
|
-
declare class CollectionService<T = ApiRecord> {
|
|
461
|
+
declare class CollectionService<T = ApiRecord, TData = never> {
|
|
462
462
|
private http;
|
|
463
463
|
private collectionName;
|
|
464
464
|
private authStore?;
|
|
@@ -497,7 +497,7 @@ declare class CollectionService<T = ApiRecord> {
|
|
|
497
497
|
* `fields` param — the server returns every non-password field.
|
|
498
498
|
* Pass `select("*")` to restore the default after a projection.
|
|
499
499
|
*/
|
|
500
|
-
select<K extends FieldKey<T> | "*">(...fields: K[]): CollectionService<T>;
|
|
500
|
+
select<K extends FieldKey<T> | "*">(...fields: K[]): CollectionService<T, TData>;
|
|
501
501
|
/**
|
|
502
502
|
* Fields to send with reads:
|
|
503
503
|
* 1. explicit `options.fields` (caller wins)
|
|
@@ -557,7 +557,7 @@ declare class CollectionService<T = ApiRecord> {
|
|
|
557
557
|
* record type), excess/unknown fields are rejected at compile time.
|
|
558
558
|
* @param options Optional request options.
|
|
559
559
|
*/
|
|
560
|
-
create(data: T extends ApiRecord ? Record<string, unknown> : CreateData<T
|
|
560
|
+
create(data: [TData] extends [never] ? T extends ApiRecord ? Record<string, unknown> : CreateData<T> : TData, options?: RequestOptions): Promise<T | null>;
|
|
561
561
|
/**
|
|
562
562
|
* Update a record by ID.
|
|
563
563
|
* @param id Record ID.
|
|
@@ -565,7 +565,7 @@ declare class CollectionService<T = ApiRecord> {
|
|
|
565
565
|
* must be a partial of `T` — unknown fields are rejected.
|
|
566
566
|
* @param options Optional request options.
|
|
567
567
|
*/
|
|
568
|
-
update(id: string, data: T extends ApiRecord ? Record<string, unknown> : UpdateData<T>, options?: RequestOptions): Promise<T | null>;
|
|
568
|
+
update(id: string, data: [TData] extends [never] ? T extends ApiRecord ? Record<string, unknown> : UpdateData<T> : Partial<TData>, options?: RequestOptions): Promise<T | null>;
|
|
569
569
|
/**
|
|
570
570
|
* Delete a record by ID.
|
|
571
571
|
* @param id Record ID.
|
|
@@ -603,7 +603,7 @@ declare class CollectionService<T = ApiRecord> {
|
|
|
603
603
|
* `types.schemas`). Enables schema-aware defaults: hidden fields are
|
|
604
604
|
* excluded from responses, select/expand are validated.
|
|
605
605
|
*/
|
|
606
|
-
withSchema(schema: CollectionSchema): CollectionService<T>;
|
|
606
|
+
withSchema(schema: CollectionSchema): CollectionService<T, TData>;
|
|
607
607
|
/**
|
|
608
608
|
* Subscribe to realtime changes for this collection.
|
|
609
609
|
* The event's `action` is one of `"create" | "update" | "delete"`.
|
package/dist/index.d.ts
CHANGED
|
@@ -458,7 +458,7 @@ type RealtimeCallback = (e: RealtimeMessage) => void;
|
|
|
458
458
|
*
|
|
459
459
|
* @typeParam T — The record shape for this collection. Defaults to {@link ApiRecord}.
|
|
460
460
|
*/
|
|
461
|
-
declare class CollectionService<T = ApiRecord> {
|
|
461
|
+
declare class CollectionService<T = ApiRecord, TData = never> {
|
|
462
462
|
private http;
|
|
463
463
|
private collectionName;
|
|
464
464
|
private authStore?;
|
|
@@ -497,7 +497,7 @@ declare class CollectionService<T = ApiRecord> {
|
|
|
497
497
|
* `fields` param — the server returns every non-password field.
|
|
498
498
|
* Pass `select("*")` to restore the default after a projection.
|
|
499
499
|
*/
|
|
500
|
-
select<K extends FieldKey<T> | "*">(...fields: K[]): CollectionService<T>;
|
|
500
|
+
select<K extends FieldKey<T> | "*">(...fields: K[]): CollectionService<T, TData>;
|
|
501
501
|
/**
|
|
502
502
|
* Fields to send with reads:
|
|
503
503
|
* 1. explicit `options.fields` (caller wins)
|
|
@@ -557,7 +557,7 @@ declare class CollectionService<T = ApiRecord> {
|
|
|
557
557
|
* record type), excess/unknown fields are rejected at compile time.
|
|
558
558
|
* @param options Optional request options.
|
|
559
559
|
*/
|
|
560
|
-
create(data: T extends ApiRecord ? Record<string, unknown> : CreateData<T
|
|
560
|
+
create(data: [TData] extends [never] ? T extends ApiRecord ? Record<string, unknown> : CreateData<T> : TData, options?: RequestOptions): Promise<T | null>;
|
|
561
561
|
/**
|
|
562
562
|
* Update a record by ID.
|
|
563
563
|
* @param id Record ID.
|
|
@@ -565,7 +565,7 @@ declare class CollectionService<T = ApiRecord> {
|
|
|
565
565
|
* must be a partial of `T` — unknown fields are rejected.
|
|
566
566
|
* @param options Optional request options.
|
|
567
567
|
*/
|
|
568
|
-
update(id: string, data: T extends ApiRecord ? Record<string, unknown> : UpdateData<T>, options?: RequestOptions): Promise<T | null>;
|
|
568
|
+
update(id: string, data: [TData] extends [never] ? T extends ApiRecord ? Record<string, unknown> : UpdateData<T> : Partial<TData>, options?: RequestOptions): Promise<T | null>;
|
|
569
569
|
/**
|
|
570
570
|
* Delete a record by ID.
|
|
571
571
|
* @param id Record ID.
|
|
@@ -603,7 +603,7 @@ declare class CollectionService<T = ApiRecord> {
|
|
|
603
603
|
* `types.schemas`). Enables schema-aware defaults: hidden fields are
|
|
604
604
|
* excluded from responses, select/expand are validated.
|
|
605
605
|
*/
|
|
606
|
-
withSchema(schema: CollectionSchema): CollectionService<T>;
|
|
606
|
+
withSchema(schema: CollectionSchema): CollectionService<T, TData>;
|
|
607
607
|
/**
|
|
608
608
|
* Subscribe to realtime changes for this collection.
|
|
609
609
|
* The event's `action` is one of `"create" | "update" | "delete"`.
|
package/dist/index.global.js
CHANGED
|
@@ -103,10 +103,9 @@ var Lazypock = (() => {
|
|
|
103
103
|
}
|
|
104
104
|
const data = await res.json();
|
|
105
105
|
if (data && typeof data.token === "string") {
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
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 Lazypock = (() => {
|
|
|
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 @@ var Lazypock = (() => {
|
|
|
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 @@ var Lazypock = (() => {
|
|
|
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
|
-
|
|
1539
|
-
|
|
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 @@ ${opts.body}
|
|
|
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 {
|