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

@graasp/apps-query-client

v3.4.14

Published

Query client repository for Graasp apps

Downloads

4,543

Readme

Graasp Apps Query Client

Latest version published on NPM Latest version released on Github NPM package downloads per month

typescript version supported react versions react-query version

This repository implements the react-query hooks and mutations for apps to consume the Graasp Apps API. It also provides a mock API server based on MirageJS for local development.

Mock API Installation

This apps-query-client package provides a mock API to mock any call an app might use to consume the Graasp API. It is based on MirageJS, which simulates the network requests themselves, and can thus remember remote state in memory. So the database is preserved as long as the app is not refreshed. This mock API is also particularly useful for continuous integration tests.

The following steps are designed to take into account Cypress, our test framework. So the mock database can also receive data from the tests and apply them.

!WARNING: The mock API cannot fake uploading and downloading files!

  1. Install the env-cmd dependency. Create a new script start:local in package.json:
"start:local": "env-cmd -f ./.env.development react-scripts start"
  1. Create .env.development which will contain the variables below. The app id you will choose doesn't have to be valid, but needs to exist.
REACT_APP_GRAASP_APP_ID=<your app id>
REACT_APP_GRAASP_APP_KEY=<your app key>
REACT_APP_ENABLE_MOCK_API=true
  1. Configure your query client in src/config/queryClient.js with the following code.
import {
  configureQueryClient,
  buildMockLocalContext,
  buildMockParentWindow,
} from '@graasp/apps-query-client';

const values = configureQueryClient({
  GRAASP_APP_KEY: process.env.REACT_APP_GRAASP_APP_KEY,
  isStandalone: MOCK_API,
});
export values;
  1. Add the following content in src/index.js. mockApi can take a defined context and/or database if necessary (see the Cypress section)
import { mockApi } from '@graasp/apps-query-client';

if (process.env.REACT_APP_ENABLE_MOCK_API === 'true') {
  mockApi();
}
  1. Use the withContext and the withToken files in your app. It will handle the authentication and fetching the local context automatically for you. For example:
const AppWithContext = withToken(App, {
  LoadingComponent: <Loader />,
  useAuthToken: hooks.useAuthToken,
  onError: () => {
    showErrorToast('An error occured while requesting the token.');
  },
});

const AppWithContextAndToken = withContext(AppWithContext, {
  LoadingComponent: <Loader />,
  useGetLocalContext: hooks.useGetLocalContext,
  useAutoResize: hooks.useAutoResize,
  onError: () => {
    showErrorToast('An error occured while fetching the context.');
  },
});

You can now start your app with the mock API installed. Don't forget to disable it when you build your app (set REACT_APP_ENABLE_MOCK_API to false).

Cypress

The next steps will help you set up Cypress to work with MirageJS. There is an official tutorial from MirageJS. But in our case, we followed a different strategy.

  1. Update your content in src/index.js to include some config defined from Cypress in the mock server:
if (process.env.REACT_APP_ENABLE_MOCK_API === 'true') {
  mockApi({
    appContext: window.Cypress ? window.appContext : undefined,
    database: window.Cypress ? window.database : undefined,
  });
}
  1. Add the following in cypress/support/commands.js. You will need to define MEMBERS and CURRENT_MEMBER to reuse them in your tests as well.
import { buildDatabase } from '@graasp/apps-query-client';

Cypress.Commands.add(
  'setUpApi',
  ({ currentMember = CURRENT_MEMBER, database = {}, appContext } = {}) => {
    // mock api and database
    Cypress.on('window:before:load', (win) => {
      win.database = buildDatabase({
        members: Object.values(MEMBERS),
        ...database,
      });
      win.appContext = appContext;
    });
  },
);
  1. Then in all your tests you will need to set up the database and context. The default values are configured so you can easily mount an empty and operational database.
// start with an empty database
cy.setUpApi();

// start with one app data pre-saved in builder for an admin
cy.setUpApi({
  database: { appData: [MOCK_APP_DATA] },
  appContext: {
    permission: 'admin',
    context: 'builder',
  },
});