Skip to content

Commit fb339b4

Browse files
authored
wip: update js client api
1 parent 48e15d2 commit fb339b4

1 file changed

Lines changed: 54 additions & 60 deletions

File tree

crates/assets/js/client/src/index.ts

Lines changed: 54 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -208,15 +208,13 @@ export interface DeleteOp {
208208
};
209209
}
210210

211-
export type Operation = CreateOp | UpdateOp | DeleteOp;
212-
213-
export interface DeferredOperation<ResponseType> {
211+
interface DeferredOperation<ResponseType> {
214212
query(): Promise<ResponseType>;
215213
}
216214

217-
export interface DeferredMutation<ResponseType>
215+
interface DeferredMutation<ResponseType>
218216
extends DeferredOperation<ResponseType> {
219-
toJSON(): Operation;
217+
toJSON(): CreateOp | UpdateOp | DeleteOp;
220218
}
221219

222220
export class CreateOperation<T = Record<string, unknown>>
@@ -227,7 +225,7 @@ export class CreateOperation<T = Record<string, unknown>>
227225
private readonly apiName: string,
228226
private readonly record: Partial<T>,
229227
) {}
230-
async query(): Promise<string> {
228+
async query(): Promise<string | number> {
231229
const response = await this.client.fetch(
232230
`${recordApiBasePath}/${this.apiName}`,
233231
{
@@ -390,7 +388,6 @@ export class ListOperation<T = Record<string, unknown>>
390388
}
391389

392390
export interface RecordApi<T = Record<string, unknown>> {
393-
// Immediate operations
394391
list(opts?: {
395392
pagination?: Pagination;
396393
order?: string[];
@@ -399,22 +396,6 @@ export interface RecordApi<T = Record<string, unknown>> {
399396
expand?: string[];
400397
}): Promise<ListResponse<T>>;
401398

402-
read(
403-
id: string | number,
404-
opt?: {
405-
expand?: string[];
406-
},
407-
): Promise<T>;
408-
409-
create(record: T): Promise<string | number>;
410-
411-
update(id: string | number, record: Partial<T>): Promise<void>;
412-
413-
delete(id: string | number): Promise<void>;
414-
415-
subscribe(id: string | number): Promise<ReadableStream<Event>>;
416-
417-
// Deferred operations
418399
listOp(opts?: {
419400
pagination?: Pagination;
420401
order?: string[];
@@ -423,32 +404,43 @@ export interface RecordApi<T = Record<string, unknown>> {
423404
expand?: string[];
424405
}): ListOperation<T>;
425406

407+
read(
408+
id: string | number,
409+
opt?: {
410+
expand?: string[];
411+
},
412+
): Promise<T>;
413+
426414
readOp(
427415
id: string | number,
428416
opt?: {
429417
expand?: string[];
430418
},
431419
): ReadOperation<T>;
432420

421+
create(record: T): Promise<string | number>;
422+
433423
createOp(record: T): CreateOperation<T>;
434424

425+
update(id: string | number, record: Partial<T>): Promise<void>;
426+
435427
updateOp(id: string | number, record: Partial<T>): UpdateOperation;
436428

429+
delete(id: string | number): Promise<void>;
430+
437431
deleteOp(id: string | number): DeleteOperation;
432+
433+
subscribe(id: string | number): Promise<ReadableStream<Event>>;
438434
}
439435

440436
/// Provides CRUD access to records through TrailBase's record API.
441437
export class RecordApiImpl<T = Record<string, unknown>>
442438
implements RecordApi<T>
443439
{
444-
private readonly _path: string;
445-
446440
constructor(
447441
private readonly client: Client,
448442
private readonly name: string,
449-
) {
450-
this._path = `${recordApiBasePath}/${this.name}`;
451-
}
443+
) {}
452444

453445
public async list(opts?: {
454446
pagination?: Pagination;
@@ -460,6 +452,16 @@ export class RecordApiImpl<T = Record<string, unknown>>
460452
return new ListOperation<T>(this.client, this.name, opts).query();
461453
}
462454

455+
public listOp(opts?: {
456+
pagination?: Pagination;
457+
order?: string[];
458+
filters?: FilterOrComposite[];
459+
count?: boolean;
460+
expand?: string[];
461+
}): ListOperation<T> {
462+
return new ListOperation<T>(this.client, this.name, opts);
463+
}
464+
463465
public async read<T = Record<string, unknown>>(
464466
id: string | number,
465467
opt?: {
@@ -469,20 +471,43 @@ export class RecordApiImpl<T = Record<string, unknown>>
469471
return new ReadOperation<T>(this.client, this.name, id, opt).query();
470472
}
471473

474+
public readOp(
475+
id: string | number,
476+
opt?: {
477+
expand?: string[];
478+
},
479+
): ReadOperation<T> {
480+
return new ReadOperation<T>(this.client, this.name, id, opt);
481+
}
482+
472483
public async create(record: T): Promise<string | number> {
473484
return new CreateOperation<T>(this.client, this.name, record).query();
474485
}
475486

487+
public createOp(record: T): CreateOperation<T> {
488+
return new CreateOperation<T>(this.client, this.name, record);
489+
}
490+
476491
public async update(id: string | number, record: Partial<T>): Promise<void> {
477492
return new UpdateOperation<T>(this.client, this.name, id, record).query();
478493
}
479494

495+
public updateOp(id: string | number, record: Partial<T>): UpdateOperation<T> {
496+
return new UpdateOperation<T>(this.client, this.name, id, record);
497+
}
498+
480499
public async delete(id: string | number): Promise<void> {
481500
return new DeleteOperation(this.client, this.name, id).query();
482501
}
483502

503+
public deleteOp(id: string | number): DeleteOperation {
504+
return new DeleteOperation(this.client, this.name, id);
505+
}
506+
484507
public async subscribe(id: string | number): Promise<ReadableStream<Event>> {
485-
const response = await this.client.fetch(`${this._path}/subscribe/${id}`);
508+
const response = await this.client.fetch(
509+
`${recordApiBasePath}/${this.name}/subscribe/${id}`,
510+
);
486511
const body = response.body;
487512
if (!body) {
488513
throw Error("Subscription reader is null.");
@@ -505,37 +530,6 @@ export class RecordApiImpl<T = Record<string, unknown>>
505530

506531
return body.pipeThrough(transformStream);
507532
}
508-
509-
public listOp(opts?: {
510-
pagination?: Pagination;
511-
order?: string[];
512-
filters?: FilterOrComposite[];
513-
count?: boolean;
514-
expand?: string[];
515-
}): ListOperation<T> {
516-
return new ListOperation<T>(this.client, this.name, opts);
517-
}
518-
519-
public readOp(
520-
id: string | number,
521-
opt?: {
522-
expand?: string[];
523-
},
524-
): ReadOperation<T> {
525-
return new ReadOperation<T>(this.client, this.name, id, opt);
526-
}
527-
528-
public createOp(record: T): CreateOperation<T> {
529-
return new CreateOperation<T>(this.client, this.name, record);
530-
}
531-
532-
public updateOp(id: string | number, record: Partial<T>): UpdateOperation<T> {
533-
return new UpdateOperation<T>(this.client, this.name, id, record);
534-
}
535-
536-
public deleteOp(id: string | number): DeleteOperation {
537-
return new DeleteOperation(this.client, this.name, id);
538-
}
539533
}
540534

541535
class ThinClient {

0 commit comments

Comments
 (0)