All files / utils apiKeyUtils.js

100% Statements 7/7
100% Branches 3/3
100% Functions 4/4
100% Lines 7/7

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  8x             79x                 9x           8x             83x       8x       17x        
export const API_KEYS_DOCS_URL =
  "https://github.com/ucsb-cs156/proj-frontiers/blob/main/docs/api-keys.md";
 
/**
 * Query key for the list of a course's API keys. Shared by the table and the
 * create/revoke mutations, so that either refreshes the list.
 */
export function apiKeysQueryKey(courseId) {
  return `/api/courses/key?courseId=${courseId}`;
}
 
/**
 * A ready-to-paste example request for the new-key modal: the student lookup
 * endpoint that autograders use, against this deployment, with the course id
 * and the key filled in.
 */
export function studentInfoCurlExample(courseId, key) {
  return (
    `curl -s '${window.location.origin}/api/courses/studentInfo?courseId=${courseId}&email=cgaucho@ucsb.edu' \\\n` +
    `  -H 'X-API-KEY: ${key}'`
  );
}
 
const MS_PER_DAY = 24 * 60 * 60 * 1000;
 
/**
 * Whole days from now until the timestamp, rounded up (so anything later today
 * counts as 1). Negative once the timestamp has passed.
 */
export function daysUntil(timestamp, now = new Date()) {
  return Math.ceil((new Date(timestamp) - now) / MS_PER_DAY);
}
 
/** An active key that expires within this many days gets a warning badge. */
export const EXPIRY_WARNING_DAYS = 14;
 
/** How a key is referred to in messages: its suffix, plus its label if it has one. */
export function describeKey(apiKey) {
  return apiKey.label
    ? `…${apiKey.keySuffix} (${apiKey.label})`
    : `…${apiKey.keySuffix}`;
}