responseCodeClass.tsx 684 B

1234567891011121314151617181920212223
  1. import {decodeScalar} from 'sentry/utils/queryString';
  2. const OPTIONS = ['' as const, '2' as const, '3' as const, '4' as const, '5' as const];
  3. const DEFAULT = '';
  4. type ResponseCodeClass = (typeof OPTIONS)[number];
  5. export default function decode(
  6. value: string | string[] | undefined | null
  7. ): ResponseCodeClass {
  8. const decodedValue = decodeScalar(value, DEFAULT);
  9. if (isAValidOption(decodedValue)) {
  10. return decodedValue;
  11. }
  12. return DEFAULT;
  13. }
  14. function isAValidOption(maybeOption: string): maybeOption is ResponseCodeClass {
  15. // Manually widen to allow the comparison to string
  16. return (OPTIONS as unknown as string[]).includes(maybeOption as ResponseCodeClass);
  17. }