All files / rpc-client/src createFetchClient.ts

100% Statements 15/15
100% Branches 2/2
100% Functions 1/1
100% Lines 13/13

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    1x 1x 1x 1x 1x 1x       1x       6x 6x 6x 6x 6x 6x    
// TODO: package.json declares none of `@jsonjoy.com/buffers`, `json-pack`,
// `rpc-codec-binary` or `rpc-codec-compact`.
import {Writer} from '@jsonjoy.com/buffers/lib/Writer';
import {CborJsonValueCodec} from '@jsonjoy.com/json-pack/lib/codecs/cbor';
import {FetchCaller} from '@jsonjoy.com/rpc-calls/lib/caller/FetchCaller';
import {RpcBinBatchCodec} from '@jsonjoy.com/rpc-codec/lib/RpcBinBatchCodec';
import {RpcCodec} from '@jsonjoy.com/rpc-codec/lib/RpcCodec';
import {RxCompactMessageCodec} from '@jsonjoy.com/rpc-codec-compact';
import type {CallerMethods} from '@jsonjoy.com/rpc-calls/lib/caller/types';
import type {RxMessage} from '@jsonjoy.com/rpc-messages';
 
export const createFetchClient = <Methods extends CallerMethods<any> = CallerMethods>(
  url: string,
  token?: string,
): FetchCaller<Methods> => {
  const writer = new Writer(1024 * 4);
  const val = new CborJsonValueCodec(writer);
  const codec = new RpcCodec<RxMessage>(new RxCompactMessageCodec(), val, val);
  const headers: Record<string, string> = {'Content-Type': 'application/x.' + codec.specifier()};
  if (token) headers.Authorization = token;
  return new FetchCaller<Methods>({url, codec: new RpcBinBatchCodec(codec), headers});
};