Skip to content

Commit

Permalink
Js issue (#873)
Browse files Browse the repository at this point in the history
* bugfix: js 配置更新

* feat: getBearToken支持配置aksk

---------

Co-authored-by: dailin01 <[email protected]>
  • Loading branch information
dl102306 and dailin01 authored Nov 21, 2024
1 parent f0508f9 commit d585e79
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
9 changes: 9 additions & 0 deletions javascript/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,15 @@ main();
千帆 JavaScript SDK 也同步支持了 V2 版本的调用方式,用户可以通过配置 `version: 'v2'` 来创建对应的 V2 版本。
用户需要配置QIANFAN_ACCESS_KEY 和 QIANFAN_SECRET_KEY,可以配置appid(应用ID) ,不传使用静默 appid。
获取BearToken的示例代码如下:
```ts
import {getBearToken} from "@baiducloud/qianfan";

const bearToken = await getBearToken();
console.log(bearToken);
```
非流式情况下的调用示例如下所示:
```ts
import {ChatCompletion} from "@baiducloud/qianfan";
Expand Down
2 changes: 1 addition & 1 deletion javascript/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@baiducloud/qianfan",
"version": "0.2.1",
"version": "0.2.2",
"publishConfig": {
"access": "public",
"registry": "https://registry.npmjs.org/"
Expand Down
12 changes: 8 additions & 4 deletions javascript/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,9 @@ export async function consoleAction({base_api_route, data, action}: ConsoleActio

interface GetTokenProps {
expireInSeconds?: number;
ak?: string;
sk?: string;
bce_token?: string;
}

interface TokenResp {
Expand All @@ -372,14 +375,14 @@ interface TokenResp {
}

async function fetchBearToken(props?: GetTokenProps): Promise<TokenResp> {
const {expireInSeconds: expireInSecondsInProps} = props || {};
const {expireInSeconds: expireInSecondsInProps, ak, sk, bce_token} = props || {};
const config = getDefaultConfig();
const {QIANFAN_BEAR_TOKEN_URL} = config;
try {
// 鉴权
const httpClientConfig = getIAMConfig(
config.QIANFAN_ACCESS_KEY,
config.QIANFAN_SECRET_KEY,
ak || config.QIANFAN_ACCESS_KEY,
sk || config.QIANFAN_SECRET_KEY,
QIANFAN_BEAR_TOKEN_URL
);
const client = new HttpClient(httpClientConfig);
Expand All @@ -390,7 +393,8 @@ async function fetchBearToken(props?: GetTokenProps): Promise<TokenResp> {
params: {expireInSeconds},
headers: {
...DEFAULT_HEADERS,
}
...(bce_token ? {'x-bce-security-token': bce_token} : {}),
},
});
const fetchInstance = new Fetch();
const {url, ...rest} = fetchOptions;
Expand Down

0 comments on commit d585e79

Please sign in to comment.