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

@agoston-io/client

v1.2.2

Published

JS client for Agoston.io backends

Downloads

62

Readme

Agoston.io js client

A client that connects an Agoston.io backend to your frontend project. The client allows you to authenticate and log out your users and exposes a preconfigured Apollo client ready to handle your GraphQL queries, mutations, subscriptions and file uploads.

Usage

1. Install the library

npm install @agoston-io/client

2. Import

import { AgostonClient } from '@agoston-io/client'

3. Create the client

NOTE: For test purposes, you can just call the client with no parameters: AgostonClient(). This will connect you to the default demo Agoston backend.

// promise with async/await
const agostonClient = await AgostonClient({
  backendUrl: process.env.AGOSTON_BACKEND_URL,
});
if (agostonClient.isAuthenticated()) {
  console.log(
    `Welcome user ${agostonClient.userId()} 👋! Your role is: ${agostonClient.userRole()}.`
  );
}

// GraphQL
const apolloClient = agostonClient.createEmbeddedApolloClient();
apolloClient
  .query({
    query: gql`
      query {
        session
      }
    `,
  })
  .then((result) => console.log(result));
// promise with then/catch
AgostonClient({ backendUrl: process.env.AGOSTON_BACKEND_URL }).then(
  (agostonClient) => {
    if (agostonClient.isAuthenticated()) {
      console.log(
        `Welcome user ${agostonClient.userId()} 👋! Your role is: ${agostonClient.userRole()}.`
      );
    }

    // GraphQL
    const apolloClient = agostonClient.createEmbeddedApolloClient();
    apolloClient
      .query({
        query: gql`
          query {
            session
          }
        `,
      })
      .then((result) => console.log(result));
  }
);

Examples

Create client with the demo backend

AgostonClient().then(async (agostonClient) => {
  if (agostonClient.isAuthenticated()) {
    console.log(
      `Welcome user ${agostonClient.userId()} 👋! Your role is: ${agostonClient.userRole()}.`
    );
    console.log(`Auth provider: ${agostonClient.userAuthProvider()}`);
    console.log(`User data: ${agostonClient.userAuthData()}`);
  }
});

Custom query

You may want to add an extra custom GraphQL query at the AgostonClient initialization time. This query will be run within the backend and returned to the client, avoiding an extra round trip to the backend if you need to initialize application data, for instance.

AgostonClient({
  backendUrl: process.env.AGOSTON_BACKEND_URL,
  customGraphQLQuery: {
    query: `query channels {
                channels {
                    totalCount
                    aggregates {
                        distinctCount {
                            userId
                        }
                    }
                }
            }`,
    variables: { id: 1 }, // if your query has variables
  },
}).then(async (agostonClient) => {
  customGraphQLQueryResult = agostonClient.customGraphQLQueryResult();
  // customGraphQLQueryResult:
  // {
  //   "data": {
  //     "channels": {
  //       "totalCount": 2703,
  //       "aggregates": {
  //         "distinctCount": {
  //           "userId": "55"
  //         }
  //       }
  //     }
  //   }
  // }
});

Authenticate with user/password

agostonClient
  .loginOrSignUpWithUserPassword({
    username: "niolap",
    password: "password7-F4-",
    options: {
      free_value: {
        dateOfBirth: "1986.01.12",
      },
      redirectSuccess: "/",
    },
  })
  .then((session) => {
    console.log(`auth_success: ${JSON.stringify(session)}`);
  })
  .catch((error) => {
    console.log(`auth_error: ${error}`);
  });

Authenticate with bearer token

AgostonClient({
  backendUrl: process.env.AGOSTON_BACKEND_URL,
  bearerToken: process.env.AGOSTON_BACKEND_URL_BEARER_TOKEN,
}).then(async (agostonClient) => {
  if (agostonClient.isAuthenticated()) {
    console.log(
      `Welcome user ${agostonClient.userId()} 👋! Your role is: ${agostonClient.userRole()}.`
    );
  }
});

Authenticate with an external provider

agostonClient.loginOrSignUpFromProvider({ strategyName: "google-oauth20" });
agostonClient.loginOrSignUpFromProvider({
  strategyName: "auth0-oidc",
  options: {
    redirectSuccess: "/profile",
    redirectError: "/login",
  },
});

agostonClient.loginOrSignUpFromProvider({ strategyName: "github-oauth20" });
agostonClient.loginOrSignUpFromProvider({
  strategyName: "auth0-oidc",
  options: {
    redirectSuccess: "/profile",
  },
});

agostonClient.loginOrSignUpFromProvider({ strategyName: "facebook-oauth20" });
agostonClient.loginOrSignUpFromProvider({
  strategyName: "auth0-oidc",
  options: {
    redirectSuccess: "/profile",
  },
});

Logout

agostonClient
  .logout()
  .then((session) => {
    console.log(`logout_success: ${JSON.stringify(session)}`);
    window.location.href = "/";
  })
  .catch((error) => {
    console.log(`logout_error: ${error}`);
  });

GraphQL Query

The Agoston package comes with an embedded Apollo client preconfigured with your backend. In most cases, it's good enough. You can create your own Apollo client if you need more specific Apollo configuration.

AgostonClient({ backendUrl: process.env.AGOSTON_BACKEND_URL }).then(
  (agostonClient) => {
    const apolloClient = agostonClient.createEmbeddedApolloClient();
    apolloClient
      .query({
        query: gql`
          query {
            session
          }
        `,
      })
      .then((result) => console.log(result));
  }
);
// return
{
  data: {
    session: {
      role: 'authenticated',
      user_id: 3,
      auth_data: {},
      session_id: 'yXV_RXuVYhnrOLOB_A-tVRzxJBYb4z8_',
      auth_subject: '3',
      auth_provider: 'http-bearer',
      is_authenticated: true
    }
  },
  loading: false,
  networkStatus: 7
}