All files / src/oauth request-params.ts

100% Statements 11/11
100% Branches 8/8
100% Functions 1/1
100% Lines 9/9

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                  220x       107x 107x 107x 730x 730x 339x 330x   98x    
/**
 * Single-valued parameters of a form-encoded OAuth request
 */
 
/**
 * The named parameters of a request body, or the name of one that is not a single string.
 * RFC 6749 section 3.2: request parameters must not be included more than once; a repeated
 * one arrives as an array (or an object, with nested syntax) and must not reach a lookup.
 */
export function singleValuedParams<Name extends string>(
  body: unknown,
  names: readonly Name[],
): Partial<Record<Name, string>> | Name {
  const source = (typeof body === 'object' && body !== null ? body : {}) as Record<string, unknown>;
  const params: Partial<Record<Name, string>> = {};
  for (const name of names) {
    const value = source[name];
    if (value === undefined) continue;
    if (typeof value !== 'string') return name;
    params[name] = value;
  }
  return params;
}