|
| 1 | +const { TypeError } = require('../../utils/error'); |
| 2 | + |
| 3 | +function isEmpty(val) { |
| 4 | + return val === undefined || val === null || (typeof val === 'number' && isNaN(val)); |
| 5 | +} |
| 6 | + |
| 7 | +function cleanEmptyValue(obj) { |
| 8 | + const newObj = {}; |
| 9 | + for (const key in obj) { |
| 10 | + const val = obj[key]; |
| 11 | + if (!isEmpty(val)) { |
| 12 | + newObj[key] = val; |
| 13 | + } |
| 14 | + } |
| 15 | + return newObj; |
| 16 | +} |
| 17 | + |
| 18 | +function apiFactory(actions) { |
| 19 | + const apis = {}; |
| 20 | + actions.forEach((action) => { |
| 21 | + apis[action] = async (capi, inputs) => { |
| 22 | + inputs = cleanEmptyValue(inputs); |
| 23 | + try { |
| 24 | + const { Response } = await capi.request( |
| 25 | + { |
| 26 | + Action: action, |
| 27 | + Version: '2018-08-08', |
| 28 | + // RequestClient: 'ServerlessComponent', |
| 29 | + // Token: capi.options.Token || null, |
| 30 | + ...inputs, |
| 31 | + }, |
| 32 | + { |
| 33 | + debug: false, |
| 34 | + ServiceType: 'apigateway', |
| 35 | + host: 'apigateway.tencentcloudapi.com', |
| 36 | + }, |
| 37 | + false, |
| 38 | + ); |
| 39 | + |
| 40 | + if (Response && Response.Error && Response.Error.Code) { |
| 41 | + if (Response.Error.Code.indexOf('ResourceNotFound') === -1) { |
| 42 | + throw new TypeError( |
| 43 | + `API_APIGW_${action}`, |
| 44 | + Response.Error.Message, |
| 45 | + null, |
| 46 | + Response.RequestId, |
| 47 | + ); |
| 48 | + } |
| 49 | + return null; |
| 50 | + } |
| 51 | + return Response.Result || Response; |
| 52 | + } catch (e) { |
| 53 | + throw new TypeError(`API_APIGW_${action}`, e.message, e.stack, e.reqId); |
| 54 | + } |
| 55 | + }; |
| 56 | + }); |
| 57 | + |
| 58 | + return apis; |
| 59 | +} |
| 60 | + |
| 61 | +const ACTIONS = [ |
| 62 | + 'CreateService', |
| 63 | + 'DeleteService', |
| 64 | + 'ModifyService', |
| 65 | + 'DescribeService', |
| 66 | + 'ReleaseService', |
| 67 | + 'UnReleaseService', |
| 68 | + 'CreateApi', |
| 69 | + 'DescribeApi', |
| 70 | + 'DeleteApi', |
| 71 | + 'ModifyApi', |
| 72 | + 'DescribeApisStatus', |
| 73 | + 'CreateUsagePlan', |
| 74 | + 'DescribeApiUsagePlan', |
| 75 | + 'DescribeUsagePlanSecretIds', |
| 76 | + 'DescribeUsagePlan', |
| 77 | + 'DeleteUsagePlan', |
| 78 | + 'ModifyUsagePlan', |
| 79 | + 'CreateApiKey', |
| 80 | + 'DeleteApiKey', |
| 81 | + 'DisableApiKey', |
| 82 | + 'EnableApiKey', |
| 83 | + 'DescribeApiKeysStatus', |
| 84 | + 'BindSecretIds', |
| 85 | + 'UnBindSecretIds', |
| 86 | + 'BindEnvironment', |
| 87 | + 'UnBindEnvironment', |
| 88 | + 'DescribeServiceSubDomains', |
| 89 | + 'BindSubDomain', |
| 90 | + 'UnBindSubDomain', |
| 91 | +]; |
| 92 | +const APIS = apiFactory(ACTIONS); |
| 93 | + |
| 94 | +module.exports = APIS; |
0 commit comments