@cloudbase/node-sdk
Advanced tools
Comparing version 3.4.0 to 3.5.0
@@ -45,2 +45,3 @@ "use strict"; | ||
const functions_1 = require("./functions"); | ||
const cloudrun_1 = require("./cloudrun"); | ||
const database_1 = require("./database"); | ||
@@ -115,2 +116,5 @@ const storage_1 = require("./storage"); | ||
} | ||
async callContainer(callContainerOptions, opts) { | ||
return await (0, cloudrun_1.callContainer)(this, callContainerOptions, opts); | ||
} | ||
async callWxOpenApi(wxOpenApiOptions, opts) { | ||
@@ -117,0 +121,0 @@ return await (0, wx_1.callWxOpenApi)(this, wxOpenApiOptions, opts); |
@@ -31,3 +31,14 @@ "use strict"; | ||
const cloudbase_1 = require("../cloudbase"); | ||
async function callFunction(cloudbase, { name, qualifier, data }, opts) { | ||
const cloudrun_1 = require("../cloudrun"); | ||
async function callFunction(cloudbase, callFunctionOptions, opts) { | ||
// cloudrunfunctions | ||
if (callFunctionOptions.type === 'cloudrun') { | ||
const resp = await (0, cloudrun_1.callContainer)(cloudbase, callFunctionOptions, opts); | ||
return { | ||
requestId: resp.requestId, | ||
result: resp.data | ||
}; | ||
} | ||
// cloudfunctions | ||
const { name, qualifier, data } = callFunctionOptions; | ||
const { TCB_ROUTE_KEY } = cloudbase_1.CloudBase.getCloudbaseContext(); | ||
@@ -34,0 +45,0 @@ let transformData; |
@@ -128,2 +128,6 @@ "use strict"; | ||
try { | ||
if (buf.byteLength === 0) { | ||
resolve(undefined); | ||
return; | ||
} | ||
resolve(JSON.parse(buf.toString())); | ||
@@ -167,2 +171,3 @@ } | ||
err.connecting = req.socket.connecting; | ||
err.url = `${req.protocol}://${req.host}${req.path}`; | ||
cb(err); | ||
@@ -222,2 +227,8 @@ }); | ||
} | ||
else { | ||
// 如果显式指明 noBody 则直接调用 end | ||
if (opts.noBody) { | ||
req.end(); | ||
} | ||
} | ||
// NOTE: http(s).request 需手动调用 end 方法 | ||
@@ -224,0 +235,0 @@ if (options.method.toLowerCase() === 'get') { |
@@ -74,2 +74,3 @@ "use strict"; | ||
statusCode: res.statusCode, | ||
headers: res.headers, | ||
body | ||
@@ -76,0 +77,0 @@ }); |
@@ -29,3 +29,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.request = exports.TcbApiHttpRequester = exports.getEnvIdFromContext = void 0; | ||
exports.request = exports.TcbApiHttpRequester = exports.prepareCredentials = exports.getCredentialsOnDemand = exports.getEnvIdFromContext = void 0; | ||
const http_1 = __importDefault(require("http")); | ||
@@ -91,2 +91,31 @@ /* eslint-disable-next-line */ | ||
} | ||
exports.getCredentialsOnDemand = getCredentialsOnDemand; | ||
async function prepareCredentials() { | ||
const opts = this.opts; | ||
// CrossAccountInfo: 跨账号调用 | ||
const getCrossAccountInfo = opts.getCrossAccountInfo || this.config.getCrossAccountInfo; | ||
/* istanbul ignore if */ | ||
if (getCrossAccountInfo) { | ||
const crossAccountInfo = await getCrossAccountInfo(); | ||
const { credential } = crossAccountInfo; | ||
const { secretId, secretKey, token } = credential || {}; | ||
this.config = Object.assign(Object.assign({}, this.config), { secretId, | ||
secretKey, sessionToken: token }); | ||
if (!this.config.secretId || !this.config.secretKey) { | ||
throw E(Object.assign(Object.assign({}, code_1.ERROR.INVALID_PARAM), { message: 'missing secretId or secretKey of tencent cloud' })); | ||
} | ||
// 替换掉原函数,缓存数据,这里缓存是否起作用,取决于 this 实例是否复用 | ||
// 另一处获取 authorization 的代码可以服用吃这里的缓存 | ||
this.opts.getCrossAccountInfo = async () => await Promise.resolve(crossAccountInfo); | ||
} | ||
else { | ||
const { secretId, secretKey, sessionToken } = this.config; | ||
const credentials = await getCredentialsOnDemand({ secretId, secretKey, sessionToken }); | ||
this.config = Object.assign(Object.assign({}, this.config), { secretId: credentials.secretId, secretKey: credentials.secretKey, sessionToken: credentials.sessionToken }); | ||
if (!this.config.secretId || !this.config.secretKey) { | ||
throw E(Object.assign(Object.assign({}, code_1.ERROR.INVALID_PARAM), { message: 'missing secretId or secretKey of tencent cloud, please set secretId and secretKey in config' })); | ||
} | ||
} | ||
} | ||
exports.prepareCredentials = prepareCredentials; | ||
class TcbApiHttpRequester { | ||
@@ -252,27 +281,3 @@ constructor(args) { | ||
async prepareCredentials() { | ||
const opts = this.opts; | ||
// CrossAccountInfo: 跨账号调用 | ||
const getCrossAccountInfo = opts.getCrossAccountInfo || this.config.getCrossAccountInfo; | ||
/* istanbul ignore if */ | ||
if (getCrossAccountInfo) { | ||
const crossAccountInfo = await getCrossAccountInfo(); | ||
const { credential } = crossAccountInfo; | ||
const { secretId, secretKey, token } = credential || {}; | ||
this.config = Object.assign(Object.assign({}, this.config), { secretId, | ||
secretKey, sessionToken: token }); | ||
if (!this.config.secretId || !this.config.secretKey) { | ||
throw E(Object.assign(Object.assign({}, code_1.ERROR.INVALID_PARAM), { message: 'missing secretId or secretKey of tencent cloud' })); | ||
} | ||
// 替换掉原函数,缓存数据,这里缓存是否起作用,取决于 this 实例是否复用 | ||
// 另一处获取 authorization 的代码可以服用吃这里的缓存 | ||
this.opts.getCrossAccountInfo = async () => await Promise.resolve(crossAccountInfo); | ||
} | ||
else { | ||
const { secretId, secretKey, sessionToken } = this.config; | ||
const credentials = await getCredentialsOnDemand({ secretId, secretKey, sessionToken }); | ||
this.config = Object.assign(Object.assign({}, this.config), { secretId: credentials.secretId, secretKey: credentials.secretKey, sessionToken: credentials.sessionToken }); | ||
if (!this.config.secretId || !this.config.secretKey) { | ||
throw E(Object.assign(Object.assign({}, code_1.ERROR.INVALID_PARAM), { message: 'missing secretId or secretKey of tencent cloud, please set secretId and secretKey in config' })); | ||
} | ||
} | ||
prepareCredentials.bind(this)(); | ||
} | ||
@@ -279,0 +284,0 @@ getHeaders(method, url, params) { |
{ | ||
"name": "@cloudbase/node-sdk", | ||
"version": "3.4.0", | ||
"version": "3.5.0", | ||
"description": "tencent cloud base server sdk for node.js", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -14,2 +14,3 @@ import axios from 'axios' | ||
ICallFunctionOptions, | ||
ICallContainerOptions, | ||
@@ -41,2 +42,3 @@ IUploadFileOptions, | ||
import { callFunction } from './functions' | ||
import { callContainer } from './cloudrun' | ||
import { newDb } from './database' | ||
@@ -158,2 +160,6 @@ import { uploadFile, deleteFile, getTempFileURL, downloadFile, getUploadMetadata, getFileAuthority, copyFile } from './storage' | ||
public async callContainer<ParaT, ResultT>(callContainerOptions: ICallContainerOptions<ParaT>, opts?: ICustomReqOpts) { | ||
return await callContainer<ParaT, ResultT>(this, callContainerOptions, opts) | ||
} | ||
public async callWxOpenApi(wxOpenApiOptions: ICallWxOpenApiOptions, opts?: ICustomReqOpts): Promise<ICallWxOpenApiResult> { | ||
@@ -160,0 +166,0 @@ return await callWxOpenApi(this, wxOpenApiOptions, opts) |
@@ -7,3 +7,16 @@ import * as tcbapicaller from '../utils/tcbapirequester' | ||
export async function callFunction<ParaT, ResultT>(cloudbase: CloudBase, { name, qualifier, data }: ICallFunctionOptions<ParaT>, opts?: ICustomReqOpts): Promise<CallFunctionResult<ResultT>> { | ||
import { callContainer } from '../cloudrun' | ||
export async function callFunction<ParaT, ResultT>(cloudbase: CloudBase, callFunctionOptions: ICallFunctionOptions<ParaT>, opts?: ICustomReqOpts): Promise<CallFunctionResult<ResultT>> { | ||
// cloudrunfunctions | ||
if (callFunctionOptions.type === 'cloudrun') { | ||
const resp = await callContainer(cloudbase, callFunctionOptions, opts) | ||
return { | ||
requestId: resp.requestId, | ||
result: resp.data as ResultT | ||
} | ||
} | ||
// cloudfunctions | ||
const { name, qualifier, data } = callFunctionOptions | ||
const { TCB_ROUTE_KEY } = CloudBase.getCloudbaseContext() | ||
@@ -10,0 +23,0 @@ |
@@ -125,2 +125,6 @@ import http from 'http' | ||
try { | ||
if (buf.byteLength === 0) { | ||
resolve(undefined) | ||
return | ||
} | ||
resolve(JSON.parse(buf.toString())) | ||
@@ -164,2 +168,3 @@ } catch (e) { | ||
; (err as any).connecting = req.socket.connecting | ||
; (err as any).url = `${req.protocol}://${req.host}${req.path}` | ||
cb(err) | ||
@@ -238,2 +243,7 @@ }) | ||
req.end() | ||
} else { | ||
// 如果显式指明 noBody 则直接调用 end | ||
if (opts.noBody) { | ||
req.end() | ||
} | ||
} | ||
@@ -240,0 +250,0 @@ |
@@ -69,2 +69,3 @@ import http from 'http' | ||
statusCode: number | ||
headers: http.IncomingHttpHeaders | ||
body: string | Buffer | ||
@@ -108,2 +109,3 @@ } | ||
statusCode: res.statusCode, | ||
headers: res.headers, | ||
body | ||
@@ -110,0 +112,0 @@ }) |
@@ -39,3 +39,3 @@ import http from 'http' | ||
async function getCredentialsOnDemand(credentials: ITencentCloudCredentials): Promise<ITencentCloudCredentials> { | ||
export async function getCredentialsOnDemand(credentials: ITencentCloudCredentials): Promise<ITencentCloudCredentials> { | ||
const { secretId, secretKey } = credentials | ||
@@ -90,5 +90,49 @@ | ||
export async function prepareCredentials(): Promise<void> { | ||
const opts = this.opts | ||
// CrossAccountInfo: 跨账号调用 | ||
const getCrossAccountInfo = opts.getCrossAccountInfo || this.config.getCrossAccountInfo | ||
/* istanbul ignore if */ | ||
if (getCrossAccountInfo) { | ||
const crossAccountInfo = await getCrossAccountInfo() | ||
const { credential } = crossAccountInfo | ||
const { secretId, secretKey, token } = credential || {} | ||
this.config = { | ||
...this.config, | ||
secretId, | ||
secretKey, | ||
sessionToken: token | ||
} | ||
if (!this.config.secretId || !this.config.secretKey) { | ||
throw E({ | ||
...ERROR.INVALID_PARAM, | ||
message: 'missing secretId or secretKey of tencent cloud' | ||
}) | ||
} | ||
// 替换掉原函数,缓存数据,这里缓存是否起作用,取决于 this 实例是否复用 | ||
// 另一处获取 authorization 的代码可以服用吃这里的缓存 | ||
this.opts.getCrossAccountInfo = async () => await Promise.resolve(crossAccountInfo) | ||
} else { | ||
const { secretId, secretKey, sessionToken } = this.config | ||
const credentials = await getCredentialsOnDemand({ secretId, secretKey, sessionToken }) | ||
this.config = { | ||
...this.config, | ||
secretId: credentials.secretId, | ||
secretKey: credentials.secretKey, | ||
sessionToken: credentials.sessionToken | ||
} | ||
if (!this.config.secretId || !this.config.secretKey) { | ||
throw E({ | ||
...ERROR.INVALID_PARAM, | ||
message: 'missing secretId or secretKey of tencent cloud, please set secretId and secretKey in config' | ||
}) | ||
} | ||
} | ||
} | ||
export class TcbApiHttpRequester { | ||
private readonly args: IRequestInfo | ||
private config: ICloudBaseConfig | ||
private readonly config: ICloudBaseConfig | ||
private readonly opts: ICustomReqOpts | ||
@@ -281,43 +325,3 @@ private readonly defaultTimeout = 15000 | ||
private async prepareCredentials(): Promise<void> { | ||
const opts = this.opts | ||
// CrossAccountInfo: 跨账号调用 | ||
const getCrossAccountInfo = opts.getCrossAccountInfo || this.config.getCrossAccountInfo | ||
/* istanbul ignore if */ | ||
if (getCrossAccountInfo) { | ||
const crossAccountInfo = await getCrossAccountInfo() | ||
const { credential } = crossAccountInfo | ||
const { secretId, secretKey, token } = credential || {} | ||
this.config = { | ||
...this.config, | ||
secretId, | ||
secretKey, | ||
sessionToken: token | ||
} | ||
if (!this.config.secretId || !this.config.secretKey) { | ||
throw E({ | ||
...ERROR.INVALID_PARAM, | ||
message: 'missing secretId or secretKey of tencent cloud' | ||
}) | ||
} | ||
// 替换掉原函数,缓存数据,这里缓存是否起作用,取决于 this 实例是否复用 | ||
// 另一处获取 authorization 的代码可以服用吃这里的缓存 | ||
this.opts.getCrossAccountInfo = async () => await Promise.resolve(crossAccountInfo) | ||
} else { | ||
const { secretId, secretKey, sessionToken } = this.config | ||
const credentials = await getCredentialsOnDemand({ secretId, secretKey, sessionToken }) | ||
this.config = { | ||
...this.config, | ||
secretId: credentials.secretId, | ||
secretKey: credentials.secretKey, | ||
sessionToken: credentials.sessionToken | ||
} | ||
if (!this.config.secretId || !this.config.secretKey) { | ||
throw E({ | ||
...ERROR.INVALID_PARAM, | ||
message: 'missing secretId or secretKey of tencent cloud, please set secretId and secretKey in config' | ||
}) | ||
} | ||
} | ||
prepareCredentials.bind(this)() | ||
} | ||
@@ -324,0 +328,0 @@ |
@@ -354,2 +354,10 @@ import fs from 'fs' | ||
// async?: boolean | ||
// Used for 云函数2.0 CallContainer | ||
type?: 'cloudrun' | ||
method?: 'POST' | ||
path?: string | ||
header?: IHeaderOpts & { | ||
// 'X-WX-SERVICE': string | ||
} | ||
} | ||
@@ -362,2 +370,26 @@ | ||
// 接口定义参考 wx.Cloud.callContainer 尽可能保持一致 | ||
// Cloud.callContainer(options: Object): Promise<Object> | ||
// https://developers.weixin.qq.com/miniprogram/dev/wxcloudservice/wxcloud/reference-sdk-api/container/Cloud.callContainer.html | ||
export interface ICallContainerOptions<T> { | ||
name: string | ||
method?: 'POST' | ||
path?: string | ||
header?: IHeaderOpts & { | ||
// 'X-WX-SERVICE': string | ||
} | ||
data?: T | ||
// timeout?: number | ||
// dataType?: string | ||
// responseType?: string | ||
} | ||
export interface CallContainerResult<T> { | ||
requestId: string | ||
statusCode: number | ||
header?: IHeaderOpts | ||
data?: T | ||
} | ||
/* ********************************** wx ********************************** */ | ||
@@ -478,2 +510,7 @@ | ||
/** | ||
* callContainer - 调用云托管 | ||
*/ | ||
callContainer<ParaT, ResultT>(callContainerOptions: ICallContainerOptions<ParaT>, opts?: ICustomReqOpts): Promise<CallContainerResult<ResultT>> | ||
/** | ||
* uploadFile - 上传文件 | ||
@@ -480,0 +517,0 @@ */ |
@@ -45,2 +45,3 @@ import { | ||
times?: number | ||
noBody?: boolean | ||
} | ||
@@ -47,0 +48,0 @@ |
255808
66
7112
18