UNPKG

studiocms

Version:

Astro Native CMS for AstroDB. Built from the ground up by the Astro community.

99 lines (98 loc) 4.33 kB
import { and, eq } from "astro:db"; import { Effect, genLogger } from "../../../effect.js"; import { AstroDB, SDKCore_Generators } from "../effect/index.js"; import { tsAPIKeys, tsPermissions } from "../tables.js"; import { _clearLibSQLError } from "../utils.js"; class SDKCore_REST_API extends Effect.Service()( "studiocms/sdk/SDKCore/modules/rest_api", { dependencies: [AstroDB.Default, SDKCore_Generators.Default], effect: genLogger("studiocms/sdk/SDKCore/modules/rest_api/effect")(function* () { const [dbService, { generateToken }] = yield* Effect.all([AstroDB, SDKCore_Generators]); const REST_API = { tokens: { /** * Retrieves all API tokens for a specific user. * @param userId - The ID of the user whose tokens are to be retrieved. * @returns An Effect that resolves to an array of API keys for the user. * @throws {LibSQLDatabaseError} If a database error occurs during the operation. */ get: dbService.makeQuery( (ex, userId) => ex((db) => db.select().from(tsAPIKeys).where(eq(tsAPIKeys.userId, userId))).pipe( Effect.catchTags({ LibSQLClientError: (cause) => _clearLibSQLError("REST_API.tokens.get", cause) }) ) ), /** * Creates a new API token for a user with the specified description. * @param userId - The ID of the user for whom to create the token. * @param description - A description for the API key. * @returns An Effect that resolves to the created API key record. * @throws {LibSQLDatabaseError} If a database error occurs during the operation. */ new: (userId, description) => Effect.gen(function* () { const key = yield* generateToken(userId, true); return yield* dbService.execute( (db) => db.insert(tsAPIKeys).values({ id: crypto.randomUUID(), creationDate: /* @__PURE__ */ new Date(), userId, key, description }).returning().get() ); }).pipe( Effect.catchTags({ LibSQLClientError: (cause) => _clearLibSQLError("REST_API.tokens.new", cause) }) ), /** * Deletes an API token for a user by its ID. * @param userId - The ID of the user whose token is to be deleted. * @param tokenId - The ID of the API token to delete. * @returns An Effect that resolves when the token is successfully deleted. * @throws {LibSQLDatabaseError} If a database error occurs during the operation. */ delete: (userId, tokenId) => dbService.execute( (db) => db.delete(tsAPIKeys).where(and(eq(tsAPIKeys.userId, userId), eq(tsAPIKeys.id, tokenId))) ).pipe( Effect.catchTags({ LibSQLClientError: (cause) => _clearLibSQLError("REST_API.tokens.delete", cause) }) ), /** * Verifies an API key and retrieves the associated user ID and rank. * @param key - The API key to verify. * @returns An Effect that resolves to an object containing userId, key, and rank if valid, or false if invalid. * @throws {LibSQLDatabaseError} If a database error occurs during the verification. */ verify: (key) => Effect.gen(function* () { const apiKey = yield* dbService.execute( (db) => db.select().from(tsAPIKeys).where(eq(tsAPIKeys.key, key)).get() ); if (!apiKey) return false; const keyRank = yield* dbService.execute( (db) => db.select().from(tsPermissions).where(eq(tsPermissions.user, apiKey.userId)).get() ); if (!keyRank) return false; return { userId: apiKey.userId, key: apiKey.key, rank: keyRank.rank }; }).pipe( Effect.catchTags({ LibSQLClientError: (cause) => _clearLibSQLError("REST_API.tokens.verify", cause) }) ) } }; return REST_API; }) } ) { } export { SDKCore_REST_API };