@trophyso/node
Version:
NodeJS SDK for the Trophy API
599 lines (598 loc) • 30.4 kB
JavaScript
"use strict";
/**
* This file was auto-generated by Fern from our API Definition.
*/
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Users = void 0;
const environments = __importStar(require("../../../../environments"));
const core = __importStar(require("../../../../core"));
const TrophyApi = __importStar(require("../../.."));
const serializers = __importStar(require("../../../../serialization"));
const url_join_1 = __importDefault(require("url-join"));
const errors = __importStar(require("../../../../errors"));
class Users {
constructor(_options) {
this._options = _options;
}
/**
* Identify a new user.
* @throws {@link TrophyApi.BadRequestError}
* @throws {@link TrophyApi.UnauthorizedError}
* @throws {@link TrophyApi.UnprocessableEntityError}
*
* @example
* await trophyApi.users.create({
* id: "user-id"
* })
*/
create(request, requestOptions) {
var _a;
return __awaiter(this, void 0, void 0, function* () {
const _response = yield core.fetcher({
url: (0, url_join_1.default)((_a = (yield core.Supplier.get(this._options.environment))) !== null && _a !== void 0 ? _a : environments.TrophyApiEnvironment.Default, "users"),
method: "POST",
headers: {
"X-API-KEY": yield core.Supplier.get(this._options.apiKey),
"X-Fern-Language": "JavaScript",
},
contentType: "application/json",
body: yield serializers.UpsertedUser.jsonOrThrow(request, { unrecognizedObjectKeys: "strip" }),
timeoutMs: (requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.timeoutInSeconds) != null ? requestOptions.timeoutInSeconds * 1000 : 60000,
maxRetries: requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.maxRetries,
});
if (_response.ok) {
return yield serializers.User.parseOrThrow(_response.body, {
unrecognizedObjectKeys: "passthrough",
allowUnrecognizedUnionMembers: true,
allowUnrecognizedEnumValues: true,
breadcrumbsPrefix: ["response"],
});
}
if (_response.error.reason === "status-code") {
switch (_response.error.statusCode) {
case 400:
throw new TrophyApi.BadRequestError(yield serializers.ErrorBody.parseOrThrow(_response.error.body, {
unrecognizedObjectKeys: "passthrough",
allowUnrecognizedUnionMembers: true,
allowUnrecognizedEnumValues: true,
breadcrumbsPrefix: ["response"],
}));
case 401:
throw new TrophyApi.UnauthorizedError(yield serializers.ErrorBody.parseOrThrow(_response.error.body, {
unrecognizedObjectKeys: "passthrough",
allowUnrecognizedUnionMembers: true,
allowUnrecognizedEnumValues: true,
breadcrumbsPrefix: ["response"],
}));
case 422:
throw new TrophyApi.UnprocessableEntityError(yield serializers.ErrorBody.parseOrThrow(_response.error.body, {
unrecognizedObjectKeys: "passthrough",
allowUnrecognizedUnionMembers: true,
allowUnrecognizedEnumValues: true,
breadcrumbsPrefix: ["response"],
}));
default:
throw new errors.TrophyApiError({
statusCode: _response.error.statusCode,
body: _response.error.body,
});
}
}
switch (_response.error.reason) {
case "non-json":
throw new errors.TrophyApiError({
statusCode: _response.error.statusCode,
body: _response.error.rawBody,
});
case "timeout":
throw new errors.TrophyApiTimeoutError();
case "unknown":
throw new errors.TrophyApiError({
message: _response.error.errorMessage,
});
}
});
}
/**
* Get a single user.
* @throws {@link TrophyApi.BadRequestError}
* @throws {@link TrophyApi.UnauthorizedError}
* @throws {@link TrophyApi.UnprocessableEntityError}
*
* @example
* await trophyApi.users.get("userId")
*/
get(id, requestOptions) {
var _a;
return __awaiter(this, void 0, void 0, function* () {
const _response = yield core.fetcher({
url: (0, url_join_1.default)((_a = (yield core.Supplier.get(this._options.environment))) !== null && _a !== void 0 ? _a : environments.TrophyApiEnvironment.Default, `users/${id}`),
method: "GET",
headers: {
"X-API-KEY": yield core.Supplier.get(this._options.apiKey),
"X-Fern-Language": "JavaScript",
},
contentType: "application/json",
timeoutMs: (requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.timeoutInSeconds) != null ? requestOptions.timeoutInSeconds * 1000 : 60000,
maxRetries: requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.maxRetries,
});
if (_response.ok) {
return yield serializers.User.parseOrThrow(_response.body, {
unrecognizedObjectKeys: "passthrough",
allowUnrecognizedUnionMembers: true,
allowUnrecognizedEnumValues: true,
breadcrumbsPrefix: ["response"],
});
}
if (_response.error.reason === "status-code") {
switch (_response.error.statusCode) {
case 400:
throw new TrophyApi.BadRequestError(yield serializers.ErrorBody.parseOrThrow(_response.error.body, {
unrecognizedObjectKeys: "passthrough",
allowUnrecognizedUnionMembers: true,
allowUnrecognizedEnumValues: true,
breadcrumbsPrefix: ["response"],
}));
case 401:
throw new TrophyApi.UnauthorizedError(yield serializers.ErrorBody.parseOrThrow(_response.error.body, {
unrecognizedObjectKeys: "passthrough",
allowUnrecognizedUnionMembers: true,
allowUnrecognizedEnumValues: true,
breadcrumbsPrefix: ["response"],
}));
case 422:
throw new TrophyApi.UnprocessableEntityError(yield serializers.ErrorBody.parseOrThrow(_response.error.body, {
unrecognizedObjectKeys: "passthrough",
allowUnrecognizedUnionMembers: true,
allowUnrecognizedEnumValues: true,
breadcrumbsPrefix: ["response"],
}));
default:
throw new errors.TrophyApiError({
statusCode: _response.error.statusCode,
body: _response.error.body,
});
}
}
switch (_response.error.reason) {
case "non-json":
throw new errors.TrophyApiError({
statusCode: _response.error.statusCode,
body: _response.error.rawBody,
});
case "timeout":
throw new errors.TrophyApiTimeoutError();
case "unknown":
throw new errors.TrophyApiError({
message: _response.error.errorMessage,
});
}
});
}
/**
* Update a user.
* @throws {@link TrophyApi.BadRequestError}
* @throws {@link TrophyApi.UnauthorizedError}
* @throws {@link TrophyApi.UnprocessableEntityError}
*
* @example
* await trophyApi.users.update("id", {
* email: "user@example.com",
* tz: "Europe/London"
* })
*/
update(id, request, requestOptions) {
var _a;
return __awaiter(this, void 0, void 0, function* () {
const _response = yield core.fetcher({
url: (0, url_join_1.default)((_a = (yield core.Supplier.get(this._options.environment))) !== null && _a !== void 0 ? _a : environments.TrophyApiEnvironment.Default, `users/${id}`),
method: "PATCH",
headers: {
"X-API-KEY": yield core.Supplier.get(this._options.apiKey),
"X-Fern-Language": "JavaScript",
},
contentType: "application/json",
body: yield serializers.UpdatedUser.jsonOrThrow(request, { unrecognizedObjectKeys: "strip" }),
timeoutMs: (requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.timeoutInSeconds) != null ? requestOptions.timeoutInSeconds * 1000 : 60000,
maxRetries: requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.maxRetries,
});
if (_response.ok) {
return yield serializers.User.parseOrThrow(_response.body, {
unrecognizedObjectKeys: "passthrough",
allowUnrecognizedUnionMembers: true,
allowUnrecognizedEnumValues: true,
breadcrumbsPrefix: ["response"],
});
}
if (_response.error.reason === "status-code") {
switch (_response.error.statusCode) {
case 400:
throw new TrophyApi.BadRequestError(yield serializers.ErrorBody.parseOrThrow(_response.error.body, {
unrecognizedObjectKeys: "passthrough",
allowUnrecognizedUnionMembers: true,
allowUnrecognizedEnumValues: true,
breadcrumbsPrefix: ["response"],
}));
case 401:
throw new TrophyApi.UnauthorizedError(yield serializers.ErrorBody.parseOrThrow(_response.error.body, {
unrecognizedObjectKeys: "passthrough",
allowUnrecognizedUnionMembers: true,
allowUnrecognizedEnumValues: true,
breadcrumbsPrefix: ["response"],
}));
case 422:
throw new TrophyApi.UnprocessableEntityError(yield serializers.ErrorBody.parseOrThrow(_response.error.body, {
unrecognizedObjectKeys: "passthrough",
allowUnrecognizedUnionMembers: true,
allowUnrecognizedEnumValues: true,
breadcrumbsPrefix: ["response"],
}));
default:
throw new errors.TrophyApiError({
statusCode: _response.error.statusCode,
body: _response.error.body,
});
}
}
switch (_response.error.reason) {
case "non-json":
throw new errors.TrophyApiError({
statusCode: _response.error.statusCode,
body: _response.error.rawBody,
});
case "timeout":
throw new errors.TrophyApiTimeoutError();
case "unknown":
throw new errors.TrophyApiError({
message: _response.error.errorMessage,
});
}
});
}
/**
* Get a single user's progress against all active metrics.
* @throws {@link TrophyApi.UnauthorizedError}
* @throws {@link TrophyApi.NotFoundError}
* @throws {@link TrophyApi.UnprocessableEntityError}
*
* @example
* await trophyApi.users.allMetrics("userId")
*/
allMetrics(id, requestOptions) {
var _a;
return __awaiter(this, void 0, void 0, function* () {
const _response = yield core.fetcher({
url: (0, url_join_1.default)((_a = (yield core.Supplier.get(this._options.environment))) !== null && _a !== void 0 ? _a : environments.TrophyApiEnvironment.Default, `users/${id}/metrics`),
method: "GET",
headers: {
"X-API-KEY": yield core.Supplier.get(this._options.apiKey),
"X-Fern-Language": "JavaScript",
},
contentType: "application/json",
timeoutMs: (requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.timeoutInSeconds) != null ? requestOptions.timeoutInSeconds * 1000 : 60000,
maxRetries: requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.maxRetries,
});
if (_response.ok) {
return yield serializers.users.allMetrics.Response.parseOrThrow(_response.body, {
unrecognizedObjectKeys: "passthrough",
allowUnrecognizedUnionMembers: true,
allowUnrecognizedEnumValues: true,
breadcrumbsPrefix: ["response"],
});
}
if (_response.error.reason === "status-code") {
switch (_response.error.statusCode) {
case 401:
throw new TrophyApi.UnauthorizedError(yield serializers.ErrorBody.parseOrThrow(_response.error.body, {
unrecognizedObjectKeys: "passthrough",
allowUnrecognizedUnionMembers: true,
allowUnrecognizedEnumValues: true,
breadcrumbsPrefix: ["response"],
}));
case 404:
throw new TrophyApi.NotFoundError(yield serializers.ErrorBody.parseOrThrow(_response.error.body, {
unrecognizedObjectKeys: "passthrough",
allowUnrecognizedUnionMembers: true,
allowUnrecognizedEnumValues: true,
breadcrumbsPrefix: ["response"],
}));
case 422:
throw new TrophyApi.UnprocessableEntityError(yield serializers.ErrorBody.parseOrThrow(_response.error.body, {
unrecognizedObjectKeys: "passthrough",
allowUnrecognizedUnionMembers: true,
allowUnrecognizedEnumValues: true,
breadcrumbsPrefix: ["response"],
}));
default:
throw new errors.TrophyApiError({
statusCode: _response.error.statusCode,
body: _response.error.body,
});
}
}
switch (_response.error.reason) {
case "non-json":
throw new errors.TrophyApiError({
statusCode: _response.error.statusCode,
body: _response.error.rawBody,
});
case "timeout":
throw new errors.TrophyApiTimeoutError();
case "unknown":
throw new errors.TrophyApiError({
message: _response.error.errorMessage,
});
}
});
}
/**
* Get a user's progress against a single active metric.
* @throws {@link TrophyApi.UnauthorizedError}
* @throws {@link TrophyApi.NotFoundError}
* @throws {@link TrophyApi.UnprocessableEntityError}
*
* @example
* await trophyApi.users.singleMetric("userId", "key")
*/
singleMetric(id, key, requestOptions) {
var _a;
return __awaiter(this, void 0, void 0, function* () {
const _response = yield core.fetcher({
url: (0, url_join_1.default)((_a = (yield core.Supplier.get(this._options.environment))) !== null && _a !== void 0 ? _a : environments.TrophyApiEnvironment.Default, `users/${id}/metrics/${key}`),
method: "GET",
headers: {
"X-API-KEY": yield core.Supplier.get(this._options.apiKey),
"X-Fern-Language": "JavaScript",
},
contentType: "application/json",
timeoutMs: (requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.timeoutInSeconds) != null ? requestOptions.timeoutInSeconds * 1000 : 60000,
maxRetries: requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.maxRetries,
});
if (_response.ok) {
return yield serializers.MetricResponse.parseOrThrow(_response.body, {
unrecognizedObjectKeys: "passthrough",
allowUnrecognizedUnionMembers: true,
allowUnrecognizedEnumValues: true,
breadcrumbsPrefix: ["response"],
});
}
if (_response.error.reason === "status-code") {
switch (_response.error.statusCode) {
case 401:
throw new TrophyApi.UnauthorizedError(yield serializers.ErrorBody.parseOrThrow(_response.error.body, {
unrecognizedObjectKeys: "passthrough",
allowUnrecognizedUnionMembers: true,
allowUnrecognizedEnumValues: true,
breadcrumbsPrefix: ["response"],
}));
case 404:
throw new TrophyApi.NotFoundError(yield serializers.ErrorBody.parseOrThrow(_response.error.body, {
unrecognizedObjectKeys: "passthrough",
allowUnrecognizedUnionMembers: true,
allowUnrecognizedEnumValues: true,
breadcrumbsPrefix: ["response"],
}));
case 422:
throw new TrophyApi.UnprocessableEntityError(yield serializers.ErrorBody.parseOrThrow(_response.error.body, {
unrecognizedObjectKeys: "passthrough",
allowUnrecognizedUnionMembers: true,
allowUnrecognizedEnumValues: true,
breadcrumbsPrefix: ["response"],
}));
default:
throw new errors.TrophyApiError({
statusCode: _response.error.statusCode,
body: _response.error.body,
});
}
}
switch (_response.error.reason) {
case "non-json":
throw new errors.TrophyApiError({
statusCode: _response.error.statusCode,
body: _response.error.rawBody,
});
case "timeout":
throw new errors.TrophyApiTimeoutError();
case "unknown":
throw new errors.TrophyApiError({
message: _response.error.errorMessage,
});
}
});
}
/**
* Get all of a user's completed achievements.
* @throws {@link TrophyApi.UnauthorizedError}
* @throws {@link TrophyApi.NotFoundError}
* @throws {@link TrophyApi.UnprocessableEntityError}
*
* @example
* await trophyApi.users.allAchievements("userId")
*/
allAchievements(id, requestOptions) {
var _a;
return __awaiter(this, void 0, void 0, function* () {
const _response = yield core.fetcher({
url: (0, url_join_1.default)((_a = (yield core.Supplier.get(this._options.environment))) !== null && _a !== void 0 ? _a : environments.TrophyApiEnvironment.Default, `users/${id}/achievements`),
method: "GET",
headers: {
"X-API-KEY": yield core.Supplier.get(this._options.apiKey),
"X-Fern-Language": "JavaScript",
},
contentType: "application/json",
timeoutMs: (requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.timeoutInSeconds) != null ? requestOptions.timeoutInSeconds * 1000 : 60000,
maxRetries: requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.maxRetries,
});
if (_response.ok) {
return yield serializers.users.allAchievements.Response.parseOrThrow(_response.body, {
unrecognizedObjectKeys: "passthrough",
allowUnrecognizedUnionMembers: true,
allowUnrecognizedEnumValues: true,
breadcrumbsPrefix: ["response"],
});
}
if (_response.error.reason === "status-code") {
switch (_response.error.statusCode) {
case 401:
throw new TrophyApi.UnauthorizedError(yield serializers.ErrorBody.parseOrThrow(_response.error.body, {
unrecognizedObjectKeys: "passthrough",
allowUnrecognizedUnionMembers: true,
allowUnrecognizedEnumValues: true,
breadcrumbsPrefix: ["response"],
}));
case 404:
throw new TrophyApi.NotFoundError(yield serializers.ErrorBody.parseOrThrow(_response.error.body, {
unrecognizedObjectKeys: "passthrough",
allowUnrecognizedUnionMembers: true,
allowUnrecognizedEnumValues: true,
breadcrumbsPrefix: ["response"],
}));
case 422:
throw new TrophyApi.UnprocessableEntityError(yield serializers.ErrorBody.parseOrThrow(_response.error.body, {
unrecognizedObjectKeys: "passthrough",
allowUnrecognizedUnionMembers: true,
allowUnrecognizedEnumValues: true,
breadcrumbsPrefix: ["response"],
}));
default:
throw new errors.TrophyApiError({
statusCode: _response.error.statusCode,
body: _response.error.body,
});
}
}
switch (_response.error.reason) {
case "non-json":
throw new errors.TrophyApiError({
statusCode: _response.error.statusCode,
body: _response.error.rawBody,
});
case "timeout":
throw new errors.TrophyApiTimeoutError();
case "unknown":
throw new errors.TrophyApiError({
message: _response.error.errorMessage,
});
}
});
}
/**
* Get a user's streak data.
* @throws {@link TrophyApi.UnauthorizedError}
* @throws {@link TrophyApi.NotFoundError}
* @throws {@link TrophyApi.UnprocessableEntityError}
*
* @example
* await trophyApi.users.streak("userId", {})
*/
streak(id, request = {}, requestOptions) {
var _a;
return __awaiter(this, void 0, void 0, function* () {
const { historyPeriods } = request;
const _queryParams = {};
if (historyPeriods != null) {
_queryParams["historyPeriods"] = historyPeriods.toString();
}
const _response = yield core.fetcher({
url: (0, url_join_1.default)((_a = (yield core.Supplier.get(this._options.environment))) !== null && _a !== void 0 ? _a : environments.TrophyApiEnvironment.Default, `users/${id}/streak`),
method: "GET",
headers: {
"X-API-KEY": yield core.Supplier.get(this._options.apiKey),
"X-Fern-Language": "JavaScript",
},
contentType: "application/json",
queryParameters: _queryParams,
timeoutMs: (requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.timeoutInSeconds) != null ? requestOptions.timeoutInSeconds * 1000 : 60000,
maxRetries: requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.maxRetries,
});
if (_response.ok) {
return yield serializers.StreakResponse.parseOrThrow(_response.body, {
unrecognizedObjectKeys: "passthrough",
allowUnrecognizedUnionMembers: true,
allowUnrecognizedEnumValues: true,
breadcrumbsPrefix: ["response"],
});
}
if (_response.error.reason === "status-code") {
switch (_response.error.statusCode) {
case 401:
throw new TrophyApi.UnauthorizedError(yield serializers.ErrorBody.parseOrThrow(_response.error.body, {
unrecognizedObjectKeys: "passthrough",
allowUnrecognizedUnionMembers: true,
allowUnrecognizedEnumValues: true,
breadcrumbsPrefix: ["response"],
}));
case 404:
throw new TrophyApi.NotFoundError(yield serializers.ErrorBody.parseOrThrow(_response.error.body, {
unrecognizedObjectKeys: "passthrough",
allowUnrecognizedUnionMembers: true,
allowUnrecognizedEnumValues: true,
breadcrumbsPrefix: ["response"],
}));
case 422:
throw new TrophyApi.UnprocessableEntityError(yield serializers.ErrorBody.parseOrThrow(_response.error.body, {
unrecognizedObjectKeys: "passthrough",
allowUnrecognizedUnionMembers: true,
allowUnrecognizedEnumValues: true,
breadcrumbsPrefix: ["response"],
}));
default:
throw new errors.TrophyApiError({
statusCode: _response.error.statusCode,
body: _response.error.body,
});
}
}
switch (_response.error.reason) {
case "non-json":
throw new errors.TrophyApiError({
statusCode: _response.error.statusCode,
body: _response.error.rawBody,
});
case "timeout":
throw new errors.TrophyApiTimeoutError();
case "unknown":
throw new errors.TrophyApiError({
message: _response.error.errorMessage,
});
}
});
}
}
exports.Users = Users;