npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@mitodl/course-search-utils

v3.3.1

Published

JS utils for interacting with MIT Open Course search

Downloads

1,912

Readme

Course Search Utils

This repo holds the core logic for our learning resource search. The basic API is a React hook (useCourseSearch) which provides for deserializing the params from the URL, managing the internal state of which facets are selected, etc.

development

run the tests:

npm run test
npm run test:watch # for watch mode

run the typechecker:

npm run typecheck

publishing

to publish a new version, open a PR with your changes and, once that is merged, check out the latest commit locally and do the following:

npm version x.x.x # increment this from the previous version appropriately
npm publish
git push origin # npm writes a version increment commit
git push --tags # and it tags it too

Usage

Hooks

@mitodl/course-search-utils exports a few hooks to assist in making search requests to MIT Open's APIs. They are:

  1. useResourceSearchParams({ searchParams, setSearchParams, ...opts? }) and useContentFileSearchParams: Derive search API parameters from a URLSearchParams object. Often, the URLSearchParams object will be derived from the browser URL, though it could be state internal to react.

    The hook extracts validated API parameters from the URLSearchParams object and returns setters that can be used to manipulate the URLSearchParams (e.g., toggling a search facet on or off).

    The URLSearchParams keys are mapped directly to API parameters.

  2. useInfiniteSearch({ params, endpoint, baseUrl, ...opts? }): Assists in making search API calls used in an infinite scrolling UI. The initial page is loaded by the hook, susbsequent pages via returm value { fetchNextPage }. The hook's result is based on useInfiniteQuery.

See Typescript annotations and docstrings for more information on hook props and results. Typical usage might look like:

import { useSearchQueryParams, useInfiniteSearch, } from "@mitodl/course-search-utils"
import type { UseInfiniteSearchProps } from "@mitodl/course-search-utils"

const CONSTANT_PARAMETERS = {
  platform: ["ocw"],
  aggregations: ["topic", "offered_by"]
}

const SearchPage: React.FC = () => {
  const [searchParams, setSearchParams] = useSearchParams()
  const {
    params,
    toggleParamValue,
    clearAllFacets,
    currentText,
    setCurrentText,
    setCurrentTextAndQuery,
  } = useSearchQueryParams({
    searchParams,
    setSearchParams,
    facets: FACETS
  })

  // If necessary
  const allParams = useMemo(() => {
    return _.merge(params, CONSTANT_PARAMETERS)
  }, [params])

  const { pages, hasNextPage, fetchNextPage } = useInfiniteSearch({
    params: allParams,
    baseUrl: "http://mitopen.odl.mit.edu/",
    keepPreviousData: true,
  })

  const results = pages?.flatMap(p => p.results) ?? []

  return (/* Search component JSX*/)
}