All files / src/utils/typeGuards synthetika.ts

100% Statements 105/105
100% Branches 15/15
100% Functions 5/5
100% Lines 105/105

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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 1881x 1x   1x   1x 1x 1x 1x 1x 1x   1x                           1x 1x 8x 8x 6x 6x 6x 6x 6x 6x 8x 8x   1x                               1x 1x 5x   5x 3x 3x   5x 1x 1x 5x   1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x   1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x                           1x 1x 14x 14x 11x 11x 11x 11x 11x 11x 14x 14x   1x                           1x 1x 7x 7x   1x                           1x 1x 5x   5x 3x 3x 3x   5x 1x 1x 1x 5x  
import * as v from 'valibot';
import { Logger } from '@/utils/Logger';
 
const logger = new Logger('typeGuards.synthetika');
 
const synthetikaSearchResponseSchema = v.object({
  count: v.number(),
  pages: v.number(),
  page: v.number(),
  list: v.array(v.unknown()),
});
 
/**
 * This can be used to check if a SynthetikaSearchResponse is valid
 *
 * @category Typeguards
 * @param data - The data to verify (raw response from Synthetika)
 * @returns True if the data is a SynthetikaSearchResponse, false otherwise
 * @example
 * ```typescript
 * const response = await fetch("https://synthetikaeu.com/webapi/front/en_US/products/usd/search/sodium%20chloride");
 * if (isSynthetikaSearchResponse(response)) {
 *   console.log(response.count);
 * }
 * ```
 * @source
 */
export function isSynthetikaSearchResponse(data: unknown): data is SynthetikaSearchResponse {
  const check = v.safeParse(synthetikaSearchResponseSchema, data);
  if (!check.success) {
    logger.warn('isSynthetikaSearchResponse: data is not a SynthetikaSearchResponse', {
      data,
      check,
      issues: check.issues,
    });
  }
  return check.success;
}
 
/**
 * This can be used to check if a SynthetikaSearchResponse is valid
 *
 * @category Typeguards
 * @param data - The data to verify (raw response from Synthetika)
 * @returns True if the data is a SynthetikaSearchResponse, false otherwise
 * @example
 * ```typescript
 * const response = await fetch("https://synthetikaeu.com/webapi/front/en_US/products/usd/search/sodium%20chloride");
 * assertIsSynthetikaSearchResponse(response);
 * const data = await response.json();
 * if (isSynthetikaSearchResponse(data)) {
 *   console.log(data.count);
 * }
 * ```
 * @source
 */
export function assertIsSynthetikaSearchResponse(
  data: unknown,
): asserts data is SynthetikaSearchResponse {
  if (!data || typeof data !== 'object') {
    throw new Error('isSynthetikaSearchResponse: data is falsey or not an object');
  }
 
  if (!isSynthetikaSearchResponse(data)) {
    throw new Error('isSynthetikaSearchResponse: data is not a SynthetikaSearchResponse');
  }
}
 
const synthetikaProductPriceSchema = v.object({
  base: v.string(),
  base_float: v.number(),
  final: v.string(),
  final_float: v.number(),
});
 
const synthetikaConfigurationOptionValueSchema = v.object({
  id: v.string(),
  order: v.string(),
  name: v.string(),
});
 
const synthetikaConfigurationOptionSchema = v.object({
  values: v.array(synthetikaConfigurationOptionValueSchema),
});
 
const synthetikaProductResponseSchema = v.object({
  id: v.number(),
  name: v.string(),
  can_buy: v.boolean(),
  code: v.string(),
  unit: v.object({
    name: v.string(),
    floating_point: v.boolean(),
  }),
  //stockId: v.number(),
  url: v.string(),
  availability: v.object({
    name: v.string(),
  }),
  price: v.object({
    gross: synthetikaProductPriceSchema,
    net: synthetikaProductPriceSchema,
  }),
  weight: v.object({
    weight_float: v.number(),
    weight: v.string(),
  }),
  //producer: v.nullable(v.record(v.string(), v.unknown())),
  shortDescription: v.string(),
  description: v.string(),
  options_configuration: v.optional(v.array(synthetikaConfigurationOptionSchema)),
});
 
/**
 * This can be used to typeguard a SynthetikaProduct
 *
 * @category Typeguards
 * @param data - The data to typeguard
 * @returns True if the data is a SynthetikaProduct, false otherwise
 * @example
 * ```typescript
 * const product = { id: 1, name: "Product 1", url: "https://example.com" };
 * if (isSynthetikaProduct(product)) {
 *   console.log(product.name);
 * }
 * ```
 * @source
 */
export function isSynthetikaProduct(data: unknown): data is SynthetikaProduct {
  const check = v.safeParse(synthetikaProductResponseSchema, data);
  if (!check.success) {
    logger.warn('isSynthetikaProduct: data is not a SynthetikaProduct', {
      data,
      check,
      issues: check.issues,
    });
  }
  return check.success;
}
 
/**
 * This can be used to typeguard the .price.gross and .price.net fields of a SynthetikaProduct
 *
 * @category Typeguards
 * @param data - The data to typeguard
 * @returns True if the data is a SynthetikaProductPrice, false otherwise
 * @example
 * ```typescript
 * const product = { price: { gross: { final: "100" }, net: { final: "90" } } };
 * if (isSynthetikaProductPrice(product.price)) {
 *   console.log(product.price.gross.final);
 * }
 * ```
 * @source
 */
export function isSynthetikaProductPrice(data: unknown): data is SynthetikaProductPrice {
  return v.safeParse(synthetikaProductPriceSchema, data).success;
}
 
/**
 * This can be used to verify the .price.gross and .price.net fields of a SynthetikaProduct
 *
 * @category Typeguards
 * @param data - The data to verify (raw response from Synthetika)
 * @returns True if the data is a SynthetikaProductPrice, false otherwise
 * @example
 * ```typescript
 * const product = { price: { gross: { final: "100" }, net: { final: "90" } } };
 * assertIsSynthetikaProductPrice(product.price.gross);
 * assertIsSynthetikaProductPrice(product.price.net);
 * console.log(product.price.gross.final);
 * ```
 * @source
 */
export function assertIsSynthetikaProductPrice(
  data: unknown,
): asserts data is SynthetikaProductPrice {
  if (!data || typeof data !== 'object') {
    logger.debug('isSynthetikaProductPrice: data is falsey or not an object');
    throw new Error('isSynthetikaProductPrice: data is falsey or not an object');
  }
 
  if (!isSynthetikaProductPrice(data)) {
    logger.debug('isSynthetikaProductPrice: data is missing base or final');
    throw new Error('isSynthetikaProductPrice: data is missing base or final');
  }
}