Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
saas_plus
/
dj-car-life-system
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit 028afd0e
authored
Aug 17, 2026
by
张立顺
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
feat: 添加本地 GSC 请求客户端
1 parent
5009f82b
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
308 additions
and
4 deletions
docs/superpowers/plans/2026-08-17-remove-sentry.md
src/api/gscClient.js
src/api/gscClient.spec.js
docs/superpowers/plans/2026-08-17-remove-sentry.md
View file @
028afd0
...
...
@@ -245,7 +245,7 @@ Expected: tests PASS,ESLint 无诊断,提交只包含上述四个文件。
-
Private: buildHeaders(config, data, params, createNonce, now) 合并默认 header、自定义 header 与签名 header。
-
Private: processResponse(response, config) 按 code_key、returnAll、成功码和登录失效码返回兼容结果并触发事件。
-
[
]
**Step 1: 写入失败的客户端单测**
-
[
x
]
**Step 1: 写入失败的客户端单测**
Create src/api/gscClient.spec.js。统一配置:
...
...
@@ -278,7 +278,7 @@ function createConfig(events = {}) {
3.
code 为 "20" 时只触发 events.logout,不触发 events.error。
4.
request reject 时触发 events.error(error, mergedConfig),并向调用方抛出同一个 error。
-
[
]
**Step 2: 运行测试确认失败**
-
[
x
]
**Step 2: 运行测试确认失败**
Run:
...
...
@@ -288,7 +288,7 @@ npm run test:unit -- src/api/gscClient.spec.js --runInBand
Expected: FAIL,提示找不到 ./gscClient。
-
[
]
**Step 3: 实现客户端**
-
[
x
]
**Step 3: 实现客户端**
Create src/api/gscClient.js,完整流程固定为:
...
...
@@ -339,7 +339,7 @@ export function createGscClient(config = {}, dependencies = {}) {
}
~~~
-
[
]
**Step 4: 运行测试和 ESLint 并提交**
-
[
x
]
**Step 4: 运行测试和 ESLint 并提交**
Run:
...
...
src/api/gscClient.js
0 → 100644
View file @
028afd0
import
axios
from
"axios"
;
import
{
createGscNonce
,
createGscSignature
}
from
"./gscSignature"
;
function
mergeConfig
(
defaults
,
requestConfig
,
url
)
{
return
{
...
defaults
,
...
requestConfig
,
events
:
{
...(
defaults
.
events
||
{}),
...(
requestConfig
.
events
||
{}),
},
headers
:
{
"Content-Type"
:
"application/json;charset=UTF-8"
,
...(
defaults
.
headers
||
{}),
...(
requestConfig
.
headers
||
{}),
},
params
:
{
...(
defaults
.
params
||
{}),
...(
requestConfig
.
params
||
{}),
},
response
:
{
...(
defaults
.
response
||
{}),
...(
requestConfig
.
response
||
{}),
},
session
:
{
requiresSession
:
false
,
sessionParamName
:
"sessionId"
,
...(
defaults
.
session
||
{}),
...(
requestConfig
.
session
||
{}),
},
timeout
:
requestConfig
.
timeout
||
defaults
.
timeout
||
15000
,
url
,
};
}
async
function
buildRequestParams
(
config
)
{
const
params
=
{
...
config
.
params
};
if
(
!
config
.
session
.
requiresSession
)
{
return
params
;
}
const
source
=
config
.
session
.
sessionId
;
if
(
!
source
)
{
throw
new
Error
(
"需要sessionId,但未提供获取方法"
);
}
const
sessionId
=
await
Promise
.
resolve
(
typeof
source
===
"function"
?
source
()
:
source
,
);
if
(
typeof
sessionId
!==
"string"
||
!
sessionId
)
{
throw
new
Error
(
"获取到的sessionId无效"
);
}
params
[
config
.
session
.
sessionParamName
||
"sessionId"
]
=
sessionId
;
return
params
;
}
function
buildHeaders
(
config
,
data
,
params
,
createNonce
,
now
)
{
const
headers
=
{
...
config
.
headers
};
if
(
!
config
.
appId
||
!
config
.
appSecret
)
{
console
.
warn
(
"缺少签名所需的应用ID或密钥"
);
return
headers
;
}
return
{
...
headers
,
...
createGscSignature
({
appId
:
config
.
appId
,
appSecret
:
config
.
appSecret
,
data
,
method
:
"POST"
,
noSignAttr
:
config
.
noSignAttr
||
[],
nonce
:
createNonce
(),
params
,
timestamp
:
now
(),
}),
};
}
function
callEvent
(
config
,
eventName
,
data
)
{
const
handler
=
config
.
events
&&
config
.
events
[
eventName
];
if
(
typeof
handler
===
"function"
)
{
handler
(
data
,
config
);
}
}
function
findValidCodeKey
(
codeKey
,
data
)
{
if
(
!
data
||
typeof
data
!==
"object"
)
{
return
undefined
;
}
if
(
Array
.
isArray
(
codeKey
))
{
return
codeKey
.
find
(
key
=>
key
in
data
);
}
return
codeKey
in
data
?
codeKey
:
undefined
;
}
function
processResponse
(
responseData
,
config
)
{
callEvent
(
config
,
"response"
,
responseData
);
const
res
=
responseData
.
data
;
const
responseConfig
=
config
.
response
;
if
(
!
responseConfig
||
!
responseConfig
.
code_key
)
{
return
responseData
;
}
const
codeKey
=
findValidCodeKey
(
responseConfig
.
code_key
,
res
);
if
(
!
codeKey
)
{
return
responseData
;
}
const
code
=
res
[
codeKey
];
const
errId
=
responseConfig
.
errId
||
"errId"
;
const
dataKey
=
responseConfig
.
data_key
||
"data"
;
const
logoutValue
=
responseConfig
.
errId_logout_value
;
const
isLogoutRequired
=
logoutValue
!==
undefined
&&
(
String
(
code
)
===
String
(
logoutValue
)
||
String
(
res
[
errId
])
===
String
(
logoutValue
));
if
(
isLogoutRequired
)
{
callEvent
(
config
,
"logout"
,
res
);
}
if
(
config
.
returnAll
)
{
return
{
...
responseData
,
data
:
res
,
};
}
const
successValue
=
responseConfig
.
code_success_value
===
undefined
?
"0"
:
String
(
responseConfig
.
code_success_value
);
if
(
String
(
code
)
===
successValue
)
{
callEvent
(
config
,
"success"
,
res
);
return
res
[
dataKey
];
}
if
(
!
isLogoutRequired
)
{
callEvent
(
config
,
"error"
,
res
);
}
return
{
...
responseData
,
data
:
res
[
dataKey
],
};
}
export
function
createGscClient
(
config
=
{},
dependencies
=
{})
{
const
request
=
dependencies
.
request
||
(
options
=>
axios
.
request
(
options
));
const
createNonce
=
dependencies
.
createNonce
||
createGscNonce
;
const
now
=
dependencies
.
now
||
Date
.
now
;
return
{
async
post
(
url
,
data
=
{},
requestConfig
=
{})
{
const
mergedConfig
=
mergeConfig
(
config
,
requestConfig
,
url
);
let
response
;
try
{
const
params
=
await
buildRequestParams
(
mergedConfig
);
const
headers
=
buildHeaders
(
mergedConfig
,
data
,
params
,
createNonce
,
now
);
response
=
await
request
({
baseURL
:
mergedConfig
.
baseURL
,
data
,
headers
,
method
:
"post"
,
params
,
timeout
:
mergedConfig
.
timeout
,
url
,
});
}
catch
(
error
)
{
callEvent
(
mergedConfig
,
"error"
,
error
);
throw
error
;
}
return
processResponse
(
response
,
mergedConfig
);
},
};
}
src/api/gscClient.spec.js
0 → 100644
View file @
028afd0
import
{
createGscClient
}
from
"./gscClient"
;
function
createConfig
(
events
=
{})
{
return
{
appId
:
"test-app"
,
appSecret
:
"test-secret"
,
baseURL
:
"https://gsc.example.com"
,
events
,
response
:
{
code_key
:
[
"code"
,
"returnCode"
],
code_success_value
:
"0"
,
data_key
:
"data"
,
errId
:
"errId"
,
errId_logout_value
:
"20"
,
},
session
:
{
requiresSession
:
true
,
sessionId
:
()
=>
"token-1"
,
},
};
}
function
createDependencies
(
request
)
{
return
{
createNonce
:
()
=>
"nonce-1"
,
now
:
()
=>
1700000000000
,
request
,
};
}
describe
(
"gscClient"
,
()
=>
{
it
(
"应注入 session 和签名并解包成功响应"
,
async
()
=>
{
const
request
=
jest
.
fn
().
mockResolvedValue
({
config
:
{
requestId
:
"request-1"
},
data
:
{
code
:
"0"
,
data
:
{
id
:
1
}
},
headers
:
{
traceId
:
"trace-1"
},
status
:
200
,
});
const
success
=
jest
.
fn
();
const
client
=
createGscClient
(
createConfig
({
success
}),
createDependencies
(
request
),
);
await
expect
(
client
.
post
(
"/orders"
,
{
name
:
"张三"
})).
resolves
.
toEqual
({
id
:
1
});
expect
(
request
).
toHaveBeenCalledWith
({
baseURL
:
"https://gsc.example.com"
,
data
:
{
name
:
"张三"
},
headers
:
expect
.
objectContaining
({
"Content-Type"
:
"application/json;charset=UTF-8"
,
"X-App-Id"
:
"test-app"
,
"X-Nonce"
:
"nonce-1"
,
"X-Sign"
:
expect
.
any
(
String
),
"X-Timestamp"
:
"1700000000000"
,
}),
method
:
"post"
,
params
:
{
sessionId
:
"token-1"
},
timeout
:
15000
,
url
:
"/orders"
,
});
expect
(
success
).
toHaveBeenCalledWith
(
{
code
:
"0"
,
data
:
{
id
:
1
}
},
expect
.
objectContaining
({
url
:
"/orders"
}),
);
});
it
(
"returnAll 应保留完整响应和原始响应体"
,
async
()
=>
{
const
response
=
{
config
:
{
requestId
:
"request-1"
},
data
:
{
code
:
"0"
,
data
:
{
id
:
1
}
},
headers
:
{
traceId
:
"trace-1"
},
status
:
200
,
};
const
request
=
jest
.
fn
().
mockResolvedValue
(
response
);
const
client
=
createGscClient
(
createConfig
(),
createDependencies
(
request
));
await
expect
(
client
.
post
(
"/orders"
,
{},
{
returnAll
:
true
})).
resolves
.
toEqual
(
response
);
});
it
(
"登录失效应只触发 logout 并返回解包后的兼容响应"
,
async
()
=>
{
const
error
=
jest
.
fn
();
const
logout
=
jest
.
fn
();
const
response
=
{
data
:
{
code
:
"20"
,
data
:
null
,
msg
:
"登录失效"
},
status
:
200
,
};
const
request
=
jest
.
fn
().
mockResolvedValue
(
response
);
const
client
=
createGscClient
(
createConfig
({
error
,
logout
}),
createDependencies
(
request
),
);
await
expect
(
client
.
post
(
"/orders"
)).
resolves
.
toEqual
({
...
response
,
data
:
null
,
});
expect
(
logout
).
toHaveBeenCalledWith
(
response
.
data
,
expect
.
objectContaining
({
url
:
"/orders"
}),
);
expect
(
error
).
not
.
toHaveBeenCalled
();
});
it
(
"普通业务失败应触发 error 并返回解包后的兼容响应"
,
async
()
=>
{
const
error
=
jest
.
fn
();
const
response
=
{
data
:
{
code
:
"500"
,
data
:
{
reason
:
"invalid"
},
msg
:
"业务失败"
},
status
:
200
,
};
const
request
=
jest
.
fn
().
mockResolvedValue
(
response
);
const
client
=
createGscClient
(
createConfig
({
error
}),
createDependencies
(
request
),
);
await
expect
(
client
.
post
(
"/orders"
)).
resolves
.
toEqual
({
...
response
,
data
:
{
reason
:
"invalid"
},
});
expect
(
error
).
toHaveBeenCalledWith
(
response
.
data
,
expect
.
objectContaining
({
url
:
"/orders"
}),
);
});
it
(
"网络失败应触发 error 并向调用方抛出原错误"
,
async
()
=>
{
const
requestError
=
new
Error
(
"network failed"
);
const
error
=
jest
.
fn
();
const
request
=
jest
.
fn
().
mockRejectedValue
(
requestError
);
const
client
=
createGscClient
(
createConfig
({
error
}),
createDependencies
(
request
),
);
await
expect
(
client
.
post
(
"/orders"
)).
rejects
.
toBe
(
requestError
);
expect
(
error
).
toHaveBeenCalledWith
(
requestError
,
expect
.
objectContaining
({
url
:
"/orders"
}),
);
});
});
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment