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

static-sheets

v1.0.3

Published

## A Google Sheets Static API Generator

Downloads

1

Readme

static-sheets

A Google Sheets Static API Generator

Builds a static API with any Google Sheet.

Installation

npm install --global static-sheets

Basic Usage

To start, you'll need your secret key and client email from your Google Sheets API credentials as well as your spreadsheet ID. Your spreadsheet ID is the long string of characters in the URL. For example: https://docs.google.com/spreadsheets/d/YOUR_SPREADSHEET_ID/edit#gid=0.

Make sure that you have a header row in your spreadsheet. The headers will be camel cased and used as keys in your data objects. Example:

Header Example

Then create a static-sheets.config.js file in the root directory of your project.

// static-sheets.config.js
module.exports = {
	privateKey: 'YOUR_SHEETS_PRIVATE_KEY',
	cilentEmail: 'YOUR_SHEETS_CLIENT_EMAIL',
	spreadsheetId: 'YOUR_SPREADSHEET_ID',
}

Run static-sheets in your terminal:

static-sheets

Endpoints

By default, there will only be a single endpoint for each row. You can add more endpoints in your config:

// static-sheets.config.js
module.exports = {
	paths: {
		'api/:categoryName': {},
		'api/:categoryName/:productId': {},
	}
	...
}

This might generate filepaths similar to:

/api/apples.json
/api/pears.json
/api/apples/apl-01.json
/api/apples/apl-02.json
/api/pears/pr-01.json
/api/pears/pr-02.json

Remember to camelcase your headers when writing your paths. For example, a header of Comment ID could be used in a path like:

// static-sheets.config.js
module.exports = {
	paths: {
		'comments/:commentId': {}
	}
	...
}

Options

Options can be supplied to individual paths, or in the config object if you want the option to apply to all paths.

Pagination

To paginate into multiple paths:

// static-sheets.config.js
...
paths: {
	'category/:categoryId': {
		paginate: 5
	}
}
...

This will limit the number of rows in a file to 5 and might generate filepaths similar to:

/category/apples/1.json
/category/apples/2.json
/category/apples/3.json

Single Object

Set single to true if you only want to return a single object rather than an array.

Type Conversion

By default, all Google sheets data comes through as a string. But if you need other types, static-sheets can convert them for you.

// static-sheets.config.js
...
paths: {
	'reviews/:productId': {}
},
types: {
	timestamp: Date,
	rating: Number,
	approved: Boolean
}
...

Sorting

Supply an key to sort by the return:

// static-sheets.config.js
...
paths: {
	'reviews/:productId': {
		sort: {
			timestamp: 'ascending',
		},
	}
}
...

If you need more control over sorting, you can also supply a sort function.

// static-sheets.config.js
...
paths: {
	'reviews/:productId': {
		sort: function(a, b){
			if(a.timestamp > b.timestamp){
				return -1
			}
			if(a.timestamp < b.timestamp){
				return 1
			}
			return 0
		}
	}
}
...

Filtering

To limit results, you can pass in a filter object. If the result doesn't match the filter object, it will not be included in the results.

...
paths: {
	'reviews/:productId': {
		filter: {
			approved: true
		}
	}
}
...