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

firestore-rest-adapter

v0.1.13

Published

Serializes and deserializes firebase typed JSON

Downloads

30

Readme

Functionality

The Firestore v1 REST API responds to requests for document fields with a typed data format outlined here. This data is structured like the following:

{
  fields: {
    myObj: {
      mapValue: {
        fields: {
          myString: {
            stringValue: 'my string'
          }
        }
      }
    },
    myArray: {
      arrayValue: {
        values: [
          {
            myInt: {
              integerValue: '5'
            }
          }
        ]
      }
    }
  }
}

This package converts the data into a more workable object format:

{
  myObj: {
    myString: 'my string'
  },
  myArray: [5]
}

You can mutate the resulting object however you desire. When you need to write the object back to Firestore, call serializeDocument on the object and include it as the body of your request.

Installation

yarn add firestore-rest-adapter
# OR
npm i --save firestore-rest-adapter

Usage

import { normalizeDocument, serializeDocument } from 'firestore-rest-adapter';

const getDoc = async () => {
  const res = await fetch(
    // `?fields=fields` query parameter to fetch document fields
    'https://firestore.googleapis.com/v1/path/to/document?fields=fields',
    {
      method: 'GET',
      headers: {
        authorization: // access token
      },
    }
  );
  const docData = await res.json();

  // Normalize Firestore Value into workable object
  return normalizeDocument(docData);
};

const writeDoc = async (obj) => {
  // Serialize obj into Firestore Value
  const doc = serializeDocument(obj);

  return fetch(
    'https://firestore.googleapis.com/v1/path/to/document?fields=fields',
    {
      method: 'PATCH',
      headers: {
        authorization: // access token
      },
      body: JSON.stringify(doc),
    }
  );
};

API

| Method name | Description | | -------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | normalizeDocument(doc: Object) | Converts an entire Firestore document's fields into a normalized object, removing the top-level fields key. | | normalizeField(field: Object) | Converts a Firestore Value into the corresponding primitive. (e.g {stringValue: 'foo'} => 'foo'). This method works recursively for iterable Values. | | serializeDocument(obj: Object) | Serializes an Object and its fields into a Firestore Value, inserting a top level fields key. (e.g {foo: 'bar'} => {fields: {foo: {stringValue: 'bar'}}}) | | serializeField(field: Object) | Serializes an Object into a Firestore Value. (e.g 'foo' => {stringValue: 'foo'}). This method works recursively for iterable Values. |