UNPKG

@htdangkhoa/google-ads

Version:
44 lines (43 loc) 2.26 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.decodePartialFailureError = exports.getGoogleAdsError = exports.decodeGoogleAdsFailureBuffer = exports.getCredentials = void 0; const grpc_js_1 = require("@grpc/grpc-js"); const errors_js_1 = require("./generated/google/ads/googleads/v19/errors/errors.js"); const constants_js_1 = require("./constants.js"); const getCredentials = (authClient) => { const ssl = grpc_js_1.credentials.createSsl(); const callCredentials = grpc_js_1.credentials.createFromGoogleCredential(authClient); const channelCredentials = grpc_js_1.credentials.combineChannelCredentials(ssl, callCredentials); return channelCredentials; }; exports.getCredentials = getCredentials; const decodeGoogleAdsFailureBuffer = (buffer) => { const input = new Uint8Array(buffer); return errors_js_1.GoogleAdsFailure.decode(input); }; exports.decodeGoogleAdsFailureBuffer = decodeGoogleAdsFailureBuffer; const getGoogleAdsError = (error) => { var _a, _b, _c; // @ts-expect-error if (typeof ((_c = (_b = (_a = error.metadata) === null || _a === void 0 ? void 0 : _a.internalRepr) === null || _b === void 0 ? void 0 : _b.get) === null || _c === void 0 ? void 0 : _c.call(_b, constants_js_1.FAILURE_KEY)) === 'undefined') { return error; } // @ts-expect-error const [buffer] = error.metadata.internalRepr.get(constants_js_1.FAILURE_KEY); return (0, exports.decodeGoogleAdsFailureBuffer)(buffer); }; exports.getGoogleAdsError = getGoogleAdsError; const decodePartialFailureError = (response) => { var _a; if (!response.partial_failure_error) return response; const { details } = response.partial_failure_error; if (!details) return response; const { value } = (_a = details.find((detail) => { var _a; return (_a = detail.type_url) === null || _a === void 0 ? void 0 : _a.includes('errors.GoogleAdsFailure'); })) !== null && _a !== void 0 ? _a : {}; if (!value) return response; const buffer = Buffer.from(value); return Object.assign(Object.assign({}, response), { partial_failure_error: (0, exports.decodeGoogleAdsFailureBuffer)(buffer) }); }; exports.decodePartialFailureError = decodePartialFailureError;