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

@viewar/backend-api

v1.10.1

Published

Package to allow apps to change ViewAR content in the backend.

Downloads

66

Readme

@viewar/backend-api

Package to allow apps to change ViewAR content in the backend.

Usage

Each request (read or write) will return the changed data from the server. This way the programmer can keep his own state in sync with the server state.

Configuration

First a user must configure this module to get access to the backend. This should be done before any call to this API:

import { Configuration } from '@viewar/backend-api';

Configuration.setHost('https://dev2.viewar.com');

Authentication

Second a user has to authenticate to set a valid token. The package internally saves the token during runtime.

One way is to authenticate with email and password:

import { Authentication } from '@viewar/backend-api';

const token = await Authentication.authenticate('[email protected]', '12345');

The other way is to directly use the token:

import { Authentication } from '@viewar/backend-api';

const token = 'token1234';
Authentication.useToken(token);

Storage

The storage is to read from ViewAR's cloud storage or write into it.

import { Storage } from '@viewar/backend-api';

const storageId = 'viewar.app.id';

// Write a file.
content = await Storage.write(storageId, 'test.json', {
  test: 1234,
});

// Read from file.
let content = await Storage.read(storageId, 'test.json');

Resources

The resources namespace is used to handle resources on the ViewAR servers.

import { Resources } from '@viewar/backend-api';

const file = new File(['mocked file content'], 'filename.txt'); // Usually taken from UI input or from a filesystem read.
const name = 'Test Texture'; // Name of the texture.
const type = 'png'; // Either png or jpg.

// Create a new texture.
const texture = await Resources.createTexture(file, name, type);

ModelData

ModelData is to read or write to a model's data field.

import { ModelData } from '@viewar/backend-api';

const modelId = '108721';

// Read model data.
let data = await ModelData.get(modelId);

// Update model data.
// Here we use object.assign to merge the new data with the
// existing ones (to avoid overwriting converter output).
data = await ModelData.save(
  modelId,
  Object.assign({}, data, {
    test: 2345,
  })
);

Error Handling

Every backend request can throw an error. If the request fails to reach the server a RequestError is thrown. If there is an error in the server response (e.g. authentication error) a BackendError is thrown.

import { Backend, ModelData, Authentication } from "@viewar/backend-api";

const token = "invalid token";
const modelId = "108721";

Authentication.useToken(token);

try {
  // Try to get model data with invalid token.
  await ModelData.get(modelId);
} catch(err) {
  if (err instanceOf Backend.RequestError) {
    // RequestError is thrown if connection to the server can't be established.
    console.log("Failed to reach the server!", err.message);
  } else if (err instanceOf Backend.BackendError) {
    // BackendError will occur.
    console.log("Server returned an error in response", err.message);
  }
  } else if (err instanceOf Backend.InvalidTokenError) {
    // InvalidTokenError is thrown is neither Authentication.useToken nor Authentication.authenticate was called.
    console.log("Please authenticate first.", err.message);
  }
}