All files / src/api Metadata.ts

100% Statements 27/27
100% Branches 12/12
100% Functions 6/6
100% Lines 27/27

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 1741x 1x       1x                                 1x     3x 3x   3x                                           1x     3x 3x   3x                                           1x     3x 3x   3x                                           1x     3x 3x   3x                                           1x     3x 3x   3x                                           1x     3x 3x   3x          
import { Client } from "../Client";
import { GET, METADATA_AREA_ENDPOINT, METADATA_TYPES_ENDPOINT, METADATA_UNITS_ENDPOINT, POST } from "../Constants";
import { AreaResponse } from "../interfaces/response/AreaResponse";
import { TypeResponse } from "../interfaces/response/TypeResponse";
import { UnitResponse } from "../interfaces/response/UnitResponse";
import { makeApiRequest } from "../request";
 
/**
 * Retrieves available emission source types for a specific endpoint using GET request.
 *
 * @export
 * @param {string} [endpoint] - The endpoint name to retrieve types for. Defaults to 'calculation' if not provided.
 * @return {Promise<TypeResponse>} A promise that resolves to a TypeResponse containing the available types
 * @throws {Error} May throw an error if the API request fails
 *
 * @example
 * // Get types for calculation endpoint (default)
 * const types = await getTypes();
 *
 * // Get types for a specific endpoint
 * const locationTypes = await getTypes('location');
 */
export async function getTypes(
  endpoint?: string
): Promise<TypeResponse> {
  const client = Client.getInstance();
  const url = client.getDomain() + METADATA_TYPES_ENDPOINT;
 
  return makeApiRequest<TypeResponse>({
    method: GET,
    url,
    params: endpoint ? { endpoint } : undefined,
  });
}
 
/**
 * Retrieves available emission source types for a specific endpoint using POST request.
 *
 * @export
 * @param {string} [endpoint] - The endpoint name to retrieve types for. Defaults to 'calculation' if not provided.
 * @return {Promise<TypeResponse>} A promise that resolves to a TypeResponse containing the available types
 * @throws {Error} May throw an error if the API request fails
 *
 * @example
 * // Get types for calculation endpoint (default)
 * const types = await postTypes();
 *
 * // Get types for a specific endpoint
 * const stationaryTypes = await postTypes('stationary');
 */
export async function postTypes(
  endpoint?: string
): Promise<TypeResponse> {
  const client = Client.getInstance();
  const url = client.getDomain() + METADATA_TYPES_ENDPOINT;
 
  return makeApiRequest<TypeResponse>({
    method: POST,
    url,
    data: endpoint ? { endpoint } : {},
  });
}
 
/**
 * Retrieves available geographic locations for a specific endpoint using GET request.
 *
 * @export
 * @param {string} [endpoint] - The endpoint name to retrieve locations for. Defaults to 'calculation' if not provided.
 * @return {Promise<AreaResponse>} A promise that resolves to an AreaResponse containing the available locations
 * @throws {Error} May throw an error if the API request fails
 *
 * @example
 * // Get areas for calculation endpoint (default)
 * const areas = await getArea();
 *
 * // Get areas for a specific endpoint
 * const mobileAreas = await getArea('mobile');
 */
export async function getArea(
  endpoint?: string
): Promise<AreaResponse> {
  const client = Client.getInstance();
  const url = client.getDomain() + METADATA_AREA_ENDPOINT;
 
  return makeApiRequest<AreaResponse>({
    method: GET,
    url,
    params: endpoint ? { endpoint } : undefined,
  });
}
 
/**
 * Retrieves available geographic locations for a specific endpoint using POST request.
 *
 * @export
 * @param {string} [endpoint] - The endpoint name to retrieve locations for. Defaults to 'calculation' if not provided.
 * @return {Promise<AreaResponse>} A promise that resolves to an AreaResponse containing the available locations
 * @throws {Error} May throw an error if the API request fails
 *
 * @example
 * // Get areas for calculation endpoint (default)
 * const areas = await postArea();
 *
 * // Get areas for a specific endpoint
 * const fugitiveAreas = await postArea('fugitive');
 */
export async function postArea(
  endpoint?: string
): Promise<AreaResponse> {
  const client = Client.getInstance();
  const url = client.getDomain() + METADATA_AREA_ENDPOINT;
 
  return makeApiRequest<AreaResponse>({
    method: POST,
    url,
    data: endpoint ? { endpoint } : {},
  });
}
 
/**
 * Retrieves available measurement units for a specific type using GET request.
 *
 * @export
 * @param {string} [type] - The emission source type to retrieve units for. If not provided, returns the full UOM table.
 * @return {Promise<UnitResponse>} A promise that resolves to a UnitResponse containing the available units
 * @throws {Error} May throw an error if the API request fails
 *
 * @example
 * // Get all units
 * const allUnits = await getUnits();
 *
 * // Get units for a specific type
 * const naturalGasUnits = await getUnits('Natural Gas');
 */
export async function getUnits(
  type?: string
): Promise<UnitResponse> {
  const client = Client.getInstance();
  const url = client.getDomain() + METADATA_UNITS_ENDPOINT;
 
  return makeApiRequest<UnitResponse>({
    method: GET,
    url,
    params: type ? { type } : undefined,
  });
}
 
/**
 * Retrieves available measurement units for a specific type using POST request.
 *
 * @export
 * @param {string} [type] - The emission source type to retrieve units for. If not provided, returns the full UOM table.
 * @return {Promise<UnitResponse>} A promise that resolves to a UnitResponse containing the available units
 * @throws {Error} May throw an error if the API request fails
 *
 * @example
 * // Get all units
 * const allUnits = await postUnits();
 *
 * // Get units for a specific type
 * const jetKeroseneUnits = await postUnits('Jet Kerosene');
 */
export async function postUnits(
  type?: string
): Promise<UnitResponse> {
  const client = Client.getInstance();
  const url = client.getDomain() + METADATA_UNITS_ENDPOINT;
 
  return makeApiRequest<UnitResponse>({
    method: POST,
    url,
    data: type ? { type } : {},
  });
}