{"version":3,"file":"extractClientError-X9wJVqGq.mjs","names":[],"sources":["../src/lib/requests/submitPrivacyRequest.ts","../src/lib/requests/extractClientError.ts"],"sourcesContent":["import {\n  IsoCountryCode,\n  IsoCountrySubdivisionCode,\n  RequestAction,\n  RequestStatus,\n} from '@transcend-io/privacy-types';\nimport { valuesOf, decodeCodec } from '@transcend-io/type-utils';\nimport type { Got } from 'got';\nimport * as t from 'io-ts';\nimport { uniq } from 'lodash-es';\n\nimport { PrivacyRequestInput } from './mapCsvRowsToRequestInputs.js';\nimport { ParsedAttributeInput } from './parseAttributesFromString.js';\n\nexport const PrivacyRequestResponse = t.type({\n  id: t.string,\n  link: t.string,\n  status: valuesOf(RequestStatus),\n  type: valuesOf(RequestAction),\n  subjectType: t.string,\n  email: t.union([t.null, t.string]),\n  coreIdentifier: t.string,\n  isSilent: t.boolean,\n  isTest: t.boolean,\n  country: t.union([t.null, valuesOf(IsoCountryCode)]),\n  countrySubDivision: t.union([t.null, valuesOf(IsoCountrySubdivisionCode)]),\n  attributeValues: t.array(\n    t.type({\n      attributeKey: t.type({ name: t.string }),\n      name: t.string,\n    }),\n  ),\n});\n\n/** Type override */\nexport type PrivacyRequestResponse = t.TypeOf<typeof PrivacyRequestResponse>;\n\n/**\n * Submit a privacy request to the Transcend API\n *\n * @param sombra - Sombra instance configured to make requests\n * @param input - Request input\n * @param options - Additional options\n * @returns Successfully submitted request\n */\nexport async function submitPrivacyRequest(\n  sombra: Got,\n  input: PrivacyRequestInput,\n  {\n    details = '',\n    isTest = false,\n    emailIsVerified = true,\n    skipSendingReceipt = false,\n    isSilent = true,\n    additionalAttributes = [],\n  }: {\n    /** Whether or not the request is a test request */\n    isTest?: boolean;\n    /** Whether or not the request is in silent mode */\n    isSilent?: boolean;\n    /** Whether the email is verified up front */\n    emailIsVerified?: boolean;\n    /** When true, skip sending of the email receipt */\n    skipSendingReceipt?: boolean;\n    /** Request details */\n    details?: string;\n    /** Additional attributes to tag the requests with */\n    additionalAttributes?: ParsedAttributeInput[];\n  } = {},\n): Promise<PrivacyRequestResponse> {\n  // Merge the per-request attributes with the\n  // global attributes\n  const mergedAttributes = [...additionalAttributes];\n  (input.attributes || []).forEach((attribute) => {\n    const existing = mergedAttributes.find((attr) => attr.key === attribute.key);\n    if (existing) {\n      existing.values.push(...attribute.values);\n      existing.values = uniq(existing.values);\n    } else {\n      mergedAttributes.push(attribute);\n    }\n  });\n\n  // Make the GraphQL request\n  let response: unknown;\n  try {\n    response = await sombra\n      .post('v1/data-subject-request', {\n        json: {\n          type: input.requestType,\n          subject: {\n            coreIdentifier: input.coreIdentifier,\n            email: input.email,\n            emailIsVerified,\n            attestedExtraIdentifiers: input.attestedExtraIdentifiers,\n          },\n          subjectType: input.subjectType,\n          isSilent,\n          isTest,\n          skipSendingReceipt,\n          ...(input.locale ? { locale: input.locale } : {}),\n          details,\n          attributes: mergedAttributes,\n          ...(input.country || input.countrySubDivision\n            ? {\n                region: {\n                  ...(input.country\n                    ? {\n                        country: input.country,\n                      }\n                    : input.countrySubDivision\n                      ? { country: input.countrySubDivision.split('-')[0] }\n                      : {}),\n                  ...(input.countrySubDivision\n                    ? { countrySubDivision: input.countrySubDivision }\n                    : {}),\n                },\n              }\n            : {}),\n          ...(input.createdAt ? { createdAt: input.createdAt } : {}),\n          ...(input.dataSiloIds ? { dataSiloIds: input.dataSiloIds } : {}),\n          ...(input.status ? { completedRequestStatus: input.status } : {}),\n        },\n      })\n      .json();\n  } catch (err) {\n    throw new Error(`Received an error from server: ${err?.response?.body || err?.message}`);\n  }\n\n  const { request: requestResponse } = decodeCodec(\n    t.type({\n      request: PrivacyRequestResponse,\n    }),\n    response,\n  );\n  return requestResponse;\n}\n","const CLIENT_ERROR = /{\\\\\"message\\\\\":\\\\\"(.+?)\\\\\",/;\n\n/**\n * Extract a client error from the request\n *\n * @param err - Error message\n * @returns Client error or null\n */\nexport function extractClientError(err: string): string | null {\n  return CLIENT_ERROR.test(err) ? CLIENT_ERROR.exec(err)![1] : null;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