Code Monkey home page Code Monkey logo

firebase-hackernews's Introduction

firebase-hackernews

Hacker News APIs with firebase

Install

$ npm install --save firebase-hackernews

Test

Express

Run node script at ./example/express/server.js, or npm run express and connect to server

react

This project was generated by create-react-app. Navigate to ./example/react/ and then run first npm install, an then run npm start. If you have any updates before run start, you should reinstall the package

Service Worker

To test service worker, run npm run sw and connet to test server. webpack watching is default options

Usage

See more examples in test.js and refer to HackerNews API for more information of firebase and

Initialzing with firebase/database

Because of firebase running environment is not only for node but also browser and service worker.So you must import both of app and database from firebase modules before import firebase-hackernews.

const firebase = require('firebase');
const hackernews = require('firebase-hackernews');

// create a service as a single instance when the fist call
// you must pass firebase to init method
const hnservice = hackernews.init(firebase)

Even it can be running with es2015 to support for live-code importing like this below,

import firebase from 'firebase/app'
import from 'firebase/database'

const hnservice = hackernews.init(firebase)

With Promise

// get all of stories by types, 'top', 'new', 'best', 'ask', 'show', 'job'
hnservice.stories('top').then(stories => {})

// get stories with custom count and page
hnservice.stories('top', {page: 1, count: 30}).then(stories => {})

// get a user
hnservice.user('jl').then(user => {})

// get a current max item id
hnservice.maxItem().then(update => {})

// get a updated items and profiles
hnservice.update().then(update => {})

Without Promise

APIs named to xxxxCached is that support return data immediately but synchronous APIs doesn't do fetch. It only works on cached data that means asynchronous apis alreay has been called or running on watch mode

hnservice.storiesCached('top').then(stories => {})

Fetching with Service Worker

This module supports that running on server-worker. After initialzing in service worker, you can get a data via fetch. To do this, firstly, you must import and initialize both of firebase and hackernews service

/* global importScripts hackernews */
importScripts('https://www.gstatic.com/firebasejs/4.1.2/firebase-app.js')
importScripts('https://www.gstatic.com/firebasejs/4.1.2/firebase-database.js')
importScripts('https://unpkg.com/[email protected]')

hackernews.init(firebase, { watch: true })

and then you can request a stories via fetch and subpath. see more examples in examples/serice-worker

const stories = await fetch('./hackernews/top')
const storiesRes = await stories.json()

storiesRes.data.forEach(s => {
	// manage a story
})

API

init(firebase, [option])

Returns hackernews service powered by firebase as a single instance

{
	firebase: `firebase package. refer to usage above`
	options: {
		watch: `true / false, enable watch mode or not`
		log: `log function, ex) console.log`
	}
}

APIs for Promise

stories(type, [options])

Returns stories with totalLength as an additional info after fetched and cached with options:

  • force: true ? returns stories fetch first, else return cached data if it exist
  • page: returns stories in page by count
  • count: count in a page. default is 50

items(id[s], [options])

Returns items by id[s] after fetched and cached with options:

  • force: true ? returns stories cache first, else return cached data after fetch

user(id)

Returns profile by id

updates()

Returns recent updates regardless fetched data

maxItem()

Returnes max item id of latest snapshot on firebase

watch()

Make the service keep listening on the changes of stories. It recommend to use it for desktop application and server side. refer to the example with express.js

length(type)

Returns a length of cached items of the target type

kids(id)

Return cached all of items related to the target id. key is item's id and data will be flatted object list

data([data])

Set and get data, on cache directly. It's useful when it comes to hydrate / serialis cache

APIs for cached data

Cached APIs returns with data immediately from chached data without fetch and Promise. It would be possible that data is not ready befor you do calll cached APIs

  • storiesCached, identical to stories()
  • itemsCached, identical to items()
  • lengthCached, identical to length() but no promise
  • dataCached, identical to data() but no promise

License

MIT Β© Jimmy Moon

firebase-hackernews's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

firebase-hackernews's Issues

Doens't build with node 10.16

When trying to add into any project, I get the following error:
yarn add firebase-hackernews

gyp ERR! cwd /opt/apps/[...]/node_modules/firebase-hackernews/node_modules/grpc
gyp ERR! node -v v10.16.0
gyp ERR! node-gyp -v v3.8.0
gyp ERR! not ok

I tried to use it locally with npm link and it worked once I updated the firebase dependency to "firebase": "6.3.3"

React Native : Can't Find Variable performance

I'm following the react example and included the neccesary files. When the App runs the Following Error Message is displayed and the data is not fetched.

Possible Unhandled Promise Rejection

Reference Error: Can't find Variable: performance

TypeError: firebaseHackernews is not a function

Running firebase-hackernews in browser throws following error.

Link:

https://npm.runkit.com/firebase-hackernews

Source:

var firebase = require('firebase');
var firebaseHackernews = require("firebase-hackernews")
var hnservice = firebaseHackernews(firebase)
hnservice.stories('top').then(stories => {
console.log(stories);
})

On running it:-

TypeError: firebaseHackernews is not a function

Solution:-

Fix the documentation.

firebaseHackernews.init(firebase)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.