gas-local Reference Err SpreadsheetApp is not defined - node.js

I'm trying to set up my app script project using clasp and test it locally using gas-local, I have reached the step of testing but I got this error ReferenceError: SpreadsheetApp is not defined.
gas.js
//require gas-local itself
var gas = require('gas-local');
//require your downloaded google apps script library from src subfolder
var glib = gas.require('./src');
where src is my google app script project folder, however when I node gas I got the ReferenceError: SpreadsheetApp is not defined
The idea from using gas-local is that I want to know my GAS cloudProject ID programatically, so I thought I would trigger any method locally and get the project-id from the error message that will be thrown.
I would appreciate anyone writes a step by step guide with screenshots if possible showing how to accomplish this process.

Related

Importing BetaAnalyticsDataClient from #google-analytics/data returns Webpack 5 errors

I am trying to create a custom dashboard on the angular app I am building. I want to use the data from Google Analytics 4 using Google Analytics Data API. I've successfully ran the scripts provided in their tutorial Using Node.JS client libraries (https://developers.google.com/analytics/devguides/reporting/data/v1/quickstart-client-libraries?hl=en_US).
I've ran the quickstart script they have provided using Node.JS and was able to get the expected response. However, when I try to ran the same script on my angular app via importing the script to fetch returned values, "ng serve" returns webpack 5 errors.
The issue was importing the BetaAnalyticsDataClient. Whenever I import it, the nodemon returns webpack errors.
The structure is like this:
simple component structure
I have a 'site-statistics.component' with its html and scss files.
I have a script named 'google-analytics.ts' inside the subfolder "services".
site-statistics.component imports an exported function from google-analytics.ts
If the issue is my usage of the Google Analytics Data API client library, I want to know how I would be able get the response from the script and send it to my angular component.
Options I've tried, but failed:
Tried adding a resolve.fallback on webpack.config.ts. However, issue still persists.
Tried setting paths to each packages on my TSCONFIG.JSON.
Tried using react-scripts-rewired
If the issue is the way I use the Google Analytics Data API client library, I want to know how I would be able get the response from the script and send it to my angular component.

how to properly initialize firebase functions test using cloud function?

