usetraceforge 0.1.8 → 0.1.9
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/dist/next-plugin.js +5 -2
- package/dist/webpack-loader.d.ts +1 -0
- package/dist/webpack-loader.js +32 -0
- package/package.json +1 -1
package/dist/next-plugin.js
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
import path from "path";
|
|
2
|
+
import { fileURLToPath } from "url";
|
|
3
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
4
|
+
const __dirname = path.dirname(__filename);
|
|
2
5
|
export function withTraceForgeConfig(nextConfig = {}) {
|
|
3
6
|
return {
|
|
4
7
|
...nextConfig,
|
|
@@ -8,8 +11,8 @@ export function withTraceForgeConfig(nextConfig = {}) {
|
|
|
8
11
|
test: /app\/api\/.*\/route\.(ts|js|tsx|jsx)$/,
|
|
9
12
|
use: [
|
|
10
13
|
{
|
|
11
|
-
// Point to our compiled
|
|
12
|
-
loader: path.resolve(__dirname, 'webpack-loader.
|
|
14
|
+
// Point to our compiled ESM loader
|
|
15
|
+
loader: path.resolve(__dirname, 'webpack-loader.js'),
|
|
13
16
|
},
|
|
14
17
|
],
|
|
15
18
|
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function traceForgeLoader(source: string): string;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
const methods = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'HEAD', 'OPTIONS'];
|
|
2
|
+
export default function traceForgeLoader(source) {
|
|
3
|
+
let modifiedSource = source;
|
|
4
|
+
let exportedMethods = [];
|
|
5
|
+
methods.forEach(method => {
|
|
6
|
+
// Matches: export async function GET OR export function GET
|
|
7
|
+
const funcRegex = new RegExp(`export\\s+(async\\s+)?function\\s+${method}\\b`, 'g');
|
|
8
|
+
// Matches: export const GET =
|
|
9
|
+
const arrowRegex = new RegExp(`export\\s+(const|let|var)\\s+${method}\\s*=`, 'g');
|
|
10
|
+
let matched = false;
|
|
11
|
+
if (funcRegex.test(modifiedSource)) {
|
|
12
|
+
matched = true;
|
|
13
|
+
modifiedSource = modifiedSource.replace(funcRegex, `const _tf_${method} = $1 function ${method}`);
|
|
14
|
+
}
|
|
15
|
+
if (arrowRegex.test(modifiedSource)) {
|
|
16
|
+
matched = true;
|
|
17
|
+
modifiedSource = modifiedSource.replace(arrowRegex, `const _tf_${method} =`);
|
|
18
|
+
}
|
|
19
|
+
if (matched) {
|
|
20
|
+
exportedMethods.push(method);
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
// If we found any API route exports, wrap them!
|
|
24
|
+
if (exportedMethods.length > 0) {
|
|
25
|
+
modifiedSource = `import { withTraceForge } from "usetraceforge/next";\n` + modifiedSource;
|
|
26
|
+
exportedMethods.forEach(method => {
|
|
27
|
+
modifiedSource += `\nexport const ${method} = withTraceForge(_tf_${method});`;
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
return modifiedSource;
|
|
31
|
+
}
|
|
32
|
+
;
|