UNPKG

better-auth

Version:

The most comprehensive authentication framework for TypeScript.

1 lines 3.66 kB
{"version":3,"file":"index.mjs","names":["resolvedHooks: Record<string, any>"],"sources":["../../../src/client/react/index.ts"],"sourcesContent":["import type {\n\tBetterAuthClientOptions,\n\tBetterAuthClientPlugin,\n} from \"@better-auth/core\";\nimport type { BASE_ERROR_CODES } from \"@better-auth/core/error\";\nimport { capitalizeFirstLetter } from \"@better-auth/core/utils\";\nimport type {\n\tBetterFetchError,\n\tBetterFetchResponse,\n} from \"@better-fetch/fetch\";\nimport type { PrettifyDeep, UnionToIntersection } from \"../../types/helper\";\nimport { getClientConfig } from \"../config\";\nimport { createDynamicPathProxy } from \"../proxy\";\nimport type {\n\tInferActions,\n\tInferClientAPI,\n\tInferErrorCodes,\n\tIsSignal,\n\tSessionQueryParams,\n} from \"../types\";\nimport { useStore } from \"./react-store\";\n\nfunction getAtomKey(str: string) {\n\treturn `use${capitalizeFirstLetter(str)}`;\n}\n\ntype InferResolvedHooks<O extends BetterAuthClientOptions> = O extends {\n\tplugins: Array<infer Plugin>;\n}\n\t? UnionToIntersection<\n\t\t\tPlugin extends BetterAuthClientPlugin\n\t\t\t\t? Plugin[\"getAtoms\"] extends (fetch: any) => infer Atoms\n\t\t\t\t\t? Atoms extends Record<string, any>\n\t\t\t\t\t\t? {\n\t\t\t\t\t\t\t\t[key in keyof Atoms as IsSignal<key> extends true\n\t\t\t\t\t\t\t\t\t? never\n\t\t\t\t\t\t\t\t\t: key extends string\n\t\t\t\t\t\t\t\t\t\t? `use${Capitalize<key>}`\n\t\t\t\t\t\t\t\t\t\t: never]: () => ReturnType<Atoms[key][\"get\"]>;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t: {}\n\t\t\t\t\t: {}\n\t\t\t\t: {}\n\t\t>\n\t: {};\n\nexport function createAuthClient<Option extends BetterAuthClientOptions>(\n\toptions?: Option | undefined,\n) {\n\tconst {\n\t\tpluginPathMethods,\n\t\tpluginsActions,\n\t\tpluginsAtoms,\n\t\t$fetch,\n\t\t$store,\n\t\tatomListeners,\n\t} = getClientConfig(options);\n\tlet resolvedHooks: Record<string, any> = {};\n\tfor (const [key, value] of Object.entries(pluginsAtoms)) {\n\t\tresolvedHooks[getAtomKey(key)] = () => useStore(value);\n\t}\n\n\tconst routes = {\n\t\t...pluginsActions,\n\t\t...resolvedHooks,\n\t\t$fetch,\n\t\t$store,\n\t};\n\tconst proxy = createDynamicPathProxy(\n\t\troutes,\n\t\t$fetch,\n\t\tpluginPathMethods,\n\t\tpluginsAtoms,\n\t\tatomListeners,\n\t);\n\n\ttype ClientAPI = InferClientAPI<Option>;\n\ttype Session = ClientAPI extends {\n\t\tgetSession: () => Promise<infer Res>;\n\t}\n\t\t? Res extends BetterFetchResponse<infer S>\n\t\t\t? S\n\t\t\t: Res\n\t\t: never;\n\treturn proxy as UnionToIntersection<InferResolvedHooks<Option>> &\n\t\tClientAPI &\n\t\tInferActions<Option> & {\n\t\t\tuseSession: () => {\n\t\t\t\tdata: Session;\n\t\t\t\tisPending: boolean;\n\t\t\t\tisRefetching: boolean;\n\t\t\t\terror: BetterFetchError | null;\n\t\t\t\trefetch: (\n\t\t\t\t\tqueryParams?: { query?: SessionQueryParams } | undefined,\n\t\t\t\t) => Promise<void>;\n\t\t\t};\n\t\t\t$Infer: {\n\t\t\t\tSession: NonNullable<Session>;\n\t\t\t};\n\t\t\t$fetch: typeof $fetch;\n\t\t\t$store: typeof $store;\n\t\t\t$ERROR_CODES: PrettifyDeep<\n\t\t\t\tInferErrorCodes<Option> & typeof BASE_ERROR_CODES\n\t\t\t>;\n\t\t};\n}\n\nexport { useStore };\nexport type * from \"@better-fetch/fetch\";\nexport type * from \"nanostores\";\nexport type * from \"../../types/helper\";\nexport type { UnionToIntersection } from \"../../types/helper\";\n"],"mappings":";;;;;;AAsBA,SAAS,WAAW,KAAa;AAChC,QAAO,MAAM,sBAAsB,IAAI;;AAuBxC,SAAgB,iBACf,SACC;CACD,MAAM,EACL,mBACA,gBACA,cACA,QACA,QACA,kBACG,gBAAgB,QAAQ;CAC5B,IAAIA,gBAAqC,EAAE;AAC3C,MAAK,MAAM,CAAC,KAAK,UAAU,OAAO,QAAQ,aAAa,CACtD,eAAc,WAAW,IAAI,UAAU,SAAS,MAAM;AAyBvD,QAhBc,uBANC;EACd,GAAG;EACH,GAAG;EACH;EACA;EACA,EAGA,QACA,mBACA,cACA,cACA"}