I've encountered an issue while trying to set up a webhook endpoint API directly from RevenueCat's documentation.
Even though my code closely resembles the example in the documentation, I am puzzled by the error that keeps popping up. Unfortunately, I lack sufficient experience in this area to troubleshoot it effectively. Below is the specific error message:
(parameter) res: functions.Response<any>
Argument of type '(req: Request, res: Response<any>) =>... specified properties from Promise<void>': then, catch, finally, [Symbol.toStringTag]ts(2345)
To be honest, I'm not entirely sure what changes are being requested. Do you have any suggestions? Here is the snippet of my code:
import * as functions from 'firebase-functions'
import { PubSub } from '@google-cloud/pubsub'
const pubsubClient = new PubSub({projectId: '<PROJ_ID>'})
function isAuthorized(req: functions.https.Request) {
// Check authorization header
if (!req.headers.authorization || !req.headers.authorization.startsWith('Bearer ')) {
return false
}
const authToken = req.headers.authorization.split('Bearer ')[1]
if (authToken !== '<MY_AUTH_TOKEN>') {
return false
}
return true
}
// Respond to incoming message
export const revenueCatApi = functions.https.onRequest((req, res) => { // *** ERROR DETECTED HERE
// Only allow POST request
if (req.method !== 'POST') {
return res.status(403).send('Forbidden')
}
// Make sure the auth key matches what we set in the Revenue Cat dashboard
if (!isAuthorized(req)) {
return res.status(401).send('Unauthorized')
}
const rc = req.body as RCEvent
var topic: RCTopic = ''
switch (rc.event.type) {
case 'INITIAL_PURCHASE':
topic = 'rc-initial-purchase'
break
case 'NON_RENEWING_PURCHASE':
topic = 'rc-non-renewing-purchase'
break
case 'RENEWAL':
topic = 'rc-renewal'
break
case 'PRODUCT_CHANGE':
topic = 'rc-product-change'
break
case 'CANCELLATION':
topic = 'rc-cancellation'
break
case 'BILLING_ISSUE':
topic = 'rc-billing-issue'
break
case 'SUBSCRIBER_ALIAS':
topic = 'rc-subscriber-alias'
break
default:
console.log('Unhandled event type: ', rc.event.type)
return res.sendStatus(200)
}
// Set the pub/sub data to the event body
const dataBuffer = Buffer.from(JSON.stringify(rc))
// Publishes a message
return pubsubClient.topic(topic)
.publish(dataBuffer)
.then(() => res.sendStatus(200))
.catch(err => {
console.error(err)
res.sendStatus(500)
return Promise.reject(err)
})
})
exports.handleInitialPurchase = functions.pubsub
.topic('rc-initial-purchase')
.onPublish(async (message, context) => {
...
})
/* Other pubsub functions below */
RCEvent:
interface RCEvent {
api_version: string
event: {
aliases: string[]
app_id: string
app_user_id: string
country_code: string
currency: string
entitlement_id: string
... // Other interface properties here
}
}