UNPKG

500px-api

Version:

This is an unofficial package to fetch photos, users, photographers, camera equipment details, and other information from the 500px.com website.

103 lines (102 loc) 4.77 kB
"use strict"; 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()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.PhotoClient = void 0; const _1 = require("."); const validate_1 = require("../helpers/validate"); class PhotoClient extends _1.Client { search(query, options = {}) { var _a, _b; return __awaiter(this, void 0, void 0, function* () { query = query.trim(); if (query === '') { throw new Error('"query" can not be empty.'); } const response = yield this.client.get('photos/search', { params: { term: query, rpp: options.limit || this.defaultLimit, page: options.page || 1, feature: options.feature, image_size: options.image_size, only: (_a = options.only) === null || _a === void 0 ? void 0 : _a.join('\n'), exclude: (_b = options.exclude) === null || _b === void 0 ? void 0 : _b.join('\n'), followers_count: options.followers_count, }, }); return response.data; }); } discover(options = {}) { return __awaiter(this, void 0, void 0, function* () { return this.discoverWithOptions(options); }); } discoverId(id, options = {}) { return __awaiter(this, void 0, void 0, function* () { if (Array.isArray(id) && id.length > 50) { throw new Error('"id" array is to large.'); } return this.discoverWithOptions(options, id); }); } /** * Covert object of photos to array of photos */ transformDiscoverIds(data) { const response = JSON.parse(data); return Object.keys(response.photos).map((index) => response.photos[index]); } discoverWithOptions(options, ids) { var _a, _b; return __awaiter(this, void 0, void 0, function* () { const response = yield this.client.get('photos', { params: Object.assign({ ids: typeof ids === 'number' ? ids : ids === null || ids === void 0 ? void 0 : ids.join(','), rpp: options.limit || this.defaultLimit, feature: options.feature, image_size: options.image_size, only: (_a = options.only) === null || _a === void 0 ? void 0 : _a.join('\n'), exclude: (_b = options.exclude) === null || _b === void 0 ? void 0 : _b.join('\n'), followers_count: options.followers_count }, (ids ? { include_tags: true, include_geo: true, include_equipment_info: true, include_licensing: true, } : {})), /** * If IDs is specified then transform the response */ transformResponse: ids ? this.transformDiscoverIds : undefined, }); return response.data; }); } comments(options) { return __awaiter(this, void 0, void 0, function* () { /** * Validation */ options.page && validate_1.Validate.pageParam(options.page); options.limit && validate_1.Validate.limitParam(options.limit); const response = yield this.client.get(`photos/${options.photo_id}/comments`, { params: { page: options.page || 1, rpp: options.limit || this.defaultLimit, /** * Nested 1 will fill the "replies" field in response object. */ nested: 1, sort: 'created_at', // TODO: implement comment sorting feature? // include_subscription: 1, // include_flagged: 1, }, }); return response.data; }); } } exports.PhotoClient = PhotoClient;