All files payload.ts

0% Statements 0/15
0% Branches 0/3
0% Functions 0/6
0% Lines 0/11
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                                                                 
import { isEmpty } from 'lodash';
import { IAuthPayload, StringAnyMap, StringStringMap } from './interfaces';

export type PayloadEntry = [string, string];

export default class AuthPayload implements IAuthPayload {
  private PAYLOAD: PayloadEntry[];
  private REVERSE_PAYLOAD: PayloadEntry[];
 
  constructor(payload: StringStringMap = {}) {
    this.PAYLOAD = Object.entries(payload);
    this.REVERSE_PAYLOAD = this.PAYLOAD.map(([pk, k]): PayloadEntry => [k, pk]);
  }
  /**
   * Replaces the keys of a payload
   */
  public reverse(keys: PayloadEntry[], payload: StringAnyMap): StringAnyMap {
    return keys.reduce((p: StringAnyMap, [k, pk]) => {
      const v = payload[k];
 
      if (!isEmpty(v)) p[pk] = v;

      return p;
    }, {});
  }
  public create(payload: StringAnyMap) {
    return this.reverse(this.REVERSE_PAYLOAD, payload);
  }
  public parse(payload: StringAnyMap) {
    return this.reverse(this.PAYLOAD, payload);
  }
}