UNPKG

astro-kinde

Version:

Astro integration module for Kinde authentication

282 lines (278 loc) 11.2 kB
/// <reference types="node" /> import { Accept, InputSchema, ActionClient } from '../actions/runtime/virtual/server.js'; import { AstroCookies } from '../core/cookies/index.js'; import { AstroDevToolbar, DevToolbarCanvas } from '../runtime/client/dev-toolbar/toolbar.js'; import { DevToolbarWindow, DevToolbarTooltip, DevToolbarHighlight, DevToolbarToggle, DevToolbarBadge, DevToolbarButton, DevToolbarIcon, DevToolbarCard, DevToolbarSelect, DevToolbarRadioCheckbox } from '../runtime/client/dev-toolbar/ui-library/index.js'; import { TransitionBeforePreparationEvent, TransitionBeforeSwapEvent } from '../transitions/events.js'; type Params = Record<string, string | undefined>; type ValidRedirectStatus = 300 | 301 | 302 | 303 | 304 | 307 | 308; interface AstroSharedContext<Props extends Record<string, any> = Record<string, any>, RouteParams extends Record<string, string | undefined> = Record<string, string | undefined>> { /** * The address (usually IP address) of the user. * * Throws an error if used within a static site, or within a prerendered page. */ clientAddress: string; /** * Utility for getting and setting the values of cookies. */ cookies: AstroCookies; /** * Information about the current request. This is a standard [Request](https://developer.mozilla.org/en-US/docs/Web/API/Request) object */ request: Request; /** * A full URL object of the request URL. */ url: URL; /** * Get action result on the server when using a form POST. */ getActionResult: <TAccept extends Accept, TInputSchema extends InputSchema<TAccept>, TAction extends ActionClient<unknown, TAccept, TInputSchema>>(action: TAction) => Awaited<ReturnType<TAction['safe']>> | undefined; /** * Route parameters for this request if this is a dynamic route. */ params: RouteParams; /** * List of props returned for this path by `getStaticPaths` (**Static Only**). */ props: Props; /** * Redirect to another page (**SSR Only**). */ redirect(path: string, status?: ValidRedirectStatus): Response; /** * It rewrites to another page. As opposed to redirects, the URL won't change, and Astro will render the HTML emitted * by the rerouted URL passed as argument. * * ## Example * * ```js * if (pageIsNotEnabled) { * return Astro.rewrite('/fallback-page') * } * ``` */ rewrite(rewritePayload: RewritePayload): Promise<Response>; /** * Object accessed via Astro middleware */ locals: App.Locals; /** * The current locale that is computed from the `Accept-Language` header of the browser (**SSR Only**). */ preferredLocale: string | undefined; /** * The list of locales computed from the `Accept-Language` header of the browser, sorted by quality value (**SSR Only**). */ preferredLocaleList: string[] | undefined; /** * The current locale computed from the URL of the request. It matches the locales in `i18n.locales`, and returns `undefined` otherwise. */ currentLocale: string | undefined; } /** * The `APIContext` is the object made available to endpoints and middleware. * It is a subset of the `Astro` global object available in pages. * * [Reference](https://docs.astro.build/en/reference/api-reference/#endpoint-context) */ interface APIContext<Props extends Record<string, any> = Record<string, any>, APIParams extends Record<string, string | undefined> = Record<string, string | undefined>> extends AstroSharedContext<Props, Params> { /** * The site provided in the astro config, parsed as an instance of `URL`, without base. * `undefined` if the site is not provided in the config. */ site: URL | undefined; /** * A human-readable string representing the Astro version used to create the project. * For example, `"Astro v1.1.1"`. */ generator: string; /** * The url of the current request, parsed as an instance of `URL`. * * Equivalent to: * ```ts * new URL(context.request.url) * ``` */ url: AstroSharedContext['url']; /** * Parameters matching the page’s dynamic route pattern. * In static builds, this will be the `params` generated by `getStaticPaths`. * In SSR builds, this can be any path segments matching the dynamic route pattern. * * Example usage: * ```ts * import type { APIContext } from "astro" * * export function getStaticPaths() { * return [ * { params: { id: '0' }, props: { name: 'Sarah' } }, * { params: { id: '1' }, props: { name: 'Chris' } }, * { params: { id: '2' }, props: { name: 'Fuzzy' } }, * ]; * } * * export async function GET({ params }: APIContext) { * return new Response(`Hello user ${params.id}!`) * } * ``` * * [Reference](https://docs.astro.build/en/reference/api-reference/#contextparams) */ params: AstroSharedContext<Props, APIParams>['params']; /** * List of props passed from `getStaticPaths`. Only available to static builds. * * Example usage: * ```ts * import type { APIContext } from "astro" * * export function getStaticPaths() { * return [ * { params: { id: '0' }, props: { name: 'Sarah' } }, * { params: { id: '1' }, props: { name: 'Chris' } }, * { params: { id: '2' }, props: { name: 'Fuzzy' } }, * ]; * } * * export function GET({ props }: APIContext): Response { * return new Response(`Hello ${props.name}!`); * } * ``` * * [Reference](https://docs.astro.build/en/guides/api-reference/#contextprops) */ props: AstroSharedContext<Props, APIParams>['props']; /** * Create a response that redirects to another page. * * Example usage: * ```ts * // src/pages/secret.ts * export function GET({ redirect }) { * return redirect('/login'); * } * ``` * * [Reference](https://docs.astro.build/en/guides/api-reference/#contextredirect) */ redirect: AstroSharedContext['redirect']; /** * It reroutes to another page. As opposed to redirects, the URL won't change, and Astro will render the HTML emitted * by the rerouted URL passed as argument. * * ## Example * * ```ts * // src/pages/secret.ts * export function GET(ctx) { * return ctx.rewrite(new URL("../"), ctx.url); * } * ``` */ rewrite: AstroSharedContext['rewrite']; /** * An object that middlewares can use to store extra information related to the request. * * It will be made available to pages as `Astro.locals`, and to endpoints as `context.locals`. * * Example usage: * * ```ts * // src/middleware.ts * import { defineMiddleware } from "astro:middleware"; * * export const onRequest = defineMiddleware((context, next) => { * context.locals.greeting = "Hello!"; * return next(); * }); * ``` * Inside a `.astro` file: * ```astro * --- * // src/pages/index.astro * const greeting = Astro.locals.greeting; * --- * <h1>{greeting}</h1> * ``` * * [Reference](https://docs.astro.build/en/reference/api-reference/#contextlocals) */ locals: App.Locals; /** * Available only when `i18n` configured and in SSR. * * It represents the preferred locale of the user. It's computed by checking the supported locales in `i18n.locales` * and locales supported by the users's browser via the header `Accept-Language` * * For example, given `i18n.locales` equals to `['fr', 'de']`, and the `Accept-Language` value equals to `en, de;q=0.2, fr;q=0.6`, the * `Astro.preferredLanguage` will be `fr` because `en` is not supported, its [quality value] is the highest. * * [quality value]: https://developer.mozilla.org/en-US/docs/Glossary/Quality_values */ preferredLocale: string | undefined; /** * Available only when `i18n` configured and in SSR. * * It represents the list of the preferred locales that are supported by the application. The list is sorted via [quality value]. * * For example, given `i18n.locales` equals to `['fr', 'pt', 'de']`, and the `Accept-Language` value equals to `en, de;q=0.2, fr;q=0.6`, the * `Astro.preferredLocaleList` will be equal to `['fs', 'de']` because `en` isn't supported, and `pt` isn't part of the locales contained in the * header. * * When the `Accept-Header` is `*`, the original `i18n.locales` are returned. The value `*` means no preferences, so Astro returns all the supported locales. * * [quality value]: https://developer.mozilla.org/en-US/docs/Glossary/Quality_values */ preferredLocaleList: string[] | undefined; /** * The current locale computed from the URL of the request. It matches the locales in `i18n.locales`, and returns `undefined` otherwise. */ currentLocale: string | undefined; } type RewritePayload = string | URL | Request; type MiddlewareNext = (rewritePayload?: RewritePayload) => Promise<Response>; type MiddlewareHandler = (context: APIContext, next: MiddlewareNext) => Promise<Response> | Response | Promise<void> | void; declare global { interface HTMLElementTagNameMap { 'astro-dev-toolbar': AstroDevToolbar; 'astro-dev-toolbar-window': DevToolbarWindow; 'astro-dev-toolbar-app-canvas': DevToolbarCanvas; 'astro-dev-toolbar-tooltip': DevToolbarTooltip; 'astro-dev-toolbar-highlight': DevToolbarHighlight; 'astro-dev-toolbar-toggle': DevToolbarToggle; 'astro-dev-toolbar-badge': DevToolbarBadge; 'astro-dev-toolbar-button': DevToolbarButton; 'astro-dev-toolbar-icon': DevToolbarIcon; 'astro-dev-toolbar-card': DevToolbarCard; 'astro-dev-toolbar-select': DevToolbarSelect; 'astro-dev-toolbar-radio-checkbox': DevToolbarRadioCheckbox; 'astro-dev-overlay': AstroDevToolbar; 'astro-dev-overlay-window': DevToolbarWindow; 'astro-dev-overlay-plugin-canvas': DevToolbarCanvas; 'astro-dev-overlay-tooltip': DevToolbarTooltip; 'astro-dev-overlay-highlight': DevToolbarHighlight; 'astro-dev-overlay-toggle': DevToolbarToggle; 'astro-dev-overlay-badge': DevToolbarBadge; 'astro-dev-overlay-button': DevToolbarButton; 'astro-dev-overlay-icon': DevToolbarIcon; 'astro-dev-overlay-card': DevToolbarCard; } namespace Config { type Database = Record<string, any>; } interface DocumentEventMap { 'astro:before-preparation': TransitionBeforePreparationEvent; 'astro:after-preparation': Event; 'astro:before-swap': TransitionBeforeSwapEvent; 'astro:after-swap': Event; 'astro:page-load': Event; } } /** * Middleware to handle authentication status and access token retrieval. */ declare const onRequest: MiddlewareHandler; export { onRequest };