UNPKG

@htdangkhoa/google-ads

Version:
37 lines (36 loc) 1.76 kB
import { credentials } from '@grpc/grpc-js'; import { GoogleAdsFailure } from './generated/google/ads/googleads/v19/errors/errors.js'; import { FAILURE_KEY } from './constants.js'; export const getCredentials = (authClient) => { const ssl = credentials.createSsl(); const callCredentials = credentials.createFromGoogleCredential(authClient); const channelCredentials = credentials.combineChannelCredentials(ssl, callCredentials); return channelCredentials; }; export const decodeGoogleAdsFailureBuffer = (buffer) => { const input = new Uint8Array(buffer); return GoogleAdsFailure.decode(input); }; export 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, FAILURE_KEY)) === 'undefined') { return error; } // @ts-expect-error const [buffer] = error.metadata.internalRepr.get(FAILURE_KEY); return decodeGoogleAdsFailureBuffer(buffer); }; export 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: decodeGoogleAdsFailureBuffer(buffer) }); };