@microfox/fillout-oauth
Version:
TypeScript OAuth package for Fillout
121 lines (118 loc) • 4.51 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var index_exports = {};
__export(index_exports, {
FilloutOAuthSdk: () => FilloutOAuthSdk,
createFilloutOAuth: () => createFilloutOAuth
});
module.exports = __toCommonJS(index_exports);
// src/schemas/index.ts
var import_zod = require("zod");
var filloutOAuthConfigSchema = import_zod.z.object({
clientId: import_zod.z.string().describe("The client ID for your Fillout application"),
clientSecret: import_zod.z.string().describe("The client secret for your Fillout application"),
redirectUri: import_zod.z.string().url().describe("The redirect URI for your Fillout application")
});
var accessTokenResponseSchema = import_zod.z.object({
access_token: import_zod.z.string().describe("The access token for authenticating API requests"),
base_url: import_zod.z.string().url().describe("The base URL for the Fillout API")
});
var authorizationCodeParamsSchema = import_zod.z.object({
code: import_zod.z.string().describe("The authorization code received from the authorization endpoint")
});
var tokenRequestParamsSchema = import_zod.z.object({
code: import_zod.z.string().describe("The authorization code received from the authorization endpoint"),
client_id: import_zod.z.string().describe("The client ID for your Fillout application"),
client_secret: import_zod.z.string().describe("The client secret for your Fillout application"),
redirect_uri: import_zod.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);
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
FilloutOAuthSdk,
createFilloutOAuth
});
//# sourceMappingURL=index.js.map