Skip to content

Commit cb9a910

Browse files
authored
Use default options when netlify.toml isn't available (#15)
* Add default options to avoid failure when netlify.toml is not present * Use /api as the default prefix path * Redirects from /.netlify/* are not used.
1 parent 34d9c93 commit cb9a910

File tree

2 files changed

+30
-23
lines changed

2 files changed

+30
-23
lines changed

README.md

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ You may provide additional configuration in the `netlify.toml` file to configure
6565
functions = "functions" # Functions source directory. Use this if you would like to use Nimbella to deploy your functions.
6666
timeout = 6000 # Function timeout limit in milliseconds.
6767
memory = 256 # Function memory limit in MB.
68-
path = "/.netlify/functions/" # The prefix path to access your deployed packages. Change this if you're using both Netlify Functions and Nimbella for your backend.
68+
path = "/api/" # The prefix path to access your deployed packages.
6969
```
7070

7171
## Usage
@@ -76,7 +76,7 @@ In this section, you will learn how to structure your repository and `netlify.to
7676

7777
The Nimbella add-on for Netlify allows you to use [Nimbella projects](https://nimbella.io/downloads/nim/nim.html#overview-of-nimbella-projects-actions-and-deployment) to automate packaging and deployment. We suggest reading the documentation about [Nimbella projects](https://nimbella.io/downloads/nim/nim.html#overview-of-nimbella-projects-actions-and-deployment) at some point. We provide a quick introduction here.
7878

79-
Nimbella projects inspect a directory named `packages` at the base of your repository. The contents of this directory dictate the serverless functions that are deployed. The plugin will automatically deploy the functions inside `packages` and will also create redirect rules so all requests to `/.netlify/functions/*` are redirected to functions (also called actions) deployed on Nimbella.
79+
Nimbella projects inspect a directory named `packages` at the base of your repository. The contents of this directory dictate the serverless functions that are deployed. The plugin will automatically deploy the functions inside `packages` and all of the functions (also called actions) can accessed using the following pattern: `https://your-site.com/<path(default="api")>/<packageName>/<actionName>`.
8080

8181
For example, for the following project structure:
8282

@@ -96,14 +96,7 @@ site
9696
└── index.html
9797
```
9898

99-
You will invoke the function `auth/login.js` via the API end point `https://your-site.com/.netlify/functions/auth/login`.
100-
101-
If you're using Netlify Functions, you need to change the base prefix `/.netlify/functions/` to something different (e.g. `/api/`) in `netlify.toml` so Netlify Functions can be accessed using `/.netlify/functions/` and Nimbella Functions can be accessed using `/api/` route.
102-
103-
```toml
104-
[nimbella]
105-
path = '/api/' # default /.netlify/functions/.
106-
```
99+
You will invoke the function `auth/login.js` via the API end point `https://your-site.com/api/auth/login`.
107100

108101
#### Deploy Netlify Functions on Nimbella Cloud
109102

@@ -122,7 +115,7 @@ This plugin builds your functions using a modified version of [netlify-lambda](h
122115

123116
All enviroment variables present in the build runtime during Netlify build (except `CI` and `NETLIFY`) are made availabe to the deployed functions on Nimbella Cloud.
124117

125-
**Note:** When you're using `packages` along with functions, make sure to apend "default" to `.netlify/functions` to invoke the functions as all functions are deployed under `default` package of your namespace.
118+
**Note:** When you're using `packages` along with functions, make sure to apend "default" to `/api/` to invoke the functions as all functions are deployed under `default` package of your namespace.
126119

127120
## Examples
128121

index.js

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,17 @@ const toml = require('@iarna/toml');
55
const cpx = require('cpx');
66
const build = require('netlify-lambda/lib/build');
77

8-
let config = {};
8+
const NIM_CLI = 'https://apigcp.nimbella.io/downloads/nim/nimbella-cli.tgz';
9+
const functionsBuildDir = `functions-build-${Date.now()}`;
10+
let netlifyToml = {};
911
let isProject = false;
1012
let isActions = false;
11-
const functionsBuildDir = `functions-build-${Date.now()}`;
12-
const NIM_CLI = 'https://apigcp.nimbella.io/downloads/nim/nimbella-cli.tgz';
13+
let options = {
14+
functions: '',
15+
timeout: 6000,
16+
memory: 256,
17+
path: '/api/'
18+
};
1319

1420
// Disable auto updates of nim.
1521
process.env.NIM_DISABLE_AUTOUPDATE = '1';
@@ -85,9 +91,16 @@ module.exports = {
8591
await utils.cache.save(nimConfig);
8692
}
8793

88-
config = toml.parse(await readFile(constants.CONFIG_PATH));
94+
if (constants.CONFIG_PATH && existsSync(constants.CONFIG_PATH)) {
95+
netlifyToml = toml.parse(await readFile(constants.CONFIG_PATH));
96+
97+
if (netlifyToml.nimbella) {
98+
options = {...options, ...netlifyToml.nimbella};
99+
}
100+
}
101+
102+
isActions = options.functions ? existsSync(options.functions) : false;
89103
isProject = existsSync('packages');
90-
isActions = existsSync(config.nimbella.functions);
91104
} catch (error) {
92105
utils.build.failBuild(error.message);
93106
}
@@ -96,11 +109,11 @@ module.exports = {
96109
onBuild: async ({utils}) => {
97110
try {
98111
if (isActions) {
99-
// Here we're passing the build directory instead of source because source is extracted from config.nimbella.functions.
112+
// Here we're passing the build directory instead of source because source is extracted from options.functions.
100113
const stats = await build.run(functionsBuildDir);
101114
console.log(stats.toString(stats.compilation.options.stats));
102115
// Copy any files that do not end with .js. T
103-
cpx.copy(config.nimbella.functions + '/**/*.!(js)', functionsBuildDir);
116+
cpx.copy(options.functions + '/**/*.!(js)', functionsBuildDir);
104117
}
105118
} catch (error) {
106119
utils.build.failBuild(error.message);
@@ -119,8 +132,9 @@ module.exports = {
119132
await deployActions({
120133
run: utils.run,
121134
functionsDir: functionsBuildDir,
122-
timeout: config.nimbella.timeout || 6000, // Default is 10 seconds
123-
memory: config.nimbella.memory || 256 // Default is 256MB (max for free tier)
135+
secretsPath: join(process.cwd(), 'env.json'),
136+
timeout: options.timeout, // Default is 6 seconds
137+
memory: options.memory // Default is 256MB (max for free tier)
124138
});
125139
}
126140

@@ -137,11 +151,11 @@ module.exports = {
137151
redirects.push(...success);
138152
}
139153

140-
if (config.redirects) {
154+
if (netlifyToml.redirects) {
141155
console.log(
142156
"Found redirect rules in netlify.toml. We will rewrite rules that redirect (200 rewrites) to '/.netlify/functions/*'."
143157
);
144-
redirects.push(...config.redirects);
158+
redirects.push(...netlifyToml.redirects);
145159
}
146160

147161
for (const redirect of redirects) {
@@ -155,7 +169,7 @@ module.exports = {
155169
}
156170
}
157171

158-
let {path: redirectPath = '.netlify/functions'} = config.nimbella;
172+
let {path: redirectPath} = options;
159173
redirectPath = redirectPath.endsWith('/')
160174
? redirectPath
161175
: redirectPath + '/';

0 commit comments

Comments
 (0)