{"version":3,"file":"oAuth.mjs","names":[],"sources":["../../../src/getIntlayerAPI/oAuth.ts"],"sourcesContent":["import type {\n  CliSessionTokenRoutes,\n  CreateCliSessionTokenResult,\n  cliSessionTokenContract,\n  GetCliSessionMeResult,\n} from '@intlayer/backend-contract/cliSessionToken';\nimport type { RouteEndpoints } from '@intlayer/backend-contract/defineRoute';\nimport { buildRouteURL } from '@intlayer/backend-contract/defineRoute';\nimport type {\n  GetOAuth2TokenBody,\n  GetOAuth2TokenResult,\n  OAuth2Routes,\n  oAuth2Contract,\n} from '@intlayer/backend-contract/oAuth2';\nimport { editor } from '@intlayer/config/built';\nimport { BACKEND_URL } from '@intlayer/config/defaultValues';\nimport type { IntlayerConfig } from '@intlayer/types/config';\nimport { type FetcherOptions, fetcher } from '../fetcher';\n\n/** OAuth2 routes, checked against the backend contract. */\nconst oAuth2Group = {\n  prefix: '/oauth2',\n} as const satisfies Pick<typeof oAuth2Contract, 'prefix'>;\nconst oAuth2Endpoints = {\n  getOAuth2AccessToken: { method: 'POST', path: '/token' },\n  extendOAuth2Token: { method: 'POST', path: '/token/extend' },\n} as const satisfies RouteEndpoints<OAuth2Routes>;\n\n/** CLI session routes, checked against the backend contract. */\nconst cliSessionGroup = {\n  prefix: '/api/cli-session',\n} as const satisfies Pick<typeof cliSessionTokenContract, 'prefix'>;\nconst cliSessionEndpoints = {\n  createCliSessionToken: { method: 'POST', path: '/' },\n  getCliSessionMe: { method: 'GET', path: '/me' },\n} as const satisfies RouteEndpoints<CliSessionTokenRoutes>;\n\nexport const getOAuthAPI = (\n  authAPIOptions: FetcherOptions = {},\n  intlayerConfig?: Pick<IntlayerConfig, 'editor'>\n) => {\n  const backendURL =\n    intlayerConfig?.editor?.backendURL ?? editor.backendURL ?? BACKEND_URL;\n  const { clientId, clientSecret } = intlayerConfig?.editor ?? {};\n\n  /**\n   * Gets an oAuth2 accessToken via client_credentials grant\n   */\n  const getOAuth2AccessToken = async (otherOptions: FetcherOptions = {}) =>\n    await fetcher<GetOAuth2TokenResult>(\n      buildRouteURL(\n        backendURL,\n        oAuth2Group,\n        oAuth2Endpoints.getOAuth2AccessToken\n      ),\n      {},\n      otherOptions,\n      {\n        method: oAuth2Endpoints.getOAuth2AccessToken.method,\n        headers: {\n          'Content-Type': 'application/x-www-form-urlencoded',\n        },\n        body: {\n          grant_type: 'client_credentials',\n          client_id: clientId!,\n          client_secret: clientSecret!,\n        } satisfies GetOAuth2TokenBody,\n      }\n    );\n\n  /**\n   * Creates a short-lived (2h) CLI session token for the authenticated user.\n   * Requires a valid browser session cookie.\n   */\n  const createCliSessionToken = async (otherOptions: FetcherOptions = {}) =>\n    await fetcher<CreateCliSessionTokenResult>(\n      buildRouteURL(\n        backendURL,\n        cliSessionGroup,\n        cliSessionEndpoints.createCliSessionToken\n      ),\n      authAPIOptions,\n      otherOptions,\n      { method: cliSessionEndpoints.createCliSessionToken.method }\n    );\n\n  /**\n   * Verifies a CLI session token and returns the associated project context.\n   * Useful for checking config consistency in the CLI.\n   */\n  const getCliSessionMe = async (otherOptions: FetcherOptions = {}) =>\n    await fetcher<GetCliSessionMeResult>(\n      buildRouteURL(\n        backendURL,\n        cliSessionGroup,\n        cliSessionEndpoints.getCliSessionMe\n      ),\n      authAPIOptions,\n      otherOptions,\n      { method: cliSessionEndpoints.getCliSessionMe.method }\n    );\n\n  return {\n    getOAuth2AccessToken,\n    createCliSessionToken,\n    getCliSessionMe,\n  };\n};\n"],"mappings":";;;;;;;AAoBA,MAAM,cAAc,EAClB,QAAQ,UACV;AACA,MAAM,kBAAkB;CACtB,sBAAsB;EAAE,QAAQ;EAAQ,MAAM;CAAS;CACvD,mBAAmB;EAAE,QAAQ;EAAQ,MAAM;CAAgB;AAC7D;;AAGA,MAAM,kBAAkB,EACtB,QAAQ,mBACV;AACA,MAAM,sBAAsB;CAC1B,uBAAuB;EAAE,QAAQ;EAAQ,MAAM;CAAI;CACnD,iBAAiB;EAAE,QAAQ;EAAO,MAAM;CAAM;AAChD;AAEA,MAAa,eACX,iBAAiC,CAAC,GAClC,mBACG;CACH,MAAM,aACJ,gBAAgB,QAAQ,cAAc,OAAO,cAAc;CAC7D,MAAM,EAAE,UAAU,iBAAiB,gBAAgB,UAAU,CAAC;;;;CAK9D,MAAM,uBAAuB,OAAO,eAA+B,CAAC,MAClE,MAAM,QACJ,cACE,YACA,aACA,gBAAgB,oBAClB,GACA,CAAC,GACD,cACA;EACE,QAAQ,gBAAgB,qBAAqB;EAC7C,SAAS,EACP,gBAAgB,oCAClB;EACA,MAAM;GACJ,YAAY;GACZ,WAAW;GACX,eAAe;EACjB;CACF,CACF;;;;;CAMF,MAAM,wBAAwB,OAAO,eAA+B,CAAC,MACnE,MAAM,QACJ,cACE,YACA,iBACA,oBAAoB,qBACtB,GACA,gBACA,cACA,EAAE,QAAQ,oBAAoB,sBAAsB,OAAO,CAC7D;;;;;CAMF,MAAM,kBAAkB,OAAO,eAA+B,CAAC,MAC7D,MAAM,QACJ,cACE,YACA,iBACA,oBAAoB,eACtB,GACA,gBACA,cACA,EAAE,QAAQ,oBAAoB,gBAAgB,OAAO,CACvD;CAEF,OAAO;EACL;EACA;EACA;CACF;AACF"}