UNPKG

@microfox/fillout-oauth

Version:

TypeScript OAuth package for Fillout

93 lines (92 loc) 3.26 kB
// src/schemas/index.ts import { z } from "zod"; var filloutOAuthConfigSchema = z.object({ clientId: z.string().describe("The client ID for your Fillout application"), clientSecret: z.string().describe("The client secret for your Fillout application"), redirectUri: z.string().url().describe("The redirect URI for your Fillout application") }); var accessTokenResponseSchema = z.object({ access_token: z.string().describe("The access token for authenticating API requests"), base_url: z.string().url().describe("The base URL for the Fillout API") }); var authorizationCodeParamsSchema = z.object({ code: z.string().describe("The authorization code received from the authorization endpoint") }); var tokenRequestParamsSchema = z.object({ code: z.string().describe("The authorization code received from the authorization endpoint"), client_id: z.string().describe("The client ID for your Fillout application"), client_secret: z.string().describe("The client secret for your Fillout application"), redirect_uri: z.string().url().describe("The redirect URI for your Fillout application") }); // src/filloutOAuthSdk.ts var FilloutOAuthSdk = class { constructor(config) { this.config = filloutOAuthConfigSchema.parse(config); } /** * Generates the authorization URL for the OAuth flow. * @param state An optional state parameter for CSRF protection * @returns The authorization URL */ getAuthorizationUrl(state) { const params = new URLSearchParams({ client_id: this.config.clientId, redirect_uri: this.config.redirectUri }); if (state) { params.append("state", state); } return `https://build.fillout.com/authorize/oauth?${params.toString()}`; } /** * Exchanges an authorization code for an access token. * @param params The parameters required for the token request * @returns The access token response */ async getAccessToken(params) { const tokenParams = { code: params.code, client_id: this.config.clientId, client_secret: this.config.clientSecret, redirect_uri: this.config.redirectUri }; const response = await fetch("https://server.fillout.com/public/oauth/accessToken", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(tokenParams) }); if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); } const data = await response.json(); return accessTokenResponseSchema.parse(data); } /** * Validates the access token by making a request to the Fillout API. * @param accessToken The access token to validate * @returns A boolean indicating whether the token is valid */ async validateAccessToken(accessToken) { try { const response = await fetch("https://api.fillout.com/v1/api/me", { headers: { Authorization: `Bearer ${accessToken}` } }); return response.ok; } catch (error) { console.error("Error validating access token:", error); return false; } } }; function createFilloutOAuth(config) { return new FilloutOAuthSdk(config); } export { FilloutOAuthSdk, createFilloutOAuth }; //# sourceMappingURL=index.mjs.map