better-auth
Version:
The most comprehensive authentication framework for TypeScript.
1 lines • 4.57 kB
Source Map (JSON)
{"version":3,"file":"index.mjs","names":[],"sources":["../../../src/plugins/open-api/index.ts"],"sourcesContent":["import type { BetterAuthPlugin, LiteralString } from \"@better-auth/core\";\nimport { createAuthEndpoint } from \"@better-auth/core/api\";\nimport { APIError } from \"../../api\";\nimport { HIDE_METADATA } from \"../../utils\";\nimport { generator } from \"./generator\";\nimport { logo } from \"./logo\";\n\nexport type { FieldSchema, OpenAPIModelSchema, Path } from \"./generator\";\n\ntype ScalarTheme =\n\t| \"alternate\"\n\t| \"default\"\n\t| \"moon\"\n\t| \"purple\"\n\t| \"solarized\"\n\t| \"bluePlanet\"\n\t| \"saturn\"\n\t| \"kepler\"\n\t| \"mars\"\n\t| \"deepSpace\"\n\t| \"laserwave\"\n\t| \"none\";\n\nconst getHTML = (\n\tapiReference: Record<string, any>,\n\ttheme?: ScalarTheme | undefined,\n\tnonce?: string | undefined,\n) => {\n\tconst nonceAttr = nonce ? `nonce=\"${nonce}\"` : \"\";\n\treturn `<!doctype html>\n<html>\n <head>\n <title>Scalar API Reference</title>\n <meta charset=\"utf-8\" />\n <meta\n name=\"viewport\"\n content=\"width=device-width, initial-scale=1\" />\n </head>\n <body>\n <script\n id=\"api-reference\"\n type=\"application/json\">\n ${JSON.stringify(apiReference)}\n </script>\n\t <script ${nonceAttr}>\n var configuration = {\n\t \tfavicon: \"data:image/svg+xml;utf8,${encodeURIComponent(logo)}\",\n\t \ttheme: \"${theme || \"default\"}\",\n metaData: {\n\t\t\ttitle: \"Better Auth API\",\n\t\t\tdescription: \"API Reference for your Better Auth Instance\",\n\t\t}\n }\n\n document.getElementById('api-reference').dataset.configuration =\n JSON.stringify(configuration)\n </script>\n\t <script src=\"https://cdn.jsdelivr.net/npm/@scalar/api-reference\" ${nonceAttr}></script>\n </body>\n</html>`;\n};\n\nexport interface OpenAPIOptions {\n\t/**\n\t * The path to the OpenAPI reference page\n\t *\n\t * keep in mind that this path will be appended to the base URL `/api/auth` path\n\t * by default, so if you set this to `/reference`, the full path will be `/api/auth/reference`\n\t *\n\t * @default \"/reference\"\n\t */\n\tpath?: LiteralString | undefined;\n\t/**\n\t * Disable the default reference page that is generated by Scalar\n\t *\n\t * @default false\n\t */\n\tdisableDefaultReference?: boolean | undefined;\n\t/**\n\t * Theme of the OpenAPI reference page.\n\t *\n\t * @default \"default\"\n\t */\n\ttheme?: ScalarTheme | undefined;\n\t/**\n\t * A 'nonce' string to be attached to inline scripts\n\t * for Content Security Policy (CSP) compliance.\n\t * @default undefined\n\t */\n\tnonce?: string | undefined;\n}\n\nexport const openAPI = <O extends OpenAPIOptions>(options?: O | undefined) => {\n\tconst path = (options?.path ?? \"/reference\") as \"/reference\";\n\treturn {\n\t\tid: \"open-api\",\n\t\tendpoints: {\n\t\t\tgenerateOpenAPISchema: createAuthEndpoint(\n\t\t\t\t\"/open-api/generate-schema\",\n\t\t\t\t{\n\t\t\t\t\tmethod: \"GET\",\n\t\t\t\t},\n\t\t\t\tasync (ctx) => {\n\t\t\t\t\tconst schema = await generator(ctx.context, ctx.context.options);\n\t\t\t\t\treturn ctx.json(schema);\n\t\t\t\t},\n\t\t\t),\n\t\t\topenAPIReference: createAuthEndpoint(\n\t\t\t\tpath,\n\t\t\t\t{\n\t\t\t\t\tmethod: \"GET\",\n\t\t\t\t\tmetadata: HIDE_METADATA,\n\t\t\t\t},\n\t\t\t\tasync (ctx) => {\n\t\t\t\t\tif (options?.disableDefaultReference) {\n\t\t\t\t\t\tthrow new APIError(\"NOT_FOUND\");\n\t\t\t\t\t}\n\t\t\t\t\tconst schema = await generator(ctx.context, ctx.context.options);\n\t\t\t\t\treturn new Response(getHTML(schema, options?.theme, options?.nonce), {\n\t\t\t\t\t\theaders: {\n\t\t\t\t\t\t\t\"Content-Type\": \"text/html\",\n\t\t\t\t\t\t},\n\t\t\t\t\t});\n\t\t\t\t},\n\t\t\t),\n\t\t},\n\t\toptions: options as NoInfer<O>,\n\t} satisfies BetterAuthPlugin;\n};\n\nexport type * from \"./generator\";\n"],"mappings":";;;;;;;;AAuBA,MAAM,WACL,cACA,OACA,UACI;CACJ,MAAM,YAAY,QAAQ,UAAU,MAAM,KAAK;AAC/C,QAAO;;;;;;;;;;;;;MAaF,KAAK,UAAU,aAAa,CAAC;;YAEvB,UAAU;;wCAEkB,mBAAmB,KAAK,CAAC;eAClD,SAAS,UAAU;;;;;;;;;;sEAUoC,UAAU;;;;AAmChF,MAAa,WAAqC,YAA4B;CAC7E,MAAM,OAAQ,SAAS,QAAQ;AAC/B,QAAO;EACN,IAAI;EACJ,WAAW;GACV,uBAAuB,mBACtB,6BACA,EACC,QAAQ,OACR,EACD,OAAO,QAAQ;IACd,MAAM,SAAS,MAAM,UAAU,IAAI,SAAS,IAAI,QAAQ,QAAQ;AAChE,WAAO,IAAI,KAAK,OAAO;KAExB;GACD,kBAAkB,mBACjB,MACA;IACC,QAAQ;IACR,UAAU;IACV,EACD,OAAO,QAAQ;AACd,QAAI,SAAS,wBACZ,OAAM,IAAI,SAAS,YAAY;IAEhC,MAAM,SAAS,MAAM,UAAU,IAAI,SAAS,IAAI,QAAQ,QAAQ;AAChE,WAAO,IAAI,SAAS,QAAQ,QAAQ,SAAS,OAAO,SAAS,MAAM,EAAE,EACpE,SAAS,EACR,gBAAgB,aAChB,EACD,CAAC;KAEH;GACD;EACQ;EACT"}