I've created a javascript script that will get information from this api. I'm trying to set this up inside of a google cloud function. I understand how you "require" or "import" a reference normally inside of a google cloud function, and even that you can make reference to a local file. When i downloaded the api.js from the source and try to reference it, i get "bomgarState is undefined" where bomgarState is the reference that i need. I understand that it is looking for a json when an api is being defined but i need to use the api.js. I know that this code can work,when you run this in collaboration with an html that sources said api, i have no problems. I have tried many different solutions such as "require(./api.js)", getting from url, and much more but am unsure if this is something that is supported at this point.
TLDR: can't make reference or source a javascript api inside of google cloud functions, had been sourcing using html, now that we moved to google cloud functions can't figure out how to make reference.
For you to import dependencies and libraries to your Cloud Functions, there are some available options that you can take with JavaScript, but only while using Node.js - without Node it's not possible.
For example, as the documentation Specifying dependencies in Node.js indicates:
A function is allowed to use external Node.js modules as well as local data. Dependencies in Node.js are managed with npm and expressed in a metadata file called package.json.
So, you can use with Node.js the npm to import your dependencies to your Cloud Functions.
Other options are indicated in the following posts from the Community - that I would recommend you to take a look at it - one, for example, would be using the 'gcloud' command to deploy your function. This way, it would upload and deploy with all files from your directory.
Google Cloud Functions include private library
How do I structure Cloud Functions for Firebase to deploy multiple functions from multiple files?
I hope these articles help you, since they indicate the only available options, unfortunately, to upload dependencies on your Cloud Functions.
Related
I am getting to grips with vanilla JS and am trying to expand my horizons using some browser API's to make some more interesting applications. I am running Webpack, Babel and Jest on most of my packages. I am trying to establish the differences between browser API's and NPM packages.
I understand what they are individually but am unsure how they interact with each other. From my understanding :
API's of the browser are ready available for us to use which are often objects we have to instantiate to use their pre-written methods in our javascript code, they are not javascript objects but are available for us to use and like anything that is global is attached to the window object.
Third Party API's are accessible via endpoints. e.g. we can make a request to a weather API by our use typing in a city and we can use this as an argument to our asychronus request to the weather API to return some JSON to us.
I understand NPM packages like jest or webpack which are written in javascript. This allow us to install other javascript files which we can import or can be run as a standalone package in the terminal instead of having to make this functionality ourselves. There are no browser API's which allow us to do this so we call upon an NPM package. They have a purpose.
What I cant understand is packages like Axios. Does Axios build on the browser fetch API and just rename itself Axios ? Or the Payments Request NPM package? Why do we need that if we have a browser API called Payments Request ? There are even packages called Console and DOM. Why are these repeated as npm packages when we can access them anyway through the Browser ? Third Party API's I can access also all seem to have NPM packages ! What is the point of these if I can directly access the API anyway through a fetch or axios request?
On the whole I guess my question is, why are there so many NPM packages for things we can already access via a browser or third party API ? Confused.
I built a client-side modular weather app by using Openweathermap API and Javascript. Since it's client side it sends the API key to user. So I found Netlify's serverless functions to hide my API keys but couldn't figure out how to use it with Parcel. I use Parcel as a bundler and it seems impossible to me using serverless functions with Parcel. Because it bundles everything into one big file but serverless things should be seperated. I did some research and fortunately there are resources for serverless functions but couldn't find anything for that exact situation. So I'll be grateful if I find a solution for this problem.
I have a Node server and multiple controllers that perform DB operations and helpers (For e-mail, for example) within that directory.
I'd like to use source from that directory within my functions. Assuming the following directory structure:
src/
server/
/app/controllers/email_helper.js
fns/
send-confirm/
What's the best way to use email_helper within the send-confirm function?
I've tried:
Symbolically linking the 'server' directory
Adding a local repo to send-confirm/package.json
Neither of the above work.
In principle, your Cloud Functions can use any other Node.js module, the same way any standard Node.js server would. However, since Cloud Functions needs to build your module in the cloud, it needs to be able to locate those dependency modules from the cloud. This is where the issue lies.
Cloud Functions can load modules from any one of these places:
Any public npm repository.
Any web-visible URL.
Anywhere in the functions/ directory that firebase init generates for you, and which gets uploaded on firebase deploy.
In your case, from the perspective of functions/package.json, the ../server/ directory doesn't fall under any of those categories, and so Cloud Functions can't use your module. Unfortunately, firebase deploy doesn't follow symlinks, which is why that solution doesn't work.
I see two possible immediate fixes:
Move your server/ directory to be under functions/. I realize this isn't the prettiest directory layout, but it's the easiest fix while hacking. In functions/package.json you can then have a local dependency on ./server.
Expose your code behind a URL somewhere. For example, you could package up a .tar and put that on Google Drive, or on Firebase Cloud Storage. Alternatively, you can use a public git repository.
In the future, I'd love it if firebase deploy followed symlinks. I've filed a feature request for that in Firebase's internal bug tracker.
So i have a static Webpage which uses some API. There's app.js file that has API key in it stored in an object property.I need to hide it using Netlify dev. How do I do that? How do I use Netlify dev tools in native js? I most probably need node js installed as well but this topic is not entirely clear to me... Could someone make a step by step tutorial for me?
If it's a static site, it won't be able to directly access the hidden netlify env variables.
If you are hosting the app.js file along with your static site then it will be easy for someone to browse to it and see the API key you are trying to hide.
One solution though is to define a Netlify Function that does the API call, and then the JS in your static site can call that function.
Your Netlify Function will be effectively a backend for your app, and can access the ENV variables you set in the Netlify UI, via process.env.
See this tutorial for a step-by-step guide.
Update re the new gatsby-build information in comments.
You can put variables in a git-ignored .env file, which means they will not end up on git, but will still get pulled in and included in the gatsby production code, which will be visible to the client, so isn't recommended for API access keys. Ideally you should connect via a backend to secure those keys, as in the above original answer.
However, instead of using a .env file, if you are using the Netlify Dev CLI then this will automatically pull down any env vars you have set in the online Netlify settings, and allow you to use those in your local environment.
From the cli docs:
Netlify Dev brings the functionality of your Netlify production environment directly to your local machine. This includes custom headers/redirects and environment variables.
Before you flame me, I've done my research (Javascript ENV variables). I know that it is not possible to access system environment variables using Javascript.
I'm using Yeoman to develop a Javascript library. That library is for use with Google Maps API and some of my tests require that Google Maps API as a dependency. To load the Google Maps API script, you need an API key. Now, my code is located on my Github and I really wouldn't like to have my API key as part of the code.
Is there any Node module that would be able to inject an environment variable into my tests when running Grunt? Are there any ENV variable equivalents in Node.js?
This is an example of a need for a JS ENV variable solution. There are other ways around the problem, but I am specifically looking for this type of a solution.
Edit: most importantly, how do I have these ENV variables auto load into my program when I run tests using grunt
Thank you!
Yes, you can access the environment variables in a Node script via the process object.
console.log(process.env.secret_key);
Storing your API keys in environment variables is much better than doing so in your repo.