@qelos/plugin-netlify-api 1.0.2 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/index.js +15 -14
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -44,8 +44,8 @@ Optional: set the API URL (default `http://159.203.152.168`) and/or enable bypas
|
|
|
44
44
|
|
|
45
45
|
- **Environment:** Sets `QELOS_API_IP` for the build (and for the proxy function at runtime).
|
|
46
46
|
- **Redirect:** Adds a rewrite so `/api/*` is served by `/.netlify/functions/qelos-api-proxy` (status 200, force).
|
|
47
|
-
- **Function:**
|
|
47
|
+
- **Function:** Copies `qelos-api-proxy.ts` from the package into your Netlify **functions directory** (default `netlify/functions`, or `[functions] directory` in `netlify.toml`) during `onPreBuild`, so the bundler sees a normal function file. Forwards requests using `QELOS_API_IP` / `API_HOST` (full URL or hostname).
|
|
48
48
|
|
|
49
|
-
No need to add redirects or the
|
|
49
|
+
No need to add redirects or commit the proxy yourself; the plugin writes it each build. Optional: add `netlify/functions/qelos-api-proxy.ts` to `.gitignore` if you run local Netlify builds and do not want the copy tracked.
|
|
50
50
|
|
|
51
51
|
**Parity with a manual Netlify function** (e.g. `[[redirects]]` → `/.netlify/functions/api-proxy` and a function using `process.env.API_HOST`): use this plugin instead of those entries; set `QELOS_API_IP` or `API_HOST` to the same value you used for `API_HOST`, and set `bypass_admin = true` under `[plugins.inputs]` if your old proxy always sent `x-bypass-admin: true`.
|
package/index.js
CHANGED
|
@@ -1,9 +1,15 @@
|
|
|
1
|
+
const fs = require('node:fs/promises');
|
|
1
2
|
const path = require('node:path');
|
|
2
3
|
|
|
3
4
|
const DEFAULT_API_URL = 'http://159.203.152.168';
|
|
4
5
|
|
|
6
|
+
function resolveFunctionsDirectory(netlifyConfig) {
|
|
7
|
+
const dir = netlifyConfig.functions?.directory ?? 'netlify/functions';
|
|
8
|
+
return path.isAbsolute(dir) ? dir : path.join(process.cwd(), dir);
|
|
9
|
+
}
|
|
10
|
+
|
|
5
11
|
module.exports = {
|
|
6
|
-
async onPreBuild({ netlifyConfig,
|
|
12
|
+
async onPreBuild({ netlifyConfig, inputs }) {
|
|
7
13
|
const apiUrl =
|
|
8
14
|
inputs.api_url ?? process.env.QELOS_API_IP ?? DEFAULT_API_URL;
|
|
9
15
|
const bypassAdmin =
|
|
@@ -19,13 +25,17 @@ module.exports = {
|
|
|
19
25
|
}
|
|
20
26
|
|
|
21
27
|
if (!netlifyConfig.functions) {
|
|
22
|
-
// [functions]
|
|
23
|
-
// directory = "netlify/functions"
|
|
24
28
|
netlifyConfig.functions = { directory: 'netlify/functions' };
|
|
25
|
-
} else {
|
|
26
|
-
// should use this dir as path of the stored function
|
|
27
29
|
}
|
|
28
30
|
|
|
31
|
+
// Copy the proxy into the site's functions dir so Netlify bundles it like any
|
|
32
|
+
// checked-in function (utils.functions.add from node_modules is unreliable for .ts).
|
|
33
|
+
const destDir = resolveFunctionsDirectory(netlifyConfig);
|
|
34
|
+
const destFile = path.join(destDir, 'qelos-api-proxy.ts');
|
|
35
|
+
const srcFile = path.join(__dirname, 'functions', 'qelos-api-proxy.ts');
|
|
36
|
+
await fs.mkdir(destDir, { recursive: true });
|
|
37
|
+
await fs.copyFile(srcFile, destFile);
|
|
38
|
+
|
|
29
39
|
// Add redirect: /api/* -> Netlify function (proxy)
|
|
30
40
|
if (!netlifyConfig.redirects) {
|
|
31
41
|
netlifyConfig.redirects = [];
|
|
@@ -36,14 +46,5 @@ module.exports = {
|
|
|
36
46
|
status: 200,
|
|
37
47
|
force: true,
|
|
38
48
|
});
|
|
39
|
-
|
|
40
|
-
// Inject the proxy function (must run in onPreBuild so it's bundled)
|
|
41
|
-
const pluginRoot = __dirname;
|
|
42
|
-
const functionPath = path.join(
|
|
43
|
-
pluginRoot,
|
|
44
|
-
'functions',
|
|
45
|
-
'qelos-api-proxy.ts',
|
|
46
|
-
);
|
|
47
|
-
await utils.functions.add(functionPath);
|
|
48
49
|
},
|
|
49
50
|
};
|