import request from '../../utils/request'; export interface CloudFunctionHttpTriggerOptions { /** * 小程序appId */ appId: string; /** * 服务空间ID */ spaceId: string; /** * 开启/关闭云函数触发功能 */ enable: boolean; /** * 云函数名 */ name: string; /** * 云函数HTTP触发路径,必须以/http开头,不能以/结尾, * 同一个Space下不允许重复,只支持(/)、(-)、(_)、(.)、字母和数字组合,最长不超过128个字符 */ httpTriggerPath?: string; } export interface CloudFunctionHttpTriggerResult { /** * 是否开启 */ enable: boolean; /** * 云函数HTTP触发器全路径 */ httpTriggerPath?: string; } interface UpdateHttpTriggerRes { success: boolean; defaultEndpoint?: string; enableService: boolean; } interface UpdateFunctionRes { success: boolean; httpTriggerPath?: string; } /** * 开启或者关闭云函数的http触发器 */ async function updateFunctionHttpTrigger( options: CloudFunctionHttpTriggerOptions ): Promise { const { spaceId, name, appId, httpTriggerPath, enable } = options; let createRes; if (enable) { createRes = await request({ method: 'GET', host: 'ide', path: '/cli/cloud/function/updateHttpTrigger.json', needSign: true, data: { spaceId, appId, enable: 'true', }, }); if (!createRes.success) { throw new Error('设置HTTP触发功能失败'); } } const updateRes = await request({ method: 'GET', host: 'ide', path: '/cli/cloud/function/updateFunction.json', needSign: true, data: { spaceId, appId, name, httpTriggerPath: enable ? httpTriggerPath : '', }, }); if (enable) { return { enable: true, httpTriggerPath: `https://${createRes.defaultEndpoint}${updateRes.httpTriggerPath}`, }; } return { enable: false, }; } export default updateFunctionHttpTrigger;