npm-middleware-repo 1.0.0
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/index.js +1 -0
- package/index.mjs +4 -0
- package/lib.cjs +47 -0
- package/package.json +32 -0
package/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
module.exports = require('./lib.cjs');
|
package/index.mjs
ADDED
package/lib.cjs
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
function initializeSK8Middleware({ apiKey, baseUrl }) {
|
|
2
|
+
if (!apiKey) {
|
|
3
|
+
throw new Error('SK8 embedded middleware: "apiKey" is required');
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
const finalBaseUrl = baseUrl ?? 'http://localhost:3000/api';
|
|
7
|
+
|
|
8
|
+
return async function embeddedMiddleware(req, res, next) {
|
|
9
|
+
try {
|
|
10
|
+
const targetUrl = `${finalBaseUrl}${req.url}`;
|
|
11
|
+
const headers = {
|
|
12
|
+
'x-api-key': apiKey,
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
if (req.headers['content-type']) {
|
|
16
|
+
headers['Content-Type'] = req.headers['content-type'];
|
|
17
|
+
} else {
|
|
18
|
+
headers['Content-Type'] = 'application/json';
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const hasBody = !['GET', 'HEAD', 'OPTIONS'].includes(req.method);
|
|
22
|
+
const body = hasBody ? JSON.stringify(req.body ?? {}) : undefined;
|
|
23
|
+
|
|
24
|
+
const upstream = await fetch(targetUrl, {
|
|
25
|
+
method: req.method,
|
|
26
|
+
headers,
|
|
27
|
+
body,
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
const text = await upstream.text();
|
|
31
|
+
res.statusCode = upstream.status;
|
|
32
|
+
const contentType = upstream.headers.get('content-type');
|
|
33
|
+
if (contentType) res.setHeader('Content-Type', contentType);
|
|
34
|
+
res.end(text);
|
|
35
|
+
} catch (err) {
|
|
36
|
+
if (typeof next === 'function') {
|
|
37
|
+
next(err);
|
|
38
|
+
} else {
|
|
39
|
+
res.statusCode = 500;
|
|
40
|
+
res.setHeader('Content-Type', 'application/json');
|
|
41
|
+
res.end(JSON.stringify({ error: err.message || 'Internal Server Error' }));
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
module.exports = { initializeSK8Middleware };
|
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "npm-middleware-repo",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "",
|
|
5
|
+
"homepage": "https://github.com/uladzislau-smirnou/middleware-npm-package#readme",
|
|
6
|
+
"bugs": {
|
|
7
|
+
"url": "https://github.com/uladzislau-smirnou/middleware-npm-package/issues"
|
|
8
|
+
},
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/uladzislau-smirnou/middleware-npm-package.git"
|
|
12
|
+
},
|
|
13
|
+
"license": "ISC",
|
|
14
|
+
"author": "",
|
|
15
|
+
"type": "commonjs",
|
|
16
|
+
"main": "index.js",
|
|
17
|
+
"module": "index.mjs",
|
|
18
|
+
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"import": "./index.mjs",
|
|
21
|
+
"require": "./index.js"
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
"files": [
|
|
25
|
+
"index.js",
|
|
26
|
+
"index.mjs",
|
|
27
|
+
"lib.cjs"
|
|
28
|
+
],
|
|
29
|
+
"scripts": {
|
|
30
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
31
|
+
}
|
|
32
|
+
}
|