import { updateUserSchema, getUserSchema } from './someFiles'
import { onCall } from 'firecall'
// use any variable name you want
const updateUser = onCall(
updateUserSchema,
{ route: 'private' }, // 'private' for protected route, user must sign in first, else automatically throw unauthenticated error (with customize-able message)
// handler
async (data, context) => {
const { name, age, address } = data // request data is what you define in schema.req
const {
auth: { uid }, // if route is protected, auth object is not undefined
} = context
try {
await updateWithSomeDatabase({ uid, name, age, address })
return { code: 'ok', data: undefined } // response data is what you define in schema.res
} catch (err) {
// this is the error we catch, however if we did not catch the error and in case of error in runtime, FireCall will automatically throw unknown error for us
// if we handle the error and return it, like this piece of code, FireCall will infer the type for us in <Error Logging> (check this section for details)
return {
code: 'unknown',
message: 'update user failed',
err, // this is the details of the error, could be object, could be string, could be anything, it is up to us
}
}
}
)
const getUser = onCall(
getUserSchema,
{ route: 'public' }, // 'public' for unprotected route
// handler
async data => {
const uid = data // request data is what you define in schema.req
try {
const { name, age, secret } = await getUserFromDatabase({
uid,
})
return { code: 'ok', data: { name, age } } // response data is what you define in schema.res
} catch (err) {
// this is the error we catch, however if we did not catch the error and in case of error in runtime, FireCall will automatically throw unknown error for us
// if we handle the error and return it, like this piece of code, FireCall will infer the type for us in <Error Logging> (check this section for details)
return {
code: 'unknown',
message: 'get user failed',
err, // this is the details of the error, could be object, could be string, could be anything, it is up to us
}
}
}
)
If the response is ok, handler must return object with code and data property, where
code: ok
data: value that has same type as type you define in schema.res
if the response is not ok, handler must return object with code and message properties, and an optional err property, where
code: Firebase Functions Error Code except 'ok'
message: string
err?: user defined error, put anything you want here, normally the error object or just skip it