How I can rewrite this code in Nylas v2
let account = await Nylas.with(accessToken).account.get();
but it doesn’t work in Nylas v3
I updated Nylas, and changed code on
const NylasConfigs = {
apiKey: functions.config().keys.apiKey,
apiUri: ‘http://api.eu.nylas.com’
}
const nylas = new Nylas(NylasConfigs)
I don’t understand how I can get grand id my user use Nylas SDK?
Here is my code use Nylas v2, but I don’t understand how I need rewrite on Nylas v2
export const getCalendars = functions
.region(functionRegion)
.runWith({ timeoutSeconds: 100, memory: ‘512MB’ })
.https.onCall(async (data, context) => {
try {
if (!context.auth) {
//no ath!!
throw new functions.https.HttpsError(‘unauthenticated’, ‘missing auth’);
}
const uid = context?.auth?.uid;
const keyDoc = await admin.firestore().collection(Collection.NylasKeys).doc(uid).get();
if (!keyDoc.exists) {
throw new functions.https.HttpsError('unauthenticated', 'missing auth');
}
const accessToken = keyDoc.data()?.accessToken;
let account = await Nylas.with(accessToken).account.get();
//For some crazy reason the node api doesn't work for this???
const accountDetails = await fetch(
`https://api.nylas.com/a/${functions.config().keys.nylasClientId}/accounts/${account.accountId}`,
{
method: 'GET',
headers: {
Authorization: `Basic ${Buffer.from(`${functions.config().keys.nylasClientSecret}:`).toString('base64')}`,
},
},
);
const accountDetailsObject = await accountDetails.json();
log(accountDetailsObject as string);
if (accountDetailsObject.sync_state != 'running') {
//Calendar not ready yet!
return {
ready: false,
};
}
log('Account ready');
const calendars = await Nylas.with(accessToken).calendars.list();
log('Got cals');
log(calendars as unknown as string);
const formattedCals = calendars.map(cal => {
return {
title: cal.name,
timezone: cal.timezone,
id: cal.id,
accountId: cal.accountId,
};
});
return {
ready: true,
calendars: formattedCals,
};
} catch (e) {
functions.logger.error(e);
throw e;
}
});