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

firecaller

v1.1.6

Published

šŸ”„Write callable functions systematically like a Firelord. No more chaotic error handling, no more unsafe endpoint data type, no more messy validation. Be the Master of Fire you always wanted to be.

Downloads

28

Readme

FireCaller validate response and handle error from FireCall.

It wraps around Firebase callable functions to provide type safety for you request data and response data with zod.

Do not use this library if you are not using FireCall.

FireCaller is a library for Web, FireCall is for Nodejs.

Usable with Emulator

Why Do You Need This? What Is The Problem FireCall Trying To Solve?

Read Here

Installation

npm i firecaller firebase zod

and of course you need typescript.

Create Schema With Zod

Normally this file is created on backend and share to frontend.

Tips: You can also use these schemas to validate your form, learn more at zod!

import { z } from 'zod'

export const updateUserSchema = {
	//request data schema
	req: z.object({
		name: z.string(),
		age: z.number(),
		address: z.string(),
	}),
	// response data schema
	res: z.undefined(),
	// function name
	name: 'updateUser',
}

export const getUserSchema = {
	res: z.string(), // userId
	res: z.object({
		name: z.string(),
		age: z.number(),
	}),
	name: 'getUser',
}

Create Callable Functions

import { initializeApp } from 'firebase/app'
import { callable } from 'firecaller'
import { updateUserSchema, getUserSchema } from './someFile'

export const app = initializeApp(yourConfig) // must initialize app before using firecaller

const funRef = getFunctions(app)

// now create the specific callable
export const updateUser = callable(updateUserSchema) // or callable(updateUserSchema, funRef)
export const getUser = callable(getUserSchema) // or callable(getUserSchema, funRef)

Calling

FireCaller never throw, all errors are caught and returned as object. We choose this pattern because it is impossible to type-safe rejected promise.

By checking the value of the code, you know how to deal with them:

| code | meaning | | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------- | | ok | success, you can access the data value | | schema-out-of-sync | Incorrect response data shape, your schema is out of sync, you can access the message | | 'functions/cancelled', 'functions/unknown', 'functions/invalid-argument', 'functions/deadline-exceeded', 'functions/not-found', 'functions/already-exists', 'functions/permission-denied', 'functions/resource-exhausted', 'functions/failed-precondition', 'functions/aborted', 'functions/out-of-range', 'functions/unimplemented', 'functions/internal', 'functions/unavailable', 'functions/data-loss', 'functions/unauthenticated' | the error source is FireCall in NodeJS, you can access the message. |

import { updateUser, getUser } from './someOtherFile'

const { name, age, address } = someFormData()

updateUser(
	// input type depends on schema.req
	{ name, age, address } // { name: string, age: number, address: string }
).then(res => {
	const { code } = res
	if (code === 'ok') {
		const data = res.data // data type depends on what you define in schema.res
	} else {
		const { code, message } = res
		// message is string
	}
})

Usage With Emulator

import { initializeApp } from 'firebase/app'
import { getFunctions, connectFunctionsEmulator } from 'firebase/functions'
import { callable } from 'firecaller'
import { z } from 'zod'

const app = initializeApp({ projectId: `### YOUR_PROJECT_ID` })
const functions = getFunctions(app)

connectFunctionsEmulator(functions, 'localhost', f.emulators.functions.port)

const schema = {
	req: z.string(),
	res: z.string(),
	name: 'hello',
}

const helloCallable = callable(schema, functions)

describe('test callable', () => {
	it('success', async () => {
		const result = await helloCallable('hello')

		expect(result.code).toBe('ok')
		expect(result.data).toEqual('hello')
	})

	it('invalid arguments', async () => {
		// @ts-expect-error
		const result = await helloCallable(123) // wrong input type

		expect(result.code).toBe('functions/invalid-argument')
		expect(result.message).toEqual('invalid-argument')
	})
})

Related Projects

  1. FirelordJS - Typescript wrapper for Firestore Web V9
  2. Firelord - Typescript wrapper for Firestore Admin
  3. Firelordrn - Typescript wrapper for Firestore React Native
  4. FireLaw - Write Firestore security rule with Typescript, utilizing Firelord type engine.