I am using
Node 14
firebase-functions-test: 0.2.3
firebase-admin: 9.6.0
firebase-functions: 3.13.2
firebase tools: 9.8.0
so I want to perform unit testing for my firestore trigger function using firebase cloud function, I read the steps from the documentation in here.
I want to perform unit test using Firebase Emulator. so I assume I will initialize the SDK in offline mode. the documentation said that
If you would like to write completely offline tests, you can
initialize the SDK without any parameters:
so I initialize it like this
import * as firebase from "firebase-functions-test";
const test = firebase();
const wrapped = test.wrap(myFunctions.onCreate);
// rest of my test code
but when I run the test, I have this error:
Error: Could not load the default credentials. Browse to
https://cloud.google.com/docs/authentication/getting-started for more
information.
so even though I will use Firebase emulator (offline), it seems I need to provide the credential, which is the step if online mode is used, like explained from documentation in here
so I initialize the SDK like this
import * as firebase from "firebase-functions-test";
const test = firebase({
databaseURL: "https://xxx-b843e.firebaseio.com",
projectId: "xxx-b843e",
}, "../../../service-account.json");
const wrapped = test.wrap(myFunctions.onCreate);
// rest of my test code
but when I run the test, I have another error
{"severity":"WARNING","message":"Warning, FIREBASE_CONFIG and
GCLOUD_PROJECT environment variables are missing. Initializing
firebase-admin will fail"}
Error: The file at ../../../service-account.json does not exist, or it
is not a file. ENOENT: no such file or directory, lstat
'/Users/xxx/Documents/service-account.json'
the service account json file doesn't exist? I believe I have set the path correctly like this
in fact, I use intellisense to guide me to service-account.json path
the service-account.json file is like the image below, i get it from firebase project overview --> project settings --> service accounts --> generate new private key
what should I do if I want to initialize firebase functions test SDK in Firebase emulator?
Explanation
Error: The file at ../../../service-account.json does not exist
That error means that the service-account.json file could not be found at runtime (when you started the emulator), because the relative path is incorrect.
in fact, I use intellisense to guide me to service-account.json path
The path that intellisense suggests may not be the correct relative to the directory the code is executing in. This is because typescript is compiled before it's run, usually from within a lib or dist folder, which means the relative path needs to be changed.
Solution 1
You can use an absolute path /my/absolutepath/to/service-account.json, this is a fast way to resolve this problem, but isn't very portable.
Solution 2
Experiment with different amounts of ../../ to find the exact relative path of the service-account.json. Try using:
../../service-account.json
../../../service-account.json
etc ... (until it works)
It's most likely only off by a few ../
To use the offline mode you need to stub API calls like admin.initializeApp(). See the example given at https://firebase.google.com/docs/functions/unit-testing#importing_your_functions
// If index.js calls admin.initializeApp at the top of the file,
// we need to stub it out before requiring index.js. This is because the
// functions will be executed as a part of the require process.
// Here we stub admin.initializeApp to be a dummy function that doesn't do anything.
adminInitStub = sinon.stub(admin, 'initializeApp');
// Now we can require index.js and save the exports inside a namespace called myFunctions.
myFunctions = require('../index');
To test against the emulator you need to the initialize the test SDK with an emulator URL. Your file not found error is probably due to a problem with resolving relative paths. Try providing an absolute path and see if that works.
Getting rid of the warning should be as simple as adding the following to your jest.preset.js (or whatever code runs before your unit tests run).
const test = require('firebase-functions-test')();
https://firebase.google.com/docs/functions/unit-testing#offline-mode

Node Module not being referenced on server

I am trying to link the Mandrill library into my node project.
I have backtracked it right upto the following code which "stops" the project. This code is from the mandril API documentation.
var mandrill = require('mandrill-api/mandrill');
var mandrill_client = new mandrill.Mandrill('My KEY');
I have installed the mandril api using npm install mandrill-api and I can see it my node_modules folder. However when I deploy the project to the server the application stops running. It clearly is not referencing properly but I don't see how.
The code is being called in config.js. I'm sure I'm making a stupid mistake.

node.js googleapis library cannot find module '/node_modules/googleapis/apis/youtube/v3'

I'm using the the official Google Apis node.js library to fetch YouTube user info. I already have this working fine in another project, but for some reason I'm getting a strange error in this one. The goal is to fetch a list of videos scoped to a particular playlist id.
I'm using Mithril.js with coffeescript. Here's the relevant snippet:
google = require 'googleapis'
Properties.controller = (options = {}) ->
#showVideos = (playlistId) =>
youtube = google.youtube('v3')
youtube.playlistItems.list({part:'snippet', playlistId: playlistId}).then(videos) ->
console.log videos
The google client loads fine, but on this line:
youtube = google.youtube('v3')
I get the following error:
Uncaught Error: Unable to load endpoint youtube("v3"): Cannot find module '/node_modules/googleapis/apis/youtube/v3'
I use this exact same code in another project on the same computer in a neighboring directory. They are both using the same version of googleapis (I copied the working folder to this project just to be doubly sure).
I tried doing this with some of the other client endpoints such as with the usage examples. None of them work. So it must be some kind of path variable?

Kiip web sdk integration showing bad request error in console

I just integrated KIIP SDK for web in my node js application and it works correctly . But the problem is it showing error on browser console on each page refresh, But this error does not affect working of this sdk, it works perfectly.
the errror message showing is,
'POST https://api.kiip.me/2.0/web/moment/?r=1426508956613 400 (Bad Request)'.
My kiip code integration is as follows,
1) Included the script file on head tag
2)Declared the app key as global variable,
kiip_app_key='app-key from kiip site';
3) And intialized the kiip instance and invoked the method.
var kiipInstance = new Kiip(kiip_app_key);
kiipInstance.setTestMode();
kiipInstance.postMoment('received offer');
Andrew from Kiip here.A few things could be causing this:
The page is running from a local file or host. Solution: run the test page on a server.
The app is not submitted for live rewards. Solution: Submit for live rewards in the Kiip dashboard.
You're passing an incorrect app key. Solution: copy the app_key for the corresponding app from the Kiip dashboard and paste it as you're global variable.
Hope this helps,
Andrew

Resources