fitbit-api-client
Version:
## ⚠️ This SDK is not ready for production
53 lines • 1.61 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseFitbitScope = parseFitbitScope;
exports.exists = exists;
exports.get = get;
exports.getOptionalValue = getOptionalValue;
const constants_1 = require("../constants");
/**
* 値をFitbitScopeに変換する
* @param value
* @returns FitbitScope
*/
function parseFitbitScope(value) {
if (typeof value === 'string' &&
constants_1.FITBIT_SCOPES.find((scope) => scope === value)) {
return value;
}
throw new Error('Invalid FitbitScope');
}
/**
* jsonオブジェクトとキーが存在するかどうかを確認する
* @param json
* @param key
* @returns boolean
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function exists(json, key) {
const value = json[key];
return value !== null && value !== undefined;
}
/**
* オブジェクトとキーが存在する場合はジェネリクスで値を取得し、存在しない場合は例外をスロー
* @param obj
* @param key
* @returns T
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function get(obj, key) {
if (!exists(obj, key)) {
throw new Error(`Key not found: ${key} in ${JSON.stringify(obj)}`);
}
return obj[key];
}
/**
* jsonオブジェクトとキーが存在する場合はジェネリクスで値を取得し、存在しない場合はundefinedを返す
* @param json
* @param key
* @returns T | undefined
*/
function getOptionalValue(json, key) {
return exists(json, key) ? get(json, key) : undefined;
}
//# sourceMappingURL=types.utils.js.map