Skip to content

Commit 709ad50

Browse files
committed
fix: test case
1 parent af66421 commit 709ad50

File tree

4 files changed

+55
-9
lines changed

4 files changed

+55
-9
lines changed

packages/cli/plugin-bff/src/utils/runtimeGenerator.ts

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,50 @@
11
import path from 'path';
22
import { fs } from '@modern-js/utils';
33

4+
/**
5+
* Get package name from package.json file
6+
* @param appDirectory - Application directory path
7+
* @returns Package name or undefined if not found
8+
*/
9+
const getPackageName = (appDirectory: string): string | undefined => {
10+
try {
11+
const packageJsonPath = path.resolve(appDirectory, './package.json');
12+
const packageJson = require(packageJsonPath);
13+
return packageJson.name;
14+
} catch (error) {
15+
// If package.json doesn't exist or is invalid, return undefined
16+
return undefined;
17+
}
18+
};
19+
420
async function runtimeGenerator({
521
runtime,
622
appDirectory,
723
relativeDistPath,
8-
}: { runtime: string; appDirectory: string; relativeDistPath: string }) {
24+
packageName,
25+
}: {
26+
runtime: string;
27+
appDirectory: string;
28+
relativeDistPath: string;
29+
packageName?: string;
30+
}) {
931
const pluginDir = path.resolve(
1032
appDirectory,
1133
`./${relativeDistPath}`,
1234
'runtime',
1335
);
1436

37+
const requestId =
38+
packageName ||
39+
getPackageName(appDirectory) ||
40+
process.env.npm_package_name ||
41+
'default';
42+
1543
const source = `import { configure as _configure } from '${runtime}'
1644
const configure = (options) => {
1745
return _configure({
1846
...options,
19-
requestId: '${process.env.npm_package_name}',
47+
requestId: '${requestId}',
2048
});
2149
}
2250
export { configure }

packages/server/bff-core/src/client/generateClient.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,22 @@ import type { HttpMethodDecider } from '@modern-js/types';
33
import { ApiRouter } from '../router';
44
import { Err, Ok, type Result } from './result';
55

6+
/**
7+
* Get package name from package.json file
8+
* @param appDir - Application directory path
9+
* @returns Package name or undefined if not found
10+
*/
11+
const getPackageName = (appDir: string): string | undefined => {
12+
try {
13+
const packageJsonPath = path.resolve(appDir, './package.json');
14+
const packageJson = require(packageJsonPath);
15+
return packageJson.name;
16+
} catch (error) {
17+
// If package.json doesn't exist or is invalid, return undefined
18+
return undefined;
19+
}
20+
};
21+
622
export type GenClientResult = Result<string>;
723

824
export type GenClientOptions = {
@@ -65,7 +81,9 @@ export const generateClient = async ({
6581
const routeName = routePath;
6682

6783
const requestId =
68-
target === 'bundle' ? process.env.npm_package_name : undefined;
84+
target === 'bundle'
85+
? getPackageName(appDir) || process.env.npm_package_name
86+
: undefined;
6987

7088
if (action === 'upload') {
7189
const requestOptions = {

packages/server/bff-core/tests/client/__snapshots__/generateClient.test.ts.snap

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
exports[`client generateClient should support cross croject invocation 1`] = `
44
"import { createRequest } from '@modern-js/plugin-bff/client';
55
6-
export var DELETE = createRequest({path: '/normal/origin',method: 'DELETE',port: 3000,httpMethodDecider: 'functionName', requestId: '@modern-js/bff-core'});
7-
export default createRequest({path: '/normal/origin',method: 'GET',port: 3000,httpMethodDecider: 'functionName', requestId: '@modern-js/bff-core'});
8-
export var putRepo = createRequest({path: '/put-repo',method: 'PUT',port: 3000,httpMethodDecider: 'functionName', requestId: '@modern-js/bff-core'});
6+
export var DELETE = createRequest({path: '/normal/origin',method: 'DELETE',port: 3000,httpMethodDecider: 'functionName', requestId: 'tests'});
7+
export default createRequest({path: '/normal/origin',method: 'GET',port: 3000,httpMethodDecider: 'functionName', requestId: 'tests'});
8+
export var putRepo = createRequest({path: '/put-repo',method: 'PUT',port: 3000,httpMethodDecider: 'functionName', requestId: 'tests'});
99
"
1010
`;
1111

packages/server/bff-core/tests/client/generateClient.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ describe('client', () => {
2626
const source = await fs.readFile(resourcePath, 'utf-8');
2727

2828
const result = await generateClient({
29-
appDir: PWD,
29+
appDir: __dirname,
3030
prefix,
3131
port,
3232
resourcePath,
@@ -49,7 +49,7 @@ describe('client', () => {
4949
const source = await fs.readFile(resourcePath, 'utf-8');
5050

5151
const result = await generateClient({
52-
appDir: PWD,
52+
appDir: __dirname,
5353
prefix,
5454
port,
5555
resourcePath,
@@ -72,7 +72,7 @@ describe('client', () => {
7272
const source = await fs.readFile(resourcePath, 'utf-8');
7373

7474
const result = await generateClient({
75-
appDir: PWD,
75+
appDir: __dirname,
7676
prefix,
7777
port,
7878
resourcePath,

0 commit comments

Comments
 (0)