All files / rpc-server/src/__demos__/json-crdt-server/routes/block/methods new.ts

100% Statements 11/11
100% Branches 0/0
100% Functions 3/3
100% Lines 11/11

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 444x     4x 4x 69x 69x                       69x           69x             69x 48x 48x 48x                  
import {BlockIdRef, BlockBatchPartialRef, BlockSnapshotReturnRef} from '../schema';
import type {RouteDeps, Router, RouterBase} from '../../types';
 
export const new_ =
  ({t, services}: RouteDeps) =>
  <R extends RouterBase>(r: Router<R>) => {
    const Request = t.Object(
      t.Key('id', BlockIdRef).options({
        title: 'New block ID',
        description:
          'The ID of the new block. Must be a unique ID, if the block already exists it will return an error.',
      }),
      t.KeyOpt('batch', BlockBatchPartialRef).options({
        title: 'Batch',
        description: 'A collection of patches to apply to the new block.',
      }),
    );
 
    const Response = t.Object(t.Key('snapshot', BlockSnapshotReturnRef)).options({
      title: 'New block creation response',
      description:
        'The response object for the new block creation, contains server generated metadata without blobs supplied by the client.',
    });
 
    const Func = t.Function(Request, Response).options({
      title: 'Create Block',
      intro: 'Creates a new block out of patches.',
      description:
        'Creates a new block out of supplied patches. A block starts empty with an `undefined` state, and patches are applied to it.',
    });
 
    return r.add('block.new', Func, async ({id, batch}) => {
      const {block} = await services.blocks.create(id, batch);
      const snapshot = block.snapshot;
      return {
        snapshot: {
          id,
          seq: snapshot.seq,
          ts: snapshot.ts,
        },
      };
    });
  };