summaryrefslogtreecommitdiffhomepage
path: root/gui/src/shared/relay-location-builder.ts
blob: 14bf72b65f4a7930a4bb6db39569be946e0c5cfb (plain)
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
import { Constraint, LiftedConstraint, RelayLocation } from './daemon-rpc-types';

export interface ILocationBuilder<Self> {
  country(country: string): Self;
  city(country: string, city: string): Self;
  hostname(country: string, city: string, hostname: string): Self;
  any(): Self;
  fromRaw(location: LiftedConstraint<RelayLocation>): Self;
}

export default function makeLocationBuilder<T>(
  context: T,
  receiver: (constraint: Constraint<RelayLocation>) => void,
): ILocationBuilder<T> {
  return {
    country: (country: string) => {
      receiver({ only: { country } });
      return context;
    },
    city: (country: string, city: string) => {
      receiver({ only: { city: [country, city] } });
      return context;
    },
    hostname: (country: string, city: string, hostname: string) => {
      receiver({ only: { hostname: [country, city, hostname] } });
      return context;
    },
    any: () => {
      receiver('any');
      return context;
    },
    fromRaw(location: LiftedConstraint<RelayLocation>) {
      if (location === 'any') {
        return this.any();
      } else {
        receiver({ only: location });
        return context;
      }
    },
  };
}