apollo-server#ApolloError TypeScript Examples

The following examples show how to use apollo-server#ApolloError. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: live.ts    From vt-api with GNU Affero General Public License v3.0 6 votes vote down vote up
export async function live(_, query: LiveQuery) {
  try {
    const { organizations = [], platforms = [], exclude_organizations = [] } = query;
    if (organizations.length && exclude_organizations.length) {
      return new UserInputError('Setting both organizations and exclude_organizations is redundant. Only choose one.');
    }
    const EXCLUDE_ORG = !organizations.length;
    const ORGANIZATIONS = parseOrganization(EXCLUDE_ORG ? exclude_organizations : organizations);
    const CACHE_KEY = getCacheKey(`LIVE:${+EXCLUDE_ORG}${cutGroupString(ORGANIZATIONS)}${platforms}`);

    const cachedVideos = await memcache.get(CACHE_KEY);
    if (cachedVideos) return cachedVideos;

    const uncachedVideos = await Videos.find({
      status: 'live',
      ...platforms[0] && { platform_id: { $in: platforms } },
      ...ORGANIZATIONS[0] && { organization: {
        ...EXCLUDE_ORG
          ? { $not: { $regex: ORGANIZATIONS, $options: 'i' } }
          : { $regex: ORGANIZATIONS, $options: 'i' }
      } }
    }).sort({ 'time.start': 1 })
      .lean()
      .exec();

    memcache.set(CACHE_KEY, uncachedVideos, 60);
    return uncachedVideos;
  } catch (error) {
    throw new ApolloError(error);
  }
}
Example #2
Source File: NotFoundError.ts    From one-platform with MIT License 5 votes vote down vote up
export default class NotFoundError extends ApolloError {
  constructor(message: string) {
    super(message, 'NOT_FOUND');

    Object.defineProperty(this, 'name', { value: 'NotFoundError' });
  }
}
Example #3
Source File: channels.ts    From vt-api with GNU Affero General Public License v3.0 5 votes vote down vote up
export async function channels(_, query: ChannelsQuery) {
  try {
    const {
      _id = [],
      name = '',
      organizations = [],
      exclude_organizations = [],
      exclude_channel_id = [],
      channel_id = [],
      platforms = [],
      page_token = '',
      limit
    } = query;
    if (limit < 1 || limit > 50) {
      return new UserInputError('limit must be between 1-50 inclusive.');
    }
    if (organizations.length && exclude_organizations.length) {
      return new UserInputError('Setting both organizations and exclude_organizations is redundant. Only choose one.');
    }
    if (channel_id.length && exclude_channel_id.length) {
      return new UserInputError('Setting both channel_id and exclude_channel_id is redundant. Only choose one.');
    }
    const EXCLUDE_ORG = !organizations.length;
    const EXCLUDE_IDS = !channel_id.length;
    const [ORDER_BY, ORDER_BY_KEY] = firstField(query.order_by);
    const [ORDER_KEY, ORDER_VALUE] = Object.entries(ORDER_BY)[0];
    const sortById = ORDER_KEY === '_id';
    const sortBy = sortById ? ORDER_BY : { [`channel_stats.${ORDER_KEY}`]: ORDER_VALUE };
    const ORGANIZATIONS = parseOrganization(EXCLUDE_ORG ? exclude_organizations : organizations);
    const CHANNEL_IDS = EXCLUDE_IDS ? exclude_channel_id : channel_id;
    const CACHE_KEY = getCacheKey(`CHNLS:${+EXCLUDE_ORG}${+EXCLUDE_IDS}${_id}${(name)}${cutGroupString(ORGANIZATIONS)}${cutChannelIds(CHANNEL_IDS)}${platforms}${limit}${ORDER_BY_KEY}${page_token}`, false);

    const cached = await memcache.get(CACHE_KEY);
    if (cached) return cached;

    const QUERY = {
      _id: { [_id[0] ? '$in' : '$nin']: _id },
      ...name && { $or: getNameQueries(name) },
      ...ORGANIZATIONS[0] && { organization: {
        ...EXCLUDE_ORG
          ? { $not: { $regex: ORGANIZATIONS, $options: 'i' } }
          : { $regex: ORGANIZATIONS, $options: 'i' }
      } },
      ...channel_id[0] && { channel_id: { [EXCLUDE_IDS ? '$nin' : '$in']: CHANNEL_IDS } },
      ...platforms[0] && { platform_id: { $in: platforms } }
    };

    const getChannelCount = Channels.countDocuments(QUERY);
    const getUncachedChannels = Channels
      .find({
        ...QUERY,
        ...page_token && { [Object.keys(sortBy)[0]]: { [ORDER_VALUE === 'asc' ? '$gte' : '$lte']: parseToken(page_token) } },
      })
      .sort(sortBy)
      .limit(limit + 1)
      .lean()
      .exec();

    const [channelCount, uncachedChannels] = await Promise.all([getChannelCount, getUncachedChannels]);
    const results = {
      items: uncachedChannels,
      next_page_token: null,
      page_info: {
        total_results: channelCount,
        results_per_page: limit
      }
    };

    const hasNextPage = uncachedChannels.length > limit && results.items.pop();
    if (hasNextPage) {
      const token = sortById ? hasNextPage._id : hasNextPage.channel_stats[ORDER_KEY];
      results.next_page_token = getNextToken(token);
    }

    memcache.set(CACHE_KEY, results, CACHE_TTL);
    return results;
  } catch(err) {
    throw new ApolloError(err);
  }
}
Example #4
Source File: data.ts    From vt-api with GNU Affero General Public License v3.0 5 votes vote down vote up
export async function data(_, query: Query) {
  try {
    const {
      organizations = [],
      exclude_organizations = [],
      channel_id = [],
      exclude_channel_id = [],
      platforms = []
    } = query;
    if (organizations.length && exclude_organizations.length) {
      return new UserInputError('Setting both organizations and exclude_organizations is redundant. Only choose one.');
    }
    if (channel_id.length && exclude_channel_id.length) {
      return new UserInputError('Setting both channel_id and exclude_channel_id is redundant. Only choose one.');
    }
    const EXCLUDE_ORG = !organizations.length;
    const EXCLUDE_IDS = !channel_id.length;
    const ORGANIZATIONS = parseOrganization(EXCLUDE_ORG ? exclude_organizations : organizations);
    const CHANNEL_IDS = EXCLUDE_IDS ? exclude_channel_id : channel_id;
    const CACHE_KEY = getCacheKey(`CHNLS:${+EXCLUDE_ORG}${cutGroupString(ORGANIZATIONS)}${cutChannelIds(CHANNEL_IDS)}${platforms}`, false);

    const cached = await memcache.get(CACHE_KEY);
    if (cached) return cached;

    const QUERY = {
      ...ORGANIZATIONS[0] && { organization: {
        ...EXCLUDE_ORG
          ? { $not: { $regex: ORGANIZATIONS, $options: 'i' } }
          : { $regex: ORGANIZATIONS, $options: 'i' }
      } },
      ...channel_id[0] && { channel_id: { [EXCLUDE_IDS ? '$nin' : '$in']: CHANNEL_IDS } },
      ...platforms[0] && { platform_id: { $in: platforms } }
    };

    const getChannelList = Channels.find(QUERY).lean().exec();
    const getVideoCount = Videos.countDocuments(QUERY).lean().exec();
    const [channelList, videoCount] = await Promise.all([getChannelList, getVideoCount]);
    const groupList = channelList
      .map(value => value.organization)
      .filter((value, index, array) => array.indexOf(value) === index);
    const channelCount = channelList.length;

    const result = {
      organizations: groupList,
      channels: channelCount,
      videos: videoCount
    };

    memcache.set(CACHE_KEY, result, CACHE_TTL);
    return result;
  } catch(err) {
    throw new ApolloError(err);
  }
}
Example #5
Source File: videos.ts    From vt-api with GNU Affero General Public License v3.0 4 votes vote down vote up
export async function videos(_, query: VideoQuery) {
  try {
    const {
      channel_id = [],
      status = [],
      title,
      organizations = [],
      exclude_organizations = [],
      platforms = [],
      max_upcoming_mins,
      page_token = '',
      limit
    } = query;

    if (limit < 1 || limit > 50) {
      return new UserInputError('limit must be between 1-50 inclusive.');
    }
    if (max_upcoming_mins < 0 || max_upcoming_mins > 2880) {
      return new UserInputError('max_upcoming_mins must be between 0-2880 inclusive.');
    }
    if (organizations.length && exclude_organizations.length) {
      return new UserInputError('Setting both organizations and exclude_organizations is redundant. Only choose one.');
    }
    const EXCLUDE_ORG = !organizations.length;
    const MAX_UPCOMING = max_upcoming_mins * 6e4;
    const TITLE = title && escapeRegex(title);
    const ORGANIZATIONS = parseOrganization(EXCLUDE_ORG ? exclude_organizations : organizations);
    const [ORDER_BY, ORDER_BY_KEY] = firstField(query.order_by);
    const [ORDER_KEY, ORDER_VALUE] = Object.entries(ORDER_BY)[0];
    const orderBy = { [`time.${ORDER_KEY}`]: ORDER_VALUE };
    const CACHE_KEY = getCacheKey(`VIDS:${+EXCLUDE_ORG}${cutGroupString(ORGANIZATIONS)}${channel_id}${status}${TITLE}${platforms}${max_upcoming_mins}${ORDER_BY_KEY}${limit}${page_token}`);

    const cached = await memcache.get(CACHE_KEY);
    if (cached) return cached;

    const QUERY: any = { // any because typescript gets mad for some reason.
      status: status[0] ? { $in: status } : { $ne: 'missing' },
      ...channel_id[0] && { channel_id: { $in: channel_id } },
      ...TITLE && { title: { $regex: TITLE, $options: 'i' } },
      ...ORGANIZATIONS[0] && { organization: {
        ...EXCLUDE_ORG
          ? { $not: { $regex: ORGANIZATIONS, $options: 'i' } }
          : { $regex: ORGANIZATIONS, $options: 'i' }
      } },
      ...platforms[0] && { platform_id: { $in: platforms } },
      ...max_upcoming_mins && { 'time.scheduled': { $lte: Date.now() + MAX_UPCOMING } }
    };

    const getVideoCount = Videos.countDocuments(QUERY);
    const getUncachedVideos = Videos
      .find({
        ...QUERY,
        ...page_token && { [Object.keys(orderBy)[0]]: { [ORDER_VALUE === 'asc' ? '$gte' : '$lte']: parseToken(page_token) } },
      })
      .sort(orderBy)
      .limit(limit + 1)
      .lean()
      .exec();

    const [videoCount, uncachedVideos] = await Promise.all([getVideoCount, getUncachedVideos]);
    const results = {
      items: uncachedVideos,
      next_page_token: null,
      page_info: {
        total_results: videoCount,
        results_per_page: limit
      }
    };

    const hasNextPage = uncachedVideos.length > limit && results.items.pop();
    if (hasNextPage) {
      const token = hasNextPage.time[ORDER_KEY];
      results.next_page_token = getNextToken(token);
    }

    memcache.set(CACHE_KEY, results, 60);
    return results;
  } catch(err) {
    throw new ApolloError(err);
  }
}